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