13 #if defined( INC_ALL )
43 #define STORAGEID_SIZE 8
82 BYTE storageID[ STORAGEID_SIZE ];
87 BIGNUM_STORAGE pkcKeyInfo[ NO_BIGNUMS ];
98 #define NO_PERSONALITIES 8
100 static PERSONALITY_INFO personalityInfo[ NO_PERSONALITIES ] = { 0 };
120 const int storageIDlength =
min( keyIDlength, STORAGEID_SIZE );
123 assert(
isReadPtr( keyID, keyIDlength ) );
124 assert(
isWritePtr( keyHandle,
sizeof(
int ) ) );
133 for( i = 0; i < NO_PERSONALITIES; i++ )
135 PERSONALITY_INFO *personalityInfoPtr = &personalityInfo[ i ];
137 if( !personalityInfoPtr->inUse )
139 if( !memcmp( personalityInfoPtr->storageID, keyID, storageIDlength ) )
150 static int findFreePersonality(
int *keyHandle )
154 assert(
isWritePtr( keyHandle,
sizeof(
int ) ) );
160 for( i = 0; i < NO_PERSONALITIES; i++ )
162 PERSONALITY_INFO *personalityInfoPtr = &personalityInfo[ i ];
164 if( !personalityInfoPtr->inUse )
166 zeroise( personalityInfoPtr,
sizeof( PERSONALITY_INFO ) );
176 static void deletePersonality(
const int keyHandle )
178 PERSONALITY_INFO *personalityInfoPtr;
180 REQUIRES_V( keyHandle >= 0 && keyHandle < NO_PERSONALITIES );
182 if( keyHandle < 0 || keyHandle >= NO_PERSONALITIES )
184 personalityInfoPtr = &personalityInfo[ keyHandle ];
185 zeroise( personalityInfoPtr,
sizeof( PERSONALITY_INFO ) );
197 static void bignumToInternal( LONG *outData,
int *outDataLength,
198 const BYTE *inData,
const int inDataLength )
200 int inIndex, outIndex = 0, i;
203 assert(
isWritePtr( outDataLength,
sizeof(
int ) ) );
204 assert(
isReadPtr( inData, inDataLength ) );
210 for( inIndex = 0; inIndex < inDataLength; inIndex +=
sizeof( LONG ) )
212 outData[ outIndex++ ] =
mgetLong( inData );
214 *outDataLength = outIndex;
217 static void bignumToExternal(
BYTE *outData,
int *outDataLength,
218 const LONG *inData,
const int inDataLength )
220 int inIndex = 0, outIndex;
223 assert(
isWritePtr( outDataLength,
sizeof(
int ) ) );
224 assert(
isReadPtr( inData, inDataLength *
sizeof( LONG ) ) );
230 for( outIndex = 0; outIndex < inDataLength; outIndex++ )
232 const LONG
value = inData[ inIndex++ ];
236 *outDataLength = outIndex *
sizeof( LONG );
242 static void dummyEncrypt(
const PERSONALITY_INFO *personalityInfoPtr,
249 assert(
isReadPtr( personalityInfoPtr,
sizeof( PERSONALITY_INFO ) ) );
253 cryptAlgo < CRYPT_ALGO_LAST_EXTERNAL );
260 int bignumDataLength;
262 bignumToExternal( bignumData, &bignumDataLength,
263 personalityInfoPtr->keyInfo.pkcKeyInfo[ 0 ].data,
264 personalityInfoPtr->keyInfo.pkcKeyInfo[ 0 ].dataSize );
265 for( i = 0; i <
length; i++ )
266 data[ i ] ^= bignumData[ i ];
280 for( i = 0; i <
length; i++ )
281 data[ i ] ^= personalityInfoPtr->keyInfo.convKeyInfo[ keyDataOffset ];
288 for( i = 0; i <
length; i++ )
289 data[ i ] ^= personalityInfoPtr->keyInfo.convKeyInfo[ i % 16 ];
293 static void dummyGenRandom(
void *
buffer,
const int length )
297 static int counter = 0;
310 memset( hashBuffer, counter, hashSize );
312 for( i = 0; i <
length; i++ )
314 if( i % hashSize == 0 )
317 hashBuffer, hashSize );
319 bufPtr[ i ] = hashBuffer[ i %
hashSize ];
331 static int aesSelfTest(
void )
342 PERSONALITY_INFO *personalityInfoPtr,
343 const int keyHandle,
const int keySize )
348 assert(
isWritePtr( personalityInfoPtr,
sizeof( PERSONALITY_INFO ) ) );
350 REQUIRES( keyHandle >= 0 && keyHandle < NO_PERSONALITIES );
360 personalityInfoPtr->storageID,
365 deletePersonality( keyHandle );
368 personalityInfoPtr->inUse =
TRUE;
376 PERSONALITY_INFO *personalityInfoPtr;
385 status = findFreePersonality( &keyHandle );
388 personalityInfoPtr = &personalityInfo[ keyHandle ];
391 memcpy( personalityInfoPtr->keyInfo.convKeyInfo, key, keyLength );
392 return( completeInitKeyAES( contextInfoPtr, personalityInfoPtr,
393 keyHandle, keyLength ) );
398 static int aesGenerateKey(
CONTEXT_INFO *contextInfoPtr,
399 const int keySizeBits )
401 PERSONALITY_INFO *personalityInfoPtr;
411 status = findFreePersonality( &keyHandle );
414 personalityInfoPtr = &personalityInfo[ keyHandle ];
417 status =
hwGetRandom( personalityInfoPtr->keyInfo.convKeyInfo, keyLength );
420 deletePersonality( keyHandle );
423 return( completeInitKeyAES( contextInfoPtr, personalityInfoPtr,
424 keyHandle, keyLength ) );
429 static int aesEncryptECB(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
432 PERSONALITY_INFO *personalityInfoPtr = \
433 &personalityInfo[ contextInfoPtr->deviceObject ];
444 static int aesDecryptECB(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
447 PERSONALITY_INFO *personalityInfoPtr = \
448 &personalityInfo[ contextInfoPtr->deviceObject ];
460 static int aesEncryptCBC(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
463 PERSONALITY_INFO *personalityInfoPtr = \
464 &personalityInfo[ contextInfoPtr->deviceObject ];
475 static int aesDecryptCBC(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
478 PERSONALITY_INFO *personalityInfoPtr = \
479 &personalityInfo[ contextInfoPtr->deviceObject ];
491 static int aesEncryptCFB(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
494 PERSONALITY_INFO *personalityInfoPtr = \
495 &personalityInfo[ contextInfoPtr->deviceObject ];
506 static int aesDecryptCFB(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
509 PERSONALITY_INFO *personalityInfoPtr = \
510 &personalityInfo[ contextInfoPtr->deviceObject ];
522 static int aesEncryptOFB(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
525 PERSONALITY_INFO *personalityInfoPtr = \
526 &personalityInfo[ contextInfoPtr->deviceObject ];
537 static int aesDecryptOFB(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
540 PERSONALITY_INFO *personalityInfoPtr = \
541 &personalityInfo[ contextInfoPtr->deviceObject ];
561 static int rsaSelfTest(
void )
571 static int completeInitKeyRSA(
CONTEXT_INFO *contextInfoPtr,
572 PERSONALITY_INFO *personalityInfoPtr,
573 const int keyHandle )
578 assert(
isWritePtr( personalityInfoPtr,
sizeof( PERSONALITY_INFO ) ) );
580 REQUIRES( keyHandle >= 0 && keyHandle < NO_PERSONALITIES );
586 personalityInfoPtr->storageID,
590 deletePersonality( keyHandle );
593 personalityInfoPtr->inUse =
TRUE;
598 static void rsaKeyToInternal( BIGNUM_STORAGE *bignumStorage,
602 sizeof( BIGNUM_STORAGE ) * NO_BIGNUMS ) );
607 bignumToInternal( bignumStorage[ 0 ].data, &bignumStorage[ 0 ].
dataSize,
609 bignumToInternal( bignumStorage[ 1 ].data, &bignumStorage[ 1 ].dataSize,
613 if( rsaKeyInfo->
dLen > 0 )
615 bignumToInternal( bignumStorage[ 2 ].data,
616 &bignumStorage[ 2 ].dataSize,
619 bignumToInternal( bignumStorage[ 3 ].data,
620 &bignumStorage[ 3 ].dataSize,
622 bignumToInternal( bignumStorage[ 4 ].data,
623 &bignumStorage[ 4 ].dataSize,
625 if( rsaKeyInfo->
e1Len > 0 )
627 bignumToInternal( bignumStorage[ 5 ].data,
628 &bignumStorage[ 5 ].dataSize,
630 bignumToInternal( bignumStorage[ 6 ].data,
631 &bignumStorage[ 6 ].dataSize,
633 bignumToInternal( bignumStorage[ 7 ].data,
634 &bignumStorage[ 7 ].dataSize,
639 static int rsaInitKey(
CONTEXT_INFO *contextInfoPtr,
const void *key,
640 const int keyLength )
642 PERSONALITY_INFO *personalityInfoPtr;
651 status = findFreePersonality( &keyHandle );
654 personalityInfoPtr = &personalityInfo[ keyHandle ];
659 rsaKeyToInternal( personalityInfoPtr->keyInfo.pkcKeyInfo, key );
664 deletePersonality( keyHandle );
667 return( completeInitKeyRSA( contextInfoPtr, personalityInfoPtr,
673 static int rsaGenerateKey(
CONTEXT_INFO *contextInfoPtr,
674 const int keySizeBits )
677 PERSONALITY_INFO *personalityInfoPtr;
686 status = findFreePersonality( &keyHandle );
689 personalityInfoPtr = &personalityInfo[ keyHandle ];
697 deletePersonality( keyHandle );
700 rsaKeyToInternal( personalityInfoPtr->keyInfo.pkcKeyInfo, &rsaKeyInfo );
702 return( completeInitKeyRSA( contextInfoPtr, personalityInfoPtr,
708 static int rsaEncrypt(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
711 PERSONALITY_INFO *personalityInfoPtr = \
712 &personalityInfo[ contextInfoPtr->deviceObject ];
724 static int rsaDecrypt(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
727 PERSONALITY_INFO *personalityInfoPtr = \
728 &personalityInfo[ contextInfoPtr->deviceObject ];
742 static int rsaSign(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
745 PERSONALITY_INFO *personalityInfoPtr = \
746 &personalityInfo[ contextInfoPtr->deviceObject ];
758 static int rsaSigCheck(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
761 PERSONALITY_INFO *personalityInfoPtr = \
762 &personalityInfo[ contextInfoPtr->deviceObject ];
782 static int shaSelfTest(
void )
794 void *data,
const int length )
809 return( getDefaultInfo( type, contextInfoPtr, data, length ) );
814 static int shaHash(
CONTEXT_INFO *contextInfoPtr,
void *buffer,
818 assert( length == 0 ||
isWritePtr( buffer, length ) );
840 memset( contextInfoPtr->ctxHash->hash,
'X', 20 );
859 rsaEncrypt, rsaDecrypt, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
860 rsaSign, rsaSigCheck },
866 aesEncryptECB, aesDecryptECB, aesEncryptCBC, aesDecryptCBC,
867 aesEncryptCFB, aesDecryptCFB, aesEncryptOFB, aesDecryptOFB,
873 shaSelfTest, shaGetInfo, NULL, NULL, NULL, NULL, shaHash, shaHash },
883 int *noCapabilities )
886 assert(
isWritePtr( noCapabilities,
sizeof(
int ) ) );
888 *capabilityInfo = capabilities;
903 dummyGenRandom( buffer, length );
910 int hwLookupItem(
const void *keyID,
const int keyIDlength,
int *keyHandle )
912 assert(
isReadPtr( keyID, keyIDlength ) );
913 assert(
isWritePtr( keyHandle,
sizeof(
int ) ) );
920 return( lookupPersonality( keyID, keyIDlength, keyHandle ) );
927 REQUIRES( keyHandle >= 0 && keyHandle < NO_PERSONALITIES );
929 deletePersonality( keyHandle );
940 for( i = 0; i < NO_PERSONALITIES; i++ )
941 deletePersonality( i );