📄 fips_desmovs.c
字号:
/* ==================================================================== * 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 DES Modes of Operation Validation System Test Program Based on the AES Validation Suite, which was: 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/des.h>#include <openssl/evp.h>#include <openssl/fips.h>#include <openssl/err.h>#include "e_os.h"/*#define AES_BLOCK_SIZE 16*/#define VERBOSE 0/*-----------------------------------------------*/int DESTest(EVP_CIPHER_CTX *ctx, char *amode, int akeysz, unsigned char *aKey, unsigned char *iVec, int dir, /* 0 = decrypt, 1 = encrypt */ unsigned char *out, unsigned char *in, int len) { const EVP_CIPHER *cipher = NULL; 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, "CFB64") == 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 (akeysz != 64 && akeysz != 192) { printf("Invalid key size: %d\n", akeysz); EXIT(1); } else { kt += akeysz; switch (kt) { case 1064: cipher=EVP_des_cbc(); break; case 1192: cipher=EVP_des_ede3_cbc(); break; case 2064: cipher=EVP_des_ecb(); break; case 2192: cipher=EVP_des_ede3_ecb(); break; case 3064: cipher=EVP_des_cfb64(); break; case 3192: cipher=EVP_des_ede3_cfb64(); break; case 4064: cipher=EVP_des_ofb(); break; case 4192: cipher=EVP_des_ede3_ofb(); break; case 5064: cipher=EVP_des_cfb1(); break; case 5192: cipher=EVP_des_ede3_cfb1(); break; case 6064: cipher=EVP_des_cfb8(); break; case 6192: cipher=EVP_des_ede3_cfb8(); break; default: printf("Didn't handle mode %d\n",kt); EXIT(1); } if(!EVP_CipherInit(ctx, cipher, aKey, iVec, dir)) { ERR_print_errors_fp(stderr); EXIT(1); } EVP_Cipher(ctx, out, in, len); } return 1; }/*-----------------------------------------------*/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 DebugValue(char *tag, unsigned char *val, int len) { char obuf[2048]; int olen; olen = bin2hex(val, len, obuf); printf("%s = %.*s\n", tag, olen, obuf); }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 }void shiftin(unsigned char *dst,unsigned char *src,int nbits) { int n; /* move the bytes... */ memmove(dst,dst+nbits/8,3*8-nbits/8); /* append new data */ memcpy(dst+3*8-nbits/8,src,(nbits+7)/8); /* left shift the bits */ if(nbits%8) for(n=0 ; n < 3*8 ; ++n) dst[n]=(dst[n] << (nbits%8))|(dst[n+1] >> (8-nbits%8)); } /*-----------------------------------------------*/char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB64"};enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB64};int Sizes[6]={64,64,64,1,8,64};void do_mct(char *amode, int akeysz, int numkeys, unsigned char *akey,unsigned char *ivec, int dir, unsigned char *text, int len, FILE *rfp) { int i,imode; unsigned char nk[4*8]; /* longest key+8 */ unsigned char text0[8]; for (imode=0 ; imode < 6 ; ++imode) if(!strcmp(amode,t_mode[imode])) break; if (imode == 6) { printf("Unrecognized mode: %s\n", amode); EXIT(1); } for(i=0 ; i < 400 ; ++i) { int j; int n; EVP_CIPHER_CTX ctx; int kp=akeysz/64; unsigned char old_iv[8]; fprintf(rfp,"\nCOUNT = %d\n",i); if(kp == 1) OutputValue("KEY",akey,8,rfp,0); else for(n=0 ; n < kp ; ++n) { fprintf(rfp,"KEY%d",n+1); OutputValue("",akey+n*8,8,rfp,0); } if(imode != ECB) OutputValue("IV",ivec,8,rfp,0); OutputValue(t_tag[dir^1],text,len,rfp,imode == CFB1); /* compensate for endianness */ if(imode == CFB1) text[0]<<=7; memcpy(text0,text,8); for(j=0 ; j < 10000 ; ++j) { unsigned char old_text[8]; memcpy(old_text,text,8); if(j == 0) { memcpy(old_iv,ivec,8); DESTest(&ctx,amode,akeysz,akey,ivec,dir,text,text,len); } else { memcpy(old_iv,ctx.iv,8); EVP_Cipher(&ctx,text,text,len); } if(j == 9999) { OutputValue(t_tag[dir],text,len,rfp,imode == CFB1); /* memcpy(ivec,text,8); */ } /* DebugValue("iv",ctx.iv,8); */ /* accumulate material for the next key */ shiftin(nk,text,Sizes[imode]); /* DebugValue("nk",nk,24);*/ if((dir && (imode == CFB1 || imode == CFB8 || imode == CFB64 || imode == CBC)) || imode == OFB) memcpy(text,old_iv,8); if(!dir && (imode == CFB1 || imode == CFB8 || imode == CFB64)) { /* the test specifies using the output of the raw DES operation which we don't have, so reconstruct it... */ for(n=0 ; n < 8 ; ++n) text[n]^=old_text[n]; } } for(n=0 ; n < 8 ; ++n) akey[n]^=nk[16+n]; for(n=0 ; n < 8 ; ++n) akey[8+n]^=nk[8+n]; for(n=0 ; n < 8 ; ++n) akey[16+n]^=nk[n]; if(numkeys < 3) memcpy(&akey[2*8],akey,8); if(numkeys < 2) memcpy(&akey[8],akey,8); DES_set_odd_parity((DES_cblock *)akey); DES_set_odd_parity((DES_cblock *)(akey+8)); DES_set_odd_parity((DES_cblock *)(akey+16)); memcpy(ivec,ctx.iv,8); /* pointless exercise - the final text doesn't depend on the initial text in OFB mode, so who cares what it is? (Who designed these tests?) */ if(imode == OFB) for(n=0 ; n < 8 ; ++n) text[n]=text0[n]^old_iv[n]; } } int proc_file(char *rqfile) { char afn[256], rfn[256]; FILE *afp = NULL, *rfp = NULL; char ibuf[2048];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -