35 static int readAttributeValue(
PKCS11_INFO *pkcs11Info,
38 void *
buffer,
const int bufMaxLen,
41 CK_ATTRIBUTE attrTemplate = { attrType, NULL_PTR, bufMaxLen };
50 memset( buffer, 0,
min( 16, bufMaxLen ) );
53 status = C_GetAttributeValue( pkcs11Info->
hSession, hObject,
58 status = C_GetAttributeValue( pkcs11Info->
hSession, hObject,
64 return( cryptStatus );
80 const void *inBuffer,
const int inLength,
88 assert(
isReadPtr( inBuffer, inLength ) );
100 status = C_SignInit( pkcs11Info->
hSession,
102 contextInfoPtr->deviceObject );
105 inLength, outBuffer, &resultLen );
115 const void *inBuffer,
const int inLength,
116 void *outBuffer,
const int outLength )
123 assert(
isReadPtr( inBuffer, inLength ) );
135 status = C_VerifyInit( pkcs11Info->
hSession,
137 contextInfoPtr->deviceObject );
140 inLength, outBuffer, outLength );
149 static int genericEncrypt(
PKCS11_INFO *pkcs11Info,
152 const int length,
const int outLength )
166 status = C_EncryptInit( pkcs11Info->
hSession,
168 contextInfoPtr->deviceObject );
170 status = C_Encrypt( pkcs11Info->
hSession, buffer, length,
171 buffer, &resultLen );
179 (
int ) resultLen < length )
181 const int delta = length - resultLen;
184 memmove( (
BYTE * ) buffer + delta, buffer, resultLen );
185 memset( buffer, 0, delta );
191 static int genericDecrypt(
PKCS11_INFO *pkcs11Info,
194 const int length,
int *resultLength )
203 assert(
isWritePtr( resultLength,
sizeof(
int ) ) );
207 status = C_DecryptInit( pkcs11Info->
hSession,
209 contextInfoPtr->deviceObject );
211 status = C_Decrypt( pkcs11Info->
hSession, buffer, length,
212 buffer, &resultLen );
237 status = C_UnwrapKey( pkcs11Info->
hSession,
239 contextInfoPtr->deviceObject, buffer, length,
240 symTemplate, 4, &hSymKey );
245 status = C_GetAttributeValue( pkcs11Info->
hSession,
246 hSymKey, valueTemplate, 1 );
249 C_DestroyObject( pkcs11Info->
hSession, hSymKey );
260 (
int ) resultLen < length )
262 const int delta = length - resultLen;
265 memmove( (
BYTE * ) buffer + delta, buffer, resultLen );
266 memset( buffer, 0, delta );
272 if( resultLength != NULL )
273 *resultLength = ( int ) resultLen;
297 int dhSetPublicComponents(
PKCS11_INFO *pkcs11Info,
300 const void *q,
const int qLen )
315 cryptStatus = readAttributeValue( pkcs11Info, hDhKey,
CKA_PRIME,
318 cryptStatus = readAttributeValue( pkcs11Info, hDhKey,
CKA_BASE,
321 cryptStatus = readAttributeValue( pkcs11Info, hDhKey,
CKA_VALUE,
324 return( cryptStatus );
335 p, pLen, g, gLen, q, qLen, y, yLen );
341 CRYPT_IATTRIBUTE_KEY_SPKI_PARTIAL );
343 return( cryptStatus );
375 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
376 &iCryptDevice, &pkcs11Info );
378 return( cryptStatus );
388 status = C_GenerateKeyPair( pkcs11Info->
hSession,
392 &hPublicKey, &hPrivateKey );
397 return( cryptStatus );
401 cryptStatus = dhSetPublicComponents( pkcs11Info,
403 hPublicKey, dhKey->
q,
408 return( cryptStatus );
414 CRYPT_IATTRIBUTE_DEVICEOBJECT );
415 contextInfoPtr->altDeviceObject = hPublicKey;
418 return( cryptStatus );
421 static int dhGenerateKey(
CONTEXT_INFO *contextInfoPtr,
const int keysizeBits )
448 return( cryptStatus );
451 &msgData, CRYPT_IATTRIBUTE_RANDOM_NONCE );
461 CRYPT_IATTRIBUTE_KEY_SPKI );
465 return( cryptStatus );
474 sMemConnect( &stream, pubkeyBuffer, msgData.
length );
475 readSequence( &stream, NULL );
476 readSequence( &stream, NULL );
477 readUniversal( &stream );
478 readSequence( &stream, NULL );
479 readGenericHole( &stream, &length, 16,
BER_INTEGER );
480 cryptStatus = sread( &stream, dhKey.
p, length );
484 readGenericHole( &stream, &length, 16,
BER_INTEGER );
485 cryptStatus = sread( &stream, dhKey.
q, length );
490 readGenericHole( &stream, &length, 16,
BER_INTEGER );
491 cryptStatus = sread( &stream, dhKey.
g, length );
495 sMemDisconnect( &stream );
502 static int dhEncrypt(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
517 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
518 &iCryptDevice, &pkcs11Info );
520 return( cryptStatus );
526 status = C_GetAttributeValue( pkcs11Info->
hSession,
527 contextInfoPtr->altDeviceObject,
528 &yValueTemplate, 1 );
531 yValueTemplate.
pValue = keyAgreeParams->publicValue;
532 status = C_GetAttributeValue( pkcs11Info->
hSession,
533 contextInfoPtr->altDeviceObject,
534 &yValueTemplate, 1 );
540 return( cryptStatus );
543 static int dhDecrypt(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
567 REQUIRES( keyAgreeParams->publicValue != NULL && \
572 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
573 &iCryptDevice, &pkcs11Info );
575 return( cryptStatus );
582 mechanism.
pParameter = keyAgreeParams->publicValue;
584 status = C_DeriveKey( pkcs11Info->
hSession, &mechanism,
585 contextInfoPtr->deviceObject,
586 symTemplate, 4, &hSymKey );
592 status = C_GetAttributeValue( pkcs11Info->
hSession,
593 hSymKey, valueTemplate, 1 );
596 C_DestroyObject( pkcs11Info->
hSession, hSymKey );
600 return( cryptStatus );
617 int rsaSetPublicComponents(
PKCS11_INFO *pkcs11Info,
625 int nLen, eLen =
DUMMY_INIT, keyDataSize, cryptStatus;
632 cryptStatus = readAttributeValue( pkcs11Info, hRsaKey,
CKA_MODULUS,
638 return( cryptStatus );
649 n, nLen, e, eLen, NULL, 0, NULL, 0 );
651 return( cryptStatus );
656 &msgData, CRYPT_IATTRIBUTE_KEY_SPKI ) );
659 &msgData, CRYPT_IATTRIBUTE_KEY_SPKI_PARTIAL ) );
677 CRYPT_IATTRIBUTE_DEVICEOBJECT );
686 CRYPT_IATTRIBUTE_KEYID );
691 if( hPublicKey != CK_OBJECT_NONE )
692 C_SetAttributeValue( pkcs11Info->
hSession, hPublicKey,
694 C_SetAttributeValue( pkcs11Info->
hSession, hPrivateKey,
698 return( cryptStatus );
701 static int rsaInitKey(
CONTEXT_INFO *contextInfoPtr,
const void *key,
702 const int keyLength )
732 const int templateCount = rsaKey->
isPublicKey ? 8 : 15;
741 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
742 &iCryptDevice, &pkcs11Info );
744 return( cryptStatus );
776 status = C_CreateObject( pkcs11Info->
hSession, rsaKeyTemplate,
777 templateCount, &hRsaKey );
791 return( cryptStatus );
796 cryptStatus = rsaSetPublicComponents( pkcs11Info,
800 cryptStatus = rsaSetKeyInfo( pkcs11Info, contextInfoPtr,
801 hRsaKey, CK_OBJECT_NONE );
803 C_DestroyObject( pkcs11Info->
hSession, hRsaKey );
811 return( cryptStatus );
814 static int rsaGenerateKey(
CONTEXT_INFO *contextInfoPtr,
const int keysizeBits )
819 const CK_ULONG modulusBits = keysizeBits;
848 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
849 &iCryptDevice, &pkcs11Info );
851 return( cryptStatus );
854 status = C_GenerateKeyPair( pkcs11Info->
hSession,
856 publicKeyTemplate, 6, privateKeyTemplate, 6,
857 &hPublicKey, &hPrivateKey );
862 return( cryptStatus );
867 cryptStatus = rsaSetPublicComponents( pkcs11Info,
871 cryptStatus = rsaSetKeyInfo( pkcs11Info, contextInfoPtr, hPrivateKey,
875 C_DestroyObject( pkcs11Info->
hSession, hPublicKey );
876 C_DestroyObject( pkcs11Info->
hSession, hPrivateKey );
885 return( cryptStatus );
888 static int rsaSign(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
904 assert( bufPtr[ 0 ] == 0 && bufPtr[ 1 ] == 1 && bufPtr[ 2 ] == 0xFF );
907 if( bufPtr[ i ] == 0 )
913 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
914 &iCryptDevice, &pkcs11Info );
916 return( cryptStatus );
917 cryptStatus = genericSign( pkcs11Info, contextInfoPtr, &mechanism,
918 bufPtr + i, keySize - i, buffer, keySize );
920 return( cryptStatus );
923 static int rsaVerify(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
929 const int keySize =
bitsToBytes( contextInfoPtr->ctxPKC->keySizeBits );
943 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
944 &iCryptDevice, &pkcs11Info );
946 return( cryptStatus );
947 cryptStatus = genericVerify( pkcs11Info, contextInfoPtr, &mechanism,
948 data, keySize, buffer, keySize );
950 return( cryptStatus );
953 static int rsaEncrypt(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
959 const int keySize =
bitsToBytes( contextInfoPtr->ctxPKC->keySizeBits );
975 assert( bufPtr[ 0 ] == 0 && bufPtr[ 1 ] == 2 );
978 if( bufPtr[ i ] == 0 )
982 memmove( bufPtr, bufPtr + i, keySize - i );
985 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
986 &iCryptDevice, &pkcs11Info );
988 return( cryptStatus );
989 cryptStatus = genericEncrypt( pkcs11Info, contextInfoPtr, &mechanism,
990 bufPtr, keySize - i, keySize );
992 return( cryptStatus );
995 static int rsaDecrypt(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
1002 const int keySize =
bitsToBytes( contextInfoPtr->ctxPKC->keySizeBits );
1003 int cryptStatus, i, resultLen;
1011 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
1012 &iCryptDevice, &pkcs11Info );
1014 return( cryptStatus );
1015 cryptStatus = genericDecrypt( pkcs11Info, contextInfoPtr, &mechanism,
1016 buffer, keySize, &resultLen );
1019 return( cryptStatus );
1041 memmove( bufPtr + keySize - resultLen, bufPtr, resultLen );
1047 CRYPT_IATTRIBUTE_RANDOM_NONCE );
1048 for( i = 2; i < keySize - resultLen - 1; i++ )
1050 if( bufPtr[ i ] == 0 )
1056 const int pad = 0xAA ^ ( i & 0xFF );
1057 bufPtr[ i ] = pad ? pad : 0x21;
1060 bufPtr[ keySize - resultLen - 1 ] = 0;
1061 ENSURES( 2 + ( keySize - resultLen - 3 ) + 1 + resultLen == keySize );
1063 return( cryptStatus );
1074 static int dsaSetKeyInfo(
PKCS11_INFO *pkcs11Info,
1078 const void *p,
const int pLen,
1079 const void *q,
const int qLen,
1080 const void *g,
const int gLen,
1081 const void *y,
const int yLen,
1087 int keyDataSize, cryptStatus;
1106 p, pLen, q, qLen, g, gLen, y, yLen );
1108 return( cryptStatus );
1115 &msgData, CRYPT_IATTRIBUTE_KEY_SPKI ) );
1118 &msgData, CRYPT_IATTRIBUTE_KEY_SPKI_PARTIAL );
1120 return( cryptStatus );
1125 CRYPT_IATTRIBUTE_DEVICEOBJECT );
1132 &msgData, CRYPT_IATTRIBUTE_KEYID );
1138 C_SetAttributeValue( pkcs11Info->
hSession, hPublicKey,
1140 C_SetAttributeValue( pkcs11Info->
hSession, hPrivateKey,
1144 return( cryptStatus );
1147 int dsaSetPublicComponents(
PKCS11_INFO *pkcs11Info,
1161 cryptStatus = readAttributeValue( pkcs11Info, hDsaKey,
CKA_PRIME,
1164 cryptStatus = readAttributeValue( pkcs11Info, hDsaKey,
CKA_SUBPRIME,
1167 cryptStatus = readAttributeValue( pkcs11Info, hDsaKey,
CKA_BASE,
1170 cryptStatus = readAttributeValue( pkcs11Info, hDsaKey,
CKA_VALUE,
1173 return( cryptStatus );
1175 return( dsaSetKeyInfo( pkcs11Info, iCryptContext, CK_OBJECT_NONE, hDsaKey,
1176 p, pLen, q, qLen, g, gLen, y, yLen,
TRUE ) );
1179 static int dsaInitKey(
CONTEXT_INFO *contextInfoPtr,
const void *key,
1180 const int keyLength )
1206 const void *yValuePtr;
1207 const int templateCount = dsaKey->
isPublicKey ? 9 : 10;
1208 int yValueLength, cryptStatus;
1240 return( cryptStatus );
1243 &msgData, CRYPT_IATTRIBUTE_RANDOM_NONCE );
1254 return( cryptStatus );
1265 CRYPT_IATTRIBUTE_KEY_SPKI );
1268 return( cryptStatus );
1269 sMemConnect( &stream, msgData.data, msgData.
length );
1270 readSequence( &stream, NULL );
1271 readUniversal( &stream );
1272 readBitStringHole( &stream, NULL, 16,
DEFAULT_TAG );
1273 status = readGenericHole( &stream, &yValueDataSize, 16,
1276 status = sMemGetDataBlock( &stream, &yValueDataPtr,
1279 ENSURES( yValueDataSize >= 16 && \
1281 memcpy( yValue, yValueDataPtr, yValueDataSize );
1282 sMemDisconnect( &stream );
1286 yValueLength = yValueDataSize;
1291 yValuePtr = dsaKey->y,
1296 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
1297 &iCryptDevice, &pkcs11Info );
1299 return( cryptStatus );
1308 if( !dsaKey->isPublicKey )
1325 status = C_CreateObject( pkcs11Info->
hSession, dsaKeyTemplate,
1326 templateCount, &hDsaKey );
1334 if( dsaKey->isPublicKey && \
1339 return( cryptStatus );
1344 cryptStatus = dsaSetKeyInfo( pkcs11Info, contextInfoPtr->
objectHandle,
1345 hDsaKey, CK_OBJECT_NONE,
1349 yValuePtr, yValueLength,
FALSE );
1351 C_DestroyObject( pkcs11Info->
hSession, hDsaKey );
1357 return( cryptStatus );
1360 static int dsaGenerateKey(
CONTEXT_INFO *contextInfoPtr,
const int keysizeBits )
1413 return( cryptStatus );
1416 &msgData, CRYPT_IATTRIBUTE_RANDOM_NONCE );
1426 CRYPT_IATTRIBUTE_KEY_SPKI );
1430 return( cryptStatus );
1438 sMemConnect( &stream, pubkeyBuffer, msgData.
length );
1439 readSequence( &stream, NULL );
1440 readSequence( &stream, NULL );
1441 readUniversal( &stream );
1442 readSequence( &stream, NULL );
1443 status = readGenericHole( &stream, &length, 16,
BER_INTEGER );
1445 status = sMemGetDataBlock( &stream, &dataPtr, length );
1448 publicKeyTemplate[ 3 ].
pValue = dataPtr;
1450 sSkip( &stream, length );
1451 status = readGenericHole( &stream, &length, 16,
BER_INTEGER );
1453 status = sMemGetDataBlock( &stream, &dataPtr, length );
1456 publicKeyTemplate[ 4 ].
pValue = dataPtr;
1458 sSkip( &stream, length );
1459 status = readGenericHole( &stream, &length, 16,
BER_INTEGER );
1461 status = sMemGetDataBlock( &stream, &dataPtr, length );
1464 publicKeyTemplate[ 5 ].
pValue = dataPtr;
1466 sMemDisconnect( &stream );
1469 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
1470 &iCryptDevice, &pkcs11Info );
1472 return( cryptStatus );
1475 status = C_GenerateKeyPair( pkcs11Info->
hSession,
1479 &hPublicKey, &hPrivateKey );
1484 return( cryptStatus );
1491 status = C_GetAttributeValue( pkcs11Info->
hSession, hPublicKey,
1492 &yValueTemplate, 1 );
1495 yValueTemplate.
pValue = pubkeyBuffer;
1496 status = C_GetAttributeValue( pkcs11Info->
hSession, hPublicKey,
1497 &yValueTemplate, 1 );
1501 cryptStatus = dsaSetKeyInfo( pkcs11Info, contextInfoPtr->
objectHandle,
1502 hPrivateKey, hPublicKey,
1509 C_DestroyObject( pkcs11Info->
hSession, hPublicKey );
1510 C_DestroyObject( pkcs11Info->
hSession, hPrivateKey );
1517 return( cryptStatus );
1520 static int dsaSign(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
1526 PKC_INFO *dsaKey = contextInfoPtr->ctxPKC;
1535 REQUIRES( dlpParams->inParam1 != NULL && \
1536 dlpParams->
inLen1 == 20 );
1537 REQUIRES( dlpParams->inParam2 == NULL && dlpParams->
inLen2 == 0 );
1538 REQUIRES( dlpParams->outParam != NULL && \
1539 dlpParams->
outLen >= ( 2 + 20 ) * 2 && \
1543 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
1544 &iCryptDevice, &pkcs11Info );
1546 return( cryptStatus );
1547 cryptStatus = genericSign( pkcs11Info, contextInfoPtr, &mechanism,
1548 dlpParams->inParam1, dlpParams->
inLen1,
1552 return( cryptStatus );
1566 cryptStatus = importBignum( r, signature, 20,
1570 cryptStatus = importBignum( r, signature + 20, 20,
1576 dsaKey->encodeDLValuesFunction( dlpParams->outParam,
1582 return( cryptStatus );
1585 static int dsaVerify(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int length )
1590 PKC_INFO *dsaKey = contextInfoPtr->ctxPKC;
1604 REQUIRES( dlpParams->inParam1 != NULL && dlpParams->
inLen1 == 20 );
1605 REQUIRES( dlpParams->inParam2 != NULL && \
1607 dlpParams->
inLen2 >= 46 ) || \
1609 dlpParams->
inLen2 == 44 ) || \
1610 ( dlpParams->
formatType == CRYPT_IFORMAT_SSH && \
1611 dlpParams->
inLen2 == 40 ) ) );
1612 REQUIRES( dlpParams->outParam == NULL && dlpParams->
outLen == 0 );
1619 dsaKey->decodeDLValuesFunction( dlpParams->inParam2, dlpParams->
inLen2,
1622 return( cryptStatus );
1630 cryptStatus = getContextDeviceInfo( contextInfoPtr->
objectHandle,
1631 &iCryptDevice, &pkcs11Info );
1633 return( cryptStatus );
1634 cryptStatus = genericVerify( pkcs11Info, contextInfoPtr, &mechanism,
1635 buffer, 20, signature, 40 );
1637 return( cryptStatus );
1649 static const PKCS11_MECHANISM_INFO mechanismInfoPKC[] = {
1669 NULL, dhInitKey, dhGenerateKey,
1670 dhEncrypt, dhDecrypt, NULL, NULL },
1673 NULL, rsaInitKey, rsaGenerateKey,
1674 rsaEncrypt, rsaDecrypt, rsaSign, rsaVerify },
1677 NULL, dsaInitKey, dsaGenerateKey,
1678 NULL, NULL, dsaSign, dsaVerify },
1684 const PKCS11_MECHANISM_INFO *getMechanismInfoPKC(
int *mechanismInfoSize )
1686 assert(
isWritePtr( mechanismInfoSize,
sizeof(
int ) ) );
1689 PKCS11_MECHANISM_INFO );
1690 return( mechanismInfoPKC );