cavp_hmac_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_hmac_test processes a NIST CAVP HMAC test vector request file and emits
16 // the corresponding response.
17 
18 #include <stdlib.h>
19 
20 #include <openssl/crypto.h>
21 #include <openssl/hmac.h>
22 #include <openssl/span.h>
23 
24 #include "../crypto/test/file_test.h"
25 #include "../crypto/test/test_util.h"
26 #include "cavp_test_util.h"
27 
28 
29 static bool TestHMAC(FileTest *t, void *arg) {
30  std::string md_len_str;
31  if (!t->GetInstruction(&md_len_str, "L")) {
32  return false;
33  }
34  const size_t md_len = strtoul(md_len_str.c_str(), nullptr, 0);
35 
36  const EVP_MD *md;
37  switch (md_len) {
38  case 20:
39  md = EVP_sha1();
40  break;
41  case 28:
42  md = EVP_sha224();
43  break;
44  case 32:
45  md = EVP_sha256();
46  break;
47  case 48:
48  md = EVP_sha384();
49  break;
50  case 64:
51  md = EVP_sha512();
52  break;
53  default:
54  return false;
55  }
56 
57  std::string count_str, k_len_str, t_len_str;
58  std::vector<uint8_t> key, msg;
59  if (!t->GetAttribute(&count_str, "Count") ||
60  !t->GetAttribute(&k_len_str, "Klen") ||
61  !t->GetAttribute(&t_len_str, "Tlen") ||
62  !t->GetBytes(&key, "Key") ||
63  !t->GetBytes(&msg, "Msg")) {
64  return false;
65  }
66 
67  size_t k_len = strtoul(k_len_str.c_str(), nullptr, 0);
68  size_t t_len = strtoul(t_len_str.c_str(), nullptr, 0);
69  if (key.size() < k_len) {
70  return false;
71  }
72  unsigned out_len;
74  if (HMAC(md, key.data(), k_len, msg.data(), msg.size(), out, &out_len) ==
75  NULL) {
76  return false;
77  }
78 
79  if (out_len < t_len) {
80  return false;
81  }
82 
83  printf("%s", t->CurrentTestToString().c_str());
84  printf("Mac = %s\r\n\r\n",
86 
87  return true;
88 }
89 
90 static int usage(char *arg) {
91  fprintf(stderr, "usage: %s <test file>\n", arg);
92  return 1;
93 }
94 
95 int cavp_hmac_test_main(int argc, char **argv) {
96  if (argc != 2) {
97  return usage(argv[0]);
98  }
99 
101  opts.path = argv[1];
102  opts.callback = TestHMAC;
103  opts.silent = true;
104  opts.comment_callback = EchoComment;
105  return FileTestMain(opts);
106 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
EVP_sha512
const OPENSSL_EXPORT EVP_MD * EVP_sha512(void)
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
EVP_sha384
const OPENSSL_EXPORT EVP_MD * EVP_sha384(void)
TestHMAC
static bool TestHMAC(FileTest *t, void *arg)
Definition: cavp_hmac_test.cc:29
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
EVP_sha256
const OPENSSL_EXPORT EVP_MD * EVP_sha256(void)
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
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
usage
static int usage(char *arg)
Definition: cavp_hmac_test.cc:90
crypto.h
arg
Definition: cmdline.cc:40
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
benchmark.md
md
Definition: benchmark.py:86
key
const char * key
Definition: hpack_parser_table.cc:164
EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE
Definition: digest.h:156
HMAC
#define HMAC
Definition: boringssl_prefix_symbols.h:1783
EVP_sha1
const OPENSSL_EXPORT EVP_MD * EVP_sha1(void)
FileTest::Options
Definition: file_test.h:104
cavp_hmac_test_main
int cavp_hmac_test_main(int argc, char **argv)
Definition: cavp_hmac_test.cc:95
span.h
EVP_sha224
const OPENSSL_EXPORT EVP_MD * EVP_sha224(void)
hmac.h
absl::MakeConstSpan
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
Definition: abseil-cpp/absl/types/span.h:707


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