Merge branch 'master' into Issue2812
This commit is contained in:
commit
4712f9f104
@ -20,7 +20,7 @@ CIRCUITPY_I2CSLAVE = 0
|
||||
CIRCUITPY_ROTARYIO = 0
|
||||
CIRCUITPY_RTC = 0
|
||||
|
||||
CFLAGS_INLINE_LIMIT = 60
|
||||
CFLAGS_INLINE_LIMIT = 55
|
||||
SUPEROPT_GC = 0
|
||||
|
||||
# Include these Python libraries in firmware.
|
||||
|
@ -93,6 +93,10 @@ endif
|
||||
|
||||
CFLAGS += $(INC) -Wall -Wno-cast-align -std=gnu11 -nostdlib $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT)
|
||||
|
||||
# TODO: add these when -Werror is applied
|
||||
# Disable some warnings, as do most ports. NXP SDK causes undef, tinyusb causes cast-align
|
||||
# CFLAGS += -Wno-undef -Wno-cast-align
|
||||
|
||||
CFLAGS += \
|
||||
-mthumb \
|
||||
-mapcs \
|
||||
|
@ -25,7 +25,6 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
//TODO
|
||||
#include "shared-bindings/busio/SPI.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
@ -35,48 +34,12 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
//bool never_reset_sercoms[SERCOM_INST_NUM];
|
||||
//
|
||||
//void never_reset_sercom(Sercom* sercom) {
|
||||
// // Reset all SERCOMs except the ones being used by on-board devices.
|
||||
// Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
|
||||
// for (int i = 0; i < SERCOM_INST_NUM; i++) {
|
||||
// if (sercom_instances[i] == sercom) {
|
||||
// never_reset_sercoms[i] = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void allow_reset_sercom(Sercom* sercom) {
|
||||
// // Reset all SERCOMs except the ones being used by on-board devices.
|
||||
// Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
|
||||
// for (int i = 0; i < SERCOM_INST_NUM; i++) {
|
||||
// if (sercom_instances[i] == sercom) {
|
||||
// never_reset_sercoms[i] = false;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void reset_sercoms(void) {
|
||||
// // Reset all SERCOMs except the ones being used by on-board devices.
|
||||
// Sercom *sercom_instances[SERCOM_INST_NUM] = SERCOM_INSTS;
|
||||
// for (int i = 0; i < SERCOM_INST_NUM; i++) {
|
||||
// if (never_reset_sercoms[i]) {
|
||||
// continue;
|
||||
// }
|
||||
// #ifdef MICROPY_HW_APA102_SERCOM
|
||||
// if (sercom_instances[i] == MICROPY_HW_APA102_SERCOM) {
|
||||
// continue;
|
||||
// }
|
||||
// #endif
|
||||
// // SWRST is same for all modes of SERCOMs.
|
||||
// sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1;
|
||||
// }
|
||||
//}
|
||||
//arrays use 0 based numbering: SPI1 is stored at index 0
|
||||
#define MAX_SPI 4
|
||||
STATIC bool reserved_spi[MAX_SPI];
|
||||
STATIC bool never_reset_spi[MAX_SPI];
|
||||
|
||||
static void config_periph_pin(const mcu_periph_obj_t *periph) {
|
||||
STATIC void config_periph_pin(const mcu_periph_obj_t *periph) {
|
||||
IOMUXC_SetPinMux(
|
||||
periph->pin->mux_reg, periph->mux_mode,
|
||||
periph->input_reg, periph->input_idx,
|
||||
@ -97,52 +60,117 @@ static void config_periph_pin(const mcu_periph_obj_t *periph) {
|
||||
|
||||
#define LPSPI_MASTER_CLK_FREQ (CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk) / (CLOCK_GetDiv(kCLOCK_LpspiDiv)))
|
||||
|
||||
void spi_reset(void) {
|
||||
for (int i = 0; i < MAX_SPI; i++) {
|
||||
reserved_spi[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
||||
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *mosi,
|
||||
const mcu_pin_obj_t *miso) {
|
||||
|
||||
// TODO: Allow none mosi or miso
|
||||
const uint32_t sck_count = MP_ARRAY_SIZE(mcu_spi_sck_list);
|
||||
const uint32_t miso_count = MP_ARRAY_SIZE(mcu_spi_miso_list);
|
||||
const uint32_t mosi_count = MP_ARRAY_SIZE(mcu_spi_mosi_list);
|
||||
bool spi_taken = false;
|
||||
|
||||
const uint32_t sck_count = sizeof(mcu_spi_sck_list) / sizeof(mcu_periph_obj_t);
|
||||
const uint32_t miso_count = sizeof(mcu_spi_miso_list) / sizeof(mcu_periph_obj_t);
|
||||
const uint32_t mosi_count = sizeof(mcu_spi_mosi_list) / sizeof(mcu_periph_obj_t);
|
||||
|
||||
for (uint32_t i = 0; i < sck_count; ++i) {
|
||||
if (mcu_spi_sck_list[i].pin != clock)
|
||||
for (uint i = 0; i < sck_count; i++) {
|
||||
if (mcu_spi_sck_list[i].pin != clock) {
|
||||
continue;
|
||||
|
||||
for (uint32_t j = 0; j < miso_count; ++j) {
|
||||
if (mcu_spi_miso_list[j].pin != miso)
|
||||
continue;
|
||||
|
||||
if (mcu_spi_miso_list[j].bank_idx != mcu_spi_sck_list[i].bank_idx)
|
||||
continue;
|
||||
|
||||
for (uint32_t k = 0; k < mosi_count; ++k) {
|
||||
if (mcu_spi_mosi_list[k].pin != mosi)
|
||||
}
|
||||
//if both MOSI and MISO exist, loop search normally
|
||||
if ((mosi != NULL) && (miso != NULL)) {
|
||||
for (uint j = 0; j < mosi_count; j++) {
|
||||
if ((mcu_spi_mosi_list[i].pin != mosi)
|
||||
|| (mcu_spi_sck_list[i].bank_idx != mcu_spi_mosi_list[j].bank_idx)){
|
||||
continue;
|
||||
|
||||
if (mcu_spi_mosi_list[k].bank_idx != mcu_spi_miso_list[j].bank_idx)
|
||||
continue;
|
||||
|
||||
self->clock_pin = &mcu_spi_sck_list[i];
|
||||
self->miso_pin = &mcu_spi_miso_list[j];
|
||||
self->mosi_pin = &mcu_spi_mosi_list[k];
|
||||
|
||||
}
|
||||
for (uint k = 0; k < miso_count; k++) {
|
||||
if ((mcu_spi_miso_list[k].pin != miso) //everything needs the same index
|
||||
|| (mcu_spi_sck_list[i].bank_idx != mcu_spi_miso_list[k].bank_idx)) {
|
||||
continue;
|
||||
}
|
||||
//keep looking if the SPI is taken, edge case
|
||||
if (reserved_spi[mcu_spi_sck_list[i].bank_idx - 1]) {
|
||||
spi_taken = true;
|
||||
continue;
|
||||
}
|
||||
//store pins if not
|
||||
self->clock = &mcu_spi_sck_list[i];
|
||||
self->mosi = &mcu_spi_mosi_list[j];
|
||||
self->miso = &mcu_spi_miso_list[k];
|
||||
break;
|
||||
}
|
||||
if (self->clock != NULL) {
|
||||
break; // Multi-level break to pick lowest peripheral
|
||||
}
|
||||
}
|
||||
if (self->clock != NULL) {
|
||||
break;
|
||||
}
|
||||
// if just MISO, reduce search
|
||||
} else if (miso != NULL) {
|
||||
for (uint j = 0; j < miso_count; j++) {
|
||||
if ((mcu_spi_miso_list[j].pin != miso) //only SCK and MISO need the same index
|
||||
|| (mcu_spi_sck_list[i].bank_idx != mcu_spi_miso_list[j].bank_idx)) {
|
||||
continue;
|
||||
}
|
||||
if (reserved_spi[mcu_spi_sck_list[i].bank_idx - 1]) {
|
||||
spi_taken = true;
|
||||
continue;
|
||||
}
|
||||
self->clock = &mcu_spi_sck_list[i];
|
||||
self->mosi = NULL;
|
||||
self->miso = &mcu_spi_miso_list[j];
|
||||
break;
|
||||
}
|
||||
if (self->clock != NULL) {
|
||||
break;
|
||||
}
|
||||
// if just MOSI, reduce search
|
||||
} else if (mosi != NULL) {
|
||||
for (uint j = 0; j < mosi_count; j++) {
|
||||
if ((mcu_spi_mosi_list[j].pin != mosi) //only SCK and MOSI need the same index
|
||||
|| (mcu_spi_sck_list[i].bank_idx != mcu_spi_mosi_list[j].bank_idx)) {
|
||||
continue;
|
||||
}
|
||||
if (reserved_spi[mcu_spi_sck_list[i].bank_idx - 1]) {
|
||||
spi_taken = true;
|
||||
continue;
|
||||
}
|
||||
self->clock = &mcu_spi_sck_list[i];
|
||||
self->mosi = &mcu_spi_mosi_list[j];
|
||||
self->miso = NULL;
|
||||
break;
|
||||
}
|
||||
if (self->clock != NULL) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
//throw an error immediately
|
||||
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
|
||||
}
|
||||
}
|
||||
|
||||
if(self->clock_pin == NULL || self->mosi_pin == NULL || self->miso_pin == NULL) {
|
||||
mp_raise_RuntimeError(translate("Invalid SPI pin selection"));
|
||||
if (self->clock != NULL && (self->mosi != NULL || self->miso != NULL)) {
|
||||
self->spi = mcu_spi_banks[self->clock->bank_idx - 1];
|
||||
} else {
|
||||
self->spi = mcu_spi_banks[self->clock_pin->bank_idx - 1];
|
||||
if (spi_taken) {
|
||||
mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
|
||||
} else {
|
||||
mp_raise_ValueError(translate("Invalid SPI pin selection"));
|
||||
}
|
||||
}
|
||||
|
||||
config_periph_pin(self->mosi_pin);
|
||||
config_periph_pin(self->miso_pin);
|
||||
config_periph_pin(self->clock_pin);
|
||||
config_periph_pin(self->clock);
|
||||
if (self->mosi != NULL) {
|
||||
config_periph_pin(self->mosi);
|
||||
}
|
||||
if (self->miso != NULL) {
|
||||
config_periph_pin(self->miso);
|
||||
}
|
||||
reserved_spi[self->clock->bank_idx - 1] = true;
|
||||
|
||||
lpspi_master_config_t config = { 0 };
|
||||
LPSPI_MasterGetDefaultConfig(&config);
|
||||
@ -150,47 +178,27 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
|
||||
// Always start at 250khz which is what SD cards need. They are sensitive to
|
||||
// SPI bus noise before they are put into SPI mode.
|
||||
config.baudRate = 250000;
|
||||
|
||||
LPSPI_MasterInit(self->spi, &config, LPSPI_MASTER_CLK_FREQ);
|
||||
|
||||
LPSPI_Enable(self->spi, false);
|
||||
uint32_t tcrPrescaleValue;
|
||||
self->baudrate = LPSPI_MasterSetBaudRate(self->spi, config.baudRate, LPSPI_MASTER_CLK_FREQ, &tcrPrescaleValue);
|
||||
LPSPI_Enable(self->spi, true);
|
||||
|
||||
claim_pin(self->clock_pin->pin);
|
||||
|
||||
// if (mosi_none) {
|
||||
// self->MOSI_pin = NO_PIN;
|
||||
// } else {
|
||||
// gpio_set_pin_direction(mosi->number, GPIO_DIRECTION_OUT);
|
||||
// gpio_set_pin_pull_mode(mosi->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(mosi->number, mosi_pinmux);
|
||||
// self->MOSI_pin = mosi->number;
|
||||
claim_pin(self->mosi_pin->pin);
|
||||
// }
|
||||
|
||||
// if (miso_none) {
|
||||
// self->MISO_pin = NO_PIN;
|
||||
// } else {
|
||||
// gpio_set_pin_direction(miso->number, GPIO_DIRECTION_IN);
|
||||
// gpio_set_pin_pull_mode(miso->number, GPIO_PULL_OFF);
|
||||
// gpio_set_pin_function(miso->number, miso_pinmux);
|
||||
// self->MISO_pin = miso->number;
|
||||
claim_pin(self->miso_pin->pin);
|
||||
// }
|
||||
claim_pin(self->clock->pin);
|
||||
if (self->mosi != NULL) {
|
||||
claim_pin(self->mosi->pin);
|
||||
}
|
||||
if (self->miso != NULL) {
|
||||
claim_pin(self->miso->pin);
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
|
||||
// never_reset_sercom(self->spi_desc.dev.prvt);
|
||||
|
||||
// never_reset_pin_number(self->clock_pin);
|
||||
// never_reset_pin_number(self->MOSI_pin);
|
||||
// never_reset_pin_number(self->MISO_pin);
|
||||
// TODO
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
|
||||
return self->clock_pin == NULL;
|
||||
return self->clock == NULL;
|
||||
}
|
||||
|
||||
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
|
||||
@ -198,14 +206,7 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
|
||||
return;
|
||||
}
|
||||
|
||||
// allow_reset_sercom(self->spi_desc.dev.prvt);
|
||||
|
||||
// spi_m_sync_disable(&self->spi_desc);
|
||||
// spi_m_sync_deinit(&self->spi_desc);
|
||||
// reset_pin_number(self->clock_pin);
|
||||
// reset_pin_number(self->MOSI_pin);
|
||||
// reset_pin_number(self->MISO_pin);
|
||||
self->clock_pin = NULL;
|
||||
self->clock = NULL;
|
||||
}
|
||||
|
||||
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
|
||||
@ -260,6 +261,9 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self,
|
||||
if (len == 0) {
|
||||
return true;
|
||||
}
|
||||
if (self->mosi == NULL) {
|
||||
mp_raise_ValueError(translate("No MOSI Pin"));
|
||||
}
|
||||
|
||||
lpspi_transfer_t xfer = { 0 };
|
||||
xfer.txData = (uint8_t*)data;
|
||||
@ -278,6 +282,9 @@ bool common_hal_busio_spi_read(busio_spi_obj_t *self,
|
||||
if (len == 0) {
|
||||
return true;
|
||||
}
|
||||
if (self->miso == NULL) {
|
||||
mp_raise_ValueError(translate("No MISO Pin"));
|
||||
}
|
||||
|
||||
LPSPI_SetDummyData(self->spi, write_value);
|
||||
|
||||
@ -296,6 +303,9 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uin
|
||||
if (len == 0) {
|
||||
return true;
|
||||
}
|
||||
if (self->miso == NULL || self->mosi == NULL) {
|
||||
mp_raise_ValueError(translate("Missing MISO or MOSI Pin"));
|
||||
}
|
||||
|
||||
LPSPI_SetDummyData(self->spi, 0xFF);
|
||||
|
||||
|
@ -38,9 +38,11 @@ typedef struct {
|
||||
LPSPI_Type *spi;
|
||||
bool has_lock;
|
||||
uint32_t baudrate;
|
||||
const mcu_periph_obj_t *clock_pin;
|
||||
const mcu_periph_obj_t *mosi_pin;
|
||||
const mcu_periph_obj_t *miso_pin;
|
||||
const mcu_periph_obj_t *clock;
|
||||
const mcu_periph_obj_t *mosi;
|
||||
const mcu_periph_obj_t *miso;
|
||||
} busio_spi_obj_t;
|
||||
|
||||
void spi_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_MIMXRT10XX_COMMON_HAL_BUSIO_SPI_H
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "common-hal/pulseio/PulseOut.h"
|
||||
#include "common-hal/pulseio/PWMOut.h"
|
||||
#include "common-hal/rtc/RTC.h"
|
||||
#include "common-hal/busio/SPI.h"
|
||||
|
||||
#include "reset.h"
|
||||
|
||||
@ -267,7 +268,7 @@ safe_mode_t port_init(void) {
|
||||
}
|
||||
|
||||
void reset_port(void) {
|
||||
//reset_sercoms();
|
||||
spi_reset();
|
||||
|
||||
#if CIRCUITPY_AUDIOIO
|
||||
audio_dma_reset();
|
||||
|
@ -134,3 +134,15 @@ GPIO_TypeDef * pin_port(uint8_t pin_port) {
|
||||
uint16_t pin_mask(uint8_t pin_number) {
|
||||
return 1<<pin_number;
|
||||
}
|
||||
|
||||
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t* pin) {
|
||||
return pin->port * 16 + pin->number;
|
||||
}
|
||||
|
||||
void common_hal_mcu_pin_claim(const mcu_pin_obj_t* pin) {
|
||||
claim_pin(pin);
|
||||
}
|
||||
|
||||
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
|
||||
reset_pin_number(pin_no / 16, pin_no % 16);
|
||||
}
|
||||
|
54
ports/stm/common-hal/rgbmatrix/RGBMatrix.c
Normal file
54
ports/stm/common-hal/rgbmatrix/RGBMatrix.c
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "common-hal/rgbmatrix/RGBMatrix.h"
|
||||
|
||||
#include STM32_HAL_H
|
||||
|
||||
extern void _PM_IRQ_HANDLER(void);
|
||||
|
||||
void *common_hal_rgbmatrix_timer_allocate() {
|
||||
// TODO(jepler) properly handle resource allocation including never-reset
|
||||
return TIM6;
|
||||
}
|
||||
|
||||
|
||||
void common_hal_rgbmatrix_timer_enable(void* ptr) {
|
||||
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
|
||||
}
|
||||
|
||||
void common_hal_rgbmatrix_timer_disable(void* ptr) {
|
||||
TIM_TypeDef *tim = (TIM_TypeDef*)ptr;
|
||||
tim->DIER &= ~TIM_DIER_UIE;
|
||||
HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn);
|
||||
}
|
||||
|
||||
void common_hal_rgbmatrix_timer_free(void* ptr) {
|
||||
common_hal_rgbmatrix_timer_disable(ptr);
|
||||
// TODO(jepler) properly handle resource allocation including never-reset
|
||||
}
|
35
ports/stm/common-hal/rgbmatrix/RGBMatrix.h
Normal file
35
ports/stm/common-hal/rgbmatrix/RGBMatrix.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Jeff Epler for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RGBMATRIX_RGBMATRIX_H
|
||||
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_RGBMATRIX_RGBMATRIX_H
|
||||
|
||||
void *common_hal_rgbmatrix_timer_allocate(void);
|
||||
void common_hal_rgbmatrix_timer_enable(void*);
|
||||
void common_hal_rgbmatrix_timer_disable(void*);
|
||||
void common_hal_rgbmatrix_timer_free(void*);
|
||||
|
||||
#endif
|
0
ports/stm/common-hal/rgbmatrix/__init__.c
Normal file
0
ports/stm/common-hal/rgbmatrix/__init__.c
Normal file
0
ports/stm/common-hal/rgbmatrix/__init__.h
Normal file
0
ports/stm/common-hal/rgbmatrix/__init__.h
Normal file
@ -3,6 +3,11 @@ LONGINT_IMPL = MPZ
|
||||
INTERNAL_LIBM = 1
|
||||
USB_SERIAL_NUMBER_LENGTH = 24
|
||||
|
||||
ifeq ($(MCU_VARIANT),STM32F405xx)
|
||||
CIRCUITPY_FRAMEBUFFERIO = 1
|
||||
CIRCUITPY_RGBMATRIX = 1
|
||||
endif
|
||||
|
||||
ifeq ($(MCU_SERIES),F4)
|
||||
# Not yet implemented common-hal modules:
|
||||
CIRCUITPY_AUDIOBUSIO = 0
|
||||
|
@ -99,6 +99,9 @@ endif
|
||||
###
|
||||
# Select which builtin modules to compile and include.
|
||||
|
||||
ifeq ($(CIRCUITPY_AESIO),1)
|
||||
SRC_PATTERNS += aesio/%
|
||||
endif
|
||||
ifeq ($(CIRCUITPY_ANALOGIO),1)
|
||||
SRC_PATTERNS += analogio/%
|
||||
endif
|
||||
@ -341,6 +344,8 @@ SRC_SHARED_MODULE_ALL = \
|
||||
bitbangio/__init__.c \
|
||||
board/__init__.c \
|
||||
busio/OneWire.c \
|
||||
aesio/__init__.c \
|
||||
aesio/aes.c \
|
||||
displayio/Bitmap.c \
|
||||
displayio/ColorConverter.c \
|
||||
displayio/Display.c \
|
||||
|
@ -223,6 +223,13 @@ typedef long mp_off_t;
|
||||
// These CIRCUITPY_xxx values should all be defined in the *.mk files as being on or off.
|
||||
// So if any are not defined in *.mk, they'll throw an error here.
|
||||
|
||||
#if CIRCUITPY_AESIO
|
||||
extern const struct _mp_obj_module_t aesio_module;
|
||||
#define AESIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_aesio), (mp_obj_t)&aesio_module },
|
||||
#else
|
||||
#define AESIO_MODULE
|
||||
#endif
|
||||
|
||||
#if CIRCUITPY_ANALOGIO
|
||||
#define ANALOGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
|
||||
extern const struct _mp_obj_module_t analogio_module;
|
||||
@ -619,6 +626,7 @@ extern const struct _mp_obj_module_t ustack_module;
|
||||
// Some of these definitions will be blank depending on what is turned on and off.
|
||||
// Some are omitted because they're in MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS above.
|
||||
#define MICROPY_PORT_BUILTIN_MODULES_STRONG_LINKS \
|
||||
AESIO_MODULE \
|
||||
ANALOGIO_MODULE \
|
||||
AUDIOBUSIO_MODULE \
|
||||
AUDIOCORE_MODULE \
|
||||
|
@ -33,6 +33,9 @@ CIRCUITPY_FULL_BUILD ?= 1
|
||||
CFLAGS += -DCIRCUITPY_FULL_BUILD=$(CIRCUITPY_FULL_BUILD)
|
||||
|
||||
|
||||
CIRCUITPY_AESIO ?= 0
|
||||
CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO)
|
||||
|
||||
CIRCUITPY_ANALOGIO ?= 1
|
||||
CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO)
|
||||
|
||||
|
@ -53,36 +53,39 @@
|
||||
//| PixelBuf
|
||||
|
||||
|
||||
//| .. function:: wheel(n)
|
||||
//| .. function:: colorwheel(n)
|
||||
//|
|
||||
//| C implementation of the common wheel() function found in many examples.
|
||||
//| Returns the colorwheel RGB value as an integer value for n (usable in :py:class:`PixelBuf`, neopixel, and dotstar).
|
||||
//|
|
||||
//| .. function:: wheel(n)
|
||||
//| Use of wheel() is deprecated. Please use colorwheel().
|
||||
|
||||
STATIC mp_obj_t pixelbuf_wheel(mp_obj_t n) {
|
||||
STATIC mp_obj_t pixelbuf_colorwheel(mp_obj_t n) {
|
||||
return MP_OBJ_NEW_SMALL_INT(colorwheel(MP_OBJ_IS_SMALL_INT(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_float_get(n)));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_wheel_obj, pixelbuf_wheel);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_colorwheel_obj, pixelbuf_colorwheel);
|
||||
|
||||
const int32_t colorwheel(float pos) {
|
||||
if (pos > 255) {
|
||||
pos = pos - ((uint32_t)(pos / 256) * 256);
|
||||
}
|
||||
if (pos < 85)
|
||||
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3)) << 8;
|
||||
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
|
||||
else if (pos < 170) {
|
||||
pos -= 85;
|
||||
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3);
|
||||
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
|
||||
} else {
|
||||
pos -= 170;
|
||||
return (uint8_t)(pos * 3) << 8 | (uint8_t)(255 - pos * 3);
|
||||
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
|
||||
}
|
||||
}
|
||||
|
||||
STATIC const mp_rom_map_elem_t pixelbuf_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pixelbuf) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_PixelBuf), MP_ROM_PTR(&pixelbuf_pixelbuf_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_wheel_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_colorwheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(pixelbuf_module_globals, pixelbuf_module_globals_table);
|
||||
|
79
shared-bindings/aesio/__init__.c
Normal file
79
shared-bindings/aesio/__init__.c
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "__init__.h"
|
||||
|
||||
//| :mod:`aesio` --- AES encryption routines
|
||||
//| ========================================
|
||||
//|
|
||||
//| .. module:: aesio
|
||||
//| :synopsis: Embedded implementation of AES
|
||||
//|
|
||||
//| The `AES` module contains classes used to implement encryption
|
||||
//| and decryption. It aims to be low overhead in terms of memory.
|
||||
//|
|
||||
//|
|
||||
|
||||
//| Libraries
|
||||
//|
|
||||
//| .. toctree::
|
||||
//| :maxdepth: 3
|
||||
//|
|
||||
//| aes
|
||||
|
||||
|
||||
STATIC const mp_obj_tuple_t mp_aes_key_size_obj = {
|
||||
{&mp_type_tuple},
|
||||
3,
|
||||
{
|
||||
MP_OBJ_NEW_SMALL_INT(16),
|
||||
MP_OBJ_NEW_SMALL_INT(24),
|
||||
MP_OBJ_NEW_SMALL_INT(32),
|
||||
}
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t aesio_module_globals_table[] = {
|
||||
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_aesio)},
|
||||
{MP_ROM_QSTR(MP_QSTR_AES), MP_ROM_PTR(&aesio_aes_type) },
|
||||
{MP_ROM_QSTR(MP_QSTR_MODE_ECB), MP_ROM_INT(AES_MODE_ECB)},
|
||||
{MP_ROM_QSTR(MP_QSTR_MODE_CBC), MP_ROM_INT(AES_MODE_CBC)},
|
||||
{MP_ROM_QSTR(MP_QSTR_MODE_CTR), MP_ROM_INT(AES_MODE_CTR)},
|
||||
{MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(AES_BLOCKLEN)},
|
||||
{MP_ROM_QSTR(MP_QSTR_key_size), (mp_obj_t)&mp_aes_key_size_obj},
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(aesio_module_globals, aesio_module_globals_table);
|
||||
|
||||
const mp_obj_module_t aesio_module = {
|
||||
.base = {&mp_type_module},
|
||||
.globals = (mp_obj_dict_t *)&aesio_module_globals,
|
||||
};
|
||||
|
53
shared-bindings/aesio/__init__.h
Normal file
53
shared-bindings/aesio/__init__.h
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* This file is part of the Micro Python project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
|
||||
#define MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
|
||||
|
||||
#include "shared-module/aesio/__init__.h"
|
||||
|
||||
extern const mp_obj_type_t aesio_aes_type;
|
||||
|
||||
void common_hal_aesio_aes_construct(aesio_aes_obj_t* self,
|
||||
const uint8_t* key,
|
||||
uint32_t key_length,
|
||||
const uint8_t* iv,
|
||||
int mode,
|
||||
int counter);
|
||||
void common_hal_aesio_aes_rekey(aesio_aes_obj_t* self,
|
||||
const uint8_t* key,
|
||||
uint32_t key_length,
|
||||
const uint8_t* iv);
|
||||
void common_hal_aesio_aes_set_mode(aesio_aes_obj_t* self,
|
||||
int mode);
|
||||
void common_hal_aesio_aes_encrypt(aesio_aes_obj_t* self,
|
||||
uint8_t* buffer,
|
||||
size_t len);
|
||||
void common_hal_aesio_aes_decrypt(aesio_aes_obj_t* self,
|
||||
uint8_t* buffer,
|
||||
size_t len);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
|
271
shared-bindings/aesio/aes.c
Normal file
271
shared-bindings/aesio/aes.c
Normal file
@ -0,0 +1,271 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/objproperty.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "shared-bindings/aesio/__init__.h"
|
||||
|
||||
// Defined at the end of this file
|
||||
|
||||
//| .. currentmodule:: aesio
|
||||
//|
|
||||
//| :class:`aesio` -- Encrypt and decrypt AES streams
|
||||
//| =====================================================
|
||||
//|
|
||||
//| An object that represents an AES stream, including the current state.
|
||||
//|
|
||||
//| .. class:: AES(key, mode=0, iv=None, segment_size=8)
|
||||
//|
|
||||
//| Create a new AES state with the given key.
|
||||
//|
|
||||
//| :param bytearray key: A 16-, 24-, or 32-byte key
|
||||
//| :param int mode: AES mode to use. One of: AES.MODE_ECB, AES.MODE_CBC, or
|
||||
//| AES.MODE_CTR
|
||||
//| :param bytearray iv: Initialization vector to use for CBC or CTR mode
|
||||
//|
|
||||
//| Additional arguments are supported for legacy reasons.
|
||||
//|
|
||||
//| Encrypting a string::
|
||||
//|
|
||||
//| import aesio
|
||||
//| from binascii import hexlify
|
||||
//|
|
||||
//| key = b'Sixteen byte key'
|
||||
//| inp = b'Circuit Python!!' # Note: 16-bytes long
|
||||
//| outp = bytearray(len(inp))
|
||||
//| cipher = aesio.AES(key, aesio.mode.MODE_ECB)
|
||||
//| cipher.encrypt_into(inp, outp)
|
||||
//| hexlify(outp)
|
||||
//|
|
||||
|
||||
STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
|
||||
const mp_obj_t *pos_args,
|
||||
mp_map_t *kw_args) {
|
||||
(void)type;
|
||||
enum { ARG_key, ARG_mode, ARG_IV, ARG_counter, ARG_segment_size };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED},
|
||||
{MP_QSTR_mode, MP_ARG_INT, {.u_int = AES_MODE_ECB}},
|
||||
{MP_QSTR_IV, MP_ARG_OBJ},
|
||||
{MP_QSTR_counter, MP_ARG_OBJ},
|
||||
{MP_QSTR_segment_size, MP_ARG_INT, {.u_int = 8}},
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
|
||||
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
|
||||
allowed_args, args);
|
||||
|
||||
aesio_aes_obj_t *self = m_new_obj(aesio_aes_obj_t);
|
||||
self->base.type = &aesio_aes_type;
|
||||
|
||||
mp_buffer_info_t bufinfo;
|
||||
|
||||
const uint8_t *key = NULL;
|
||||
uint32_t key_length = 0;
|
||||
if (mp_get_buffer(args[ARG_key].u_obj, &bufinfo, MP_BUFFER_READ)) {
|
||||
if ((bufinfo.len != 16) && (bufinfo.len != 24) && (bufinfo.len != 32)) {
|
||||
mp_raise_TypeError(translate("Key must be 16, 24, or 32 bytes long"));
|
||||
}
|
||||
key = bufinfo.buf;
|
||||
key_length = bufinfo.len;
|
||||
} else {
|
||||
mp_raise_TypeError(translate("No key was specified"));
|
||||
}
|
||||
|
||||
int mode = args[ARG_mode].u_int;
|
||||
switch (args[ARG_mode].u_int) {
|
||||
case AES_MODE_CBC:
|
||||
case AES_MODE_ECB:
|
||||
case AES_MODE_CTR:
|
||||
break;
|
||||
default:
|
||||
mp_raise_TypeError(translate("Requested AES mode is unsupported"));
|
||||
}
|
||||
|
||||
// IV is required for CBC mode and is ignored for other modes.
|
||||
const uint8_t *iv = NULL;
|
||||
if (args[ARG_IV].u_obj != NULL &&
|
||||
mp_get_buffer(args[ARG_IV].u_obj, &bufinfo, MP_BUFFER_READ)) {
|
||||
if (bufinfo.len != AES_BLOCKLEN) {
|
||||
mp_raise_TypeError_varg(translate("IV must be %d bytes long"),
|
||||
AES_BLOCKLEN);
|
||||
}
|
||||
iv = bufinfo.buf;
|
||||
}
|
||||
|
||||
common_hal_aesio_aes_construct(self, key, key_length, iv, mode,
|
||||
args[ARG_counter].u_int);
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args) {
|
||||
aesio_aes_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
||||
|
||||
size_t key_length = 0;
|
||||
const uint8_t *key =
|
||||
(const uint8_t *)mp_obj_str_get_data(pos_args[1], &key_length);
|
||||
if (key == NULL) {
|
||||
mp_raise_ValueError(translate("No key was specified"));
|
||||
}
|
||||
if ((key_length != 16) && (key_length != 24) && (key_length != 32)) {
|
||||
mp_raise_TypeError(translate("Key must be 16, 24, or 32 bytes long"));
|
||||
}
|
||||
|
||||
const uint8_t *iv = NULL;
|
||||
if (n_args > 2) {
|
||||
size_t iv_length = 0;
|
||||
iv = (const uint8_t *)mp_obj_str_get_data(pos_args[2], &iv_length);
|
||||
if (iv_length != AES_BLOCKLEN) {
|
||||
mp_raise_TypeError_varg(translate("IV must be %d bytes long"),
|
||||
AES_BLOCKLEN);
|
||||
}
|
||||
}
|
||||
|
||||
common_hal_aesio_aes_rekey(self, key, key_length, iv);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR(aesio_aes_rekey_obj, 2, aesio_aes_rekey);
|
||||
|
||||
STATIC void validate_length(aesio_aes_obj_t *self, size_t src_length,
|
||||
size_t dest_length) {
|
||||
if (src_length != dest_length) {
|
||||
mp_raise_ValueError(
|
||||
translate("Source and destination buffers must be the same length"));
|
||||
}
|
||||
|
||||
switch (self->mode) {
|
||||
case AES_MODE_ECB:
|
||||
if (src_length != 16) {
|
||||
mp_raise_msg(&mp_type_ValueError,
|
||||
translate("ECB only operates on 16 bytes at a time"));
|
||||
}
|
||||
break;
|
||||
case AES_MODE_CBC:
|
||||
if ((src_length & 15) != 0) {
|
||||
mp_raise_msg(&mp_type_ValueError,
|
||||
translate("CBC blocks must be multiples of 16 bytes"));
|
||||
}
|
||||
break;
|
||||
case AES_MODE_CTR:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//| .. method:: encrypt_into(src, dest)
|
||||
//|
|
||||
//| Encrypt the buffer from ``src`` into ``dest``.
|
||||
//| For ECB mode, the buffers must be 16 bytes long. For CBC mode, the
|
||||
//| buffers must be a multiple of 16 bytes, and must be equal length. For
|
||||
//| CTX mode, there are no restrictions.
|
||||
//|
|
||||
STATIC mp_obj_t aesio_aes_encrypt_into(mp_obj_t aesio_obj, mp_obj_t src,
|
||||
mp_obj_t dest) {
|
||||
if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
|
||||
mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
|
||||
}
|
||||
// Convert parameters into expected types.
|
||||
aesio_aes_obj_t *aes = MP_OBJ_TO_PTR(aesio_obj);
|
||||
|
||||
mp_buffer_info_t srcbufinfo, destbufinfo;
|
||||
mp_get_buffer_raise(src, &srcbufinfo, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(dest, &destbufinfo, MP_BUFFER_READ);
|
||||
validate_length(aes, srcbufinfo.len, destbufinfo.len);
|
||||
|
||||
memcpy(destbufinfo.buf, srcbufinfo.buf, srcbufinfo.len);
|
||||
|
||||
common_hal_aesio_aes_encrypt(aes, (uint8_t *)destbufinfo.buf,
|
||||
destbufinfo.len);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(aesio_aes_encrypt_into_obj,
|
||||
aesio_aes_encrypt_into);
|
||||
|
||||
//| .. method:: decrypt_into(src, dest)
|
||||
//|
|
||||
//| Decrypt the buffer from ``src`` into ``dest``.
|
||||
//| For ECB mode, the buffers must be 16 bytes long. For CBC mode, the
|
||||
//| buffers must be a multiple of 16 bytes, and must be equal length. For
|
||||
//| CTX mode, there are no restrictions.
|
||||
//|
|
||||
STATIC mp_obj_t aesio_aes_decrypt_into(mp_obj_t aesio_obj, mp_obj_t src,
|
||||
mp_obj_t dest) {
|
||||
if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
|
||||
mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
|
||||
}
|
||||
// Convert parameters into expected types.
|
||||
aesio_aes_obj_t *aes = MP_OBJ_TO_PTR(aesio_obj);
|
||||
|
||||
mp_buffer_info_t srcbufinfo, destbufinfo;
|
||||
mp_get_buffer_raise(src, &srcbufinfo, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(dest, &destbufinfo, MP_BUFFER_READ);
|
||||
validate_length(aes, srcbufinfo.len, destbufinfo.len);
|
||||
|
||||
memcpy(destbufinfo.buf, srcbufinfo.buf, srcbufinfo.len);
|
||||
|
||||
common_hal_aesio_aes_decrypt(aes, (uint8_t *)destbufinfo.buf,
|
||||
destbufinfo.len);
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(aesio_aes_decrypt_into_obj,
|
||||
aesio_aes_decrypt_into);
|
||||
|
||||
STATIC mp_obj_t aesio_aes_get_mode(mp_obj_t aesio_obj) {
|
||||
if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
|
||||
mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
|
||||
}
|
||||
aesio_aes_obj_t *self = MP_OBJ_TO_PTR(aesio_obj);
|
||||
return MP_OBJ_NEW_SMALL_INT(self->mode);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(aesio_aes_get_mode_obj, aesio_aes_get_mode);
|
||||
|
||||
STATIC mp_obj_t aesio_aes_set_mode(mp_obj_t aesio_obj, mp_obj_t mode_obj) {
|
||||
if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
|
||||
mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
|
||||
}
|
||||
aesio_aes_obj_t *self = MP_OBJ_TO_PTR(aesio_obj);
|
||||
|
||||
int mode = mp_obj_get_int(mode_obj);
|
||||
switch (mode) {
|
||||
case AES_MODE_CBC:
|
||||
case AES_MODE_ECB:
|
||||
case AES_MODE_CTR:
|
||||
break;
|
||||
default:
|
||||
mp_raise_TypeError(translate("Requested AES mode is unsupported"));
|
||||
}
|
||||
|
||||
common_hal_aesio_aes_set_mode(self, mode);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(aesio_aes_set_mode_obj, aesio_aes_set_mode);
|
||||
|
||||
const mp_obj_property_t aesio_aes_mode_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {
|
||||
(mp_obj_t)&aesio_aes_get_mode_obj,
|
||||
(mp_obj_t)&aesio_aes_set_mode_obj,
|
||||
(mp_obj_t)&mp_const_none_obj
|
||||
},
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t aesio_locals_dict_table[] = {
|
||||
// Methods
|
||||
{MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_AES)},
|
||||
{MP_ROM_QSTR(MP_QSTR_encrypt_into), (mp_obj_t)&aesio_aes_encrypt_into_obj},
|
||||
{MP_ROM_QSTR(MP_QSTR_decrypt_into), (mp_obj_t)&aesio_aes_decrypt_into_obj},
|
||||
{MP_ROM_QSTR(MP_QSTR_rekey), (mp_obj_t)&aesio_aes_rekey_obj},
|
||||
{MP_ROM_QSTR(MP_QSTR_mode), (mp_obj_t)&aesio_aes_mode_obj},
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(aesio_locals_dict, aesio_locals_dict_table);
|
||||
|
||||
const mp_obj_type_t aesio_aes_type = {
|
||||
{&mp_type_type},
|
||||
.name = MP_QSTR_AES,
|
||||
.make_new = aesio_aes_make_new,
|
||||
.locals_dict = (mp_obj_dict_t *)&aesio_locals_dict,
|
||||
};
|
@ -65,7 +65,7 @@ STATIC mp_obj_t rtc_rtc_make_new(const mp_obj_type_t *type, size_t n_args, const
|
||||
//| import time
|
||||
//|
|
||||
//| r = rtc.RTC()
|
||||
//| r.datetime = rtctime.struct_time((2019, 5, 29, 15, 14, 15, 0, -1, -1))
|
||||
//| r.datetime = time.struct_time((2019, 5, 29, 15, 14, 15, 0, -1, -1))
|
||||
//|
|
||||
//|
|
||||
//| Once set, the RTC will automatically update this value as time passes. You can read this
|
||||
|
58
shared-module/aesio/__init__.c
Normal file
58
shared-module/aesio/__init__.c
Normal file
@ -0,0 +1,58 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "shared-bindings/aesio/__init__.h"
|
||||
#include "shared-module/aesio/__init__.h"
|
||||
|
||||
void common_hal_aesio_aes_construct(aesio_aes_obj_t *self, const uint8_t *key,
|
||||
uint32_t key_length, const uint8_t *iv,
|
||||
int mode, int counter) {
|
||||
self->mode = mode;
|
||||
self->counter = counter;
|
||||
common_hal_aesio_aes_rekey(self, key, key_length, iv);
|
||||
}
|
||||
|
||||
void common_hal_aesio_aes_rekey(aesio_aes_obj_t *self, const uint8_t *key,
|
||||
uint32_t key_length, const uint8_t *iv) {
|
||||
memset(&self->ctx, 0, sizeof(self->ctx));
|
||||
if (iv != NULL) {
|
||||
AES_init_ctx_iv(&self->ctx, key, key_length, iv);
|
||||
} else {
|
||||
AES_init_ctx(&self->ctx, key, key_length);
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_aesio_aes_set_mode(aesio_aes_obj_t *self, int mode) {
|
||||
self->mode = mode;
|
||||
}
|
||||
|
||||
void common_hal_aesio_aes_encrypt(aesio_aes_obj_t *self, uint8_t *buffer,
|
||||
size_t length) {
|
||||
switch (self->mode) {
|
||||
case AES_MODE_ECB:
|
||||
AES_ECB_encrypt(&self->ctx, buffer);
|
||||
break;
|
||||
case AES_MODE_CBC:
|
||||
AES_CBC_encrypt_buffer(&self->ctx, buffer, length);
|
||||
break;
|
||||
case AES_MODE_CTR:
|
||||
AES_CTR_xcrypt_buffer(&self->ctx, buffer, length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_aesio_aes_decrypt(aesio_aes_obj_t *self, uint8_t *buffer,
|
||||
size_t length) {
|
||||
switch (self->mode) {
|
||||
case AES_MODE_ECB:
|
||||
AES_ECB_decrypt(&self->ctx, buffer);
|
||||
break;
|
||||
case AES_MODE_CBC:
|
||||
AES_CBC_decrypt_buffer(&self->ctx, buffer, length);
|
||||
break;
|
||||
case AES_MODE_CTR:
|
||||
AES_CTR_xcrypt_buffer(&self->ctx, buffer, length);
|
||||
break;
|
||||
}
|
||||
}
|
59
shared-module/aesio/__init__.h
Normal file
59
shared-module/aesio/__init__.h
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SHARED_MODULE_AESIO__INIT__H
|
||||
#define MICROPY_INCLUDED_SHARED_MODULE_AESIO__INIT__H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/proto.h"
|
||||
|
||||
#include "shared-module/aesio/aes.h"
|
||||
|
||||
// These values were chosen to correspond with the values
|
||||
// present in pycrypto.
|
||||
enum AES_MODE {
|
||||
AES_MODE_ECB = 1,
|
||||
AES_MODE_CBC = 2,
|
||||
AES_MODE_CTR = 6,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
|
||||
// The tinyaes context
|
||||
struct AES_ctx ctx;
|
||||
|
||||
// Which AES mode this instance of the object is configured to use
|
||||
enum AES_MODE mode;
|
||||
|
||||
// Counter for running in CTR mode
|
||||
uint32_t counter;
|
||||
} aesio_aes_obj_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_MODULE_AESIO__INIT__H
|
608
shared-module/aesio/aes.c
Normal file
608
shared-module/aesio/aes.c
Normal file
@ -0,0 +1,608 @@
|
||||
/*
|
||||
|
||||
This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode.
|
||||
Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
|
||||
|
||||
The implementation is verified against the test vectors in:
|
||||
National Institute of Standards and Technology Special Publication 800-38A 2001 ED
|
||||
|
||||
ECB-AES128
|
||||
----------
|
||||
|
||||
plain-text:
|
||||
6bc1bee22e409f96e93d7e117393172a
|
||||
ae2d8a571e03ac9c9eb76fac45af8e51
|
||||
30c81c46a35ce411e5fbc1191a0a52ef
|
||||
f69f2445df4f9b17ad2b417be66c3710
|
||||
|
||||
key:
|
||||
2b7e151628aed2a6abf7158809cf4f3c
|
||||
|
||||
resulting cipher
|
||||
3ad77bb40d7a3660a89ecaf32466ef97
|
||||
f5d3d58503b9699de785895a96fdbaaf
|
||||
43b1cd7f598ece23881b00e3ed030688
|
||||
7b0c785e27e8ad3f8223207104725dd4
|
||||
|
||||
|
||||
NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
|
||||
You should pad the end of the string with zeros if this is not the case.
|
||||
For AES192/256 the key size is proportionally larger.
|
||||
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Includes: */
|
||||
/*****************************************************************************/
|
||||
#include <string.h> // CBC mode, for memset
|
||||
#include "aes.h"
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Defines: */
|
||||
/*****************************************************************************/
|
||||
// The number of columns comprising a state in AES. This is a constant in AES.
|
||||
// Value=4
|
||||
#define Nb 4UL
|
||||
|
||||
#if defined(AES256) && (AES256 == 1)
|
||||
#define Nk256 8UL
|
||||
#define Nr256 14UL
|
||||
#endif
|
||||
#if defined(AES192) && (AES192 == 1)
|
||||
#define Nk192 6UL
|
||||
#define Nr192 12UL
|
||||
#endif
|
||||
#if defined(AES128) && (AES128 == 1)
|
||||
#define Nk128 4UL // The number of 32 bit words in a key.
|
||||
#define Nr128 10UL // The number of rounds in AES Cipher.
|
||||
#endif
|
||||
|
||||
// jcallan@github points out that declaring Multiply as a function reduces code
|
||||
// size considerably with the Keil ARM compiler. See this link for more
|
||||
// information: https://github.com/kokke/tiny-AES-C/pull/3
|
||||
#ifndef MULTIPLY_AS_A_FUNCTION
|
||||
#define MULTIPLY_AS_A_FUNCTION 0
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Private variables: */
|
||||
/*****************************************************************************/
|
||||
// state - array holding the intermediate results during decryption.
|
||||
typedef uint8_t state_t[4][4];
|
||||
|
||||
|
||||
|
||||
// The lookup-tables are marked const so they can be placed in read-only storage
|
||||
// instead of RAM The numbers below can be computed dynamically trading ROM for
|
||||
// RAM - This can be useful in (embedded) bootloader applications, where ROM is
|
||||
// often limited.
|
||||
static const uint8_t sbox[256] = {
|
||||
//0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
||||
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
||||
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
||||
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
||||
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
||||
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
|
||||
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
|
||||
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
||||
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
|
||||
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
|
||||
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
||||
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
||||
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
||||
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
||||
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
|
||||
|
||||
static const uint8_t rsbox[256] = {
|
||||
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
||||
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
|
||||
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
|
||||
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
|
||||
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
||||
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
|
||||
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
|
||||
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
||||
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
|
||||
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
|
||||
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
||||
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
|
||||
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
|
||||
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
|
||||
|
||||
// The round constant word array, Rcon[i], contains the values given by x to the
|
||||
// power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
|
||||
static const uint8_t Rcon[11] = {
|
||||
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
|
||||
|
||||
/*
|
||||
* Jordan Goulder points out in PR #12
|
||||
* (https://github.com/kokke/tiny-AES-C/pull/12), that you can remove most of
|
||||
* the elements in the Rcon array, because they are unused.
|
||||
*
|
||||
* From Wikipedia's article on the Rijndael key schedule @
|
||||
* https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon
|
||||
*
|
||||
* "Only the first some of these constants are actually used – up to rcon[10]
|
||||
* for AES-128 (as 11 round keys are needed), up to rcon[8] for AES-192, up to
|
||||
* rcon[7] for AES-256. rcon[0] is not used in AES algorithm."
|
||||
*/
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Private functions: */
|
||||
/*****************************************************************************/
|
||||
static const uint8_t *GetRoundKey(const struct AES_ctx *ctx) {
|
||||
switch (ctx->KeyLength) {
|
||||
#if defined(AES128) && (AES128 == 1)
|
||||
case 16: return ctx->RoundKey128;
|
||||
#endif
|
||||
#if defined(AES192) && (AES192 == 1)
|
||||
case 24: return ctx->RoundKey192;
|
||||
#endif
|
||||
#if defined(AES256) && (AES256 == 1)
|
||||
case 32: return ctx->RoundKey256;
|
||||
#endif
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static uint8_t getSBoxValue(uint8_t num)
|
||||
{
|
||||
return sbox[num];
|
||||
}
|
||||
*/
|
||||
#define getSBoxValue(num) (sbox[(num)])
|
||||
/*
|
||||
static uint8_t getSBoxInvert(uint8_t num)
|
||||
{
|
||||
return rsbox[num];
|
||||
}
|
||||
*/
|
||||
#define getSBoxInvert(num) (rsbox[(num)])
|
||||
|
||||
// This function produces Nb(Nr+1) round keys. The round keys are used in each
|
||||
// round to decrypt the states.
|
||||
static void KeyExpansion(struct AES_ctx* ctx, const uint8_t* Key)
|
||||
{
|
||||
uint8_t* RoundKey = (uint8_t *)GetRoundKey(ctx);
|
||||
|
||||
unsigned i, j, k;
|
||||
uint8_t tempa[4]; // Used for the column/row operations
|
||||
|
||||
// The first round key is the key itself.
|
||||
for (i = 0; i < ctx->Nk; ++i)
|
||||
{
|
||||
RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
|
||||
RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
|
||||
RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
|
||||
RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
|
||||
}
|
||||
|
||||
// All other round keys are found from the previous round keys.
|
||||
for (i = ctx->Nk; i < Nb * (ctx->Nr + 1); ++i)
|
||||
{
|
||||
{
|
||||
k = (i - 1) * 4;
|
||||
tempa[0]=RoundKey[k + 0];
|
||||
tempa[1]=RoundKey[k + 1];
|
||||
tempa[2]=RoundKey[k + 2];
|
||||
tempa[3]=RoundKey[k + 3];
|
||||
|
||||
}
|
||||
|
||||
if (i % ctx->Nk == 0)
|
||||
{
|
||||
// This function shifts the 4 bytes in a word to the left once.
|
||||
// [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
|
||||
|
||||
// Function RotWord()
|
||||
{
|
||||
const uint8_t u8tmp = tempa[0];
|
||||
tempa[0] = tempa[1];
|
||||
tempa[1] = tempa[2];
|
||||
tempa[2] = tempa[3];
|
||||
tempa[3] = u8tmp;
|
||||
}
|
||||
|
||||
// SubWord() is a function that takes a four-byte input word and applies
|
||||
// the S-box to each of the four bytes to produce an output word.
|
||||
|
||||
// Function Subword()
|
||||
{
|
||||
tempa[0] = getSBoxValue(tempa[0]);
|
||||
tempa[1] = getSBoxValue(tempa[1]);
|
||||
tempa[2] = getSBoxValue(tempa[2]);
|
||||
tempa[3] = getSBoxValue(tempa[3]);
|
||||
}
|
||||
|
||||
tempa[0] = tempa[0] ^ Rcon[i/ctx->Nk];
|
||||
}
|
||||
#if defined(AES256) && (AES256 == 1)
|
||||
if (ctx->KeyLength == 32) {
|
||||
if (i % ctx->Nk == 4)
|
||||
{
|
||||
// Function Subword()
|
||||
{
|
||||
tempa[0] = getSBoxValue(tempa[0]);
|
||||
tempa[1] = getSBoxValue(tempa[1]);
|
||||
tempa[2] = getSBoxValue(tempa[2]);
|
||||
tempa[3] = getSBoxValue(tempa[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
j = i * 4; k=(i - ctx->Nk) * 4;
|
||||
RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
|
||||
RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
|
||||
RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
|
||||
RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
|
||||
}
|
||||
}
|
||||
|
||||
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen)
|
||||
{
|
||||
ctx->KeyLength = keylen;
|
||||
switch (ctx->KeyLength) {
|
||||
#if defined(AES128) && (AES128 == 1)
|
||||
case 16: ctx->Nr = Nr128; ctx->Nk = Nk128; break;
|
||||
#endif
|
||||
#if defined(AES192) && (AES192 == 1)
|
||||
case 24: ctx->Nr = Nr192; ctx->Nk = Nk192; break;
|
||||
#endif
|
||||
#if defined(AES256) && (AES256 == 1)
|
||||
case 32: ctx->Nr = Nr256; ctx->Nk = Nk256; break;
|
||||
#endif
|
||||
default: ctx->Nr = 0; ctx->Nk = 0; break;
|
||||
}
|
||||
KeyExpansion(ctx, key);
|
||||
}
|
||||
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
|
||||
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen, const uint8_t* iv)
|
||||
{
|
||||
AES_init_ctx(ctx, key, keylen);
|
||||
memcpy (ctx->Iv, iv, AES_BLOCKLEN);
|
||||
}
|
||||
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv)
|
||||
{
|
||||
memcpy (ctx->Iv, iv, AES_BLOCKLEN);
|
||||
}
|
||||
#endif
|
||||
|
||||
// This function adds the round key to state. The round key is added to the
|
||||
// state by an XOR function.
|
||||
static void AddRoundKey(uint8_t round, state_t* state, const uint8_t* RoundKey)
|
||||
{
|
||||
uint8_t i,j;
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
for (j = 0; j < 4; ++j)
|
||||
{
|
||||
(*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The SubBytes Function Substitutes the values in the state matrix with values
|
||||
// in an S-box.
|
||||
static void SubBytes(state_t* state)
|
||||
{
|
||||
uint8_t i, j;
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
for (j = 0; j < 4; ++j)
|
||||
{
|
||||
(*state)[j][i] = getSBoxValue((*state)[j][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The ShiftRows() function shifts the rows in the state to the left. Each row
|
||||
// is shifted with different offset. Offset = Row number. So the first row is
|
||||
// not shifted.
|
||||
static void ShiftRows(state_t* state)
|
||||
{
|
||||
uint8_t temp;
|
||||
|
||||
// Rotate first row 1 columns to left
|
||||
temp = (*state)[0][1];
|
||||
(*state)[0][1] = (*state)[1][1];
|
||||
(*state)[1][1] = (*state)[2][1];
|
||||
(*state)[2][1] = (*state)[3][1];
|
||||
(*state)[3][1] = temp;
|
||||
|
||||
// Rotate second row 2 columns to left
|
||||
temp = (*state)[0][2];
|
||||
(*state)[0][2] = (*state)[2][2];
|
||||
(*state)[2][2] = temp;
|
||||
|
||||
temp = (*state)[1][2];
|
||||
(*state)[1][2] = (*state)[3][2];
|
||||
(*state)[3][2] = temp;
|
||||
|
||||
// Rotate third row 3 columns to left
|
||||
temp = (*state)[0][3];
|
||||
(*state)[0][3] = (*state)[3][3];
|
||||
(*state)[3][3] = (*state)[2][3];
|
||||
(*state)[2][3] = (*state)[1][3];
|
||||
(*state)[1][3] = temp;
|
||||
}
|
||||
|
||||
static uint8_t xtime(uint8_t x)
|
||||
{
|
||||
return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
|
||||
}
|
||||
|
||||
// MixColumns function mixes the columns of the state matrix
|
||||
static void MixColumns(state_t* state)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t Tmp, Tm, t;
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
t = (*state)[i][0];
|
||||
Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
|
||||
Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
|
||||
Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
|
||||
Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
|
||||
Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply is used to multiply numbers in the field GF(2^8)
|
||||
// Note: The last call to xtime() is unneeded, but often ends up generating a smaller binary
|
||||
// The compiler seems to be able to vectorize the operation better this way.
|
||||
// See https://github.com/kokke/tiny-AES-c/pull/34
|
||||
#if MULTIPLY_AS_A_FUNCTION
|
||||
static uint8_t Multiply(uint8_t x, uint8_t y)
|
||||
{
|
||||
return (((y & 1) * x) ^
|
||||
((y>>1 & 1) * xtime(x)) ^
|
||||
((y>>2 & 1) * xtime(xtime(x))) ^
|
||||
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
|
||||
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); /* this last call to xtime() can be omitted */
|
||||
}
|
||||
#else
|
||||
#define Multiply(x, y) \
|
||||
( ((y & 1) * x) ^ \
|
||||
((y>>1 & 1) * xtime(x)) ^ \
|
||||
((y>>2 & 1) * xtime(xtime(x))) ^ \
|
||||
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \
|
||||
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \
|
||||
|
||||
#endif
|
||||
|
||||
#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
|
||||
// MixColumns function mixes the columns of the state matrix. The method used to
|
||||
// multiply may be difficult to understand for the inexperienced. Please use the
|
||||
// references to gain more information.
|
||||
static void InvMixColumns(state_t* state)
|
||||
{
|
||||
int i;
|
||||
uint8_t a, b, c, d;
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
a = (*state)[i][0];
|
||||
b = (*state)[i][1];
|
||||
c = (*state)[i][2];
|
||||
d = (*state)[i][3];
|
||||
|
||||
(*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
|
||||
(*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
|
||||
(*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
|
||||
(*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// The SubBytes Function Substitutes the values in the state matrix with values
|
||||
// in an S-box.
|
||||
static void InvSubBytes(state_t* state)
|
||||
{
|
||||
uint8_t i, j;
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
for (j = 0; j < 4; ++j)
|
||||
{
|
||||
(*state)[j][i] = getSBoxInvert((*state)[j][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void InvShiftRows(state_t* state)
|
||||
{
|
||||
uint8_t temp;
|
||||
|
||||
// Rotate first row 1 columns to right
|
||||
temp = (*state)[3][1];
|
||||
(*state)[3][1] = (*state)[2][1];
|
||||
(*state)[2][1] = (*state)[1][1];
|
||||
(*state)[1][1] = (*state)[0][1];
|
||||
(*state)[0][1] = temp;
|
||||
|
||||
// Rotate second row 2 columns to right
|
||||
temp = (*state)[0][2];
|
||||
(*state)[0][2] = (*state)[2][2];
|
||||
(*state)[2][2] = temp;
|
||||
|
||||
temp = (*state)[1][2];
|
||||
(*state)[1][2] = (*state)[3][2];
|
||||
(*state)[3][2] = temp;
|
||||
|
||||
// Rotate third row 3 columns to right
|
||||
temp = (*state)[0][3];
|
||||
(*state)[0][3] = (*state)[1][3];
|
||||
(*state)[1][3] = (*state)[2][3];
|
||||
(*state)[2][3] = (*state)[3][3];
|
||||
(*state)[3][3] = temp;
|
||||
}
|
||||
#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
|
||||
|
||||
// Cipher is the main function that encrypts the PlainText.
|
||||
static void Cipher(state_t* state, const struct AES_ctx* ctx)
|
||||
{
|
||||
const uint8_t* RoundKey = GetRoundKey(ctx);
|
||||
uint8_t round = 0;
|
||||
|
||||
// Add the First round key to the state before starting the rounds.
|
||||
AddRoundKey(0, state, RoundKey);
|
||||
|
||||
// There will be Nr rounds. The first Nr-1 rounds are identical. These Nr
|
||||
// rounds are executed in the loop below. Last one without MixColumns()
|
||||
for (round = 1; ; ++round)
|
||||
{
|
||||
SubBytes(state);
|
||||
ShiftRows(state);
|
||||
if (round == ctx->Nr) {
|
||||
break;
|
||||
}
|
||||
MixColumns(state);
|
||||
AddRoundKey(round, state, RoundKey);
|
||||
}
|
||||
// Add round key to last round
|
||||
AddRoundKey(ctx->Nr, state, RoundKey);
|
||||
}
|
||||
|
||||
#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
|
||||
static void InvCipher(state_t* state, const struct AES_ctx* ctx)
|
||||
{
|
||||
const uint8_t* RoundKey = GetRoundKey(ctx);
|
||||
uint8_t round = 0;
|
||||
|
||||
// Add the First round key to the state before starting the rounds.
|
||||
AddRoundKey(ctx->Nr, state, RoundKey);
|
||||
|
||||
// There will be Nr rounds. The first Nr-1 rounds are identical. These Nr
|
||||
// rounds are executed in the loop below. Last one without InvMixColumn()
|
||||
for (round = (ctx->Nr - 1); ; --round)
|
||||
{
|
||||
InvShiftRows(state);
|
||||
InvSubBytes(state);
|
||||
AddRoundKey(round, state, RoundKey);
|
||||
if (round == 0) {
|
||||
break;
|
||||
}
|
||||
InvMixColumns(state);
|
||||
}
|
||||
|
||||
}
|
||||
#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
|
||||
|
||||
/*****************************************************************************/
|
||||
/* Public functions: */
|
||||
/*****************************************************************************/
|
||||
#if defined(ECB) && (ECB == 1)
|
||||
|
||||
|
||||
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf)
|
||||
{
|
||||
// The next function call encrypts the PlainText with the Key using AES
|
||||
// algorithm.
|
||||
Cipher((state_t*)buf, ctx);
|
||||
}
|
||||
|
||||
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf)
|
||||
{
|
||||
// The next function call decrypts the PlainText with the Key using AES
|
||||
// algorithm.
|
||||
InvCipher((state_t*)buf, ctx);
|
||||
}
|
||||
|
||||
|
||||
#endif // #if defined(ECB) && (ECB == 1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if defined(CBC) && (CBC == 1)
|
||||
|
||||
|
||||
static void XorWithIv(uint8_t* buf, const uint8_t* Iv)
|
||||
{
|
||||
uint8_t i;
|
||||
for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
|
||||
{
|
||||
buf[i] ^= Iv[i];
|
||||
}
|
||||
}
|
||||
|
||||
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, uint32_t length)
|
||||
{
|
||||
uintptr_t i;
|
||||
uint8_t *Iv = ctx->Iv;
|
||||
for (i = 0; i < length; i += AES_BLOCKLEN)
|
||||
{
|
||||
XorWithIv(buf, Iv);
|
||||
Cipher((state_t*)buf, ctx);
|
||||
Iv = buf;
|
||||
buf += AES_BLOCKLEN;
|
||||
}
|
||||
/* store Iv in ctx for next call */
|
||||
memcpy(ctx->Iv, Iv, AES_BLOCKLEN);
|
||||
}
|
||||
|
||||
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
|
||||
{
|
||||
uintptr_t i;
|
||||
uint8_t storeNextIv[AES_BLOCKLEN];
|
||||
for (i = 0; i < length; i += AES_BLOCKLEN)
|
||||
{
|
||||
memcpy(storeNextIv, buf, AES_BLOCKLEN);
|
||||
InvCipher((state_t*)buf, ctx);
|
||||
XorWithIv(buf, ctx->Iv);
|
||||
memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
|
||||
buf += AES_BLOCKLEN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // #if defined(CBC) && (CBC == 1)
|
||||
|
||||
|
||||
|
||||
#if defined(CTR) && (CTR == 1)
|
||||
|
||||
/* Symmetrical operation: same function for encrypting as for decrypting. Note
|
||||
any IV/nonce should never be reused with the same key */
|
||||
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
|
||||
{
|
||||
uint8_t buffer[AES_BLOCKLEN];
|
||||
|
||||
unsigned i;
|
||||
int bi;
|
||||
for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
|
||||
{
|
||||
if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
|
||||
{
|
||||
memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
|
||||
Cipher((state_t*)buffer, ctx);
|
||||
|
||||
/* Increment Iv and handle overflow */
|
||||
for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
|
||||
{
|
||||
/* inc will overflow */
|
||||
if (ctx->Iv[bi] == 255)
|
||||
{
|
||||
ctx->Iv[bi] = 0;
|
||||
continue;
|
||||
}
|
||||
ctx->Iv[bi] += 1;
|
||||
break;
|
||||
}
|
||||
bi = 0;
|
||||
}
|
||||
|
||||
buf[i] = (buf[i] ^ buffer[bi]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // #if defined(CTR) && (CTR == 1)
|
105
shared-module/aesio/aes.h
Normal file
105
shared-module/aesio/aes.h
Normal file
@ -0,0 +1,105 @@
|
||||
#ifndef _AES_H_
|
||||
#define _AES_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// #define the macros below to 1/0 to enable/disable the mode of operation.
|
||||
//
|
||||
// CBC enables AES encryption in CBC-mode of operation.
|
||||
// CTR enables encryption in counter-mode.
|
||||
// ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
|
||||
|
||||
// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
|
||||
#ifndef CBC
|
||||
#define CBC 1
|
||||
#endif
|
||||
|
||||
#ifndef ECB
|
||||
#define ECB 1
|
||||
#endif
|
||||
|
||||
#ifndef CTR
|
||||
#define CTR 1
|
||||
#endif
|
||||
|
||||
|
||||
#define AES128 1
|
||||
#define AES192 1
|
||||
#define AES256 1
|
||||
|
||||
#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
|
||||
|
||||
#if defined(AES256) && (AES256 == 1)
|
||||
#define AES_KEYLEN256 32
|
||||
#define AES_keyExpSize256 240
|
||||
#endif
|
||||
#if defined(AES192) && (AES192 == 1)
|
||||
#define AES_KEYLEN192 24
|
||||
#define AES_keyExpSize192 208
|
||||
#endif
|
||||
#if defined(AES128) && (AES128 == 1)
|
||||
#define AES_KEYLEN128 16 // Key length in bytes
|
||||
#define AES_keyExpSize128 176
|
||||
#endif
|
||||
|
||||
struct AES_ctx
|
||||
{
|
||||
union {
|
||||
#if defined(AES256) && (AES256 == 1)
|
||||
uint8_t RoundKey256[AES_keyExpSize256];
|
||||
#endif
|
||||
#if defined(AES192) && (AES192 == 1)
|
||||
uint8_t RoundKey192[AES_keyExpSize192];
|
||||
#endif
|
||||
#if defined(AES128) && (AES128 == 1)
|
||||
uint8_t RoundKey128[AES_keyExpSize128];
|
||||
#endif
|
||||
};
|
||||
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
|
||||
uint8_t Iv[AES_BLOCKLEN];
|
||||
#endif
|
||||
uint32_t KeyLength;
|
||||
uint8_t Nr;
|
||||
uint8_t Nk;
|
||||
};
|
||||
|
||||
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen);
|
||||
#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
|
||||
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen, const uint8_t* iv);
|
||||
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
|
||||
#endif
|
||||
|
||||
#if defined(ECB) && (ECB == 1)
|
||||
// buffer size is exactly AES_BLOCKLEN bytes;
|
||||
// you need only AES_init_ctx as IV is not used in ECB
|
||||
// NB: ECB is considered insecure for most uses
|
||||
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
|
||||
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
|
||||
|
||||
#endif // #if defined(ECB) && (ECB == !)
|
||||
|
||||
|
||||
#if defined(CBC) && (CBC == 1)
|
||||
// buffer size MUST be mutile of AES_BLOCKLEN;
|
||||
// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
|
||||
// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
|
||||
// no IV should ever be reused with the same key
|
||||
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
|
||||
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
|
||||
|
||||
#endif // #if defined(CBC) && (CBC == 1)
|
||||
|
||||
|
||||
#if defined(CTR) && (CTR == 1)
|
||||
|
||||
// Same function for encrypting as for decrypting.
|
||||
// IV is incremented for every block, and used after encryption as XOR-compliment for output
|
||||
// Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
|
||||
// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
|
||||
// no IV should ever be reused with the same key
|
||||
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
|
||||
|
||||
#endif // #if defined(CTR) && (CTR == 1)
|
||||
|
||||
|
||||
#endif // _AES_H_
|
Loading…
Reference in New Issue
Block a user