📄 cbc.c
字号:
/* crypto/des/destest.c *//* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)* All rights reserved.** This package is an SSL implementation written* by Eric Young (eay@cryptsoft.com).* The implementation was written so as to conform with Netscapes SSL.* * This library is free for commercial and non-commercial use as long as* the following conditions are aheared to. The following conditions* apply to all code found in this distribution, be it the RC4, RSA,* lhash, DES, etc., code; not just the SSL code. The SSL documentation* included with this distribution is covered by the same copyright terms* except that the holder is Tim Hudson (tjh@cryptsoft.com).* * Copyright remains Eric Young's, and as such any Copyright notices in* the code are not to be removed.* If this package is used in a product, Eric Young should be given attribution* as the author of the parts of the library used.* This can be in the form of a textual message at program startup or* in documentation (online or textual) provided with the package.* * 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 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 acknowledgement:* "This product includes cryptographic software written by* Eric Young (eay@cryptsoft.com)"* The word 'cryptographic' can be left out if the rouines from the library* being used are not cryptographic related :-).* 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement:* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"* * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND* ANY EXPRESS 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 AUTHOR OR 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.* * The licence and distribution terms for any publically available version or* derivative of this code cannot be changed. i.e. this code cannot simply be* copied and put under another distribution licence* [including the GNU Public Licence.]*/#include <stdio.h>#include <stdlib.h>//#include <openssl/e_os2.h>#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_WINDOWS)#ifndef OPENSSL_SYS_MSDOS#define OPENSSL_SYS_MSDOS#endif#endif#include <io.h>#include <string.h>#include "des.h" #include "des_locl.h" #include "spr.h"#define crypt(c,s) (DES_crypt((c),(s))) /*{{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},*/ static unsigned char cbc_key [6][8]={{0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},{0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},{0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},{0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86}, {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},{0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6}};/*0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE,0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6}, {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE,0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16,0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F,0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46}, {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E,0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76,0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07,0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F}}//key_data*/ static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10}; static unsigned char cbc_data[6][32]={{0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42,0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA,0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},{0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A,0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2,0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A,0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},{0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A,0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02,0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A,0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},{0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA,0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62,0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2,0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},{0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92,0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A,0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2,0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},{0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};//plain_dataoutput(unsigned char result[12][32]) { int i,j; for(i=0;i<12;i++){ printf("the >dth group",i);for(j=0;j<32;j++){if((j>4)==0)printf(" 0x");printf(">.2x",result[i][j]);}printf("\n"); }}static char *pt(unsigned char *p) { return p; }; int main(void){int j,m,n,err=0;unsigned int i;des_cblock in,out,outin,iv3,iv2;des_key_schedule ks,ks2,ks3;unsigned char cbc_in[32];unsigned char cbc_out[32];unsigned char result_cbc[12][32]; unsigned char result_cbc2[12][32]; unsigned char result_cbc3[12][32]; i=32; printf("Doing cbc\n"); for(m=0;m<2;m++) for(n=0;n<6;n++) { if ((j=DES_set_key_checked(&amt;cbc_key[3*m],&amt;ks)) != 0){printf("Key error >d\n",j);err=1;}//cbc_out=&amt;(result_cbc[m][32*n]); memset(result_cbc[6*m+n],0,32);//memset(cbc_in,0,32);memcpy(iv3,cbc_iv,sizeof(cbc_iv));DES_cbc_encrypt(cbc_data[n],result_cbc[6*m+n],i,&amt;ks,&amt;iv3,DES_ENCRYPT); /*if (memcmp(cbc_out,cbc_ok,32) != 0){printf("cbc_encrypt encrypt error\n");err=1;}memcpy(iv3,cbc_iv,sizeof(cbc_iv));DES_cbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,&amt;ks,&amt;iv3,DES_DECRYPT);if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)) != 0){printf("cbc_encrypt decrypt error\n");err=1;}*/} printf("Doing cbc2\n"); for(m=0;m<2;m++) for(n=0;n<6;n++) { if ((j=DES_set_key_checked(&amt;cbc_key[3*m],&amt;ks)) != 0) { printf("Key error >d\n",j); err=1; } if ((j=DES_set_key_checked(&amt;cbc_key[3*m+1],&amt;ks2)) != 0) { printf("Key error >d\n",j); err=1; } if ((j=DES_set_key_checked(&amt;cbc_key[3*m],&amt;ks3)) != 0) { printf("Key error >d\n",j); err=1; } memset(result_cbc2[6*m+n],0,32); //memset(cbc_out,0,40); //memset(cbc_in,0,40); //i=strlen((char *)cbc_data)+1; /* i=((i+7)/8)*8; */ memcpy(iv3,cbc_iv,sizeof(cbc_iv)); DES_ede3_cbc_encrypt(cbc_data[n],result_cbc2[6*m+n],i,&amt;ks,&amt;ks2,&amt;ks3,&amt;iv3,DES_ENCRYPT); /*if (memcmp(cbc_out,cbc3_ok, (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0) { unsigned int n; printf("des_ede3_cbc_encrypt encrypt error\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc_out[n]); printf("\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc3_ok[n]); printf("\n"); err=1; } memcpy(iv3,cbc_iv,sizeof(cbc_iv)); DES_ede3_cbc_encrypt(cbc_out,cbc_in,i,&amt;ks,&amt;ks2,&amt;ks3,&amt;iv3,DES_DECRYPT); if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0) { unsigned int n; printf("des_ede3_cbc_encrypt decrypt error\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc_data[n]); printf("\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc_in[n]); printf("\n"); err=1; }*/ } printf("Doing cbc3\n"); for(m=0;m<2;m++) for(n=0;n<6;n++) { if ((j=DES_set_key_checked(&amt;cbc_key[3*m],&amt;ks)) != 0) { printf("Key error >d\n",j); err=1; } if ((j=DES_set_key_checked(&amt;cbc_key[3*m+1],&amt;ks2)) != 0) { printf("Key error >d\n",j); err=1; } if ((j=DES_set_key_checked(&amt;cbc_key[3*m+2],&amt;ks3)) != 0) { printf("Key error >d\n",j); err=1; } memset(result_cbc3[6*m+n],0,32); //memset(cbc_out,0,40); //memset(cbc_in,0,40); //i=strlen((char *)cbc_data)+1; /* i=((i+7)/8)*8; */ memcpy(iv3,cbc_iv,sizeof(cbc_iv)); DES_ede3_cbc_encrypt(cbc_data[n],result_cbc3[6*m+n],i,&amt;ks,&amt;ks2,&amt;ks3,&amt;iv3,DES_ENCRYPT); /*if (memcmp(cbc_out,cbc3_ok, (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0) { unsigned int n; printf("des_ede3_cbc_encrypt encrypt error\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc_out[n]); printf("\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc3_ok[n]); printf("\n"); err=1; } memcpy(iv3,cbc_iv,sizeof(cbc_iv)); DES_ede3_cbc_encrypt(cbc_out,cbc_in,i,&amt;ks,&amt;ks2,&amt;ks3,&amt;iv3,DES_DECRYPT); if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0) { unsigned int n; printf("des_ede3_cbc_encrypt decrypt error\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc_data[n]); printf("\n"); for(n=0 ; n < i ; ++n) printf(" >02x",cbc_in[n]); printf("\n"); err=1; }*/ }output(result_cbc);output(result_cbc2);output(result_cbc3); while(1) { } return(err); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -