Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 /* | 4 /* |
| 5 * This file implements PKCS 11 on top of our existing security modules | 5 * This file implements PKCS 11 on top of our existing security modules |
| 6 * | 6 * |
| 7 * For more information about PKCS 11 See PKCS 11 Token Inteface Standard. | 7 * For more information about PKCS 11 See PKCS 11 Token Inteface Standard. |
| 8 * This implementation has two slots: | 8 * This implementation has two slots: |
| 9 * slot 1 is our generic crypto support. It does not require login. | 9 * slot 1 is our generic crypto support. It does not require login. |
| 10 * It supports Public Key ops, and all they bulk ciphers and hashes. | 10 * It supports Public Key ops, and all they bulk ciphers and hashes. |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 | 468 |
| 469 static SECStatus | 469 static SECStatus |
| 470 sftk_DecryptOAEP(SFTKOAEPDecryptInfo *info, unsigned char *output, | 470 sftk_DecryptOAEP(SFTKOAEPDecryptInfo *info, unsigned char *output, |
| 471 unsigned int *outputLen, unsigned int maxLen, | 471 unsigned int *outputLen, unsigned int maxLen, |
| 472 unsigned char *input, unsigned int inputLen) | 472 unsigned char *input, unsigned int inputLen) |
| 473 { | 473 { |
| 474 return RSA_DecryptOAEP(info->params, info->key, output, outputLen, | 474 return RSA_DecryptOAEP(info->params, info->key, output, outputLen, |
| 475 maxLen, input, inputLen); | 475 maxLen, input, inputLen); |
| 476 } | 476 } |
| 477 | 477 |
| 478 static SFTKChaCha20Poly1305Info * | |
| 479 sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key, unsigned int keyLe n, | |
|
agl
2013/10/23 14:24:28
(not about about local style, but this spills over
wtc
2013/10/23 20:04:20
Done.
| |
| 480 const CK_NSS_AEAD_PARAMS* params) | |
| 481 { | |
| 482 SFTKChaCha20Poly1305Info *ctx; | |
| 483 | |
| 484 if (params->ulIvLen != sizeof(ctx->nonce)) { | |
| 485 PORT_SetError(SEC_ERROR_INPUT_LEN); | |
| 486 return NULL; | |
| 487 } | |
| 488 | |
| 489 ctx = PORT_New(SFTKChaCha20Poly1305Info); | |
| 490 if (ctx == NULL) { | |
| 491 return NULL; | |
| 492 } | |
| 493 | |
| 494 if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen, | |
| 495 params->ulTagLen) != SECSuccess) { | |
| 496 PORT_Free(ctx); | |
| 497 return NULL; | |
| 498 } | |
| 499 | |
| 500 memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); | |
| 501 | |
| 502 if (params->ulAADLen > sizeof(ctx->ad)) { | |
| 503 /* Need to allocate an overflow buffer for the additional data. */ | |
| 504 ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); | |
| 505 if (!ctx->adOverflow) { | |
| 506 PORT_Free(ctx); | |
| 507 return NULL; | |
| 508 } | |
| 509 memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen); | |
| 510 } else { | |
| 511 ctx->adOverflow = NULL; | |
| 512 memcpy(ctx->ad, params->pAAD, params->ulAADLen); | |
| 513 } | |
| 514 ctx->adLen = params->ulAADLen; | |
| 515 | |
| 516 return ctx; | |
| 517 } | |
| 518 | |
| 519 static void | |
| 520 sftk_ChaCha20Poly1305_DestroyContext(SFTKChaCha20Poly1305Info *ctx, PRBool freei t) | |
| 521 { | |
| 522 ChaCha20Poly1305_DestroyContext(&ctx->freeblCtx, PR_FALSE); | |
| 523 if (ctx->adOverflow != NULL) { | |
| 524 PORT_Free(ctx->adOverflow); | |
| 525 ctx->adOverflow = NULL; | |
| 526 } | |
| 527 ctx->adLen = 0; | |
| 528 if (freeit) { | |
| 529 PORT_Free(ctx); | |
| 530 } | |
| 531 } | |
| 532 | |
| 533 static SECStatus | |
| 534 sftk_ChaCha20Poly1305_Encrypt(const SFTKChaCha20Poly1305Info *ctx, | |
| 535 unsigned char *output, unsigned int *outputLen, | |
| 536 unsigned int maxOutputLen, | |
| 537 const unsigned char *input, unsigned int inputLen) | |
| 538 { | |
| 539 const unsigned char *ad = ctx->adOverflow; | |
| 540 | |
| 541 if (ad == NULL) { | |
| 542 ad = ctx->ad; | |
| 543 } | |
| 544 | |
| 545 return ChaCha20Poly1305_Seal(&ctx->freeblCtx, output, outputLen, maxOutputLe n, | |
| 546 input, inputLen, ctx->nonce, sizeof(ctx->nonce) , | |
| 547 ad, ctx->adLen); | |
| 548 } | |
| 549 | |
| 550 static SECStatus | |
| 551 sftk_ChaCha20Poly1305_Decrypt(const SFTKChaCha20Poly1305Info *ctx, | |
| 552 unsigned char *output, unsigned int *outputLen, | |
| 553 unsigned int maxOutputLen, | |
| 554 const unsigned char *input, unsigned int inputLen) | |
| 555 { | |
| 556 const unsigned char *ad = ctx->adOverflow; | |
| 557 | |
| 558 if (ad == NULL) { | |
| 559 ad = ctx->ad; | |
| 560 } | |
| 561 | |
| 562 return ChaCha20Poly1305_Open(&ctx->freeblCtx, output, outputLen, maxOutputLe n, | |
| 563 input, inputLen, ctx->nonce, sizeof(ctx->nonce) , | |
| 564 ad, ctx->adLen); | |
| 565 } | |
| 566 | |
| 478 /** NSC_CryptInit initializes an encryption/Decryption operation. | 567 /** NSC_CryptInit initializes an encryption/Decryption operation. |
| 479 * | 568 * |
| 480 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. | 569 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. |
| 481 * Called by NSC_SignInit, NSC_VerifyInit (via sftk_InitCBCMac) only for block | 570 * Called by NSC_SignInit, NSC_VerifyInit (via sftk_InitCBCMac) only for block |
| 482 * ciphers MAC'ing. | 571 * ciphers MAC'ing. |
| 483 */ | 572 */ |
| 484 static CK_RV | 573 static CK_RV |
| 485 sftk_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, | 574 sftk_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, |
| 486 CK_OBJECT_HANDLE hKey, | 575 CK_OBJECT_HANDLE hKey, |
| 487 CK_ATTRIBUTE_TYPE mechUsage, CK_ATTRIBUTE_TYPE keyUsage, | 576 CK_ATTRIBUTE_TYPE mechUsage, CK_ATTRIBUTE_TYPE keyUsage, |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 863 isEncrypt, att->attrib.ulValueLen, 16); | 952 isEncrypt, att->attrib.ulValueLen, 16); |
| 864 sftk_FreeAttribute(att); | 953 sftk_FreeAttribute(att); |
| 865 if (context->cipherInfo == NULL) { | 954 if (context->cipherInfo == NULL) { |
| 866 crv = CKR_HOST_MEMORY; | 955 crv = CKR_HOST_MEMORY; |
| 867 break; | 956 break; |
| 868 } | 957 } |
| 869 context->update = (SFTKCipher) (isEncrypt ? AES_Encrypt : AES_Decrypt); | 958 context->update = (SFTKCipher) (isEncrypt ? AES_Encrypt : AES_Decrypt); |
| 870 context->destroy = (SFTKDestroy) AES_DestroyContext; | 959 context->destroy = (SFTKDestroy) AES_DestroyContext; |
| 871 break; | 960 break; |
| 872 | 961 |
| 962 case CKM_NSS_CHACHA20_POLY1305: | |
| 963 if (pMechanism->ulParameterLen != sizeof(CK_NSS_AEAD_PARAMS)) { | |
| 964 crv = CKR_MECHANISM_PARAM_INVALID; | |
| 965 break; | |
| 966 } | |
| 967 context->multi = PR_FALSE; | |
| 968 if (key_type != CKK_NSS_CHACHA20) { | |
| 969 crv = CKR_KEY_TYPE_INCONSISTENT; | |
| 970 break; | |
| 971 } | |
| 972 att = sftk_FindAttribute(key,CKA_VALUE); | |
| 973 if (att == NULL) { | |
| 974 crv = CKR_KEY_HANDLE_INVALID; | |
| 975 break; | |
| 976 } | |
| 977 context->cipherInfo = sftk_ChaCha20Poly1305_CreateContext( | |
| 978 (unsigned char*) att->attrib.pValue, att->attrib.ulValueLen, | |
| 979 (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter); | |
| 980 sftk_FreeAttribute(att); | |
| 981 if (context->cipherInfo == NULL) { | |
| 982 /* XXX map error code */ | |
| 983 crv = CKR_HOST_MEMORY; | |
| 984 break; | |
| 985 } | |
| 986 context->update = (SFTKCipher) (isEncrypt ? sftk_ChaCha20Poly1305_Encryp t : | |
| 987 sftk_ChaCha20Poly1305_Decrypt); | |
| 988 context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_DestroyContext; | |
| 989 break; | |
| 990 | |
| 873 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: | 991 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: |
| 874 context->doPad = PR_TRUE; | 992 context->doPad = PR_TRUE; |
| 875 /* fall thru */ | 993 /* fall thru */ |
| 876 case CKM_NETSCAPE_AES_KEY_WRAP: | 994 case CKM_NETSCAPE_AES_KEY_WRAP: |
| 877 context->multi = PR_FALSE; | 995 context->multi = PR_FALSE; |
| 878 context->blockSize = 8; | 996 context->blockSize = 8; |
| 879 if (key_type != CKK_AES) { | 997 if (key_type != CKK_AES) { |
| 880 crv = CKR_KEY_TYPE_INCONSISTENT; | 998 crv = CKR_KEY_TYPE_INCONSISTENT; |
| 881 break; | 999 break; |
| 882 } | 1000 } |
| (...skipping 2382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3265 *key_length = 16; | 3383 *key_length = 16; |
| 3266 break; | 3384 break; |
| 3267 case CKM_CAMELLIA_KEY_GEN: | 3385 case CKM_CAMELLIA_KEY_GEN: |
| 3268 *key_type = CKK_CAMELLIA; | 3386 *key_type = CKK_CAMELLIA; |
| 3269 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | 3387 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
| 3270 break; | 3388 break; |
| 3271 case CKM_AES_KEY_GEN: | 3389 case CKM_AES_KEY_GEN: |
| 3272 *key_type = CKK_AES; | 3390 *key_type = CKK_AES; |
| 3273 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | 3391 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
| 3274 break; | 3392 break; |
| 3393 case CKM_NSS_CHACHA20_KEY_GEN: | |
| 3394 *key_type = CKK_NSS_CHACHA20; | |
| 3395 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | |
| 3396 break; | |
| 3275 default: | 3397 default: |
| 3276 PORT_Assert(0); | 3398 PORT_Assert(0); |
| 3277 crv = CKR_MECHANISM_INVALID; | 3399 crv = CKR_MECHANISM_INVALID; |
| 3278 break; | 3400 break; |
| 3279 } | 3401 } |
| 3280 | 3402 |
| 3281 return crv; | 3403 return crv; |
| 3282 } | 3404 } |
| 3283 | 3405 |
| 3284 CK_RV | 3406 CK_RV |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3509 case CKM_DES_KEY_GEN: | 3631 case CKM_DES_KEY_GEN: |
| 3510 case CKM_DES2_KEY_GEN: | 3632 case CKM_DES2_KEY_GEN: |
| 3511 case CKM_DES3_KEY_GEN: | 3633 case CKM_DES3_KEY_GEN: |
| 3512 checkWeak = PR_TRUE; | 3634 checkWeak = PR_TRUE; |
| 3513 case CKM_RC2_KEY_GEN: | 3635 case CKM_RC2_KEY_GEN: |
| 3514 case CKM_RC4_KEY_GEN: | 3636 case CKM_RC4_KEY_GEN: |
| 3515 case CKM_GENERIC_SECRET_KEY_GEN: | 3637 case CKM_GENERIC_SECRET_KEY_GEN: |
| 3516 case CKM_SEED_KEY_GEN: | 3638 case CKM_SEED_KEY_GEN: |
| 3517 case CKM_CAMELLIA_KEY_GEN: | 3639 case CKM_CAMELLIA_KEY_GEN: |
| 3518 case CKM_AES_KEY_GEN: | 3640 case CKM_AES_KEY_GEN: |
| 3641 case CKM_NSS_CHACHA20_KEY_GEN: | |
| 3519 #if NSS_SOFTOKEN_DOES_RC5 | 3642 #if NSS_SOFTOKEN_DOES_RC5 |
| 3520 case CKM_RC5_KEY_GEN: | 3643 case CKM_RC5_KEY_GEN: |
| 3521 #endif | 3644 #endif |
| 3522 crv = nsc_SetupBulkKeyGen(pMechanism->mechanism,&key_type,&key_length); | 3645 crv = nsc_SetupBulkKeyGen(pMechanism->mechanism,&key_type,&key_length); |
| 3523 break; | 3646 break; |
| 3524 case CKM_SSL3_PRE_MASTER_KEY_GEN: | 3647 case CKM_SSL3_PRE_MASTER_KEY_GEN: |
| 3525 key_type = CKK_GENERIC_SECRET; | 3648 key_type = CKK_GENERIC_SECRET; |
| 3526 key_length = 48; | 3649 key_length = 48; |
| 3527 key_gen_type = nsc_ssl; | 3650 key_gen_type = nsc_ssl; |
| 3528 break; | 3651 break; |
| (...skipping 3418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6947 att = sftk_FindAttribute(key,CKA_VALUE); | 7070 att = sftk_FindAttribute(key,CKA_VALUE); |
| 6948 sftk_FreeObject(key); | 7071 sftk_FreeObject(key); |
| 6949 if (!att) { | 7072 if (!att) { |
| 6950 return CKR_KEY_HANDLE_INVALID; | 7073 return CKR_KEY_HANDLE_INVALID; |
| 6951 } | 7074 } |
| 6952 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, | 7075 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, |
| 6953 att->attrib.ulValueLen); | 7076 att->attrib.ulValueLen); |
| 6954 sftk_FreeAttribute(att); | 7077 sftk_FreeAttribute(att); |
| 6955 return crv; | 7078 return crv; |
| 6956 } | 7079 } |
| OLD | NEW |