From fb54df66e491c8a9bba8a61253de55291ba3758c Mon Sep 17 00:00:00 2001 From: wangyu- Date: Mon, 18 Sep 2017 06:34:03 -0500 Subject: [PATCH] added wrapper.c --- encrypt.h | 6 +++ lib/{aes_c => aes_faster_c}/aes.c | 1 + lib/{aes_c => aes_faster_c}/aes.h | 0 lib/aes_faster_c/wrapper.c | 65 +++++++++++++++++++++++++++++++ main.cpp | 30 ++++++++++++++ makefile | 6 ++- 6 files changed, 106 insertions(+), 2 deletions(-) rename lib/{aes_c => aes_faster_c}/aes.c (99%) rename lib/{aes_c => aes_faster_c}/aes.h (100%) create mode 100644 lib/aes_faster_c/wrapper.c diff --git a/encrypt.h b/encrypt.h index 8e03d78..b247b53 100755 --- a/encrypt.h +++ b/encrypt.h @@ -32,4 +32,10 @@ extern cipher_mode_t cipher_mode; extern unordered_map auth_mode_tostring; extern unordered_map cipher_mode_tostring; + + + +int cipher_decrypt(const char *data,char *output,int &len,char * key); +int cipher_encrypt(const char *data,char *output,int &len,char * key); + #endif diff --git a/lib/aes_c/aes.c b/lib/aes_faster_c/aes.c similarity index 99% rename from lib/aes_c/aes.c rename to lib/aes_faster_c/aes.c index bf0c700..41480fb 100644 --- a/lib/aes_c/aes.c +++ b/lib/aes_faster_c/aes.c @@ -1221,6 +1221,7 @@ int aes_self_test( int verbose ) memset( buf, 0, 16 ); + if( v == AES_DECRYPT ) { aes_setkey_dec( &ctx, key, 128 + u * 64 ); diff --git a/lib/aes_c/aes.h b/lib/aes_faster_c/aes.h similarity index 100% rename from lib/aes_c/aes.h rename to lib/aes_faster_c/aes.h diff --git a/lib/aes_faster_c/wrapper.c b/lib/aes_faster_c/wrapper.c new file mode 100644 index 0000000..4094fee --- /dev/null +++ b/lib/aes_faster_c/wrapper.c @@ -0,0 +1,65 @@ +#include "aes.h" +#include +#include + +#if defined(AES256) && (AES256 == 1) +#define AES_KEYSIZE 256 +#elif defined(AES192) && (AES192 == 1) +#define AES_KEYSIZE 192 +#else +#define AES_KEYSIZE 128 +#endif + + +static aes_context ctx_de; +static aes_context ctx_en; +inline void init_de() +{ + static int done=0; + if(done==0) + { + aes_init( &ctx_de ); + done=1; + } +} +inline void init_en() +{ + static int done=0; + if(done==0) + { + aes_init( &ctx_en); + done=1; + } +} +void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length) +{ + init_en(); + printf("AES_ECB_encrypt not implemented\n"); + exit(-1); +} +void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length) +{ + init_de(); + printf("AES_ECB_encrypt not implemented\n"); + exit(-1); +} + +void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv) +{ + char tmp_iv[16]; + init_en(); + if(key!=0) aes_setkey_enc(&ctx_en,key,AES_KEYSIZE); + + memcpy(tmp_iv,iv,16); + aes_crypt_cbc( &ctx_en, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output ); + return ; +} +void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv) +{ + char tmp_iv[16]; + init_de(); + if(key!=0) aes_setkey_dec(&ctx_de,key,AES_KEYSIZE); + memcpy(tmp_iv,iv,16); + aes_crypt_cbc( &ctx_de, AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output ); + return; +} diff --git a/main.cpp b/main.cpp index f410ce5..a470ed0 100755 --- a/main.cpp +++ b/main.cpp @@ -2677,6 +2677,7 @@ int load_config(char *file_name, int &argc, vector &argv) //load conf fi return 0; } + int unit_test() { printf("running unit test\n"); @@ -2703,6 +2704,35 @@ int unit_test() printf("%x %x\n",(int)c1,(int)c2); + const char buf[]={1,2,3,4,5,6,7,8,9,10,11,2,13,14,15,16}; + char key[100]={0}; + char buf2[100]={0}; + char buf3[100]={0}; + char buf4[100]={0}; + int len=16; + for(int i=0;i",buf[i]); + } + printf("\n"); + cipher_encrypt(buf,buf2,len,key); + for(int i=0;i",buf2[i]); + } + printf("\n"); + int temp_len=len; + cipher_decrypt(buf2,buf3,len,key); + for(int i=0;i",buf3[i]); + } + printf("\n"); + cipher_encrypt(buf2,buf4,temp_len,key); + for(int i=0;i",buf4[i]); + } return 0; } diff --git a/makefile b/makefile index 3513e03..28112ba 100755 --- a/makefile +++ b/makefile @@ -8,8 +8,10 @@ cc_arm= /toolchains/arm-2014.05/bin/arm-none-linux-gnueabi-g++ #cc_bcm2708=/home/wangyu/raspberry/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ FLAGS= -std=c++11 -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-missing-field-initializers -SOURCES=main.cpp lib/aes.c lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp -lpthread -SOURCES_AES_ACC=$(filter-out lib/aes.c,$(SOURCES)) $(wildcard lib/aes_acc/aes*.c) +COMMON=main.cpp lib/md5.c encrypt.cpp log.cpp network.cpp common.cpp -lpthread +SOURCES= $(COMMON) lib/aes_faster_c/aes.c lib/aes_faster_c/wrapper.c +SOURCES_TINY_AES= $(COMMON) lib/aes.c +SOURCES_AES_ACC=$(COMMON) $(wildcard lib/aes_acc/aes*.c) NAME=udp2raw TARGETS=amd64 arm amd64_hw_aes arm_asm_aes mips24kc_be mips24kc_be_asm_aes x86 x86_asm_aes mips24kc_le mips24kc_le_asm_aes