瀏覽代碼

Set2 t2 - init

api_change
Krzysztof Kwiatkowski 9 年之前
父節點
當前提交
c1399d6991
共有 9 個文件被更改,包括 177 次插入179 次删除
  1. +3
    -0
      MatsanoCrypto
  2. +76
    -149
      MatsanoCrypto.sublime-workspace
  3. +6
    -5
      set1/ecb.c
  4. +37
    -9
      set2/cbc.c
  5. +25
    -3
      set2/runner.c
  6. +3
    -1
      set2/runner.h
  7. +15
    -11
      utils/common.c
  8. +2
    -0
      utils/common.h
  9. +10
    -1
      utils/xor.h

+ 3
- 0
MatsanoCrypto 查看文件

@@ -3,6 +3,9 @@
[
{
"path": "."
},
{
"path": "/home/flowher/repos/notes"
}
],
"settings":


+ 76
- 149
MatsanoCrypto.sublime-workspace 查看文件

@@ -3,6 +3,10 @@
{
"selected_items":
[
[
"base",
"base64_to_hex"
],
[
"count",
"count_array_size"
@@ -510,10 +514,6 @@
[
"onRece",
"onInitReceived"
],
[
"subsc",
"subscriber"
]
]
},
@@ -543,23 +543,6 @@
"line_ending": "Unix"
}
},
{
"file": "/home/flowher/backup_server",
"settings":
{
"buffer_size": 743,
"line_ending": "Unix"
}
},
{
"file": "/home/flowher/server_snapshot.sh",
"settings":
{
"buffer_size": 1305,
"line_ending": "Unix",
"name": "#!/bin/bash"
}
},
{
"file": "set1/ecb.c",
"settings":
@@ -588,7 +571,7 @@
"file": "utils/common.c",
"settings":
{
"buffer_size": 881,
"buffer_size": 1200,
"line_ending": "Unix"
}
},
@@ -596,7 +579,7 @@
"file": "utils/base64.cpp",
"settings":
{
"buffer_size": 8064,
"buffer_size": 8075,
"line_ending": "Unix"
}
},
@@ -687,10 +670,18 @@
"build_varint": "",
"command_palette":
{
"height": 54.0,
"last_filter": "bash",
"height": 392.0,
"last_filter": "markdown",
"selected_items":
[
[
"markdown",
"Markdown Preview: Preview in Browser"
],
[
"markdow",
"Markdown Preview: Preview in Browser"
],
[
"bash",
"Set Syntax: Shell Script (Bash)"
@@ -719,10 +710,6 @@
"git diff",
"Git: Diff All Files"
],
[
"markdown",
"Markdown Preview: Preview in Browser"
],
[
"mard",
"Markdown Preview: Preview in Browser"
@@ -1048,7 +1035,7 @@
"Package Control: Install Package"
]
],
"width": 467.0
"width": 575.0
},
"console":
{
@@ -1071,13 +1058,15 @@
},
"expanded_folders":
[
"/home/flowher/repos/MatasanoCrypto",
"/home/flowher/repos/MatasanoCrypto/set1",
"/home/flowher/repos/MatasanoCrypto/set2",
"/home/flowher/repos/MatasanoCrypto/utils"
"/home/flowher/repos/notes",
"/home/flowher/repos/notes/hacking"
],
"file_history":
[
"/home/flowher/backup_server",
"/home/flowher/repos/notes/hacking/WPA.md",
"/home/flowher/repos/notes/hacking/WPA.mde",
"/home/flowher/server_snapshot.sh",
"/home/flowher/repos/MatasanoCrypto/utils/common.h",
"/home/flowher/repos/MatasanoCrypto/set1/xor_char_finder.h",
"/home/flowher/repos/MatasanoCrypto/utils/common.c",
@@ -1201,11 +1190,7 @@
"/home/kkwiatkowski/storage/91_Repositories/openssl101j/doc/crypto/SMIME_read_PKCS7.pod",
"/home/kkwiatkowski/storage/91_Repositories/openssl101j/apps/s512-req.pem",
"/home/kkwiatkowski/storage/91_Repositories/openssl101j/Netware/set_env.bat",
"/home/kkwiatkowski/amadeus_workdir/repos/03_Components/acf_ssl/br_12-0-1/src/SSLSocket.cpp",
"/home/kkwiatkowski/amadeus_workdir/repos/Tracer_br_12-0-0/test/data/Test100_shared",
"/home/kkwiatkowski/storage/91_Repositories/openssl101j/crypto/cryptlib.c",
"/home/kkwiatkowski/storage/91_Repositories/openssl101j/tools/c_rehash",
"/home/kkwiatkowski/storage/91_Repositories/srp_client_server_implementation_with_openssl/client.cpp"
"/home/kkwiatkowski/amadeus_workdir/repos/03_Components/acf_ssl/br_12-0-1/src/SSLSocket.cpp"
],
"find":
{
@@ -1216,6 +1201,7 @@
"height": 90.0,
"where_history":
[
"/home/flowher/repos/MatasanoCrypto/utils",
"/home/kkwiatkowski/amadeus_workdir/repos/02_SI/br_5-1/test/python/si",
"/home/kkwiatkowski/amadeus_workdir/repos/03_Components/CMG/CMG_br_12-0",
"/home/kkwiatkowski/storage/91_Repositories/openssl101j",
@@ -1278,6 +1264,7 @@
"case_sensitive": false,
"find_history":
[
"base64_to_hex",
"void",
"VOLUME_GROUPE",
"count",
@@ -1404,8 +1391,7 @@
"SocketTest",
"handleClient",
"runClientHandler",
"handleClient",
"SAFE_BOOST_CHECK"
"handleClient"
],
"highlight": true,
"in_selection": false,
@@ -1468,7 +1454,7 @@
"groups":
[
{
"selected": 8,
"selected": 0,
"sheets":
[
{
@@ -1484,8 +1470,8 @@
"selection":
[
[
0,
0
5533,
5533
]
],
"settings":
@@ -1497,7 +1483,7 @@
"translation.y": 2418.0,
"zoom_level": 1.0
},
"stack_index": 13,
"stack_index": 0,
"type": "text"
},
{
@@ -1525,7 +1511,7 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 1,
"stack_index": 7,
"type": "text"
},
{
@@ -1550,73 +1536,14 @@
"syntax": "Packages/C Improved/C Improved.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 57.0,
"zoom_level": 1.0
},
"stack_index": 10,
"type": "text"
},
{
"buffer": 3,
"file": "/home/flowher/backup_server",
"semi_transient": false,
"settings":
{
"buffer_size": 743,
"regions":
{
},
"selection":
[
[
743,
743
]
],
"settings":
{
"syntax": "Packages/Text/Plain text.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 11,
"type": "text"
},
{
"buffer": 4,
"file": "/home/flowher/server_snapshot.sh",
"semi_transient": false,
"settings":
{
"buffer_size": 1305,
"regions":
{
},
"selection":
[
[
898,
898
]
],
"settings":
{
"auto_name": "#!/bin/bash",
"syntax": "Packages/ShellScript/Shell-Unix-Generic.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 19.0,
"zoom_level": 1.0
},
"stack_index": 8,
"stack_index": 1,
"type": "text"
},
{
"buffer": 5,
"buffer": 3,
"file": "set1/ecb.c",
"semi_transient": false,
"settings":
@@ -1642,11 +1569,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 7,
"stack_index": 11,
"type": "text"
},
{
"buffer": 6,
"buffer": 4,
"file": "set2/cbc.c",
"semi_transient": false,
"settings":
@@ -1658,8 +1585,8 @@
"selection":
[
[
276,
276
459,
459
]
],
"settings":
@@ -1670,11 +1597,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 3,
"stack_index": 5,
"type": "text"
},
{
"buffer": 7,
"buffer": 5,
"file": "utils/pkcs7.h",
"semi_transient": false,
"settings":
@@ -1702,20 +1629,20 @@
"type": "text"
},
{
"buffer": 8,
"buffer": 6,
"file": "utils/common.c",
"semi_transient": false,
"settings":
{
"buffer_size": 881,
"buffer_size": 1200,
"regions":
{
},
"selection":
[
[
690,
690
60,
60
]
],
"settings":
@@ -1727,24 +1654,24 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 0,
"stack_index": 3,
"type": "text"
},
{
"buffer": 9,
"buffer": 7,
"file": "utils/base64.cpp",
"semi_transient": false,
"settings":
{
"buffer_size": 8064,
"buffer_size": 8075,
"regions":
{
},
"selection":
[
[
7735,
7735
0,
0
]
],
"settings":
@@ -1753,14 +1680,14 @@
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 2387.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 9,
"stack_index": 2,
"type": "text"
},
{
"buffer": 10,
"buffer": 8,
"file": "set1/xor_char_finder.h",
"semi_transient": false,
"settings":
@@ -1785,11 +1712,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 18,
"stack_index": 16,
"type": "text"
},
{
"buffer": 11,
"buffer": 9,
"file": "set2/runner.c",
"semi_transient": false,
"settings":
@@ -1801,8 +1728,8 @@
"selection":
[
[
925,
925
924,
924
]
],
"settings":
@@ -1815,11 +1742,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 2,
"stack_index": 4,
"type": "text"
},
{
"buffer": 12,
"buffer": 10,
"semi_transient": false,
"settings":
{
@@ -1843,11 +1770,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 19,
"stack_index": 17,
"type": "text"
},
{
"buffer": 13,
"buffer": 11,
"file": "set1/xor_char_finder.cpp",
"semi_transient": false,
"settings":
@@ -1872,11 +1799,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 14,
"stack_index": 13,
"type": "text"
},
{
"buffer": 14,
"buffer": 12,
"file": "utils/runner.cpp",
"semi_transient": false,
"settings":
@@ -1888,8 +1815,8 @@
"selection":
[
[
2312,
2312
3736,
3736
]
],
"settings":
@@ -1898,14 +1825,14 @@
"translate_tabs_to_spaces": false
},
"translation.x": 0.0,
"translation.y": 789.0,
"translation.y": 1102.0,
"zoom_level": 1.0
},
"stack_index": 15,
"stack_index": 6,
"type": "text"
},
{
"buffer": 15,
"buffer": 13,
"file": "main.cpp",
"semi_transient": false,
"settings":
@@ -1929,11 +1856,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 16,
"stack_index": 14,
"type": "text"
},
{
"buffer": 16,
"buffer": 14,
"file": "utils/runner.h",
"semi_transient": false,
"settings":
@@ -1957,11 +1884,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 17,
"stack_index": 15,
"type": "text"
},
{
"buffer": 17,
"buffer": 15,
"file": "set1/runner.h",
"semi_transient": false,
"settings":
@@ -1985,11 +1912,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 5,
"stack_index": 9,
"type": "text"
},
{
"buffer": 18,
"buffer": 16,
"file": "set2/runner.h",
"semi_transient": false,
"settings":
@@ -2013,11 +1940,11 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 4,
"stack_index": 8,
"type": "text"
},
{
"buffer": 19,
"buffer": 17,
"file": "Makefile",
"semi_transient": false,
"settings":
@@ -2041,7 +1968,7 @@
"translation.y": 0.0,
"zoom_level": 1.0
},
"stack_index": 6,
"stack_index": 10,
"type": "text"
}
]


+ 6
- 5
set1/ecb.c 查看文件

@@ -11,10 +11,11 @@ uint32_t ecb_decrypt( const unsigned char* i_stream,
int ret, ret_fin;
ret=ret_fin=0;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);

EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL);
EVP_CipherInit_ex(&ctx, EVP_aes_128_ecb(), NULL, i_pass, NULL, 0);
EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size);
EVP_DecryptInit (&ctx, NULL, i_pass, NULL);
EVP_DecryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size);
EVP_DecryptFinal (&ctx, o_buff+ret, &ret_fin);
return ret+ret_fin;
@@ -30,10 +31,10 @@ uint32_t ecb_encrypt( const unsigned char* i_stream,
int ret, ret_fin;
ret=ret_fin=0;
EVP_CIPHER_CTX ctx;

EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL);
EVP_CIPHER_CTX_init(&ctx);
EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size);
EVP_EncryptInit (&ctx, NULL, i_pass, NULL);

EVP_EncryptInit (&ctx, EVP_aes_128_ecb(), i_pass, NULL);
EVP_EncryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size);
EVP_EncryptFinal (&ctx, o_buff+ret, &ret_fin);
return ret+ret_fin;


