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_aes_gcm_test processes a NIST CAVP AES GCM test vector request file and
16 // emits the corresponding response.
17
18 #include <stdlib.h>
19
20 #include <openssl/aead.h>
21 #include <openssl/cipher.h>
22 #include <openssl/crypto.h>
23 #include <openssl/err.h>
24
25 #include "../crypto/test/file_test.h"
26 #include "cavp_test_util.h"
27
28
29 namespace {
30
31 struct TestCtx {
32 const EVP_AEAD *aead;
33 };
34
35 }
36
GetAEAD(const std::string & name,const bool enc)37 static const EVP_AEAD *GetAEAD(const std::string &name, const bool enc) {
38 if (name == "aes-128-gcm") {
39 return EVP_aead_aes_128_gcm();
40 } else if (name == "aes-256-gcm") {
41 return EVP_aead_aes_256_gcm();
42 }
43 return nullptr;
44 }
45
TestAEADEncrypt(FileTest * t,void * arg)46 static bool TestAEADEncrypt(FileTest *t, void *arg) {
47 TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
48
49 std::string key_len_str, iv_len_str, pt_len_str, aad_len_str, tag_len_str;
50 if (!t->GetInstruction(&key_len_str, "Keylen") ||
51 !t->GetInstruction(&iv_len_str, "IVlen") ||
52 !t->GetInstruction(&pt_len_str, "PTlen") ||
53 !t->GetInstruction(&aad_len_str, "AADlen") ||
54 !t->GetInstruction(&tag_len_str, "Taglen")) {
55 return false;
56 }
57
58 std::string count;
59 std::vector<uint8_t> key, iv, pt, aad, tag, ct;
60 if (!t->GetAttribute(&count, "Count") ||
61 !t->GetBytes(&key, "Key") ||
62 !t->GetBytes(&iv, "IV") ||
63 !t->GetBytes(&pt, "PT") ||
64 !t->GetBytes(&aad, "AAD") ||
65 key.size() * 8 != strtoul(key_len_str.c_str(), nullptr, 0) ||
66 iv.size() * 8 != strtoul(iv_len_str.c_str(), nullptr, 0) ||
67 pt.size() * 8 != strtoul(pt_len_str.c_str(), nullptr, 0) ||
68 aad.size() * 8 != strtoul(aad_len_str.c_str(), nullptr, 0) ||
69 iv.size() != 12) {
70 return false;
71 }
72
73 const size_t tag_len = strtoul(tag_len_str.c_str(), nullptr, 0) / 8;
74 if (!AEADEncrypt(ctx->aead, &ct, &tag, tag_len, key, pt, aad, iv)) {
75 return false;
76 }
77 printf("%s", t->CurrentTestToString().c_str());
78 printf("CT = %s\r\n", EncodeHex(ct.data(), ct.size()).c_str());
79 printf("Tag = %s\r\n\r\n", EncodeHex(tag.data(), tag.size()).c_str());
80
81 return true;
82 }
83
TestAEADDecrypt(FileTest * t,void * arg)84 static bool TestAEADDecrypt(FileTest *t, void *arg) {
85 TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
86
87 std::string key_len, iv_len, pt_len_str, aad_len_str, tag_len;
88 if (!t->GetInstruction(&key_len, "Keylen") ||
89 !t->GetInstruction(&iv_len, "IVlen") ||
90 !t->GetInstruction(&pt_len_str, "PTlen") ||
91 !t->GetInstruction(&aad_len_str, "AADlen") ||
92 !t->GetInstruction(&tag_len, "Taglen")) {
93 t->PrintLine("Invalid instruction block.");
94 return false;
95 }
96 size_t aad_len = strtoul(aad_len_str.c_str(), nullptr, 0) / 8;
97 size_t pt_len = strtoul(pt_len_str.c_str(), nullptr, 0) / 8;
98
99 std::string count;
100 std::vector<uint8_t> key, iv, ct, aad, tag, pt;
101 if (!t->GetAttribute(&count, "Count") ||
102 !t->GetBytes(&key, "Key") ||
103 !t->GetBytes(&aad, "AAD") ||
104 !t->GetBytes(&tag, "Tag") ||
105 !t->GetBytes(&iv, "IV") ||
106 !t->GetBytes(&ct, "CT") ||
107 key.size() * 8 != strtoul(key_len.c_str(), nullptr, 0) ||
108 iv.size() * 8 != strtoul(iv_len.c_str(), nullptr, 0) ||
109 ct.size() != pt_len ||
110 aad.size() != aad_len ||
111 tag.size() * 8 != strtoul(tag_len.c_str(), nullptr, 0)) {
112 t->PrintLine("Invalid test case");
113 return false;
114 }
115
116 printf("%s", t->CurrentTestToString().c_str());
117 bool aead_result =
118 AEADDecrypt(ctx->aead, &pt, pt_len, key, aad, ct, tag, iv);
119 if (aead_result) {
120 printf("PT = %s\r\n\r\n", EncodeHex(pt.data(), pt.size()).c_str());
121 } else {
122 printf("FAIL\r\n\r\n");
123 }
124
125 return true;
126 }
127
usage(char * arg)128 static int usage(char *arg) {
129 fprintf(stderr, "usage: %s (enc|dec) <cipher> <test file>\n", arg);
130 return 1;
131 }
132
cavp_aes_gcm_test_main(int argc,char ** argv)133 int cavp_aes_gcm_test_main(int argc, char **argv) {
134 if (argc != 4) {
135 return usage(argv[0]);
136 }
137
138 const std::string mode(argv[1]);
139 bool (*test_fn)(FileTest * t, void *arg);
140 if (mode == "enc") {
141 test_fn = &TestAEADEncrypt;
142 } else if (mode == "dec") {
143 test_fn = &TestAEADDecrypt;
144 } else {
145 return usage(argv[0]);
146 }
147
148 const EVP_AEAD *aead = GetAEAD(argv[2], mode == "enc");
149 if (aead == nullptr) {
150 fprintf(stderr, "invalid aead: %s\n", argv[2]);
151 return 1;
152 }
153
154 TestCtx ctx = {aead};
155
156 FileTest::Options opts;
157 opts.path = argv[3];
158 opts.callback = test_fn;
159 opts.arg = &ctx;
160 opts.silent = true;
161 opts.comment_callback = EchoComment;
162 return FileTestMain(opts);
163 }
164