Skip to content

Commit 3144e9f

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: ``` >>> from Crypto.Cipher import AES >>> >>> key = b'Sixteen byte key' >>> cipher = AES.new(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 51dbe91 commit 3144e9f

File tree

10 files changed

+1241
-0
lines changed

10 files changed

+1241
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ endif
136136
ifeq ($(CIRCUITPY_BUSIO),1)
137137
SRC_PATTERNS += busio/% bitbangio/OneWire.%
138138
endif
139+
ifeq ($(CIRCUITPY_CRYPTO),1)
140+
SRC_PATTERNS += crypto/%
141+
endif
139142
ifeq ($(CIRCUITPY_DIGITALIO),1)
140143
SRC_PATTERNS += digitalio/%
141144
endif
@@ -342,6 +345,8 @@ SRC_SHARED_MODULE_ALL = \
342345
bitbangio/__init__.c \
343346
board/__init__.c \
344347
busio/OneWire.c \
348+
crypto/__init__.c \
349+
crypto/aes.c \
345350
displayio/Bitmap.c \
346351
displayio/ColorConverter.c \
347352
displayio/Display.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@ extern const struct _mp_obj_module_t busio_module;
321321
#define BUSIO_MODULE
322322
#endif
323323

324+
#if CIRCUITPY_CRYPTO
325+
extern const struct _mp_obj_module_t crypto_module;
326+
#define CRYPTO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_Crypto), (mp_obj_t)&crypto_module },
327+
#else
328+
#define CRYPTO_MODULE
329+
#endif
330+
324331
#if CIRCUITPY_DIGITALIO
325332
extern const struct _mp_obj_module_t digitalio_module;
326333
#define DIGITALIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module },
@@ -637,6 +644,7 @@ extern const struct _mp_obj_module_t ustack_module;
637644
BLEIO_MODULE \
638645
BOARD_MODULE \
639646
BUSIO_MODULE \
647+
CRYPTO_MODULE \
640648
DIGITALIO_MODULE \
641649
DISPLAYIO_MODULE \
642650
FONTIO_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ CIRCUITPY_BUSIO = $(CIRCUITPY_DEFAULT_BUILD)
139139
endif
140140
CFLAGS += -DCIRCUITPY_BUSIO=$(CIRCUITPY_BUSIO)
141141

142+
ifndef CIRCUITPY_CRYPTO
143+
CIRCUITPY_CRYPTO = $(CIRCUITPY_FULL_BUILD)
144+
endif
145+
CFLAGS += -DCIRCUITPY_CRYPTO=$(CIRCUITPY_CRYPTO)
146+
142147
ifndef CIRCUITPY_DIGITALIO
143148
CIRCUITPY_DIGITALIO = $(CIRCUITPY_DEFAULT_BUILD)
144149
endif

shared-bindings/crypto/__init__.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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:`Crypto.Cipher` --- Cipher encryption and decryption routines
35+
//| ==================================================================
36+
//|
37+
//| .. module:: Crypto.Cipher
38+
//| :synopsis: Embedded implementation of PyCrypto
39+
//|
40+
//| The `Crypto.Cipher` submodule contains classes used to implement
41+
//| encryption and decryption routines.
42+
//|
43+
//| Libraries
44+
//|
45+
//| .. toctree::
46+
//| :maxdepth: 3
47+
//|
48+
//| aes
49+
//|
50+
51+
52+
STATIC const mp_rom_map_elem_t cipher_module_globals_table[] = {
53+
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_Cipher)},
54+
{MP_OBJ_NEW_QSTR(MP_QSTR_AES), MP_ROM_PTR(&aes_module)},
55+
};
56+
57+
STATIC MP_DEFINE_CONST_DICT(cipher_module_globals, cipher_module_globals_table);
58+
59+
60+
const mp_obj_module_t cipher_module = {
61+
{&mp_type_module},
62+
.globals = (mp_obj_dict_t *)&cipher_module_globals,
63+
};
64+
65+
//| :mod:`Crypto` --- Cryptographic routines
66+
//| ========================================
67+
//|
68+
//| .. module:: Crypto
69+
//| :synopsis: Embedded implementation of PyCrypto
70+
//|
71+
//| The `Crypto` module contains classes used to implement cryptography.
72+
//| It aims to be API-compatible with the PyCrypto project.
73+
//|
74+
//|
75+
76+
STATIC const mp_rom_map_elem_t crypto_module_globals_table[] = {
77+
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_Crypto)},
78+
{MP_OBJ_NEW_QSTR(MP_QSTR_Cipher), MP_ROM_PTR(&cipher_module)},
79+
};
80+
81+
STATIC MP_DEFINE_CONST_DICT(crypto_module_globals, crypto_module_globals_table);
82+
83+
const mp_obj_module_t crypto_module = {
84+
.base = {&mp_type_module},
85+
.globals = (mp_obj_dict_t *)&crypto_module_globals,
86+
};

shared-bindings/crypto/__init__.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_CRYPTO_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_CRYPTO_H
29+
30+
#include "shared-module/crypto/__init__.h"
31+
32+
extern const mp_obj_module_t crypto_module;
33+
extern const mp_obj_module_t aes_module;
34+
35+
void common_hal_aes_construct(aes_obj_t* self,
36+
const uint8_t* key,
37+
uint32_t key_length,
38+
const uint8_t* iv,
39+
int mode,
40+
int counter);
41+
void common_hal_aes_encrypt(aes_obj_t* self,
42+
uint8_t* buffer,
43+
size_t len);
44+
void common_hal_aes_decrypt(aes_obj_t* self,
45+
uint8_t* buffer,
46+
size_t len);
47+
48+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CRYPTO_H

0 commit comments

Comments
 (0)