1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * HMAC low level APIs are deprecated for public use, but still ok for internal
12*e0c4386eSCy Schubert * use.
13*e0c4386eSCy Schubert */
14*e0c4386eSCy Schubert #include "internal/deprecated.h"
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <string.h>
18*e0c4386eSCy Schubert #include <stdlib.h>
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubert #include "internal/nelem.h"
21*e0c4386eSCy Schubert
22*e0c4386eSCy Schubert # include <openssl/hmac.h>
23*e0c4386eSCy Schubert # include <openssl/sha.h>
24*e0c4386eSCy Schubert # ifndef OPENSSL_NO_MD5
25*e0c4386eSCy Schubert # include <openssl/md5.h>
26*e0c4386eSCy Schubert # endif
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert # ifdef CHARSET_EBCDIC
29*e0c4386eSCy Schubert # include <openssl/ebcdic.h>
30*e0c4386eSCy Schubert # endif
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubert #include "testutil.h"
33*e0c4386eSCy Schubert
34*e0c4386eSCy Schubert # ifndef OPENSSL_NO_MD5
35*e0c4386eSCy Schubert static struct test_st {
36*e0c4386eSCy Schubert const char key[16];
37*e0c4386eSCy Schubert int key_len;
38*e0c4386eSCy Schubert const unsigned char data[64];
39*e0c4386eSCy Schubert int data_len;
40*e0c4386eSCy Schubert const char *digest;
41*e0c4386eSCy Schubert } test[8] = {
42*e0c4386eSCy Schubert {
43*e0c4386eSCy Schubert "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
44*e0c4386eSCy Schubert "e9139d1e6ee064ef8cf514fc7dc83e86",
45*e0c4386eSCy Schubert },
46*e0c4386eSCy Schubert {
47*e0c4386eSCy Schubert "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
48*e0c4386eSCy Schubert 16, "Hi There", 8,
49*e0c4386eSCy Schubert "9294727a3638bb1c13f48ef8158bfc9d",
50*e0c4386eSCy Schubert },
51*e0c4386eSCy Schubert {
52*e0c4386eSCy Schubert "Jefe", 4, "what do ya want for nothing?", 28,
53*e0c4386eSCy Schubert "750c783e6ab0b503eaa86e310a5db738",
54*e0c4386eSCy Schubert },
55*e0c4386eSCy Schubert {
56*e0c4386eSCy Schubert "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
57*e0c4386eSCy Schubert 16, {
58*e0c4386eSCy Schubert 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
59*e0c4386eSCy Schubert 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
60*e0c4386eSCy Schubert 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
61*e0c4386eSCy Schubert 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
62*e0c4386eSCy Schubert 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
63*e0c4386eSCy Schubert }, 50, "56be34521d144c88dbb8c733f0e8b3f6",
64*e0c4386eSCy Schubert },
65*e0c4386eSCy Schubert {
66*e0c4386eSCy Schubert "", 0, "My test data", 12,
67*e0c4386eSCy Schubert "61afdecb95429ef494d61fdee15990cabf0826fc"
68*e0c4386eSCy Schubert },
69*e0c4386eSCy Schubert {
70*e0c4386eSCy Schubert "", 0, "My test data", 12,
71*e0c4386eSCy Schubert "2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
72*e0c4386eSCy Schubert },
73*e0c4386eSCy Schubert {
74*e0c4386eSCy Schubert "123456", 6, "My test data", 12,
75*e0c4386eSCy Schubert "bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
76*e0c4386eSCy Schubert },
77*e0c4386eSCy Schubert {
78*e0c4386eSCy Schubert "12345", 5, "My test data again", 18,
79*e0c4386eSCy Schubert "a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
80*e0c4386eSCy Schubert }
81*e0c4386eSCy Schubert };
82*e0c4386eSCy Schubert # endif
83*e0c4386eSCy Schubert
84*e0c4386eSCy Schubert static char *pt(unsigned char *md, unsigned int len);
85*e0c4386eSCy Schubert
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubert # ifndef OPENSSL_NO_MD5
test_hmac_md5(int idx)88*e0c4386eSCy Schubert static int test_hmac_md5(int idx)
89*e0c4386eSCy Schubert {
90*e0c4386eSCy Schubert char *p;
91*e0c4386eSCy Schubert # ifdef CHARSET_EBCDIC
92*e0c4386eSCy Schubert ebcdic2ascii(test[0].data, test[0].data, test[0].data_len);
93*e0c4386eSCy Schubert ebcdic2ascii(test[1].data, test[1].data, test[1].data_len);
94*e0c4386eSCy Schubert ebcdic2ascii(test[2].key, test[2].key, test[2].key_len);
95*e0c4386eSCy Schubert ebcdic2ascii(test[2].data, test[2].data, test[2].data_len);
96*e0c4386eSCy Schubert # endif
97*e0c4386eSCy Schubert
98*e0c4386eSCy Schubert p = pt(HMAC(EVP_md5(),
99*e0c4386eSCy Schubert test[idx].key, test[idx].key_len,
100*e0c4386eSCy Schubert test[idx].data, test[idx].data_len, NULL, NULL),
101*e0c4386eSCy Schubert MD5_DIGEST_LENGTH);
102*e0c4386eSCy Schubert
103*e0c4386eSCy Schubert return TEST_ptr(p) && TEST_str_eq(p, test[idx].digest);
104*e0c4386eSCy Schubert }
105*e0c4386eSCy Schubert # endif
106*e0c4386eSCy Schubert
test_hmac_bad(void)107*e0c4386eSCy Schubert static int test_hmac_bad(void)
108*e0c4386eSCy Schubert {
109*e0c4386eSCy Schubert HMAC_CTX *ctx = NULL;
110*e0c4386eSCy Schubert int ret = 0;
111*e0c4386eSCy Schubert
112*e0c4386eSCy Schubert ctx = HMAC_CTX_new();
113*e0c4386eSCy Schubert if (!TEST_ptr(ctx)
114*e0c4386eSCy Schubert || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
115*e0c4386eSCy Schubert || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
116*e0c4386eSCy Schubert || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
117*e0c4386eSCy Schubert || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL))
118*e0c4386eSCy Schubert || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len)))
119*e0c4386eSCy Schubert goto err;
120*e0c4386eSCy Schubert
121*e0c4386eSCy Schubert ret = 1;
122*e0c4386eSCy Schubert err:
123*e0c4386eSCy Schubert HMAC_CTX_free(ctx);
124*e0c4386eSCy Schubert return ret;
125*e0c4386eSCy Schubert }
126*e0c4386eSCy Schubert
test_hmac_run(void)127*e0c4386eSCy Schubert static int test_hmac_run(void)
128*e0c4386eSCy Schubert {
129*e0c4386eSCy Schubert char *p;
130*e0c4386eSCy Schubert HMAC_CTX *ctx = NULL;
131*e0c4386eSCy Schubert unsigned char buf[EVP_MAX_MD_SIZE];
132*e0c4386eSCy Schubert unsigned int len;
133*e0c4386eSCy Schubert int ret = 0;
134*e0c4386eSCy Schubert
135*e0c4386eSCy Schubert if (!TEST_ptr(ctx = HMAC_CTX_new()))
136*e0c4386eSCy Schubert return 0;
137*e0c4386eSCy Schubert HMAC_CTX_reset(ctx);
138*e0c4386eSCy Schubert
139*e0c4386eSCy Schubert if (!TEST_ptr(ctx)
140*e0c4386eSCy Schubert || !TEST_ptr_null(HMAC_CTX_get_md(ctx))
141*e0c4386eSCy Schubert || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
142*e0c4386eSCy Schubert || !TEST_false(HMAC_Update(ctx, test[4].data, test[4].data_len))
143*e0c4386eSCy Schubert || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)))
144*e0c4386eSCy Schubert goto err;
145*e0c4386eSCy Schubert
146*e0c4386eSCy Schubert if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL))
147*e0c4386eSCy Schubert || !TEST_true(HMAC_Update(ctx, test[4].data, test[4].data_len))
148*e0c4386eSCy Schubert || !TEST_true(HMAC_Final(ctx, buf, &len)))
149*e0c4386eSCy Schubert goto err;
150*e0c4386eSCy Schubert
151*e0c4386eSCy Schubert p = pt(buf, len);
152*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest))
153*e0c4386eSCy Schubert goto err;
154*e0c4386eSCy Schubert
155*e0c4386eSCy Schubert if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)))
156*e0c4386eSCy Schubert goto err;
157*e0c4386eSCy Schubert
158*e0c4386eSCy Schubert if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL))
159*e0c4386eSCy Schubert || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256())
160*e0c4386eSCy Schubert || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len))
161*e0c4386eSCy Schubert || !TEST_true(HMAC_Final(ctx, buf, &len)))
162*e0c4386eSCy Schubert goto err;
163*e0c4386eSCy Schubert
164*e0c4386eSCy Schubert p = pt(buf, len);
165*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[5].digest))
166*e0c4386eSCy Schubert goto err;
167*e0c4386eSCy Schubert
168*e0c4386eSCy Schubert if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL))
169*e0c4386eSCy Schubert || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
170*e0c4386eSCy Schubert || !TEST_true(HMAC_Final(ctx, buf, &len)))
171*e0c4386eSCy Schubert goto err;
172*e0c4386eSCy Schubert p = pt(buf, len);
173*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
174*e0c4386eSCy Schubert goto err;
175*e0c4386eSCy Schubert
176*e0c4386eSCy Schubert /* Test reusing a key */
177*e0c4386eSCy Schubert if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
178*e0c4386eSCy Schubert || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
179*e0c4386eSCy Schubert || !TEST_true(HMAC_Final(ctx, buf, &len)))
180*e0c4386eSCy Schubert goto err;
181*e0c4386eSCy Schubert p = pt(buf, len);
182*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
183*e0c4386eSCy Schubert goto err;
184*e0c4386eSCy Schubert
185*e0c4386eSCy Schubert /*
186*e0c4386eSCy Schubert * Test reusing a key where the digest is provided again but is the same as
187*e0c4386eSCy Schubert * last time
188*e0c4386eSCy Schubert */
189*e0c4386eSCy Schubert if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL))
190*e0c4386eSCy Schubert || !TEST_true(HMAC_Update(ctx, test[6].data, test[6].data_len))
191*e0c4386eSCy Schubert || !TEST_true(HMAC_Final(ctx, buf, &len)))
192*e0c4386eSCy Schubert goto err;
193*e0c4386eSCy Schubert p = pt(buf, len);
194*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest))
195*e0c4386eSCy Schubert goto err;
196*e0c4386eSCy Schubert
197*e0c4386eSCy Schubert ret = 1;
198*e0c4386eSCy Schubert err:
199*e0c4386eSCy Schubert HMAC_CTX_free(ctx);
200*e0c4386eSCy Schubert return ret;
201*e0c4386eSCy Schubert }
202*e0c4386eSCy Schubert
203*e0c4386eSCy Schubert
test_hmac_single_shot(void)204*e0c4386eSCy Schubert static int test_hmac_single_shot(void)
205*e0c4386eSCy Schubert {
206*e0c4386eSCy Schubert char *p;
207*e0c4386eSCy Schubert
208*e0c4386eSCy Schubert /* Test single-shot with NULL key. */
209*e0c4386eSCy Schubert p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
210*e0c4386eSCy Schubert NULL, NULL), SHA_DIGEST_LENGTH);
211*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest))
212*e0c4386eSCy Schubert return 0;
213*e0c4386eSCy Schubert
214*e0c4386eSCy Schubert return 1;
215*e0c4386eSCy Schubert }
216*e0c4386eSCy Schubert
217*e0c4386eSCy Schubert
test_hmac_copy(void)218*e0c4386eSCy Schubert static int test_hmac_copy(void)
219*e0c4386eSCy Schubert {
220*e0c4386eSCy Schubert char *p;
221*e0c4386eSCy Schubert HMAC_CTX *ctx = NULL, *ctx2 = NULL;
222*e0c4386eSCy Schubert unsigned char buf[EVP_MAX_MD_SIZE];
223*e0c4386eSCy Schubert unsigned int len;
224*e0c4386eSCy Schubert int ret = 0;
225*e0c4386eSCy Schubert
226*e0c4386eSCy Schubert ctx = HMAC_CTX_new();
227*e0c4386eSCy Schubert ctx2 = HMAC_CTX_new();
228*e0c4386eSCy Schubert if (!TEST_ptr(ctx) || !TEST_ptr(ctx2))
229*e0c4386eSCy Schubert goto err;
230*e0c4386eSCy Schubert
231*e0c4386eSCy Schubert if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL))
232*e0c4386eSCy Schubert || !TEST_true(HMAC_Update(ctx, test[7].data, test[7].data_len))
233*e0c4386eSCy Schubert || !TEST_true(HMAC_CTX_copy(ctx2, ctx))
234*e0c4386eSCy Schubert || !TEST_true(HMAC_Final(ctx2, buf, &len)))
235*e0c4386eSCy Schubert goto err;
236*e0c4386eSCy Schubert
237*e0c4386eSCy Schubert p = pt(buf, len);
238*e0c4386eSCy Schubert if (!TEST_ptr(p) || !TEST_str_eq(p, test[7].digest))
239*e0c4386eSCy Schubert goto err;
240*e0c4386eSCy Schubert
241*e0c4386eSCy Schubert ret = 1;
242*e0c4386eSCy Schubert err:
243*e0c4386eSCy Schubert HMAC_CTX_free(ctx2);
244*e0c4386eSCy Schubert HMAC_CTX_free(ctx);
245*e0c4386eSCy Schubert return ret;
246*e0c4386eSCy Schubert }
247*e0c4386eSCy Schubert
test_hmac_copy_uninited(void)248*e0c4386eSCy Schubert static int test_hmac_copy_uninited(void)
249*e0c4386eSCy Schubert {
250*e0c4386eSCy Schubert const unsigned char key[24] = {0};
251*e0c4386eSCy Schubert const unsigned char ct[166] = {0};
252*e0c4386eSCy Schubert EVP_PKEY *pkey = NULL;
253*e0c4386eSCy Schubert EVP_MD_CTX *ctx = NULL;
254*e0c4386eSCy Schubert EVP_MD_CTX *ctx_tmp = NULL;
255*e0c4386eSCy Schubert int res = 0;
256*e0c4386eSCy Schubert
257*e0c4386eSCy Schubert if (!TEST_ptr(ctx = EVP_MD_CTX_new())
258*e0c4386eSCy Schubert || !TEST_ptr(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
259*e0c4386eSCy Schubert key, sizeof(key)))
260*e0c4386eSCy Schubert || !TEST_true(EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey))
261*e0c4386eSCy Schubert || !TEST_ptr(ctx_tmp = EVP_MD_CTX_new())
262*e0c4386eSCy Schubert || !TEST_true(EVP_MD_CTX_copy(ctx_tmp, ctx)))
263*e0c4386eSCy Schubert goto err;
264*e0c4386eSCy Schubert EVP_MD_CTX_free(ctx);
265*e0c4386eSCy Schubert ctx = ctx_tmp;
266*e0c4386eSCy Schubert ctx_tmp = NULL;
267*e0c4386eSCy Schubert
268*e0c4386eSCy Schubert if (!TEST_true(EVP_DigestSignUpdate(ctx, ct, sizeof(ct))))
269*e0c4386eSCy Schubert goto err;
270*e0c4386eSCy Schubert res = 1;
271*e0c4386eSCy Schubert err:
272*e0c4386eSCy Schubert EVP_MD_CTX_free(ctx);
273*e0c4386eSCy Schubert EVP_MD_CTX_free(ctx_tmp);
274*e0c4386eSCy Schubert EVP_PKEY_free(pkey);
275*e0c4386eSCy Schubert return res;
276*e0c4386eSCy Schubert }
277*e0c4386eSCy Schubert
278*e0c4386eSCy Schubert # ifndef OPENSSL_NO_MD5
pt(unsigned char * md,unsigned int len)279*e0c4386eSCy Schubert static char *pt(unsigned char *md, unsigned int len)
280*e0c4386eSCy Schubert {
281*e0c4386eSCy Schubert unsigned int i;
282*e0c4386eSCy Schubert static char buf[80];
283*e0c4386eSCy Schubert
284*e0c4386eSCy Schubert if (md == NULL)
285*e0c4386eSCy Schubert return NULL;
286*e0c4386eSCy Schubert for (i = 0; i < len; i++)
287*e0c4386eSCy Schubert sprintf(&(buf[i * 2]), "%02x", md[i]);
288*e0c4386eSCy Schubert return buf;
289*e0c4386eSCy Schubert }
290*e0c4386eSCy Schubert # endif
291*e0c4386eSCy Schubert
setup_tests(void)292*e0c4386eSCy Schubert int setup_tests(void)
293*e0c4386eSCy Schubert {
294*e0c4386eSCy Schubert ADD_ALL_TESTS(test_hmac_md5, 4);
295*e0c4386eSCy Schubert ADD_TEST(test_hmac_single_shot);
296*e0c4386eSCy Schubert ADD_TEST(test_hmac_bad);
297*e0c4386eSCy Schubert ADD_TEST(test_hmac_run);
298*e0c4386eSCy Schubert ADD_TEST(test_hmac_copy);
299*e0c4386eSCy Schubert ADD_TEST(test_hmac_copy_uninited);
300*e0c4386eSCy Schubert return 1;
301*e0c4386eSCy Schubert }
302*e0c4386eSCy Schubert
303