@@ -536,35 +536,37 @@ | |||
} | |||
}, | |||
{ | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/enc_modes.c", | |||
"file": "/home/flowher/repos/MatasanoCrypto/sol/set2.c", | |||
"settings": | |||
{ | |||
"buffer_size": 5568, | |||
"buffer_size": 13607, | |||
"line_ending": "Unix" | |||
} | |||
}, | |||
{ | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/pkcs7.h", | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/enc_modes.c", | |||
"settings": | |||
{ | |||
"buffer_size": 406, | |||
"buffer_size": 5568, | |||
"line_ending": "Unix" | |||
} | |||
}, | |||
{ | |||
"file": "/home/flowher/repos/MatasanoCrypto/sol/set2.c", | |||
"file": "/home/flowher/repos/MatasanoCrypto/sol/set2.h", | |||
"settings": | |||
{ | |||
"buffer_size": 13542, | |||
"buffer_size": 549, | |||
"line_ending": "Unix" | |||
} | |||
}, | |||
{ | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/pkcs7.c", | |||
"contents": "Searching 32 files for \"pkcs7_pad\"\n\n~/repos/MatasanoCrypto/main:\n <binary>\n\n/home/flowher/repos/MatasanoCrypto/sol/set2.c:\n 89 char buff2[10];\n 90 \n 91: ret = pkcs7_pad(text1, 5, &buff1, 32);\n 92 CHECK(ret==true, (const unsigned char* const) \"Padding operation failed\");\n 93 ret = memcmp(text1, buff1, 5);\n\n/home/flowher/repos/MatasanoCrypto/src/enc_modes.c:\n 123 if(new_buf_len == attribs->input_len)\n 124 new_buf_len += 16;\n 125: pkcs7_pad(attribs->input, attribs->input_len, &padded_input, new_buf_len);\n 126 }\n 127 else\n\n/home/flowher/repos/MatasanoCrypto/src/pkcs7.c:\n 19 *\n 20 -------------------------------------------------------------------------------- */\n 21: bool pkcs7_pad(const uint8_t* i_buff, size_t i_len, uint8_t** o_buff, size_t i_obuff_len )\n 22 {\n 23 if( (i_obuff_len < i_len ) ||\n\n/home/flowher/repos/MatasanoCrypto/src/pkcs7.h:\n 5 #include <stdint.h>\n 6 \n 7: bool pkcs7_pad( const uint8_t* i_buff,\n 8 size_t i_len,\n 9 uint8_t** o_buff,\n\n7 matches across 5 files\n\n\nSearching 32 files for \"pkcs7_pad\"\n\n~/repos/MatasanoCrypto/main:\n <binary>\n\n/home/flowher/repos/MatasanoCrypto/sol/set2.c:\n 90 char buff2[10];\n 91 \n 92: ret = pkcs7_pad(text1, 5, &p_buff1, 32);\n 93 CHECK(ret==true, (const unsigned char* const) \"Padding operation failed\");\n 94 ret = memcmp(text1, buff1, 5);\n\n/home/flowher/repos/MatasanoCrypto/src/enc_modes.c:\n 123 if(new_buf_len == attribs->input_len)\n 124 new_buf_len += 16;\n 125: pkcs7_pad(attribs->input, attribs->input_len, &padded_input, new_buf_len);\n 126 }\n 127 else\n\n/home/flowher/repos/MatasanoCrypto/src/pkcs7.c:\n 19 *\n 20 -------------------------------------------------------------------------------- */\n 21: bool pkcs7_pad(const uint8_t* i_buff, size_t i_len, uint8_t** o_buff, size_t i_obuff_len )\n 22 {\n 23 if( (i_obuff_len < i_len ) ||\n\n/home/flowher/repos/MatasanoCrypto/src/pkcs7.h:\n 5 #include <stdint.h>\n 6 \n 7: bool pkcs7_pad( const uint8_t* i_buff,\n 8 size_t i_len,\n 9 uint8_t** o_buff,\n\n7 matches across 5 files\n\n\nSearching 32 files for \"pkcs7_unpad\"\n\n~/repos/MatasanoCrypto/main:\n <binary>\n\n/home/flowher/repos/MatasanoCrypto/sol/set2.c:\n 100 \n 101 size_t unpad_len = 0;\n 102: CHECK( true == pkcs7_unpad(buff1, 32, unpadded, &unpad_len),\n 103 (const uint8_t*) \"Unpadding failed\");\n 104 CHECK(unpad_len == 5, (const uint8_t*) \"Unpadded length wrong\");\n\n/home/flowher/repos/MatasanoCrypto/src/pkcs7.c:\n 54 *\n 55 -------------------------------------------------------------------------------- */\n 56: bool pkcs7_unpad( const uint8_t* i_padded_buf,\n 57 size_t i_len,\n 58 uint8_t** o_unpad_buf,\n\n/home/flowher/repos/MatasanoCrypto/src/pkcs7.h:\n 10 size_t i_obuff_len );\n 11 \n 12: bool pkcs7_unpad( const char* i_padded_buf,\n 13 size_t i_len,\n 14 char* o_unpad_buf,\n\n6 matches across 4 files\n", | |||
"settings": | |||
{ | |||
"buffer_size": 907, | |||
"line_ending": "Unix" | |||
"buffer_size": 3214, | |||
"line_ending": "Unix", | |||
"name": "Find Results", | |||
"scratch": true | |||
} | |||
}, | |||
{ | |||
@@ -640,14 +642,22 @@ | |||
"build_varint": "", | |||
"command_palette": | |||
{ | |||
"height": 243.0, | |||
"last_filter": "comme", | |||
"height": 201.0, | |||
"last_filter": "comment", | |||
"selected_items": | |||
[ | |||
[ | |||
"comment", | |||
"Snippet: comment-method-simple" | |||
], | |||
[ | |||
"comme", | |||
"Snippet: comment-simple" | |||
], | |||
[ | |||
"metho", | |||
"Snippet: comment-method" | |||
], | |||
[ | |||
"comm", | |||
"Toggle Comment" | |||
@@ -692,10 +702,6 @@ | |||
"bash", | |||
"Set Syntax: Shell Script (Bash)" | |||
], | |||
[ | |||
"comment", | |||
"Toggle Comment" | |||
], | |||
[ | |||
"snippet", | |||
"Snippet: comment" | |||
@@ -736,10 +742,6 @@ | |||
"method", | |||
"Snippet: comment-method-simple" | |||
], | |||
[ | |||
"metho", | |||
"Snippet: comment-method-simple" | |||
], | |||
[ | |||
"comem", | |||
"Snippet: comment-method" | |||
@@ -1052,16 +1054,18 @@ | |||
], | |||
"file_history": | |||
[ | |||
"/home/flowher/repos/MatasanoCrypto/test.c", | |||
"/home/flowher/repos/MatasanoCrypto/src/pkcs7.c", | |||
"/home/flowher/repos/MatasanoCrypto/src/pkcs7.h", | |||
"/home/flowher/repos/MatasanoCrypto/sol/set2.c", | |||
"/home/flowher/test.c", | |||
"/home/flowher/repos/MatasanoCrypto/src/enc_modes.c", | |||
"/home/flowher/repos/MatasanoCrypto/test.c", | |||
"/home/flowher/repos/MatasanoCrypto/sol/set2.h", | |||
"/home/flowher/repos/OpenCrypto/util/incore", | |||
"/home/flowher/test.cpp", | |||
"/home/flowher/Work/goldfish_env/usr/bin/mkbootimg", | |||
"/home/flowher/repos/MatasanoCrypto/src/common.h", | |||
"/home/flowher/repos/MatasanoCrypto/src/main.cpp", | |||
"/home/flowher/repos/MatasanoCrypto/sol/set2.c", | |||
"/home/flowher/repos/MatasanoCrypto/src/enc_modes.c", | |||
"/home/flowher/repos/MatasanoCrypto/out", | |||
"/home/flowher/repos/MatasanoCrypto/Makefile", | |||
"/home/flowher/repos/OpenCrypto/crypto/evp/evp_enc.c", | |||
@@ -1083,7 +1087,6 @@ | |||
"/home/flowher/repos/MatasanoCrypto/suppressions.valgrind", | |||
"/home/flowher/repos/MatasanoCrypto/tst/utils.h", | |||
"/home/flowher/repos/MatasanoCrypto/src/ecb.c", | |||
"/home/flowher/test.c", | |||
"/home/flowher/.config/sublime-text-3/Packages/User/Preferences.sublime-settings", | |||
"/home/flowher/repos/MatasanoCrypto/prj/Matasano.sublime-project", | |||
"/home/flowher/repos/notes/french/words/words", | |||
@@ -1178,8 +1181,7 @@ | |||
"/home/kkwiatkowski/amadeus_workdir/repos/03_Components/acf_ssl/br_12-0-1/test/src/SSLSocketTest.cpp", | |||
"/home/kkwiatkowski/amadeus_workdir/repos/03_Components/acf_ssl/br_12-0-1/include/acf/ssl/SSLOptions.h", | |||
"/home/kkwiatkowski/amadeus_workdir/repos/03_Components/acf_ssl/br_12-0-1/.bms/bmsrc", | |||
"/home/kkwiatkowski/valgrind.suppr", | |||
"/home/kkwiatkowski/storage/91_Repositories/openssl101j/ssl/ssl.h" | |||
"/home/kkwiatkowski/valgrind.suppr" | |||
], | |||
"find": | |||
{ | |||
@@ -1254,6 +1256,10 @@ | |||
"case_sensitive": false, | |||
"find_history": | |||
[ | |||
"pkcs7_unpad", | |||
"0xCAFECAFE", | |||
"0xDEADBEEF", | |||
"pkcs7_pad", | |||
"aes", | |||
"pkcs7_pad", | |||
"pkcs7", | |||
@@ -1377,11 +1383,7 @@ | |||
"set1/", | |||
"utils/", | |||
"utils", | |||
"RSA_METHOD", | |||
"ENGINE_CMD_DEFN", | |||
"plaintext", | |||
"xor_repeatedly", | |||
" k" | |||
"RSA_METHOD" | |||
], | |||
"highlight": true, | |||
"in_selection": false, | |||
@@ -1447,7 +1449,7 @@ | |||
"groups": | |||
[ | |||
{ | |||
"selected": 4, | |||
"selected": 2, | |||
"sheets": | |||
[ | |||
{ | |||
@@ -1477,7 +1479,7 @@ | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 5, | |||
"stack_index": 8, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1505,24 +1507,24 @@ | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 10, | |||
"stack_index": 3, | |||
"type": "text" | |||
}, | |||
{ | |||
"buffer": 2, | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/enc_modes.c", | |||
"file": "/home/flowher/repos/MatasanoCrypto/sol/set2.c", | |||
"semi_transient": false, | |||
"settings": | |||
{ | |||
"buffer_size": 5568, | |||
"buffer_size": 13607, | |||
"regions": | |||
{ | |||
}, | |||
"selection": | |||
[ | |||
[ | |||
3505, | |||
3505 | |||
12368, | |||
12368 | |||
] | |||
], | |||
"settings": | |||
@@ -1532,35 +1534,37 @@ | |||
"translate_tabs_to_spaces": true | |||
}, | |||
"translation.x": 0.0, | |||
"translation.y": 1716.0, | |||
"translation.y": 6493.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 3, | |||
"stack_index": 0, | |||
"type": "text" | |||
}, | |||
{ | |||
"buffer": 3, | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/pkcs7.h", | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/enc_modes.c", | |||
"semi_transient": false, | |||
"settings": | |||
{ | |||
"buffer_size": 406, | |||
"buffer_size": 5568, | |||
"regions": | |||
{ | |||
}, | |||
"selection": | |||
[ | |||
[ | |||
61, | |||
81 | |||
1423, | |||
1423 | |||
] | |||
], | |||
"settings": | |||
{ | |||
"syntax": "Packages/C Improved/C Improved.tmLanguage" | |||
"syntax": "Packages/C Improved/C Improved.tmLanguage", | |||
"tab_size": 4, | |||
"translate_tabs_to_spaces": true | |||
}, | |||
"translation.x": 0.0, | |||
"translation.y": 0.0, | |||
"translation.y": 1206.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 2, | |||
@@ -1568,19 +1572,19 @@ | |||
}, | |||
{ | |||
"buffer": 4, | |||
"file": "/home/flowher/repos/MatasanoCrypto/sol/set2.c", | |||
"file": "/home/flowher/repos/MatasanoCrypto/sol/set2.h", | |||
"semi_transient": false, | |||
"settings": | |||
{ | |||
"buffer_size": 13542, | |||
"buffer_size": 549, | |||
"regions": | |||
{ | |||
}, | |||
"selection": | |||
[ | |||
[ | |||
13408, | |||
13408 | |||
500, | |||
500 | |||
] | |||
], | |||
"settings": | |||
@@ -1590,38 +1594,97 @@ | |||
"translate_tabs_to_spaces": true | |||
}, | |||
"translation.x": 0.0, | |||
"translation.y": 6561.0, | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 0, | |||
"stack_index": 1, | |||
"type": "text" | |||
}, | |||
{ | |||
"buffer": 5, | |||
"file": "/home/flowher/repos/MatasanoCrypto/src/pkcs7.c", | |||
"semi_transient": false, | |||
"settings": | |||
{ | |||
"buffer_size": 907, | |||
"buffer_size": 3214, | |||
"regions": | |||
{ | |||
"match": | |||
{ | |||
"flags": 112, | |||
"regions": | |||
[ | |||
[ | |||
178, | |||
187 | |||
], | |||
[ | |||
498, | |||
507 | |||
], | |||
[ | |||
764, | |||
773 | |||
], | |||
[ | |||
996, | |||
1005 | |||
], | |||
[ | |||
1314, | |||
1323 | |||
], | |||
[ | |||
1636, | |||
1645 | |||
], | |||
[ | |||
1902, | |||
1911 | |||
], | |||
[ | |||
2134, | |||
2143 | |||
], | |||
[ | |||
2470, | |||
2481 | |||
], | |||
[ | |||
2811, | |||
2822 | |||
], | |||
[ | |||
3060, | |||
3071 | |||
] | |||
], | |||
"scope": "" | |||
} | |||
}, | |||
"selection": | |||
[ | |||
[ | |||
292, | |||
292 | |||
2478, | |||
2478 | |||
] | |||
], | |||
"settings": | |||
{ | |||
"syntax": "Packages/C Improved/C Improved.tmLanguage" | |||
"detect_indentation": false, | |||
"line_numbers": false, | |||
"output_tag": 3, | |||
"result_base_dir": "", | |||
"result_file_regex": "^([A-Za-z\\\\/<].*):$", | |||
"result_line_regex": "^ +([0-9]+):", | |||
"scroll_past_end": true, | |||
"syntax": "Packages/Default/Find Results.hidden-tmLanguage", | |||
"translate_tabs_to_spaces": false | |||
}, | |||
"translation.x": 0.0, | |||
"translation.y": 0.0, | |||
"translation.y": 1207.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 1, | |||
"stack_index": 4, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1651,7 +1714,7 @@ | |||
"translation.y": 685.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 7, | |||
"stack_index": 9, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1680,7 +1743,7 @@ | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 11, | |||
"stack_index": 12, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1708,7 +1771,7 @@ | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 4, | |||
"stack_index": 7, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1736,7 +1799,7 @@ | |||
"translation.y": 1461.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 6, | |||
"stack_index": 5, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1762,10 +1825,10 @@ | |||
"translate_tabs_to_spaces": false | |||
}, | |||
"translation.x": 0.0, | |||
"translation.y": 4111.0, | |||
"translation.y": 4094.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 9, | |||
"stack_index": 11, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1794,7 +1857,7 @@ | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 8, | |||
"stack_index": 10, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1823,7 +1886,7 @@ | |||
"translation.y": 1014.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 12, | |||
"stack_index": 13, | |||
"type": "text" | |||
}, | |||
{ | |||
@@ -1851,7 +1914,7 @@ | |||
"translation.y": 0.0, | |||
"zoom_level": 1.0 | |||
}, | |||
"stack_index": 13, | |||
"stack_index": 6, | |||
"type": "text" | |||
} | |||
] | |||
@@ -65,7 +65,7 @@ TCASE(ecb_encrypt_decrypt_single_block) | |||
memcpy(key1.key, "YELLOW SUBMARINE", 16); | |||
memcpy(encode.input, expected_result, 16); | |||
ecb_encrypt(&encode, &key1); | |||
CHECK(ecb_encrypt(&encode, &key1) == Result_OK); | |||
CHECK(encode.output_len==16); | |||
memcpy(decode.input, encode.output, 16); | |||
@@ -388,35 +388,69 @@ TCASE_E | |||
TCASE(encrypt_padding_pkcs7) | |||
{ | |||
/* char text[] = "ABC"; | |||
char iv[16] = {0}; | |||
uint8_t text[3] = {'D', 'E', 'F'}; | |||
uint8_t expected_no_padding_dec[16] = {0}; | |||
uint8_t iv[16] = {0}; | |||
uint8_t key[16] = {0}; | |||
// "ABC" must be padded with 13,13,13.... | |||
memcpy(expected_no_padding_dec, text, 3); | |||
memset(expected_no_padding_dec+3, 13, 13); | |||
// key | |||
Key_t keyObj = {0}; | |||
Key_t::Init(&keyObj); | |||
keyObj.key = key; | |||
keyObj.len = 16; | |||
CryptoAttribs_t attribs_enc; | |||
CryptoAttribs_t::Init(&attribs_enc); | |||
attribs_enc.input = &text[0]; | |||
attribs_enc.input_len = sizeof(text); | |||
attribs_enc.iv = &iv1[0]; | |||
attribs_enc.iv_len = sizeof(iv1); | |||
attribs_enc.input_len = 3; | |||
attribs_enc.iv = &iv[0]; | |||
attribs_enc.iv_len = sizeof(iv); | |||
attribs_enc.operation = kEncrypt; | |||
attribs_enc.padding = kPadding_PKCS7; | |||
CHECK( Result_OK == cbc_encrypt(&attribs_enc, &keyObj) ); | |||
CHECK( attribs_enc.output_len == 16, (uint8_t*) "Wrong out size"); | |||
CryptoAttribs_t attribs_openssl_dec; | |||
CryptoAttribs_t::Init(&attribs_openssl_dec); | |||
attribs_openssl_dec.input = attribs_enc.output; | |||
attribs_openssl_dec.input_len = attribs_enc.output_len; | |||
attribs_openssl_dec.iv = &iv1[0]; | |||
attribs_openssl_dec.iv_len = sizeof(iv1); | |||
attribs_openssl_dec.operation = kDecrypt; | |||
CHECK(OpenSSL::Cbc(&attribs_openssl_dec, &keyObj)==Result_OK); | |||
CHECK(attribs_openssl_dec.output_len==48, (uint8_t*)"Ciphertext has wrong size"); | |||
CHECK( | |||
memcmp( concatenated_blocks, | |||
attribs_openssl_dec.output, | |||
attribs_openssl_dec.output_len) == 0, | |||
(uint8_t*)"Input/Output differs"); | |||
// Decrypt with openssl and no padding. Check padding value | |||
{ | |||
CryptoAttribs_t attribs_openssl_dec; | |||
CryptoAttribs_t::Init(&attribs_openssl_dec); | |||
attribs_openssl_dec.input = attribs_enc.output; | |||
attribs_openssl_dec.input_len = attribs_enc.output_len; | |||
attribs_openssl_dec.iv = &iv[0]; | |||
attribs_openssl_dec.iv_len = sizeof(iv); | |||
attribs_openssl_dec.operation = kDecrypt; | |||
attribs_openssl_dec.padding = kPadding_None; | |||
CHECK(OpenSSL::Cbc(&attribs_openssl_dec, &keyObj)==Result_OK); | |||
CHECK(attribs_openssl_dec.output_len==16, (uint8_t*)"Ciphertext has wrong size"); | |||
CHECK( 0==memcmp( attribs_openssl_dec.output, | |||
expected_no_padding_dec, | |||
16), (uint8_t*)"Wrong padding decrypted" ); | |||
::free(attribs_openssl_dec.output); | |||
} | |||
// Decrypt padding | |||
{ | |||
CryptoAttribs_t attribs_dec; | |||
CryptoAttribs_t::Init(&attribs_dec); | |||
attribs_dec.input = attribs_enc.output; | |||
attribs_dec.input_len = attribs_enc.output_len; | |||
attribs_dec.iv = &iv[0]; | |||
attribs_dec.iv_len = sizeof(iv); | |||
attribs_dec.operation = kDecrypt; | |||
attribs_dec.padding = kPadding_PKCS7; | |||
CHECK( Result_OK == cbc_decrypt(&attribs_dec, &keyObj) ); | |||
CHECK(attribs_dec.output_len==3, (uint8_t*)"Ciphertext has wrong size"); | |||
CHECK( 0==memcmp( attribs_dec.output, | |||
expected_no_padding_dec, | |||
3), (uint8_t*)"Wrong padding decrypted" ); | |||
::free(attribs_dec.output); | |||
} | |||
::free(attribs_enc.output); | |||
::free(attribs_openssl_dec.output); | |||
*/ | |||
} | |||
TCASE_E |
@@ -16,10 +16,10 @@ struct SET2 | |||
ecb_encrypt_decrypt_single_block(); | |||
pkcs7_test(); | |||
encode_decode_openssl(); | |||
cbc_decrypt_test(); | |||
cbc_enc_dec_test(); | |||
encode_oracle_test(); | |||
encrypt_padding_pkcs7(); | |||
//cbc_decrypt_test(); | |||
//cbc_enc_dec_test(); | |||
//encode_oracle_test(); | |||
//encrypt_padding_pkcs7(); | |||
} | |||
}; | |||
@@ -58,6 +58,7 @@ struct CryptoAttribs_t { | |||
uint8_t* iv; | |||
size_t iv_len; | |||
CryptoOperation operation; | |||
// If requested, padding will align produced block size with key size | |||
PaddingType padding; | |||
static void Init(CryptoAttribs_t* ctx); | |||
@@ -15,16 +15,13 @@ static Result_t crypt( CryptoAttribs_t* attribs, | |||
assert(key != NULL); | |||
assert(attribs != NULL); | |||
assert(key->len == 16); | |||
size_t max_size = ROUNDUP_16_BYTES(attribs->input_len) ; | |||
// if output is NULL then allocate same size as input rounded to block size | |||
if(attribs->output == NULL) | |||
{ | |||
attribs->output = (uint8_t*) malloc( | |||
ROUNDUP_16_BYTES(attribs->input_len) ); | |||
attribs->output_len = attribs->input_len; | |||
attribs->output = (uint8_t*) malloc(max_size+1/* +1 because it's needed in final */); | |||
} | |||
uint8_t* out = attribs->output; | |||
int ret = 0; | |||
EVP_CIPHER_CTX ctx; | |||
EVP_CIPHER_CTX_init(&ctx); | |||
@@ -34,11 +31,14 @@ static Result_t crypt( CryptoAttribs_t* attribs, | |||
(attribs->operation == kEncrypt ? 1 : 0)) ); | |||
OP_CHECK( EVP_CIPHER_CTX_iv_length(&ctx) == 0); | |||
EVP_CIPHER_CTX_set_padding(&ctx, 0); | |||
// printf("ZZ %d\n", ret); | |||
OP_CHECK( EVP_CipherUpdate(&ctx, &out[ret], &ret, attribs->input, attribs->input_len) ); | |||
attribs->output_len = ret; | |||
// printf("OO %d\n", ret); | |||
OP_CHECK( EVP_CipherFinal_ex(&ctx, &out[ret], &ret) ); | |||
OP_CHECK( EVP_CipherUpdate(&ctx, attribs->output, &ret, attribs->input, attribs->input_len) ); | |||
attribs->output_len += ret; | |||
if(ret > max_size ) | |||
{ | |||
printf("ERROR %d %lu \n", ret, attribs->output_len); | |||
goto end; | |||
} | |||
OP_CHECK( EVP_CipherFinal_ex(&ctx, &attribs->output[ret], &ret) ); | |||
attribs->output_len += ret; | |||
EVP_CIPHER_CTX_cleanup(&ctx); | |||
return Result_OK; | |||
@@ -51,6 +51,9 @@ Result_t cbc_decrypt( | |||
CryptoAttribs_t* attribs, | |||
const Key_t* const key) | |||
{ | |||
if( NULL == attribs->iv ) | |||
return Result_Error; | |||
if((attribs->input_len % key->len) != 0 ) | |||
return Result_Error; | |||
@@ -60,13 +63,13 @@ Result_t cbc_decrypt( | |||
if( key->len == 0 ) | |||
return Result_Error; | |||
const size_t bc = attribs->input_len / key->len; | |||
const size_t bc = (size_t) (attribs->input_len / key->len); | |||
// Allocate memory for output, if NULL | |||
if( attribs->output == NULL) | |||
{ | |||
// same as input + 16 bytes iv | |||
const size_t len = (ROUNDUP_16_BYTES(attribs->input_len)+16); | |||
const size_t len = (ROUNDUP_16_BYTES(attribs->input_len)); | |||
attribs->output = (uint8_t*) malloc(len); | |||
assert( attribs->output != NULL ); | |||
assert( len != 0 ); | |||
@@ -100,7 +103,17 @@ Result_t cbc_decrypt( | |||
iv = ecb_attribs.input; | |||
attribs->output_len += key->len; | |||
} | |||
free(ecb_attribs.output); | |||
::free(ecb_attribs.output); | |||
if(attribs->padding == kPadding_PKCS7) | |||
{ | |||
uint8_t* unpadded = NULL; | |||
size_t length; | |||
pkcs7_unpad(attribs->output, attribs->output_len, &unpadded, &length); | |||
memcpy(attribs->output, unpadded, length); | |||
attribs->output_len = length; | |||
} | |||
return Result_OK; | |||
} | |||
@@ -114,32 +127,34 @@ Result_t cbc_encrypt( | |||
if( key->len == 0 ) | |||
return Result_Error; | |||
uint8_t* padded_input = NULL; | |||
size_t new_buf_len = 0; | |||
uint8_t* local_input = NULL; | |||
size_t local_input_len = 0; | |||
if( attribs->padding == kPadding_PKCS7 ) | |||
{ | |||
new_buf_len = ROUNDUP_16_BYTES(attribs->input_len); | |||
// TODO: it should round up to key length not necsairlly 16 | |||
local_input_len = ROUNDUP_16_BYTES(attribs->input_len); | |||
// one block extra if size aligned | |||
if(new_buf_len == attribs->input_len) | |||
new_buf_len += 16; | |||
pkcs7_pad(attribs->input, attribs->input_len, &padded_input, new_buf_len); | |||
if(local_input_len == attribs->input_len) | |||
local_input_len += 16; | |||
pkcs7_pad(attribs->input, attribs->input_len, &local_input, local_input_len); | |||
} | |||
else | |||
{ | |||
if((attribs->input_len % key->len) != 0 ) | |||
if((attribs->input_len % key->len) != 0 ) | |||
return Result_Error; | |||
local_input_len = attribs->input_len; | |||
local_input = (uint8_t*) malloc(local_input_len); | |||
memcpy(local_input, attribs->input, local_input_len); | |||
} | |||
const size_t bc_without_last = (size_t)( attribs->input_len / key->len ); | |||
const size_t bc = bc_without_last + 1; | |||
const size_t bc = (size_t)( local_input_len / key->len ); | |||
const size_t bs = key->len; | |||
const size_t last_bs = attribs->input_len % bs; | |||
if( attribs->output == NULL) | |||
{ | |||
// same as input + 16 bytes iv | |||
attribs->output = (uint8_t*) malloc( | |||
(ROUNDUP_16_BYTES(attribs->input_len)+16) ); | |||
(ROUNDUP_16_BYTES(local_input_len)+16) ); | |||
assert( attribs->output != NULL ); | |||
} | |||
uint8_t* iv = attribs->iv; | |||
@@ -153,7 +168,7 @@ Result_t cbc_encrypt( | |||
Result_t ret = Result_OK; | |||
for(int i=0; i<bc; ++i) | |||
{ | |||
uint8_t* buffptr = (attribs->input)+(bs*i); | |||
uint8_t* buffptr = (local_input)+(bs*i); | |||
// 1. Xor IV | |||
xor_repeatedly( | |||
@@ -180,7 +195,7 @@ Result_t cbc_encrypt( | |||
attribs->output_len += ecb_attribs.output_len; | |||
} | |||
CryptoAttribs_t::Free(&ecb_attribs); | |||
::free(padded_input); | |||
::free(local_input); | |||
return ret; | |||
} | |||
@@ -1,4 +1,5 @@ | |||
#include <string.h> | |||
#include <assert.h> | |||
void xor_repeatedly( | |||
const unsigned char* const i_xor, const unsigned i_xor_len, | |||
@@ -6,6 +7,7 @@ void xor_repeatedly( | |||
unsigned char* o_xored) | |||
{ | |||
unsigned counter = 0; | |||
assert(i_string_len == i_xor_len); | |||
while( counter < i_string_len ) | |||
{ | |||
unsigned xor_idx = counter % i_xor_len; | |||
@@ -68,7 +68,7 @@ void xor_char_finder(const unsigned char* const p_ciphertext_xor, struct frequen | |||
{ | |||
for(int i=0; i<xorable; ++i) | |||
{ | |||
xor_repeatedly(&xorable_chars[i], 1, p_ciphertext_xor, ciphertext_len, plaintext); | |||
xor_repeatedly(&xorable_chars[i], 1, p_ciphertext_xor, 1/*ciphertext_len*/, plaintext); | |||
unsigned long long score = frequency_analysis(plaintext, ciphertext_len); | |||
if(o_frequency.score > 0 && (score == o_frequency.score) ) | |||
@@ -153,6 +153,7 @@ int crack_repeted_xor(const char* const i_hex_string, unsigned char* o_buf, cons | |||
// printf("> Score %d %c\n", max_score.score, max_score.letter); | |||
found_key[j]=max_score.letter; | |||
} | |||
assert(keysize == len); | |||
xor_repeatedly(found_key, keysize, ciphertext_xor, len, o_buf); | |||
delete [] slide; | |||