Skip to content

Commit b168784

Browse files
committed
aesio: add basic AES encryption and decryption
This adds initial support for an AES module named aesio. This implementation supports only a subset of AES modes, namely ECB, CBC, and CTR modes. Example usage: ``` >>> import aesio >>> >>> key = b'Sixteen byte key' >>> cipher = aesio.AES(key, aesio.MODE_ECB) >>> output = bytearray(16) >>> cipher.encrypt_into(b'Circuit Python!!', output) >>> output bytearray(b'E\x14\x85\x18\x9a\x9c\r\x95>\xa7kV\xa2`\x8b\n') >>> ``` This key is 16-bytes, so it uses AES128. If your key is 24- or 32- bytes long, it will switch to AES192 or AES256 respectively. This has been tested with many of the official NIST test vectors, such as those used in `pycryptodome` at https://github.com/Legrandin/pycryptodome/tree/39626a5b01ce5c1cf51d022be166ad0aea722177/lib/Crypto/SelfTest/Cipher/test_vectors/AES CTR has not been tested as NIST does not provide test vectors for it. Signed-off-by: Sean Cross <[email protected]>
1 parent 90625d1 commit b168784

File tree

10 files changed

+1249
-0
lines changed

10 files changed

+1249
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ endif
9999
###
100100
# Select which builtin modules to compile and include.
101101

102+
ifeq ($(CIRCUITPY_AESIO),1)
103+
SRC_PATTERNS += aesio/%
104+
endif
102105
ifeq ($(CIRCUITPY_ANALOGIO),1)
103106
SRC_PATTERNS += analogio/%
104107
endif
@@ -341,6 +344,8 @@ SRC_SHARED_MODULE_ALL = \
341344
bitbangio/__init__.c \
342345
board/__init__.c \
343346
busio/OneWire.c \
347+
aesio/__init__.c \
348+
aesio/aes.c \
344349
displayio/Bitmap.c \
345350
displayio/ColorConverter.c \
346351
displayio/Display.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ typedef long mp_off_t;
223223
// These CIRCUITPY_xxx values should all be defined in the *.mk files as being on or off.
224224
// So if any are not defined in *.mk, they'll throw an error here.
225225

226+
#if CIRCUITPY_AESIO
227+
extern const struct _mp_obj_module_t aesio_module;
228+
#define AESIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_aesio), (mp_obj_t)&aesio_module },
229+
#else
230+
#define AESIO_MODULE
231+
#endif
232+
226233
#if CIRCUITPY_ANALOGIO
227234
#define ANALOGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
228235
extern const struct _mp_obj_module_t analogio_module;
@@ -619,6 +626,7 @@ extern const struct _mp_obj_module_t ustack_module;
619626
// Some of these definitions will be blank depending on what is turned on and off.
620627
// Some are omitted because they're in MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS above.
621628
#define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \
629+
AESIO_MODULE \
622630
ANALOGIO_MODULE \
623631
AUDIOBUSIO_MODULE \
624632
AUDIOCORE_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ CIRCUITPY_FULL_BUILD ?= 1
3333
CFLAGS += -DCIRCUITPY_FULL_BUILD=$(CIRCUITPY_FULL_BUILD)
3434

3535

36+
CIRCUITPY_AESIO ?= 0
37+
CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO)
38+
3639
CIRCUITPY_ANALOGIO ?= 1
3740
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
3841

shared-bindings/aesio/__init__.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
#include "py/obj.h"
30+
#include "py/runtime.h"
31+
32+
#include "__init__.h"
33+
34+
//| :mod:`aesio` --- AES encryption routines
35+
//| ========================================
36+
//|
37+
//| .. module:: aesio
38+
//| :synopsis: Embedded implementation of AES
39+
//|
40+
//| The `AES` module contains classes used to implement encryption
41+
//| and decryption. It aims to be low overhead in terms of memory.
42+
//|
43+
//|
44+
45+
//| Libraries
46+
//|
47+
//| .. toctree::
48+
//| :maxdepth: 3
49+
//|
50+
//| aes
51+
52+
53+
STATIC const mp_obj_tuple_t mp_aes_key_size_obj = {
54+
{&mp_type_tuple},
55+
3,
56+
{
57+
MP_OBJ_NEW_SMALL_INT(16),
58+
MP_OBJ_NEW_SMALL_INT(24),
59+
MP_OBJ_NEW_SMALL_INT(32),
60+
}
61+
};
62+
63+
STATIC const mp_rom_map_elem_t aesio_module_globals_table[] = {
64+
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_aesio)},
65+
{MP_ROM_QSTR(MP_QSTR_AES), MP_ROM_PTR(&aesio_aes_type) },
66+
{MP_ROM_QSTR(MP_QSTR_MODE_ECB), MP_ROM_INT(AES_MODE_ECB)},
67+
{MP_ROM_QSTR(MP_QSTR_MODE_CBC), MP_ROM_INT(AES_MODE_CBC)},
68+
{MP_ROM_QSTR(MP_QSTR_MODE_CTR), MP_ROM_INT(AES_MODE_CTR)},
69+
{MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(AES_BLOCKLEN)},
70+
{MP_ROM_QSTR(MP_QSTR_key_size), (mp_obj_t)&mp_aes_key_size_obj},
71+
};
72+
73+
STATIC MP_DEFINE_CONST_DICT(aesio_module_globals, aesio_module_globals_table);
74+
75+
const mp_obj_module_t aesio_module = {
76+
.base = {&mp_type_module},
77+
.globals = (mp_obj_dict_t *)&aesio_module_globals,
78+
};
79+

shared-bindings/aesio/__init__.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
29+
30+
#include "shared-module/aesio/__init__.h"
31+
32+
extern const mp_obj_type_t aesio_aes_type;
33+
34+
void common_hal_aesio_aes_construct(aesio_aes_obj_t* self,
35+
const uint8_t* key,
36+
uint32_t key_length,
37+
const uint8_t* iv,
38+
int mode,
39+
int counter);
40+
void common_hal_aesio_aes_rekey(aesio_aes_obj_t* self,
41+
const uint8_t* key,
42+
uint32_t key_length,
43+
const uint8_t* iv);
44+
void common_hal_aesio_aes_set_mode(aesio_aes_obj_t* self,
45+
int mode);
46+
void common_hal_aesio_aes_encrypt(aesio_aes_obj_t* self,
47+
uint8_t* buffer,
48+
size_t len);
49+
void common_hal_aesio_aes_decrypt(aesio_aes_obj_t* self,
50+
uint8_t* buffer,
51+
size_t len);
52+
53+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H

0 commit comments

Comments
 (0)