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