⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timing.c

📁 cryptlib是功能强大的安全工具集。允许开发人员快速在自己的软件中集成加密和认证服务。
💻 C
📖 第 1 页 / 共 3 页
字号:
									 CRYPT_ALGO_RSA );
	if( cryptStatusError( status ) )
		{
		printf( "crypt%sCreateContext() failed with error code %d.\n",
				isDevice ? "Device" : "", status );
		return( FALSE );
		}
	cryptSetAttributeString( *decryptContext, CRYPT_CTXINFO_LABEL,
							 "RSA private key", strlen( "RSA private key" ) );
	cryptInitComponents( rsaKey, CRYPT_KEYTYPE_PRIVATE );
	cryptSetComponent( rsaKey->n, rsaKeyTemplate->n, rsaKeyTemplate->nLen );
	cryptSetComponent( rsaKey->e, rsaKeyTemplate->e, rsaKeyTemplate->eLen );
	cryptSetComponent( rsaKey->d, rsaKeyTemplate->d, rsaKeyTemplate->dLen );
	cryptSetComponent( rsaKey->p, rsaKeyTemplate->p, rsaKeyTemplate->pLen );
	cryptSetComponent( rsaKey->q, rsaKeyTemplate->q, rsaKeyTemplate->qLen );
	cryptSetComponent( rsaKey->u, rsaKeyTemplate->u, rsaKeyTemplate->uLen );
	cryptSetComponent( rsaKey->e1, rsaKeyTemplate->e1, rsaKeyTemplate->e1Len );
	cryptSetComponent( rsaKey->e2, rsaKeyTemplate->e2, rsaKeyTemplate->e2Len );
	status = cryptSetAttributeString( *decryptContext,
									  CRYPT_CTXINFO_KEY_COMPONENTS, rsaKey,
									  sizeof( CRYPT_PKCINFO_RSA ) );
	cryptDestroyComponents( rsaKey );
	free( rsaKey );
	if( cryptStatusError( status ) )
		{
		printf( "Key load failed with error code %d.\n", status );
		return( FALSE );
		}

	return( TRUE );
	}

typedef struct {
	const int pLen; const BYTE p[ 128 ];
	const int qLen; const BYTE q[ 20 ];
	const int gLen; const BYTE g[ 128 ];
	const int xLen; const BYTE x[ 20 ];
	const int yLen; const BYTE y[ 128 ];
	} DLP_KEY;

static const DLP_KEY dlp1024Key = {
	/* p */
	1024,
	{ 0x03, 0x16, 0x6D, 0x9C, 0x64, 0x0C, 0x65, 0x25,
	  0xDB, 0x73, 0xD0, 0x7D, 0x8B, 0xB2, 0x48, 0xD3,
	  0x70, 0x53, 0xE1, 0x68, 0x0C, 0x2E, 0x98, 0xF7,
	  0xD9, 0xCA, 0x72, 0x10, 0x03, 0x7C, 0x8D, 0x30,
	  0x02, 0xFB, 0xF2, 0xA1, 0x57, 0x61, 0x0B, 0xA0,
	  0x4A, 0x6D, 0x5B, 0x99, 0xCC, 0xB8, 0xD9, 0x0F,
	  0x9F, 0xD4, 0x3C, 0x67, 0x97, 0x35, 0xDE, 0x8D,
	  0x48, 0xE4, 0x7B, 0x7C, 0xEB, 0x69, 0xA4, 0x9F,
	  0x5C, 0x67, 0xA3, 0x6B, 0x27, 0x49, 0xF9, 0x98,
	  0x0D, 0x3B, 0x85, 0xBC, 0xEC, 0x33, 0x39, 0xB1,
	  0x86, 0xFF, 0xAF, 0x98, 0x34, 0x88, 0x30, 0xC3,
	  0x58, 0x62, 0x65, 0xE0, 0xE4, 0xDF, 0xC7, 0xD3,
	  0x2E, 0x36, 0x4F, 0x21, 0x7C, 0x35, 0x86, 0x59,
	  0x02, 0x3C, 0x0D, 0x94, 0x86, 0xC7, 0xC6, 0x59,
	  0x9C, 0x02, 0x66, 0x55, 0x68, 0x1A, 0x77, 0xD2,
	  0x00, 0x6B, 0x61, 0x41, 0x52, 0x26, 0x18, 0x6B },

	/* q */
	160,
	{ 0xE8, 0x5A, 0x93, 0xE9, 0x4D, 0x15, 0xB5, 0x96,
	  0x7E, 0xE3, 0x2A, 0x47, 0x8E, 0xD4, 0xAC, 0x72,
	  0x3D, 0x82, 0xB6, 0x49 },

	/* g */
	1024,
	{ 0x00, 0xA0, 0xED, 0xFF, 0x76, 0x7C, 0x99, 0xA3,
	  0x43, 0x81, 0x12, 0x78, 0x0F, 0x3D, 0x60, 0xCA,
	  0xA7, 0x5D, 0xA4, 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61, 0xE4, 0x8D, 0x13, 0xE1, 0x84, 0xC9, 0x43,
	  0x62, 0x26, 0x8E, 0xD7, 0x91, 0xDC, 0xBD, 0xAA,
	  0x21, 0x03, 0xA7, 0x96, 0xF2, 0x8F, 0x2F, 0xBF,
	  0x22, 0x67, 0xAB, 0x54, 0xB4, 0x8E, 0x76, 0xDC,
	  0x64, 0xCF, 0x6D, 0x21, 0xDD, 0x9B, 0x41, 0x53,
	  0x11, 0x48, 0x93, 0x12, 0x75, 0x75, 0x1B, 0x1F,
	  0xDA, 0x2B, 0x8E, 0x5C, 0x75, 0xDC, 0x5C, 0x77,
	  0xE7, 0xBE, 0x25, 0x6B, 0xB9, 0x9B, 0x5F, 0x63,
	  0x11, 0xAF, 0x54, 0xEC, 0xFE, 0x08, 0xDE, 0x1E,
	  0x4D, 0x38, 0xE3, 0x77, 0x26, 0x2C, 0xDB, 0xDB,
	  0xAB, 0xC8, 0x60, 0x5F, 0x88, 0x5C, 0x98, 0xFE }
	};

