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