1 /* Copyright 2016 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <openssl/hmac.h>
10
11 #include "2sha.h"
12 #include "2hmac.h"
13 #include "common/tests.h"
14
15 const char short_key[] = "key";
16 const char message[] = "The quick brown fox jumps over the lazy dog";
17 /* This is supposed to be longer than the supported block sizes */
18 const char long_key[] =
19 "loooooooooooooooooooooooooooooooooooooooooooonooooooooooooooooooo"
20 "ooooooooooooooooooooooooooooooooooooooooooooonooooooooooooog key";
21
test_hmac_by_openssl(enum vb2_hash_algorithm alg,const void * key,uint32_t key_size,const void * msg,uint32_t msg_size)22 static void test_hmac_by_openssl(enum vb2_hash_algorithm alg,
23 const void *key, uint32_t key_size,
24 const void *msg, uint32_t msg_size)
25 {
26 struct vb2_hash mac;
27 uint8_t md[VB2_MAX_DIGEST_SIZE];
28 uint32_t md_size = sizeof(md);
29 char test_name[256];
30
31 switch (alg) {
32 case VB2_HASH_SHA1:
33 HMAC(EVP_sha1(), key, key_size, msg, msg_size, md, &md_size);
34 break;
35 case VB2_HASH_SHA224:
36 HMAC(EVP_sha224(), key, key_size, msg, msg_size, md, &md_size);
37 break;
38 case VB2_HASH_SHA256:
39 HMAC(EVP_sha256(), key, key_size, msg, msg_size, md, &md_size);
40 break;
41 case VB2_HASH_SHA384:
42 HMAC(EVP_sha384(), key, key_size, msg, msg_size, md, &md_size);
43 break;
44 case VB2_HASH_SHA512:
45 HMAC(EVP_sha512(), key, key_size, msg, msg_size, md, &md_size);
46 break;
47 default:
48 TEST_SUCC(-1, "Unsupported hash algorithm");
49 }
50 sprintf(test_name, "%s: HMAC-%s (key_size=%d)",
51 __func__, vb2_get_hash_algorithm_name(alg), key_size);
52 TEST_EQ(vb2_digest_size(alg), md_size, "HMAC size");
53 TEST_SUCC(vb2_hmac_calculate(false, alg, key, key_size, msg, msg_size, &mac),
54 test_name);
55 TEST_SUCC(memcmp(mac.raw, md, md_size), "HMAC digests match");
56 TEST_EQ(alg, mac.algo, "HMAC algo match");
57 }
58
test_hmac_error(void)59 static void test_hmac_error(void)
60 {
61 struct vb2_hash mac;
62 enum vb2_hash_algorithm alg;
63
64 alg = VB2_HASH_SHA1;
65 TEST_TRUE(vb2_hmac_calculate(false, alg, NULL, 0, message, strlen(message), &mac),
66 "key = NULL");
67 TEST_TRUE(vb2_hmac_calculate(false, alg, short_key, strlen(short_key), NULL, 0, &mac),
68 "msg = NULL");
69 TEST_TRUE(vb2_hmac_calculate(false, alg, short_key, strlen(short_key), message,
70 strlen(message), NULL),
71 "mac = NULL");
72 alg = -1;
73 TEST_TRUE(vb2_hmac_calculate(false, alg, short_key, strlen(short_key), message,
74 strlen(message), &mac),
75 "Invalid algorithm");
76 }
77
test_hmac(void)78 static void test_hmac(void)
79 {
80 int alg;
81
82 for (alg = 1; alg < VB2_HASH_ALG_COUNT; alg++) {
83 /* Try short key */
84 test_hmac_by_openssl(alg, short_key, strlen(short_key),
85 message, strlen(message));
86 /* Try key longer than a block size */
87 test_hmac_by_openssl(alg, long_key, strlen(long_key),
88 message, strlen(message));
89 /* Try empty key and message */
90 test_hmac_by_openssl(alg, "", 0, "", 0);
91 }
92 }
93
main(void)94 int main(void)
95 {
96 test_hmac();
97 test_hmac_error();
98
99 return gTestSuccess ? 0 : 255;
100 }
101