static BOOLEAN loadDSAKey( const CRYPT_DEVICE cryptDevice,
						   CRYPT_CONTEXT *signContext,
						   CRYPT_CONTEXT *sigCheckContext )
	{
	CRYPT_PKCINFO_DLP *dsaKey;
	const DLP_KEY *dlpKeyTemplate = &dlp1024Key;
	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 ) )
			{
			printf( "crypt%sCreateContext() failed with error code %d.\n",
					isDevice ? "Device" : "", status );
			return( FALSE );
			}
		cryptSetAttributeString( *signContext, CRYPT_CTXINFO_LABEL,
								 "DSA private key", strlen( "DSA private key" ) );
		cryptInitComponents( dsaKey, CRYPT_KEYTYPE_PRIVATE );
		cryptSetComponent( dsaKey->p, dlpKeyTemplate->p, dlpKeyTemplate->pLen );
		cryptSetComponent( dsaKey->q, dlpKeyTemplate->q, dlpKeyTemplate->qLen );
		cryptSetComponent( dsaKey->g, dlpKeyTemplate->g, dlpKeyTemplate->gLen );
		cryptSetComponent( dsaKey->x, dlpKeyTemplate->x, dlpKeyTemplate->xLen );
		cryptSetComponent( dsaKey->y, dlpKeyTemplate->y, dlpKeyTemplate->yLen );
		status = cryptSetAttributeString( *signContext,
									CRYPT_CTXINFO_KEY_COMPONENTS, dsaKey,
									sizeof( CRYPT_PKCINFO_DLP ) );
		cryptDestroyComponents( dsaKey );
		if( cryptStatusError( status ) )
			{
			printf( "Key load failed with error code %d.\n", status );
			return( FALSE );
			}
		if( sigCheckContext == NULL )
			{
			/* We're only using a public-key context, return */
			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 ) )
		{
		printf( "crypt%sCreateContext() failed with error code %d.\n",
				isDevice ? "Device" : "", status );
		return( FALSE );
		}
	cryptSetAttributeString( *sigCheckContext, CRYPT_CTXINFO_LABEL,
							 "DSA public key", strlen( "DSA public key" ) );
	cryptInitComponents( dsaKey, CRYPT_KEYTYPE_PUBLIC );
	cryptSetComponent( dsaKey->p, dlpKeyTemplate->p, dlpKeyTemplate->pLen );
	cryptSetComponent( dsaKey->q, dlpKeyTemplate->q, dlpKeyTemplate->qLen );
	cryptSetComponent( dsaKey->g, dlpKeyTemplate->g, dlpKeyTemplate->gLen );
	cryptSetComponent( dsaKey->y, dlpKeyTemplate->y, dlpKeyTemplate->yLen );
	status = cryptSetAttributeString( *sigCheckContext,
									  CRYPT_CTXINFO_KEY_COMPONENTS, dsaKey,
									  sizeof( CRYPT_PKCINFO_DLP ) );
	cryptDestroyComponents( dsaKey );
	free( dsaKey );
	if( cryptStatusError( status ) )
		{
		printf( "Key load failed with error code %d.\n", status );
		return( FALSE );
		}

	return( TRUE );
	}

