1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef OHCRYPTO_KDF_TEST_H 16 #define OHCRYPTO_KDF_TEST_H 17 18 #include "crypto_common.h" 19 #include "crypto_kdf.h" 20 21 #define OHTEST_MD5_DIGIESTSIZE 16 22 #define OHTEST_SHA1_DIGIESTSIZE 20 23 #define OHTEST_SHA224_DIGIESTSIZE 28 24 #define OHTEST_SHA256_DIGIESTSIZE 32 25 #define OHTEST_SHA384_DIGIESTSIZE 48 26 #define OHTEST_SHA512_DIGIESTSIZE 64 27 #define OHTEST_SM3_DIGIESTSIZE 32 28 29 #define OHTEST_KDF_CONSTANT_LEN 255 30 31 typedef struct { 32 uint8_t *password; 33 uint8_t *salt; 34 int iterations; 35 } paramPbkdf2Spec; 36 37 typedef struct { 38 uint8_t *key; 39 uint8_t *salt; 40 uint8_t *info; 41 } paramHkdfSpec; 42 43 typedef struct { 44 uint8_t *passphrase; 45 uint8_t *salt; 46 uint64_t n; 47 uint64_t p; 48 uint64_t r; 49 uint64_t max_mem; 50 } paramScryptSpec; 51 52 typedef struct { 53 uint8_t *password; 54 size_t passLen; 55 uint8_t *salt; 56 size_t saltLen; 57 int iterations; 58 } paramVectorPbkdf2Spec; 59 60 typedef struct { 61 uint8_t *key; 62 size_t keyLen; 63 uint8_t *salt; 64 size_t saltLen; 65 uint8_t *info; 66 size_t infoLen; 67 } paramVectorHkdfSpec; 68 69 typedef struct { 70 uint8_t *passphrase; 71 size_t passLen; 72 uint8_t *salt; 73 size_t saltLen; 74 uint64_t n; 75 uint64_t p; 76 uint64_t r; 77 uint64_t max_mem; 78 } paramVectorScryptSpec; 79 80 typedef union { 81 paramPbkdf2Spec pbkdf2Params; 82 paramHkdfSpec hkdfParams; 83 paramScryptSpec scryptParams; 84 } paramsSpec; 85 86 typedef union { 87 paramVectorPbkdf2Spec pbkdf2Params; 88 paramVectorHkdfSpec hkdfParams; 89 paramVectorScryptSpec scryptParams; 90 } paramsVectorSpec; 91 92 typedef enum { 93 OHTEST_KDF_PBKDF2 = 0, 94 OHTEST_KDF_HKDF, 95 OHTEST_KDF_SCRYPT, 96 OHTEST_KDF_MAX = 16, 97 } KdfType; 98 99 typedef struct { 100 KdfType kdfType; 101 const char *algoName; 102 int keySize; 103 paramsSpec params; 104 } KdfSpec; 105 106 typedef struct { 107 KdfType kdfType; 108 const char *algoName; 109 int keySize; 110 paramsVectorSpec params; 111 uint8_t *expectSecret; 112 } KdfVectorSpec; 113 114 /* PBKDF2 SHA1 VECTOR */ 115 static uint8_t pbhdf2_sha1_password[] = { 116 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64 117 }; 118 static uint8_t pbhdf2_sha1_salt[] = { 119 0x73, 0x61, 0x6c, 0x74 120 }; 121 static uint8_t pbhdf2_sha1_expectSecret[] = { 122 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0, 123 0xd8, 0xde, 0x89, 0x57 124 }; 125 /* PBKDF2 SHA224 VECTOR */ 126 static uint8_t pbhdf2_sha224_password[] = { 127 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64 128 }; 129 static uint8_t pbhdf2_sha224_salt[] = { 130 0x73, 0x61, 0x6c, 0x74 131 }; 132 static uint8_t pbhdf2_sha224_expectSecret[] = { 133 0x3c, 0x19, 0x8c, 0xbd, 0xb9, 0x46, 0x4b, 0x78, 0x57, 0x96, 0x6b, 0xd0, 0x5b, 0x7b, 0xc9, 0x2b, 134 0xc1, 0xcc, 0x4e, 0x6e 135 }; 136 /* PBKDF2 SHA256 VECTOR */ 137 static uint8_t pbhdf2_sha256_password[] = { 138 0x70, 0x61, 0x73, 0x73, 0x77, 0x64 139 }; 140 static uint8_t pbhdf2_sha256_salt[] = { 141 0x73, 0x61, 0x6c, 0x74 142 }; 143 static uint8_t pbhdf2_sha256_expectSecret[] = { 144 0x55, 0xac, 0x04, 0x6e, 0x56, 0xe3, 0x08, 0x9f, 0xec, 0x16, 0x91, 0xc2, 0x25, 0x44, 0xb6, 0x05, 145 0xf9, 0x41, 0x85, 0x21, 0x6d, 0xde, 0x04, 0x65, 0xe6, 0x8b, 0x9d, 0x57, 0xc2, 0x0d, 0xac, 0xbc, 146 0x49, 0xca, 0x9c, 0xcc, 0xf1, 0x79, 0xb6, 0x45, 0x99, 0x16, 0x64, 0xb3, 0x9d, 0x77, 0xef, 0x31, 147 0x7c, 0x71, 0xb8, 0x45, 0xb1, 0xe3, 0x0b, 0xd5, 0x09, 0x11, 0x20, 0x41, 0xd3, 0xa1, 0x97, 0x83 148 }; 149 /* PBKDF2 SHA384 VECTOR */ 150 static uint8_t pbhdf2_sha384_password[] = { 151 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64 152 }; 153 static uint8_t pbhdf2_sha384_salt[] = { 154 0x73, 0x61, 0x6c, 0x74 155 }; 156 static uint8_t pbhdf2_sha384_expectSecret[] = { 157 0xc0, 0xe1, 0x4f, 0x06, 0xe4, 0x9e, 0x32, 0xd7, 0x3f, 0x9f, 0x52, 0xdd, 0xf1, 0xd0, 0xc5, 0xc7, 158 0x19, 0x16, 0x09, 0x23 159 }; 160 /* PBKDF2 SHA512 VECTOR */ 161 static uint8_t pbhdf2_sha512_password[] = { 162 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64 163 }; 164 static uint8_t pbhdf2_sha512_salt[] = { 165 0x73, 0x61, 0x6c, 0x74 166 }; 167 static uint8_t pbhdf2_sha512_expectSecret[] = { 168 0x86, 0x7f, 0x70, 0xcf, 0x1a, 0xde, 0x02, 0xcf, 0xf3, 0x75, 0x25, 0x99, 0xa3, 0xa5, 0x3d, 0xc4, 169 0xaf, 0x34, 0xc7, 0xa6 170 }; 171 /* PBKDF2 SM3 VECTOR */ 172 static uint8_t pbhdf2_sm3_password[] = { 173 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64 174 }; 175 static uint8_t pbhdf2_sm3_salt[] = { 176 0x73, 0x61, 0x6c, 0x74 177 }; 178 static uint8_t pbhdf2_sm3_expectSecret[] = { 179 0x73, 0x8c, 0x8c, 0x43, 0x23, 0x72, 0xd9, 0x8a, 0x73, 0x35, 0x0b, 0xc2, 0x52, 0x20, 0x9e, 0x4c, 180 0xf2, 0xac, 0xdd, 0xe7 181 }; 182 /* HKDF SHA1 VECTOR */ 183 static uint8_t hkdf_sha1_key[] = { 184 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 185 }; 186 static uint8_t hkdf_sha1_salt[] = { 187 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c 188 }; 189 static uint8_t hkdf_sha1_info[] = { 190 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 191 }; 192 static uint8_t hkdf_sha1_expectSecret[] = { 193 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69, 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81, 194 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15, 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2, 195 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3, 0xf8, 0x96 196 }; 197 /* HKDF SHA224 VECTOR */ 198 static uint8_t hkdf_sha224_key[] = { 199 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 200 }; 201 static uint8_t hkdf_sha224_salt[] = { 202 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c 203 }; 204 static uint8_t hkdf_sha224_info[] = { 205 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 206 }; 207 static uint8_t hkdf_sha224_expectSecret[] = { 208 0x7f, 0xc8, 0xae, 0x03, 0x35, 0xed, 0x46, 0x8c, 0xef, 0x56, 0xbe, 0x09, 0x1f, 0x64, 0x78, 0xa1, 209 0xaa, 0xe8, 0x4c, 0x0d, 0xa5, 0x4c, 0xe5, 0x17, 0x6a, 0xa3, 0x89, 0x46, 0xc7, 0x9e, 0x21, 0x0e 210 }; 211 /* HKDF SHA256 VECTOR */ 212 static uint8_t hkdf_sha256_key[] = { 213 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 214 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 215 }; 216 static uint8_t hkdf_sha256_salt[] = { 217 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c 218 }; 219 static uint8_t hkdf_sha256_info[] = { 220 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 221 }; 222 static uint8_t hkdf_sha256_expectSecret[] = { 223 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, 224 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, 225 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65 226 }; 227 /* HKDF SHA384 VECTOR */ 228 static uint8_t hkdf_sha384_key[] = { 229 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 230 }; 231 static uint8_t hkdf_sha384_salt[] = { 232 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c 233 }; 234 static uint8_t hkdf_sha384_info[] = { 235 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 236 }; 237 static uint8_t hkdf_sha384_expectSecret[] = { 238 0xfb, 0x7e, 0x67, 0x43, 0xeb, 0x42, 0xcd, 0xe9, 0x6f, 0x1b, 0x70, 0x77, 0x89, 0x52, 0xab, 0x75, 239 0x48, 0xca, 0xfe, 0x53, 0x24, 0x9f, 0x7f, 0xfe, 0x14, 0x97, 0xa1, 0x63, 0x5b, 0x20, 0x1f, 0xf1 240 }; 241 /* HKDF SHA512 VECTOR */ 242 static uint8_t hkdf_sha512_key[] = { 243 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b 244 }; 245 static uint8_t hkdf_sha512_salt[] = { 246 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c 247 }; 248 static uint8_t hkdf_sha512_info[] = { 249 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9 250 }; 251 static uint8_t hkdf_sha512_expectSecret[] = { 252 0x74, 0x13, 0xe8, 0x99, 0x7e, 0x02, 0x06, 0x10, 0xfb, 0xf6, 0x82, 0x3f, 0x2c, 0xe1, 0x4b, 0xff, 253 0x01, 0x87, 0x5d, 0xb1, 0xca, 0x55, 0xf6, 0x8c, 0xfc, 0xf3, 0x95, 0x4d, 0xc8, 0xaf, 0xf5, 0x35 254 }; 255 /* HKDF SM3 VECTOR */ 256 static uint8_t hkdf_sm3_key[] = { 257 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 258 }; 259 static uint8_t hkdf_sm3_salt[] = { 260 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 261 }; 262 static uint8_t hkdf_sm3_info[] = { 263 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 264 }; 265 static uint8_t hkdf_sm3_expectSecret[] = { 266 0xeb, 0xa1, 0x72, 0x2b, 0xd7, 0x9d, 0x3d, 0x00, 0x1e, 0xf1, 0x58, 0x2a, 0xd2, 0x3f, 0xf8, 0xda 267 }; 268 269 #endif