]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/hmactest.c
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 * HMAC low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
20 #include "internal/nelem.h"
22 # include <openssl/hmac.h>
23 # include <openssl/sha.h>
24 # ifndef OPENSSL_NO_MD5
25 # include <openssl/md5.h>
28 # ifdef CHARSET_EBCDIC
29 # include <openssl/ebcdic.h>
34 # ifndef OPENSSL_NO_MD5
35 static struct test_st
{
38 const unsigned char data
[64];
43 "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
44 "e9139d1e6ee064ef8cf514fc7dc83e86",
47 "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
49 "9294727a3638bb1c13f48ef8158bfc9d",
52 "Jefe", 4, "what do ya want for nothing?", 28,
53 "750c783e6ab0b503eaa86e310a5db738",
56 "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
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, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
62 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
63 }, 50, "56be34521d144c88dbb8c733f0e8b3f6",
66 "", 0, "My test data", 12,
67 "61afdecb95429ef494d61fdee15990cabf0826fc"
70 "", 0, "My test data", 12,
71 "2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
74 "123456", 6, "My test data", 12,
75 "bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
78 "12345", 5, "My test data again", 18,
79 "a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
84 static char *pt(unsigned char *md
, unsigned int len
);
87 # ifndef OPENSSL_NO_MD5
88 static int test_hmac_md5(int idx
)
91 # ifdef CHARSET_EBCDIC
92 ebcdic2ascii(test
[0].data
, test
[0].data
, test
[0].data_len
);
93 ebcdic2ascii(test
[1].data
, test
[1].data
, test
[1].data_len
);
94 ebcdic2ascii(test
[2].key
, test
[2].key
, test
[2].key_len
);
95 ebcdic2ascii(test
[2].data
, test
[2].data
, test
[2].data_len
);
98 p
= pt(HMAC(EVP_md5(),
99 test
[idx
].key
, test
[idx
].key_len
,
100 test
[idx
].data
, test
[idx
].data_len
, NULL
, NULL
),
103 return TEST_ptr(p
) && TEST_str_eq(p
, test
[idx
].digest
);
107 static int test_hmac_bad(void)
109 HMAC_CTX
*ctx
= NULL
;
112 ctx
= HMAC_CTX_new();
114 || !TEST_ptr_null(HMAC_CTX_get_md(ctx
))
115 || !TEST_false(HMAC_Init_ex(ctx
, NULL
, 0, NULL
, NULL
))
116 || !TEST_false(HMAC_Update(ctx
, test
[4].data
, test
[4].data_len
))
117 || !TEST_false(HMAC_Init_ex(ctx
, NULL
, 0, EVP_sha1(), NULL
))
118 || !TEST_false(HMAC_Update(ctx
, test
[4].data
, test
[4].data_len
)))
127 static int test_hmac_run(void)
130 HMAC_CTX
*ctx
= NULL
;
131 unsigned char buf
[EVP_MAX_MD_SIZE
];
135 ctx
= HMAC_CTX_new();
139 || !TEST_ptr_null(HMAC_CTX_get_md(ctx
))
140 || !TEST_false(HMAC_Init_ex(ctx
, NULL
, 0, NULL
, NULL
))
141 || !TEST_false(HMAC_Update(ctx
, test
[4].data
, test
[4].data_len
))
142 || !TEST_false(HMAC_Init_ex(ctx
, test
[4].key
, -1, EVP_sha1(), NULL
)))
145 if (!TEST_true(HMAC_Init_ex(ctx
, test
[4].key
, test
[4].key_len
, EVP_sha1(), NULL
))
146 || !TEST_true(HMAC_Update(ctx
, test
[4].data
, test
[4].data_len
))
147 || !TEST_true(HMAC_Final(ctx
, buf
, &len
)))
151 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[4].digest
))
154 if (!TEST_false(HMAC_Init_ex(ctx
, NULL
, 0, EVP_sha256(), NULL
)))
157 if (!TEST_true(HMAC_Init_ex(ctx
, test
[5].key
, test
[5].key_len
, EVP_sha256(), NULL
))
158 || !TEST_ptr_eq(HMAC_CTX_get_md(ctx
), EVP_sha256())
159 || !TEST_true(HMAC_Update(ctx
, test
[5].data
, test
[5].data_len
))
160 || !TEST_true(HMAC_Final(ctx
, buf
, &len
)))
164 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[5].digest
))
167 if (!TEST_true(HMAC_Init_ex(ctx
, test
[6].key
, test
[6].key_len
, NULL
, NULL
))
168 || !TEST_true(HMAC_Update(ctx
, test
[6].data
, test
[6].data_len
))
169 || !TEST_true(HMAC_Final(ctx
, buf
, &len
)))
172 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[6].digest
))
175 /* Test reusing a key */
176 if (!TEST_true(HMAC_Init_ex(ctx
, NULL
, 0, NULL
, NULL
))
177 || !TEST_true(HMAC_Update(ctx
, test
[6].data
, test
[6].data_len
))
178 || !TEST_true(HMAC_Final(ctx
, buf
, &len
)))
181 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[6].digest
))
185 * Test reusing a key where the digest is provided again but is the same as
188 if (!TEST_true(HMAC_Init_ex(ctx
, NULL
, 0, EVP_sha256(), NULL
))
189 || !TEST_true(HMAC_Update(ctx
, test
[6].data
, test
[6].data_len
))
190 || !TEST_true(HMAC_Final(ctx
, buf
, &len
)))
193 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[6].digest
))
203 static int test_hmac_single_shot(void)
207 /* Test single-shot with NULL key. */
208 p
= pt(HMAC(EVP_sha1(), NULL
, 0, test
[4].data
, test
[4].data_len
,
209 NULL
, NULL
), SHA_DIGEST_LENGTH
);
210 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[4].digest
))
217 static int test_hmac_copy(void)
220 HMAC_CTX
*ctx
= NULL
, *ctx2
= NULL
;
221 unsigned char buf
[EVP_MAX_MD_SIZE
];
225 ctx
= HMAC_CTX_new();
226 ctx2
= HMAC_CTX_new();
227 if (!TEST_ptr(ctx
) || !TEST_ptr(ctx2
))
230 if (!TEST_true(HMAC_Init_ex(ctx
, test
[7].key
, test
[7].key_len
, EVP_sha1(), NULL
))
231 || !TEST_true(HMAC_Update(ctx
, test
[7].data
, test
[7].data_len
))
232 || !TEST_true(HMAC_CTX_copy(ctx2
, ctx
))
233 || !TEST_true(HMAC_Final(ctx2
, buf
, &len
)))
237 if (!TEST_ptr(p
) || !TEST_str_eq(p
, test
[7].digest
))
247 # ifndef OPENSSL_NO_MD5
248 static char *pt(unsigned char *md
, unsigned int len
)
255 for (i
= 0; i
< len
; i
++)
256 sprintf(&(buf
[i
* 2]), "%02x", md
[i
]);
261 int setup_tests(void)
263 ADD_ALL_TESTS(test_hmac_md5
, 4);
264 ADD_TEST(test_hmac_single_shot
);
265 ADD_TEST(test_hmac_bad
);
266 ADD_TEST(test_hmac_run
);
267 ADD_TEST(test_hmac_copy
);