Skip to content

Commit

Permalink
c backend rewrite aes gcm 32bit, clear malloced
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfd committed Jan 3, 2025
1 parent 8fa114b commit bac5a35
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
18 changes: 13 additions & 5 deletions lotordb/src/rewrite/lotordb/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ void gcm_ciphertag(uint8_t *c, uint8_t *t, const uint8_t *key, uint8_t *iv, cons
uint32_t keywrd[32] = {0}, hwrd[32] = {0}, hkwrd[32] = {0}, pc = (16 * (clen / 16)) - clen, pa = (16 * (aadlen / 16)) - aadlen;
uint32_t bhlen = aadlen + (4 * sizeof(uint32_t)) + clen;
uint8_t *bh = malloc(bhlen), hk[32] = {0}, h[32] = {0}, j0[16] = {0}, hb[32] = {0}, bkey[4] = {0}, bbh[4] = {0}, bhk[4] = {0};
memset(bh, 0, bhlen * sizeof(uint8_t));
for (int j = 0; j < 32; j+=4) {
bkey[0] = key[j + 0];
bkey[1] = key[j + 1];
Expand All @@ -523,7 +524,8 @@ void gcm_ciphertag(uint8_t *c, uint8_t *t, const uint8_t *key, uint8_t *iv, cons
memcpy(iv + ivlen, b0, 4);
} else {
uint32_t pl = (16 * (ivlen / 16)) - ivlen;
uint8_t *bs = malloc(ivlen + pl + (2 * sizeof(uint32_t)));
uint8_t *bs = malloc((ivlen + pl + (2 * sizeof(uint32_t))) * sizeof(uint8_t));
memset(bs, 0, ivlen + pl + (2 * sizeof(uint32_t)));
memcpy(bs, iv, ivlen);
memcpy(bs + ivlen, &pl, sizeof(uint32_t));
memcpy(bs + ivlen + sizeof(uint32_t), &ivlen, sizeof(uint32_t));
Expand All @@ -549,14 +551,16 @@ void gcm_ciphertag32bit(uint32_t *c, uint32_t *t, const uint32_t *key, uint32_t
if (lenx > MAXPLAIN || aadlen > MAXAAD || ivlen > MAXIV || ivlen < 1) return;
uint32_t pc = (8 * (clen / 8)) - clen, pa = (8 * (aadlen / 8)) - aadlen;
uint32_t bhlen = aadlen + (4 * sizeof(uint32_t)) + clen, hk[32] = {0}, h[32] = {0}, j0[32] = {0};
uint32_t *bh = malloc(bhlen*sizeof(uint32_t)), hb[32] = {0};//, bkey[4] = {0}, bbh[4] = {0}, bhk[4] = {0};
uint32_t *bh = malloc(bhlen * sizeof(uint32_t)), hb[32] = {0};
memset(bh, 0, bhlen * sizeof(uint32_t));
cipher(hk, key, h);
if (ivlen == 12) { // when does this happen?!
uint32_t b0[4] = {0x00000000, 0x00000000, 0x00000000, 0x000000000001};
memcpy(iv + ivlen, b0, 4*sizeof(uint32_t));
} else {
uint32_t pl = (16 * (ivlen / 16)) - ivlen;
uint32_t *bs = malloc((ivlen + pl + (2 * sizeof(uint32_t)))* sizeof(uint32_t));
uint32_t *bs = malloc((ivlen + pl + (2 * sizeof(uint32_t))) * sizeof(uint32_t));
memset(bs, 0, (ivlen + pl + (2 * sizeof(uint32_t))) * sizeof(uint32_t));
memcpy(bs, iv, ivlen);
memcpy(bs + ivlen, &pl, sizeof(uint32_t));
memcpy(bs + ivlen + sizeof(uint32_t), &ivlen, sizeof(uint32_t));
Expand Down Expand Up @@ -586,6 +590,7 @@ void gcm_inv_ciphertag(uint8_t *plain, uint8_t *t, const uint8_t *key, const uin
uint32_t pc = (16 * (clen / 16)) - clen, pa = (16 * (aadlen / 16)) - aadlen, bhlen = aadlen + (4 * sizeof(uint32_t)) + clen;
uint32_t keywrd[32] = {0}, hwrd[32] = {0}, hkwrd[32] = {0};
uint8_t bkey[4] = {0}, bbh[4] = {0}, bhk[4] = {0}, hk[32] = {0}, h[32] = {0}, j0[32] = {0}, hb[32] = {0}, *bh = malloc(bhlen);
memset(bh, 0, bhlen * sizeof(uint8_t));
for (int j = 0; j < 32; j+=4) {
bkey[0] = key[j + 0];
bkey[1] = key[j + 1];
Expand All @@ -611,6 +616,7 @@ void gcm_inv_ciphertag(uint8_t *plain, uint8_t *t, const uint8_t *key, const uin
} else {
uint32_t pl = (16 * (ivlen / 16)) - ivlen;
uint8_t *bs = malloc(ivlen + pl + (2 * sizeof(uint32_t)));
memset(bs, 0, ivlen + pl + (2 * sizeof(uint32_t)));
memcpy(bs, iv, ivlen);
memcpy(bs + ivlen, &pl, sizeof(uint32_t));
memcpy(bs + ivlen + sizeof(uint32_t), &ivlen, sizeof(uint32_t));
Expand Down Expand Up @@ -638,15 +644,17 @@ void gcm_inv_ciphertag32bit(uint32_t *plain, uint32_t *t, const uint32_t *key, c
u64 aadlen = 12, ivlen = 8, clen = 8;
if (clen > MAXPLAIN || aadlen > MAXAAD || ivlen > MAXIV || ivlen < 1) return;
uint32_t pc = (8 * (clen / 8)) - clen, pa = (8 * (aadlen / 8)) - aadlen, bhlen = aadlen + (4 * sizeof(uint32_t)) + clen;
uint32_t j0[32] = {0}, hk[32] = {0}, h[32] = {0}, hb[32] = {0}, *bh = malloc(bhlen*sizeof(uint32_t));
uint32_t j0[32] = {0}, hk[32] = {0}, h[32] = {0}, hb[32] = {0}, *bh = malloc(bhlen * sizeof(uint32_t));
memset(bh, 0, bhlen * sizeof(uint32_t));
cipher(hk, key, h);
if (ivlen == 12) { // when does this happen?!
uint32_t b0[4] = {0x00000000, 0x00000000, 0x00000000, 0x00000001};
memcpy(j0, iv, ivlen*sizeof(uint32_t));
memcpy(j0 + ivlen, b0, 4*sizeof(uint32_t));
} else {
uint32_t pl = (16 * (ivlen / 16)) - ivlen;
uint32_t *bs = malloc(ivlen + pl + (2 * sizeof(uint32_t)));
uint32_t *bs = malloc((ivlen + pl + (2 * sizeof(uint32_t))) * sizeof(uint32_t));
memset(bs, 0, (ivlen + pl + (2 * sizeof(uint32_t))) * sizeof(uint32_t));
memcpy(bs, iv, ivlen);
memcpy(bs + ivlen, &pl, sizeof(uint32_t));
memcpy(bs + ivlen + sizeof(uint32_t), &ivlen, sizeof(uint32_t));
Expand Down
4 changes: 3 additions & 1 deletion lotordb/src/rewrite/lotordb/src/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#define MAXPLAIN 68719476704 // (2 ^ 39) - 256
#define MAXAAD 2305843009213693952 // (2 ^ 64) - 1
#define MAXIV 2305843009213693952 // (2 ^ 64) - 1
// AES
void cipher(uint32_t *ret, const uint32_t *key, const uint32_t *block);
void inv_cipher(uint32_t *ret, const uint32_t *key, const uint32_t *block);

// AES GCM
void gcm_ciphertag(uint8_t *c, uint8_t *t, const uint8_t *key, uint8_t *iv, const uint8_t *plain, const uint8_t *aad, const u64 lenx);
void gcm_inv_ciphertag(uint8_t *plain, uint8_t *t, const uint8_t *key, const uint8_t *iv, const uint8_t *c, const uint8_t *aad, const uint8_t *tag);
// AES GCM 32bit
void gcm_ciphertag32bit(uint32_t *c, uint32_t *t, const uint32_t *key, uint32_t *iv, const uint32_t *plain, const uint32_t *aad, const u64 lenx);
void gcm_inv_ciphertag32bit(uint32_t *plain, uint32_t *t, const uint32_t *key, const uint32_t *iv, const uint32_t *c, const uint32_t *aad, const uint32_t *tag);
#endif
Expand Down
2 changes: 2 additions & 0 deletions lotordb/src/rewrite/lotordb/src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdbool.h>
#include "hash.h"

static char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

//
// 0-255 to 0x0 to 0xff
static void to_hex(uint8_t h[], uint8_t d) {
Expand Down
1 change: 0 additions & 1 deletion lotordb/src/rewrite/lotordb/src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define HASH_H 1
#include <stdint.h>

static char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
// Imitate pythons %. -1 % 5 = 4, not -1
#define MOD(n, m) (((int)n % (int)m) + (int)m) % (int)m
#define SHA3_BITS 1024 // SHA3-256 = 512, SHA3-512 = 1024 (default)
Expand Down
4 changes: 2 additions & 2 deletions lotordb/src/rewrite/lotordb/src/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void test_aesgcmloop(void) {
assert(plain[j] == plain2[j]);
}
}
printf("aesgcmloop :Time %lus %lums\n", ((clock() - start) * 1000 / CLOCKS_PER_SEC) / 1000, ((clock() - start) * 1000 / CLOCKS_PER_SEC) % 1000);
printf("aesgcmloop: Time %lus %lums\n", ((clock() - start) * 1000 / CLOCKS_PER_SEC) / 1000, ((clock() - start) * 1000 / CLOCKS_PER_SEC) % 1000);
}

void test_aesgcm32bit(void) {
Expand Down Expand Up @@ -121,7 +121,7 @@ void test_aesgcm32bitloop(void) {
assert(plain[j] == plain2[j]);
}
}
printf("aesgcm32bitloop :Time %lus %lums\n", ((clock() - start) * 1000 / CLOCKS_PER_SEC) / 1000, ((clock() - start) * 1000 / CLOCKS_PER_SEC) % 1000);
printf("aesgcm32bitloop: Time %us %ums\n", (uint32_t)((clock() - start) * 1000 / CLOCKS_PER_SEC) / 1000, (uint32_t)((clock() - start) * 1000 / CLOCKS_PER_SEC) % 1000);
}

int main(void) {
Expand Down

0 comments on commit bac5a35

Please sign in to comment.