summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.c27
-rw-r--r--lib/lib.h2
-rw-r--r--lib/lib2.c10
-rw-r--r--lib/lib2.h1
-rw-r--r--set1/task6.c28
-rw-r--r--set3/Makefile8
-rw-r--r--set3/task20.c64
7 files changed, 106 insertions, 34 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 7c8beae..0e02726 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -555,4 +555,31 @@ int string_is_ecb_encrypted(char *string, int length_string, int blocksize)
return (count_equal_blocks(string, length_string, blocksize) > (length_string/blocksize));
}
+/**
+* split ciphertext in blocks of size blocksize
+ * @return returns an array of string of size with blocksize elements
+ */
+
+
+char **transpose_blocks(char *ciphertext, int blocksize, int length)
+{
+ char **blocks;
+ int i, j;
+ int number_blocks = length / blocksize;
+
+ blocks = malloc(blocksize*(sizeof(char*)));
+
+ for(i=0;i<blocksize;i++) {
+ blocks[i] = malloc(number_blocks+1);
+ }
+
+ for(j=0;j<blocksize;j++) {
+ for(i=0;i<number_blocks;i++) {
+ blocks[j][i] = ciphertext[(i*blocksize)+j];
+ }
+ }
+
+ return blocks;
+}
+
diff --git a/lib/lib.h b/lib/lib.h
index 8e384e8..a561442 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -24,6 +24,6 @@ char brute_force_single_byte_xor(char *string, int length, struct key_and_freq *
int isprintable(char *string, int length);
int read_base64_file(const char *file, char **out);
int string_is_ecb_encrypted(char *string, int length_string, int blocksize);
-
+char **transpose_blocks(char *ciphertext, int blocksize, int length);
#endif /* __CYRPTO_LIB__ */
diff --git a/lib/lib2.c b/lib/lib2.c
index c7e6ebb..e447054 100644
--- a/lib/lib2.c
+++ b/lib/lib2.c
@@ -6,12 +6,11 @@
* and append to last block so that it is also of blocksize length
*/
-char *pkcs7_padding(char *string, int length_string, int blocksize)
+char *__pkcs7_padding(char *string, int length_string, int blocksize, int *padding)
{
char *result = NULL;
int i;
int value = blocksize - (length_string % blocksize);
-
result = malloc(length_string+value+1);
memcpy(result, string, length_string);
@@ -19,10 +18,15 @@ char *pkcs7_padding(char *string, int length_string, int blocksize)
result[length_string+i] = (char) value;
}
result[length_string+i] = '\0';
-
+ *padding = value;
return result;
}
+char *pkcs7_padding(char *string, int length_string, int blocksize)
+{
+ int not_interested;
+ return __pkcs7_padding(string, length_string, blocksize, &not_interested);
+}
/**
* unpadd a string
* @param in string which should be unpadded
diff --git a/lib/lib2.h b/lib/lib2.h
index f0a1a26..e5666ea 100644
--- a/lib/lib2.h
+++ b/lib/lib2.h
@@ -15,6 +15,7 @@ char key[17];
char iv[17];
char *pkcs7_padding(char *string, int length_string, int blocksize);
+char *__pkcs7_padding(char *string, int length_string, int blocksize, int *padding);
int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector, int encrypt);
int valid_pkcs7_padding(const char *in, int length_in, char *unpadded, int blocksize);
int aes_ecb(char *in, int length_in, char *out, unsigned char *string_key,
diff --git a/set1/task6.c b/set1/task6.c
index 10e19b2..a6a4bfb 100644
--- a/set1/task6.c
+++ b/set1/task6.c
@@ -1,33 +1,5 @@
#include "../lib/lib.h"
-/**
- * split ciphertext in blocks of size blocksize
- * @return returns an array of string of size with blocksize elements
- */
-
-
-char **transpose_blocks(char *ciphertext, int blocksize, int length)
-{
- char **blocks;
- int i, j;
- int number_blocks = length / blocksize;
-
- blocks = malloc(blocksize*(sizeof(char*)));
-
- for(i=0;i<blocksize;i++) {
- blocks[i] = malloc(number_blocks+1);
- }
-
- for(j=0;j<blocksize;j++) {
- for(i=0;i<number_blocks;i++) {
- blocks[j][i] = ciphertext[(i*blocksize)+j];
- }
- }
-
- return blocks;
-}
-
-
int main()
{
int keysize;
diff --git a/set3/Makefile b/set3/Makefile
index d3b3f03..a6ed5fc 100644
--- a/set3/Makefile
+++ b/set3/Makefile
@@ -3,12 +3,16 @@ CC=gcc
CFLAGS := -g $(CFLAGS)
CLIBS=`pkg-config --cflags --libs libcrypto`
-all: task17 task18
+all: task17 task18 task19 task20
task17:
$(CC) $(CFLAGS) task17.c $(LIB) $(CLIBS) -o task17
task18:
$(CC) $(CFLAGS) task18.c $(LIB) $(CLIBS) -o task18
+task19:
+ $(CC) $(CFLAGS) task19.c $(LIB) $(CLIBS) -o task19
+task20:
+ $(CC) $(CFLAGS) task20.c $(LIB) $(CLIBS) -o task20
clean:
- rm task17 task18
+ rm task17 task18 task19 task20
diff --git a/set3/task20.c b/set3/task20.c
new file mode 100644
index 0000000..7ab24de
--- /dev/null
+++ b/set3/task20.c
@@ -0,0 +1,64 @@
+#include "../lib/lib.h"
+#include "../lib/lib2.h"
+#include "../lib/lib3.h"
+#include <time.h>
+
+#define CHALLENGE20_FILE_NR 60
+int main()
+{
+ int i;
+ char **file = malloc(sizeof(char *)*CHALLENGE20_FILE_NR);
+ int length_file[CHALLENGE20_FILE_NR];
+ int length[CHALLENGE20_FILE_NR];
+ char **plaintext= malloc(sizeof(char *)*CHALLENGE20_FILE_NR);
+ char **ciphertext= malloc(sizeof(char *)*CHALLENGE20_FILE_NR);
+ char filename[] = "./task20_data/task20_00";
+
+ char nonce[16];
+ int min_length = 9999999;
+ generate_random_bytes(key, 16);
+ generate_random_bytes(nonce, 16);
+
+ for(i=0;i<CHALLENGE20_FILE_NR;i++) {
+ filename[strlen(filename)-1] = ((i % 10)+ '0');
+ filename[strlen(filename)-2] = ((i / 10) + '0');
+ length_file[i] = read_base64_file(filename, &file[i]);
+ plaintext[i] = malloc(length_file[i]);
+ length[i] = decode_base64(file[i], plaintext[i]);
+ if(length[i] < min_length)
+ min_length = length[i];
+ ciphertext[i] = malloc(length[i]);
+ aes_ctr(plaintext[i], length[i], ciphertext[i], key, nonce);
+ }
+
+ printf("smallest string : %i\n", min_length);
+ // let the cracking begin
+
+ // merge all the chunks and then do the vingere chiffre break
+ // so we have 60 blocks and the key length is min_length
+ // no need
+ char *merged = malloc(min_length*CHALLENGE20_FILE_NR);
+ for(i=0;i<CHALLENGE20_FILE_NR;i++) {
+ memcpy(&merged[i*min_length], ciphertext[i], min_length);
+ }
+
+ char **blocks = transpose_blocks(merged, min_length,
+ (min_length*CHALLENGE20_FILE_NR));
+
+ // run the normal ceasr chiffre attack
+ char restored_keystream[min_length];
+ struct key_and_freq tmp_NOT_USED_HERE;
+ for(i=0;i<min_length;i++) {
+ restored_keystream[i] = brute_force_single_byte_xor(
+ blocks[i], min_length, &tmp_NOT_USED_HERE);
+ }
+
+ // xor ciphertext against keystream
+ char *restored_plaintext = malloc(min_length);
+ for(i=0;i<CHALLENGE20_FILE_NR;i++) {
+ xor_string(ciphertext[i], restored_keystream, restored_plaintext,
+ min_length, min_length);
+ printf("%s\n", plaintext[i]);
+ printf("%s\n", restored_plaintext);
+ }
+}