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

📄 hw_zencod.c

📁 开源的ssl算法openssl,版本0.9.8H
💻 C
📖 第 1 页 / 共 4 页
字号:
/* The one for DES ... *//* Try something static ... */typedef struct{	unsigned char des_key [ 24 ] ;	unsigned char des_iv [ 8 ] ;} ZEN_DES_KEY ;static const EVP_CIPHER engine_des_cbc =	{	NID_des_cbc,	8, 8, 8,	0 | EVP_CIPH_CBC_MODE,	engine_des_init_key,	engine_des_cbc_cipher,	engine_cipher_cleanup,	sizeof(ZEN_DES_KEY),	EVP_CIPHER_set_asn1_iv,	EVP_CIPHER_get_asn1_iv,	NULL,	NULL	};/* The one for 3DES ... *//* Try something static ... */typedef struct{	unsigned char des3_key [ 24 ] ;	unsigned char des3_iv [ 8 ] ;} ZEN_3DES_KEY ;#define des_data(ctx)				 ( (DES_EDE_KEY *) ( ctx )->cipher_data )static const EVP_CIPHER engine_des_ede3_cbc =	{	NID_des_ede3_cbc,	8, 8, 8,	0 | EVP_CIPH_CBC_MODE,	engine_des_ede3_init_key,	engine_des_ede3_cbc_cipher,	engine_cipher_cleanup,	sizeof(ZEN_3DES_KEY),	EVP_CIPHER_set_asn1_iv,	EVP_CIPHER_get_asn1_iv,	NULL,	NULL	};/* General function cloned on hw_openbsd_dev_crypto one ... */static int engine_digests ( ENGINE *e, const EVP_MD **digest, const int **nids, int nid ){#ifdef DEBUG_ZENCOD_MD	fprintf ( stderr, "\t=>Function : static int engine_digests () called !\n" ) ;#endif	if ( !digest ) {		/* We are returning a list of supported nids */		*nids = engine_digest_nids ;		return engine_digest_nids_num ;	}	/* We are being asked for a specific digest */	if ( nid == NID_md5 ) {		*digest = &engine_md5_md ;	}	else if ( nid == NID_sha1 ) {		*digest = &engine_sha1_md ;	}	else {		*digest = NULL ;		return 0 ;	}	return 1 ;}/* SHA stuff Functions */static int engine_sha1_init ( EVP_MD_CTX *ctx ){	int to_return = 0 ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_sha1_init ( (ZEN_MD_DATA *) ctx->md_data ) ;	to_return = !to_return ;	return to_return ;}static int engine_sha1_update ( EVP_MD_CTX *ctx, const void *data, unsigned long count ){	zen_nb_t input ;	int to_return = 0 ;	/* Convert parameters ... */	input.len = count ;	input.data = (unsigned char *) data ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_sha1_update ( (ZEN_MD_DATA *) ctx->md_data, (const zen_nb_t *) &input ) ;	to_return = !to_return ;	return to_return ;}static int engine_sha1_final ( EVP_MD_CTX *ctx, unsigned char *md ){	zen_nb_t output ;	int to_return = 0 ;	/* Convert parameters ... */	output.len = SHA_DIGEST_LENGTH ;	output.data = md ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_sha1_do_final ( (ZEN_MD_DATA *) ctx->md_data, (zen_nb_t *) &output ) ;	to_return = !to_return ;	return to_return ;}/* MD5 stuff Functions */static int engine_md5_init ( EVP_MD_CTX *ctx ){	int to_return = 0 ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_md5_init ( (ZEN_MD_DATA *) ctx->md_data ) ;	to_return = !to_return ;	return to_return ;}static int engine_md5_update ( EVP_MD_CTX *ctx, const void *data, unsigned long count ){	zen_nb_t input ;	int to_return = 0 ;	/* Convert parameters ... */	input.len = count ;	input.data = (unsigned char *) data ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_md5_update ( (ZEN_MD_DATA *) ctx->md_data, (const zen_nb_t *) &input ) ;	to_return = !to_return ;	return to_return ;}static int engine_md5_final ( EVP_MD_CTX *ctx, unsigned char *md ){	zen_nb_t output ;	int to_return = 0 ;	/* Convert parameters ... */	output.len = MD5_DIGEST_LENGTH ;	output.data = md ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_md5_do_final ( (ZEN_MD_DATA *) ctx->md_data, (zen_nb_t *) &output ) ;	to_return = !to_return ;	return to_return ;}static int engine_md_cleanup ( EVP_MD_CTX *ctx ){	ZEN_MD_DATA *zen_md_data = (ZEN_MD_DATA *) ctx->md_data ;	if ( zen_md_data->HashBuffer != NULL ) {		OPENSSL_free ( zen_md_data->HashBuffer ) ;		zen_md_data->HashBufferSize = 0 ;		ctx->md_data = NULL ;	}	return 1 ;}static int engine_md_copy ( EVP_MD_CTX *to, const EVP_MD_CTX *from ){	const ZEN_MD_DATA *from_md = (ZEN_MD_DATA *) from->md_data ;	ZEN_MD_DATA *to_md = (ZEN_MD_DATA *) to->md_data ;	to_md->HashBuffer = OPENSSL_malloc ( from_md->HashBufferSize ) ;	memcpy ( to_md->HashBuffer, from_md->HashBuffer, from_md->HashBufferSize ) ;	return 1;}/* General function cloned on hw_openbsd_dev_crypto one ... */static int engine_ciphers ( ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid ){	if ( !cipher ) {		/* We are returning a list of supported nids */		*nids = engine_cipher_nids ;		return engine_cipher_nids_num ;	}	/* We are being asked for a specific cipher */	if ( nid == NID_rc4 ) {		*cipher = &engine_rc4 ;	}	else if ( nid == NID_rc4_40 ) {		*cipher = &engine_rc4_40 ;	}	else if ( nid == NID_des_cbc ) {		*cipher = &engine_des_cbc ;	}	else if ( nid == NID_des_ede3_cbc ) {		*cipher = &engine_des_ede3_cbc ;	}	else {		*cipher = NULL ;		return 0 ;	}	return 1 ;}static int engine_rc4_init_key ( EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc ){	int to_return = 0 ;	int i = 0 ;	int nb = 0 ;	NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL ;	tmp_rc4_key = (NEW_ZEN_RC4_KEY *) ( ctx->cipher_data ) ;	tmp_rc4_key->first = 0 ;	tmp_rc4_key->len = ctx->key_len ;	tmp_rc4_key->rc4_state [ 0 ] = 0x00 ;	tmp_rc4_key->rc4_state [ 2 ] = 0x00 ;	nb = 256 / ctx->key_len ;	for ( i = 0; i < nb ; i++ ) {		memcpy ( &( tmp_rc4_key->rc4_state [ 4 + i*ctx->key_len ] ), key, ctx->key_len ) ;	}	to_return = 1 ;	return to_return ;}static int engine_rc4_cipher ( EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int in_len ){	zen_nb_t output, input ;	zen_nb_t rc4key ;	int to_return = 0 ;	NEW_ZEN_RC4_KEY *tmp_rc4_key = NULL ;	/* Convert parameters ... */	input.len = in_len ;	input.data = (unsigned char *) in ;	output.len = in_len ;	output.data = (unsigned char *) out ;	tmp_rc4_key = ( (NEW_ZEN_RC4_KEY *) ( ctx->cipher_data ) ) ;	rc4key.len = 260 ;	rc4key.data = &( tmp_rc4_key->rc4_state [ 0 ] ) ;	/* Test with zenbridge library ... */	to_return = ptr_zencod_rc4_cipher ( &output, &input, (const zen_nb_t *) &rc4key, &( tmp_rc4_key->rc4_state [0] ), &( tmp_rc4_key->rc4_state [3] ), !tmp_rc4_key->first ) ;	to_return = !to_return ;	/* Update encryption state ... */	tmp_rc4_key->first = 1 ;	tmp_rc4_key = NULL ;	return to_return ;}static int engine_des_init_key ( EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc ){	ZEN_DES_KEY *tmp_des_key = NULL ;	int to_return = 0 ;	tmp_des_key = (ZEN_DES_KEY *) ( ctx->cipher_data ) ;	memcpy ( &( tmp_des_key->des_key [ 0 ] ), key, 8 ) ;	memcpy ( &( tmp_des_key->des_key [ 8 ] ), key, 8 ) ;	memcpy ( &( tmp_des_key->des_key [ 16 ] ), key, 8 ) ;	memcpy ( &( tmp_des_key->des_iv [ 0 ] ), iv, 8 ) ;	to_return = 1 ;	return to_return ;}static int engine_des_cbc_cipher ( EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl ){	zen_nb_t output, input ;	zen_nb_t deskey_1, deskey_2, deskey_3, iv ;	int to_return = 0 ;	/* Convert parameters ... */	input.len = inl ;	input.data = (unsigned char *) in ;	output.len = inl ;	output.data = out ;	/* Set key parameters ... */	deskey_1.len = 8 ;	deskey_2.len = 8 ;	deskey_3.len = 8 ;	deskey_1.data = (unsigned char *) ( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_key ;	deskey_2.data =  (unsigned char *) &( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_key [ 8 ] ;	deskey_3.data =  (unsigned char *) &( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_key [ 16 ] ;	/* Key correct iv ... */	memcpy ( ( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_iv, ctx->iv, 8 ) ;	iv.len = 8 ;	iv.data = (unsigned char *) ( (ZEN_DES_KEY *) ( ctx->cipher_data ) )->des_iv ;	if ( ctx->encrypt == 0 ) {		memcpy ( ctx->iv, &( input.data [ input.len - 8 ] ), 8 ) ;	}	/* Test with zenbridge library ... */	to_return = ptr_zencod_xdes_cipher ( &output, &input,			(zen_nb_t *) &deskey_1, (zen_nb_t *) &deskey_2, (zen_nb_t *) &deskey_3, &iv, ctx->encrypt ) ;	to_return = !to_return ;	/* But we need to set up the rigth iv ...	 * Test ENCRYPT or DECRYPT mode to set iv ... */	if ( ctx->encrypt == 1 ) {		memcpy ( ctx->iv, &( output.data [ output.len - 8 ] ), 8 ) ;	}	return to_return ;}static int engine_des_ede3_init_key ( EVP_CIPHER_CTX *ctx, const unsigned char *key, const unsigned char *iv, int enc ){	ZEN_3DES_KEY *tmp_3des_key = NULL ;	int to_return = 0 ;	tmp_3des_key = (ZEN_3DES_KEY *) ( ctx->cipher_data ) ;	memcpy ( &( tmp_3des_key->des3_key [ 0 ] ), key, 24 ) ;	memcpy ( &( tmp_3des_key->des3_iv [ 0 ] ), iv, 8 ) ;	to_return = 1;	return to_return ;}static int engine_des_ede3_cbc_cipher ( EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,	unsigned int in_len ){	zen_nb_t output, input ;	zen_nb_t deskey_1, deskey_2, deskey_3, iv ;	int to_return = 0 ;	/* Convert parameters ... */	input.len = in_len ;	input.data = (unsigned char *) in ;	output.len = in_len ;	output.data = out ;	/* Set key ... */	deskey_1.len = 8 ;	deskey_2.len = 8 ;	deskey_3.len = 8 ;	deskey_1.data =  (unsigned char *) ( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_key ;	deskey_2.data =  (unsigned char *) &( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_key [ 8 ] ;	deskey_3.data =  (unsigned char *) &( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_key [ 16 ] ;	/* Key correct iv ... */	memcpy ( ( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_iv, ctx->iv, 8 ) ;	iv.len = 8 ;	iv.data = (unsigned char *) ( (ZEN_3DES_KEY *) ( ctx->cipher_data ) )->des3_iv ;	if ( ctx->encrypt == 0 ) {		memcpy ( ctx->iv, &( input.data [ input.len - 8 ] ), 8 ) ;	}	/* Test with zenbridge library ... */	to_return = ptr_zencod_xdes_cipher ( &output, &input,			(zen_nb_t *) &deskey_1, (zen_nb_t *) &deskey_2, (zen_nb_t *) &deskey_3, &iv, ctx->encrypt ) ;	to_return = !to_return ;	if ( ctx->encrypt == 1 ) {		memcpy ( ctx->iv, &( output.data [ output.len - 8 ] ), 8 ) ;	}	return to_return ;}static int engine_cipher_cleanup ( EVP_CIPHER_CTX *ctx ){	/* Set the key pointer ... */	if ( ctx->cipher->nid == NID_rc4 || ctx->cipher->nid == NID_rc4_40 ) {	}	else if ( ctx->cipher->nid == NID_des_cbc ) {	}	else if ( ctx->cipher->nid == NID_des_ede3_cbc ) {	}	return 1 ;}#endif /* !OPENSSL_NO_HW_ZENCOD */#endif /* !OPENSSL_NO_HW */

⌨️ 快捷键说明

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