+ 37
- 9
set2/cbc.c 查看文件

@@ -1,21 +1,49 @@
#include "set1/ecb.h"
#include "utils/xor.h"
#include "utils/common.h"
#include "set1/ecb.h"
#include <stdio.h>

Result_t decrypt_cbc( uint8_t* input_buff,
size_t buff_len,
uint8_t* output_buff,
size_t* result_len)
Result_t decrypt_cbc( uint8_t* iv,
size_t iv_len,
uint8_t* input_buff,
size_t buff_len,
uint8_t* key,
size_t key_len,
uint8_t** output_buff,
size_t* result_len)
{
uint8_t iv[16] = {0};
if(buff_len % key_len != 0 )
return Result_Error;
size_t bc = buff_len / key_len;
uint8_t tmpbuff[key_len];

for(int i=0; i<bc; ++i)
{
uint8_t* i_buff_ptr = input_buff+(bc*i);
uint8_t* o_buff_ptr = *output_buff+(bc*i);
// 1. Decrypt
ecb_decrypt(i_buff_ptr, key_len /*block size*/, key, key_len, tmpbuff);

// 2. Xor IV
xor_repeatedly(iv, iv_len, tmpbuff, key_len, (unsigned char*) o_buff_ptr);

// 3. Change iv with block
iv = i_buff_ptr;
printf("T");
}
return Result_OK;
}


