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