/* Time the RSA operation speed */

static int encRSATest( const CRYPT_CONTEXT cryptContext,
					   const CRYPT_CONTEXT decryptContext,
					   HIRES_TIME times[] )
	{
	BYTE buffer[ CRYPT_MAX_PKCSIZE ];
	HIRES_TIME timeVal;
	int status;

	memset( buffer, '*', 128 );
	buffer[ 0 ] = 1;
	timeVal = timeDiff( 0 );
	status = cryptEncrypt( cryptContext, buffer, 128 );
	times[ 0 ] = timeDiff( timeVal );
	if( cryptStatusError( status ) )
		{
		printf( "Couldn't encrypt data, status = %d.\n", status );
		return( FALSE );
		}
	timeVal = timeDiff( 0 );
	status = cryptDecrypt( decryptContext, buffer, 128 );
	times[ 1 ] = timeDiff( timeVal );
	if( cryptStatusError( status ) )
		{
		printf( "Couldn't decrypt data, status = %d.\n", status );
		return( FALSE );
		}
	return( TRUE );
	}

static BOOLEAN testRSA( void )
	{
	CRYPT_CONTEXT cryptContext, decryptContext;
	HIRES_TIME times[ NO_TESTS + 1 ][ 8 ];
	int i;

	memset( times, 0, sizeof( times ) );

	/* Load the RSA keys */
	if( !loadRSAKey( CRYPT_UNUSED, &cryptContext, &decryptContext ) )
		return( FALSE );

	/* Encrypt and decrypt a test buffer */
	printf( "RSA 1024-bit " );
	for( i = 0; i < NO_TESTS + 1; i++ )
		{
		int status;

		status = encRSATest( cryptContext, decryptContext, times[ i ] );
		if( !status )
			return( FALSE );
		}
	printTimes( times, 2 );

	/* Clean up */
	cryptDestroyContext( cryptContext );
	cryptDestroyContext( decryptContext );

	return( TRUE );
	}

/* Time the DSA operation speed */

static BOOLEAN testDSA( void )
	{
	CRYPT_CONTEXT signContext, sigCheckContext;
	HIRES_TIME times[ NO_TESTS + 1 ][ 8 ];
	int i;

	memset( times, 0, sizeof( times ) );

	/* Load the DSA keys */
	if( !loadDSAKey( CRYPT_UNUSED, &signContext, &sigCheckContext ) )
		return( FALSE );

	/* Sign and verify a test buffer */
	printf( "DSA 1024-bit " );
	for( i = 0; i < NO_TESTS + 1; i++ )
		{
		DLP_PARAMS dlpParams;
		HIRES_TIME timeVal;
		BYTE buffer[ 128 ];
		int sigSize, status;

		/* Perform the test sign/sig.check */
		setDLPParams( &dlpParams, "********************", 20, buffer, 128 );
		timeVal = timeDiff( 0 );
#if 0
		status = krnlSendMessage( signContext, IMESSAGE_CTX_SIGN,
								  &dlpParams, sizeof( DLP_PARAMS ) );
#else
		status = cryptDeviceQueryCapability( signContext, 1002,
									( CRYPT_QUERY_INFO * ) &dlpParams );
#endif /* 0 */
		times[ i ][ 0 ] = timeDiff( timeVal );
		if( cryptStatusError( status ) )
			{
			printf( "Couldn't create DSA signature, status = %d.\n",
					status );
			return( FALSE );
			}
		sigSize = dlpParams.outLen;

		timeVal = timeDiff( 0 );
		setDLPParams( &dlpParams, "********************", 20, NULL, 0 );
		dlpParams.inParam2 = buffer;
		dlpParams.inLen2 = sigSize;
#if 0
		status = krnlSendMessage( sigCheckContext, IMESSAGE_CTX_SIGCHECK,
								  &dlpParams, sizeof( DLP_PARAMS ) );
#else
		status = cryptDeviceQueryCapability( sigCheckContext, 1003,
									( CRYPT_QUERY_INFO * ) &dlpParams );
#endif /* 0 */
		times[ i ][ 1 ] = timeDiff( timeVal );
		if( cryptStatusError( status ) )
			{
			printf( "Couldn't verify DSA signature, status = %d.\n",
					status );
			return( FALSE );
			}
		}
	printTimes( times, 2 );

	/* Clean up */
	cryptDestroyContext( signContext );
	cryptDestroyContext( sigCheckContext );

	return( TRUE );
	}