Result_t encrypt_cbc( uint8_t* input_buff,
size_t buff_len,
uint8_t* output_buff,
size_t* result_len)
Result_t encrypt_cbc( uint8_t* iv,
size_t iv_len,
uint8_t* input_buff,
size_t buff_len,
uint8_t* key,
size_t key_len,
uint8_t** output_buff,
size_t* result_len)
{

}

+ 25
- 3
set2/runner.c 查看文件

@@ -5,6 +5,27 @@
#include <string.h>
#include "cbc.h"

struct OpenSSL
{
static void EncryptCbc(const unsigned char* i_stream,
const uint32_t i_stream_size,
const unsigned char* i_pass,
const uint32_t i_pass_size,
unsigned char* o_buff)
{
/* int ret, ret_fin;
ret=ret_fin=0;
EVP_CIPHER_CTX ctx;

EVP_DecryptInit(&ctx, EVP_aes_128_ecb(), NULL, NULL);
EVP_CIPHER_CTX_set_key_length(&ctx, i_pass_size);
EVP_EncryptInit (&ctx, NULL, i_pass, NULL);
EVP_EncryptUpdate(&ctx, o_buff, &ret, i_stream, i_stream_size);
EVP_EncryptFinal (&ctx, o_buff+ret, &ret_fin);
return ret+ret_fin;
*/ }
};

