2023-11-04 06:44:18 +03:00
|
|
|
#include "aes256.h"
|
|
|
|
|
|
|
|
struct ctr256_ctx {
|
|
|
|
uint32_t expandedKey[EXPANDED_KEY_SIZE];
|
2023-11-07 22:49:35 +03:00
|
|
|
uint8_t iv[AES_BLOCK_SIZE];
|
2023-11-04 06:44:18 +03:00
|
|
|
uint8_t state;
|
|
|
|
};
|
|
|
|
|
2023-11-07 22:49:35 +03:00
|
|
|
WASM_EXPORT struct ctr256_ctx* ctr256_alloc() {
|
2023-11-04 06:44:18 +03:00
|
|
|
struct ctr256_ctx *state = (struct ctr256_ctx *) __malloc(sizeof(struct ctr256_ctx));
|
2023-11-07 22:49:35 +03:00
|
|
|
aes256_set_encryption_key(aes_shared_key_buffer, state->expandedKey);
|
2023-11-04 06:44:18 +03:00
|
|
|
|
2023-11-07 22:49:35 +03:00
|
|
|
memcpy(state->iv, aes_shared_iv_buffer, AES_BLOCK_SIZE);
|
2023-11-04 06:44:18 +03:00
|
|
|
state->state = 0;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:49:35 +03:00
|
|
|
WASM_EXPORT void ctr256_free(struct ctr256_ctx* ctx) {
|
2023-11-04 06:44:18 +03:00
|
|
|
__free(ctx);
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:49:35 +03:00
|
|
|
WASM_EXPORT void ctr256(struct ctr256_ctx* ctx, uint8_t* in, uint32_t length, uint8_t *out) {
|
2023-11-04 06:44:18 +03:00
|
|
|
uint8_t chunk[AES_BLOCK_SIZE];
|
|
|
|
uint32_t* expandedKey = ctx->expandedKey;
|
|
|
|
uint8_t* iv = ctx->iv;
|
|
|
|
uint8_t state = ctx->state;
|
|
|
|
uint32_t i, j, k;
|
|
|
|
|
|
|
|
aes256_encrypt(iv, chunk, expandedKey);
|
|
|
|
|
|
|
|
for (i = 0; i < length; i += AES_BLOCK_SIZE) {
|
|
|
|
for (j = 0; j < MIN(length - i, AES_BLOCK_SIZE); ++j) {
|
|
|
|
out[i + j] = in[i + j] ^ chunk[state++];
|
|
|
|
|
|
|
|
if (state >= AES_BLOCK_SIZE)
|
|
|
|
state = 0;
|
|
|
|
|
|
|
|
if (state == 0) {
|
|
|
|
k = AES_BLOCK_SIZE;
|
|
|
|
while(k--)
|
|
|
|
if (++iv[k])
|
|
|
|
break;
|
|
|
|
|
|
|
|
aes256_encrypt(iv, chunk, expandedKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__free(in);
|
|
|
|
|
|
|
|
ctx->state = state;
|
|
|
|
}
|