Skip to content

Commit 9251c6f

Browse files
authored
Merge pull request adafruit#7684 from isacben/document-aeios-AES-rekey
Document aeios.AES.rekey() and refactor arg validation
2 parents 35e82a5 + b7db289 commit 9251c6f

File tree

1 file changed

+30
-14
lines changed
  • shared-bindings/aesio

1 file changed

+30
-14
lines changed

shared-bindings/aesio/aes.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
//| self,
2121
//| key: ReadableBuffer,
2222
//| mode: int = 0,
23-
//| iv: Optional[ReadableBuffer] = None,
23+
//| IV: Optional[ReadableBuffer] = None,
2424
//| segment_size: int = 8,
2525
//| ) -> None:
2626
//| """Create a new AES state with the given key.
2727
//|
2828
//| :param ~circuitpython_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key
2929
//| :param int mode: AES mode to use. One of: `MODE_ECB`, `MODE_CBC`, or
3030
//| `MODE_CTR`
31-
//| :param ~circuitpython_typing.ReadableBuffer iv: Initialization vector to use for CBC or CTR mode
31+
//| :param ~circuitpython_typing.ReadableBuffer IV: Initialization vector to use for CBC or CTR mode
3232
//|
3333
//| Additional arguments are supported for legacy reasons.
3434
//|
@@ -98,33 +98,49 @@ STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
9898
return MP_OBJ_FROM_PTR(self);
9999
}
100100

101-
STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args) {
101+
//| def rekey(
102+
//| self,
103+
//| key: ReadableBuffer,
104+
//| IV: Optional[ReadableBuffer] = None,
105+
//| ) -> None:
106+
//| """Update the AES state with the given key.
107+
//|
108+
//| :param ~circuitpython_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key
109+
//| :param ~circuitpython_typing.ReadableBuffer IV: Initialization vector to use
110+
//| for CBC or CTR mode"""
111+
//| ...
112+
STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
102113
aesio_aes_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
114+
enum { ARG_key, ARG_IV };
115+
static const mp_arg_t allowed_args[] = {
116+
{MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
117+
{MP_QSTR_IV, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
118+
};
119+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
120+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
103121

104122
mp_buffer_info_t bufinfo;
105-
mp_get_buffer_raise(pos_args[1], &bufinfo, MP_BUFFER_READ);
123+
124+
mp_get_buffer_raise(args[ARG_key].u_obj, &bufinfo, MP_BUFFER_READ);
106125
const uint8_t *key = bufinfo.buf;
107126
size_t key_length = bufinfo.len;
108-
if (key == NULL) {
109-
mp_raise_ValueError(translate("No key was specified"));
110-
}
127+
111128
if ((key_length != 16) && (key_length != 24) && (key_length != 32)) {
112129
mp_raise_ValueError(translate("Key must be 16, 24, or 32 bytes long"));
113130
}
114131

115132
const uint8_t *iv = NULL;
116-
if (n_args > 2) {
117-
mp_get_buffer_raise(pos_args[2], &bufinfo, MP_BUFFER_READ);
118-
size_t iv_length = bufinfo.len;
119-
iv = (const uint8_t *)bufinfo.buf;
120-
(void)mp_arg_validate_length(iv_length, AES_BLOCKLEN, MP_QSTR_IV);
133+
if (args[ARG_IV].u_obj != NULL &&
134+
mp_get_buffer(args[ARG_IV].u_obj, &bufinfo, MP_BUFFER_READ)) {
135+
(void)mp_arg_validate_length(bufinfo.len, AES_BLOCKLEN, MP_QSTR_IV);
136+
137+
iv = bufinfo.buf;
121138
}
122139

123140
common_hal_aesio_aes_rekey(self, key, key_length, iv);
124141
return mp_const_none;
125142
}
126-
127-
MP_DEFINE_CONST_FUN_OBJ_VAR(aesio_aes_rekey_obj, 2, aesio_aes_rekey);
143+
MP_DEFINE_CONST_FUN_OBJ_KW(aesio_aes_rekey_obj, 1, aesio_aes_rekey);
128144

129145
STATIC void validate_length(aesio_aes_obj_t *self, size_t src_length,
130146
size_t dest_length) {

0 commit comments

Comments
 (0)