FUNC(pkcs7_test)
{
// int pkcs7_pad(const char* i_buff, const size_t i_len, char* o_buff );
@@ -25,15 +46,16 @@ FUNC(pkcs7_test)
}
FUNC_E

FUNC(cbc_decrypt)
FUNC(cbc_decrypt_test)
{
uint8_t iv[16] = {0};
char key[17] = "YELLOW SUBMARINE"; // 16+'\0' = 17
uint8_t* plaintext = NULL;
uint8_t key[17] = "YELLOW SUBMARINE"; // 16+'\0' = 17
size_t len = 0;
uint8_t buff[4096] = {0};
Result_t res = Result_Error;

res = load_base64_to_hex("etc/set2_t2.txt", buff, &len);
decrypt_cbc(iv, 16, buff, len, key, 16, &plaintext, &len);
}
FUNC_E

+ 3
- 1
set2/runner.h 查看文件

@@ -2,13 +2,15 @@
#define __set2_runner__

void pkcs7_test();
void cbc_decrypt_test();

struct SET2
{
static void run()
{
pkcs7_test();
}
cbc_decrypt_test();
}
};




+ 15
- 11
utils/common.c 查看文件

@@ -1,6 +1,9 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "common.h"
#include "base64.h"

unsigned long read_file_to_buffer( const char* const filepath,
char* obuff,
@@ -17,13 +20,13 @@ unsigned long read_file_to_buffer( const char* const filepath,
exit(1);
}

if(obuff=NULL)
if(NULL==obuff)
{
//allocate
fseek(fh, 0, SEEK_END);
int pos = ftell(fh);
fseek(fh, 0, SEEK_CUR);
obuff = malloc(pos);
fseek(fh, 0, SEEK_SET);
obuff = (char*) malloc(pos);
}

while( getline(&line, &len, fh) != -1 )
@@ -34,24 +37,25 @@ unsigned long read_file_to_buffer( const char* const filepath,

strncpy(obuff+total_len, line, len);
total_len+=len;
free(line);
if(line!=NULL)
free(line);
line=NULL;
len=0;
}
return total_len;
}

