Skip to content

Commit

Permalink
Add NULL check for AES
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 22, 2024
1 parent 40586f8 commit b8236b1
Show file tree
Hide file tree
Showing 8 changed files with 417 additions and 47 deletions.
15 changes: 15 additions & 0 deletions src/common/aes/aes128_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ typedef struct {
} aes128ctx;

void oqs_aes128_load_iv_armv8(const uint8_t *iv, size_t iv_len, void *_schedule) {
if (_schedule == NULL) {
return;
}
aes128ctx *ctx = _schedule;
if (iv_len == 12) {
memcpy(ctx->iv, iv, 12);
Expand Down Expand Up @@ -63,12 +66,18 @@ static inline void aes128_armv8_encrypt(const unsigned char *rkeys, const unsign
}

void oqs_aes128_enc_sch_block_armv8(const uint8_t *plaintext, const void *_schedule, uint8_t *ciphertext) {
if (_schedule == NULL || plaintext == NULL || ciphertext == NULL) {
return;
}
const unsigned char *schedule = (const unsigned char *) _schedule;
aes128_armv8_encrypt(schedule, plaintext, ciphertext);
}

void oqs_aes128_ecb_enc_sch_armv8(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext) {
assert(plaintext_len % 16 == 0);
if (schedule == NULL || plaintext == NULL || ciphertext == NULL) {
return;
}
const aes128ctx *ctx = (const aes128ctx *) schedule;

for (size_t block = 0; block < plaintext_len / 16; block++) {
Expand All @@ -91,6 +100,9 @@ static uint32_t UINT32_TO_BE(const uint32_t x) {


void oqs_aes128_ctr_enc_sch_upd_blks_armv8(void *schedule, uint8_t *out, size_t out_blks) {
if (schedule == NULL || out == NULL) {
return;
}
aes128ctx *ctx = (aes128ctx *) schedule;
uint8_t *block = ctx->iv;
uint32_t ctr;
Expand All @@ -108,6 +120,9 @@ void oqs_aes128_ctr_enc_sch_upd_blks_armv8(void *schedule, uint8_t *out, size_t
}

void oqs_aes128_ctr_enc_sch_armv8(const uint8_t *iv, const size_t iv_len, const void *schedule, uint8_t *out, size_t out_len) {
if (iv == NULL || schedule == NULL || out == NULL) {
return;
}
uint8_t block[16];
uint32_t ctr;
uint32_t ctr_be;
Expand Down
26 changes: 24 additions & 2 deletions src/common/aes/aes128_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ static inline void aes128ni_setkey_encrypt(const unsigned char *key, __m128i rke
}

void oqs_aes128_load_schedule_ni(const uint8_t *key, void **_schedule) {
if (_schedule == NULL) {
return;
}
*_schedule = OQS_MEM_malloc(sizeof(aes128ctx));
OQS_EXIT_IF_NULLPTR(*_schedule, "AES");
assert(*_schedule != NULL);
if (*_schedule == NULL) {
return;
}
__m128i *schedule = ((aes128ctx *) *_schedule)->sk_exp;
aes128ni_setkey_encrypt(key, schedule);
}

void oqs_aes128_load_iv_ni(const uint8_t *iv, size_t iv_len, void *_schedule) {
if (_schedule == NULL) {
return;
}
aes128ctx *ctx = _schedule;
__m128i idx = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);
if (iv_len == 12) {
Expand All @@ -70,6 +77,9 @@ void oqs_aes128_load_iv_ni(const uint8_t *iv, size_t iv_len, void *_schedule) {
}

void oqs_aes128_load_iv_u64_ni(uint64_t iv, void *_schedule) {
if (_schedule == NULL) {
return;
}
aes128ctx *ctx = _schedule;
ctx->iv = _mm_loadl_epi64((__m128i *)&iv);
}
Expand Down Expand Up @@ -133,18 +143,27 @@ static inline void aes128ni_encrypt_x4(const __m128i rkeys[11], __m128i n0,
}

void oqs_aes128_enc_sch_block_ni(const uint8_t *plaintext, const void *_schedule, uint8_t *ciphertext) {
if (_schedule == NULL) {
return;
}
const __m128i *schedule = ((const aes128ctx *) _schedule)->sk_exp;
aes128ni_encrypt(schedule, _mm_loadu_si128((const __m128i *)plaintext), ciphertext);
}

void oqs_aes128_ecb_enc_sch_ni(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext) {
if (schedule == NULL) {
return;
}
assert(plaintext_len % 16 == 0);
for (size_t block = 0; block < plaintext_len / 16; block++) {
oqs_aes128_enc_sch_block_ni(plaintext + (16 * block), schedule, ciphertext + (16 * block));
}
}

void oqs_aes128_ctr_enc_sch_upd_blks_ni(void *schedule, uint8_t *out, size_t out_blks) {
if (schedule == NULL) {
return;
}
aes128ctx *ctx = (aes128ctx *) schedule;
const __m128i mask = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);

Expand All @@ -168,6 +187,9 @@ void oqs_aes128_ctr_enc_sch_upd_blks_ni(void *schedule, uint8_t *out, size_t out
}

void oqs_aes128_ctr_enc_sch_ni(const uint8_t *iv, const size_t iv_len, const void *schedule, uint8_t *out, size_t out_len) {
if (schedule == NULL) {
return;
}
__m128i block;
__m128i mask = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);
if (iv_len == 12) {
Expand Down
15 changes: 15 additions & 0 deletions src/common/aes/aes256_armv8.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ typedef struct {
} aes256ctx_nobitslice;

void oqs_aes256_load_iv_armv8(const uint8_t *iv, size_t iv_len, void *_schedule) {
if (_schedule == NULL) {
return;
}
aes256ctx_nobitslice *ctx = _schedule;
if (iv_len == 12) {
memcpy(ctx->iv, iv, 12);
Expand Down Expand Up @@ -70,11 +73,17 @@ static inline void aes256_armv8_encrypt(const unsigned char *rkeys, const unsign
}

void oqs_aes256_enc_sch_block_armv8(const uint8_t *plaintext, const void *_schedule, uint8_t *ciphertext) {
if (_schedule == NULL) {
return;
}
const unsigned char *schedule = (const unsigned char *) ((const aes256ctx_nobitslice *) _schedule)->sk_exp;
aes256_armv8_encrypt(schedule, plaintext, ciphertext);
}

void oqs_aes256_ecb_enc_sch_armv8(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext) {
if (schedule == NULL) {
return;
}
assert(plaintext_len % 16 == 0);
for (size_t block = 0; block < plaintext_len / 16; block++) {
oqs_aes256_enc_sch_block_armv8(plaintext + (16 * block), schedule, ciphertext + (16 * block));
Expand All @@ -95,6 +104,9 @@ static uint32_t UINT32_TO_BE(const uint32_t x) {
#define BE_TO_UINT32(n) (uint32_t)((((uint8_t *) &(n))[0] << 24) | (((uint8_t *) &(n))[1] << 16) | (((uint8_t *) &(n))[2] << 8) | (((uint8_t *) &(n))[3] << 0))

void oqs_aes256_ctr_enc_sch_upd_blks_armv8(void *schedule, uint8_t *out, size_t out_blks) {
if (schedule == NULL) {
return;
}
aes256ctx_nobitslice *ctx = (aes256ctx_nobitslice *) schedule;
uint8_t *block = ctx->iv;
uint32_t ctr;
Expand All @@ -112,6 +124,9 @@ void oqs_aes256_ctr_enc_sch_upd_blks_armv8(void *schedule, uint8_t *out, size_t
}

void oqs_aes256_ctr_enc_sch_armv8(const uint8_t *iv, const size_t iv_len, const void *schedule, uint8_t *out, size_t out_len) {
if (schedule == NULL || iv == NULL || out == NULL) {
return;
}
uint8_t block[16];
uint32_t ctr;
uint32_t ctr_be;
Expand Down
27 changes: 24 additions & 3 deletions src/common/aes/aes256_ni.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ static inline void aes256ni_setkey_encrypt(const unsigned char *key, __m128i rke
}

void oqs_aes256_load_schedule_ni(const uint8_t *key, void **_schedule) {
if (_schedule == NULL) {
return;
}
*_schedule = OQS_MEM_malloc(sizeof(aes256ctx));
OQS_EXIT_IF_NULLPTR(*_schedule, "AES");
assert(*_schedule != NULL);
if (*_schedule == NULL) {
return;
}
__m128i *schedule = ((aes256ctx *) *_schedule)->sk_exp;
aes256ni_setkey_encrypt(key, schedule);
}

void oqs_aes256_load_iv_ni(const uint8_t *iv, size_t iv_len, void *_schedule) {
if (_schedule == NULL) {
return;
}
aes256ctx *ctx = _schedule;
__m128i idx = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);
if (iv_len == 12) {
Expand All @@ -97,6 +104,9 @@ void oqs_aes256_load_iv_ni(const uint8_t *iv, size_t iv_len, void *_schedule) {
}

void oqs_aes256_load_iv_u64_ni(uint64_t iv, void *_schedule) {
if (_schedule == NULL) {
return;
}
aes256ctx *ctx = _schedule;
ctx->iv = _mm_loadl_epi64((__m128i *)&iv);
}
Expand Down Expand Up @@ -167,18 +177,26 @@ static inline void aes256ni_encrypt_x4(const __m128i rkeys[15], __m128i n0, __m1
}

void oqs_aes256_enc_sch_block_ni(const uint8_t *plaintext, const void *_schedule, uint8_t *ciphertext) {
if (_schedule == NULL || plaintext == NULL || ciphertext == NULL) {
return;
}
const __m128i *schedule = ((const aes256ctx *) _schedule)->sk_exp;
aes256ni_encrypt(schedule, _mm_loadu_si128((const __m128i *)plaintext), ciphertext);
}

void oqs_aes256_ecb_enc_sch_ni(const uint8_t *plaintext, const size_t plaintext_len, const void *schedule, uint8_t *ciphertext) {
if (plaintext == NULL || schedule == NULL || ciphertext == NULL) {
return;
}
assert(plaintext_len % 16 == 0);
for (size_t block = 0; block < plaintext_len / 16; block++) {
oqs_aes256_enc_sch_block_ni(plaintext + (16 * block), schedule, ciphertext + (16 * block));
}
}

void oqs_aes256_ctr_enc_sch_upd_blks_ni(void *schedule, uint8_t *out, size_t out_blks) {
if (schedule == NULL || out == NULL) {
return;
}
aes256ctx *ctx = (aes256ctx *) schedule;
const __m128i mask = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);

Expand All @@ -202,6 +220,9 @@ void oqs_aes256_ctr_enc_sch_upd_blks_ni(void *schedule, uint8_t *out, size_t out
}

void oqs_aes256_ctr_enc_sch_ni(const uint8_t *iv, const size_t iv_len, const void *schedule, uint8_t *out, size_t out_len) {
if (iv == NULL || schedule == NULL || out == NULL) {
return;
}
__m128i block;
__m128i mask = _mm_set_epi8(8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0);
if (iv_len == 12) {
Expand Down
Loading

0 comments on commit b8236b1

Please sign in to comment.