clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name sftkike.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/security/nss/lib/softoken/softoken_softokn -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/security/nss/lib/softoken/softoken_softokn -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG -D SHLIB_SUFFIX="so" -D SHLIB_PREFIX="lib" -D SOFTOKEN_LIB_NAME="libsoftokn3.so" -D SHLIB_VERSION="3" -D NSS_FIPS_DISABLED -D NSS_NO_INIT_SUPPORT -D NSS_X86_OR_X64 -D NSS_X64 -D NSS_USE_64 -D USE_UTIL_DIRECTLY -D NO_NSPR_10_SUPPORT -D SSL_DISABLE_DEPRECATED_CIPHER_SUITE_NAMES -D LINUX2_1 -D LINUX -D linux -D _DEFAULT_SOURCE -D _BSD_SOURCE -D _POSIX_SOURCE -D SDB_MEASURE_USE_TEMP_DIR -D HAVE_STRERROR -D XP_UNIX -D _REENTRANT -D NSS_DISABLE_DBM -D NSS_DISABLE_LIBPKIX -D NSS_USE_PKCS5_PBKD2_PARAMS2_ONLY -D SOFTOKEN_USE_PKCS5_PBKD2_PARAMS2_ONLY -I /root/firefox-clang/security/nss/lib/softoken -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/security/nss/lib/softoken/softoken_softokn -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/private/nss -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -D MOZILLA_CLIENT -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=tautological-type-limit-compare -Wno-range-loop-analysis -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-unknown-warning-option -Wno-character-conversion -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c /root/firefox-clang/security/nss/lib/softoken/sftkike.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | #include "seccomon.h" |
| 21 | #include "secitem.h" |
| 22 | #include "secport.h" |
| 23 | #include "blapi.h" |
| 24 | #include "pkcs11.h" |
| 25 | #include "pkcs11i.h" |
| 26 | #include "pkcs1sig.h" |
| 27 | #include "lowkeyi.h" |
| 28 | #include "secder.h" |
| 29 | #include "secdig.h" |
| 30 | #include "lowpbe.h" /* We do PBE below */ |
| 31 | #include "pkcs11t.h" |
| 32 | #include "secoid.h" |
| 33 | #include "alghmac.h" |
| 34 | #include "softoken.h" |
| 35 | #include "secasn1.h" |
| 36 | #include "secerr.h" |
| 37 | |
| 38 | #include "prprf.h" |
| 39 | #include "prenv.h" |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | typedef struct prfContextStr { |
| 46 | HASH_HashType hashType; |
| 47 | const SECHashObject *hashObj; |
| 48 | HMACContext *hmac; |
| 49 | AESContext *aes; |
| 50 | unsigned int nextChar; |
| 51 | unsigned char padBuf[AES_BLOCK_SIZE]; |
| 52 | unsigned char macBuf[AES_BLOCK_SIZE]; |
| 53 | unsigned char k1[AES_BLOCK_SIZE]; |
| 54 | unsigned char k2[AES_BLOCK_SIZE]; |
| 55 | unsigned char k3[AES_BLOCK_SIZE]; |
| 56 | } prfContext; |
| 57 | |
| 58 | |
| 59 | static const unsigned char iv_zero[] = { |
| 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | static CK_RV |
| 70 | sftk_aes_xcbc_get_keys(const unsigned char *keyValue, unsigned int keyLen, |
| 71 | unsigned char *k1, unsigned char *k2, unsigned char *k3) |
| 72 | { |
| 73 | SECStatus rv; |
| 74 | CK_RV crv; |
| 75 | unsigned int tmpLen; |
| 76 | AESContext *aes_context = NULL; |
| 77 | unsigned char newKey[AES_BLOCK_SIZE]; |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | static const unsigned char k1data[] = { |
| 83 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 84 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 |
| 85 | }; |
| 86 | static const unsigned char k2data[] = { |
| 87 | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 88 | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 |
| 89 | }; |
| 90 | static const unsigned char k3data[] = { |
| 91 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 92 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | static const unsigned char k1_0[] = { |
| 97 | 0xe1, 0x4d, 0x5d, 0x0e, 0xe2, 0x77, 0x15, 0xdf, |
| 98 | 0x08, 0xb4, 0x15, 0x2b, 0xa2, 0x3d, 0xa8, 0xe0 |
| 99 | |
| 100 | }; |
| 101 | |
| 102 | static const unsigned char k2_0[] = { |
| 103 | 0x5e, 0xba, 0x73, 0xf8, 0x91, 0x42, 0xc5, 0x48, |
| 104 | 0x80, 0xf6, 0x85, 0x94, 0x37, 0x3c, 0x5c, 0x37 |
| 105 | }; |
| 106 | |
| 107 | static const unsigned char k3_0[] = { |
| 108 | 0x8d, 0x34, 0xef, 0xcb, 0x3b, 0xd5, 0x45, 0xca, |
| 109 | 0x06, 0x2a, 0xec, 0xdf, 0xef, 0x7c, 0x0b, 0xfa |
| 110 | }; |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | if (keyLen < AES_BLOCK_SIZE) { |
| 117 | PORT_Memcpy(newKey, keyValue, keyLen); |
| 118 | PORT_Memset(&newKey[keyLen], 0, AES_BLOCK_SIZE - keyLen); |
| 119 | keyValue = newKey; |
| 120 | } else if (keyLen > AES_BLOCK_SIZE) { |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | aes_context = AES_CreateContext(k1_0, iv_zero, NSS_AES_CBC, |
| 130 | PR_TRUE, AES_BLOCK_SIZE, AES_BLOCK_SIZE); |
| 131 | |
| 132 | while (keyLen > AES_BLOCK_SIZE) { |
| 133 | rv = AES_Encrypt(aes_context, newKey, &tmpLen, AES_BLOCK_SIZE, |
| 134 | keyValue, AES_BLOCK_SIZE); |
| 135 | if (rv != SECSuccess) { |
| 136 | goto fail; |
| 137 | } |
| 138 | keyValue += AES_BLOCK_SIZE; |
| 139 | keyLen -= AES_BLOCK_SIZE; |
| 140 | } |
| 141 | PORT_Memcpy(newKey, keyValue, keyLen); |
| 142 | sftk_xcbc_mac_pad(newKey, keyLen, AES_BLOCK_SIZE, k2_0, k3_0); |
| 143 | rv = AES_Encrypt(aes_context, newKey, &tmpLen, AES_BLOCK_SIZE, |
| 144 | newKey, AES_BLOCK_SIZE); |
| 145 | if (rv != SECSuccess) { |
| 146 | goto fail; |
| 147 | } |
| 148 | keyValue = newKey; |
| 149 | AES_DestroyContext(aes_context, PR_TRUE); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | aes_context = AES_CreateContext(keyValue, iv_zero, |
| 156 | NSS_AES, PR_TRUE, AES_BLOCK_SIZE, AES_BLOCK_SIZE); |
| 157 | if (aes_context == NULL) { |
| 158 | goto fail; |
| 159 | } |
| 160 | rv = AES_Encrypt(aes_context, k1, &tmpLen, AES_BLOCK_SIZE, |
| 161 | k1data, sizeof(k1data)); |
| 162 | if (rv != SECSuccess) { |
| 163 | goto fail; |
| 164 | } |
| 165 | rv = AES_Encrypt(aes_context, k2, &tmpLen, AES_BLOCK_SIZE, |
| 166 | k2data, sizeof(k2data)); |
| 167 | if (rv != SECSuccess) { |
| 168 | goto fail; |
| 169 | } |
| 170 | rv = AES_Encrypt(aes_context, k3, &tmpLen, AES_BLOCK_SIZE, |
| 171 | k3data, sizeof(k3data)); |
| 172 | if (rv != SECSuccess) { |
| 173 | goto fail; |
| 174 | } |
| 175 | AES_DestroyContext(aes_context, PR_TRUE); |
| 176 | PORT_Memset(newKey, 0, AES_BLOCK_SIZE); |
| 177 | return CKR_OK; |
| 178 | fail: |
| 179 | crv = sftk_MapCryptError(PORT_GetError()); |
| 180 | if (aes_context) { |
| 181 | AES_DestroyContext(aes_context, PR_TRUE); |
| 182 | } |
| 183 | PORT_Memset(k1, 0, AES_BLOCK_SIZE); |
| 184 | PORT_Memset(k2, 0, AES_BLOCK_SIZE); |
| 185 | PORT_Memset(k3, 0, AES_BLOCK_SIZE); |
| 186 | PORT_Memset(newKey, 0, AES_BLOCK_SIZE); |
| 187 | return crv; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | CK_RV |
| 192 | sftk_xcbc_mac_pad(unsigned char *padBuf, unsigned int bufLen, |
| 193 | unsigned int blockSize, const unsigned char *k2, |
| 194 | const unsigned char *k3) |
| 195 | { |
| 196 | unsigned int i; |
| 197 | if (bufLen == blockSize) { |
| 198 | for (i = 0; i < blockSize; i++) { |
| 199 | padBuf[i] ^= k2[i]; |
| 200 | } |
| 201 | } else { |
| 202 | padBuf[bufLen++] = 0x80; |
| 203 | for (i = bufLen; i < blockSize; i++) { |
| 204 | padBuf[i] = 0x00; |
| 205 | } |
| 206 | for (i = 0; i < blockSize; i++) { |
| 207 | padBuf[i] ^= k3[i]; |
| 208 | } |
| 209 | } |
| 210 | return CKR_OK; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | static HASH_HashType |
| 217 | sftk_map_hmac_to_hash(CK_MECHANISM_TYPE type) |
| 218 | { |
| 219 | switch (type) { |
| 220 | case CKM_SHA_1_HMAC: |
| 221 | case CKM_SHA_1: |
| 222 | return HASH_AlgSHA1; |
| 223 | case CKM_MD5_HMAC: |
| 224 | case CKM_MD5: |
| 225 | return HASH_AlgMD5; |
| 226 | case CKM_MD2_HMAC: |
| 227 | case CKM_MD2: |
| 228 | return HASH_AlgMD2; |
| 229 | case CKM_SHA224_HMAC: |
| 230 | case CKM_SHA224: |
| 231 | return HASH_AlgSHA224; |
| 232 | case CKM_SHA256_HMAC: |
| 233 | case CKM_SHA256: |
| 234 | return HASH_AlgSHA256; |
| 235 | case CKM_SHA384_HMAC: |
| 236 | case CKM_SHA384: |
| 237 | return HASH_AlgSHA384; |
| 238 | case CKM_SHA512_HMAC: |
| 239 | case CKM_SHA512: |
| 240 | return HASH_AlgSHA512; |
| 241 | } |
| 242 | return HASH_AlgNULL; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | static CK_RV |
| 255 | prf_setup(prfContext *context, CK_MECHANISM_TYPE mech) |
| 256 | { |
| 257 | context->hashType = sftk_map_hmac_to_hash(mech); |
| 258 | context->hashObj = NULL; |
| 259 | context->hmac = NULL; |
| 260 | context->aes = NULL; |
| 261 | if (context->hashType != HASH_AlgNULL) { |
| 262 | context->hashObj = HASH_GetRawHashObject(context->hashType); |
| 263 | if (context->hashObj == NULL) { |
| 264 | return CKR_GENERAL_ERROR; |
| 265 | } |
| 266 | return CKR_OK; |
| 267 | } else if (mech == CKM_AES_XCBC_MAC) { |
| 268 | return CKR_OK; |
| 269 | } |
| 270 | return CKR_MECHANISM_PARAM_INVALID; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | |
| 275 | static CK_RV |
| 276 | prf_length(prfContext *context) |
| 277 | { |
| 278 | if (context->hashObj) { |
| 279 | return context->hashObj->length; |
| 280 | } |
| 281 | return AES_BLOCK_SIZE; |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | static CK_RV |
| 289 | prf_init(prfContext *context, const unsigned char *keyValue, |
| 290 | unsigned int keyLen) |
| 291 | { |
| 292 | CK_RV crv; |
| 293 | |
| 294 | context->hmac = NULL; |
| 295 | if (context->hashObj) { |
| 296 | context->hmac = HMAC_Create(context->hashObj, |
| 297 | keyValue, keyLen, PR_FALSE); |
| 298 | if (context->hmac == NULL) { |
| 299 | return sftk_MapCryptError(PORT_GetError()); |
| 300 | } |
| 301 | HMAC_Begin(context->hmac); |
| 302 | } else { |
| 303 | crv = sftk_aes_xcbc_get_keys(keyValue, keyLen, context->k1, |
| 304 | context->k2, context->k3); |
| 305 | if (crv != CKR_OK) |
| 306 | return crv; |
| 307 | context->nextChar = 0; |
| 308 | context->aes = AES_CreateContext(context->k1, iv_zero, NSS_AES_CBC, |
| 309 | PR_TRUE, sizeof(context->k1), AES_BLOCK_SIZE); |
| 310 | if (context->aes == NULL) { |
| 311 | crv = sftk_MapCryptError(PORT_GetError()); |
| 312 | PORT_Memset(context->k1, 0, sizeof(context->k1)); |
| 313 | PORT_Memset(context->k2, 0, sizeof(context->k2)); |
| 314 | PORT_Memset(context->k3, 0, sizeof(context->k2)); |
| 315 | return crv; |
| 316 | } |
| 317 | } |
| 318 | return CKR_OK; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | static CK_RV |
| 325 | prf_update(prfContext *context, const unsigned char *buf, unsigned int len) |
| 326 | { |
| 327 | unsigned int tmpLen; |
| 328 | SECStatus rv; |
| 329 | |
| 330 | if (context->hmac) { |
| 331 | HMAC_Update(context->hmac, buf, len); |
| 332 | } else { |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | while (context->nextChar + len > AES_BLOCK_SIZE) { |
| 338 | if (context->nextChar != 0) { |
| 339 | |
| 340 | unsigned int left = AES_BLOCK_SIZE - context->nextChar; |
| 341 | |
| 342 | PORT_Memcpy(context->padBuf + context->nextChar, buf, left); |
| 343 | |
| 344 | |
| 345 | |
| 346 | rv = AES_Encrypt(context->aes, context->macBuf, &tmpLen, |
| 347 | sizeof(context->macBuf), context->padBuf, |
| 348 | sizeof(context->padBuf)); |
| 349 | if (rv != SECSuccess) { |
| 350 | return sftk_MapCryptError(PORT_GetError()); |
| 351 | } |
| 352 | context->nextChar = 0; |
| 353 | len -= left; |
| 354 | buf += left; |
| 355 | } else { |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | rv = AES_Encrypt(context->aes, context->macBuf, &tmpLen, |
| 361 | sizeof(context->macBuf), buf, AES_BLOCK_SIZE); |
| 362 | if (rv != SECSuccess) { |
| 363 | return sftk_MapCryptError(PORT_GetError()); |
| 364 | } |
| 365 | len -= AES_BLOCK_SIZE; |
| 366 | buf += AES_BLOCK_SIZE; |
| 367 | } |
| 368 | } |
| 369 | PORT_Memcpy(context->padBuf + context->nextChar, buf, len); |
| 370 | context->nextChar += len; |
| 371 | } |
| 372 | return CKR_OK; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | static void |
| 383 | prf_free(prfContext *context) |
| 384 | { |
| 385 | if (context->hmac) { |
| 386 | HMAC_Destroy(context->hmac, PR_TRUE); |
| 387 | context->hmac = NULL; |
| 388 | } |
| 389 | if (context->aes) { |
| 390 | PORT_Memset(context->k1, 0, sizeof(context->k1)); |
| 391 | PORT_Memset(context->k2, 0, sizeof(context->k2)); |
| 392 | PORT_Memset(context->k3, 0, sizeof(context->k2)); |
| 393 | PORT_Memset(context->padBuf, 0, sizeof(context->padBuf)); |
| 394 | PORT_Memset(context->macBuf, 0, sizeof(context->macBuf)); |
| 395 | AES_DestroyContext(context->aes, PR_TRUE); |
| 396 | context->aes = NULL; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | static CK_RV |
| 405 | prf_final(prfContext *context, unsigned char *buf, unsigned int len) |
| 406 | { |
| 407 | unsigned int tmpLen; |
| 408 | SECStatus rv; |
| 409 | |
| 410 | if (context->hmac) { |
| 8 | | Assuming field 'hmac' is null | |
|
| |
| 411 | unsigned int outLen; |
| 412 | HMAC_Finish(context->hmac, buf, &outLen, len); |
| 413 | if (outLen != len) { |
| 414 | return CKR_GENERAL_ERROR; |
| 415 | } |
| 416 | } else { |
| 417 | |
| 418 | |
| 419 | |
| 420 | CK_RV crv = sftk_xcbc_mac_pad(context->padBuf, context->nextChar, |
| 421 | AES_BLOCK_SIZE, context->k2, context->k3); |
| 422 | if (crv != CKR_OK) { |
| 10 | | Assuming 'crv' is equal to CKR_OK | |
|
| |
| 423 | return crv; |
| 424 | } |
| 425 | rv = AES_Encrypt(context->aes, context->macBuf, &tmpLen, |
| 426 | sizeof(context->macBuf), context->padBuf, AES_BLOCK_SIZE); |
| 427 | if (rv != SECSuccess) { |
| 12 | | Assuming 'rv' is not equal to SECSuccess | |
|
| |
| 428 | return sftk_MapCryptError(PORT_GetError()); |
| 14 | | Returning without writing to '*buf' | |
|
| 429 | } |
| 430 | PORT_Memcpy(buf, context->macBuf, len); |
| 431 | } |
| 432 | prf_free(context); |
| 433 | return CKR_OK; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | CK_RV |
| 461 | sftk_ike_prf(CK_SESSION_HANDLE hSession, const SFTKAttribute *inKey, |
| 462 | const CK_IKE_PRF_DERIVE_PARAMS *params, SFTKObject *outKey) |
| 463 | { |
| 464 | SFTKAttribute *newKeyValue = NULL; |
| 465 | SFTKObject *newKeyObj = NULL; |
| 466 | unsigned char outKeyData[HASH_LENGTH_MAX]; |
| 467 | unsigned char *newInKey = NULL; |
| 468 | unsigned int newInKeySize = 0; |
| 469 | unsigned int macSize; |
| 470 | CK_RV crv = CKR_OK; |
| 471 | prfContext context; |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | if (params->ulNiLen > 0xffff || params->ulNrLen > 0xffff) { |
| 480 | return CKR_MECHANISM_PARAM_INVALID; |
| 481 | } |
| 482 | |
| 483 | crv = prf_setup(&context, params->prfMechanism); |
| 484 | if (crv != CKR_OK) { |
| 485 | return crv; |
| 486 | } |
| 487 | macSize = prf_length(&context); |
| 488 | if ((params->bDataAsKey) && (params->bRekey)) { |
| 489 | return CKR_ARGUMENTS_BAD; |
| 490 | } |
| 491 | if (params->bRekey) { |
| 492 | |
| 493 | SFTKSession *session = sftk_SessionFromHandle(hSession); |
| 494 | if (session == NULL) { |
| 495 | return CKR_SESSION_HANDLE_INVALID; |
| 496 | } |
| 497 | newKeyObj = sftk_ObjectFromHandle(params->hNewKey, session); |
| 498 | sftk_FreeSession(session); |
| 499 | if (newKeyObj == NULL) { |
| 500 | return CKR_KEY_HANDLE_INVALID; |
| 501 | } |
| 502 | newKeyValue = sftk_FindAttribute(newKeyObj, CKA_VALUE); |
| 503 | if (newKeyValue == NULL) { |
| 504 | crv = CKR_KEY_HANDLE_INVALID; |
| 505 | goto fail; |
| 506 | } |
| 507 | } |
| 508 | if (params->bDataAsKey) { |
| 509 | |
| 510 | newInKeySize = params->ulNiLen + params->ulNrLen; |
| 511 | newInKey = PORT_Alloc(newInKeySize); |
| 512 | if (newInKey == NULL) { |
| 513 | crv = CKR_HOST_MEMORY; |
| 514 | goto fail; |
| 515 | } |
| 516 | PORT_Memcpy(newInKey, params->pNi, params->ulNiLen); |
| 517 | PORT_Memcpy(newInKey + params->ulNiLen, params->pNr, params->ulNrLen); |
| 518 | crv = prf_init(&context, newInKey, newInKeySize); |
| 519 | if (crv != CKR_OK) { |
| 520 | goto fail; |
| 521 | } |
| 522 | |
| 523 | crv = prf_update(&context, inKey->attrib.pValue, |
| 524 | inKey->attrib.ulValueLen); |
| 525 | if (crv != CKR_OK) { |
| 526 | goto fail; |
| 527 | } |
| 528 | } else { |
| 529 | |
| 530 | |
| 531 | if (!params->bRekey) { |
| 532 | sftk_setFIPS(outKey, PR_FALSE); |
| 533 | } |
| 534 | |
| 535 | crv = prf_init(&context, inKey->attrib.pValue, |
| 536 | inKey->attrib.ulValueLen); |
| 537 | if (crv != CKR_OK) { |
| 538 | goto fail; |
| 539 | } |
| 540 | if (newKeyValue) { |
| 541 | crv = prf_update(&context, newKeyValue->attrib.pValue, |
| 542 | newKeyValue->attrib.ulValueLen); |
| 543 | if (crv != CKR_OK) { |
| 544 | goto fail; |
| 545 | } |
| 546 | } |
| 547 | crv = prf_update(&context, params->pNi, params->ulNiLen); |
| 548 | if (crv != CKR_OK) { |
| 549 | goto fail; |
| 550 | } |
| 551 | crv = prf_update(&context, params->pNr, params->ulNrLen); |
| 552 | if (crv != CKR_OK) { |
| 553 | goto fail; |
| 554 | } |
| 555 | } |
| 556 | crv = prf_final(&context, outKeyData, macSize); |
| 557 | if (crv != CKR_OK) { |
| 558 | goto fail; |
| 559 | } |
| 560 | |
| 561 | crv = sftk_forceAttribute(outKey, CKA_VALUE, outKeyData, macSize); |
| 562 | fail: |
| 563 | if (newInKey) { |
| 564 | PORT_ZFree(newInKey, newInKeySize); |
| 565 | } |
| 566 | if (newKeyValue) { |
| 567 | sftk_FreeAttribute(newKeyValue); |
| 568 | } |
| 569 | if (newKeyObj) { |
| 570 | sftk_FreeObject(newKeyObj); |
| 571 | } |
| 572 | PORT_Memset(outKeyData, 0, macSize); |
| 573 | prf_free(&context); |
| 574 | return crv; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | CK_RV |
| 600 | sftk_ike1_prf(CK_SESSION_HANDLE hSession, const SFTKAttribute *inKey, |
| 601 | const CK_IKE1_PRF_DERIVE_PARAMS *params, SFTKObject *outKey, |
| 602 | unsigned int keySize) |
| 603 | { |
| 604 | SFTKAttribute *gxyKeyValue = NULL; |
| 605 | SFTKObject *gxyKeyObj = NULL; |
| 606 | SFTKAttribute *prevKeyValue = NULL; |
| 607 | SFTKObject *prevKeyObj = NULL; |
| 608 | SFTKSession *session; |
| 609 | unsigned char outKeyData[HASH_LENGTH_MAX]; |
| 610 | unsigned int macSize; |
| 611 | CK_RV crv; |
| 612 | prfContext context; |
| 613 | |
| 614 | crv = prf_setup(&context, params->prfMechanism); |
| 615 | if (crv != CKR_OK) { |
| 616 | return crv; |
| 617 | } |
| 618 | macSize = prf_length(&context); |
| 619 | if (keySize > macSize) { |
| 620 | return CKR_KEY_SIZE_RANGE; |
| 621 | } |
| 622 | if (keySize == 0) { |
| 623 | keySize = macSize; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | session = sftk_SessionFromHandle(hSession); |
| 628 | if (session == NULL) { |
| 629 | return CKR_SESSION_HANDLE_INVALID; |
| 630 | } |
| 631 | gxyKeyObj = sftk_ObjectFromHandle(params->hKeygxy, session); |
| 632 | if (params->bHasPrevKey) { |
| 633 | prevKeyObj = sftk_ObjectFromHandle(params->hPrevKey, session); |
| 634 | } |
| 635 | sftk_FreeSession(session); |
| 636 | if ((gxyKeyObj == NULL) || ((params->bHasPrevKey) && |
| 637 | (prevKeyObj == NULL))) { |
| 638 | crv = CKR_KEY_HANDLE_INVALID; |
| 639 | goto fail; |
| 640 | } |
| 641 | gxyKeyValue = sftk_FindAttribute(gxyKeyObj, CKA_VALUE); |
| 642 | if (gxyKeyValue == NULL) { |
| 643 | crv = CKR_KEY_HANDLE_INVALID; |
| 644 | goto fail; |
| 645 | } |
| 646 | if (prevKeyObj) { |
| 647 | prevKeyValue = sftk_FindAttribute(prevKeyObj, CKA_VALUE); |
| 648 | if (prevKeyValue == NULL) { |
| 649 | crv = CKR_KEY_HANDLE_INVALID; |
| 650 | goto fail; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | |
| 655 | crv = prf_init(&context, inKey->attrib.pValue, inKey->attrib.ulValueLen); |
| 656 | if (crv != CKR_OK) { |
| 657 | goto fail; |
| 658 | } |
| 659 | if (prevKeyValue) { |
| 660 | crv = prf_update(&context, prevKeyValue->attrib.pValue, |
| 661 | prevKeyValue->attrib.ulValueLen); |
| 662 | if (crv != CKR_OK) { |
| 663 | goto fail; |
| 664 | } |
| 665 | } |
| 666 | crv = prf_update(&context, gxyKeyValue->attrib.pValue, |
| 667 | gxyKeyValue->attrib.ulValueLen); |
| 668 | if (crv != CKR_OK) { |
| 669 | goto fail; |
| 670 | } |
| 671 | crv = prf_update(&context, params->pCKYi, params->ulCKYiLen); |
| 672 | if (crv != CKR_OK) { |
| 673 | goto fail; |
| 674 | } |
| 675 | crv = prf_update(&context, params->pCKYr, params->ulCKYrLen); |
| 676 | if (crv != CKR_OK) { |
| 677 | goto fail; |
| 678 | } |
| 679 | crv = prf_update(&context, ¶ms->keyNumber, 1); |
| 680 | if (crv != CKR_OK) { |
| 681 | goto fail; |
| 682 | } |
| 683 | crv = prf_final(&context, outKeyData, macSize); |
| 684 | if (crv != CKR_OK) { |
| 685 | goto fail; |
| 686 | } |
| 687 | |
| 688 | crv = sftk_forceAttribute(outKey, CKA_VALUE, outKeyData, keySize); |
| 689 | fail: |
| 690 | if (gxyKeyValue) { |
| 691 | sftk_FreeAttribute(gxyKeyValue); |
| 692 | } |
| 693 | if (prevKeyValue) { |
| 694 | sftk_FreeAttribute(prevKeyValue); |
| 695 | } |
| 696 | if (gxyKeyObj) { |
| 697 | sftk_FreeObject(gxyKeyObj); |
| 698 | } |
| 699 | if (prevKeyObj) { |
| 700 | sftk_FreeObject(prevKeyObj); |
| 701 | } |
| 702 | PORT_Memset(outKeyData, 0, macSize); |
| 703 | prf_free(&context); |
| 704 | return crv; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | CK_RV |
| 727 | sftk_ike1_appendix_b_prf(CK_SESSION_HANDLE hSession, const SFTKAttribute *inKey, |
| 728 | const CK_IKE1_EXTENDED_DERIVE_PARAMS *params, |
| 729 | SFTKObject *outKey, unsigned int keySize) |
| 730 | { |
| 731 | SFTKAttribute *gxyKeyValue = NULL; |
| 732 | SFTKObject *gxyKeyObj = NULL; |
| 733 | unsigned char *outKeyData = NULL; |
| 734 | unsigned char *thisKey = NULL; |
| 735 | unsigned char *lastKey = NULL; |
| 736 | unsigned int macSize; |
| 737 | unsigned int outKeySize; |
| 738 | unsigned int genKeySize; |
| 739 | PRBool quickMode = PR_FALSE; |
| 740 | CK_RV crv; |
| 741 | prfContext context; |
| 742 | |
| 743 | if ((params->ulExtraDataLen != 0) && (params->pExtraData == NULL)) { |
| 744 | return CKR_ARGUMENTS_BAD; |
| 745 | } |
| 746 | crv = prf_setup(&context, params->prfMechanism); |
| 747 | if (crv != CKR_OK) { |
| 748 | return crv; |
| 749 | } |
| 750 | |
| 751 | if (params->bHasKeygxy) { |
| 752 | SFTKSession *session; |
| 753 | session = sftk_SessionFromHandle(hSession); |
| 754 | if (session == NULL) { |
| 755 | return CKR_SESSION_HANDLE_INVALID; |
| 756 | } |
| 757 | gxyKeyObj = sftk_ObjectFromHandle(params->hKeygxy, session); |
| 758 | sftk_FreeSession(session); |
| 759 | if (gxyKeyObj == NULL) { |
| 760 | crv = CKR_KEY_HANDLE_INVALID; |
| 761 | goto fail; |
| 762 | } |
| 763 | gxyKeyValue = sftk_FindAttribute(gxyKeyObj, CKA_VALUE); |
| 764 | if (gxyKeyValue == NULL) { |
| 765 | crv = CKR_KEY_HANDLE_INVALID; |
| 766 | goto fail; |
| 767 | } |
| 768 | quickMode = PR_TRUE; |
| 769 | } |
| 770 | |
| 771 | if (params->ulExtraDataLen != 0) { |
| 772 | quickMode = PR_TRUE; |
| 773 | } |
| 774 | |
| 775 | macSize = prf_length(&context); |
| 776 | |
| 777 | if (keySize == 0) { |
| 778 | keySize = macSize; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 | if ((!quickMode) && (keySize <= inKey->attrib.ulValueLen)) { |
| 787 | return sftk_forceAttribute(outKey, CKA_VALUE, |
| 788 | inKey->attrib.pValue, keySize); |
| 789 | } |
| 790 | |
| 791 | outKeySize = PR_ROUNDUP(keySize, macSize); |
| 792 | |
| 793 | |
| 794 | if (outKeySize < keySize) { |
| 795 | crv = CKR_KEY_SIZE_RANGE; |
| 796 | goto fail; |
| 797 | } |
| 798 | outKeyData = PORT_Alloc(outKeySize); |
| 799 | if (outKeyData == NULL) { |
| 800 | crv = CKR_HOST_MEMORY; |
| 801 | goto fail; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | thisKey = outKeyData; |
| 811 | for (genKeySize = 0; genKeySize < keySize; genKeySize += macSize) { |
| 812 | PRBool hashedData = PR_FALSE; |
| 813 | crv = prf_init(&context, inKey->attrib.pValue, inKey->attrib.ulValueLen); |
| 814 | if (crv != CKR_OK) { |
| 815 | goto fail; |
| 816 | } |
| 817 | if (lastKey != NULL) { |
| 818 | crv = prf_update(&context, lastKey, macSize); |
| 819 | if (crv != CKR_OK) { |
| 820 | goto fail; |
| 821 | } |
| 822 | hashedData = PR_TRUE; |
| 823 | } |
| 824 | if (gxyKeyValue != NULL) { |
| 825 | crv = prf_update(&context, gxyKeyValue->attrib.pValue, |
| 826 | gxyKeyValue->attrib.ulValueLen); |
| 827 | if (crv != CKR_OK) { |
| 828 | goto fail; |
| 829 | } |
| 830 | hashedData = PR_TRUE; |
| 831 | } |
| 832 | if (params->ulExtraDataLen != 0) { |
| 833 | crv = prf_update(&context, params->pExtraData, params->ulExtraDataLen); |
| 834 | if (crv != CKR_OK) { |
| 835 | goto fail; |
| 836 | } |
| 837 | hashedData = PR_TRUE; |
| 838 | } |
| 839 | |
| 840 | if (hashedData == PR_FALSE) { |
| 841 | const unsigned char zero = 0; |
| 842 | crv = prf_update(&context, &zero, 1); |
| 843 | if (crv != CKR_OK) { |
| 844 | goto fail; |
| 845 | } |
| 846 | } |
| 847 | crv = prf_final(&context, thisKey, macSize); |
| 848 | if (crv != CKR_OK) { |
| 849 | goto fail; |
| 850 | } |
| 851 | lastKey = thisKey; |
| 852 | thisKey += macSize; |
| 853 | } |
| 854 | crv = sftk_forceAttribute(outKey, CKA_VALUE, outKeyData, keySize); |
| 855 | fail: |
| 856 | if (gxyKeyValue) { |
| 857 | sftk_FreeAttribute(gxyKeyValue); |
| 858 | } |
| 859 | if (gxyKeyObj) { |
| 860 | sftk_FreeObject(gxyKeyObj); |
| 861 | } |
| 862 | if (outKeyData) { |
| 863 | PORT_ZFree(outKeyData, outKeySize); |
| 864 | } |
| 865 | prf_free(&context); |
| 866 | return crv; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | static CK_RV |
| 893 | sftk_ike_prf_plus_raw(CK_SESSION_HANDLE hSession, |
| 894 | const unsigned char *inKeyData, CK_ULONG inKeyLen, |
| 895 | const CK_IKE2_PRF_PLUS_DERIVE_PARAMS *params, |
| 896 | unsigned char **outKeyDataPtr, unsigned int *outKeySizePtr, |
| 897 | unsigned int keySize) |
| 898 | { |
| 899 | SFTKAttribute *seedValue = NULL; |
| 900 | SFTKObject *seedKeyObj = NULL; |
| 901 | unsigned char *outKeyData = NULL; |
| 902 | unsigned int outKeySize; |
| 903 | unsigned char *thisKey; |
| 904 | unsigned char *lastKey = NULL; |
| 905 | unsigned char currentByte = 0; |
| 906 | unsigned int getKeySize; |
| 907 | unsigned int macSize; |
| 908 | CK_RV crv; |
| 909 | prfContext context; |
| 910 | |
| 911 | if (keySize == 0) { |
| 912 | return CKR_KEY_SIZE_RANGE; |
| 913 | } |
| 914 | |
| 915 | crv = prf_setup(&context, params->prfMechanism); |
| 916 | if (crv != CKR_OK) { |
| 917 | return crv; |
| 918 | } |
| 919 | |
| 920 | if (params->bHasSeedKey) { |
| 921 | SFTKSession *session = sftk_SessionFromHandle(hSession); |
| 922 | if (session == NULL) { |
| 923 | return CKR_SESSION_HANDLE_INVALID; |
| 924 | } |
| 925 | seedKeyObj = sftk_ObjectFromHandle(params->hSeedKey, session); |
| 926 | sftk_FreeSession(session); |
| 927 | if (seedKeyObj == NULL) { |
| 928 | return CKR_KEY_HANDLE_INVALID; |
| 929 | } |
| 930 | seedValue = sftk_FindAttribute(seedKeyObj, CKA_VALUE); |
| 931 | if (seedValue == NULL) { |
| 932 | crv = CKR_KEY_HANDLE_INVALID; |
| 933 | goto fail; |
| 934 | } |
| 935 | } else if (params->ulSeedDataLen == 0) { |
| 936 | crv = CKR_ARGUMENTS_BAD; |
| 937 | goto fail; |
| 938 | } |
| 939 | macSize = prf_length(&context); |
| 940 | |
| 941 | |
| 942 | if (keySize > 255 * macSize) { |
| 943 | crv = CKR_KEY_SIZE_RANGE; |
| 944 | goto fail; |
| 945 | } |
| 946 | outKeySize = PR_ROUNDUP(keySize, macSize); |
| 947 | outKeyData = PORT_Alloc(outKeySize); |
| 948 | if (outKeyData == NULL) { |
| 949 | crv = CKR_HOST_MEMORY; |
| 950 | goto fail; |
| 951 | } |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | thisKey = outKeyData; |
| 960 | for (getKeySize = 0; getKeySize < keySize; getKeySize += macSize) { |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | if (currentByte == 255) { |
| 966 | crv = CKR_KEY_SIZE_RANGE; |
| 967 | goto fail; |
| 968 | } |
| 969 | crv = prf_init(&context, inKeyData, inKeyLen); |
| 970 | if (crv != CKR_OK) { |
| 971 | goto fail; |
| 972 | } |
| 973 | |
| 974 | if (lastKey) { |
| 975 | crv = prf_update(&context, lastKey, macSize); |
| 976 | if (crv != CKR_OK) { |
| 977 | goto fail; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | if (seedValue) { |
| 982 | crv = prf_update(&context, seedValue->attrib.pValue, |
| 983 | seedValue->attrib.ulValueLen); |
| 984 | if (crv != CKR_OK) { |
| 985 | goto fail; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | if (params->ulSeedDataLen != 0) { |
| 990 | crv = prf_update(&context, params->pSeedData, |
| 991 | params->ulSeedDataLen); |
| 992 | if (crv != CKR_OK) { |
| 993 | goto fail; |
| 994 | } |
| 995 | } |
| 996 | currentByte++; |
| 997 | crv = prf_update(&context, ¤tByte, 1); |
| 998 | if (crv != CKR_OK) { |
| 999 | goto fail; |
| 1000 | } |
| 1001 | crv = prf_final(&context, thisKey, macSize); |
| 1002 | if (crv != CKR_OK) { |
| 1003 | goto fail; |
| 1004 | } |
| 1005 | lastKey = thisKey; |
| 1006 | thisKey += macSize; |
| 1007 | } |
| 1008 | *outKeyDataPtr = outKeyData; |
| 1009 | *outKeySizePtr = outKeySize; |
| 1010 | outKeyData = NULL; |
| 1011 | fail: |
| 1012 | if (outKeyData) { |
| 1013 | PORT_ZFree(outKeyData, outKeySize); |
| 1014 | } |
| 1015 | if (seedValue) { |
| 1016 | sftk_FreeAttribute(seedValue); |
| 1017 | } |
| 1018 | if (seedKeyObj) { |
| 1019 | sftk_FreeObject(seedKeyObj); |
| 1020 | } |
| 1021 | prf_free(&context); |
| 1022 | return crv; |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | CK_RV |
| 1029 | sftk_ike_prf_plus(CK_SESSION_HANDLE hSession, const SFTKAttribute *inKey, |
| 1030 | const CK_IKE2_PRF_PLUS_DERIVE_PARAMS *params, SFTKObject *outKey, |
| 1031 | unsigned int keySize) |
| 1032 | { |
| 1033 | unsigned char *outKeyData = NULL; |
| 1034 | unsigned int outKeySize; |
| 1035 | CK_RV crv; |
| 1036 | |
| 1037 | crv = sftk_ike_prf_plus_raw(hSession, inKey->attrib.pValue, |
| 1038 | inKey->attrib.ulValueLen, params, |
| 1039 | &outKeyData, &outKeySize, keySize); |
| 1040 | if (crv != CKR_OK) { |
| 1041 | return crv; |
| 1042 | } |
| 1043 | |
| 1044 | crv = sftk_forceAttribute(outKey, CKA_VALUE, outKeyData, keySize); |
| 1045 | PORT_ZFree(outKeyData, outKeySize); |
| 1046 | return crv; |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | CK_RV |
| 1060 | sftk_aes_xcbc_new_keys(CK_SESSION_HANDLE hSession, |
| 1061 | CK_OBJECT_HANDLE hKey, CK_OBJECT_HANDLE_PTR phKey, |
| 1062 | unsigned char *k2, unsigned char *k3) |
| 1063 | { |
| 1064 | SFTKObject *key = NULL; |
| 1065 | SFTKSession *session = NULL; |
| 1066 | SFTKObject *inKeyObj = NULL; |
| 1067 | SFTKAttribute *inKeyValue = NULL; |
| 1068 | CK_KEY_TYPE key_type = CKK_AES; |
| 1069 | CK_OBJECT_CLASS objclass = CKO_SECRET_KEY; |
| 1070 | CK_BBOOL ck_true = CK_TRUE; |
| 1071 | CK_RV crv = CKR_OK; |
| 1072 | SFTKSlot *slot = sftk_SlotFromSessionHandle(hSession); |
| 1073 | unsigned char buf[AES_BLOCK_SIZE]; |
| 1074 | |
| 1075 | if (!slot) { |
| 1076 | return CKR_SESSION_HANDLE_INVALID; |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | session = sftk_SessionFromHandle(hSession); |
| 1081 | if (session == NULL) { |
| 1082 | crv = CKR_SESSION_HANDLE_INVALID; |
| 1083 | goto fail; |
| 1084 | } |
| 1085 | |
| 1086 | inKeyObj = sftk_ObjectFromHandle(hKey, session); |
| 1087 | if (inKeyObj == NULL) { |
| 1088 | crv = CKR_KEY_HANDLE_INVALID; |
| 1089 | goto fail; |
| 1090 | } |
| 1091 | |
| 1092 | inKeyValue = sftk_FindAttribute(inKeyObj, CKA_VALUE); |
| 1093 | if (inKeyValue == NULL) { |
| 1094 | crv = CKR_KEY_HANDLE_INVALID; |
| 1095 | goto fail; |
| 1096 | } |
| 1097 | |
| 1098 | crv = sftk_aes_xcbc_get_keys(inKeyValue->attrib.pValue, |
| 1099 | inKeyValue->attrib.ulValueLen, buf, k2, k3); |
| 1100 | |
| 1101 | if (crv != CKR_OK) { |
| 1102 | goto fail; |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | key = sftk_NewObject(slot); |
| 1109 | if (key == NULL) { |
| 1110 | crv = CKR_HOST_MEMORY; |
| 1111 | goto fail; |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | sftk_DeleteAttributeType(key, CKA_CLASS); |
| 1116 | sftk_DeleteAttributeType(key, CKA_KEY_TYPE); |
| 1117 | sftk_DeleteAttributeType(key, CKA_VALUE); |
| 1118 | sftk_DeleteAttributeType(key, CKA_SIGN); |
| 1119 | |
| 1120 | |
| 1121 | crv = sftk_AddAttributeType(key, CKA_CLASS, &objclass, sizeof(CK_OBJECT_CLASS)); |
| 1122 | if (crv != CKR_OK) { |
| 1123 | goto fail; |
| 1124 | } |
| 1125 | crv = sftk_AddAttributeType(key, CKA_KEY_TYPE, &key_type, sizeof(CK_KEY_TYPE)); |
| 1126 | if (crv != CKR_OK) { |
| 1127 | goto fail; |
| 1128 | } |
| 1129 | crv = sftk_AddAttributeType(key, CKA_SIGN, &ck_true, sizeof(CK_BBOOL)); |
| 1130 | if (crv != CKR_OK) { |
| 1131 | goto fail; |
| 1132 | } |
| 1133 | crv = sftk_AddAttributeType(key, CKA_VALUE, buf, AES_BLOCK_SIZE); |
| 1134 | if (crv != CKR_OK) { |
| 1135 | goto fail; |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | |
| 1141 | crv = sftk_handleObject(key, session); |
| 1142 | if (crv != CKR_OK) { |
| 1143 | goto fail; |
| 1144 | } |
| 1145 | *phKey = key->handle; |
| 1146 | fail: |
| 1147 | if (session) { |
| 1148 | sftk_FreeSession(session); |
| 1149 | } |
| 1150 | |
| 1151 | if (inKeyValue) { |
| 1152 | sftk_FreeAttribute(inKeyValue); |
| 1153 | } |
| 1154 | if (inKeyObj) { |
| 1155 | sftk_FreeObject(inKeyObj); |
| 1156 | } |
| 1157 | if (key) { |
| 1158 | sftk_FreeObject(key); |
| 1159 | } |
| 1160 | |
| 1161 | PORT_Memset(buf, 0, sizeof(buf)); |
| 1162 | if (crv != CKR_OK) { |
| 1163 | PORT_Memset(k2, 0, AES_BLOCK_SIZE); |
| 1164 | PORT_Memset(k3, 0, AES_BLOCK_SIZE); |
| 1165 | } |
| 1166 | return crv; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | |
| 1172 | static SECStatus |
| 1173 | prf_test(CK_MECHANISM_TYPE mech, |
| 1174 | const unsigned char *inKey, unsigned int inKeyLen, |
| 1175 | const unsigned char *plainText, unsigned int plainTextLen, |
| 1176 | const unsigned char *expectedResult, unsigned int expectedResultLen) |
| 1177 | { |
| 1178 | PRUint8 ike_computed_mac[HASH_LENGTH_MAX]; |
| 2 | | 'ike_computed_mac' declared without an initial value | |
|
| 1179 | prfContext context; |
| 1180 | unsigned int macSize; |
| 1181 | CK_RV crv; |
| 1182 | |
| 1183 | crv = prf_setup(&context, mech); |
| 1184 | if (crv != CKR_OK) { |
| |
| 1185 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
| 1186 | return SECFailure; |
| 1187 | } |
| 1188 | macSize = prf_length(&context); |
| 1189 | crv = prf_init(&context, inKey, inKeyLen); |
| 1190 | if (crv != CKR_OK) { |
| |
| 1191 | goto fail; |
| 1192 | } |
| 1193 | crv = prf_update(&context, plainText, plainTextLen); |
| 1194 | if (crv != CKR_OK) { |
| 5 | | Assuming 'crv' is equal to CKR_OK | |
|
| |
| 1195 | goto fail; |
| 1196 | } |
| 1197 | crv = prf_final(&context, ike_computed_mac, macSize); |
| |
| 15 | | Returning from 'prf_final' | |
|
| 1198 | if (crv != CKR_OK) { |
| 16 | | Assuming 'crv' is equal to CKR_OK | |
|
| |
| 1199 | goto fail; |
| 1200 | } |
| 1201 | |
| 1202 | if (macSize != expectedResultLen) { |
| |
| 1203 | goto fail; |
| 1204 | } |
| 1205 | if (PORT_Memcmp(expectedResult, ike_computed_mac, macSize) != 0) { |
| Other elements might also be undefined |
| 19 | | The first element of the 2nd argument is undefined |
|
| 1206 | goto fail; |
| 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | if (plainTextLen <= macSize) { |
| 1211 | return SECSuccess; |
| 1212 | } |
| 1213 | prf_free(&context); |
| 1214 | |
| 1215 | crv = prf_init(&context, inKey, inKeyLen); |
| 1216 | if (crv != CKR_OK) { |
| 1217 | goto fail; |
| 1218 | } |
| 1219 | crv = prf_update(&context, plainText, 1); |
| 1220 | if (crv != CKR_OK) { |
| 1221 | goto fail; |
| 1222 | } |
| 1223 | crv = prf_update(&context, &plainText[1], macSize); |
| 1224 | if (crv != CKR_OK) { |
| 1225 | goto fail; |
| 1226 | } |
| 1227 | crv = prf_update(&context, &plainText[1 + macSize], plainTextLen - (macSize + 1)); |
| 1228 | if (crv != CKR_OK) { |
| 1229 | goto fail; |
| 1230 | } |
| 1231 | crv = prf_final(&context, ike_computed_mac, macSize); |
| 1232 | if (crv != CKR_OK) { |
| 1233 | goto fail; |
| 1234 | } |
| 1235 | if (PORT_Memcmp(expectedResult, ike_computed_mac, macSize) != 0) { |
| 1236 | goto fail; |
| 1237 | } |
| 1238 | prf_free(&context); |
| 1239 | return SECSuccess; |
| 1240 | fail: |
| 1241 | prf_free(&context); |
| 1242 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
| 1243 | return SECFailure; |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | SECStatus |
| 1251 | sftk_fips_IKE_PowerUpSelfTests(void) |
| 1252 | { |
| 1253 | |
| 1254 | static const PRUint8 ike_xcbc_known_key[] = { |
| 1255 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 1256 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 1257 | }; |
| 1258 | static const PRUint8 ike_xcbc_known_plain_text[] = { |
| 1259 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 1260 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f |
| 1261 | }; |
| 1262 | static const PRUint8 ike_xcbc_known_mac[] = { |
| 1263 | 0xd2, 0xa2, 0x46, 0xfa, 0x34, 0x9b, 0x68, 0xa7, |
| 1264 | 0x99, 0x98, 0xa4, 0x39, 0x4f, 0xf7, 0xa2, 0x63 |
| 1265 | }; |
| 1266 | |
| 1267 | static const PRUint8 ike_xcbc_known_plain_text_2[] = { |
| 1268 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 1269 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 1270 | 0x10, 0x11, 0x12, 0x13 |
| 1271 | }; |
| 1272 | static const PRUint8 ike_xcbc_known_mac_2[] = { |
| 1273 | 0x47, 0xf5, 0x1b, 0x45, 0x64, 0x96, 0x62, 0x15, |
| 1274 | 0xb8, 0x98, 0x5c, 0x63, 0x05, 0x5e, 0xd3, 0x08 |
| 1275 | }; |
| 1276 | static const PRUint8 ike_xcbc_known_key_3[] = { |
| 1277 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 1278 | 0x08, 0x09 |
| 1279 | }; |
| 1280 | |
| 1281 | static const PRUint8 ike_xcbc_known_mac_3[] = { |
| 1282 | 0x0f, 0xa0, 0x87, 0xaf, 0x7d, 0x86, 0x6e, 0x76, |
| 1283 | 0x53, 0x43, 0x4e, 0x60, 0x2f, 0xdd, 0xe8, 0x35 |
| 1284 | }; |
| 1285 | static const PRUint8 ike_xcbc_known_key_4[] = { |
| 1286 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 1287 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 1288 | 0xed, 0xcb |
| 1289 | }; |
| 1290 | |
| 1291 | static const PRUint8 ike_xcbc_known_mac_4[] = { |
| 1292 | 0x8c, 0xd3, 0xc9, 0x3a, 0xe5, 0x98, 0xa9, 0x80, |
| 1293 | 0x30, 0x06, 0xff, 0xb6, 0x7c, 0x40, 0xe9, 0xe4 |
| 1294 | }; |
| 1295 | static const PRUint8 ike_sha1_known_key[] = { |
| 1296 | 0x59, 0x98, 0x2b, 0x5b, 0xa5, 0x7e, 0x62, 0xc0, |
| 1297 | 0x46, 0x0d, 0xef, 0xc7, 0x1e, 0x18, 0x64, 0x63 |
| 1298 | }; |
| 1299 | static const PRUint8 ike_sha1_known_plain_text[] = { |
| 1300 | 0x1c, 0x07, 0x32, 0x1a, 0x9a, 0x7e, 0x41, 0xcd, |
| 1301 | 0x88, 0x0c, 0xa3, 0x7a, 0xdb, 0x10, 0xc7, 0x3b, |
| 1302 | 0xf0, 0x0e, 0x7a, 0xe3, 0xcf, 0xc6, 0xfd, 0x8b, |
| 1303 | 0x51, 0xbc, 0xe2, 0xb9, 0x90, 0xe6, 0xf2, 0x01 |
| 1304 | }; |
| 1305 | static const PRUint8 ike_sha1_known_mac[] = { |
| 1306 | 0x0c, 0x2a, 0xf3, 0x42, 0x97, 0x15, 0x62, 0x1d, |
| 1307 | 0x2a, 0xad, 0xc9, 0x94, 0x5a, 0x90, 0x26, 0xfa, |
| 1308 | 0xc7, 0x91, 0xe2, 0x4b |
| 1309 | }; |
| 1310 | static const PRUint8 ike_sha256_known_key[] = { |
| 1311 | 0x9d, 0xa2, 0xd5, 0x8f, 0x57, 0xf0, 0x39, 0xf9, |
| 1312 | 0x20, 0x4e, 0x0d, 0xd0, 0xef, 0x04, 0xf3, 0x72 |
| 1313 | }; |
| 1314 | static const PRUint8 ike_sha256_known_plain_text[] = { |
| 1315 | 0x33, 0xf1, 0x7a, 0xfc, 0xb6, 0x13, 0x4c, 0xbf, |
| 1316 | 0x1c, 0xab, 0x59, 0x87, 0x7d, 0x42, 0xdb, 0x35, |
| 1317 | 0x82, 0x22, 0x6e, 0xff, 0x74, 0xdd, 0x37, 0xeb, |
| 1318 | 0x8b, 0x75, 0xe6, 0x75, 0x64, 0x5f, 0xc1, 0x69 |
| 1319 | }; |
| 1320 | static const PRUint8 ike_sha256_known_mac[] = { |
| 1321 | 0x80, 0x4b, 0x4a, 0x1e, 0x0e, 0xc5, 0x93, 0xcf, |
| 1322 | 0xb6, 0xe4, 0x54, 0x52, 0x41, 0x49, 0x39, 0x6d, |
| 1323 | 0xe2, 0x34, 0xd0, 0xda, 0xe2, 0x9f, 0x34, 0xa8, |
| 1324 | 0xfd, 0xb5, 0xf9, 0xaf, 0xe7, 0x6e, 0xa6, 0x52 |
| 1325 | }; |
| 1326 | static const PRUint8 ike_sha384_known_key[] = { |
| 1327 | 0xce, 0xc8, 0x9d, 0x84, 0x5a, 0xdd, 0x83, 0xef, |
| 1328 | 0xce, 0xbd, 0x43, 0xab, 0x71, 0xd1, 0x7d, 0xb9 |
| 1329 | }; |
| 1330 | static const PRUint8 ike_sha384_known_plain_text[] = { |
| 1331 | 0x17, 0x24, 0xdb, 0xd8, 0x93, 0x52, 0x37, 0x64, |
| 1332 | 0xbf, 0xef, 0x8c, 0x6f, 0xa9, 0x27, 0x85, 0x6f, |
| 1333 | 0xcc, 0xfb, 0x77, 0xae, 0x25, 0x43, 0x58, 0xcc, |
| 1334 | 0xe2, 0x9c, 0x27, 0x69, 0xa3, 0x29, 0x15, 0xc1 |
| 1335 | }; |
| 1336 | static const PRUint8 ike_sha384_known_mac[] = { |
| 1337 | 0x6e, 0x45, 0x14, 0x61, 0x0b, 0xf8, 0x2d, 0x0a, |
| 1338 | 0xb7, 0xbf, 0x02, 0x60, 0x09, 0x6f, 0x61, 0x46, |
| 1339 | 0xa1, 0x53, 0xc7, 0x12, 0x07, 0x1a, 0xbb, 0x63, |
| 1340 | 0x3c, 0xed, 0x81, 0x3c, 0x57, 0x21, 0x56, 0xc7, |
| 1341 | 0x83, 0xe3, 0x68, 0x74, 0xa6, 0x5a, 0x64, 0x69, |
| 1342 | 0x0c, 0xa7, 0x01, 0xd4, 0x0d, 0x56, 0xea, 0x18 |
| 1343 | }; |
| 1344 | static const PRUint8 ike_sha512_known_key[] = { |
| 1345 | 0xac, 0xad, 0xc6, 0x31, 0x4a, 0x69, 0xcf, 0xcd, |
| 1346 | 0x4e, 0x4a, 0xd1, 0x77, 0x18, 0xfe, 0xa7, 0xce |
| 1347 | }; |
| 1348 | static const PRUint8 ike_sha512_known_plain_text[] = { |
| 1349 | 0xb1, 0x5a, 0x9c, 0xfc, 0xe8, 0xc8, 0xd7, 0xea, |
| 1350 | 0xb8, 0x79, 0xd6, 0x24, 0x30, 0x29, 0xd4, 0x01, |
| 1351 | 0x88, 0xd3, 0xb7, 0x40, 0x87, 0x5a, 0x6a, 0xc6, |
| 1352 | 0x2f, 0x56, 0xca, 0xc4, 0x37, 0x7e, 0x2e, 0xdd |
| 1353 | }; |
| 1354 | static const PRUint8 ike_sha512_known_mac[] = { |
| 1355 | 0xf0, 0x5a, 0xa0, 0x36, 0xdf, 0xce, 0x45, 0xa5, |
| 1356 | 0x58, 0xd4, 0x04, 0x18, 0xde, 0xa9, 0x80, 0x96, |
| 1357 | 0xe5, 0x19, 0xbc, 0x78, 0x41, 0xe3, 0xdb, 0x3d, |
| 1358 | 0xd9, 0x36, 0x58, 0xd1, 0x18, 0xc3, 0xe8, 0x3b, |
| 1359 | 0x50, 0x2f, 0x39, 0x8e, 0xcb, 0x13, 0x61, 0xec, |
| 1360 | 0x77, 0xd3, 0x8a, 0x88, 0x55, 0xef, 0xff, 0x40, |
| 1361 | 0x7f, 0x6f, 0x77, 0x2e, 0x5d, 0x65, 0xb5, 0x8e, |
| 1362 | 0xb1, 0x13, 0x40, 0x96, 0xe8, 0x47, 0x8d, 0x2b |
| 1363 | }; |
| 1364 | static const PRUint8 ike_known_sha256_prf_plus[] = { |
| 1365 | 0xe6, 0xf1, 0x9b, 0x4a, 0x02, 0xe9, 0x73, 0x72, |
| 1366 | 0x93, 0x9f, 0xdb, 0x46, 0x1d, 0xb1, 0x49, 0xcb, |
| 1367 | 0x53, 0x08, 0x98, 0x3d, 0x41, 0x36, 0xfa, 0x8b, |
| 1368 | 0x47, 0x04, 0x49, 0x11, 0x0d, 0x6e, 0x96, 0x1d, |
| 1369 | 0xab, 0xbe, 0x94, 0x28, 0xa0, 0xb7, 0x9c, 0xa3, |
| 1370 | 0x29, 0xe1, 0x40, 0xf8, 0xf8, 0x88, 0xb9, 0xb5, |
| 1371 | 0x40, 0xd4, 0x54, 0x4d, 0x25, 0xab, 0x94, 0xd4, |
| 1372 | 0x98, 0xd8, 0x00, 0xbf, 0x6f, 0xef, 0xe8, 0x39 |
| 1373 | }; |
| 1374 | SECStatus rv; |
| 1375 | CK_RV crv; |
| 1376 | unsigned char *outKeyData = NULL; |
| 1377 | unsigned int outKeySize; |
| 1378 | CK_IKE2_PRF_PLUS_DERIVE_PARAMS ike_params; |
| 1379 | |
| 1380 | rv = prf_test(CKM_AES_XCBC_MAC, |
| |
| 1381 | ike_xcbc_known_key, sizeof(ike_xcbc_known_key), |
| 1382 | ike_xcbc_known_plain_text, sizeof(ike_xcbc_known_plain_text), |
| 1383 | ike_xcbc_known_mac, sizeof(ike_xcbc_known_mac)); |
| 1384 | if (rv != SECSuccess) |
| 1385 | return rv; |
| 1386 | rv = prf_test(CKM_AES_XCBC_MAC, |
| 1387 | ike_xcbc_known_key, sizeof(ike_xcbc_known_key), |
| 1388 | ike_xcbc_known_plain_text_2, sizeof(ike_xcbc_known_plain_text_2), |
| 1389 | ike_xcbc_known_mac_2, sizeof(ike_xcbc_known_mac_2)); |
| 1390 | if (rv != SECSuccess) |
| 1391 | return rv; |
| 1392 | rv = prf_test(CKM_AES_XCBC_MAC, |
| 1393 | ike_xcbc_known_key_3, sizeof(ike_xcbc_known_key_3), |
| 1394 | ike_xcbc_known_plain_text_2, sizeof(ike_xcbc_known_plain_text_2), |
| 1395 | ike_xcbc_known_mac_3, sizeof(ike_xcbc_known_mac_3)); |
| 1396 | if (rv != SECSuccess) |
| 1397 | return rv; |
| 1398 | rv = prf_test(CKM_AES_XCBC_MAC, |
| 1399 | ike_xcbc_known_key_4, sizeof(ike_xcbc_known_key_4), |
| 1400 | ike_xcbc_known_plain_text_2, sizeof(ike_xcbc_known_plain_text_2), |
| 1401 | ike_xcbc_known_mac_4, sizeof(ike_xcbc_known_mac_4)); |
| 1402 | if (rv != SECSuccess) |
| 1403 | return rv; |
| 1404 | rv = prf_test(CKM_SHA_1_HMAC, |
| 1405 | ike_sha1_known_key, sizeof(ike_sha1_known_key), |
| 1406 | ike_sha1_known_plain_text, sizeof(ike_sha1_known_plain_text), |
| 1407 | ike_sha1_known_mac, sizeof(ike_sha1_known_mac)); |
| 1408 | if (rv != SECSuccess) |
| 1409 | return rv; |
| 1410 | rv = prf_test(CKM_SHA256_HMAC, |
| 1411 | ike_sha256_known_key, sizeof(ike_sha256_known_key), |
| 1412 | ike_sha256_known_plain_text, |
| 1413 | sizeof(ike_sha256_known_plain_text), |
| 1414 | ike_sha256_known_mac, sizeof(ike_sha256_known_mac)); |
| 1415 | if (rv != SECSuccess) |
| 1416 | return rv; |
| 1417 | rv = prf_test(CKM_SHA384_HMAC, |
| 1418 | ike_sha384_known_key, sizeof(ike_sha384_known_key), |
| 1419 | ike_sha384_known_plain_text, |
| 1420 | sizeof(ike_sha384_known_plain_text), |
| 1421 | ike_sha384_known_mac, sizeof(ike_sha384_known_mac)); |
| 1422 | if (rv != SECSuccess) |
| 1423 | return rv; |
| 1424 | rv = prf_test(CKM_SHA512_HMAC, |
| 1425 | ike_sha512_known_key, sizeof(ike_sha512_known_key), |
| 1426 | ike_sha512_known_plain_text, |
| 1427 | sizeof(ike_sha512_known_plain_text), |
| 1428 | ike_sha512_known_mac, sizeof(ike_sha512_known_mac)); |
| 1429 | |
| 1430 | ike_params.prfMechanism = CKM_SHA256_HMAC; |
| 1431 | ike_params.bHasSeedKey = PR_FALSE; |
| 1432 | ike_params.hSeedKey = CK_INVALID_HANDLE; |
| 1433 | ike_params.pSeedData = (CK_BYTE_PTR)ike_sha256_known_plain_text; |
| 1434 | ike_params.ulSeedDataLen = sizeof(ike_sha256_known_plain_text); |
| 1435 | crv = sftk_ike_prf_plus_raw(CK_INVALID_HANDLE, ike_sha256_known_key, |
| 1436 | sizeof(ike_sha256_known_key), &ike_params, |
| 1437 | &outKeyData, &outKeySize, 64); |
| 1438 | if ((crv != CKR_OK) || |
| 1439 | (outKeySize != sizeof(ike_known_sha256_prf_plus)) || |
| 1440 | (PORT_Memcmp(outKeyData, ike_known_sha256_prf_plus, |
| 1441 | sizeof(ike_known_sha256_prf_plus)) != 0)) { |
| 1442 | PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); |
| 1443 | return SECFailure; |
| 1444 | } |
| 1445 | PORT_ZFree(outKeyData, outKeySize); |
| 1446 | return rv; |
| 1447 | } |