Result_t load_base64_to_hex(const char* const filepath, uint8_t* hex_buffer, size_t* len)
{
uint8_t* base64_buff;
char base64_buff[4000];
*len = read_file_to_buffer(filepath, base64_buff, 1);
/// int base64_to_hex(const unsigned char* const i_string,
//int i_string_len,
//unsigned char* o_hex_array );
base64_to_hex(base64_buff, *len, hex_buffer);


if(*len == 0)
return Result_Error;
base64_to_hex((unsigned char*) base64_buff, *len, hex_buffer);
return Result_OK;
}

// this is needed for tests
void check(int iFlag, const unsigned char* const msg)
{
if(!iFlag)


+ 2
- 0
utils/common.h 查看文件

@@ -2,6 +2,7 @@
#define __common_h__

#include <stddef.h>
#include <stdint.h>

#define FUNC(x) void x() { printf("Entering %s\n", __func__);
#define FUNC_E /*printf("Test done\n");*/ }
@@ -15,5 +16,6 @@ typedef enum
void check(int iFlag, const unsigned char* const msg = 0);
struct int24 { unsigned int data:24; };
unsigned long read_file_to_buffer(const char* const filepath, char* obuff, int remove_new_line_chr = 1);
Result_t load_base64_to_hex(const char* const filepath, uint8_t* hex_buffer, size_t* len);

#endif // __common_h__

+ 10
- 1
utils/xor.h 查看文件

@@ -1 +1,10 @@
void xor_repeatedly(const unsigned char* const i_xor, const unsigned i_xor_len, const unsigned char* const i_string, const unsigned i_string_len, unsigned char* o_xored);
#ifndef __xor_h__
#define __xor_h__

void xor_repeatedly(const unsigned char* const i_xor,
const unsigned i_xor_len,
const unsigned char* const i_string,
const unsigned i_string_len,
unsigned char* o_xored);

#endif // __xor_h__

Loading…
取消
儲存