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