cryptopals_c

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

commit 9ba6b299291137c3caac35496044ee27c9e12cb9
parent 61013081552caa361b5c0e39a0448f4a441fedb4
Author: superpozycja <anna@superpozycja.net>
Date:   Sun, 23 Feb 2025 02:15:07 +0100

fix styling overall

Diffstat:
Mlib/aes.c | 48+++++++++++++++++++++++++++++-------------------
Mlib/ba.c | 4+++-
Mlib/util.c | 5+++--
Msrc/s1/c7.c | 2+-
4 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/lib/aes.c b/lib/aes.c @@ -41,15 +41,19 @@ static uint8_t sbox_inv[0x100] = { }; static int rot_word(ba *input, ba **output, unsigned int amt) { + int i; + *output = ba_alloc(4); - for (int i = 0; i < 4; i++) { + for (i = 0; i < 4; i++) { (*output)->val[i] = input->val[(i+amt)%4]; } } static int sub_word(ba *input, ba **output) { + int i; + *output = ba_alloc(4); - for (int i = 0; i < 4; i++) { + for (i = 0; i < 4; i++) { (*output)->val[i] = sbox[input->val[i]]; } } @@ -57,6 +61,9 @@ static int sub_word(ba *input, ba **output) { static int key_expansion(ba *key, unsigned int keylen, unsigned int rounds, ba *round_keys[4*(rounds + 1)]) { + const int nk = keylen / 4; + int i; + ba *rcon[10] = { ba_from_hex("01000000"), ba_from_hex("02000000"), @@ -70,30 +77,33 @@ static int key_expansion(ba *key, unsigned int keylen, unsigned int rounds, ba_from_hex("36000000") }; - const int nk = keylen / 4; - for (int i = 0; i < nk; i++) { + for (i = 0; i < nk; i++) { + int j; + round_keys[i] = ba_alloc(4); - for (int j = 0; j < 4; j++) + for (j = 0; j < 4; j++) (round_keys[i])->val[j] = key->val[i*4+j]; } - for (int i = nk; i < 4 * (rounds + 1); i++) { + for (i = nk; i < 4 * (rounds + 1); i++) { ba *tmp = ba_alloc(4); ba *tmp2 = ba_alloc(4); - round_keys[i] = ba_alloc(4); + round_keys[i] = ba_alloc(4); ba_copy(tmp, round_keys[i - 1]); ba_copy(tmp2, round_keys[i - nk]); if (i % nk == 0) { ba *tmp3; + rot_word(tmp, &tmp3, 1); sub_word(tmp3, &tmp); ba_xor(tmp, rcon[(i-1) / nk]); ba_free(tmp3); } else if (nk > 6 && i % nk == 4) { ba *tmp3 = ba_alloc(4); + ba_copy(tmp3, tmp); sub_word(tmp3, &tmp); ba_free(tmp3); @@ -111,6 +121,15 @@ static int key_expansion(ba *key, unsigned int keylen, unsigned int rounds, static int aes_generic(unsigned int rounds, unsigned int keylen, ba *plaintext, ba *key, ba* ciphertext) { + ba *round_keys[4*(rounds + 1)]; + ba *state[4] = { + ba_alloc(4), + ba_alloc(4), + ba_alloc(4), + ba_alloc(4), + }; + int i; + if (plaintext->len != 16) { printf("invalid block len\n"); return -EINVAL; @@ -120,21 +139,13 @@ static int aes_generic(unsigned int rounds, unsigned int keylen, return -EINVAL; } - ba *state[4] = { - ba_alloc(4), - ba_alloc(4), - ba_alloc(4), - ba_alloc(4), - }; - - for (int i = 0; i < 16; i++) { + for (i = 0; i < 16; i++) { (state[i/4]->val)[i%4] = plaintext->val[i]; } - ba *round_keys[4*(rounds + 1)]; key_expansion(key, keylen, rounds, round_keys); - for (int i = 0; i < 4*(rounds + 1); i++) { + for (i = 0; i < 4*(rounds + 1); i++) { ba_fprint(round_keys[i], stdout, 0); printf("\n"); } @@ -153,9 +164,8 @@ static int aes_generic(unsigned int rounds, unsigned int keylen, */ ciphertext = ba_alloc(16); - for (int i = 0; i < 16; i++) { + for (i = 0; i < 16; i++) ciphertext->val[i] = (state[i/4]->val)[i%4]; - } return 0; } diff --git a/lib/ba.c b/lib/ba.c @@ -103,7 +103,9 @@ ba* ba_from_hex_n(char* hex, unsigned int n) } for (o = 0; o < b_len * 2; o += offset) { - for (int i = 0; i < offset; i++) { + int i; + + for (i = 0; i < offset; i++) { char tmp[3]; strncpy(tmp, hex + 2 * i, 2); tmp[2] = '\0'; diff --git a/lib/util.c b/lib/util.c @@ -7,7 +7,7 @@ static int b64_chunk_to_hex(char **hex, char *b64) { unsigned int val; unsigned int sz; - unsigned int i; + int i; if (strlen(b64) != 4) return -1; @@ -34,11 +34,12 @@ static int b64_chunk_to_hex(char **hex, char *b64) int base64_to_hex(char **hex, char *b64) { unsigned int len; + int i; len = (strlen(b64) * 3 / 4) + 2; *hex = (char *) malloc(sizeof(char *) * len); - for (int i = 0; i < strlen(b64)/4; i++) { + for (i = 0; i < strlen(b64)/4; i++) { char b64_chunk[5]; char *hex_chunk; diff --git a/src/s1/c7.c b/src/s1/c7.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) ba *ct_ba; aes_128_encrypt(pt_ba, key_ba, ct_ba); - aes_192_encrypt(pt_ba, key_192_ba, ct_ba); + //aes_192_encrypt(pt_ba, key_192_ba, ct_ba); return 0; }