2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
16 # include <openssl/hmac.h>
17 # include <openssl/sha.h>
18 # ifndef OPENSSL_NO_MD5
19 # include <openssl/md5.h>
22 # ifdef CHARSET_EBCDIC
23 # include <openssl/ebcdic.h>
26 # ifndef OPENSSL_NO_MD5
27 static struct test_st {
28 unsigned char key[16];
30 unsigned char data[64];
32 unsigned char *digest;
35 "", 0, "More text test vectors to stuff up EBCDIC machines :-)", 54,
36 (unsigned char *)"e9139d1e6ee064ef8cf514fc7dc83e86",
40 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
41 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
43 (unsigned char *)"9294727a3638bb1c13f48ef8158bfc9d",
46 "Jefe", 4, "what do ya want for nothing?", 28,
47 (unsigned char *)"750c783e6ab0b503eaa86e310a5db738",
51 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
52 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
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, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
57 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
58 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
59 }, 50, (unsigned char *)"56be34521d144c88dbb8c733f0e8b3f6",
62 "", 0, "My test data", 12,
63 (unsigned char *)"61afdecb95429ef494d61fdee15990cabf0826fc"
66 "", 0, "My test data", 12,
67 (unsigned char *)"2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776"
70 "123456", 6, "My test data", 12,
71 (unsigned char *)"bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd"
74 "12345", 5, "My test data again", 18,
75 (unsigned char *)"a12396ceddd2a85f4c656bc1e0aa50c78cffde3e"
80 static char *pt(unsigned char *md, unsigned int len);
82 int main(int argc, char *argv[])
84 # ifndef OPENSSL_NO_MD5
89 HMAC_CTX *ctx = NULL, *ctx2 = NULL;
90 unsigned char buf[EVP_MAX_MD_SIZE];
93 # ifdef OPENSSL_NO_MD5
94 printf("test skipped: MD5 disabled\n");
97 # ifdef CHARSET_EBCDIC
98 ebcdic2ascii(test[0].data, test[0].data, test[0].data_len);
99 ebcdic2ascii(test[1].data, test[1].data, test[1].data_len);
100 ebcdic2ascii(test[2].key, test[2].key, test[2].key_len);
101 ebcdic2ascii(test[2].data, test[2].data, test[2].data_len);
104 for (i = 0; i < 4; i++) {
105 p = pt(HMAC(EVP_md5(),
106 test[i].key, test[i].key_len,
107 test[i].data, test[i].data_len, NULL, NULL),
110 if (strcmp(p, (char *)test[i].digest) != 0) {
111 printf("Error calculating HMAC on %d entry'\n", i);
112 printf("got %s instead of %s\n", p, test[i].digest);
115 printf("test %d ok\n", i);
117 # endif /* OPENSSL_NO_MD5 */
120 ctx = HMAC_CTX_new();
122 printf("HMAC malloc failure (test 4)\n");
126 if (HMAC_CTX_get_md(ctx) != NULL) {
127 printf("Message digest not NULL for HMAC (test 4)\n");
131 if (HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) {
132 printf("Should fail to initialise HMAC with empty MD and key (test 4)\n");
136 if (HMAC_Update(ctx, test[4].data, test[4].data_len)) {
137 printf("Should fail HMAC_Update with ctx not set up (test 4)\n");
141 if (HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL)) {
142 printf("Should fail to initialise HMAC with empty key (test 4)\n");
146 if (HMAC_Update(ctx, test[4].data, test[4].data_len)) {
147 printf("Should fail HMAC_Update with ctx not set up (test 4)\n");
151 printf("test 4 ok\n");
153 /* Test 5 has empty key; test that single-shot accepts a NULL key. */
154 p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len,
155 NULL, NULL), SHA_DIGEST_LENGTH);
156 if (strcmp(p, (char *)test[4].digest) != 0) {
157 printf("Error calculating HMAC on %d entry'\n", i);
158 printf("got %s instead of %s\n", p, test[4].digest);
163 if (HMAC_CTX_get_md(ctx) != NULL) {
164 printf("Message digest not NULL for HMAC (test 5)\n");
168 if (HMAC_Init_ex(ctx, test[4].key, test[4].key_len, NULL, NULL)) {
169 printf("Should fail to initialise HMAC with empty MD (test 5)\n");
173 if (HMAC_Update(ctx, test[4].data, test[4].data_len)) {
174 printf("Should fail HMAC_Update with ctx not set up (test 5)\n");
178 if (HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL)) {
179 printf("Should fail to initialise HMAC with invalid key len(test 5)\n");
183 if (!HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL)) {
184 printf("Failed to initialise HMAC (test 5)\n");
188 if (!HMAC_Update(ctx, test[4].data, test[4].data_len)) {
189 printf("Error updating HMAC with data (test 5)\n");
193 if (!HMAC_Final(ctx, buf, &len)) {
194 printf("Error finalising data (test 5)\n");
199 if (strcmp(p, (char *)test[4].digest) != 0) {
200 printf("Error calculating interim HMAC on test 5\n");
201 printf("got %s instead of %s\n", p, test[4].digest);
205 if (HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)) {
206 printf("Should disallow changing MD without a new key (test 5)\n");
210 if (!HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL)) {
211 printf("Failed to reinitialise HMAC (test 5)\n");
215 if (HMAC_CTX_get_md(ctx) != EVP_sha256()) {
216 printf("Unexpected message digest for HMAC (test 5)\n");
220 if (!HMAC_Update(ctx, test[5].data, test[5].data_len)) {
221 printf("Error updating HMAC with data (sha256) (test 5)\n");
225 if (!HMAC_Final(ctx, buf, &len)) {
226 printf("Error finalising data (sha256) (test 5)\n");
231 if (strcmp(p, (char *)test[5].digest) != 0) {
232 printf("Error calculating 2nd interim HMAC on test 5\n");
233 printf("got %s instead of %s\n", p, test[5].digest);
237 if (!HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL)) {
238 printf("Failed to reinitialise HMAC with key (test 5)\n");
242 if (!HMAC_Update(ctx, test[6].data, test[6].data_len)) {
243 printf("Error updating HMAC with data (new key) (test 5)\n");
247 if (!HMAC_Final(ctx, buf, &len)) {
248 printf("Error finalising data (new key) (test 5)\n");
253 if (strcmp(p, (char *)test[6].digest) != 0) {
254 printf("error calculating HMAC on test 5\n");
255 printf("got %s instead of %s\n", p, test[6].digest);
258 printf("test 5 ok\n");
262 ctx2 = HMAC_CTX_new();
264 printf("HMAC malloc failure (test 6)\n");
268 if (!HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL)) {
269 printf("Failed to initialise HMAC (test 6)\n");
273 if (!HMAC_Update(ctx, test[7].data, test[7].data_len)) {
274 printf("Error updating HMAC with data (test 6)\n");
278 if (!HMAC_CTX_copy(ctx2, ctx)) {
279 printf("Failed to copy HMAC_CTX (test 6)\n");
283 if (!HMAC_Final(ctx2, buf, &len)) {
284 printf("Error finalising data (test 6)\n");
289 if (strcmp(p, (char *)test[7].digest) != 0) {
290 printf("Error calculating HMAC on test 6\n");
291 printf("got %s instead of %s\n", p, test[7].digest);
294 printf("test 6 ok\n");
302 # ifndef OPENSSL_NO_MD5
303 static char *pt(unsigned char *md, unsigned int len)
308 for (i = 0; i < len; i++)
309 sprintf(&(buf[i * 2]), "%02x", md[i]);