Skip to content
Snippets Groups Projects
Commit 752e8923 authored by ivan.fedorov's avatar ivan.fedorov
Browse files

padding iaes error fixed

test revised
parent 0bc4248c
No related branches found
No related tags found
1 merge request!25Rct2
......@@ -17,7 +17,7 @@ void dap_enc_aes_key_delete(struct dap_enc_key *a_key);
void dap_enc_aes_key_generate(struct dap_enc_key * a_key, const void *kex_buf, size_t kex_size,
const void * seed, size_t seed_size, size_t key_size);
size_t dap_enc_iaes256_calc_decode_size(const size_t size_in);
size_t dap_enc_iaes256_calc_decode_max_size(const size_t size_in);
size_t dap_enc_iaes256_calc_encode_size(const size_t size_in);
size_t dap_enc_iaes256_cbc_decrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out);
......
......@@ -83,13 +83,13 @@ size_t dap_enc_iaes256_cbc_decrypt(struct dap_enc_key * a_key, const void * a_in
*a_out = (uint8_t *) malloc(a_in_size);
return IAES_256_CBC_decrypt(a_in, *a_out, DAP_ENC_AES_KEY(a_key)->ivec, a_in_size, a_key->priv_key_data);
return dap_enc_iaes256_cbc_decrypt_fast(a_key, a_in, a_in_size, *a_out, a_in_size);
}
size_t dap_enc_iaes256_cbc_decrypt_fast(struct dap_enc_key * a_key, const void * a_in,
size_t a_in_size, void * buf_out, size_t buf_out_size)
{
if (a_in_size % 16) {
if (a_in_size % IAES_BLOCK_SIZE) {
log_it(L_ERROR, "Bad in size");
return 0;
} else if(buf_out_size < a_in_size) {
......@@ -97,30 +97,53 @@ size_t dap_enc_iaes256_cbc_decrypt_fast(struct dap_enc_key * a_key, const void *
return 0;
}
return IAES_256_CBC_decrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec,
a_in_size, a_key->priv_key_data);
size_t block_in32_size = IAES_BLOCK_SIZE/sizeof(uint32_t);
uint32_t round_decrypt_key[60];
uint32_t feedback[block_in32_size];
memcpy(&feedback[0], DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE);
swap_endian((uint32_t *)a_key->priv_key_data, IAES_KEYSIZE/sizeof(uint32_t));
Key_Shedule_for_decrypT((uint32_t *)a_key->priv_key_data, round_decrypt_key);
void *data = buf_out;
const void *cdata = a_in;
size_t count_block, count32_word;
for(count_block = 0; count_block < (a_in_size/IAES_BLOCK_SIZE); count_block++){
AES256_dec_cernelT((uint32_t *)cdata + count_block*block_in32_size,
(uint32_t *)data + count_block*block_in32_size, round_decrypt_key);
for (count32_word = 0; count32_word < block_in32_size; count32_word++)
*((uint32_t *)data + count_block * block_in32_size + count32_word) ^= feedback[count32_word];
memcpy(&feedback[0], (uint32_t *)cdata + count_block*block_in32_size, IAES_BLOCK_SIZE);
}
// for(int i = 0; i < 16; ++i)
// {printf("%.2x ", ((uint8_t*)data)[i]);}
// printf("\n");fflush(stdout);
swap_endian((uint32_t *)a_key->priv_key_data, IAES_KEYSIZE/sizeof(uint32_t));
return a_in_size - ((uint8_t *)data)[a_in_size - 1];
}
size_t dap_enc_iaes256_cbc_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out)
{
size_t length_data_new;
uint8_t *data_new;
length_data_new = iaes_block128_padding(a_in, &data_new, a_in_size);
*a_out = (uint8_t *)malloc(length_data_new);
length_data_new = dap_enc_iaes256_calc_encode_size(a_in_size);
IAES_256_CBC_encrypt(data_new, *a_out, DAP_ENC_AES_KEY(a_key)->ivec, length_data_new, a_key->priv_key_data);
*a_out = DAP_NEW_SIZE(uint8_t, length_data_new);
free(data_new);
dap_enc_iaes256_cbc_encrypt_fast(a_key, a_in, a_in_size, *a_out, length_data_new);
return length_data_new;
}
size_t dap_enc_iaes256_calc_encode_size(const size_t size_in)
{
return iaes_calc_block128_size(size_in);
return size_in + 1 + (IAES_BLOCK_SIZE - (size_in + 1)%IAES_BLOCK_SIZE)%IAES_BLOCK_SIZE;
}
size_t dap_enc_iaes256_calc_decode_size(const size_t size_in)
size_t dap_enc_iaes256_calc_decode_max_size(const size_t size_in)
{
return size_in;
}
......@@ -128,24 +151,60 @@ size_t dap_enc_iaes256_calc_decode_size(const size_t size_in)
size_t dap_enc_iaes256_cbc_encrypt_fast(struct dap_enc_key * a_key, const void * a_in,
size_t a_in_size, void * buf_out, size_t buf_out_size)
{
size_t out_size = iaes_calc_block128_size(a_in_size);
if((a_in_size % IAES_BLOCK_SIZE) == 0) {
IAES_256_CBC_encrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, out_size, a_key->priv_key_data);
return out_size;
}
size_t out_size = dap_enc_iaes256_calc_encode_size(a_in_size);
if(buf_out_size < out_size) {
log_it(L_ERROR, "buf_out_size less than expected encrypt out size data");
return 0;
}
uint8_t* data_in_new;
iaes_block128_padding(a_in, &data_in_new, a_in_size);
IAES_256_CBC_encrypt(data_in_new, buf_out, DAP_ENC_AES_KEY(a_key)->ivec,
out_size, a_key->priv_key_data);
int last_block_from_in = a_in_size%IAES_BLOCK_SIZE;
size_t block_in32_size = IAES_BLOCK_SIZE/sizeof(uint32_t);
uint32_t feedback[block_in32_size];
memcpy(&feedback[0], DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE);
swap_endian((uint32_t *)a_key->priv_key_data, IAES_KEYSIZE/sizeof(uint32_t));
size_t count_block, count32_word;
const void *data = a_in;
void *cdata = buf_out;
for(count_block = 0; count_block < (a_in_size - last_block_from_in)/IAES_BLOCK_SIZE; count_block++)
{
for (count32_word = 0; count32_word < block_in32_size; count32_word++)
*((uint32_t *)cdata + count_block * block_in32_size + count32_word) =
*((uint32_t *)data + count_block * block_in32_size + count32_word) ^ feedback[count32_word];
AES256_enc_cernelT(((uint32_t *)cdata + count_block * block_in32_size), feedback, (uint32_t *)a_key->priv_key_data);
memcpy ((uint32_t *)cdata + count_block * block_in32_size, &feedback[0], IAES_BLOCK_SIZE);
}
uint8_t tmp_in[IAES_BLOCK_SIZE];
memcpy(tmp_in, a_in + a_in_size/IAES_BLOCK_SIZE*IAES_BLOCK_SIZE, last_block_from_in);
int padd_size = IAES_BLOCK_SIZE - last_block_from_in;
for(int padd_num = 0; padd_num < padd_size - 1; ++padd_num)
tmp_in[last_block_from_in + padd_num] = 16;
tmp_in[IAES_BLOCK_SIZE - 1] = padd_size;
for (count32_word = 0; count32_word < block_in32_size; count32_word++)
*((uint32_t *)cdata + count_block * block_in32_size + count32_word) =
*((uint32_t *)tmp_in + count32_word) ^ feedback[count32_word];
AES256_enc_cernelT(((uint32_t *)cdata + count_block * block_in32_size), feedback, (uint32_t *)a_key->priv_key_data);
memcpy ((uint32_t *)cdata + count_block * block_in32_size, &feedback[0], IAES_BLOCK_SIZE);
swap_endian((uint32_t *)a_key->priv_key_data,IAES_KEYSIZE/sizeof(uint32_t));
// IAES_256_CBC_encrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, a_in_size - last_block_from_in, a_key->priv_key_data);
// uint8_t tmp_in[IAES_BLOCK_SIZE];
// memcpy(tmp_in, a_in + a_in_size/IAES_BLOCK_SIZE*IAES_BLOCK_SIZE, last_block_from_in);
// int padd_size = IAES_BLOCK_SIZE - last_block_from_in;
// for(int padd_num = 0; padd_num < padd_size; ++padd_num)
// tmp_in[last_block_from_in + padd_num] = 16;
free(data_in_new);
// tmp_in[last_block_from_in + IAES_BLOCK_SIZE - 1] = padd_size;
// IAES_256_CBC_encrypt(tmp_in, buf_out + a_in_size - last_block_from_in, buf_out + a_in_size - last_block_from_in - IAES_BLOCK_SIZE, IAES_BLOCK_SIZE, a_key->priv_key_data);
return out_size;
}
......@@ -79,7 +79,7 @@ struct dap_enc_key_callbacks{
.gen_key_public = NULL,
.gen_key_public_size = NULL,
.enc_out_size = dap_enc_iaes256_calc_encode_size,
.dec_out_size = dap_enc_iaes256_calc_decode_size,
.dec_out_size = dap_enc_iaes256_calc_decode_max_size,
.sign_get = NULL,
.sign_verify = NULL
},
......
......@@ -6,6 +6,10 @@
#define IAES_BLOCK_SIZE 16
#define IAES_KEYSIZE 32
void AES256_enc_cernelT(uint32_t * in, uint32_t * out, uint32_t * masterkey);
void AES256_dec_cernelT(uint32_t * in, uint32_t * out, uint32_t * decr_key);
void swap_endian(uint32_t *buff, unsigned long len);
void Key_Shedule_for_decrypT(uint32_t * key, uint32_t * rounds_keys);
void IAES_256_CBC_encrypt(const uint8_t *data, uint8_t *output, uint8_t * ivec, unsigned long length, uint8_t *masterkey);
size_t IAES_256_CBC_decrypt(const uint8_t *cdata, uint8_t *output, uint8_t * ivec, unsigned long length, uint8_t *key);
......
......@@ -13,16 +13,6 @@ size_t iaes_calc_block128_size(size_t length_data)
return new_length_data;
}
size_t iaes_block128_padding(const void *data, uint8_t **data_new, size_t length_data)
{
size_t length_data_new = iaes_calc_block128_size(length_data);
*data_new = (uint8_t *) malloc(length_data_new);
memcpy(*data_new, data, length_data);
memset(*data_new + length_data, 0x0, length_data_new - length_data);
return length_data_new;
}
void swap_endian(uint32_t *buff, unsigned long len)
{
......@@ -210,7 +200,7 @@ void IAES_256_CBC_encrypt(const uint8_t *data, uint8_t *cdata, uint8_t *ivec, un
AES256_enc_cernelT(((uint32_t *)cdata + count_block * block_in32_size), feedback, (uint32_t *)masterkey);
memcpy ((uint32_t *)cdata + count_block * block_in32_size, &feedback[0], IAES_BLOCK_SIZE);
}
}
swap_endian((uint32_t *)masterkey,IAES_KEYSIZE/sizeof(uint32_t));
}
......@@ -483,19 +473,7 @@ size_t IAES_256_CBC_decrypt(const uint8_t *cdata, uint8_t *data, uint8_t *ivec,
memcpy(&feedback[0], (uint32_t *)cdata + count_block*block_in32_size, IAES_BLOCK_SIZE);
}
swap_endian((uint32_t *)masterkey, IAES_KEYSIZE/sizeof(uint32_t));
size_t i, padding = 0;
size_t end = length > 16 ? length - 16 : 0;
if(length>0)
for(i = length-1; i >= end; i--)
{
if(data[i] == 0)
padding++;
else
break;
}
return length - padding;
return length;
}
......@@ -35,7 +35,7 @@ void test_encode_decode(int count_steps, const dap_enc_key_type_t key_type, cons
uint8_t *source = DAP_NEW_SIZE(uint8_t, source_size+verb);
randombase64(source, source_size);
randombytes(source, source_size);//randombase64(source, source_size);
// for(int i = 0; i < source_size; ++i)
// printf("%.2x ", source[i]);
// printf(" = len(%d)\n", source_size);fflush(stdout);
......@@ -99,7 +99,7 @@ void test_encode_decode_fast(int count_steps, const dap_enc_key_type_t key_type,
source_size = 1 + random_uint32_t(2000);
uint8_t *source = DAP_NEW_SIZE(uint8_t,source_size + 0);
randombase64(source, source_size);
randombytes(source, source_size);//randombase64(source, source_size);
size_t encrypted_size = key->enc_na(key, source, source_size, buf_encrypt_out, buf_size);
......@@ -125,65 +125,62 @@ static void _encrypt_decrypt(enum dap_enc_key_type key_type,
size_t count_steps)
{
size_t source_size = 1;
const int MAX_SEED_SIZE = 14;
const int MAX_SEED_SIZE = 100;
uint8_t seed[MAX_SEED_SIZE];
for (size_t i = 0; i < count_steps; i++) {
int step = 2;// + random_uint32_t( 20);
source_size = (size_t)step;
source_size = 1 + random_uint32_t(2000);
const char *kex_data = "123";
size_t kex_size = strlen(kex_data);
const size_t seed_size = 2;// + random_uint32_t(MAX_SEED_SIZE-1);
const size_t seed_size = 1 + random_uint32_t(MAX_SEED_SIZE-1);
randombytes(seed, seed_size);
printf("i = %d ss = %d, ss=%d\n",i, source_size,seed_size );fflush(stdout);
// printf("i = %d ss = %d, ss=%d\n",i, source_size,seed_size );fflush(stdout);
uint8_t *source = DAP_NEW_SIZE(uint8_t, source_size);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
randombytes(source, source_size);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
dap_enc_key_t* key = dap_enc_key_new_generate(key_type, kex_data, kex_size, seed, seed_size, 0);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
size_t encrypt_buff_size = dap_enc_code_out_size(key, source_size, data_type);
uint8_t *encrypt_result = DAP_NEW_SIZE(uint8_t, encrypt_buff_size);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
size_t encrypted_size = dap_enc_code(key, source,
source_size,
encrypt_result,
encrypt_buff_size,
data_type);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
size_t min_decode_buff_size = dap_enc_decode_out_size(key, encrypt_buff_size, data_type);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
uint8_t *decode_result = DAP_NEW_SIZE(uint8_t, min_decode_buff_size);
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
size_t out_size = dap_enc_decode(key,
encrypt_result,
encrypted_size,
decode_result,
min_decode_buff_size,
data_type);
printf("source_size = %d, out_size = %d, min_decode_buff_size = %d, encrypt_buff_size = %d, encrypted_size = %d\n",
source_size, out_size,min_decode_buff_size, encrypt_buff_size, encrypted_size);
printf("%.2x%.2x\n", source[0], source[1]);
printf(".");fflush(stdout);
// printf("source_size = %d, out_size = %d, min_decode_buff_size = %d, encrypt_buff_size = %d, encrypted_size = %d\n",
// source_size, out_size,min_decode_buff_size, encrypt_buff_size, encrypted_size);
// printf("%.2x%.2x\n", source[0], source[1]);
// printf(".");fflush(stdout);
dap_assert_PIF(source_size == out_size, "Check result decode size");
printf(".");fflush(stdout);
// printf(".");fflush(stdout);
dap_assert_PIF(memcmp(source, decode_result, source_size) == 0, "Check source and encode->decode data");
printf(".");fflush(stdout);
#ifdef xxxxx
// printf(".");fflush(stdout);
//#ifdef xxxxx
#endif
//#endif
DAP_DELETE(decode_result);
DAP_DELETE(encrypt_result);
DAP_DELETE(source);
printf(".");fflush(stdout);
dap_enc_key_delete(key);
printf(".\n");fflush(stdout);
}
}
......@@ -466,9 +463,9 @@ static void test_serealize_deserealize_pub_priv(dap_enc_key_type_t key_type)
void dap_enc_tests_run() {
dap_print_module_name("dap_enc");
init_test_case();
test_encode_decode_raw(500);return;
test_encode_decode_raw_b64(50);
test_encode_decode_raw_b64_url_safe(50);
test_encode_decode_raw(500);
test_encode_decode_raw_b64(500);
test_encode_decode_raw_b64_url_safe(500);
test_key_transfer_msrln();
dap_print_module_name("dap_enc serealize->deserealize IAES");
test_serealize_deserealize(DAP_ENC_KEY_TYPE_IAES);
......
......@@ -19,19 +19,19 @@ int main(void)
// switch off debug info from library
dap_log_level_set(L_CRITICAL);
dap_enc_tests_run();return 0;
dap_enc_tests_run();
dap_enc_ringct20_tests_run(100);
test_encode_decode (100, DAP_ENC_KEY_TYPE_BF_CBC,0);
test_encode_decode_fast (100, DAP_ENC_KEY_TYPE_BF_CBC,0);
test_encode_decode (100, DAP_ENC_KEY_TYPE_BF_OFB,0);
test_encode_decode_fast (100, DAP_ENC_KEY_TYPE_BF_OFB,0);
test_encode_decode (100, DAP_ENC_KEY_TYPE_GOST_OFB,32);
test_encode_decode_fast (100, DAP_ENC_KEY_TYPE_GOST_OFB,32);
test_encode_decode (100, DAP_ENC_KEY_TYPE_IAES,32);
test_encode_decode_fast (100, DAP_ENC_KEY_TYPE_IAES,32);
test_encode_decode (100, DAP_ENC_KEY_TYPE_OAES,32);
test_encode_decode_fast (100, DAP_ENC_KEY_TYPE_OAES,32);
dap_enc_ringct20_tests_run(1000);
test_encode_decode (1000, DAP_ENC_KEY_TYPE_BF_CBC,0);
test_encode_decode_fast (1000, DAP_ENC_KEY_TYPE_BF_CBC,0);
test_encode_decode (1000, DAP_ENC_KEY_TYPE_BF_OFB,0);
test_encode_decode_fast (1000, DAP_ENC_KEY_TYPE_BF_OFB,0);
test_encode_decode (1000, DAP_ENC_KEY_TYPE_GOST_OFB,32);
test_encode_decode_fast (1000, DAP_ENC_KEY_TYPE_GOST_OFB,32);
test_encode_decode (1000, DAP_ENC_KEY_TYPE_IAES,32);
test_encode_decode_fast (1000, DAP_ENC_KEY_TYPE_IAES,32);
test_encode_decode (1000, DAP_ENC_KEY_TYPE_OAES,32);
test_encode_decode_fast (1000, DAP_ENC_KEY_TYPE_OAES,32);
dap_enc_picnic_tests_run();
dap_enc_sig_bliss_tests_run();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment