cavp_aes_gcm_test.cc
Go to the documentation of this file.
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 "../crypto/test/test_util.h"
27 #include "cavp_test_util.h"
28 
29 
30 namespace {
31 
32 struct TestCtx {
33  const EVP_AEAD *aead;
34 };
35 
36 }
37 
38 static const EVP_AEAD *GetAEAD(const std::string &name, const bool enc) {
39  if (name == "aes-128-gcm") {
40  return EVP_aead_aes_128_gcm();
41  } else if (name == "aes-192-gcm") {
42  return EVP_aead_aes_192_gcm();
43  } else if (name == "aes-256-gcm") {
44  return EVP_aead_aes_256_gcm();
45  }
46  return nullptr;
47 }
48 
49 static bool TestAEADEncrypt(FileTest *t, void *arg) {
50  TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
51 
52  std::string key_len_str, iv_len_str, pt_len_str, aad_len_str, tag_len_str;
53  if (!t->GetInstruction(&key_len_str, "Keylen") ||
54  !t->GetInstruction(&iv_len_str, "IVlen") ||
55  !t->GetInstruction(&pt_len_str, "PTlen") ||
56  !t->GetInstruction(&aad_len_str, "AADlen") ||
57  !t->GetInstruction(&tag_len_str, "Taglen")) {
58  return false;
59  }
60 
62  std::vector<uint8_t> key, iv, pt, aad, tag, ct;
63  if (!t->GetAttribute(&count, "Count") ||
64  !t->GetBytes(&key, "Key") ||
65  !t->GetBytes(&iv, "IV") ||
66  !t->GetBytes(&pt, "PT") ||
67  !t->GetBytes(&aad, "AAD") ||
68  key.size() * 8 != strtoul(key_len_str.c_str(), nullptr, 0) ||
69  iv.size() * 8 != strtoul(iv_len_str.c_str(), nullptr, 0) ||
70  pt.size() * 8 != strtoul(pt_len_str.c_str(), nullptr, 0) ||
71  aad.size() * 8 != strtoul(aad_len_str.c_str(), nullptr, 0) ||
72  iv.size() != 12) {
73  return false;
74  }
75 
76  const size_t tag_len = strtoul(tag_len_str.c_str(), nullptr, 0) / 8;
77  if (!AEADEncrypt(ctx->aead, &ct, &tag, tag_len, key, pt, aad, iv)) {
78  return false;
79  }
80  printf("%s", t->CurrentTestToString().c_str());
81  printf("CT = %s\r\n", EncodeHex(ct).c_str());
82  printf("Tag = %s\r\n\r\n", EncodeHex(tag).c_str());
83 
84  return true;
85 }
86 
87 static bool TestAEADDecrypt(FileTest *t, void *arg) {
88  TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
89 
90  std::string key_len, iv_len, pt_len_str, aad_len_str, tag_len;
91  if (!t->GetInstruction(&key_len, "Keylen") ||
92  !t->GetInstruction(&iv_len, "IVlen") ||
93  !t->GetInstruction(&pt_len_str, "PTlen") ||
94  !t->GetInstruction(&aad_len_str, "AADlen") ||
95  !t->GetInstruction(&tag_len, "Taglen")) {
96  t->PrintLine("Invalid instruction block.");
97  return false;
98  }
99  size_t aad_len = strtoul(aad_len_str.c_str(), nullptr, 0) / 8;
100  size_t pt_len = strtoul(pt_len_str.c_str(), nullptr, 0) / 8;
101 
103  std::vector<uint8_t> key, iv, ct, aad, tag, pt;
104  if (!t->GetAttribute(&count, "Count") ||
105  !t->GetBytes(&key, "Key") ||
106  !t->GetBytes(&aad, "AAD") ||
107  !t->GetBytes(&tag, "Tag") ||
108  !t->GetBytes(&iv, "IV") ||
109  !t->GetBytes(&ct, "CT") ||
110  key.size() * 8 != strtoul(key_len.c_str(), nullptr, 0) ||
111  iv.size() * 8 != strtoul(iv_len.c_str(), nullptr, 0) ||
112  ct.size() != pt_len ||
113  aad.size() != aad_len ||
114  tag.size() * 8 != strtoul(tag_len.c_str(), nullptr, 0)) {
115  t->PrintLine("Invalid test case");
116  return false;
117  }
118 
119  printf("%s", t->CurrentTestToString().c_str());
120  bool aead_result =
121  AEADDecrypt(ctx->aead, &pt, pt_len, key, aad, ct, tag, iv);
122  if (aead_result) {
123  printf("PT = %s\r\n\r\n", EncodeHex(pt).c_str());
124  } else {
125  printf("FAIL\r\n\r\n");
126  }
127 
128  return true;
129 }
130 
131 static int usage(char *arg) {
132  fprintf(stderr, "usage: %s (enc|dec) <cipher> <test file>\n", arg);
133  return 1;
134 }
135 
136 int cavp_aes_gcm_test_main(int argc, char **argv) {
137  if (argc != 4) {
138  return usage(argv[0]);
139  }
140 
141  const std::string mode(argv[1]);
142  bool (*test_fn)(FileTest * t, void *arg);
143  if (mode == "enc") {
144  test_fn = &TestAEADEncrypt;
145  } else if (mode == "dec") {
146  test_fn = &TestAEADDecrypt;
147  } else {
148  return usage(argv[0]);
149  }
150 
151  const EVP_AEAD *aead = GetAEAD(argv[2], mode == "enc");
152  if (aead == nullptr) {
153  fprintf(stderr, "invalid aead: %s\n", argv[2]);
154  return 1;
155  }
156 
157  TestCtx ctx = {aead};
158 
160  opts.path = argv[3];
161  opts.callback = test_fn;
162  opts.arg = &ctx;
163  opts.silent = true;
164  opts.comment_callback = EchoComment;
165  return FileTestMain(opts);
166 }
GetAEAD
static const EVP_AEAD * GetAEAD(const std::string &name, const bool enc)
Definition: cavp_aes_gcm_test.cc:38
ctx
Definition: benchmark-async.c:30
bool
bool
Definition: setup_once.h:312
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
cstest_report.opts
opts
Definition: cstest_report.py:81
mode
const char int mode
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
setup.name
name
Definition: setup.py:542
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
TestAEADEncrypt
static bool TestAEADEncrypt(FileTest *t, void *arg)
Definition: cavp_aes_gcm_test.cc:49
EVP_aead_aes_256_gcm
const OPENSSL_EXPORT EVP_AEAD * EVP_aead_aes_256_gcm(void)
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
cavp_aes_gcm_test_main
int cavp_aes_gcm_test_main(int argc, char **argv)
Definition: cavp_aes_gcm_test.cc:136
cavp_test_util.h
EncodeHex
std::string EncodeHex(bssl::Span< const uint8_t > in)
Definition: boringssl-with-bazel/src/crypto/test/test_util.cc:75
FileTest
Definition: file_test.h:90
err.h
crypto.h
cipher.h
arg
Definition: cmdline.cc:40
TestAEADDecrypt
static bool TestAEADDecrypt(FileTest *t, void *arg)
Definition: cavp_aes_gcm_test.cc:87
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
aead.h
AEADDecrypt
bool AEADDecrypt(const EVP_AEAD *aead, std::vector< uint8_t > *pt, size_t pt_len, const std::vector< uint8_t > &key, const std::vector< uint8_t > &aad, const std::vector< uint8_t > &ct, const std::vector< uint8_t > &tag, const std::vector< uint8_t > &iv)
Definition: cavp_test_util.cc:125
evp_aead_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/cipher/internal.h:77
AEADEncrypt
bool AEADEncrypt(const EVP_AEAD *aead, std::vector< uint8_t > *ct, std::vector< uint8_t > *tag, size_t tag_len, const std::vector< uint8_t > &key, const std::vector< uint8_t > &pt, const std::vector< uint8_t > &aad, const std::vector< uint8_t > &iv)
Definition: cavp_test_util.cc:97
key
const char * key
Definition: hpack_parser_table.cc:164
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
usage
static int usage(char *arg)
Definition: cavp_aes_gcm_test.cc:131
EVP_aead_aes_192_gcm
const OPENSSL_EXPORT EVP_AEAD * EVP_aead_aes_192_gcm(void)
arg
struct arg arg
FileTest::Options
Definition: file_test.h:104
EVP_aead_aes_128_gcm
const OPENSSL_EXPORT EVP_AEAD * EVP_aead_aes_128_gcm(void)


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52