Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: mozilla/security/nss/lib/softoken/pkcs11c.c

Issue 11359091: Update NSS to NSS 3.14 pre-release snapshot 2012-06-26 01:00:00 PDT. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/nss/
Patch Set: Remove the RCS Id from nss-shvfy-const.patch Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 2039 matching lines...) Expand 10 before | Expand all | Expand 10 after
2050 break; 2050 break;
2051 } 2051 }
2052 privKey = sftk_GetPrivKey(key,CKK_DSA,&crv); 2052 privKey = sftk_GetPrivKey(key,CKK_DSA,&crv);
2053 if (privKey == NULL) { 2053 if (privKey == NULL) {
2054 break; 2054 break;
2055 } 2055 }
2056 context->cipherInfo = privKey; 2056 context->cipherInfo = privKey;
2057 context->update = (SFTKCipher) nsc_DSA_Sign_Stub; 2057 context->update = (SFTKCipher) nsc_DSA_Sign_Stub;
2058 context->destroy = (privKey == key->objectInfo) ? 2058 context->destroy = (privKey == key->objectInfo) ?
2059 (SFTKDestroy) sftk_Null:(SFTKDestroy)sftk_FreePrivKey; 2059 (SFTKDestroy) sftk_Null:(SFTKDestroy)sftk_FreePrivKey;
2060 » context->maxLen = DSA_SIGNATURE_LEN; 2060 » context->maxLen = DSA_MAX_SIGNATURE_LEN;
2061 2061
2062 break; 2062 break;
2063 2063
2064 #ifdef NSS_ENABLE_ECC 2064 #ifdef NSS_ENABLE_ECC
2065 case CKM_ECDSA_SHA1: 2065 case CKM_ECDSA_SHA1:
2066 context->multi = PR_TRUE; 2066 context->multi = PR_TRUE;
2067 crv = sftk_doSubSHA1(context); 2067 crv = sftk_doSubSHA1(context);
2068 if (crv != CKR_OK) break; 2068 if (crv != CKR_OK) break;
2069 /* fall through */ 2069 /* fall through */
2070 case CKM_ECDSA: 2070 case CKM_ECDSA:
(...skipping 827 matching lines...) Expand 10 before | Expand all | Expand 10 after
2898 2898
2899 if (iv.data) { 2899 if (iv.data) {
2900 if (pbe_params && pbe_params->pInitVector != NULL) { 2900 if (pbe_params && pbe_params->pInitVector != NULL) {
2901 PORT_Memcpy(pbe_params->pInitVector, iv.data, iv.len); 2901 PORT_Memcpy(pbe_params->pInitVector, iv.data, iv.len);
2902 } 2902 }
2903 PORT_Free(iv.data); 2903 PORT_Free(iv.data);
2904 } 2904 }
2905 2905
2906 return CKR_OK; 2906 return CKR_OK;
2907 } 2907 }
2908
2909 /*
2910 * this is coded for "full" support. These selections will be limitted to
2911 * the official subset by freebl.
2912 */
2913 static unsigned int
2914 sftk_GetSubPrimeFromPrime(unsigned int primeBits)
2915 {
2916 if (primeBits <= 1024) {
2917 return 160;
2918 } else if (primeBits <= 2048) {
2919 return 224;
2920 } else if (primeBits <= 3072) {
2921 return 256;
2922 } else if (primeBits <= 7680) {
2923 return 384;
2924 } else {
2925 return 512;
2926 }
2927 }
2928
2908 static CK_RV 2929 static CK_RV
2909 nsc_parameter_gen(CK_KEY_TYPE key_type, SFTKObject *key) 2930 nsc_parameter_gen(CK_KEY_TYPE key_type, SFTKObject *key)
2910 { 2931 {
2911 SFTKAttribute *attribute; 2932 SFTKAttribute *attribute;
2912 CK_ULONG counter; 2933 CK_ULONG counter;
2913 unsigned int seedBits = 0; 2934 unsigned int seedBits = 0;
2935 unsigned int subprimeBits = 0;
2914 unsigned int primeBits; 2936 unsigned int primeBits;
2915 unsigned int j; 2937 unsigned int j = 8; /* default to 1024 bits */
2916 CK_RV crv = CKR_OK; 2938 CK_RV crv = CKR_OK;
2917 PQGParams *params = NULL; 2939 PQGParams *params = NULL;
2918 PQGVerify *vfy = NULL; 2940 PQGVerify *vfy = NULL;
2919 SECStatus rv; 2941 SECStatus rv;
2920 2942
2921 attribute = sftk_FindAttribute(key, CKA_PRIME_BITS); 2943 attribute = sftk_FindAttribute(key, CKA_PRIME_BITS);
2922 if (attribute == NULL) { 2944 if (attribute == NULL) {
2923 return CKR_TEMPLATE_INCOMPLETE; 2945 return CKR_TEMPLATE_INCOMPLETE;
2924 } 2946 }
2925 primeBits = (unsigned int) *(CK_ULONG *)attribute->attrib.pValue; 2947 primeBits = (unsigned int) *(CK_ULONG *)attribute->attrib.pValue;
2926 sftk_FreeAttribute(attribute); 2948 sftk_FreeAttribute(attribute);
2927 j = PQG_PBITS_TO_INDEX(primeBits); 2949 if (primeBits < 1024) {
2928 if (j == (unsigned int)-1) { 2950 » j = PQG_PBITS_TO_INDEX(primeBits);
2929 » return CKR_ATTRIBUTE_VALUE_INVALID; 2951 » if (j == (unsigned int)-1) {
2952 » return CKR_ATTRIBUTE_VALUE_INVALID;
2953 » }
2930 } 2954 }
2931 2955
2932 attribute = sftk_FindAttribute(key, CKA_NETSCAPE_PQG_SEED_BITS); 2956 attribute = sftk_FindAttribute(key, CKA_NETSCAPE_PQG_SEED_BITS);
2933 if (attribute != NULL) { 2957 if (attribute != NULL) {
2934 seedBits = (unsigned int) *(CK_ULONG *)attribute->attrib.pValue; 2958 seedBits = (unsigned int) *(CK_ULONG *)attribute->attrib.pValue;
2935 sftk_FreeAttribute(attribute); 2959 sftk_FreeAttribute(attribute);
2936 } 2960 }
2937 2961
2962 attribute = sftk_FindAttribute(key, CKA_SUBPRIME_BITS);
2963 if (attribute != NULL) {
2964 subprimeBits = (unsigned int) *(CK_ULONG *)attribute->attrib.pValue;
2965 sftk_FreeAttribute(attribute);
2966 }
2967
2938 sftk_DeleteAttributeType(key,CKA_PRIME_BITS); 2968 sftk_DeleteAttributeType(key,CKA_PRIME_BITS);
2969 sftk_DeleteAttributeType(key,CKA_SUBPRIME_BITS);
2939 sftk_DeleteAttributeType(key,CKA_NETSCAPE_PQG_SEED_BITS); 2970 sftk_DeleteAttributeType(key,CKA_NETSCAPE_PQG_SEED_BITS);
2940 2971
2941 if (seedBits == 0) { 2972 /* use the old PQG interface if we have old input data */
2942 » rv = PQG_ParamGen(j, &params, &vfy); 2973 if ((primeBits < 1024) || ((primeBits == 1024) && (subprimeBits == 0))) {
2974 » if (seedBits == 0) {
2975 » rv = PQG_ParamGen(j, &params, &vfy);
2976 » } else {
2977 » rv = PQG_ParamGenSeedLen(j,seedBits/8, &params, &vfy);
2978 » }
2943 } else { 2979 } else {
2944 » rv = PQG_ParamGenSeedLen(j,seedBits/8, &params, &vfy); 2980 » if (subprimeBits == 0) {
2981 » subprimeBits = sftk_GetSubPrimeFromPrime(primeBits);
2982 }
2983 » if (seedBits == 0) {
2984 » seedBits = primeBits;
2985 » }
2986 » rv = PQG_ParamGenV2(primeBits, subprimeBits, seedBits/8, &params, &vfy);
2945 } 2987 }
2988
2989
2946 2990
2947 if (rv != SECSuccess) { 2991 if (rv != SECSuccess) {
2948 if (PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) { 2992 if (PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) {
2949 sftk_fatalError = PR_TRUE; 2993 sftk_fatalError = PR_TRUE;
2950 } 2994 }
2951 return sftk_MapCryptError(PORT_GetError()); 2995 return sftk_MapCryptError(PORT_GetError());
2952 } 2996 }
2953 crv = sftk_AddAttributeType(key,CKA_PRIME, 2997 crv = sftk_AddAttributeType(key,CKA_PRIME,
2954 params->prime.data, params->prime.len); 2998 params->prime.data, params->prime.len);
2955 if (crv != CKR_OK) goto loser; 2999 if (crv != CKR_OK) goto loser;
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
3452 * For sign/verify: CKK_RSA => CKM_RSA_PKCS 3496 * For sign/verify: CKK_RSA => CKM_RSA_PKCS
3453 * CKK_DSA => CKM_DSA 3497 * CKK_DSA => CKM_DSA
3454 * CKK_EC => CKM_ECDSA 3498 * CKK_EC => CKM_ECDSA
3455 * others => CKM_INVALID_MECHANISM 3499 * others => CKM_INVALID_MECHANISM
3456 * 3500 *
3457 * None of these mechanisms has a parameter. 3501 * None of these mechanisms has a parameter.
3458 */ 3502 */
3459 CK_MECHANISM mech = {0, NULL, 0}; 3503 CK_MECHANISM mech = {0, NULL, 0};
3460 3504
3461 CK_ULONG modulusLen; 3505 CK_ULONG modulusLen;
3506 CK_ULONG subPrimeLen;
3462 PRBool isEncryptable = PR_FALSE; 3507 PRBool isEncryptable = PR_FALSE;
3463 PRBool canSignVerify = PR_FALSE; 3508 PRBool canSignVerify = PR_FALSE;
3464 PRBool isDerivable = PR_FALSE; 3509 PRBool isDerivable = PR_FALSE;
3465 CK_RV crv; 3510 CK_RV crv;
3466 3511
3467 /* Variables used for Encrypt/Decrypt functions. */ 3512 /* Variables used for Encrypt/Decrypt functions. */
3468 unsigned char *known_message = (unsigned char *)"Known Crypto Message"; 3513 unsigned char *known_message = (unsigned char *)"Known Crypto Message";
3469 unsigned char plaintext[PAIRWISE_MESSAGE_LENGTH]; 3514 unsigned char plaintext[PAIRWISE_MESSAGE_LENGTH];
3470 CK_ULONG bytes_decrypted; 3515 CK_ULONG bytes_decrypted;
3471 unsigned char *ciphertext; 3516 unsigned char *ciphertext;
3472 unsigned char *text_compared; 3517 unsigned char *text_compared;
3473 CK_ULONG bytes_encrypted; 3518 CK_ULONG bytes_encrypted;
3474 CK_ULONG bytes_compared; 3519 CK_ULONG bytes_compared;
3520 CK_ULONG pairwise_digest_length = PAIRWISE_DIGEST_LENGTH;
3475 3521
3476 /* Variables used for Signature/Verification functions. */ 3522 /* Variables used for Signature/Verification functions. */
3477 /* always uses SHA-1 digest */ 3523 /* Must be at least 256 bits for DSA2 digest */
3478 unsigned char *known_digest = (unsigned char *)"Mozilla Rules World!"; 3524 unsigned char *known_digest = (unsigned char *)
3525 » » » » "Mozilla Rules the World through NSS!";
3479 unsigned char *signature; 3526 unsigned char *signature;
3480 CK_ULONG signature_length; 3527 CK_ULONG signature_length;
3481 3528
3482 if (keyType == CKK_RSA) { 3529 if (keyType == CKK_RSA) {
3483 SFTKAttribute *attribute; 3530 SFTKAttribute *attribute;
3484 3531
3485 /* Get modulus length of private key. */ 3532 /* Get modulus length of private key. */
3486 attribute = sftk_FindAttribute(privateKey, CKA_MODULUS); 3533 attribute = sftk_FindAttribute(privateKey, CKA_MODULUS);
3487 if (attribute == NULL) { 3534 if (attribute == NULL) {
3488 return CKR_DEVICE_ERROR; 3535 return CKR_DEVICE_ERROR;
3489 } 3536 }
3490 modulusLen = attribute->attrib.ulValueLen; 3537 modulusLen = attribute->attrib.ulValueLen;
3491 if (*(unsigned char *)attribute->attrib.pValue == 0) { 3538 if (*(unsigned char *)attribute->attrib.pValue == 0) {
3492 modulusLen--; 3539 modulusLen--;
3493 } 3540 }
3494 sftk_FreeAttribute(attribute); 3541 sftk_FreeAttribute(attribute);
3542 } else if (keyType == CKK_DSA) {
3543 SFTKAttribute *attribute;
3544
3545 /* Get subprime length of private key. */
3546 attribute = sftk_FindAttribute(privateKey, CKA_SUBPRIME);
3547 if (attribute == NULL) {
3548 return CKR_DEVICE_ERROR;
3549 }
3550 subPrimeLen = attribute->attrib.ulValueLen;
3551 if (subPrimeLen > 1 && *(unsigned char *)attribute->attrib.pValue == 0) {
3552 subPrimeLen--;
3553 }
3554 sftk_FreeAttribute(attribute);
3495 } 3555 }
3496 3556
3497 /**************************************************/ 3557 /**************************************************/
3498 /* Pairwise Consistency Check of Encrypt/Decrypt. */ 3558 /* Pairwise Consistency Check of Encrypt/Decrypt. */
3499 /**************************************************/ 3559 /**************************************************/
3500 3560
3501 isEncryptable = sftk_isTrue(privateKey, CKA_DECRYPT); 3561 isEncryptable = sftk_isTrue(privateKey, CKA_DECRYPT);
3502 3562
3503 /* 3563 /*
3504 * If the decryption attribute is set, attempt to encrypt 3564 * If the decryption attribute is set, attempt to encrypt
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
3610 canSignVerify = sftk_isTrue(privateKey, CKA_SIGN); 3670 canSignVerify = sftk_isTrue(privateKey, CKA_SIGN);
3611 3671
3612 if (canSignVerify) { 3672 if (canSignVerify) {
3613 /* Determine length of signature. */ 3673 /* Determine length of signature. */
3614 switch (keyType) { 3674 switch (keyType) {
3615 case CKK_RSA: 3675 case CKK_RSA:
3616 signature_length = modulusLen; 3676 signature_length = modulusLen;
3617 mech.mechanism = CKM_RSA_PKCS; 3677 mech.mechanism = CKM_RSA_PKCS;
3618 break; 3678 break;
3619 case CKK_DSA: 3679 case CKK_DSA:
3620 » signature_length = DSA_SIGNATURE_LEN; 3680 » signature_length = DSA_MAX_SIGNATURE_LEN;
3681 » pairwise_digest_length = subPrimeLen;
3621 mech.mechanism = CKM_DSA; 3682 mech.mechanism = CKM_DSA;
3622 break; 3683 break;
3623 #ifdef NSS_ENABLE_ECC 3684 #ifdef NSS_ENABLE_ECC
3624 case CKK_EC: 3685 case CKK_EC:
3625 signature_length = MAX_ECKEY_LEN * 2; 3686 signature_length = MAX_ECKEY_LEN * 2;
3626 mech.mechanism = CKM_ECDSA; 3687 mech.mechanism = CKM_ECDSA;
3627 break; 3688 break;
3628 #endif 3689 #endif
3629 default: 3690 default:
3630 return CKR_DEVICE_ERROR; 3691 return CKR_DEVICE_ERROR;
3631 } 3692 }
3632 3693
3633 /* Allocate space for signature data. */ 3694 /* Allocate space for signature data. */
3634 signature = (unsigned char *) PORT_ZAlloc(signature_length); 3695 signature = (unsigned char *) PORT_ZAlloc(signature_length);
3635 if (signature == NULL) { 3696 if (signature == NULL) {
3636 return CKR_HOST_MEMORY; 3697 return CKR_HOST_MEMORY;
3637 } 3698 }
3638 3699
3639 /* Sign the known hash using the private key. */ 3700 /* Sign the known hash using the private key. */
3640 crv = NSC_SignInit(hSession, &mech, privateKey->handle); 3701 crv = NSC_SignInit(hSession, &mech, privateKey->handle);
3641 if (crv != CKR_OK) { 3702 if (crv != CKR_OK) {
3642 PORT_Free(signature); 3703 PORT_Free(signature);
3643 return crv; 3704 return crv;
3644 } 3705 }
3645 3706
3646 crv = NSC_Sign(hSession, 3707 crv = NSC_Sign(hSession,
3647 known_digest, 3708 known_digest,
3648 » » PAIRWISE_DIGEST_LENGTH, 3709 » » pairwise_digest_length,
3649 signature, 3710 signature,
3650 &signature_length); 3711 &signature_length);
3651 if (crv != CKR_OK) { 3712 if (crv != CKR_OK) {
3652 PORT_Free(signature); 3713 PORT_Free(signature);
3653 return crv; 3714 return crv;
3654 } 3715 }
3655 3716
3656 /* Verify the known hash using the public key. */ 3717 /* Verify the known hash using the public key. */
3657 crv = NSC_VerifyInit(hSession, &mech, publicKey->handle); 3718 crv = NSC_VerifyInit(hSession, &mech, publicKey->handle);
3658 if (crv != CKR_OK) { 3719 if (crv != CKR_OK) {
3659 PORT_Free(signature); 3720 PORT_Free(signature);
3660 return crv; 3721 return crv;
3661 } 3722 }
3662 3723
3663 crv = NSC_Verify(hSession, 3724 crv = NSC_Verify(hSession,
3664 known_digest, 3725 known_digest,
3665 » » » PAIRWISE_DIGEST_LENGTH, 3726 » » » pairwise_digest_length,
3666 signature, 3727 signature,
3667 signature_length); 3728 signature_length);
3668 3729
3669 /* Free signature data. */ 3730 /* Free signature data. */
3670 PORT_Free(signature); 3731 PORT_Free(signature);
3671 3732
3672 if ((crv == CKR_SIGNATURE_LEN_RANGE) || 3733 if ((crv == CKR_SIGNATURE_LEN_RANGE) ||
3673 (crv == CKR_SIGNATURE_INVALID)) { 3734 (crv == CKR_SIGNATURE_INVALID)) {
3674 return CKR_GENERAL_ERROR; 3735 return CKR_GENERAL_ERROR;
3675 } 3736 }
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
3938 } 3999 }
3939 crv = sftk_AddAttributeType(privateKey,CKA_BASE, 4000 crv = sftk_AddAttributeType(privateKey,CKA_BASE,
3940 sftk_item_expand(&pqgParam.base)); 4001 sftk_item_expand(&pqgParam.base));
3941 if (crv != CKR_OK) { 4002 if (crv != CKR_OK) {
3942 PORT_Free(pqgParam.prime.data); 4003 PORT_Free(pqgParam.prime.data);
3943 PORT_Free(pqgParam.subPrime.data); 4004 PORT_Free(pqgParam.subPrime.data);
3944 PORT_Free(pqgParam.base.data); 4005 PORT_Free(pqgParam.base.data);
3945 break; 4006 break;
3946 } 4007 }
3947 4008
4009 /*
4010 * these are checked by DSA_NewKey
4011 */
3948 bitSize = sftk_GetLengthInBits(pqgParam.subPrime.data, 4012 bitSize = sftk_GetLengthInBits(pqgParam.subPrime.data,
3949 pqgParam.subPrime.len); 4013 pqgParam.subPrime.len);
3950 if (bitSize != DSA_Q_BITS) { 4014 if ((bitSize < DSA_MIN_Q_BITS) || (bitSize > DSA_MAX_Q_BITS)) {
3951 crv = CKR_TEMPLATE_INCOMPLETE; 4015 crv = CKR_TEMPLATE_INCOMPLETE;
3952 PORT_Free(pqgParam.prime.data); 4016 PORT_Free(pqgParam.prime.data);
3953 PORT_Free(pqgParam.subPrime.data); 4017 PORT_Free(pqgParam.subPrime.data);
3954 PORT_Free(pqgParam.base.data); 4018 PORT_Free(pqgParam.base.data);
3955 break; 4019 break;
3956 } 4020 }
3957 bitSize = sftk_GetLengthInBits(pqgParam.prime.data,pqgParam.prime.len); 4021 bitSize = sftk_GetLengthInBits(pqgParam.prime.data,pqgParam.prime.len);
3958 if ((bitSize < DSA_MIN_P_BITS) || (bitSize > DSA_MAX_P_BITS)) { 4022 if ((bitSize < DSA_MIN_P_BITS) || (bitSize > DSA_MAX_P_BITS)) {
3959 crv = CKR_TEMPLATE_INCOMPLETE; 4023 crv = CKR_TEMPLATE_INCOMPLETE;
3960 PORT_Free(pqgParam.prime.data); 4024 PORT_Free(pqgParam.prime.data);
3961 PORT_Free(pqgParam.subPrime.data); 4025 PORT_Free(pqgParam.subPrime.data);
3962 PORT_Free(pqgParam.base.data); 4026 PORT_Free(pqgParam.base.data);
3963 break; 4027 break;
3964 } 4028 }
3965 bitSize = sftk_GetLengthInBits(pqgParam.base.data,pqgParam.base.len); 4029 bitSize = sftk_GetLengthInBits(pqgParam.base.data,pqgParam.base.len);
3966 if ((bitSize < 1) || (bitSize > DSA_MAX_P_BITS)) { 4030 if ((bitSize < 2) || (bitSize > DSA_MAX_P_BITS)) {
3967 crv = CKR_TEMPLATE_INCOMPLETE; 4031 crv = CKR_TEMPLATE_INCOMPLETE;
3968 PORT_Free(pqgParam.prime.data); 4032 PORT_Free(pqgParam.prime.data);
3969 PORT_Free(pqgParam.subPrime.data); 4033 PORT_Free(pqgParam.subPrime.data);
3970 PORT_Free(pqgParam.base.data); 4034 PORT_Free(pqgParam.base.data);
3971 break; 4035 break;
3972 } 4036 }
3973 4037
3974 /* Generate the key */ 4038 /* Generate the key */
3975 rv = DSA_NewKey(&pqgParam, &dsaPriv); 4039 rv = DSA_NewKey(&pqgParam, &dsaPriv);
3976 4040
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after
5058 return 16; 5122 return 16;
5059 case CKK_DES3: 5123 case CKK_DES3:
5060 return 24; 5124 return 24;
5061 /* IDEA and CAST need to be added */ 5125 /* IDEA and CAST need to be added */
5062 default: 5126 default:
5063 break; 5127 break;
5064 } 5128 }
5065 return 0; 5129 return 0;
5066 } 5130 }
5067 5131
5132 /* Inputs:
5133 * key_len: Length of derived key to be generated.
5134 * SharedSecret: a shared secret that is the output of a key agreement primitiv e.
5135 * SharedInfo: (Optional) some data shared by the entities computing the secret key.
5136 * SharedInfoLen: the length in octets of SharedInfo
5137 * Hash: The hash function to be used in the KDF
5138 * HashLen: the length in octets of the output of Hash
5139 * Output:
5140 * key: Pointer to a buffer containing derived key, if return value is SECSucce ss.
5141 */
5142 static CK_RV sftk_compute_ANSI_X9_63_kdf(CK_BYTE **key, CK_ULONG key_len, SECIte m *SharedSecret,
5143 CK_BYTE_PTR SharedInfo, CK_ULONG SharedInfoLen,
5144 SECStatus Hash(unsigned char *, const unsigned char *, uint32),
5145 CK_ULONG HashLen)
5146 {
5147 unsigned char *buffer = NULL, *output_buffer = NULL;
5148 uint32 buffer_len, max_counter, i;
5149 SECStatus rv;
5150
5151 /* Check that key_len isn't too long. The maximum key length could be
5152 * greatly increased if the code below did not limit the 4-byte counter
5153 * to a maximum value of 255. */
5154 if (key_len > 254 * HashLen)
5155 return SEC_ERROR_INVALID_ARGS;
5156
5157 if (SharedInfo == NULL)
5158 SharedInfoLen = 0;
5159
5160 buffer_len = SharedSecret->len + 4 + SharedInfoLen;
5161 buffer = (CK_BYTE *)PORT_Alloc(buffer_len);
5162 if (buffer == NULL) {
5163 rv = SEC_ERROR_NO_MEMORY;
5164 goto loser;
5165 }
5166
5167 max_counter = key_len/HashLen;
5168 if (key_len > max_counter * HashLen)
5169 max_counter++;
5170
5171 output_buffer = (CK_BYTE *)PORT_Alloc(max_counter * HashLen);
5172 if (output_buffer == NULL) {
5173 rv = SEC_ERROR_NO_MEMORY;
5174 goto loser;
5175 }
5176
5177 /* Populate buffer with SharedSecret || Counter || [SharedInfo]
5178 * where Counter is 0x00000001 */
5179 PORT_Memcpy(buffer, SharedSecret->data, SharedSecret->len);
5180 buffer[SharedSecret->len] = 0;
5181 buffer[SharedSecret->len + 1] = 0;
5182 buffer[SharedSecret->len + 2] = 0;
5183 buffer[SharedSecret->len + 3] = 1;
5184 if (SharedInfo) {
5185 PORT_Memcpy(&buffer[SharedSecret->len + 4], SharedInfo, SharedInfoLen);
5186 }
5187
5188 for(i=0; i < max_counter; i++) {
5189 rv = Hash(&output_buffer[i * HashLen], buffer, buffer_len);
5190 if (rv != SECSuccess)
5191 goto loser;
5192
5193 /* Increment counter (assumes max_counter < 255) */
5194 buffer[SharedSecret->len + 3]++;
5195 }
5196
5197 PORT_ZFree(buffer, buffer_len);
5198 if (key_len < max_counter * HashLen) {
5199 PORT_Memset(output_buffer + key_len, 0, max_counter * HashLen - key_len) ;
5200 }
5201 *key = output_buffer;
5202
5203 return SECSuccess;
5204
5205 loser:
5206 if (buffer) {
5207 PORT_ZFree(buffer, buffer_len);
5208 }
5209 if (output_buffer) {
5210 PORT_ZFree(output_buffer, max_counter * HashLen);
5211 }
5212 return rv;
5213 }
5214
5215 static CK_RV sftk_ANSI_X9_63_kdf(CK_BYTE **key, CK_ULONG key_len,
5216 SECItem *SharedSecret,
5217 CK_BYTE_PTR SharedInfo, CK_ULONG SharedInfoLen,
5218 CK_EC_KDF_TYPE kdf)
5219 {
5220 if (kdf == CKD_SHA1_KDF)
5221 return sftk_compute_ANSI_X9_63_kdf(key, key_len, SharedSecret, SharedInf o,
5222 SharedInfoLen, SHA1_HashBuf, SHA1_LENGTH);
5223 else if (kdf == CKD_SHA224_KDF)
5224 return sftk_compute_ANSI_X9_63_kdf(key, key_len, SharedSecret, SharedInf o,
5225 SharedInfoLen, SHA224_HashBuf, SHA224_LENGTH);
5226 else if (kdf == CKD_SHA256_KDF)
5227 return sftk_compute_ANSI_X9_63_kdf(key, key_len, SharedSecret, SharedInf o,
5228 SharedInfoLen, SHA256_HashBuf, SHA256_LENGTH);
5229 else if (kdf == CKD_SHA384_KDF)
5230 return sftk_compute_ANSI_X9_63_kdf(key, key_len, SharedSecret, SharedInf o,
5231 SharedInfoLen, SHA384_HashBuf, SHA384_LENGTH);
5232 else if (kdf == CKD_SHA512_KDF)
5233 return sftk_compute_ANSI_X9_63_kdf(key, key_len, SharedSecret, SharedInf o,
5234 SharedInfoLen, SHA512_HashBuf, SHA512_LENGTH);
5235 else
5236 return SEC_ERROR_INVALID_ALGORITHM;
5237 }
5238
5068 /* 5239 /*
5069 * SSL Key generation given pre master secret 5240 * SSL Key generation given pre master secret
5070 */ 5241 */
5071 #define NUM_MIXERS 9 5242 #define NUM_MIXERS 9
5072 static const char * const mixers[NUM_MIXERS] = { 5243 static const char * const mixers[NUM_MIXERS] = {
5073 "A", 5244 "A",
5074 "BB", 5245 "BB",
5075 "CCC", 5246 "CCC",
5076 "DDDD", 5247 "DDDD",
5077 "EEEEE", 5248 "EEEEE",
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after
5960 break; 6131 break;
5961 } 6132 }
5962 6133
5963 #ifdef NSS_ENABLE_ECC 6134 #ifdef NSS_ENABLE_ECC
5964 case CKM_ECDH1_DERIVE: 6135 case CKM_ECDH1_DERIVE:
5965 case CKM_ECDH1_COFACTOR_DERIVE: 6136 case CKM_ECDH1_COFACTOR_DERIVE:
5966 { 6137 {
5967 SECItem ecScalar, ecPoint; 6138 SECItem ecScalar, ecPoint;
5968 SECItem tmp; 6139 SECItem tmp;
5969 PRBool withCofactor = PR_FALSE; 6140 PRBool withCofactor = PR_FALSE;
5970 unsigned char secret_hash[20];
5971 unsigned char *secret; 6141 unsigned char *secret;
5972 unsigned char *keyData = NULL; 6142 unsigned char *keyData = NULL;
5973 int secretlen, curveLen, pubKeyLen; 6143 int secretlen, curveLen, pubKeyLen;
5974 CK_ECDH1_DERIVE_PARAMS *mechParams; 6144 CK_ECDH1_DERIVE_PARAMS *mechParams;
5975 NSSLOWKEYPrivateKey *privKey; 6145 NSSLOWKEYPrivateKey *privKey;
5976 PLArenaPool *arena = NULL; 6146 PLArenaPool *arena = NULL;
5977 6147
5978 /* Check mechanism parameters */ 6148 /* Check mechanism parameters */
5979 mechParams = (CK_ECDH1_DERIVE_PARAMS *) pMechanism->pParameter; 6149 mechParams = (CK_ECDH1_DERIVE_PARAMS *) pMechanism->pParameter;
5980 if ((pMechanism->ulParameterLen != sizeof(CK_ECDH1_DERIVE_PARAMS)) || 6150 if ((pMechanism->ulParameterLen != sizeof(CK_ECDH1_DERIVE_PARAMS)) ||
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
6046 if (arena) { 6216 if (arena) {
6047 PORT_FreeArena(arena,PR_FALSE); 6217 PORT_FreeArena(arena,PR_FALSE);
6048 arena=NULL; 6218 arena=NULL;
6049 } 6219 }
6050 6220
6051 if (rv != SECSuccess) { 6221 if (rv != SECSuccess) {
6052 crv = sftk_MapCryptError(PORT_GetError()); 6222 crv = sftk_MapCryptError(PORT_GetError());
6053 break; 6223 break;
6054 } 6224 }
6055 6225
6056 /*
6057 * tmp is the raw data created by ECDH_Derive,
6058 * secret and secretlen are the values we will eventually pass as our
6059 * generated key.
6060 */
6061 secret = tmp.data;
6062 secretlen = tmp.len;
6063 6226
6064 /* 6227 /*
6065 * apply the kdf function. 6228 * apply the kdf function.
6066 */ 6229 */
6067 » if (mechParams->kdf == CKD_SHA1_KDF) { 6230 » if (mechParams->kdf == CKD_NULL) {
6068 » /* Compute SHA1 hash */ 6231 » /*
6069 » PORT_Memset(secret_hash, 0, 20); 6232 » * tmp is the raw data created by ECDH_Derive,
6070 » rv = SHA1_HashBuf(secret_hash, tmp.data, tmp.len); 6233 » * secret and secretlen are the values we will
6234 » * eventually pass as our generated key.
6235 » */
6236 » secret = tmp.data;
6237 » secretlen = tmp.len;
6238 » } else {
6239 » secretlen = keySize;
6240 » rv = sftk_ANSI_X9_63_kdf(&secret, keySize,
6241 » » » &tmp, mechParams->pSharedData,
6242 » » » mechParams->ulSharedDataLen, mechParams->kdf);
6243 » PORT_ZFree(tmp.data, tmp.len);
6071 if (rv != SECSuccess) { 6244 if (rv != SECSuccess) {
6072 PORT_ZFree(tmp.data, tmp.len);
6073 crv = CKR_HOST_MEMORY; 6245 crv = CKR_HOST_MEMORY;
6074 break; 6246 break;
6075 » } 6247 » }
6076 » secret = secret_hash; 6248 » tmp.data = secret;
6077 » secretlen = 20; 6249 » tmp.len = secretlen;
6078 } 6250 }
6079 6251
6080 /* 6252 /*
6081 * if keySize is supplied, then we are generating a key of a specific 6253 * if keySize is supplied, then we are generating a key of a specific
6082 * length. This is done by taking the least significant 'keySize' 6254 * length. This is done by taking the least significant 'keySize'
6083 * bytes from the unsigned value calculated by ECDH. Note: this may 6255 * bytes from the unsigned value calculated by ECDH. Note: this may
6084 * mean padding temp with extra leading zeros from what ECDH_Derive 6256 * mean padding temp with extra leading zeros from what ECDH_Derive
6085 * already returned (which itself may contain leading zeros). 6257 * already returned (which itself may contain leading zeros).
6086 */ 6258 */
6087 if (keySize) { 6259 if (keySize) {
(...skipping 10 matching lines...) Expand all
6098 secret += (secretlen - keySize); 6270 secret += (secretlen - keySize);
6099 } 6271 }
6100 secretlen = keySize; 6272 secretlen = keySize;
6101 } 6273 }
6102 6274
6103 sftk_forceAttribute(key, CKA_VALUE, secret, secretlen); 6275 sftk_forceAttribute(key, CKA_VALUE, secret, secretlen);
6104 PORT_ZFree(tmp.data, tmp.len); 6276 PORT_ZFree(tmp.data, tmp.len);
6105 if (keyData) { 6277 if (keyData) {
6106 PORT_ZFree(keyData, keySize); 6278 PORT_ZFree(keyData, keySize);
6107 } 6279 }
6108 PORT_Memset(secret_hash, 0, 20);
6109
6110 break; 6280 break;
6111 6281
6112 ec_loser: 6282 ec_loser:
6113 crv = CKR_ARGUMENTS_BAD; 6283 crv = CKR_ARGUMENTS_BAD;
6114 PORT_Free(ecScalar.data); 6284 PORT_Free(ecScalar.data);
6115 if (privKey != sourceKey->objectInfo) 6285 if (privKey != sourceKey->objectInfo)
6116 nsslowkey_DestroyPrivateKey(privKey); 6286 nsslowkey_DestroyPrivateKey(privKey);
6117 if (arena) { 6287 if (arena) {
6118 PORT_FreeArena(arena, PR_FALSE); 6288 PORT_FreeArena(arena, PR_FALSE);
6119 } 6289 }
(...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after
6532 att = sftk_FindAttribute(key,CKA_VALUE); 6702 att = sftk_FindAttribute(key,CKA_VALUE);
6533 sftk_FreeObject(key); 6703 sftk_FreeObject(key);
6534 if (!att) { 6704 if (!att) {
6535 return CKR_KEY_HANDLE_INVALID; 6705 return CKR_KEY_HANDLE_INVALID;
6536 } 6706 }
6537 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, 6707 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue,
6538 att->attrib.ulValueLen); 6708 att->attrib.ulValueLen);
6539 sftk_FreeAttribute(att); 6709 sftk_FreeAttribute(att);
6540 return crv; 6710 return crv;
6541 } 6711 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698