diff options
author | Yonatan Goldschmidt <yon.goldschmidt@gmail.com> | 2019-04-28 23:57:11 +0300 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-05-06 18:20:56 +1000 |
commit | 32ba679924b8f5c8a81cff905e6bd295c6bb4df8 (patch) | |
tree | df6a097f2315cea396a35724fee17ef020e99d89 | |
parent | ef9843653b795b650e1fe77e22f87e3523bd0a08 (diff) |
extmod/moducryptolib: Add AES-CTR support for axTLS builds.
-rw-r--r-- | extmod/moducryptolib.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/extmod/moducryptolib.c b/extmod/moducryptolib.c index 6c45c2fde..15cd4535f 100644 --- a/extmod/moducryptolib.c +++ b/extmod/moducryptolib.c @@ -139,6 +139,33 @@ STATIC void aes_process_cbc_impl(AES_CTX_IMPL *ctx, const uint8_t *in, uint8_t * AES_cbc_decrypt(ctx, in, out, in_len); } } + +#if MICROPY_PY_UCRYPTOLIB_CTR +// axTLS doesn't have CTR support out of the box. This implements the counter part using the ECB primitive. +STATIC void aes_process_ctr_impl(AES_CTX_IMPL *ctx, const uint8_t *in, uint8_t *out, size_t in_len, struct ctr_params *ctr_params) { + size_t n = ctr_params->offset; + uint8_t *const counter = ctx->iv; + + while (in_len--) { + if (n == 0) { + aes_process_ecb_impl(ctx, counter, ctr_params->encrypted_counter, true); + + // increment the 128-bit counter + for (int i = 15; i >= 0; --i) { + if (++counter[i] != 0) { + break; + } + } + } + + *out++ = *in++ ^ ctr_params->encrypted_counter[n]; + n = (n + 1) & 0xf; + } + + ctr_params->offset = n; +} +#endif + #endif #if MICROPY_SSL_MBEDTLS |