Skip to content

Commit fea0103

Browse files
authored
Merge pull request #5638 from jepler/test-aesio
Test aesio
2 parents 39cc38e + 1654f5f commit fea0103

File tree

5 files changed

+162
-18
lines changed

5 files changed

+162
-18
lines changed

ports/unix/variants/coverage/mpconfigvariant.mk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ SRC_BITMAP := \
3131
$(patsubst ../../%,%,$(wildcard ../../shared-bindings/gifio/*.c ../../shared-module/gifio/*.c)) \
3232
shared/runtime/context_manager_helpers.c \
3333
displayio_min.c \
34+
shared-bindings/aesio/aes.c \
35+
shared-bindings/aesio/__init__.c \
3436
shared-bindings/bitmaptools/__init__.c \
3537
shared-bindings/displayio/Bitmap.c \
3638
shared-bindings/rainbowio/__init__.c \
3739
shared-bindings/util.c \
40+
shared-module/aesio/aes.c \
41+
shared-module/aesio/__init__.c \
3842
shared-module/bitmaptools/__init__.c \
3943
shared-module/displayio/area.c \
4044
shared-module/displayio/Bitmap.c \
@@ -45,7 +49,7 @@ SRC_BITMAP := \
4549
$(info $(SRC_BITMAP))
4650
SRC_C += $(SRC_BITMAP)
4751

48-
CFLAGS += -DCIRCUITPY_GIFIO=1 -DCIRCUITPY_DISPLAYIO_UNIX=1 -DCIRCUITPY_BITMAPTOOLS=1 -DCIRCUITPY_RAINBOWIO=1
52+
CFLAGS += -DCIRCUITPY_GIFIO=1 -DCIRCUITPY_DISPLAYIO_UNIX=1 -DCIRCUITPY_BITMAPTOOLS=1 -DCIRCUITPY_RAINBOWIO=1 -DCIRCUITPY_AESIO=1
4953

5054
SRC_C += coverage.c
5155
SRC_CXX += coveragecpp.cpp

shared-bindings/aesio/aes.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99

1010
// Defined at the end of this file
1111

12+
//| MODE_ECB: int
13+
//| MODE_CBC: int
14+
//| MODE_CTR: int
15+
//|
1216
//| class AES:
1317
//| """Encrypt and decrypt AES streams"""
1418
//|
1519
//| def __init__(self, key: ReadableBuffer, mode: int = 0, iv: Optional[ReadableBuffer] = None, segment_size: int = 8) -> None:
1620
//| """Create a new AES state with the given key.
1721
//|
1822
//| :param ~_typing.ReadableBuffer key: A 16-, 24-, or 32-byte key
19-
//| :param int mode: AES mode to use. One of: AES.MODE_ECB, AES.MODE_CBC, or
20-
//| AES.MODE_CTR
23+
//| :param int mode: AES mode to use. One of: `MODE_ECB`, `MODE_CBC`, or
24+
//| `MODE_CTR`
2125
//| :param ~_typing.ReadableBuffer iv: Initialization vector to use for CBC or CTR mode
2226
//|
2327
//| Additional arguments are supported for legacy reasons.
@@ -30,7 +34,7 @@
3034
//| key = b'Sixteen byte key'
3135
//| inp = b'CircuitPython!!!' # Note: 16-bytes long
3236
//| outp = bytearray(len(inp))
33-
//| cipher = aesio.AES(key, aesio.mode.MODE_ECB)
37+
//| cipher = aesio.AES(key, aesio.MODE_ECB)
3438
//| cipher.encrypt_into(inp, outp)
3539
//| hexlify(outp)"""
3640
//| ...
@@ -41,10 +45,10 @@ STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
4145
(void)type;
4246
enum { ARG_key, ARG_mode, ARG_IV, ARG_counter, ARG_segment_size };
4347
static const mp_arg_t allowed_args[] = {
44-
{MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED},
45-
{MP_QSTR_mode, MP_ARG_INT, {.u_int = AES_MODE_ECB}},
46-
{MP_QSTR_IV, MP_ARG_OBJ},
47-
{MP_QSTR_counter, MP_ARG_OBJ},
48+
{MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
49+
{MP_QSTR_mode, MP_ARG_INT, {.u_int = AES_MODE_ECB} },
50+
{MP_QSTR_IV, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
51+
{MP_QSTR_counter, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
4852
{MP_QSTR_segment_size, MP_ARG_INT, {.u_int = 8}},
4953
};
5054
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];

tests/circuitpython/aes.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import aesio
2+
from binascii import hexlify, unhexlify
3+
4+
# doc example
5+
key = b"Sixteen byte key"
6+
inp = b"CircuitPython!!!" # Note: 16-bytes long
7+
outp = bytearray(len(inp))
8+
cipher = aesio.AES(key, aesio.MODE_ECB)
9+
cipher.encrypt_into(inp, outp)
10+
print(str(hexlify(outp), ""))
11+
12+
cipher = aesio.AES(key, aesio.MODE_ECB)
13+
cipher.decrypt_into(outp, outp)
14+
print(str(outp, ""))
15+
print()
16+
17+
print("ECB")
18+
# ECB mode test vector, from the aes.c source
19+
plaintext = unhexlify(
20+
"6bc1bee22e409f96e93d7e117393172a"
21+
"ae2d8a571e03ac9c9eb76fac45af8e51"
22+
"30c81c46a35ce411e5fbc1191a0a52ef"
23+
"f69f2445df4f9b17ad2b417be66c3710"
24+
)
25+
26+
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
27+
28+
cyphertext = bytearray(len(plaintext))
29+
cipher = aesio.AES(key, aesio.MODE_ECB)
30+
for i in range(0, len(plaintext), 16):
31+
output = memoryview(cyphertext)[i : i + 16]
32+
cipher.encrypt_into(plaintext[i : i + 16], output)
33+
print(str(hexlify(output), ""))
34+
print()
35+
36+
plaintext = bytearray(len(plaintext))
37+
cipher = aesio.AES(key, aesio.MODE_ECB)
38+
for i in range(0, len(plaintext), 16):
39+
output = memoryview(plaintext)[i : i + 16]
40+
cipher.decrypt_into(cyphertext[i : i + 16], output)
41+
print(str(hexlify(output), ""))
42+
print()
43+
44+
print("CBC")
45+
# CBC128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p50
46+
47+
plaintext = unhexlify(
48+
"6bc1bee22e409f96e93d7e117393172a"
49+
"ae2d8a571e03ac9c9eb76fac45af8e51"
50+
"30c81c46a35ce411e5fbc1191a0a52ef"
51+
"f69f2445df4f9b17ad2b417be66c3710"
52+
)
53+
54+
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
55+
iv = unhexlify("000102030405060708090a0b0c0d0e0f")
56+
cyphertext = bytearray(len(plaintext))
57+
cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv)
58+
for i in range(0, len(plaintext), 16):
59+
output = memoryview(cyphertext)[i : i + 16]
60+
cipher.encrypt_into(plaintext[i : i + 16], output)
61+
print(str(hexlify(output), ""))
62+
print()
63+
64+
plaintext = bytearray(len(plaintext))
65+
cipher = aesio.AES(key, aesio.MODE_CBC, IV=iv)
66+
for i in range(0, len(plaintext), 16):
67+
output = memoryview(plaintext)[i : i + 16]
68+
cipher.decrypt_into(cyphertext[i : i + 16], output)
69+
print(str(hexlify(output), ""))
70+
print()
71+
72+
73+
print("CTR")
74+
# CTR128-AES128 test vector from NIST Special Publication 800-38A, 2001 edition, p55
75+
76+
plaintext = unhexlify(
77+
"6bc1bee22e409f96e93d7e117393172a"
78+
"ae2d8a571e03ac9c9eb76fac45af8e51"
79+
"30c81c46a35ce411e5fbc1191a0a52ef"
80+
"f69f2445df4f9b17ad2b417be66c3710"
81+
)
82+
83+
key = unhexlify("2b7e151628aed2a6abf7158809cf4f3c")
84+
counter = unhexlify("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff")
85+
cyphertext = bytearray(len(plaintext))
86+
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
87+
for i in range(0, len(plaintext), 16):
88+
output = memoryview(cyphertext)[i : i + 16]
89+
cipher.encrypt_into(plaintext[i : i + 16], output)
90+
print(str(hexlify(output), ""))
91+
print()
92+
93+
plaintext = bytearray(len(plaintext))
94+
cipher = aesio.AES(key, aesio.MODE_CTR, IV=counter)
95+
for i in range(0, len(plaintext), 16):
96+
output = memoryview(plaintext)[i : i + 16]
97+
cipher.decrypt_into(cyphertext[i : i + 16], output)
98+
print(str(hexlify(output), ""))
99+
print()

tests/circuitpython/aes.py.exp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
abb1a1f98f57409e455ac06e71535ffe
2+
CircuitPython!!!
3+
4+
ECB
5+
3ad77bb40d7a3660a89ecaf32466ef97
6+
f5d3d58503b9699de785895a96fdbaaf
7+
43b1cd7f598ece23881b00e3ed030688
8+
7b0c785e27e8ad3f8223207104725dd4
9+
10+
6bc1bee22e409f96e93d7e117393172a
11+
ae2d8a571e03ac9c9eb76fac45af8e51
12+
30c81c46a35ce411e5fbc1191a0a52ef
13+
f69f2445df4f9b17ad2b417be66c3710
14+
15+
CBC
16+
7649abac8119b246cee98e9b12e9197d
17+
5086cb9b507219ee95db113a917678b2
18+
73bed6b8e3c1743b7116e69e22229516
19+
3ff1caa1681fac09120eca307586e1a7
20+
21+
6bc1bee22e409f96e93d7e117393172a
22+
ae2d8a571e03ac9c9eb76fac45af8e51
23+
30c81c46a35ce411e5fbc1191a0a52ef
24+
f69f2445df4f9b17ad2b417be66c3710
25+
26+
CTR
27+
874d6191b620e3261bef6864990db6ce
28+
9806f66b7970fdff8617187bb9fffdff
29+
5ae4df3edbd5d35e5b4f09020db03eab
30+
1e031dda2fbe03d1792170a0f3009cee
31+
32+
6bc1bee22e409f96e93d7e117393172a
33+
ae2d8a571e03ac9c9eb76fac45af8e51
34+
30c81c46a35ce411e5fbc1191a0a52ef
35+
f69f2445df4f9b17ad2b417be66c3710
36+

tests/unix/extra_coverage.py.exp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ RuntimeError:
2929
ame__
3030
mport
3131

32-
builtins micropython _thread array
33-
binascii bitmaptools btree cexample
34-
cmath collections cppexample displayio
35-
errno ffi framebuf gc
36-
gifio hashlib json math
37-
qrio rainbowio re sys
38-
termios ubinascii uctypes uerrno
39-
uheapq uio ujson ulab
40-
uos urandom ure uselect
41-
ustruct utime utimeq uzlib
32+
builtins micropython _thread aesio
33+
array binascii bitmaptools btree
34+
cexample cmath collections cppexample
35+
displayio errno ffi framebuf
36+
gc gifio hashlib json
37+
math qrio rainbowio re
38+
sys termios ubinascii uctypes
39+
uerrno uheapq uio ujson
40+
ulab uos urandom ure
41+
uselect ustruct utime utimeq
42+
uzlib
4243
ime
4344

4445
utime utimeq

0 commit comments

Comments
 (0)