mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-01-19 14:29:34 +08:00
optimize md5 and aes
This commit is contained in:
parent
0de85dd736
commit
661b329930
@ -208,6 +208,10 @@ int de_padding(const char *data ,int &data_len,int padding_num)
|
|||||||
}
|
}
|
||||||
int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
|
int cipher_aes128cbc_encrypt(const char *data,char *output,int &len,char * key)
|
||||||
{
|
{
|
||||||
|
static int first_time=1;
|
||||||
|
if(first_time==0) key=0;
|
||||||
|
else first_time=0;
|
||||||
|
|
||||||
char buf[buf_len];
|
char buf[buf_len];
|
||||||
memcpy(buf,data,len);//TODO inefficient code
|
memcpy(buf,data,len);//TODO inefficient code
|
||||||
|
|
||||||
@ -253,6 +257,9 @@ int cipher_none_encrypt(const char *data,char *output,int &len,char * key)
|
|||||||
}
|
}
|
||||||
int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
|
int cipher_aes128cbc_decrypt(const char *data,char *output,int &len,char * key)
|
||||||
{
|
{
|
||||||
|
static int first_time=1;
|
||||||
|
if(first_time==0) key=0;
|
||||||
|
else first_time=0;
|
||||||
|
|
||||||
if(len%16 !=0) {mylog(log_debug,"len%%16!=0\n");return -1;}
|
if(len%16 !=0) {mylog(log_debug,"len%%16!=0\n");return -1;}
|
||||||
//if(len<0) {mylog(log_debug,"len <0\n");return -1;}
|
//if(len<0) {mylog(log_debug,"len <0\n");return -1;}
|
||||||
|
@ -340,14 +340,15 @@ int AES_support_hwaccel(void)
|
|||||||
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
||||||
{
|
{
|
||||||
uint8_t iv_tmp[16];
|
uint8_t iv_tmp[16];
|
||||||
uint8_t rk[AES_RKSIZE];
|
static uint8_t rk[AES_RKSIZE];
|
||||||
|
|
||||||
if (key == NULL || iv == NULL)
|
if (iv == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
aeshw_init();
|
aeshw_init();
|
||||||
memcpy(iv_tmp, iv, 16);
|
memcpy(iv_tmp, iv, 16);
|
||||||
|
if(key!= NULL)
|
||||||
setkey_enc(rk, key);
|
setkey_enc(rk, key);
|
||||||
encrypt_cbc(rk, length, iv_tmp, input, output);
|
encrypt_cbc(rk, length, iv_tmp, input, output);
|
||||||
}
|
}
|
||||||
@ -355,15 +356,18 @@ void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, co
|
|||||||
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
||||||
{
|
{
|
||||||
uint8_t iv_tmp[16];
|
uint8_t iv_tmp[16];
|
||||||
uint8_t rk[AES_RKSIZE];
|
static uint8_t rk[AES_RKSIZE];
|
||||||
|
|
||||||
if (key == NULL || iv == NULL)
|
if (iv == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
aeshw_init();
|
aeshw_init();
|
||||||
memcpy(iv_tmp, iv, 16);
|
memcpy(iv_tmp, iv, 16);
|
||||||
|
if(key!= NULL)
|
||||||
|
{
|
||||||
setkey_dec(rk, key);
|
setkey_dec(rk, key);
|
||||||
|
}
|
||||||
decrypt_cbc(rk, length, iv_tmp, input, output);
|
decrypt_cbc(rk, length, iv_tmp, input, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,55 +11,45 @@
|
|||||||
#endif
|
#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)
|
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");
|
printf("AES_ECB_encrypt not implemented\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
void AES_ECB_decrypt(const uint8_t* input, const uint8_t* key, uint8_t *output, const uint32_t length)
|
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");
|
printf("AES_ECB_encrypt not implemented\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
void AES_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
||||||
{
|
{
|
||||||
|
static aes_context ctx;
|
||||||
|
static int done=0;
|
||||||
|
if(done==0)
|
||||||
|
{
|
||||||
|
aes_init( &ctx);
|
||||||
|
done=1;
|
||||||
|
}
|
||||||
char tmp_iv[16];
|
char tmp_iv[16];
|
||||||
init_en();
|
if(key!=0) aes_setkey_enc(&ctx,key,AES_KEYSIZE);
|
||||||
if(key!=0) aes_setkey_enc(&ctx_en,key,AES_KEYSIZE);
|
|
||||||
|
|
||||||
memcpy(tmp_iv,iv,16);
|
memcpy(tmp_iv,iv,16);
|
||||||
aes_crypt_cbc( &ctx_en, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
|
aes_crypt_cbc( &ctx, AES_ENCRYPT, length, (unsigned char* )tmp_iv, (const unsigned char*)input,(unsigned char*) output );
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
void AES_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv)
|
||||||
{
|
{
|
||||||
|
static aes_context ctx;
|
||||||
|
static int done=0;
|
||||||
|
if(done==0)
|
||||||
|
{
|
||||||
|
aes_init( &ctx);
|
||||||
|
done=1;
|
||||||
|
}
|
||||||
char tmp_iv[16];
|
char tmp_iv[16];
|
||||||
init_de();
|
if(key!=0) aes_setkey_dec(&ctx,key,AES_KEYSIZE);
|
||||||
if(key!=0) aes_setkey_dec(&ctx_de,key,AES_KEYSIZE);
|
|
||||||
memcpy(tmp_iv,iv,16);
|
memcpy(tmp_iv,iv,16);
|
||||||
aes_crypt_cbc( &ctx_de, AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
|
aes_crypt_cbc( &ctx,AES_DECRYPT, length, (unsigned char*)tmp_iv, (const unsigned char*)input, (unsigned char*) output );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user