From ca4a5d34175af915ee6c99fbf9134ad94832b803 Mon Sep 17 00:00:00 2001 From: wangyu Date: Tue, 14 Jul 2020 11:41:21 -0400 Subject: [PATCH] modify scheme of cfb --- encrypt.cpp | 30 +++++++++++++++++++++++++++++- encrypt.h | 2 ++ lib/aes_acc/aesacc.c | 4 ++-- lib/aes_faster_c/wrapper.cpp | 4 ++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/encrypt.cpp b/encrypt.cpp index 0df0c01..dced67e 100755 --- a/encrypt.cpp +++ b/encrypt.cpp @@ -297,6 +297,28 @@ int de_padding(const char *data ,int &data_len,int padding_num) } return 0; } +void aes_ecb_encrypt(const char *data,char *output) +{ + static int first_time=1; + char *key=(char*)cipher_key_encrypt; + if(aes_key_optimize) + { + if(first_time==0) key=0; + else first_time=0; + } + AES_ECB_encrypt_buffer((uint8_t*)data,(uint8_t*)key,(uint8_t*)output); +} +void aes_ecb_decrypt(const char *data,char *output) +{ + static int first_time=1; + char *key=(char*)cipher_key_decrypt; + if(aes_key_optimize) + { + if(first_time==0) key=0; + else first_time=0; + } + AES_ECB_decrypt_buffer((uint8_t*)data,(uint8_t*)key,(uint8_t*)output); +} int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key) { static int first_time=1; @@ -326,6 +348,7 @@ int cipher_aes128cfb_encrypt(const char *data,char *output,int &len,char * key) if(first_time==0) key=0; else first_time=0; } + aes_ecb_encrypt(data,buf); //encrypt the first block AES_CFB_encrypt_buffer((unsigned char *)output,(unsigned char *)buf,len,(unsigned char *)key,(unsigned char *)zero_iv); return 0; @@ -374,7 +397,12 @@ int cipher_aes128cfb_decrypt(const char *data,char *output,int &len,char * key) if(first_time==0) key=0; else first_time=0; } - AES_CFB_decrypt_buffer((unsigned char *)output,(unsigned char *)data,len,(unsigned char *)key,(unsigned char *)zero_iv); + char buf[buf_len]; + memcpy(buf,data,len);//TODO inefficient code + + aes_ecb_decrypt(data,buf); //decrypt the first block + + AES_CFB_decrypt_buffer((unsigned char *)output,(unsigned char *)buf,len,(unsigned char *)key,(unsigned char *)zero_iv); //if(de_padding(output,len,16)<0) return -1; return 0; } diff --git a/encrypt.h b/encrypt.h index fc22f17..c02e585 100755 --- a/encrypt.h +++ b/encrypt.h @@ -39,4 +39,6 @@ extern char gro_xor[256+100]; int cipher_decrypt(const char *data,char *output,int &len,char * key);//internal interface ,exposed for test only int cipher_encrypt(const char *data,char *output,int &len,char * key);//internal interface ,exposed for test only +void aes_ecb_encrypt(const char *data,char *output); +void aes_ecb_decrypt(const char *data,char *output); #endif diff --git a/lib/aes_acc/aesacc.c b/lib/aes_acc/aesacc.c index 236cd13..ca2f379 100644 --- a/lib/aes_acc/aesacc.c +++ b/lib/aes_acc/aesacc.c @@ -366,7 +366,7 @@ void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co decrypt_cbc(rk, length, iv_tmp, input, output); } -void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output, const uint32_t length) +void AES_ECB_encrypt_buffer(const uint8_t* input, const uint8_t* key, uint8_t* output) { static uint8_t rk[AES_RKSIZE]; @@ -376,7 +376,7 @@ void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t* output, encrypt_ecb(AES_NR, rk, input, output); } -void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length) +void AES_ECB_decrypt_buffer(const uint8_t* input, const uint8_t* key, uint8_t *output) { static uint8_t rk[AES_RKSIZE]; diff --git a/lib/aes_faster_c/wrapper.cpp b/lib/aes_faster_c/wrapper.cpp index 01cd94f..d58ace8 100644 --- a/lib/aes_faster_c/wrapper.cpp +++ b/lib/aes_faster_c/wrapper.cpp @@ -12,7 +12,7 @@ #endif -void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length) +void AES_ECB_encrypt_buffer(const uint8_t* input, const uint8_t* key, uint8_t *output) { static aes_context ctx; if(key!=0) @@ -24,7 +24,7 @@ void AES_ECB_encrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, assert(ret==0); return ; } -void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length) +void AES_ECB_decrypt_buffer(const uint8_t* input, const uint8_t* key, uint8_t *output) { static aes_context ctx; if(key!=0)