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_New(const unsigned char *key, |
| 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 if (params->ulTagBits == 0 || |
| 490 params->ulTagBits > 128 || |
| 491 (params->ulTagBits & 7) != 0) { |
| 492 PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 493 return NULL; |
| 494 } |
| 495 |
| 496 ctx = PORT_New(SFTKChaCha20Poly1305Info); |
| 497 if (ctx == NULL) { |
| 498 return NULL; |
| 499 } |
| 500 |
| 501 memcpy(ctx->key, key, sizeof(ctx->key)); |
| 502 memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); |
| 503 ctx->tagLen = params->ulTagBits >> 3; |
| 504 |
| 505 if (params->ulAADLen > sizeof(ctx->ad)) { |
| 506 /* Need to allocate an overflow buffer for the additional data. */ |
| 507 ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); |
| 508 if (!ctx->adOverflow) { |
| 509 PORT_Free(ctx); |
| 510 return NULL; |
| 511 } |
| 512 memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen); |
| 513 } else { |
| 514 ctx->adOverflow = NULL; |
| 515 memcpy(ctx->ad, params->pAAD, params->ulAADLen); |
| 516 } |
| 517 ctx->adLen = params->ulAADLen; |
| 518 |
| 519 return ctx; |
| 520 } |
| 521 |
| 522 static void |
| 523 sftk_ChaCha20Poly1305_Free(SFTKChaCha20Poly1305Info *ctx) |
| 524 { |
| 525 if (ctx->adOverflow != NULL) { |
| 526 PORT_Free(ctx->adOverflow); |
| 527 } |
| 528 PORT_Free(ctx); |
| 529 } |
| 530 |
| 531 static SECStatus |
| 532 sftk_ChaCha20Poly1305_Seal(const SFTKChaCha20Poly1305Info *ctx, |
| 533 unsigned char *output, unsigned int *outputLen, |
| 534 unsigned int maxOutputLen, |
| 535 const unsigned char *input, unsigned int inputLen) |
| 536 { |
| 537 const unsigned char *ad = ctx->adOverflow; |
| 538 |
| 539 *outputLen = inputLen + ctx->tagLen; |
| 540 if (maxOutputLen < *outputLen) { |
| 541 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 542 return SECFailure; |
| 543 } |
| 544 |
| 545 if (ad == NULL) { |
| 546 ad = ctx->ad; |
| 547 } |
| 548 |
| 549 return ChaCha20Poly1305_Seal(output, ad, ctx->adLen, input, inputLen, |
| 550 ctx->tagLen, ctx->key, ctx->nonce); |
| 551 } |
| 552 |
| 553 static SECStatus |
| 554 sftk_ChaCha20Poly1305_Open(const SFTKChaCha20Poly1305Info *ctx, |
| 555 unsigned char *output, unsigned int *outputLen, |
| 556 unsigned int maxOutputLen, |
| 557 const unsigned char *input, unsigned int inputLen) |
| 558 { |
| 559 const unsigned char *ad = ctx->adOverflow; |
| 560 |
| 561 if (inputLen < ctx->tagLen) { |
| 562 PORT_SetError(SEC_ERROR_INPUT_LEN); |
| 563 return SECFailure; |
| 564 } |
| 565 *outputLen = inputLen - ctx->tagLen; |
| 566 if (maxOutputLen < *outputLen) { |
| 567 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
| 568 return SECFailure; |
| 569 } |
| 570 |
| 571 if (ad == NULL) { |
| 572 ad = ctx->ad; |
| 573 } |
| 574 |
| 575 return ChaCha20Poly1305_Open(output, ad, ctx->adLen, input, inputLen, |
| 576 ctx->tagLen, ctx->key, ctx->nonce); |
| 577 } |
| 578 |
478 /** NSC_CryptInit initializes an encryption/Decryption operation. | 579 /** NSC_CryptInit initializes an encryption/Decryption operation. |
479 * | 580 * |
480 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. | 581 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. |
481 * Called by NSC_SignInit, NSC_VerifyInit (via sftk_InitCBCMac) only for block | 582 * Called by NSC_SignInit, NSC_VerifyInit (via sftk_InitCBCMac) only for block |
482 * ciphers MAC'ing. | 583 * ciphers MAC'ing. |
483 */ | 584 */ |
484 static CK_RV | 585 static CK_RV |
485 sftk_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, | 586 sftk_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, |
486 CK_OBJECT_HANDLE hKey, | 587 CK_OBJECT_HANDLE hKey, |
487 CK_ATTRIBUTE_TYPE mechUsage, CK_ATTRIBUTE_TYPE keyUsage, | 588 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); | 964 isEncrypt, att->attrib.ulValueLen, 16); |
864 sftk_FreeAttribute(att); | 965 sftk_FreeAttribute(att); |
865 if (context->cipherInfo == NULL) { | 966 if (context->cipherInfo == NULL) { |
866 crv = CKR_HOST_MEMORY; | 967 crv = CKR_HOST_MEMORY; |
867 break; | 968 break; |
868 } | 969 } |
869 context->update = (SFTKCipher) (isEncrypt ? AES_Encrypt : AES_Decrypt); | 970 context->update = (SFTKCipher) (isEncrypt ? AES_Encrypt : AES_Decrypt); |
870 context->destroy = (SFTKDestroy) AES_DestroyContext; | 971 context->destroy = (SFTKDestroy) AES_DestroyContext; |
871 break; | 972 break; |
872 | 973 |
| 974 case CKM_NSS_CHACHA20_POLY1305: |
| 975 context->multi = PR_FALSE; |
| 976 if (key_type != CKK_NSS_CHACHA20) { |
| 977 crv = CKR_KEY_TYPE_INCONSISTENT; |
| 978 break; |
| 979 } |
| 980 att = sftk_FindAttribute(key,CKA_VALUE); |
| 981 if (att == NULL) { |
| 982 crv = CKR_KEY_HANDLE_INVALID; |
| 983 break; |
| 984 } |
| 985 context->cipherInfo = sftk_ChaCha20Poly1305_New( |
| 986 (unsigned char*) att->attrib.pValue, |
| 987 (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter); |
| 988 sftk_FreeAttribute(att); |
| 989 if (context->cipherInfo == NULL) { |
| 990 crv = CKR_HOST_MEMORY; |
| 991 break; |
| 992 } |
| 993 context->update = (SFTKCipher) (isEncrypt ? sftk_ChaCha20Poly1305_Seal : |
| 994 sftk_ChaCha20Poly1305_Open); |
| 995 context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_Free; |
| 996 break; |
| 997 |
873 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: | 998 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: |
874 context->doPad = PR_TRUE; | 999 context->doPad = PR_TRUE; |
875 /* fall thru */ | 1000 /* fall thru */ |
876 case CKM_NETSCAPE_AES_KEY_WRAP: | 1001 case CKM_NETSCAPE_AES_KEY_WRAP: |
877 context->multi = PR_FALSE; | 1002 context->multi = PR_FALSE; |
878 context->blockSize = 8; | 1003 context->blockSize = 8; |
879 if (key_type != CKK_AES) { | 1004 if (key_type != CKK_AES) { |
880 crv = CKR_KEY_TYPE_INCONSISTENT; | 1005 crv = CKR_KEY_TYPE_INCONSISTENT; |
881 break; | 1006 break; |
882 } | 1007 } |
(...skipping 2382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3265 *key_length = 16; | 3390 *key_length = 16; |
3266 break; | 3391 break; |
3267 case CKM_CAMELLIA_KEY_GEN: | 3392 case CKM_CAMELLIA_KEY_GEN: |
3268 *key_type = CKK_CAMELLIA; | 3393 *key_type = CKK_CAMELLIA; |
3269 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | 3394 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
3270 break; | 3395 break; |
3271 case CKM_AES_KEY_GEN: | 3396 case CKM_AES_KEY_GEN: |
3272 *key_type = CKK_AES; | 3397 *key_type = CKK_AES; |
3273 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; | 3398 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
3274 break; | 3399 break; |
| 3400 case CKM_NSS_CHACHA20_KEY_GEN: |
| 3401 *key_type = CKK_NSS_CHACHA20; |
| 3402 if (*key_length == 0) crv = CKR_TEMPLATE_INCOMPLETE; |
| 3403 break; |
3275 default: | 3404 default: |
3276 PORT_Assert(0); | 3405 PORT_Assert(0); |
3277 crv = CKR_MECHANISM_INVALID; | 3406 crv = CKR_MECHANISM_INVALID; |
3278 break; | 3407 break; |
3279 } | 3408 } |
3280 | 3409 |
3281 return crv; | 3410 return crv; |
3282 } | 3411 } |
3283 | 3412 |
3284 CK_RV | 3413 CK_RV |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3509 case CKM_DES_KEY_GEN: | 3638 case CKM_DES_KEY_GEN: |
3510 case CKM_DES2_KEY_GEN: | 3639 case CKM_DES2_KEY_GEN: |
3511 case CKM_DES3_KEY_GEN: | 3640 case CKM_DES3_KEY_GEN: |
3512 checkWeak = PR_TRUE; | 3641 checkWeak = PR_TRUE; |
3513 case CKM_RC2_KEY_GEN: | 3642 case CKM_RC2_KEY_GEN: |
3514 case CKM_RC4_KEY_GEN: | 3643 case CKM_RC4_KEY_GEN: |
3515 case CKM_GENERIC_SECRET_KEY_GEN: | 3644 case CKM_GENERIC_SECRET_KEY_GEN: |
3516 case CKM_SEED_KEY_GEN: | 3645 case CKM_SEED_KEY_GEN: |
3517 case CKM_CAMELLIA_KEY_GEN: | 3646 case CKM_CAMELLIA_KEY_GEN: |
3518 case CKM_AES_KEY_GEN: | 3647 case CKM_AES_KEY_GEN: |
| 3648 case CKM_NSS_CHACHA20_KEY_GEN: |
3519 #if NSS_SOFTOKEN_DOES_RC5 | 3649 #if NSS_SOFTOKEN_DOES_RC5 |
3520 case CKM_RC5_KEY_GEN: | 3650 case CKM_RC5_KEY_GEN: |
3521 #endif | 3651 #endif |
3522 crv = nsc_SetupBulkKeyGen(pMechanism->mechanism,&key_type,&key_length); | 3652 crv = nsc_SetupBulkKeyGen(pMechanism->mechanism,&key_type,&key_length); |
3523 break; | 3653 break; |
3524 case CKM_SSL3_PRE_MASTER_KEY_GEN: | 3654 case CKM_SSL3_PRE_MASTER_KEY_GEN: |
3525 key_type = CKK_GENERIC_SECRET; | 3655 key_type = CKK_GENERIC_SECRET; |
3526 key_length = 48; | 3656 key_length = 48; |
3527 key_gen_type = nsc_ssl; | 3657 key_gen_type = nsc_ssl; |
3528 break; | 3658 break; |
(...skipping 3418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6947 att = sftk_FindAttribute(key,CKA_VALUE); | 7077 att = sftk_FindAttribute(key,CKA_VALUE); |
6948 sftk_FreeObject(key); | 7078 sftk_FreeObject(key); |
6949 if (!att) { | 7079 if (!att) { |
6950 return CKR_KEY_HANDLE_INVALID; | 7080 return CKR_KEY_HANDLE_INVALID; |
6951 } | 7081 } |
6952 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, | 7082 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, |
6953 att->attrib.ulValueLen); | 7083 att->attrib.ulValueLen); |
6954 sftk_FreeAttribute(att); | 7084 sftk_FreeAttribute(att); |
6955 return crv; | 7085 return crv; |
6956 } | 7086 } |
OLD | NEW |