|
20 | 20 | //| self,
|
21 | 21 | //| key: ReadableBuffer,
|
22 | 22 | //| mode: int = 0,
|
23 |
| -//| iv: Optional[ReadableBuffer] = None, |
| 23 | +//| IV: Optional[ReadableBuffer] = None, |
24 | 24 | //| segment_size: int = 8,
|
25 | 25 | //| ) -> None:
|
26 | 26 | //| """Create a new AES state with the given key.
|
27 | 27 | //|
|
28 | 28 | //| :param ~circuitpython_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key
|
29 | 29 | //| :param int mode: AES mode to use. One of: `MODE_ECB`, `MODE_CBC`, or
|
30 | 30 | //| `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 |
32 | 32 | //|
|
33 | 33 | //| Additional arguments are supported for legacy reasons.
|
34 | 34 | //|
|
@@ -98,33 +98,49 @@ STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
|
98 | 98 | return MP_OBJ_FROM_PTR(self);
|
99 | 99 | }
|
100 | 100 |
|
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) { |
102 | 113 | 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); |
103 | 121 |
|
104 | 122 | 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); |
106 | 125 | const uint8_t *key = bufinfo.buf;
|
107 | 126 | size_t key_length = bufinfo.len;
|
108 |
| - if (key == NULL) { |
109 |
| - mp_raise_ValueError(translate("No key was specified")); |
110 |
| - } |
| 127 | + |
111 | 128 | if ((key_length != 16) && (key_length != 24) && (key_length != 32)) {
|
112 | 129 | mp_raise_ValueError(translate("Key must be 16, 24, or 32 bytes long"));
|
113 | 130 | }
|
114 | 131 |
|
115 | 132 | 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; |
121 | 138 | }
|
122 | 139 |
|
123 | 140 | common_hal_aesio_aes_rekey(self, key, key_length, iv);
|
124 | 141 | return mp_const_none;
|
125 | 142 | }
|
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); |
128 | 144 |
|
129 | 145 | STATIC void validate_length(aesio_aes_obj_t *self, size_t src_length,
|
130 | 146 | size_t dest_length) {
|
|
0 commit comments