Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2017-2024 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 /* Tests of the EVP_PKEY_CTX_set_* macro family */
     11      1.1  christos 
     12      1.1  christos #include <stdio.h>
     13      1.1  christos #include <string.h>
     14      1.1  christos 
     15      1.1  christos #include <openssl/evp.h>
     16      1.1  christos #include <openssl/kdf.h>
     17      1.1  christos #include "testutil.h"
     18      1.1  christos 
     19      1.1  christos static int test_kdf_tls1_prf(int index)
     20      1.1  christos {
     21      1.1  christos     int ret = 0;
     22      1.1  christos     EVP_PKEY_CTX *pctx;
     23      1.1  christos     unsigned char out[16];
     24      1.1  christos     size_t outlen = sizeof(out);
     25      1.1  christos 
     26      1.1  christos     if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL)) == NULL) {
     27      1.1  christos         TEST_error("EVP_PKEY_TLS1_PRF");
     28      1.1  christos         goto err;
     29      1.1  christos     }
     30      1.1  christos     if (EVP_PKEY_derive_init(pctx) <= 0) {
     31      1.1  christos         TEST_error("EVP_PKEY_derive_init");
     32      1.1  christos         goto err;
     33      1.1  christos     }
     34      1.1  christos     if (EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_sha256()) <= 0) {
     35      1.1  christos         TEST_error("EVP_PKEY_CTX_set_tls1_prf_md");
     36      1.1  christos         goto err;
     37      1.1  christos     }
     38      1.1  christos     if (EVP_PKEY_CTX_set1_tls1_prf_secret(pctx,
     39  1.1.1.2  christos             (unsigned char *)"secret", 6)
     40  1.1.1.2  christos         <= 0) {
     41      1.1  christos         TEST_error("EVP_PKEY_CTX_set1_tls1_prf_secret");
     42      1.1  christos         goto err;
     43      1.1  christos     }
     44      1.1  christos     if (index == 0) {
     45      1.1  christos         if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx,
     46  1.1.1.2  christos                 (unsigned char *)"seed", 4)
     47  1.1.1.2  christos             <= 0) {
     48      1.1  christos             TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed");
     49      1.1  christos             goto err;
     50      1.1  christos         }
     51      1.1  christos     } else {
     52      1.1  christos         if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx,
     53  1.1.1.2  christos                 (unsigned char *)"se", 2)
     54  1.1.1.2  christos             <= 0) {
     55      1.1  christos             TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed");
     56      1.1  christos             goto err;
     57      1.1  christos         }
     58      1.1  christos         if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx,
     59  1.1.1.2  christos                 (unsigned char *)"ed", 2)
     60  1.1.1.2  christos             <= 0) {
     61      1.1  christos             TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed");
     62      1.1  christos             goto err;
     63      1.1  christos         }
     64      1.1  christos     }
     65      1.1  christos     if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
     66      1.1  christos         TEST_error("EVP_PKEY_derive");
     67      1.1  christos         goto err;
     68      1.1  christos     }
     69      1.1  christos 
     70      1.1  christos     {
     71      1.1  christos         const unsigned char expected[sizeof(out)] = {
     72      1.1  christos             0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
     73      1.1  christos             0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
     74      1.1  christos         };
     75      1.1  christos         if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
     76      1.1  christos             goto err;
     77      1.1  christos         }
     78      1.1  christos     }
     79      1.1  christos     ret = 1;
     80      1.1  christos err:
     81      1.1  christos     EVP_PKEY_CTX_free(pctx);
     82      1.1  christos     return ret;
     83      1.1  christos }
     84      1.1  christos 
     85      1.1  christos static int test_kdf_hkdf(int index)
     86      1.1  christos {
     87      1.1  christos     int ret = 0;
     88      1.1  christos     EVP_PKEY_CTX *pctx;
     89      1.1  christos     unsigned char out[10];
     90      1.1  christos     size_t outlen = sizeof(out);
     91      1.1  christos 
     92      1.1  christos     if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) {
     93      1.1  christos         TEST_error("EVP_PKEY_HKDF");
     94      1.1  christos         goto err;
     95      1.1  christos     }
     96      1.1  christos     if (EVP_PKEY_derive_init(pctx) <= 0) {
     97      1.1  christos         TEST_error("EVP_PKEY_derive_init");
     98      1.1  christos         goto err;
     99      1.1  christos     }
    100      1.1  christos     if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
    101      1.1  christos         TEST_error("EVP_PKEY_CTX_set_hkdf_md");
    102      1.1  christos         goto err;
    103      1.1  christos     }
    104      1.1  christos     if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, (const unsigned char *)"salt", 4)
    105  1.1.1.2  christos         <= 0) {
    106      1.1  christos         TEST_error("EVP_PKEY_CTX_set1_hkdf_salt");
    107      1.1  christos         goto err;
    108      1.1  christos     }
    109      1.1  christos     if (EVP_PKEY_CTX_set1_hkdf_key(pctx, (const unsigned char *)"secret", 6)
    110  1.1.1.2  christos         <= 0) {
    111      1.1  christos         TEST_error("EVP_PKEY_CTX_set1_hkdf_key");
    112      1.1  christos         goto err;
    113      1.1  christos     }
    114      1.1  christos     if (index == 0) {
    115      1.1  christos         if (EVP_PKEY_CTX_add1_hkdf_info(pctx, (const unsigned char *)"label", 5)
    116      1.1  christos             <= 0) {
    117      1.1  christos             TEST_error("EVP_PKEY_CTX_add1_hkdf_info");
    118      1.1  christos             goto err;
    119      1.1  christos         }
    120      1.1  christos     } else {
    121      1.1  christos         if (EVP_PKEY_CTX_add1_hkdf_info(pctx, (const unsigned char *)"lab", 3)
    122      1.1  christos             <= 0) {
    123      1.1  christos             TEST_error("EVP_PKEY_CTX_add1_hkdf_info");
    124      1.1  christos             goto err;
    125      1.1  christos         }
    126      1.1  christos         if (EVP_PKEY_CTX_add1_hkdf_info(pctx, (const unsigned char *)"el", 2)
    127      1.1  christos             <= 0) {
    128      1.1  christos             TEST_error("EVP_PKEY_CTX_add1_hkdf_info");
    129      1.1  christos             goto err;
    130      1.1  christos         }
    131      1.1  christos     }
    132      1.1  christos     if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
    133      1.1  christos         TEST_error("EVP_PKEY_derive");
    134      1.1  christos         goto err;
    135      1.1  christos     }
    136      1.1  christos 
    137      1.1  christos     {
    138      1.1  christos         const unsigned char expected[sizeof(out)] = {
    139      1.1  christos             0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
    140      1.1  christos         };
    141      1.1  christos         if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
    142      1.1  christos             goto err;
    143      1.1  christos         }
    144      1.1  christos     }
    145      1.1  christos     ret = 1;
    146      1.1  christos err:
    147      1.1  christos     EVP_PKEY_CTX_free(pctx);
    148      1.1  christos     return ret;
    149      1.1  christos }
    150      1.1  christos 
    151      1.1  christos #ifndef OPENSSL_NO_SCRYPT
    152      1.1  christos static int test_kdf_scrypt(void)
    153      1.1  christos {
    154      1.1  christos     int ret = 0;
    155      1.1  christos     EVP_PKEY_CTX *pctx;
    156      1.1  christos     unsigned char out[64];
    157      1.1  christos     size_t outlen = sizeof(out);
    158      1.1  christos 
    159      1.1  christos     if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL)) == NULL) {
    160      1.1  christos         TEST_error("EVP_PKEY_SCRYPT");
    161      1.1  christos         goto err;
    162      1.1  christos     }
    163      1.1  christos     if (EVP_PKEY_derive_init(pctx) <= 0) {
    164      1.1  christos         TEST_error("EVP_PKEY_derive_init");
    165      1.1  christos         goto err;
    166      1.1  christos     }
    167      1.1  christos     if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) {
    168      1.1  christos         TEST_error("EVP_PKEY_CTX_set1_pbe_pass");
    169      1.1  christos         goto err;
    170      1.1  christos     }
    171      1.1  christos     if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, (unsigned char *)"NaCl", 4) <= 0) {
    172      1.1  christos         TEST_error("EVP_PKEY_CTX_set1_scrypt_salt");
    173      1.1  christos         goto err;
    174      1.1  christos     }
    175      1.1  christos     if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) {
    176      1.1  christos         TEST_error("EVP_PKEY_CTX_set_scrypt_N");
    177      1.1  christos         goto err;
    178      1.1  christos     }
    179      1.1  christos     if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) {
    180      1.1  christos         TEST_error("EVP_PKEY_CTX_set_scrypt_r");
    181      1.1  christos         goto err;
    182      1.1  christos     }
    183      1.1  christos     if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) {
    184      1.1  christos         TEST_error("EVP_PKEY_CTX_set_scrypt_p");
    185      1.1  christos         goto err;
    186      1.1  christos     }
    187      1.1  christos     if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) {
    188      1.1  christos         TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
    189      1.1  christos         goto err;
    190      1.1  christos     }
    191      1.1  christos     if (EVP_PKEY_derive(pctx, out, &outlen) > 0) {
    192      1.1  christos         TEST_error("EVP_PKEY_derive should have failed");
    193      1.1  christos         goto err;
    194      1.1  christos     }
    195      1.1  christos     if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) {
    196      1.1  christos         TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
    197      1.1  christos         goto err;
    198      1.1  christos     }
    199      1.1  christos     if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
    200      1.1  christos         TEST_error("EVP_PKEY_derive");
    201      1.1  christos         goto err;
    202      1.1  christos     }
    203      1.1  christos 
    204      1.1  christos     {
    205      1.1  christos         const unsigned char expected[sizeof(out)] = {
    206      1.1  christos             0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
    207      1.1  christos             0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
    208      1.1  christos             0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
    209      1.1  christos             0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
    210      1.1  christos             0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
    211      1.1  christos             0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
    212      1.1  christos             0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
    213      1.1  christos             0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
    214      1.1  christos         };
    215      1.1  christos         if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
    216      1.1  christos             goto err;
    217      1.1  christos         }
    218      1.1  christos     }
    219      1.1  christos     ret = 1;
    220      1.1  christos err:
    221      1.1  christos     EVP_PKEY_CTX_free(pctx);
    222      1.1  christos     return ret;
    223      1.1  christos }
    224      1.1  christos #endif
    225      1.1  christos 
    226      1.1  christos int setup_tests(void)
    227      1.1  christos {
    228      1.1  christos     int tests = 1;
    229      1.1  christos 
    230      1.1  christos     if (fips_provider_version_ge(NULL, 3, 3, 1))
    231      1.1  christos         tests = 2;
    232      1.1  christos 
    233      1.1  christos     ADD_ALL_TESTS(test_kdf_tls1_prf, tests);
    234      1.1  christos     ADD_ALL_TESTS(test_kdf_hkdf, tests);
    235      1.1  christos #ifndef OPENSSL_NO_SCRYPT
    236      1.1  christos     ADD_TEST(test_kdf_scrypt);
    237      1.1  christos #endif
    238      1.1  christos     return 1;
    239      1.1  christos }
    240