Skip to content

Commit

Permalink
c backend ciphers long
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfd committed Jul 7, 2024
1 parent a5a81d3 commit 1af6b40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
16 changes: 8 additions & 8 deletions lotordb/src/ciphers.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,52 +364,52 @@ static void decrypt_block(uint8_t out[], const uint8_t in[], const uint8_t *rk)
//
// k = key, o = out
// https://medium.com/asecuritysite-when-bob-met-alice/a-bluffers-guide-to-aes-modes-ecb-cbc-cfb-and-all-that-jazz-4180f1882e16
void cipher_encrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv) {
void cipher_encrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv, uint32_t len) {
uint8_t block[NB * NR] = {0}, roundkeys[4 * NB * (NR + 1)] = {0};

key_expansion(roundkeys, key);
memcpy(block, iv, BBL);
// CBC
for (uint32_t i = 0; i < BBL; i += BBL) {
for (uint32_t i = 0; i < len; i += BBL) {
xor(block, block, (in + i), BBL);
encrypt_block((out + i), block, roundkeys);
memcpy(block, (out + i), BBL);
}
}

void cipher_encrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv) {
void cipher_encrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv, uint32_t len) {
uint8_t block[NB * NR] = {0}, encryptedblock[NB * NR] = {0}, roundkeys[4 * NB * (NR + 1)] = {0};

key_expansion(roundkeys, key);
memcpy(block, iv, BBL);
// CFB
for (uint32_t i = 0; i < BBL; i += BBL) {
for (uint32_t i = 0; i < len; i += BBL) {
encrypt_block(encryptedblock, block, roundkeys);
xor((out + i), (in + i), encryptedblock, BBL);
memcpy(block, (out + i), BBL);
}
}

void cipher_decrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv) {
void cipher_decrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv, uint32_t len) {
uint8_t block[NB * NR] = {0}, roundkeys[4 * NB * (NR + 1)] = {0};

key_expansion(roundkeys, key);
memcpy(block, iv, BBL);
// CBC
for (uint32_t i = 0; i < BBL; i += BBL) {
for (uint32_t i = 0; i < len; i += BBL) {
decrypt_block((out + i), (in + i), roundkeys);
xor((out + i), block, (out + i), BBL);
memcpy(block, in + i, BBL);
}
}

void cipher_decrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv) {
void cipher_decrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t key[], const uint8_t *iv, uint32_t len) {
uint8_t block[NB * NR] = {0}, encryptedblock[NB * NR] = {0}, roundkeys[4 * NB * (NR + 1)] = {0};

key_expansion(roundkeys, key);
memcpy(block, iv, BBL);
// CFB
for (uint32_t i = 0; i < BBL; i += BBL) {
for (uint32_t i = 0; i < len; i += BBL) {
encrypt_block(encryptedblock, block, roundkeys);
xor((out + i), (in + i), encryptedblock, BBL);
memcpy(block, in + i, BBL);
Expand Down
8 changes: 4 additions & 4 deletions lotordb/src/ciphers.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
extern const uint8_t SBOXINV[16][16], GF[15][256], MIX[4][4], MIX[4][4], K[32], SBOX[16][16];
extern const u64 WW[8];

void cipher_decrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv);
void cipher_decrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv);
void cipher_encrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv);
void cipher_encrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv);
void cipher_decrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv, uint32_t len);
void cipher_decrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv, uint32_t len);
void cipher_encrypt_cfb(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv, uint32_t len);
void cipher_encrypt_cbc(uint8_t out[], const uint8_t in[], const uint8_t k[], const uint8_t *iv, uint32_t len);
#endif
41 changes: 33 additions & 8 deletions lotordb/src/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,44 @@ void test_ciphers_cfb(void) {
uint8_t plain[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
iv[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, out[BBL] = {0}, in[BBL];
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, out[sizeof(plain)] = {0}, in[sizeof(plain)];

cipher_encrypt_cfb(out, plain, key, iv);
cipher_decrypt_cfb(in, out, key, iv);
assert(memcmp(plain, in, BBL * sizeof(uint8_t)) == 0);
cipher_encrypt_cfb(out, plain, key, iv, sizeof(plain));
cipher_decrypt_cfb(in, out, key, iv, sizeof(plain));
assert(memcmp(plain, in, sizeof(plain) * sizeof(uint8_t)) == 0);
}

void test_ciphers_cbc(void) {
uint8_t plain[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
iv[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, out[BBL] = {0}, in[BBL];
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, out[sizeof(plain)] = {0}, in[sizeof(plain)];

cipher_encrypt_cbc(out, plain, key, iv);
cipher_decrypt_cbc(in, out, key, iv);
assert(memcmp(plain, in, BBL * sizeof(uint8_t)) == 0);
cipher_encrypt_cbc(out, plain, key, iv, sizeof(plain));
cipher_decrypt_cbc(in, out, key, iv, sizeof(plain));
assert(memcmp(plain, in, sizeof(plain) * sizeof(uint8_t)) == 0);
}

void test_ciphers_cbc_long(void) {
uint8_t plain[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
iv[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, out[sizeof(plain)] = {0}, in[sizeof(plain)];

cipher_encrypt_cbc(out, plain, key, iv, sizeof(plain));
cipher_decrypt_cbc(in, out, key, iv, sizeof(plain));
assert(memcmp(plain, in, sizeof(plain) * sizeof(uint8_t)) == 0);
}

void test_ciphers_cfb_long(void) {
uint8_t plain[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
iv[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, out[sizeof(plain)] = {0}, in[sizeof(plain)];

cipher_encrypt_cfb(out, plain, key, iv, sizeof(plain));
cipher_decrypt_cfb(in, out, key, iv, sizeof(plain));
assert(memcmp(plain, in, sizeof(plain) * sizeof(uint8_t)) == 0);
}

void test_keys_verify(void) {
Expand All @@ -70,7 +92,10 @@ int main(void) {
printf("\"[o.o]\" testing .... \"[o.o]\"\n\n");
printf("lotordb test\n");
test_genkeys();
test_keys_verify();
test_ciphers_cbc();
test_ciphers_cfb();
test_ciphers_cbc_long();
test_ciphers_cfb_long();
printf("OK\n");
}

0 comments on commit 1af6b40

Please sign in to comment.