cryptopals_c

cryptopals crypto challenges solutions in pure c
git clone git://git.superpozycja.net/cryptopals_c
Log | Files | Refs | README

commit b4c9f249b57abb2f38e9ede0b9246d058084aa42
parent 64da71212ce18d3288514dac0a0477df5a5b2d75
Author: superpozycja <anna@superpozycja.net>
Date:   Sun,  1 Sep 2024 19:36:21 +0200

move common functions elsewhere

Diffstat:
Msrc/s1/c1.c | 54++----------------------------------------------------
Msrc/s1/c6.c | 48+-----------------------------------------------
Msrc/s1/c7.c | 54+-----------------------------------------------------
3 files changed, 4 insertions(+), 152 deletions(-)

diff --git a/src/s1/c1.c b/src/s1/c1.c @@ -1,62 +1,12 @@ #include <stdio.h> -#include <string.h> -#include <stdlib.h> - -static int hex_to_base64(const char* hex, char** b64) -{ - const char b64table[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - int leftover_length; - int block_count; - int res_length; - int length; - char *res; - int i; - - length = strlen(hex); - block_count = length / 6; - leftover_length = length % 6; - res_length = block_count * 4; - - if (leftover_length%2 == 1) { - fprintf(stderr, "bad sequence length\n"); - return -1; - } - - res = (char *) malloc(sizeof(char) * res_length + 2); - - for (i = 0; i < block_count + (leftover_length != 0); i++) { - char block[6+1]; - int block_hex; - int end_ix; - int j; - - end_ix = i < block_count ? 6 : leftover_length; - memcpy(block, hex+(6*i), end_ix); - block[end_ix] = '\0'; - - block_hex = strtol(block, NULL, 16); - block_hex <<= 4 * (6 - end_ix); - - for (j = 0; j < 4; j++) { - char b64; - - b64 = b64table[(block_hex >> 6 * (3 - j)) & 0x3f]; - res[4 *i + j] = j <= end_ix / 2 ? b64 : '='; - } - } - - *b64 = res; - return 0; -} +#include <util.h> int main(int argc, char* argv[]) { char* b64; int ret; - if (hex_to_base64(argv[1], &b64) == 0) + if (hex_to_base64(&b64, argv[1]) == 0) printf("%s\n", b64); return 0; diff --git a/src/s1/c6.c b/src/s1/c6.c @@ -2,8 +2,7 @@ #include <ctype.h> #include <ba.h> #include <cryptanalysis.h> - -const char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +#include <util.h> float i_c(ba *ct) { @@ -87,51 +86,6 @@ void break_m(ba *ct, unsigned int m) } } -int b64_chunk_to_hex(char **hex, char *b64) -{ - unsigned int val; - unsigned int sz; - unsigned int i; - - if (strlen(b64) != 4) - return -1; - - val = 0; - for (i = 0; i < 4; i++) { - int j = 0; - val <<= 6; - - while (b64[i] != '=' && b64table[j] != b64[i]) - j++; - - val += j; - } - - sz = 6 - 2 * ((b64[2] == '=') + (b64[3] == '=')); - sz++; - - *hex = (char *) malloc(sizeof(char) * sz); - snprintf(*hex, sz, "%0*x", sz - 1, val); - return 0; -} - -int base64_to_hex(char **hex, char *b64) -{ - unsigned int len; - - len = (strlen(b64) * 3 / 4) + 2; - *hex = (char *) malloc(sizeof(char *) * len); - - for (int i = 0; i < strlen(b64)/4; i++) { - char b64_chunk[5]; - char *hex_chunk; - - b64_chunk[4] = '\0'; - memcpy(b64_chunk, b64+4*i, 4); - b64_chunk_to_hex(&hex_chunk, b64_chunk); - strcat(*hex, hex_chunk); - } -} /* use coincidence test to determine the key */ int main(int argc, char *argv[]) diff --git a/src/s1/c7.c b/src/s1/c7.c @@ -2,59 +2,7 @@ #include <stdlib.h> #include <string.h> #include <ba.h> - -const char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -int b64_chunk_to_hex(char **hex, char *b64) -{ - unsigned int val; - unsigned int sz; - unsigned int i; - - if (strlen(b64) != 4) - return -1; - - val = 0; - for (i = 0; i < 4; i++) { - int j = 0; - val <<= 6; - - while (b64[i] != '=' && b64table[j] != b64[i]) - j++; - - val += j; - } - - sz = 3 - ((b64[2] == '=') + (b64[3] == '=')); - - *hex = (char *) malloc(sizeof(char) * sz); - i = 0; - while (val > 0) { - (*hex)[i] = val % 256; - val /= 8; - i++; - } - return 0; -} - -int base64_to_hex(char **hex, char *b64) -{ - unsigned int len; - - len = (strlen(b64) * 3 / 4) + 1; - *hex = (char *) malloc(sizeof(char *) * len); - - for (int i = 0; i < strlen(b64)/4; i++) { - char b64_chunk[5]; - char *hex_chunk; - - b64_chunk[4] = '\0'; - memcpy(b64_chunk, b64+4*i, 4); - b64_chunk_to_hex(&hex_chunk, b64_chunk); - strcat(*hex, hex_chunk); - } - (*hex)[len-1] = '\0'; -} +#include <util.h> /* int decrypt(unsigned char *ct, int ct_len, unsigned char *key,