From 11b2ff584c67cf85ebe2405f6a74ab2799736927 Mon Sep 17 00:00:00 2001 From: Benedict Date: Thu, 4 Aug 2016 14:28:15 +0200 Subject: completed set3, task 18 --- lib/lib3.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/lib3.h | 16 ++++++++++++ set3/Makefile | 14 ++++++++++ set3/task18.c | 20 ++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 lib/lib3.c create mode 100644 lib/lib3.h create mode 100644 set3/Makefile create mode 100644 set3/task18.c diff --git a/lib/lib3.c b/lib/lib3.c new file mode 100644 index 0000000..87865ed --- /dev/null +++ b/lib/lib3.c @@ -0,0 +1,83 @@ +#include "lib3.h" +#include "lib2.h" +#include "lib.h" + + +#define NR_STIRNGS_CHALLENGE17 10 + +char *challenge17_encrypt(int *length) +{ + int i, t; + char **string; + char **decoded; + string = malloc(sizeof(char *)*NR_STIRNGS_CHALLENGE17); + decoded = malloc(sizeof(char *)*NR_STIRNGS_CHALLENGE17); + char filename[] = "task17_0"; + + for(i=0;i<10;i++) { + filename[strlen(filename)-1] = (char) (i+'0'); + t = read_base64_file(filename, &string[i]); + decoded[i] = malloc(t); + length[i] = decode_base64(string[i], decoded[i]); + printf("read: %s\n", string[i]); + } + // choose one randomly + int random = rand() % NR_STIRNGS_CHALLENGE17; + int padding; + printf("plaintext: %s\n", string[random]); + *length = strlen(string[random]); + char *padded_string = __pkcs7_padding(string[random], *length, 16, &padding); + char *encrypted = malloc(strlen(padded_string)); + *length += padding; + aes_cbc(padded_string, strlen(padded_string), encrypted, key, iv, 1); + return encrypted; +} + +int cbc_padding_oracle(char *encrypted, int length) +{ + char *decrypted = malloc(length); + char *unpadded= malloc(length); + + aes_cbc(encrypted, length, decrypted, key, iv, 0); + + int valid = valid_pkcs7_padding(decrypted, length, unpadded, 16); + free(decrypted); + free(unpadded); + return valid; + +} + +int convert_to_little_endian(char *string) +{ + +} +/** + * format is: 64 nonce concat with 64 bit counter + * calle has to make sure that nonce is at least 8 bytes + * its all little endian + */ +int aes_ctr(char *in, int length_in, char *out, char *string_key, char *nonce) +{ + long counter; + unsigned char tmp[16]; + char keystream[16]; + long nr_blocks = length_in / BLOCKSIZE; + int length_last_block = length_in % BLOCKSIZE; + memcpy(tmp, nonce, 16); + + for(counter=0;counter +#include +#include +#include + +#define BLOCKSIZE 16 + +char *challenge17_encrypt(int *length); +int cbc_padding_oracle(char *encrypted, int length); +int aes_ctr(char *in, int length_in, char *out, char *string_key, char *nonce); + + +#endif diff --git a/set3/Makefile b/set3/Makefile new file mode 100644 index 0000000..d3b3f03 --- /dev/null +++ b/set3/Makefile @@ -0,0 +1,14 @@ +LIB=../lib/lib2.c ../lib/lib.c ../lib/lib3.c +CC=gcc +CFLAGS := -g $(CFLAGS) +CLIBS=`pkg-config --cflags --libs libcrypto` + +all: task17 task18 + +task17: + $(CC) $(CFLAGS) task17.c $(LIB) $(CLIBS) -o task17 + +task18: + $(CC) $(CFLAGS) task18.c $(LIB) $(CLIBS) -o task18 +clean: + rm task17 task18 diff --git a/set3/task18.c b/set3/task18.c new file mode 100644 index 0000000..072156a --- /dev/null +++ b/set3/task18.c @@ -0,0 +1,20 @@ +#include "../lib/lib.h" +#include "../lib/lib2.h" +#include "../lib/lib3.h" +#include + +int main() +{ + char *file; + int length_file = read_base64_file("task18.txt", &file); + char *ciphertext = malloc(length_file); + int length = decode_base64(file, ciphertext); + + char *decrypted = malloc(length); + char *key = "YELLOW SUBMARINE"; + char nonce[16]; + memset(nonce, 0, 16); + aes_ctr(ciphertext, length, decrypted, key, nonce); + + printf("plaintext: %s\n", decrypted); +} -- cgit v1.2.3-70-g09d2