/* Time the DH operation speed */

static BOOLEAN testDH( void )
	{
	CRYPT_CONTEXT cryptContext;
	HIRES_TIME times[ NO_TESTS + 1 ][ 8 ];
	int i;

	memset( times, 0, sizeof( times ) );

	printf( "DH  1024-bit " );
	for( i = 0; i < NO_TESTS + 1; i++ )
		{
		KEYAGREE_PARAMS keyAgreeParams;
		HIRES_TIME timeVal;
		int status;

		/* Load the DH key */
		if( !loadDHKey( &cryptContext ) )
			return( FALSE );

		/* Perform the DH key agreement */
		memset( &keyAgreeParams, 0, sizeof( KEYAGREE_PARAMS ) );
		memset( keyAgreeParams.publicValue, '*', 128 );
		keyAgreeParams.publicValueLen = 128;
		timeVal = timeDiff( 0 );
#if 0
		status = krnlSendMessage( cryptContext, IMESSAGE_CTX_DECRYPT,
								  &keyAgreeParams, sizeof( KEYAGREE_PARAMS ) );
#else
		status = cryptDeviceQueryCapability( cryptContext, 1001,
									( CRYPT_QUERY_INFO * ) &keyAgreeParams );
#endif /* 0 */
		times[ i ][ 0 ] = timeDiff( timeVal );
		if( cryptStatusError( status ) )
			{
			printf( "Couldn't perform DH key agreement, status = %d.\n",
					status );
			return( FALSE );
			}

		/* Clean up */
		cryptDestroyContext( cryptContext );
		}
	printTimes( times, 1 );

	return( TRUE );
	}

/****************************************************************************
*																			*
*							Standalone Runtime Support						*
*																			*
****************************************************************************/

/* Standalone main when we're not called from testlib.c */

int main( int argc, char **argv )
	{
	CRYPT_CONTEXT cryptContext;
#ifdef __WINDOWS__
	LARGE_INTEGER performanceCount;
#endif /* __WINDOWS__ */
	int status;

	/* Get rid of compiler warnings */
	if( argc || argv );

	/* Initialise cryptlib */
	status = cryptInit();
	if( cryptStatusError( status ) )
		{
		printf( "cryptInit() failed with error code %d.\n", status );
		exit( EXIT_FAILURE );
		}

	/* Try and bypass the randomness-handling by adding some junk (this only
	   works in the Windows debug build), then ensure that the randomness-
	   polling has completed by performing a blocking operation that
	   requires randomness */
#ifndef __WINDOWS__
	puts( "Forcing RNG reseed, this may take a few seconds..." );
#endif /* __WINDOWS__ */
	cryptAddRandom( "xyzzy", 5 );
	cryptCreateContext( &cryptContext, CRYPT_UNUSED, CRYPT_ALGO_DES );
	cryptGenerateKey( cryptContext );
	cryptDestroyContext( cryptContext );

#ifdef __WINDOWS__
	QueryPerformanceFrequency( &performanceCount );
	printf( "Clock ticks %d times per second", performanceCount.LowPart );
#else
	printf( "Clock ticks ~1000000 times per second" );
#endif /* __WINDOWS__ */
	puts( ", result rows are +0, +1, +4," );
	printf( "  modes CBC or ECB+CBC, %d tests per result.\n", NO_TESTS );

	testDH();
	testRSA();
	testDSA();
	performanceTests( CRYPT_UNUSED );

	/* Clean up */
	cryptEnd();
	return( EXIT_SUCCESS );
	}
#endif /* __WINDOWS__ || __UNIX__ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -