summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBenedict <benedict@0xb8000.de>2016-03-21 21:32:46 +0100
committerBenedict <benedict@0xb8000.de>2017-02-21 13:00:25 +0100
commit27a1b8050d2170b40024fe82cb27088f3c676f26 (patch)
treec27966b7aeb1e4eb4fe889e6b7fc9a6cd68a5b17 /lib
parent95b1e82c4fca864600108d4e36ceadbb290a76f3 (diff)
completed set2, challenge 10
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c51
-rw-r--r--lib/lib.h2
-rw-r--r--lib/lib2.c39
-rw-r--r--lib/lib2.h5
4 files changed, 94 insertions, 3 deletions
diff --git a/lib/lib.c b/lib/lib.c
index c2ee753..0eb5be8 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -471,3 +471,54 @@ char brute_force_single_byte_xor(char *string, int length, struct key_and_freq *
return key;
}
+
+
+/**
+ * reads a base64 an decode it
+ * @param file path of the file
+ * @param out writes file in this buffer, allocated by this method
+ */
+int read_base64_file(const char *file, char **out)
+{
+ char ch;
+ FILE *fp;
+ int file_size = 60;
+ char *new_file_content;
+ int file_pos = 0;
+
+ fp = fopen(file, "r");
+
+ if (fp == NULL) {
+ printf("Error open file\n");
+ exit(1);
+ }
+
+ *out = malloc(file_size);
+
+ if (*out == NULL) {
+ perror("out of memory");
+ exit(1);
+ }
+
+ // read data and decode it from base64
+ while ( (ch = fgetc(fp)) != EOF) {
+ // ignore new lines as this is part of base64
+ if (ch == '\n' || ch == '\r')
+ continue;
+
+ if (file_pos+1 >= file_size) {
+ new_file_content = realloc(*out, (file_size+60));
+ if (new_file_content != NULL) {
+ *out = new_file_content;
+ file_size += 60;
+ }
+ else {
+ printf("error allocating memory\n");
+ exit(1);
+ }
+ }
+ (*out)[file_pos++] = ch;
+ }
+ return file_pos;
+
+}
diff --git a/lib/lib.h b/lib/lib.h
index 3e2a309..cca5fb0 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -22,6 +22,6 @@ void print_base64_string(char *string);
int hamming_distance_equal_length(char *string1, char *string2, int length);
char brute_force_single_byte_xor(char *string, int length, struct key_and_freq *tmp);
int isprintable(char *string, int length);
-
+int read_base64_file(const char *file, char **out);
#endif /* __CYRPTO_LIB__ */
diff --git a/lib/lib2.c b/lib/lib2.c
index 86dd823..fe65bb8 100644
--- a/lib/lib2.c
+++ b/lib/lib2.c
@@ -1,4 +1,5 @@
#include "lib2.h"
+#include "lib.h"
/**
* appends PKCS#7 padding to string. devide string in blocks of size blocksize
@@ -21,3 +22,41 @@ char *pkcs7_padding(char *string, int length_string, int blocksize)
return result;
}
+
+/**
+ * decrypts content which is encrypted in AES CBC mode
+ * @param in input content
+ * @param length_in length of parametere in
+ * @param out place where the decrypted content should be written to
+ * @param string_key key with which the content in in has been decrypted
+ * @param iv initalization vector
+ */
+int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector)
+{
+ char iv[16];
+ AES_KEY key;
+ int number_blocks = length_in / 16;
+ int i, j;
+ unsigned char ciphertext[128+1];
+ unsigned char tmp_after_aes[128+1];
+ unsigned char cleartext[128+1];
+ // set the key and bits
+ AES_set_decrypt_key(string_key, 128, &key);
+
+ memcpy(init_vector, iv, 16);
+
+ // implement cbc mode
+ for(i=0;i<number_blocks;i++) {
+ //do aes decryption
+ AES_decrypt(&in[i*16], tmp_after_aes, &key);
+ // xor
+ xor_string(iv, tmp_after_aes, &out[i*16], 16, 16);
+ // this ciphertext block is the next iv
+ for(j=0;j<16;j++) {
+ iv[j] = in[i*16+j];
+ }
+ }
+
+ return 0;
+
+}
diff --git a/lib/lib2.h b/lib/lib2.h
index 818558d..fabca8a 100644
--- a/lib/lib2.h
+++ b/lib/lib2.h
@@ -1,11 +1,12 @@
#ifndef __LIB2_H__
-#define __LIN2_H__
+#define __LIB2_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <openssl/aes.h>
char *pkcs7_padding(char *string, int length_string, int blocksize);
-
+int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector);
#endif