From 90155925d570313b2ccb3b70549aec0c2327a4a4 Mon Sep 17 00:00:00 2001 From: Isaac Benitez Date: Sun, 5 Mar 2023 12:58:40 -0800 Subject: [PATCH 1/3] Document aeios.AES.rekey() and refactor arg validation --- shared-bindings/aesio/aes.c | 40 ++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/shared-bindings/aesio/aes.c b/shared-bindings/aesio/aes.c index 50e07a5b61..ca0cf735e1 100644 --- a/shared-bindings/aesio/aes.c +++ b/shared-bindings/aesio/aes.c @@ -98,33 +98,49 @@ STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args, return MP_OBJ_FROM_PTR(self); } -STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args) { +//| def rekey( +//| self, +//| key: ReadableBuffer, +//| iv: Optional[ReadableBuffer] = None, +//| ) -> None: +//| """Update the AES state with the given key. +//| +//| :param ~circuitpython_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key +//| :param ~circuitpython_typing.ReadableBuffer iv: Initialization vector to use +//| for CBC or CTR mode""" +//| ... +STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { aesio_aes_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + enum { ARG_key, ARG_IV }; + static const mp_arg_t allowed_args[] = { + {MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} }, + {MP_QSTR_IV, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_buffer_info_t bufinfo; - mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_READ); + + mp_get_buffer_raise(args[ARG_key].u_obj, &bufinfo, MP_BUFFER_READ); const uint8_t *key = bufinfo.buf; size_t key_length = bufinfo.len; - if (key == NULL) { - mp_raise_ValueError(translate("No key was specified")); - } + if ((key_length != 16) && (key_length != 24) && (key_length != 32)) { mp_raise_ValueError(translate("Key must be 16, 24, or 32 bytes long")); } const uint8_t *iv = NULL; - if (n_args > 2) { - mp_get_buffer_raise(pos_args[2], &bufinfo, MP_BUFFER_READ); - size_t iv_length = bufinfo.len; - iv = (const uint8_t *)bufinfo.buf; - (void)mp_arg_validate_length(iv_length, AES_BLOCKLEN, MP_QSTR_IV); + if (args[ARG_IV].u_obj != NULL && + mp_get_buffer(args[ARG_IV].u_obj, &bufinfo, MP_BUFFER_READ)) { + (void)mp_arg_validate_length(bufinfo.len, AES_BLOCKLEN, MP_QSTR_IV); + + iv = bufinfo.buf; } common_hal_aesio_aes_rekey(self, key, key_length, iv); return mp_const_none; } - -MP_DEFINE_CONST_FUN_OBJ_VAR(aesio_aes_rekey_obj, 2, aesio_aes_rekey); +MP_DEFINE_CONST_FUN_OBJ_KW(aesio_aes_rekey_obj, 1, aesio_aes_rekey); STATIC void validate_length(aesio_aes_obj_t *self, size_t src_length, size_t dest_length) { From 8955caff442ebe2b0ab7bd752b45b08bab7e98e0 Mon Sep 17 00:00:00 2001 From: Isaac Benitez Date: Mon, 6 Mar 2023 10:38:28 -0800 Subject: [PATCH 2/3] Fix capitalization of the IV argument (documentation) --- shared-bindings/aesio/aes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/aesio/aes.c b/shared-bindings/aesio/aes.c index ca0cf735e1..8572b58e9e 100644 --- a/shared-bindings/aesio/aes.c +++ b/shared-bindings/aesio/aes.c @@ -20,7 +20,7 @@ //| self, //| key: ReadableBuffer, //| mode: int = 0, -//| iv: Optional[ReadableBuffer] = None, +//| IV: Optional[ReadableBuffer] = None, //| segment_size: int = 8, //| ) -> None: //| """Create a new AES state with the given key. @@ -101,7 +101,7 @@ STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args, //| def rekey( //| self, //| key: ReadableBuffer, -//| iv: Optional[ReadableBuffer] = None, +//| IV: Optional[ReadableBuffer] = None, //| ) -> None: //| """Update the AES state with the given key. //| From b7db2896d16098d46d742771100130e4d0776590 Mon Sep 17 00:00:00 2001 From: Isaac Benitez Date: Tue, 7 Mar 2023 10:12:57 -0800 Subject: [PATCH 3/3] Second fix for the IV argument capitalization (documentation) --- shared-bindings/aesio/aes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/aesio/aes.c b/shared-bindings/aesio/aes.c index 8572b58e9e..c5c50515ea 100644 --- a/shared-bindings/aesio/aes.c +++ b/shared-bindings/aesio/aes.c @@ -28,7 +28,7 @@ //| :param ~circuitpython_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key //| :param int mode: AES mode to use. One of: `MODE_ECB`, `MODE_CBC`, or //| `MODE_CTR` -//| :param ~circuitpython_typing.ReadableBuffer iv: Initialization vector to use for CBC or CTR mode +//| :param ~circuitpython_typing.ReadableBuffer IV: Initialization vector to use for CBC or CTR mode //| //| Additional arguments are supported for legacy reasons. //| @@ -106,7 +106,7 @@ STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args, //| """Update the AES state with the given key. //| //| :param ~circuitpython_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key -//| :param ~circuitpython_typing.ReadableBuffer iv: Initialization vector to use +//| :param ~circuitpython_typing.ReadableBuffer IV: Initialization vector to use //| for CBC or CTR mode""" //| ... STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {