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

📄 eng_padlock.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 2 页
字号:
		xor	eax,ecx		bt	eax,21		jnc	noluck		mov	eax,0		cpuid		xor	eax,eax		cmp	ebx,'tneC'		jne	noluck		cmp	edx,'Hrua'		jne	noluck		cmp	ecx,'slua'		jne	noluck		mov	eax,0xC0000000		cpuid		mov	edx,eax		xor	eax,eax		cmp	edx,0xC0000001		jb	noluck		mov	eax,0xC0000001		cpuid		xor	eax,eax		bt	edx,6		jnc	skip_a		bt	edx,7		jnc	skip_a		mov	padlock_use_ace,1		inc	eax	skip_a:	bt	edx,2		jnc	skip_r		bt	edx,3		jnc	skip_r		mov	padlock_use_rng,1		inc	eax	skip_r:	noluck:		}}static void __fastcallpadlock_bswapl(void *key){	_asm	{		pushfd		cld		mov	esi,ecx		mov	edi,ecx		mov	ecx,60	up:	lodsd		bswap	eax		stosd		loop	up		popfd		}}/* MS actually specifies status of Direction Flag and compiler even * manages to compile following as 'rep movsd' all by itself... */#define padlock_memcpy(o,i,n) ((unsigned char *)memcpy((o),(i),(n)&~3U))#endif/* ===== AES encryption/decryption ===== */#ifndef OPENSSL_NO_AES#if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)#define NID_aes_128_cfb	NID_aes_128_cfb128#endif#if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)#define NID_aes_128_ofb	NID_aes_128_ofb128#endif#if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)#define NID_aes_192_cfb	NID_aes_192_cfb128#endif#if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)#define NID_aes_192_ofb	NID_aes_192_ofb128#endif#if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)#define NID_aes_256_cfb	NID_aes_256_cfb128#endif#if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)#define NID_aes_256_ofb	NID_aes_256_ofb128#endif/* List of supported ciphers. */static int padlock_cipher_nids[] = {	NID_aes_128_ecb,	NID_aes_128_cbc,	NID_aes_128_cfb,	NID_aes_128_ofb,	NID_aes_192_ecb,	NID_aes_192_cbc,#if 0	NID_aes_192_cfb,	/* FIXME: AES192/256 CFB/OFB don't work. */	NID_aes_192_ofb,#endif	NID_aes_256_ecb,	NID_aes_256_cbc,#if 0	NID_aes_256_cfb,	NID_aes_256_ofb,#endif};static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids)/				      sizeof(padlock_cipher_nids[0]));/* Function prototypes ... */static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,				const unsigned char *iv, int enc);static int padlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,			      const unsigned char *in, size_t nbytes);#define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) +		\	( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F )	)#define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\	NEAREST_ALIGNED(ctx->cipher_data))/* Declaring so many ciphers by hand would be a pain.   Instead introduce a bit of preprocessor magic :-) */#define	DECLARE_AES_EVP(ksize,lmode,umode)	\static const EVP_CIPHER padlock_aes_##ksize##_##lmode = {	\	NID_aes_##ksize##_##lmode,		\	AES_BLOCK_SIZE,			\	AES_KEY_SIZE_##ksize,		\	AES_BLOCK_SIZE,			\	0 | EVP_CIPH_##umode##_MODE,	\	padlock_aes_init_key,		\	padlock_aes_cipher,		\	NULL,				\	sizeof(struct padlock_cipher_data) + 16,	\	EVP_CIPHER_set_asn1_iv,		\	EVP_CIPHER_get_asn1_iv,		\	NULL,				\	NULL				\}DECLARE_AES_EVP(128,ecb,ECB);DECLARE_AES_EVP(128,cbc,CBC);DECLARE_AES_EVP(128,cfb,CFB);DECLARE_AES_EVP(128,ofb,OFB);DECLARE_AES_EVP(192,ecb,ECB);DECLARE_AES_EVP(192,cbc,CBC);DECLARE_AES_EVP(192,cfb,CFB);DECLARE_AES_EVP(192,ofb,OFB);DECLARE_AES_EVP(256,ecb,ECB);DECLARE_AES_EVP(256,cbc,CBC);DECLARE_AES_EVP(256,cfb,CFB);DECLARE_AES_EVP(256,ofb,OFB);static intpadlock_ciphers (ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid){	/* No specific cipher => return a list of supported nids ... */	if (!cipher) {		*nids = padlock_cipher_nids;		return padlock_cipher_nids_num;	}	/* ... or the requested "cipher" otherwise */	switch (nid) {	  case NID_aes_128_ecb:	    *cipher = &padlock_aes_128_ecb;	    break;	  case NID_aes_128_cbc:	    *cipher = &padlock_aes_128_cbc;	    break;	  case NID_aes_128_cfb:	    *cipher = &padlock_aes_128_cfb;	    break;	  case NID_aes_128_ofb:	    *cipher = &padlock_aes_128_ofb;	    break;	  case NID_aes_192_ecb:	    *cipher = &padlock_aes_192_ecb;	    break;	  case NID_aes_192_cbc:	    *cipher = &padlock_aes_192_cbc;	    break;	  case NID_aes_192_cfb:	    *cipher = &padlock_aes_192_cfb;	    break;	  case NID_aes_192_ofb:	    *cipher = &padlock_aes_192_ofb;	    break;	  case NID_aes_256_ecb:	    *cipher = &padlock_aes_256_ecb;	    break;	  case NID_aes_256_cbc:	    *cipher = &padlock_aes_256_cbc;	    break;	  case NID_aes_256_cfb:	    *cipher = &padlock_aes_256_cfb;	    break;	  case NID_aes_256_ofb:	    *cipher = &padlock_aes_256_ofb;	    break;	  default:	    /* Sorry, we don't support this NID */	    *cipher = NULL;	    return 0;	}	return 1;}/* Prepare the encryption key for PadLock usage */static intpadlock_aes_init_key (EVP_CIPHER_CTX *ctx, const unsigned char *key,		      const unsigned char *iv, int enc){	struct padlock_cipher_data *cdata;	int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;	if (key==NULL) return 0;	/* ERROR */	cdata = ALIGNED_CIPHER_DATA(ctx);	memset(cdata, 0, sizeof(struct padlock_cipher_data));	/* Prepare Control word. */	cdata->cword.b.encdec = (ctx->encrypt == 0);	cdata->cword.b.rounds = 10 + (key_len - 128) / 32;	cdata->cword.b.ksize = (key_len - 128) / 64;	switch(key_len) {		case 128:			/* PadLock can generate an extended key for			   AES128 in hardware */			memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);			cdata->cword.b.keygen = 0;			break;		case 192:		case 256:			/* Generate an extended AES key in software.			   Needed for AES192/AES256 */			/* Well, the above applies to Stepping 8 CPUs			   and is listed as hardware errata. They most			   likely will fix it at some point and then			   a check for stepping would be due here. */			if (enc)				AES_set_encrypt_key(key, key_len, &cdata->ks);			else				AES_set_decrypt_key(key, key_len, &cdata->ks);#ifndef AES_ASM			/* OpenSSL C functions use byte-swapped extended key. */			padlock_bswapl(&cdata->ks);#endif			cdata->cword.b.keygen = 1;			break;		default:			/* ERROR */			return 0;	}	/*	 * This is done to cover for cases when user reuses the	 * context for new key. The catch is that if we don't do	 * this, padlock_eas_cipher might proceed with old key...	 */	padlock_reload_key ();	return 1;}/*  * Simplified version of padlock_aes_cipher() used when * 1) both input and output buffers are at aligned addresses. * or when * 2) running on a newer CPU that doesn't require aligned buffers. */static intpadlock_aes_cipher_omnivorous(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,		const unsigned char *in_arg, size_t nbytes){	struct padlock_cipher_data *cdata;	void  *iv;	cdata = ALIGNED_CIPHER_DATA(ctx);	padlock_verify_context(cdata);	switch (EVP_CIPHER_CTX_mode(ctx)) {	case EVP_CIPH_ECB_MODE:		padlock_xcrypt_ecb(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);		break;	case EVP_CIPH_CBC_MODE:		memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);		iv = padlock_xcrypt_cbc(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);		memcpy(ctx->iv, iv, AES_BLOCK_SIZE);		break;	case EVP_CIPH_CFB_MODE:		memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);		iv = padlock_xcrypt_cfb(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);		memcpy(ctx->iv, iv, AES_BLOCK_SIZE);		break;	case EVP_CIPH_OFB_MODE:		memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);		padlock_xcrypt_ofb(nbytes/AES_BLOCK_SIZE, cdata, out_arg, in_arg);		memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);		break;	default:		return 0;	}	memset(cdata->iv, 0, AES_BLOCK_SIZE);	return 1;}#ifndef  PADLOCK_CHUNK# define PADLOCK_CHUNK	4096	/* Must be a power of 2 larger than 16 */#endif#if PADLOCK_CHUNK<16 || PADLOCK_CHUNK&(PADLOCK_CHUNK-1)# error "insane PADLOCK_CHUNK..."#endif/* Re-align the arguments to 16-Bytes boundaries and run the    encryption function itself. This function is not AES-specific. */static intpadlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,		   const unsigned char *in_arg, size_t nbytes){	struct padlock_cipher_data *cdata;	const  void *inp;	unsigned char  *out;	void  *iv;	int    inp_misaligned, out_misaligned, realign_in_loop;	size_t chunk, allocated=0;	if (nbytes == 0)		return 1;	if (nbytes % AES_BLOCK_SIZE)		return 0; /* are we expected to do tail processing? */	/* VIA promises CPUs that won't require alignment in the future.	   For now padlock_aes_align_required is initialized to 1 and	   the condition is never met... */	if (!padlock_aes_align_required)		return padlock_aes_cipher_omnivorous(ctx, out_arg, in_arg, nbytes);	inp_misaligned = (((size_t)in_arg) & 0x0F);	out_misaligned = (((size_t)out_arg) & 0x0F);	/* Note that even if output is aligned and input not,	 * I still prefer to loop instead of copy the whole	 * input and then encrypt in one stroke. This is done	 * in order to improve L1 cache utilization... */	realign_in_loop = out_misaligned|inp_misaligned;	if (!realign_in_loop)		return padlock_aes_cipher_omnivorous(ctx, out_arg, in_arg, nbytes);	/* this takes one "if" out of the loops */	chunk  = nbytes;	chunk %= PADLOCK_CHUNK;	if (chunk==0) chunk = PADLOCK_CHUNK;	if (out_misaligned) {		/* optmize for small input */		allocated = (chunk<nbytes?PADLOCK_CHUNK:nbytes);		out = alloca(0x10 + allocated);		out = NEAREST_ALIGNED(out);	}	else		out = out_arg;	cdata = ALIGNED_CIPHER_DATA(ctx);	padlock_verify_context(cdata);	switch (EVP_CIPHER_CTX_mode(ctx)) {	case EVP_CIPH_ECB_MODE:		do	{			if (inp_misaligned)				inp = padlock_memcpy(out, in_arg, chunk);			else				inp = in_arg;			in_arg += chunk;			padlock_xcrypt_ecb(chunk/AES_BLOCK_SIZE, cdata, out, inp);			if (out_misaligned)				out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;			else				out     = out_arg+=chunk;			nbytes -= chunk;			chunk   = PADLOCK_CHUNK;		} while (nbytes);		break;	case EVP_CIPH_CBC_MODE:		memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);		goto cbc_shortcut;		do	{			if (iv != cdata->iv)				memcpy(cdata->iv, iv, AES_BLOCK_SIZE);			chunk = PADLOCK_CHUNK;		cbc_shortcut: /* optimize for small input */			if (inp_misaligned)				inp = padlock_memcpy(out, in_arg, chunk);			else				inp = in_arg;			in_arg += chunk;			iv = padlock_xcrypt_cbc(chunk/AES_BLOCK_SIZE, cdata, out, inp);			if (out_misaligned)				out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;			else				out     = out_arg+=chunk;		} while (nbytes -= chunk);		memcpy(ctx->iv, iv, AES_BLOCK_SIZE);		break;	case EVP_CIPH_CFB_MODE:		memcpy (cdata->iv, ctx->iv, AES_BLOCK_SIZE);		goto cfb_shortcut;		do	{			if (iv != cdata->iv)				memcpy(cdata->iv, iv, AES_BLOCK_SIZE);			chunk = PADLOCK_CHUNK;		cfb_shortcut: /* optimize for small input */			if (inp_misaligned)				inp = padlock_memcpy(out, in_arg, chunk);			else				inp = in_arg;			in_arg += chunk;			iv = padlock_xcrypt_cfb(chunk/AES_BLOCK_SIZE, cdata, out, inp);			if (out_misaligned)				out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;			else				out     = out_arg+=chunk;		} while (nbytes -= chunk);		memcpy(ctx->iv, iv, AES_BLOCK_SIZE);		break;	case EVP_CIPH_OFB_MODE:		memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);		do	{			if (inp_misaligned)				inp = padlock_memcpy(out, in_arg, chunk);			else				inp = in_arg;			in_arg += chunk;			padlock_xcrypt_ofb(chunk/AES_BLOCK_SIZE, cdata, out, inp);			if (out_misaligned)				out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;			else				out     = out_arg+=chunk;			nbytes -= chunk;			chunk   = PADLOCK_CHUNK;		} while (nbytes);		memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);		break;	default:		return 0;	}	/* Clean the realign buffer if it was used */	if (out_misaligned) {		volatile unsigned long *p=(void *)out;		size_t   n = allocated/sizeof(*p);		while (n--) *p++=0;	}	memset(cdata->iv, 0, AES_BLOCK_SIZE);	return 1;}#endif /* OPENSSL_NO_AES *//* ===== Random Number Generator ===== *//* * This code is not engaged. The reason is that it does not comply * with recommendations for VIA RNG usage for secure applications * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it * provide meaningful error control... *//* Wrapper that provides an interface between the API and    the raw PadLock RNG */static intpadlock_rand_bytes(unsigned char *output, int count){	unsigned int eax, buf;	while (count >= 8) {		eax = padlock_xstore(output, 0);		if (!(eax&(1<<6)))	return 0; /* RNG disabled */		/* this ---vv--- covers DC bias, Raw Bits and String Filter */		if (eax&(0x1F<<10))	return 0;		if ((eax&0x1F)==0)	continue; /* no data, retry... */		if ((eax&0x1F)!=8)	return 0; /* fatal failure...  */		output += 8;		count  -= 8;	}	while (count > 0) {		eax = padlock_xstore(&buf, 3);		if (!(eax&(1<<6)))	return 0; /* RNG disabled */		/* this ---vv--- covers DC bias, Raw Bits and String Filter */		if (eax&(0x1F<<10))	return 0;		if ((eax&0x1F)==0)	continue; /* no data, retry... */		if ((eax&0x1F)!=1)	return 0; /* fatal failure...  */		*output++ = (unsigned char)buf;		count--;	}	*(volatile unsigned int *)&buf=0;	return 1;}/* Dummy but necessary function */static intpadlock_rand_status(void){	return 1;}/* Prepare structure for registration */static RAND_METHOD padlock_rand = {	NULL,			/* seed */	padlock_rand_bytes,	/* bytes */	NULL,			/* cleanup */	NULL,			/* add */	padlock_rand_bytes,	/* pseudorand */	padlock_rand_status,	/* rand status */};#endif /* COMPILE_HW_PADLOCK */#endif /* !OPENSSL_NO_HW_PADLOCK */#endif /* !OPENSSL_NO_HW */

⌨️ 快捷键说明

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