blob: 7278ceaf88bc9b1ea90cbb00bfbbe3ccc2fd9819 [file] [log] [blame]
Robert Sloan2424d842017-05-01 07:46:28 -07001/* 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
Robert Sloan8ff03552017-06-14 12:40:58 -070016// emits the corresponding response.
Robert Sloan2424d842017-05-01 07:46:28 -070017
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
Robert Sloan8ff03552017-06-14 12:40:58 -070029namespace {
30
Robert Sloan2424d842017-05-01 07:46:28 -070031struct TestCtx {
32 const EVP_AEAD *aead;
Robert Sloan2424d842017-05-01 07:46:28 -070033};
34
Robert Sloan8ff03552017-06-14 12:40:58 -070035}
36
Robert Sloan2424d842017-05-01 07:46:28 -070037static const EVP_AEAD *GetAEAD(const std::string &name, const bool enc) {
38 if (name == "aes-128-gcm") {
Robert Sloan8ff03552017-06-14 12:40:58 -070039 return EVP_aead_aes_128_gcm();
Robert Sloan2424d842017-05-01 07:46:28 -070040 } else if (name == "aes-256-gcm") {
Robert Sloan8ff03552017-06-14 12:40:58 -070041 return EVP_aead_aes_256_gcm();
Robert Sloan2424d842017-05-01 07:46:28 -070042 }
43 return nullptr;
44}
45
46static 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") ||
Robert Sloan8ff03552017-06-14 12:40:58 -070062 !t->GetBytes(&iv, "IV") ||
Robert Sloan2424d842017-05-01 07:46:28 -070063 !t->GetBytes(&pt, "PT") ||
Robert Sloan8ff03552017-06-14 12:40:58 -070064 !t->GetBytes(&aad, "AAD") ||
Robert Sloan2424d842017-05-01 07:46:28 -070065 key.size() * 8 != strtoul(key_len_str.c_str(), nullptr, 0) ||
Robert Sloan8ff03552017-06-14 12:40:58 -070066 iv.size() * 8 != strtoul(iv_len_str.c_str(), nullptr, 0) ||
Robert Sloan2424d842017-05-01 07:46:28 -070067 pt.size() * 8 != strtoul(pt_len_str.c_str(), nullptr, 0) ||
Robert Sloan8ff03552017-06-14 12:40:58 -070068 aad.size() * 8 != strtoul(aad_len_str.c_str(), nullptr, 0) ||
69 iv.size() != 12) {
Robert Sloan2424d842017-05-01 07:46:28 -070070 return false;
71 }
72
Robert Sloan8ff03552017-06-14 12:40:58 -070073 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)) {
Robert Sloan2424d842017-05-01 07:46:28 -070075 return false;
76 }
77 printf("%s", t->CurrentTestToString().c_str());
Robert Sloan2424d842017-05-01 07:46:28 -070078 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
Robert Sloan2424d842017-05-01 07:46:28 -070081 return true;
82}
83
84static 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 =
Robert Sloan8ff03552017-06-14 12:40:58 -0700118 AEADDecrypt(ctx->aead, &pt, pt_len, key, aad, ct, tag, iv);
Robert Sloan2424d842017-05-01 07:46:28 -0700119 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
Robert Sloan2424d842017-05-01 07:46:28 -0700125 return true;
126}
127
128static int usage(char *arg) {
Robert Sloan8ff03552017-06-14 12:40:58 -0700129 fprintf(stderr, "usage: %s (enc|dec) <cipher> <test file>\n", arg);
Robert Sloan2424d842017-05-01 07:46:28 -0700130 return 1;
131}
132
Robert Sloan8ff03552017-06-14 12:40:58 -0700133int cavp_aes_gcm_test_main(int argc, char **argv) {
134 if (argc != 4) {
Robert Sloan2424d842017-05-01 07:46:28 -0700135 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
Robert Sloan8ff03552017-06-14 12:40:58 -0700154 TestCtx ctx = {aead};
Robert Sloan2424d842017-05-01 07:46:28 -0700155
Robert Sloan8ff03552017-06-14 12:40:58 -0700156 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);
Robert Sloan2424d842017-05-01 07:46:28 -0700163}