1 1.1 christos /* 2 1.1 christos * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos /* 11 1.1 christos * HMAC low level APIs are deprecated for public use, but still ok for internal 12 1.1 christos * use. 13 1.1 christos */ 14 1.1 christos #include "internal/deprecated.h" 15 1.1 christos 16 1.1 christos #include <stdio.h> 17 1.1 christos #include <string.h> 18 1.1 christos #include <stdlib.h> 19 1.1 christos 20 1.1 christos #include "internal/nelem.h" 21 1.1 christos 22 1.3 christos #include <openssl/hmac.h> 23 1.3 christos #include <openssl/sha.h> 24 1.3 christos #ifndef OPENSSL_NO_MD5 25 1.3 christos #include <openssl/md5.h> 26 1.3 christos #endif 27 1.3 christos 28 1.3 christos #ifdef CHARSET_EBCDIC 29 1.3 christos #include <openssl/ebcdic.h> 30 1.3 christos #endif 31 1.1 christos 32 1.1 christos #include "testutil.h" 33 1.1 christos 34 1.3 christos #ifndef OPENSSL_NO_MD5 35 1.1 christos static struct test_st { 36 1.1 christos const char key[16]; 37 1.1 christos int key_len; 38 1.1 christos const unsigned char data[64]; 39 1.1 christos int data_len; 40 1.1 christos const char *digest; 41 1.1 christos } test[8] = { 42 1.1 christos { 43 1.3 christos "", 44 1.3 christos 0, 45 1.3 christos "More text test vectors to stuff up EBCDIC machines :-)", 46 1.3 christos 54, 47 1.1 christos "e9139d1e6ee064ef8cf514fc7dc83e86", 48 1.1 christos }, 49 1.1 christos { 50 1.1 christos "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 51 1.3 christos 16, 52 1.3 christos "Hi There", 53 1.3 christos 8, 54 1.1 christos "9294727a3638bb1c13f48ef8158bfc9d", 55 1.1 christos }, 56 1.1 christos { 57 1.3 christos "Jefe", 58 1.3 christos 4, 59 1.3 christos "what do ya want for nothing?", 60 1.3 christos 28, 61 1.1 christos "750c783e6ab0b503eaa86e310a5db738", 62 1.1 christos }, 63 1.1 christos { 64 1.1 christos "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 65 1.3 christos 16, 66 1.3 christos { 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 67 1.1 christos 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 68 1.1 christos 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 69 1.1 christos 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 70 1.3 christos 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd }, 71 1.3 christos 50, 72 1.3 christos "56be34521d144c88dbb8c733f0e8b3f6", 73 1.3 christos }, 74 1.3 christos { "", 0, "My test data", 12, 75 1.3 christos "61afdecb95429ef494d61fdee15990cabf0826fc" }, 76 1.3 christos { "", 0, "My test data", 12, 77 1.3 christos "2274b195d90ce8e03406f4b526a47e0787a88a65479938f1a5baa3ce0f079776" }, 78 1.3 christos { "123456", 6, "My test data", 12, 79 1.3 christos "bab53058ae861a7f191abe2d0145cbb123776a6369ee3f9d79ce455667e411dd" }, 80 1.3 christos { "12345", 5, "My test data again", 18, 81 1.3 christos "a12396ceddd2a85f4c656bc1e0aa50c78cffde3e" } 82 1.1 christos }; 83 1.3 christos #endif 84 1.1 christos 85 1.1 christos static char *pt(unsigned char *md, unsigned int len); 86 1.1 christos 87 1.2 christos #define UC(a) ((const unsigned char *)(a)) 88 1.2 christos 89 1.1 christos 90 1.3 christos #ifndef OPENSSL_NO_MD5 91 1.1 christos static int test_hmac_md5(int idx) 92 1.1 christos { 93 1.1 christos char *p; 94 1.3 christos #ifdef CHARSET_EBCDIC 95 1.1 christos ebcdic2ascii(test[0].data, test[0].data, test[0].data_len); 96 1.1 christos ebcdic2ascii(test[1].data, test[1].data, test[1].data_len); 97 1.1 christos ebcdic2ascii(test[2].key, test[2].key, test[2].key_len); 98 1.1 christos ebcdic2ascii(test[2].data, test[2].data, test[2].data_len); 99 1.3 christos #endif 100 1.1 christos 101 1.1 christos p = pt(HMAC(EVP_md5(), 102 1.3 christos test[idx].key, test[idx].key_len, 103 1.3 christos UC(test[idx].data), test[idx].data_len, NULL, NULL), 104 1.3 christos MD5_DIGEST_LENGTH); 105 1.1 christos 106 1.1 christos return TEST_ptr(p) && TEST_str_eq(p, test[idx].digest); 107 1.1 christos } 108 1.3 christos #endif 109 1.1 christos 110 1.1 christos static int test_hmac_bad(void) 111 1.1 christos { 112 1.1 christos HMAC_CTX *ctx = NULL; 113 1.1 christos int ret = 0; 114 1.1 christos 115 1.1 christos ctx = HMAC_CTX_new(); 116 1.1 christos if (!TEST_ptr(ctx) 117 1.1 christos || !TEST_ptr_null(HMAC_CTX_get_md(ctx)) 118 1.1 christos || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) 119 1.2 christos || !TEST_false(HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) 120 1.1 christos || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha1(), NULL)) 121 1.2 christos || !TEST_false(HMAC_Update(ctx, UC(test[4].data), test[4].data_len))) 122 1.1 christos goto err; 123 1.1 christos 124 1.1 christos ret = 1; 125 1.1 christos err: 126 1.1 christos HMAC_CTX_free(ctx); 127 1.1 christos return ret; 128 1.1 christos } 129 1.1 christos 130 1.1 christos static int test_hmac_run(void) 131 1.1 christos { 132 1.1 christos char *p; 133 1.1 christos HMAC_CTX *ctx = NULL; 134 1.1 christos unsigned char buf[EVP_MAX_MD_SIZE]; 135 1.1 christos unsigned int len; 136 1.1 christos int ret = 0; 137 1.1 christos 138 1.1 christos if (!TEST_ptr(ctx = HMAC_CTX_new())) 139 1.1 christos return 0; 140 1.1 christos HMAC_CTX_reset(ctx); 141 1.1 christos 142 1.1 christos if (!TEST_ptr(ctx) 143 1.1 christos || !TEST_ptr_null(HMAC_CTX_get_md(ctx)) 144 1.1 christos || !TEST_false(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) 145 1.2 christos || !TEST_false(HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) 146 1.1 christos || !TEST_false(HMAC_Init_ex(ctx, test[4].key, -1, EVP_sha1(), NULL))) 147 1.1 christos goto err; 148 1.1 christos 149 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, test[4].key, test[4].key_len, EVP_sha1(), NULL)) 150 1.2 christos || !TEST_true(HMAC_Update(ctx, UC(test[4].data), test[4].data_len)) 151 1.1 christos || !TEST_true(HMAC_Final(ctx, buf, &len))) 152 1.1 christos goto err; 153 1.1 christos 154 1.1 christos p = pt(buf, len); 155 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest)) 156 1.1 christos goto err; 157 1.1 christos 158 1.1 christos if (!TEST_false(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL))) 159 1.1 christos goto err; 160 1.1 christos 161 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL)) 162 1.1 christos || !TEST_ptr_eq(HMAC_CTX_get_md(ctx), EVP_sha256()) 163 1.2 christos || !TEST_true(HMAC_Update(ctx, UC(test[5].data), test[5].data_len)) 164 1.1 christos || !TEST_true(HMAC_Final(ctx, buf, &len))) 165 1.1 christos goto err; 166 1.1 christos 167 1.1 christos p = pt(buf, len); 168 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[5].digest)) 169 1.1 christos goto err; 170 1.1 christos 171 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, test[6].key, test[6].key_len, NULL, NULL)) 172 1.2 christos || !TEST_true(HMAC_Update(ctx, UC(test[6].data), test[6].data_len)) 173 1.1 christos || !TEST_true(HMAC_Final(ctx, buf, &len))) 174 1.1 christos goto err; 175 1.1 christos p = pt(buf, len); 176 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest)) 177 1.1 christos goto err; 178 1.1 christos 179 1.1 christos /* Test reusing a key */ 180 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL)) 181 1.2 christos || !TEST_true(HMAC_Update(ctx, UC(test[6].data), test[6].data_len)) 182 1.1 christos || !TEST_true(HMAC_Final(ctx, buf, &len))) 183 1.1 christos goto err; 184 1.1 christos p = pt(buf, len); 185 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest)) 186 1.1 christos goto err; 187 1.1 christos 188 1.1 christos /* 189 1.1 christos * Test reusing a key where the digest is provided again but is the same as 190 1.1 christos * last time 191 1.1 christos */ 192 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, NULL, 0, EVP_sha256(), NULL)) 193 1.2 christos || !TEST_true(HMAC_Update(ctx, UC(test[6].data), test[6].data_len)) 194 1.1 christos || !TEST_true(HMAC_Final(ctx, buf, &len))) 195 1.1 christos goto err; 196 1.1 christos p = pt(buf, len); 197 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[6].digest)) 198 1.1 christos goto err; 199 1.1 christos 200 1.1 christos ret = 1; 201 1.1 christos err: 202 1.1 christos HMAC_CTX_free(ctx); 203 1.1 christos return ret; 204 1.1 christos } 205 1.1 christos 206 1.1 christos static int test_hmac_single_shot(void) 207 1.1 christos { 208 1.1 christos char *p; 209 1.1 christos 210 1.1 christos /* Test single-shot with NULL key. */ 211 1.1 christos p = pt(HMAC(EVP_sha1(), NULL, 0, test[4].data, test[4].data_len, 212 1.3 christos NULL, NULL), 213 1.3 christos SHA_DIGEST_LENGTH); 214 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[4].digest)) 215 1.1 christos return 0; 216 1.1 christos 217 1.1 christos return 1; 218 1.1 christos } 219 1.1 christos 220 1.1 christos /* https://github.com/openssl/openssl/issues/13210 */ 221 1.1 christos static int test_hmac_final_update_fail(void) 222 1.1 christos { 223 1.1 christos HMAC_CTX *ctx = NULL; 224 1.1 christos unsigned char buf[EVP_MAX_MD_SIZE]; 225 1.1 christos unsigned int len; 226 1.1 christos int ret = 0; 227 1.1 christos 228 1.1 christos /* HMAC_Update() after HMAC_Final() must return an error. */ 229 1.1 christos if (!TEST_ptr(ctx = HMAC_CTX_new())) 230 1.1 christos goto err; 231 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, test[5].key, test[5].key_len, EVP_sha256(), NULL)) 232 1.1 christos || !TEST_true(HMAC_Update(ctx, test[5].data, test[5].data_len)) 233 1.1 christos || !TEST_true(HMAC_Final(ctx, buf, &len)) 234 1.1 christos || !TEST_false(HMAC_Update(ctx, test[5].data, test[5].data_len)) 235 1.1 christos || !TEST_false(HMAC_Final(ctx, buf, &len))) 236 1.1 christos goto err; 237 1.1 christos 238 1.1 christos ret = 1; 239 1.1 christos err: 240 1.1 christos HMAC_CTX_free(ctx); 241 1.1 christos return ret; 242 1.1 christos } 243 1.1 christos 244 1.1 christos static int test_hmac_copy(void) 245 1.1 christos { 246 1.1 christos char *p; 247 1.1 christos HMAC_CTX *ctx = NULL, *ctx2 = NULL; 248 1.1 christos unsigned char buf[EVP_MAX_MD_SIZE]; 249 1.1 christos unsigned int len; 250 1.1 christos int ret = 0; 251 1.1 christos 252 1.1 christos ctx = HMAC_CTX_new(); 253 1.1 christos ctx2 = HMAC_CTX_new(); 254 1.1 christos if (!TEST_ptr(ctx) || !TEST_ptr(ctx2)) 255 1.1 christos goto err; 256 1.1 christos 257 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, test[7].key, test[7].key_len, EVP_sha1(), NULL)) 258 1.2 christos || !TEST_true(HMAC_Update(ctx, UC(test[7].data), test[7].data_len)) 259 1.1 christos || !TEST_true(HMAC_CTX_copy(ctx2, ctx)) 260 1.1 christos || !TEST_true(HMAC_Final(ctx2, buf, &len))) 261 1.1 christos goto err; 262 1.1 christos 263 1.1 christos p = pt(buf, len); 264 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test[7].digest)) 265 1.1 christos goto err; 266 1.1 christos 267 1.1 christos ret = 1; 268 1.1 christos err: 269 1.1 christos HMAC_CTX_free(ctx2); 270 1.1 christos HMAC_CTX_free(ctx); 271 1.1 christos return ret; 272 1.1 christos } 273 1.1 christos 274 1.1 christos static int test_hmac_copy_uninited(void) 275 1.1 christos { 276 1.3 christos const unsigned char key[24] = { 0 }; 277 1.3 christos const unsigned char ct[166] = { 0 }; 278 1.1 christos EVP_PKEY *pkey = NULL; 279 1.1 christos EVP_MD_CTX *ctx = NULL; 280 1.1 christos EVP_MD_CTX *ctx_tmp = NULL; 281 1.1 christos int res = 0; 282 1.1 christos 283 1.1 christos if (!TEST_ptr(ctx = EVP_MD_CTX_new()) 284 1.3 christos || !TEST_ptr(pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, 285 1.3 christos key, sizeof(key))) 286 1.3 christos || !TEST_true(EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey)) 287 1.3 christos || !TEST_ptr(ctx_tmp = EVP_MD_CTX_new()) 288 1.3 christos || !TEST_true(EVP_MD_CTX_copy(ctx_tmp, ctx))) 289 1.1 christos goto err; 290 1.1 christos EVP_MD_CTX_free(ctx); 291 1.1 christos ctx = ctx_tmp; 292 1.1 christos ctx_tmp = NULL; 293 1.1 christos 294 1.1 christos if (!TEST_true(EVP_DigestSignUpdate(ctx, ct, sizeof(ct)))) 295 1.1 christos goto err; 296 1.1 christos res = 1; 297 1.3 christos err: 298 1.1 christos EVP_MD_CTX_free(ctx); 299 1.1 christos EVP_MD_CTX_free(ctx_tmp); 300 1.1 christos EVP_PKEY_free(pkey); 301 1.1 christos return res; 302 1.1 christos } 303 1.1 christos 304 1.1 christos #ifndef OPENSSL_NO_MD5 305 1.3 christos #define OSSL_HEX_CHARS_PER_BYTE 2 306 1.1 christos static char *pt(unsigned char *md, unsigned int len) 307 1.1 christos { 308 1.1 christos unsigned int i; 309 1.1 christos static char buf[201]; 310 1.1 christos 311 1.1 christos if (md == NULL) 312 1.1 christos return NULL; 313 1.1 christos for (i = 0; i < len && (i + 1) * OSSL_HEX_CHARS_PER_BYTE < sizeof(buf); i++) 314 1.1 christos BIO_snprintf(buf + i * OSSL_HEX_CHARS_PER_BYTE, 315 1.3 christos OSSL_HEX_CHARS_PER_BYTE + 1, "%02x", md[i]); 316 1.1 christos return buf; 317 1.1 christos } 318 1.1 christos #endif 319 1.1 christos 320 1.1 christos static struct test_chunks_st { 321 1.1 christos const char *md_name; 322 1.1 christos char key[256]; 323 1.1 christos int key_len; 324 1.1 christos int chunks; 325 1.1 christos int chunk_size[10]; 326 1.1 christos const char *digest; 327 1.1 christos } test_chunks[12] = { 328 1.3 christos { "SHA224", 329 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 64, 330 1.1 christos 4, { 1, 50, 200, 4000 }, 331 1.3 christos "40821a39dd54f01443b3f96b9370a15023fbdd819a074ffc4b703c77" }, 332 1.3 christos { "SHA224", 333 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 334 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 335 1.3 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 336 1.3 christos 192, 337 1.1 christos 10, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 338 1.3 christos "55ffa85e53e9a68f41c8d653c60b4ada9566d22aed3811834882661c" }, 339 1.3 christos { "SHA224", "0123456789abcdef0123456789abcdef", 32, 340 1.1 christos 4, { 100, 4096, 100, 3896 }, 341 1.3 christos "0fd18e7d8e974f401b29bf0502a71f6a9b77804e9191380ce9f48377" }, 342 1.3 christos { "SHA256", 343 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 64, 344 1.1 christos 4, { 1, 50, 200, 4000 }, 345 1.3 christos "f67a46fa77c66d3ea5b3ffb9a10afb3e501eaadd16b15978fdee9f014a782140" }, 346 1.3 christos { "SHA256", 347 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 348 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 349 1.3 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 350 1.3 christos 192, 351 1.1 christos 10, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 352 1.3 christos "21a6f61ed6dbec30b58557a80988ff610d69b50b2e96d75863ab50f99da58c9d" }, 353 1.3 christos { "SHA256", "0123456789abcdef0123456789abcdef", 32, 354 1.1 christos 4, { 100, 4096, 100, 3896 }, 355 1.3 christos "7bfd45c1bdde9b79244816b0aea0a67ea954a182e74c60410bfbc1fdc4842660" }, 356 1.3 christos { "SHA384", 357 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 64, 358 1.1 christos 4, { 1, 50, 200, 4000 }, 359 1.1 christos "e270e3c8ca3f2796a0c29cc7569fcec7584b04db26da64326aca0d17bd7731de" 360 1.3 christos "938694b273f3dafe6e2dc123cde26640" }, 361 1.3 christos { "SHA384", 362 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 363 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 364 1.3 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 365 1.3 christos 192, 366 1.1 christos 10, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 367 1.1 christos "7036fd7d251298975acd18938471243e92fffe67be158f16c910c400576592d2" 368 1.3 christos "618c3c077ef25d703312668bd2d813ff" }, 369 1.3 christos { "SHA384", "0123456789abcdef0123456789abcdef", 32, 370 1.1 christos 4, { 100, 8192, 100, 8092 }, 371 1.1 christos "0af8224145bd0812d2e34ba1f980ed4d218461271a54cce75dc43d36eda01e4e" 372 1.3 christos "ff4299c1ebf533a7ae636fa3e6aff903" }, 373 1.3 christos { "SHA512", 374 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 64, 375 1.1 christos 4, { 1, 50, 200, 4000 }, 376 1.1 christos "4016e960e2342553d4b9d34fb57355ab8b7f33af5dc2676fc1189e94b38f2b2c" 377 1.3 christos "a0ec8dc3c8b95fb1109d58480cea1e8f88e02f34ad79b303e4809373c46c1b16" }, 378 1.3 christos { "SHA512", 379 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 380 1.1 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" 381 1.3 christos "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 382 1.3 christos 192, 383 1.1 christos 10, { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 384 1.1 christos "7ceb6a421fc19434bcb7ec9c8a15ea524dbfb896c24f5f517513f06597de99b1" 385 1.3 christos "918eb6b2472e52215ec7d1b5544766f79ff6ac6d1eb456f19a93819fa2d43c29" }, 386 1.3 christos { "SHA512", "0123456789abcdef0123456789abcdef", 32, 387 1.1 christos 4, { 100, 8192, 100, 8092 }, 388 1.1 christos "cebf722ffdff5f0e4cbfbd480cd086101d4627d30d42f1f7cf21c43251018069" 389 1.3 christos "854d8e030b5a54cec1e2245d5b4629ff928806d4eababb427d751ec7c274047f" }, 390 1.1 christos }; 391 1.1 christos 392 1.1 christos static int test_hmac_chunks(int idx) 393 1.1 christos { 394 1.1 christos char *p; 395 1.1 christos HMAC_CTX *ctx = NULL; 396 1.1 christos unsigned char buf[32768]; 397 1.1 christos unsigned int len; 398 1.1 christos const EVP_MD *md; 399 1.1 christos int i, ret = 0; 400 1.1 christos 401 1.1 christos if (!TEST_ptr(md = EVP_get_digestbyname(test_chunks[idx].md_name))) 402 1.1 christos goto err; 403 1.1 christos 404 1.1 christos if (!TEST_ptr(ctx = HMAC_CTX_new())) 405 1.1 christos goto err; 406 1.1 christos 407 1.1 christos #ifdef CHARSET_EBCDIC 408 1.1 christos ebcdic2ascii(test_chunks[idx].key, test_chunks[idx].key, 409 1.3 christos test_chunks[idx].key_len); 410 1.1 christos #endif 411 1.1 christos 412 1.1 christos if (!TEST_true(HMAC_Init_ex(ctx, test_chunks[idx].key, 413 1.3 christos test_chunks[idx].key_len, md, NULL))) 414 1.1 christos goto err; 415 1.1 christos 416 1.1 christos for (i = 0; i < test_chunks[idx].chunks; i++) { 417 1.1 christos if (!TEST_true((test_chunks[idx].chunk_size[i] < (int)sizeof(buf)))) 418 1.1 christos goto err; 419 1.1 christos memset(buf, i, test_chunks[idx].chunk_size[i]); 420 1.1 christos if (!TEST_true(HMAC_Update(ctx, buf, test_chunks[idx].chunk_size[i]))) 421 1.1 christos goto err; 422 1.1 christos } 423 1.1 christos 424 1.1 christos if (!TEST_true(HMAC_Final(ctx, buf, &len))) 425 1.1 christos goto err; 426 1.1 christos 427 1.1 christos p = pt(buf, len); 428 1.1 christos if (!TEST_ptr(p) || !TEST_str_eq(p, test_chunks[idx].digest)) 429 1.1 christos goto err; 430 1.1 christos 431 1.1 christos ret = 1; 432 1.1 christos 433 1.1 christos err: 434 1.1 christos HMAC_CTX_free(ctx); 435 1.1 christos return ret; 436 1.1 christos } 437 1.1 christos 438 1.1 christos int setup_tests(void) 439 1.1 christos { 440 1.1 christos ADD_ALL_TESTS(test_hmac_md5, 4); 441 1.1 christos ADD_TEST(test_hmac_single_shot); 442 1.1 christos ADD_TEST(test_hmac_bad); 443 1.1 christos ADD_TEST(test_hmac_run); 444 1.1 christos ADD_TEST(test_hmac_final_update_fail); 445 1.1 christos ADD_TEST(test_hmac_copy); 446 1.1 christos ADD_TEST(test_hmac_copy_uninited); 447 1.1 christos ADD_ALL_TESTS(test_hmac_chunks, 448 1.3 christos sizeof(test_chunks) / sizeof(struct test_chunks_st)); 449 1.1 christos return 1; 450 1.1 christos } 451