📄 keyload.c
字号:
CRYPT_CONTEXT *sigCheckContext,
const C_STR signContextLabel,
const C_STR sigCheckContextLabel )
{
CRYPT_PKCINFO_DLP *dsaKey;
const BOOLEAN isDevice = ( cryptDevice != CRYPT_UNUSED ) ? TRUE : FALSE;
int status;
/* Allocate room for the public-key components */
if( ( dsaKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
return( CRYPT_ERROR_MEMORY );
/* Create the encryption context */
if( signContext != NULL )
{
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, signContext,
CRYPT_ALGO_DSA );
else
status = cryptCreateContext( signContext, CRYPT_UNUSED,
CRYPT_ALGO_DSA );
if( cryptStatusError( status ) )
{
free( dsaKey );
printf( "cryptCreateContext() failed with error code %d.\n",
status );
return( FALSE );
}
if( !setLabel( *signContext, signContextLabel ) )
{
free( dsaKey );
cryptDestroyContext( *signContext );
return( FALSE );
}
cryptInitComponents( dsaKey, CRYPT_KEYTYPE_PRIVATE );
cryptSetComponent( dsaKey->p, dlpTestKey.p, dlpTestKey.pLen );
cryptSetComponent( dsaKey->q, dlpTestKey.q, dlpTestKey.qLen );
cryptSetComponent( dsaKey->g, dlpTestKey.g, dlpTestKey.gLen );
cryptSetComponent( dsaKey->x, dlpTestKey.x, dlpTestKey.xLen );
cryptSetComponent( dsaKey->y, dlpTestKey.y, dlpTestKey.yLen );
status = cryptSetAttributeString( *signContext,
CRYPT_CTXINFO_KEY_COMPONENTS, dsaKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( dsaKey );
if( cryptStatusError( status ) )
{
free( dsaKey );
cryptDestroyContext( *signContext );
printf( "Key load failed with error code %d.\n", status );
return( FALSE );
}
if( sigCheckContext == NULL )
{
free( dsaKey );
return( TRUE );
}
}
/* Create the decryption context */
if( isDevice )
status = cryptDeviceCreateContext( cryptDevice, sigCheckContext,
CRYPT_ALGO_DSA );
else
status = cryptCreateContext( sigCheckContext, CRYPT_UNUSED,
CRYPT_ALGO_DSA );
if( cryptStatusError( status ) )
{
free( dsaKey );
if( signContext != NULL )
{
cryptDestroyContext( *signContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
signContextLabel );
}
printf( "cryptCreateContext() failed with error code %d.\n", status );
return( FALSE );
}
if( !setLabel( *sigCheckContext, sigCheckContextLabel ) )
{
if( signContext != NULL )
{
cryptDestroyContext( *signContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
signContextLabel );
}
cryptDestroyContext( *sigCheckContext );
return( FALSE );
}
cryptInitComponents( dsaKey, CRYPT_KEYTYPE_PUBLIC );
cryptSetComponent( dsaKey->p, dlpTestKey.p, dlpTestKey.pLen );
cryptSetComponent( dsaKey->q, dlpTestKey.q, dlpTestKey.qLen );
cryptSetComponent( dsaKey->g, dlpTestKey.g, dlpTestKey.gLen );
cryptSetComponent( dsaKey->y, dlpTestKey.y, dlpTestKey.yLen );
status = cryptSetAttributeString( *sigCheckContext,
CRYPT_CTXINFO_KEY_COMPONENTS, dsaKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( dsaKey );
free( dsaKey );
if( cryptStatusError( status ) )
{
if( signContext != NULL )
{
cryptDestroyContext( *signContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
signContextLabel );
}
cryptDestroyContext( *sigCheckContext );
if( isDevice )
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME,
sigCheckContextLabel );
printf( "Key load failed with error code %d.\n", status );
return( FALSE );
}
return( TRUE );
}
BOOLEAN loadDSAContexts( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT *signContext,
CRYPT_CONTEXT *sigCheckContext )
{
return( loadDSAContextsEx( cryptDevice, signContext, sigCheckContext,
DSA_PRIVKEY_LABEL, DSA_PUBKEY_LABEL ) );
}
BOOLEAN loadElgamalContexts( CRYPT_CONTEXT *cryptContext,
CRYPT_CONTEXT *decryptContext )
{
CRYPT_PKCINFO_DLP *elgamalKey;
int status;
/* Allocate room for the public-key components */
if( ( elgamalKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
return( CRYPT_ERROR_MEMORY );
/* Create the encryption context */
if( cryptContext != NULL )
{
status = cryptCreateContext( cryptContext, CRYPT_UNUSED,
CRYPT_ALGO_ELGAMAL );
if( cryptStatusError( status ) )
{
free( elgamalKey );
printf( "cryptCreateContext() failed with error code %d.\n", status );
return( FALSE );
}
if( !setLabel( *cryptContext, ELGAMAL_PUBKEY_LABEL ) )
{
free( elgamalKey );
cryptDestroyContext( *cryptContext );
return( FALSE );
}
cryptInitComponents( elgamalKey, CRYPT_KEYTYPE_PUBLIC );
cryptSetComponent( elgamalKey->p, dlpTestKey.p, dlpTestKey.pLen );
cryptSetComponent( elgamalKey->g, dlpTestKey.g, dlpTestKey.gLen );
cryptSetComponent( elgamalKey->q, dlpTestKey.q, dlpTestKey.qLen );
cryptSetComponent( elgamalKey->y, dlpTestKey.y, dlpTestKey.yLen );
status = cryptSetAttributeString( *cryptContext,
CRYPT_CTXINFO_KEY_COMPONENTS, elgamalKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( elgamalKey );
if( cryptStatusError( status ) )
{
free( elgamalKey );
cryptDestroyContext( *cryptContext );
printf( "Key load failed with error code %d.\n", status );
return( FALSE );
}
if( decryptContext == NULL )
{
free( elgamalKey );
return( TRUE );
}
}
/* Create the decryption context */
status = cryptCreateContext( decryptContext, CRYPT_UNUSED,
CRYPT_ALGO_ELGAMAL );
if( cryptStatusError( status ) )
{
free( elgamalKey );
if( cryptContext != NULL )
cryptDestroyContext( *cryptContext );
printf( "cryptCreateContext() failed with error code %d.\n", status );
return( FALSE );
}
if( !setLabel( *decryptContext, ELGAMAL_PRIVKEY_LABEL ) )
{
free( elgamalKey );
if( cryptContext != NULL )
cryptDestroyContext( *cryptContext );
cryptDestroyContext( *decryptContext );
return( FALSE );
}
cryptInitComponents( elgamalKey, CRYPT_KEYTYPE_PRIVATE );
cryptSetComponent( elgamalKey->p, dlpTestKey.p, dlpTestKey.pLen );
cryptSetComponent( elgamalKey->g, dlpTestKey.g, dlpTestKey.gLen );
cryptSetComponent( elgamalKey->q, dlpTestKey.q, dlpTestKey.qLen );
cryptSetComponent( elgamalKey->y, dlpTestKey.y, dlpTestKey.yLen );
cryptSetComponent( elgamalKey->x, dlpTestKey.x, dlpTestKey.xLen );
status = cryptSetAttributeString( *decryptContext,
CRYPT_CTXINFO_KEY_COMPONENTS, elgamalKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( elgamalKey );
free( elgamalKey );
if( cryptStatusError( status ) )
{
cryptDestroyContext( *cryptContext );
cryptDestroyContext( *decryptContext );
printf( "Key load failed with error code %d.\n", status );
return( FALSE );
}
return( TRUE );
}
/* Load Diffie-Hellman encrytion contexts */
BOOLEAN loadDHContexts( CRYPT_CONTEXT *cryptContext1,
CRYPT_CONTEXT *cryptContext2, int keySize )
{
CRYPT_PKCINFO_DLP *dhKey;
int status;
/* Allocate room for the public-key components */
if( ( dhKey = ( CRYPT_PKCINFO_DLP * ) malloc( sizeof( CRYPT_PKCINFO_DLP ) ) ) == NULL )
return( CRYPT_ERROR_MEMORY );
/* Create the first encryption context */
status = cryptCreateContext( cryptContext1, CRYPT_UNUSED, CRYPT_ALGO_DH );
if( cryptStatusError( status ) )
{
free( dhKey );
printf( "cryptCreateContext() failed with error code %d.\n", status );
return( FALSE );
}
if( !setLabel( *cryptContext1, DH_KEY1_LABEL ) )
{
free( dhKey );
cryptDestroyContext( *cryptContext1 );
return( FALSE );
}
cryptInitComponents( dhKey, CRYPT_KEYTYPE_PUBLIC );
cryptSetComponent( dhKey->p, dlpTestKey.p, dlpTestKey.pLen );
cryptSetComponent( dhKey->q, dlpTestKey.q, dlpTestKey.qLen );
cryptSetComponent( dhKey->g, dlpTestKey.g, dlpTestKey.gLen );
status = cryptSetAttributeString( *cryptContext1,
CRYPT_CTXINFO_KEY_COMPONENTS, dhKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( dhKey );
if( cryptStatusError( status ) )
{
free( dhKey );
printf( "Key load failed with error code %d.\n", status );
return( FALSE );
}
if( cryptContext2 == NULL )
{
free( dhKey );
return( TRUE );
}
/* Create the second encryption context */
status = cryptCreateContext( cryptContext2, CRYPT_UNUSED, CRYPT_ALGO_DH );
if( cryptStatusError( status ) )
{
free( dhKey );
printf( "cryptCreateContext() failed with error code %d.\n", status );
return( FALSE );
}
if( !setLabel( *cryptContext2, DH_KEY2_LABEL ) )
{
free( dhKey );
if( cryptContext1 != NULL )
cryptDestroyContext( *cryptContext1 );
cryptDestroyContext( *cryptContext2 );
return( FALSE );
}
cryptInitComponents( dhKey, CRYPT_KEYTYPE_PUBLIC );
cryptSetComponent( dhKey->p, dlpTestKey.p, dlpTestKey.pLen );
cryptSetComponent( dhKey->q, dlpTestKey.q, dlpTestKey.qLen );
cryptSetComponent( dhKey->g, dlpTestKey.g, dlpTestKey.gLen );
status = cryptSetAttributeString( *cryptContext2,
CRYPT_CTXINFO_KEY_COMPONENTS, dhKey,
sizeof( CRYPT_PKCINFO_DLP ) );
cryptDestroyComponents( dhKey );
free( dhKey );
if( cryptStatusError( status ) )
{
printf( "Key load failed with error code %d.\n", status );
return( FALSE );
}
return( TRUE );
}
/* Destroy the encryption contexts */
void destroyContexts( const CRYPT_DEVICE cryptDevice,
CRYPT_CONTEXT cryptContext,
CRYPT_CONTEXT decryptContext )
{
int cryptAlgo, status;
cryptGetAttribute( cryptContext, CRYPT_CTXINFO_ALGO, &cryptAlgo );
status = cryptDestroyContext( cryptContext );
if( cryptStatusError( status ) )
printf( "cryptDestroyContext() failed with error code %d.\n", status );
status = cryptDestroyContext( decryptContext );
if( cryptStatusError( status ) )
printf( "cryptDestroyContext() failed with error code %d.\n", status );
if( cryptDevice == CRYPT_UNUSED )
return;
/* If the context is associated with a device then creating the object
will generally also create a persistent object in the device, after
performing the tests we have to explicitly delete the persistent
object */
if( cryptAlgo == CRYPT_ALGO_RSA )
{
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME, RSA_PUBKEY_LABEL );
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME, RSA_PRIVKEY_LABEL );
}
else
if( cryptAlgo == CRYPT_ALGO_DSA )
{
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME, DSA_PUBKEY_LABEL );
cryptDeleteKey( cryptDevice, CRYPT_KEYID_NAME, DSA_PRIVKEY_LABEL );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -