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
TestHMAC(FileTest * t,void * arg)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;
73 uint8_t out[EVP_MAX_MD_SIZE];
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",
85 EncodeHex(bssl::MakeConstSpan(out, t_len)).c_str());
86
87 return true;
88 }
89
usage(char * arg)90 static int usage(char *arg) {
91 fprintf(stderr, "usage: %s <test file>\n", arg);
92 return 1;
93 }
94
cavp_hmac_test_main(int argc,char ** argv)95 int cavp_hmac_test_main(int argc, char **argv) {
96 if (argc != 2) {
97 return usage(argv[0]);
98 }
99
100 FileTest::Options opts;
101 opts.path = argv[1];
102 opts.callback = TestHMAC;
103 opts.silent = true;
104 opts.comment_callback = EchoComment;
105 return FileTestMain(opts);
106 }
107