2018-01-07 13:13:56 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2018 Paul Sokolovsky
|
2018-06-15 15:07:47 +01:00
|
|
|
* Copyright (c) 2018 Yonatan Goldschmidt
|
2018-01-07 13:13:56 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
2018-06-15 15:07:47 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB
|
2018-01-07 13:13:56 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
// This module implements crypto ciphers API, roughly following
|
|
|
|
// https://www.python.org/dev/peps/pep-0272/ . Exact implementation
|
|
|
|
// of PEP 272 can be made with a simple wrapper which adds all the
|
|
|
|
// needed boilerplate.
|
|
|
|
|
2018-06-15 15:07:47 +01:00
|
|
|
// values follow PEP 272
|
|
|
|
enum {
|
2019-04-23 10:39:05 +01:00
|
|
|
UCRYPTOLIB_MODE_ECB = 1,
|
|
|
|
UCRYPTOLIB_MODE_CBC = 2,
|
|
|
|
UCRYPTOLIB_MODE_CTR = 6,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ctr_params {
|
|
|
|
// counter is the IV of the AES context.
|
|
|
|
|
|
|
|
size_t offset; // in encrypted_counter
|
|
|
|
// encrypted counter
|
|
|
|
uint8_t encrypted_counter[16];
|
2018-06-15 15:07:47 +01:00
|
|
|
};
|
|
|
|
|
2018-01-07 13:13:56 +00:00
|
|
|
#if MICROPY_SSL_AXTLS
|
|
|
|
#include "lib/axtls/crypto/crypto.h"
|
2018-06-15 23:16:01 +01:00
|
|
|
|
|
|
|
#define AES_CTX_IMPL AES_CTX
|
2018-01-07 13:13:56 +00:00
|
|
|
#endif
|
|
|
|
|
2018-06-15 23:16:57 +01:00
|
|
|
#if MICROPY_SSL_MBEDTLS
|
|
|
|
#include <mbedtls/aes.h>
|
|
|
|
|
|
|
|
// we can't run mbedtls AES key schedule until we know whether we're used for encrypt or decrypt.
|
|
|
|
// therefore, we store the key & keysize and on the first call to encrypt/decrypt we override them
|
|
|
|
// with the mbedtls_aes_context, as they are not longer required. (this is done to save space)
|
|
|
|
struct mbedtls_aes_ctx_with_key {
|
|
|
|
union {
|
|
|
|
mbedtls_aes_context mbedtls_ctx;
|
|
|
|
struct {
|
|
|
|
uint8_t key[32];
|
|
|
|
uint8_t keysize;
|
|
|
|
} init_data;
|
|
|
|
} u;
|
|
|
|
unsigned char iv[16];
|
|
|
|
};
|
|
|
|
#define AES_CTX_IMPL struct mbedtls_aes_ctx_with_key
|
|
|
|
#endif
|
2018-01-07 13:13:56 +00:00
|
|
|
|
|
|
|
typedef struct _mp_obj_aes_t {
|
|
|
|
mp_obj_base_t base;
|
2018-06-15 23:16:01 +01:00
|
|
|
AES_CTX_IMPL ctx;
|
|
|
|
uint8_t block_mode : 6;
|
|
|
|
#define AES_KEYTYPE_NONE 0
|
|
|
|
#define AES_KEYTYPE_ENC 1
|
|
|
|
#define AES_KEYTYPE_DEC 2
|
|
|
|
uint8_t key_type : 2;
|
2018-01-07 13:13:56 +00:00
|
|
|
} mp_obj_aes_t;
|
|
|
|
|
2019-08-19 13:29:24 +01:00
|
|
|
static inline bool is_ctr_mode(int block_mode) {
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CTR
|
2019-04-23 10:39:05 +01:00
|
|
|
return block_mode == UCRYPTOLIB_MODE_CTR;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-19 13:29:24 +01:00
|
|
|
static inline struct ctr_params *ctr_params_from_aes(mp_obj_aes_t *o) {
|
2019-04-23 10:39:05 +01:00
|
|
|
// ctr_params follows aes object struct
|
|
|
|
return (struct ctr_params *)&o[1];
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:16:01 +01:00
|
|
|
#if MICROPY_SSL_AXTLS
|
|
|
|
STATIC void aes_initial_set_key_impl(AES_CTX_IMPL *ctx, const uint8_t *key, size_t keysize, const uint8_t iv[16]) {
|
|
|
|
assert(16 == keysize || 32 == keysize);
|
|
|
|
AES_set_key(ctx, key, iv, (16 == keysize) ? AES_MODE_128 : AES_MODE_256);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void aes_final_set_key_impl(AES_CTX_IMPL *ctx, bool encrypt) {
|
|
|
|
if (!encrypt) {
|
|
|
|
AES_convert_key(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void aes_process_ecb_impl(AES_CTX_IMPL *ctx, const uint8_t in[16], uint8_t out[16], bool encrypt) {
|
|
|
|
memcpy(out, in, 16);
|
|
|
|
// We assume that out (vstr.buf or given output buffer) is uint32_t aligned
|
|
|
|
uint32_t *p = (uint32_t *)out;
|
|
|
|
// axTLS likes it weird and complicated with byteswaps
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
p[i] = MP_HTOBE32(p[i]);
|
|
|
|
}
|
|
|
|
if (encrypt) {
|
|
|
|
AES_encrypt(ctx, p);
|
|
|
|
} else {
|
|
|
|
AES_decrypt(ctx, p);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
p[i] = MP_BE32TOH(p[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void aes_process_cbc_impl(AES_CTX_IMPL *ctx, const uint8_t *in, uint8_t *out, size_t in_len, bool encrypt) {
|
|
|
|
if (encrypt) {
|
|
|
|
AES_cbc_encrypt(ctx, in, out, in_len);
|
|
|
|
} else {
|
|
|
|
AES_cbc_decrypt(ctx, in, out, in_len);
|
|
|
|
}
|
|
|
|
}
|
2019-04-28 21:57:11 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CTR
|
2019-04-28 21:57:11 +01:00
|
|
|
// 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
|
|
|
|
|
2018-06-15 23:16:01 +01:00
|
|
|
#endif
|
|
|
|
|
2018-06-15 23:16:57 +01:00
|
|
|
#if MICROPY_SSL_MBEDTLS
|
|
|
|
STATIC void aes_initial_set_key_impl(AES_CTX_IMPL *ctx, const uint8_t *key, size_t keysize, const uint8_t iv[16]) {
|
|
|
|
ctx->u.init_data.keysize = keysize;
|
|
|
|
memcpy(ctx->u.init_data.key, key, keysize);
|
|
|
|
|
|
|
|
if (NULL != iv) {
|
|
|
|
memcpy(ctx->iv, iv, sizeof(ctx->iv));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void aes_final_set_key_impl(AES_CTX_IMPL *ctx, bool encrypt) {
|
|
|
|
// first, copy key aside
|
|
|
|
uint8_t key[32];
|
|
|
|
uint8_t keysize = ctx->u.init_data.keysize;
|
|
|
|
memcpy(key, ctx->u.init_data.key, keysize);
|
|
|
|
// now, override key with the mbedtls context object
|
|
|
|
mbedtls_aes_init(&ctx->u.mbedtls_ctx);
|
|
|
|
|
|
|
|
// setkey call will succeed, we've already checked the keysize earlier.
|
|
|
|
assert(16 == keysize || 32 == keysize);
|
|
|
|
if (encrypt) {
|
|
|
|
mbedtls_aes_setkey_enc(&ctx->u.mbedtls_ctx, key, keysize * 8);
|
|
|
|
} else {
|
|
|
|
mbedtls_aes_setkey_dec(&ctx->u.mbedtls_ctx, key, keysize * 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void aes_process_ecb_impl(AES_CTX_IMPL *ctx, const uint8_t in[16], uint8_t out[16], bool encrypt) {
|
|
|
|
mbedtls_aes_crypt_ecb(&ctx->u.mbedtls_ctx, encrypt ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT, in, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void aes_process_cbc_impl(AES_CTX_IMPL *ctx, const uint8_t *in, uint8_t *out, size_t in_len, bool encrypt) {
|
|
|
|
mbedtls_aes_crypt_cbc(&ctx->u.mbedtls_ctx, encrypt ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT, in_len, ctx->iv, in, out);
|
|
|
|
}
|
2019-04-23 10:39:05 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CTR
|
2019-04-23 10:39:05 +01:00
|
|
|
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) {
|
|
|
|
mbedtls_aes_crypt_ctr(&ctx->u.mbedtls_ctx, in_len, &ctr_params->offset, ctx->iv, ctr_params->encrypted_counter, in, out);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-15 23:16:57 +01:00
|
|
|
#endif
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t cryptolib_aes_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2018-01-07 13:13:56 +00:00
|
|
|
mp_arg_check_num(n_args, n_kw, 2, 3, false);
|
|
|
|
|
2019-04-23 10:39:05 +01:00
|
|
|
const mp_int_t block_mode = mp_obj_get_int(args[1]);
|
|
|
|
|
|
|
|
switch (block_mode) {
|
|
|
|
case UCRYPTOLIB_MODE_ECB:
|
|
|
|
case UCRYPTOLIB_MODE_CBC:
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CTR
|
2019-04-23 10:39:05 +01:00
|
|
|
case UCRYPTOLIB_MODE_CTR:
|
|
|
|
#endif
|
|
|
|
break;
|
2018-01-07 13:13:56 +00:00
|
|
|
|
2019-04-23 10:39:05 +01:00
|
|
|
default:
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("mode"));
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 08:09:15 +01:00
|
|
|
mp_obj_aes_t *o = mp_obj_malloc_var(mp_obj_aes_t, struct ctr_params, !!is_ctr_mode(block_mode), type);
|
2019-04-23 10:39:05 +01:00
|
|
|
|
|
|
|
o->block_mode = block_mode;
|
|
|
|
o->key_type = AES_KEYTYPE_NONE;
|
|
|
|
|
2018-01-07 13:13:56 +00:00
|
|
|
mp_buffer_info_t keyinfo;
|
|
|
|
mp_get_buffer_raise(args[0], &keyinfo, MP_BUFFER_READ);
|
2018-06-15 23:16:01 +01:00
|
|
|
if (32 != keyinfo.len && 16 != keyinfo.len) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("key"));
|
2018-06-15 23:16:01 +01:00
|
|
|
}
|
2018-01-07 13:13:56 +00:00
|
|
|
|
|
|
|
mp_buffer_info_t ivinfo;
|
|
|
|
ivinfo.buf = NULL;
|
|
|
|
if (n_args > 2 && args[2] != mp_const_none) {
|
|
|
|
mp_get_buffer_raise(args[2], &ivinfo, MP_BUFFER_READ);
|
|
|
|
|
2018-06-15 23:16:01 +01:00
|
|
|
if (16 != ivinfo.len) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("IV"));
|
2018-06-15 23:16:01 +01:00
|
|
|
}
|
2019-04-23 10:39:05 +01:00
|
|
|
} else if (o->block_mode == UCRYPTOLIB_MODE_CBC || is_ctr_mode(o->block_mode)) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("IV"));
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 10:39:05 +01:00
|
|
|
if (is_ctr_mode(block_mode)) {
|
|
|
|
ctr_params_from_aes(o)->offset = 0;
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:16:01 +01:00
|
|
|
aes_initial_set_key_impl(&o->ctx, keyinfo.buf, keyinfo.len, ivinfo.buf);
|
2018-01-07 13:13:56 +00:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t aes_process(size_t n_args, const mp_obj_t *args, bool encrypt) {
|
|
|
|
mp_obj_aes_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
mp_obj_t in_buf = args[1];
|
|
|
|
mp_obj_t out_buf = MP_OBJ_NULL;
|
|
|
|
if (n_args > 2) {
|
|
|
|
out_buf = args[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_buffer_info_t in_bufinfo;
|
|
|
|
mp_get_buffer_raise(in_buf, &in_bufinfo, MP_BUFFER_READ);
|
|
|
|
|
2019-04-23 10:39:05 +01:00
|
|
|
if (!is_ctr_mode(self->block_mode) && in_bufinfo.len % 16 != 0) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("blksize % 16"));
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vstr_t vstr;
|
|
|
|
mp_buffer_info_t out_bufinfo;
|
|
|
|
uint8_t *out_buf_ptr;
|
|
|
|
|
|
|
|
if (out_buf != MP_OBJ_NULL) {
|
|
|
|
mp_get_buffer_raise(out_buf, &out_bufinfo, MP_BUFFER_WRITE);
|
|
|
|
if (out_bufinfo.len < in_bufinfo.len) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("output too small"));
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
out_buf_ptr = out_bufinfo.buf;
|
|
|
|
} else {
|
|
|
|
vstr_init_len(&vstr, in_bufinfo.len);
|
|
|
|
out_buf_ptr = (uint8_t *)vstr.buf;
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:16:01 +01:00
|
|
|
if (AES_KEYTYPE_NONE == self->key_type) {
|
2019-04-23 10:39:05 +01:00
|
|
|
// always set key for encryption if CTR mode.
|
|
|
|
const bool encrypt_mode = encrypt || is_ctr_mode(self->block_mode);
|
|
|
|
aes_final_set_key_impl(&self->ctx, encrypt_mode);
|
2018-06-15 23:16:01 +01:00
|
|
|
self->key_type = encrypt ? AES_KEYTYPE_ENC : AES_KEYTYPE_DEC;
|
|
|
|
} else {
|
|
|
|
if ((encrypt && self->key_type == AES_KEYTYPE_DEC) ||
|
|
|
|
(!encrypt && self->key_type == AES_KEYTYPE_ENC)) {
|
|
|
|
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("can't encrypt & decrypt"));
|
2018-06-15 23:16:01 +01:00
|
|
|
}
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 10:39:05 +01:00
|
|
|
switch (self->block_mode) {
|
|
|
|
case UCRYPTOLIB_MODE_ECB: {
|
|
|
|
uint8_t *in = in_bufinfo.buf, *out = out_buf_ptr;
|
|
|
|
uint8_t *top = in + in_bufinfo.len;
|
|
|
|
for (; in < top; in += 16, out += 16) {
|
|
|
|
aes_process_ecb_impl(&self->ctx, in, out, encrypt);
|
|
|
|
}
|
|
|
|
break;
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
2019-04-23 10:39:05 +01:00
|
|
|
|
|
|
|
case UCRYPTOLIB_MODE_CBC:
|
|
|
|
aes_process_cbc_impl(&self->ctx, in_bufinfo.buf, out_buf_ptr, in_bufinfo.len, encrypt);
|
|
|
|
break;
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CTR
|
2019-04-23 10:39:05 +01:00
|
|
|
case UCRYPTOLIB_MODE_CTR:
|
|
|
|
aes_process_ctr_impl(&self->ctx, in_bufinfo.buf, out_buf_ptr, in_bufinfo.len,
|
|
|
|
ctr_params_from_aes(self));
|
|
|
|
break;
|
|
|
|
#endif
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (out_buf != MP_OBJ_NULL) {
|
|
|
|
return out_buf;
|
|
|
|
}
|
2022-08-22 08:08:05 +01:00
|
|
|
return mp_obj_new_bytes_from_vstr(&vstr);
|
2018-01-07 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t cryptolib_aes_encrypt(size_t n_args, const mp_obj_t *args) {
|
2018-01-07 13:13:56 +00:00
|
|
|
return aes_process(n_args, args, true);
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(cryptolib_aes_encrypt_obj, 2, 3, cryptolib_aes_encrypt);
|
2018-01-07 13:13:56 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t cryptolib_aes_decrypt(size_t n_args, const mp_obj_t *args) {
|
2018-01-07 13:13:56 +00:00
|
|
|
return aes_process(n_args, args, false);
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(cryptolib_aes_decrypt_obj, 2, 3, cryptolib_aes_decrypt);
|
2018-01-07 13:13:56 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC const mp_rom_map_elem_t cryptolib_aes_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_encrypt), MP_ROM_PTR(&cryptolib_aes_encrypt_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_decrypt), MP_ROM_PTR(&cryptolib_aes_decrypt_obj) },
|
2018-01-07 13:13:56 +00:00
|
|
|
};
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(cryptolib_aes_locals_dict, cryptolib_aes_locals_dict_table);
|
2018-01-07 13:13:56 +00:00
|
|
|
|
2021-07-14 05:38:38 +01:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
2022-08-18 05:49:57 +01:00
|
|
|
cryptolib_aes_type,
|
2021-07-14 05:38:38 +01:00
|
|
|
MP_QSTR_aes,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-08-18 05:49:57 +01:00
|
|
|
make_new, cryptolib_aes_make_new,
|
|
|
|
locals_dict, &cryptolib_aes_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2018-01-07 13:13:56 +00:00
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_cryptolib_globals_table[] = {
|
2022-08-17 07:18:31 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cryptolib) },
|
2022-08-18 05:49:57 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_aes), MP_ROM_PTR(&cryptolib_aes_type) },
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CONSTS
|
2018-06-15 15:07:47 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_MODE_ECB), MP_ROM_INT(UCRYPTOLIB_MODE_ECB) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_MODE_CBC), MP_ROM_INT(UCRYPTOLIB_MODE_CBC) },
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_CRYPTOLIB_CTR
|
2019-04-23 10:39:05 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_MODE_CTR), MP_ROM_INT(UCRYPTOLIB_MODE_CTR) },
|
|
|
|
#endif
|
2018-06-15 15:07:47 +01:00
|
|
|
#endif
|
2018-01-07 13:13:56 +00:00
|
|
|
};
|
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_cryptolib_globals, mp_module_cryptolib_globals_table);
|
2018-01-07 13:13:56 +00:00
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
const mp_obj_module_t mp_module_cryptolib = {
|
2018-01-07 13:13:56 +00:00
|
|
|
.base = { &mp_type_module },
|
2022-08-18 05:47:56 +01:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_cryptolib_globals,
|
2018-01-07 13:13:56 +00:00
|
|
|
};
|
|
|
|
|
2023-06-02 03:33:25 +01:00
|
|
|
// This module should not be extensible (as it is not a CPython standard
|
|
|
|
// library nor is it necessary to override from the filesystem), however it
|
|
|
|
// has previously been known as `ucryptolib`, so by making it extensible the
|
|
|
|
// `ucryptolib` alias will continue to work.
|
|
|
|
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_cryptolib, mp_module_cryptolib);
|
2022-04-20 07:06:22 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_CRYPTOLIB
|