Skip to content

Commit 3788e13

Browse files
committed
crypto: add initial Crypto module with AESCipher
This adds initial support for the PyCrypto AESCipher module. This implementation supports only a subset of AESCipher modes, namely ECB, CBC, and CTR modes. Example usage adapted from the PyCrypto README for AES: ``` >>> import aes >>> >>> key = b'Sixteen byte key' >>> cipher = aes.AES(key, aes.MODE_ECB) >>> cipher.encrypt(b'Circuit Python!!') 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 dd216b6 commit 3788e13

File tree

10 files changed

+1350
-0
lines changed

10 files changed

+1350
-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_AES),1)
103+
SRC_PATTERNS += aes/%
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+
aes/__init__.c \
348+
aes/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_AES
227+
extern const struct _mp_obj_module_t aes_module;
228+
#define AES_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_aes), (mp_obj_t)&aes_module },
229+
#else
230+
#define AES_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+
AES_MODULE \
622630
ANALOGIO_MODULE \
623631
AUDIOBUSIO_MODULE \
624632
AUDIOCORE_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ endif
3535
CFLAGS += -DCIRCUITPY_FULL_BUILD=$(CIRCUITPY_FULL_BUILD)
3636

3737

38+
ifndef CIRCUITPY_AES
39+
CIRCUITPY_AES = 0
40+
endif
41+
CFLAGS += -DCIRCUITPY_AES=$(CIRCUITPY_AES)
42+
3843
ifndef CIRCUITPY_ANALOGIO
3944
CIRCUITPY_ANALOGIO = 1
4045
endif

shared-bindings/aes/__init__.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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:`aes` --- AES encryption routines
35+
//| ========================================
36+
//|
37+
//| .. module:: aes
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+
46+
STATIC const mp_obj_tuple_t mp_aes_key_size_obj = {
47+
{&mp_type_tuple},
48+
3,
49+
{
50+
MP_OBJ_NEW_SMALL_INT(16),
51+
MP_OBJ_NEW_SMALL_INT(24),
52+
MP_OBJ_NEW_SMALL_INT(32),
53+
}
54+
};
55+
56+
STATIC const mp_rom_map_elem_t aes_module_globals_table[] = {
57+
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_aes)},
58+
{MP_ROM_QSTR(MP_QSTR_AES), MP_ROM_PTR(&aes_aes_type) },
59+
{MP_ROM_QSTR(MP_QSTR_MODE_ECB), MP_ROM_INT(AES_MODE_ECB)},
60+
{MP_ROM_QSTR(MP_QSTR_MODE_CBC), MP_ROM_INT(AES_MODE_CBC)},
61+
{MP_ROM_QSTR(MP_QSTR_MODE_CTR), MP_ROM_INT(AES_MODE_CTR)},
62+
{MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(AES_BLOCKLEN)},
63+
{MP_ROM_QSTR(MP_QSTR_key_size), (mp_obj_t)&mp_aes_key_size_obj},
64+
};
65+
66+
STATIC MP_DEFINE_CONST_DICT(aes_module_globals, aes_module_globals_table);
67+
68+
const mp_obj_module_t aes_module = {
69+
.base = {&mp_type_module},
70+
.globals = (mp_obj_dict_t *)&aes_module_globals,
71+
};
72+

shared-bindings/aes/__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_AES_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_AES_H
29+
30+
#include "shared-module/aes/__init__.h"
31+
32+
extern const mp_obj_type_t aes_aes_type;
33+
34+
void common_hal_aes_construct(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_aes_rekey(aes_obj_t* self,
41+
const uint8_t* key,
42+
uint32_t key_length,
43+
const uint8_t* iv);
44+
void common_hal_aes_set_mode(aes_obj_t* self,
45+
int mode);
46+
void common_hal_aes_encrypt(aes_obj_t* self,
47+
uint8_t* buffer,
48+
size_t len);
49+
void common_hal_aes_decrypt(aes_obj_t* self,
50+
uint8_t* buffer,
51+
size_t len);
52+
53+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AES_H

0 commit comments

Comments
 (0)