1 /* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 // cavp_keywrap_test processes a NIST CAVP AES test vector request file and
16 // emits the corresponding response.
17
18 #include <stdlib.h>
19
20 #include <openssl/aes.h>
21 #include <openssl/crypto.h>
22
23 #include "../crypto/test/file_test.h"
24 #include "cavp_test_util.h"
25
26
27 namespace {
28
29 struct TestCtx {
30 bool encrypt;
31 };
32
33 }
34
AESKeyWrap(std::vector<uint8_t> * out,bool encrypt,const std::vector<uint8_t> & key,const std::vector<uint8_t> & in)35 static bool AESKeyWrap(std::vector<uint8_t> *out, bool encrypt,
36 const std::vector<uint8_t> &key,
37 const std::vector<uint8_t> &in) {
38 size_t key_bits = key.size() * 8;
39 if (key_bits != 128 && key_bits != 256) {
40 return false;
41 }
42 AES_KEY aes_key;
43
44 if (encrypt) {
45 out->resize(in.size() + 8);
46 if (AES_set_encrypt_key(key.data(), key_bits, &aes_key) ||
47 AES_wrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
48 -1) {
49 return false;
50 }
51 } else {
52 out->resize(in.size() - 8);
53 if (AES_set_decrypt_key(key.data(), key_bits, &aes_key) ||
54 AES_unwrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
55 -1) {
56 return false;
57 }
58 }
59
60 return true;
61 }
62
TestCipher(FileTest * t,void * arg)63 static bool TestCipher(FileTest *t, void *arg) {
64 TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
65
66 std::string count, unused, in_label = ctx->encrypt ? "P" : "C",
67 result_label = ctx->encrypt ? "C" : "P";
68 std::vector<uint8_t> key, in, result;
69 // clang-format off
70 if (!t->GetInstruction(&unused, "PLAINTEXT LENGTH") ||
71 !t->GetAttribute(&count, "COUNT") ||
72 !t->GetBytes(&key, "K") ||
73 !t->GetBytes(&in, in_label)) {
74 return false;
75 }
76 // clang-format on
77
78 printf("%s", t->CurrentTestToString().c_str());
79 if (!AESKeyWrap(&result, ctx->encrypt, key, in)) {
80 if (ctx->encrypt) {
81 return false;
82 } else {
83 printf("FAIL\r\n\r\n");
84 }
85 } else {
86 printf("%s = %s\r\n\r\n", result_label.c_str(),
87 EncodeHex(result.data(), result.size()).c_str());
88 }
89
90 return true;
91 }
92
usage(char * arg)93 static int usage(char *arg) {
94 fprintf(
95 stderr,
96 "usage: %s (enc|dec) (128|256) <test file>\n",
97 arg);
98 return 1;
99 }
100
cavp_keywrap_test_main(int argc,char ** argv)101 int cavp_keywrap_test_main(int argc, char **argv) {
102 if (argc != 4) {
103 return usage(argv[0]);
104 }
105
106 const std::string op(argv[1]);
107 bool encrypt;
108 if (op == "enc") {
109 encrypt = true;
110 } else if (op == "dec") {
111 encrypt = false;
112 } else {
113 return usage(argv[0]);
114 }
115
116 TestCtx ctx = {encrypt};
117
118 FileTest::Options opts;
119 opts.path = argv[3];
120 opts.callback = TestCipher;
121 opts.arg = &ctx;
122 opts.silent = true;
123 opts.comment_callback = EchoComment;
124 return FileTestMain(opts);
125 }
126