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

📄 fips_aesavs.c

📁 openssl包含TLS
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ==================================================================== * Copyright (c) 2004 The OpenSSL Project.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. All advertising materials mentioning features or use of this *    software must display the following acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to *    endorse or promote products derived from this software without *    prior written permission. For written permission, please contact *    openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" *    nor may "OpenSSL" appear in their names without prior written *    permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following *    acknowledgment: *    "This product includes software developed by the OpenSSL Project *    for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * *//*---------------------------------------------  NIST AES Algorithm Validation Suite  Test Program  Donated to OpenSSL by:  V-ONE Corporation  20250 Century Blvd, Suite 300  Germantown, MD 20874  U.S.A.  ----------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <assert.h>#include <openssl/aes.h>#include <openssl/evp.h>#include <openssl/fips.h>#include <openssl/err.h>#include "e_os.h"#define AES_BLOCK_SIZE 16#define VERBOSE 1/*-----------------------------------------------*/int AESTest(EVP_CIPHER_CTX *ctx,	    char *amode, int akeysz, unsigned char *aKey, 	    unsigned char *iVec, 	    int dir,  /* 0 = decrypt, 1 = encrypt */	    unsigned char *plaintext, unsigned char *ciphertext, int len)    {    const EVP_CIPHER *cipher = NULL;    int ret = 1;    int kt = 0;    if (ctx)	memset(ctx, 0, sizeof(EVP_CIPHER_CTX));    if (strcasecmp(amode, "CBC") == 0)	kt = 1000;    else if (strcasecmp(amode, "ECB") == 0)	kt = 2000;    else if (strcasecmp(amode, "CFB128") == 0)	kt = 3000;    else if (strncasecmp(amode, "OFB", 3) == 0)	kt = 4000;    else if(!strcasecmp(amode,"CFB1"))	kt=5000;    else if(!strcasecmp(amode,"CFB8"))	kt=6000;    else	{	printf("Unknown mode: %s\n", amode);	EXIT(1);	}    if (ret)	{	if ((akeysz != 128) && (akeysz != 192) && (akeysz != 256))	    {	    printf("Invalid key size: %d\n", akeysz);	    ret = 0;	    }	else	    {	    kt += akeysz;	    switch (kt)		{	    case 1128:  /* CBC 128 */		cipher = EVP_aes_128_cbc();		break;	    case 1192:  /* CBC 192 */		cipher = EVP_aes_192_cbc();		break;	    case 1256:  /* CBC 256 */		cipher = EVP_aes_256_cbc();		break;	    case 2128:  /* ECB 128 */		cipher = EVP_aes_128_ecb();		break;	    case 2192:  /* ECB 192 */		cipher = EVP_aes_192_ecb();		break;	    case 2256:  /* ECB 256 */		cipher = EVP_aes_256_ecb();		break;	    case 3128:  /* CFB 128 */		cipher = EVP_aes_128_cfb();		break;	    case 3192:  /* CFB 192 */		cipher = EVP_aes_192_cfb();		break;	    case 3256:  /* CFB 256 */		cipher = EVP_aes_256_cfb();		break;	    case 4128:  /* OFB 128 */		cipher = EVP_aes_128_ofb();		break;	    case 4192:  /* OFB 192 */		cipher = EVP_aes_192_ofb();		break;	    case 4256:  /* OFB 256 */		cipher = EVP_aes_256_ofb();		break;	    case 5128:		cipher=EVP_aes_128_cfb1();		break;	    case 5192:		cipher=EVP_aes_192_cfb1();		break;	    case 5256:		cipher=EVP_aes_256_cfb1();		break;	    case 6128:		cipher=EVP_aes_128_cfb8();		break;	    case 6192:		cipher=EVP_aes_192_cfb8();		break;	    case 6256:		cipher=EVP_aes_256_cfb8();		break;	    default:		printf("Didn't handle mode %d\n",kt);		EXIT(1);		}	    if (dir)		{ /* encrypt */		if(!EVP_CipherInit(ctx, cipher, aKey, iVec, AES_ENCRYPT))		    {		    ERR_print_errors_fp(stderr);		    EXIT(1);		    }		  		EVP_Cipher(ctx, ciphertext, (unsigned char*)plaintext, len);		}	    else		{ /* decrypt */		if(!EVP_CipherInit(ctx, cipher, aKey, iVec, AES_DECRYPT))		    {		    ERR_print_errors_fp(stderr);		    EXIT(1);		    }		EVP_Cipher(ctx, (unsigned char*)plaintext, ciphertext, len);		}	    }	}    return ret;    }/*-----------------------------------------------*/int hex2bin(char *in, int len, unsigned char *out){  int n1, n2;  unsigned char ch;  for (n1 = 0, n2 = 0; n1 < len; )    { /* first byte */      if ((in[n1] >= '0') && (in[n1] <= '9'))	ch = in[n1++] - '0';      else if ((in[n1] >= 'A') && (in[n1] <= 'F'))	ch = in[n1++] - 'A' + 10;      else if ((in[n1] >= 'a') && (in[n1] <= 'f'))	ch = in[n1++] - 'a' + 10;      else	return -1;      if(len == 1)	  {	  out[n2++]=ch;	  break;	  }      out[n2] = ch << 4;      /* second byte */      if ((in[n1] >= '0') && (in[n1] <= '9'))	ch = in[n1++] - '0';      else if ((in[n1] >= 'A') && (in[n1] <= 'F'))	ch = in[n1++] - 'A' + 10;      else if ((in[n1] >= 'a') && (in[n1] <= 'f'))	ch = in[n1++] - 'a' + 10;      else	return -1;      out[n2++] |= ch;    }  return n2;}/*-----------------------------------------------*/int bin2hex(unsigned char *in, int len, char *out){  int n1, n2;  unsigned char ch;  for (n1 = 0, n2 = 0; n1 < len; ++n1)    {      /* first nibble */      ch = in[n1] >> 4;      if (ch <= 0x09)	out[n2++] = ch + '0';      else	out[n2++] = ch - 10 + 'a';      /* second nibble */      ch = in[n1] & 0x0f;      if (ch <= 0x09)	out[n2++] = ch + '0';      else	out[n2++] = ch - 10 + 'a';    }  return n2;}/* NB: this return the number of _bits_ read */int bint2bin(const char *in, int len, unsigned char *out)    {    int n;    memset(out,0,len);    for(n=0 ; n < len ; ++n)	if(in[n] == '1')	    out[n/8]|=(0x80 >> (n%8));    return len;    }int bin2bint(const unsigned char *in,int len,char *out)    {    int n;    for(n=0 ; n < len ; ++n)	out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';    return n;    }/*-----------------------------------------------*/void PrintValue(char *tag, unsigned char *val, int len){#if VERBOSE  char obuf[2048];  int olen;  olen = bin2hex(val, len, obuf);  printf("%s = %.*s\n", tag, olen, obuf);#endif}void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)    {    char obuf[2048];    int olen;    if(bitmode)	olen=bin2bint(val,len,obuf);    else	olen=bin2hex(val,len,obuf);    fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);#if VERBOSE    printf("%s = %.*s\n", tag, olen, obuf);#endif    }/*-----------------------------------------------*/char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB128"};enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB128};enum XCrypt {XDECRYPT, XENCRYPT};/*=============================*//*  Monte Carlo Tests          *//*-----------------------------*//*#define gb(a,b) (((a)[(b)/8] >> ((b)%8))&1)*//*#define sb(a,b,v) ((a)[(b)/8]=((a)[(b)/8]&~(1 << ((b)%8)))|(!!(v) << ((b)%8)))*/#define gb(a,b) (((a)[(b)/8] >> (7-(b)%8))&1)#define sb(a,b,v) ((a)[(b)/8]=((a)[(b)/8]&~(1 << (7-(b)%8)))|(!!(v) << (7-(b)%8)))int do_mct(char *amode, 	   int akeysz, unsigned char *aKey,unsigned char *iVec,	   int dir, unsigned char *text, int len,	   FILE *rfp)    {    int ret = 0;    unsigned char key[101][32];    unsigned char iv[101][AES_BLOCK_SIZE];    unsigned char ptext[1001][32];    unsigned char ctext[1001][32];    unsigned char ciphertext[64+4];    int i, j, n, n1, n2;    int imode = 0, nkeysz = akeysz/8;    EVP_CIPHER_CTX ctx;    if (len > 32)	{	printf("\n>>>> Length exceeds 32 for %s %d <<<<\n\n", 	       amode, akeysz);	return -1;	}    for (imode = 0; imode < 6; ++imode)	if (strcmp(amode, t_mode[imode]) == 0)	    break;    if (imode == 6)	{ 	printf("Unrecognized mode: %s\n", amode);	return -1;	}    memcpy(key[0], aKey, nkeysz);    if (iVec)	memcpy(iv[0], iVec, AES_BLOCK_SIZE);    if (dir == XENCRYPT)	memcpy(ptext[0], text, len);    else	memcpy(ctext[0], text, len);    for (i = 0; i < 100; ++i)	{	/* printf("Iteration %d\n", i); */	if (i > 0)	    {	    fprintf(rfp,"COUNT = %d\n",i);	    OutputValue("KEY",key[i],nkeysz,rfp,0);	    if (imode != ECB)  /* ECB */		OutputValue("IV",iv[i],AES_BLOCK_SIZE,rfp,0);	    /* Output Ciphertext | Plaintext */	    OutputValue(t_tag[dir^1],dir ? ptext[0] : ctext[0],len,rfp,			imode == CFB1);	    }	for (j = 0; j < 1000; ++j)	    {	    switch (imode)		{	    case ECB:		if (j == 0)		    { /* set up encryption */		    ret = AESTest(&ctx, amode, akeysz, key[i], NULL, 				  dir,  /* 0 = decrypt, 1 = encrypt */				  ptext[j], ctext[j], len);		    if (dir == XENCRYPT)			memcpy(ptext[j+1], ctext[j], len);		    else			memcpy(ctext[j+1], ptext[j], len);		    }		else		    {		    if (dir == XENCRYPT)			{			EVP_Cipher(&ctx, ctext[j], ptext[j], len);			memcpy(ptext[j+1], ctext[j], len);			}		    else			{			EVP_Cipher(&ctx, ptext[j], ctext[j], len);			memcpy(ctext[j+1], ptext[j], len);			}		    }		break;	    case CBC:	    case OFB:  	    case CFB128:		if (j == 0)		    {		    ret = AESTest(&ctx, amode, akeysz, key[i], iv[i], 				  dir,  /* 0 = decrypt, 1 = encrypt */				  ptext[j], ctext[j], len);		    if (dir == XENCRYPT)			memcpy(ptext[j+1], iv[i], len);		    else			memcpy(ctext[j+1], iv[i], len);		    }		else		    {		    if (dir == XENCRYPT)			{			EVP_Cipher(&ctx, ctext[j], ptext[j], len);			memcpy(ptext[j+1], ctext[j-1], len);			}		    else			{			EVP_Cipher(&ctx, ptext[j], ctext[j], len);			memcpy(ctext[j+1], ptext[j-1], len);			}		    }		break;	    case CFB8:		if (j == 0)		    {		    ret = AESTest(&ctx, amode, akeysz, key[i], iv[i], 				  dir,  /* 0 = decrypt, 1 = encrypt */				  ptext[j], ctext[j], len);		    }		else		    {		    if (dir == XENCRYPT)			EVP_Cipher(&ctx, ctext[j], ptext[j], len);		    else			EVP_Cipher(&ctx, ptext[j], ctext[j], len);		    }		if (dir == XENCRYPT)		    {		    if (j < 16)			memcpy(ptext[j+1], &iv[i][j], len);		    else			memcpy(ptext[j+1], ctext[j-16], len);		    }		else		    {		    if (j < 16)			memcpy(ctext[j+1], &iv[i][j], len);		    else			memcpy(ctext[j+1], ptext[j-16], len);		    }		break;	    case CFB1:		if(j == 0)		    {		    /* compensate for wrong endianness of input file */		    if(i == 0)			ptext[0][0]<<=7;		    ret=AESTest(&ctx,amode,akeysz,key[i],iv[i],dir,				ptext[j], ctext[j], len);		    }		else		    {		    if (dir == XENCRYPT)			EVP_Cipher(&ctx, ctext[j], ptext[j], len);		    else			EVP_Cipher(&ctx, ptext[j], ctext[j], len);		    }		if(dir == XENCRYPT)		    {		    if(j < 128)			sb(ptext[j+1],0,gb(iv[i],j));		    else			sb(ptext[j+1],0,gb(ctext[j-128],0));		    }		else		    {		    if(j < 128)			sb(ctext[j+1],0,gb(iv[i],j));		    else			sb(ctext[j+1],0,gb(ptext[j-128],0));		    }		break;		}	    }	--j; /* reset to last of range */	/* Output Ciphertext | Plaintext */	OutputValue(t_tag[dir],dir ? ctext[j] : ptext[j],len,rfp,

⌨️ 快捷键说明

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