diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index e6a0bfe3..dcea794b 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -15,4 +15,4 @@ jobs: - uses: jidicula/clang-format-action@v4.13.0 with: clang-format-version: '18' - exclude-regex: '(lib|Drivers|Core|net/include/sys|net/include/netinet|sframe|gsl)' + exclude-regex: '(lib|Drivers|Core|net/include/sys|net/include/netinet|cmox)' diff --git a/.gitmodules b/.gitmodules index ebd11ef5..2a81cc99 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,3 +5,6 @@ [submodule "firmware/net/components/nlohmann_json/lib"] path = firmware/net/components/nlohmann_json/lib url = git@github.com:nlohmann/json.git +[submodule "firmware/ui/dependencies/sframe"] + path = firmware/ui/dependencies/sframe + url = https://github.com/GhostofCookie/sframe.git diff --git a/README.md b/README.md index d9132551..e38385ef 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,10 @@ Hardware design for test device ## Table of contents: -1. [Where To Find Things](#where) -2. [Hardware](#hardware) -3. [Firmware](#firmware) +1. [License](#License) +2. [Where To Find Things](#where) +3. [Hardware](#hardware) +4. [Firmware](#firmware) 1. [Management](#management) 2. [User Interface](#ui) 3. [Network](#network) @@ -17,6 +18,9 @@ Hardware design for test device 2. [Python Serial Monitor](#serial_monitor) 5. [Hactar Setup](#hactar_setup) +## License + +The license for information in this repository is in [LICENSE]. This license covers everything in the repository except for the directory `firmware/ui/dependencies/cmox`, which is covered by the license in [firmware/ui/dependencies/cmox/LICENSE].

Where To Find Things

diff --git a/firmware/ui/.mxproject b/firmware/ui/.mxproject index a0d3afd8..133c0079 100644 --- a/firmware/ui/.mxproject +++ b/firmware/ui/.mxproject @@ -1,24 +1,11 @@ [PreviousLibFiles] -LibFiles=Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_bus.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_utils.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dmamux.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_usart.h;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_bus.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_utils.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dmamux.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_usart.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f405xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/core_cm35p.h;Drivers/CMSIS/Include/cmsis_armclang_ltm.h;Drivers/CMSIS/Include/pac_armv81.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/cachel1_armv7.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/pmu_armv8.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_armv81mml.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/core_starmc1.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm55.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_cm85.h;Drivers/CMSIS/Include/cmsis_version.h; - -[PreviousUsedIarFiles] -SourceFiles=../Core/Src/main.c;../Core/Src/stm32f4xx_it.c;../Core/Src/stm32f4xx_hal_msp.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;../Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;../Core/Src/system_stm32f4xx.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;../Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;../Core/Src/system_stm32f4xx.c;;; -HeaderPath=../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Drivers/CMSIS/Include;../Core/Inc; -CDefines=USE_HAL_DRIVER;STM32F405xx;USE_HAL_DRIVER;USE_HAL_DRIVER; - -[PreviousUsedMakefileFiles] -SourceFiles=Core/Src/main.c;Core/Src/stm32f4xx_it.c;Core/Src/stm32f4xx_hal_msp.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Core/Src/system_stm32f4xx.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Core/Src/system_stm32f4xx.c;;; -HeaderPath=Drivers/STM32F4xx_HAL_Driver/Inc;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32F4xx/Include;Drivers/CMSIS/Include;Core/Inc; -CDefines=USE_HAL_DRIVER;STM32F405xx;USE_HAL_DRIVER;USE_HAL_DRIVER; +LibFiles=Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_bus.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_utils.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dmamux.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_usart.h;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_adc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_bus.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_system.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_utils.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_dmamux.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_exti.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_crc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_i2c.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2c_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_i2s_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_rng.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_spi.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_ll_usart.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f405xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Drivers/CMSIS/Include/core_cm85.h;Drivers/CMSIS/Include/cachel1_armv7.h;Drivers/CMSIS/Include/pac_armv81.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/core_cm35p.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_armv81mml.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_cm55.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/pmu_armv8.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_starmc1.h;Drivers/CMSIS/Include/cmsis_armclang_ltm.h; [PreviousUsedCMakes] SourceFiles=Core/Src/main.c;Core/Src/stm32f4xx_it.c;Core/Src/stm32f4xx_hal_msp.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Core/Src/system_stm32f4xx.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Core/Src/system_stm32f4xx.c;;; HeaderPath=Drivers/STM32F4xx_HAL_Driver/Inc;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;Drivers/CMSIS/Device/ST/STM32F4xx/Include;Drivers/CMSIS/Include;Core/Inc; CDefines=USE_HAL_DRIVER;STM32F405xx;USE_HAL_DRIVER;USE_HAL_DRIVER; -[] -SourceFiles=;; - [PreviousGenFiles] AdvancedFolderStructure=true HeaderFileListSize=3 diff --git a/firmware/ui/CMakeLists.txt b/firmware/ui/CMakeLists.txt new file mode 100644 index 00000000..fdfee662 --- /dev/null +++ b/firmware/ui/CMakeLists.txt @@ -0,0 +1,94 @@ +cmake_minimum_required(VERSION 3.22) + +set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/gcc-arm-none-eabi.cmake") +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) + +# +# This file is generated only once, +# and is not re-generated if converter is called multiple times. +# +# User is free to modify the file as much as necessary +# + +# Setup compiler settings +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS ON) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS ON) + +# Define the build type +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug") +endif() + +# Set the project name +set(CMAKE_PROJECT_NAME ui) + +# Enable compile command to ease indexing with e.g. clangd +set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) + +# Find dependencies +set(STM32Cryptographic_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/cmox) +find_package(STM32Cryptographic COMPONENTS CM4 REQUIRED) +add_library(STM32Cryptographic ALIAS STM32Cryptographic::CM4) + +set(CRYPTO "STM32" CACHE STRING "" FORCE) +set(NO_ALLOC ON CACHE BOOL "" FORCE) +add_compile_definitions(NO_ALLOC) +add_subdirectory(dependencies/sframe) + +# Core project settings +project(${CMAKE_PROJECT_NAME} C CXX ASM) +message("Build type: " ${CMAKE_BUILD_TYPE}) + +# Enable CMake support for ASM and C languages +enable_language(C CXX ASM) + +# Create an executable object type +add_executable(${CMAKE_PROJECT_NAME}) + +# Add STM32CubeMX generated sources +add_subdirectory(cmake/stm32cubemx) + +# Link directories setup +target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE + # Add user defined library search paths +) + +file(GLOB_RECURSE CXX_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc") +file(GLOB_RECURSE SHARED_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../shared/*.cc") +message(STATUS ${CXX_SOURCES} + ${SHARED_SOURCES}) +target_sources(${CMAKE_PROJECT_NAME} PRIVATE + ${CXX_SOURCES} + ${SHARED_SOURCES} +) + +# Add include paths +target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/Core/Inc + ${CMAKE_CURRENT_SOURCE_DIR}/inc + ${CMAKE_CURRENT_SOURCE_DIR}/inc/fonts + ${CMAKE_CURRENT_SOURCE_DIR}/../shared_inc + ${CMAKE_CURRENT_SOURCE_DIR}/../shared/ +) + +# Add project symbols (macros) +target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE +) + +# Add linked libraries +target_link_libraries(${CMAKE_PROJECT_NAME} + stm32cubemx + + sframe +) + +add_custom_command( + TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -O binary $ ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin + COMMAND ${CMAKE_OBJCOPY} -O ihex $ ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.hex + COMMENT "Generating .bin and .hex files" +) diff --git a/firmware/ui/CMakePresets.json b/firmware/ui/CMakePresets.json new file mode 100644 index 00000000..1ed4e19d --- /dev/null +++ b/firmware/ui/CMakePresets.json @@ -0,0 +1,60 @@ +{ + "version": 3, + "configurePresets": [ + { + "name": "default", + "hidden": true, + "generator": "Ninja", + "binaryDir": "${sourceDir}/build/${presetName}", + "toolchainFile": "${sourceDir}/cmake/gcc-arm-none-eabi.cmake", + "cacheVariables": { + } + }, + { + "name": "Debug", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug" + } + }, + { + "name": "RelWithDebInfo", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "RelWithDebInfo" + } + }, + { + "name": "Release", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + }, + { + "name": "MinSizeRel", + "inherits": "default", + "cacheVariables": { + "CMAKE_BUILD_TYPE": "MinSizeRel" + } + } + ], + "buildPresets": [ + { + "name": "Debug", + "configurePreset": "Debug" + }, + { + "name": "RelWithDebInfo", + "configurePreset": "RelWithDebInfo" + }, + { + "name": "Release", + "configurePreset": "Release" + }, + { + "name": "MinSizeRel", + "configurePreset": "MinSizeRel" + } + ] +} \ No newline at end of file diff --git a/firmware/ui/Makefile b/firmware/ui/Makefile index eb798592..f7275319 100644 --- a/firmware/ui/Makefile +++ b/firmware/ui/Makefile @@ -33,9 +33,6 @@ OPT = -Os ####################################### # Build path BUILD_DIR = ./build -STM32_CRYPTO_DIR= -STM32_CRYPTO_INCLUDE_DIR=${STM32_CRYPTO_DIR}/include -STM32_CRYPTO_LIB_DIR=${STM32_CRYPTO_DIR}/lib ####################################### # programming @@ -50,263 +47,17 @@ CUBE_PROGRAMMER_ARGS = -c port=${PORT} br=${BAUD} P=even db=8 -w ${BUILD_DIR}/${ DOCKER_IMAGE = build-hactar-ui mft=compile -###################################### -# source -###################################### -# HAL C sources -SRCS_C = \ -main.c \ -stm32f4xx_hal_msp.c \ -stm32f4xx_it.c \ -system_stm32f4xx.c \ -stm32f4xx_hal.c \ -stm32f4xx_hal_adc.c \ -stm32f4xx_hal_cortex.c \ -stm32f4xx_hal_dma.c \ -stm32f4xx_hal_dma_ex.c \ -stm32f4xx_hal_exti.c \ -stm32f4xx_hal_flash.c \ -stm32f4xx_hal_flash_ex.c \ -stm32f4xx_hal_flash_ramfunc.c \ -stm32f4xx_hal_gpio.c \ -stm32f4xx_hal_i2c.c \ -stm32f4xx_hal_i2s.c \ -stm32f4xx_hal_i2s_ex.c \ -stm32f4xx_hal_pwr.c \ -stm32f4xx_hal_pwr_ex.c \ -stm32f4xx_hal_spi.c \ -stm32f4xx_hal_tim.c \ -stm32f4xx_hal_tim_ex.c \ -stm32f4xx_hal_rcc.c \ -stm32f4xx_hal_rcc_ex.c \ -stm32f4xx_hal_uart.c \ -stm32f4xx_hal_rng.c \ -stm32f4xx_hal_crc.c \ -sysmem.c \ -syscalls.c - -# User C/C++ sources -SRCS_CC = $(shell find src -name '*.cc' -exec basename {} \;) -SRCS_CC += serial_handler.cc - -SRCS_C += $(shell find src -name '*.c' -exec basename {} \;) - -SRCS = ${SRCS_CC} ${SRCS_C} - -# ASM sources -ASM_SOURCES = \ -startup_stm32f405xx.s - -# C includes -INCS = \ --IDrivers/STM32F4xx_HAL_Driver/Inc \ --IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy \ --IDrivers/CMSIS/Device/ST/STM32F4xx/Include \ --IDrivers/CMSIS/Include - -ifdef STM32_CRYPTO_DIR -INCS += -I${STM32_CRYPTO_INCLUDE_DIR} -endif - -INCS += $(addprefix -I,$(shell find inc -type d)) -INCS += $(addprefix -I,$(shell find Core/Inc -type d)) -INCS += -I../shared_inc -INCS += -I../shared - -####################################### -# vpaths -####################################### -VPATH = $(shell find src -type d) -VPATH += $(shell find Core/Src -type d) -VPATH += Drivers/STM32F4xx_HAL_Driver/Src -VPATH += $(shell find ../shared -type d) -# VPATH += $(BSP_SRC_DIR) -# VPATH += $(CMSIS_CORE_SRC_DIR) -# VPATH += $(CMSIS_DEVICE_SRC_DIR) -# VPATH += $(HAL_SRC_DIR) - -####################################### -# openocd -####################################### -# TODO - - -####################################### -# binaries -####################################### -PREFIX = arm-none-eabi- -# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx) -# either it can be added to the PATH environment variable. -ifdef GCC_PATH -CC = $(GCC_PATH)/$(PREFIX)g++ -AS = $(GCC_PATH)/$(PREFIX)g++ -x assembler-with-cpp -CP = $(GCC_PATH)/$(PREFIX)objcopy -DP = $(GCC_PATH)/$(PREFIX)objdump -SZ = $(GCC_PATH)/$(PREFIX)size -else -CC = $(PREFIX)g++ -AS = $(PREFIX)g++ -x assembler-with-cpp -CP = $(PREFIX)objcopy -DP = $(PREFIX)objdump -SZ = $(PREFIX)size -endif -HEX = $(CP) -O ihex -BIN = $(CP) -O binary -S - -####################################### -# CFLAGS -####################################### -# cpu -CPU = -mcpu=cortex-m4 - -# fpu -FPU = -mfpu=fpv4-sp-d16 - -# float-abi -FLOAT-ABI = -mfloat-abi=hard - -# mcu -MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI) - -# macros for gcc -# AS defines -AS_DEFS = - -# C defines -C_DEFS = \ --DUSE_HAL_DRIVER \ --DSTM32F405xx - - -# AS includes -AS_INCLUDES = - - -# compile gcc flags -ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections - -CFLAGS += $(MCU) $(C_DEFS) $(INCS) $(OPT) -std=c++17 \ - -Wall -Wextra \ - -fdata-sections -ffunction-sections -fexceptions - # -Wshadow \ - # -Wnon-virtual-dtor \ - # -Wcast-align \ - # -Wunused \ - # -Woverloaded-virtual \ - # -Wpedantic \ - # -Wconversion \ - # -Wsign-conversion \ - # -Wnull-dereference \ - # -Wdouble-promotion \ - # -Wformat=2 \ - # -Wimplicit-fallthrough - -HAL_CFLAGS += $(MCU) $(C_DEFS) $(INCS) -Os -Wall -fdata-sections -ffunction-sections - -ifeq ($(DEBUG), 1) -CFLAGS += -g -gdwarf-2 -HAL_CFLAGS += -g -gdwarf-2 -endif - -ifdef STM32_CRYPTO_DIR -CFLAGS += -DCRYPTO -DNO_ALLOC -endif - -ifeq (${PROGRAMMER}, CUBE) - ifeq (${OS}, WINDOWS_NT) - EXT = .exe - else - EXT = - endif -endif - OCD_DIR = /usr/share/openocd/scripts OCDFLAGS = -f interface/stlink.cfg -f target/stm32f4x.cfg - -# Generate dependency informationINCS = $(addprefix -I,$(shell find inc -type d))INCS = $(addprefix -I,$(shell find inc -type d))INCS = $(addprefix -I,$(shell find inc -type d)) -CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -HAL_CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" - - -####################################### -# LDFLAGS -####################################### -# link script -LDSCRIPT = stm32f405rgtx_flash.ld - -# libraries -# LIBS = -lc -lm -lnosys -lrdimon -lSTM32Cryptographic_CM4 -LIBS = -lc -lm -lnosys -LIBDIR = -Llib -LDFLAGS = $(MCU) --specs=nosys.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/${TARGET}.map,--cref -Wl,--gc-sections - -ifdef STM32_CRYPTO_DIR -LIBS += -lSTM32Cryptographic_CM4 -LIBDIR += -L$(STM32_CRYPTO_LIB_DIR) -endif -####################################### -# build the application -####################################### -# list of objects -OBJECTS_C = $(addprefix $(BUILD_DIR)/,$(SRCS_C:.c=.o)) -# vpath %.c $(sort $(dir $(SRCS_C))) -OBJECTS_CC = $(addprefix $(BUILD_DIR)/,$(SRCS_CC:.cc=.o)) -# vpath %.cc $(sort $(dir $(SRCS_CC))) -# list of ASM program objects -OBJECTS_S = $(addprefix $(BUILD_DIR)/,$(ASM_SOURCES:.s=.o)) -# vpath %.s $(sort $(dir $(ASM_SOURCES))) - -OBJECTS = ${OBJECTS_C} ${OBJECTS_CC} ${OBJECTS_S} - -# Deps -DEPS_C = $(addprefix ${BUILD_DIR}/,$(SRCS_C:.c=.d)) -DEPS_CC = $(addprefix ${BUILD_DIR}/,$(SRCS_CC:.cc=.d)) -DEPS = ${DEPS_C} ${DEPS_CC} - .PHONY: all # default action: build all all: info compile --include $(DEPS) - -# So I can silence the warning from the HAL lib -${BUILD_DIR}/stm32f4%.o : stm32f4%.c | ${BUILD_DIR} - @echo "[C] $(notdir $<)" - @$(CC) $(HAL_CFLAGS) -c -o $@ $< -MMD -MF ${BUILD_DIR}/$(*F).d -${BUILD_DIR}/main.o : main.c | ${BUILD_DIR} - @echo "[C] $(notdir $<)" - @$(CC) $(HAL_CFLAGS) -c -o $@ $< -MMD -MF ${BUILD_DIR}/$(*F).d - -${BUILD_DIR}/%.o : %.c | ${BUILD_DIR} - @echo "[C] $(notdir $<)" - @$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF ${BUILD_DIR}/$(*F).d - -${BUILD_DIR}/%.o : %.cc | ${BUILD_DIR} - @echo "[CC] $(notdir $<)" - @$(CC) $(CFLAGS) -c -o $@ $< -MMD -MF ${BUILD_DIR}/$(*F).d - -$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR) - @echo "[S] $(notdir $<)" - @$(AS) -c $(CFLAGS) $< -o $@ - -$(BUILD_DIR)/${TARGET}.elf: ${OBJECTS} | ${BUILD_DIR} - @echo "[LD] ${TARGET}.elf" - @$(CC) $(OBJECTS) $(LDFLAGS) -o $@ - @$(DP) -St $@ >$(BUILD_DIR)/${TARGET}.lst - $(SZ) $@ - -$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) - @echo "[LD] ${TARGET}.hex" - @$(HEX) $< $@ - $(SZ) $@ - -$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR) - @echo "[LD] ${TARGET}.bin" - @$(BIN) $< $@ - -compile: $(BUILD_DIR)/${TARGET}.bin $(BUILD_DIR)/${TARGET}.hex +compile: CMakeLists.txt + cmake -B $(BUILD_DIR) + cmake --build $(BUILD_DIR) -j upload: compile st-flash --reset --format ihex write ${BUILD_DIR}/${TARGET}.hex @@ -326,7 +77,6 @@ upload_cube_swd: compile STM32_Programmer_CLI${EXT} ${CUBE_PROGRAMMER_ARGS_SWD} ; \ fi \ - # NOTE - mft is the makefile target if you want to use something other than compile docker: @if ! docker image inspect ${DOCKER_IMAGE} > /dev/null 2>&1; then \ @@ -351,7 +101,6 @@ openocd: monitor: python3 ../tools/monitor.py ${PORT} ${BAUD} - format: find inc -iname "*.h" -or -iname "*.hh" | xargs clang-format -i find src -iname "*.c" -or -iname "*.cc" | xargs clang-format -i diff --git a/firmware/ui/stm32f405rgtx_flash.ld b/firmware/ui/STM32F405XX_FLASH.ld similarity index 81% rename from firmware/ui/stm32f405rgtx_flash.ld rename to firmware/ui/STM32F405XX_FLASH.ld index 4dcf56f0..5bbe8618 100644 --- a/firmware/ui/stm32f405rgtx_flash.ld +++ b/firmware/ui/STM32F405XX_FLASH.ld @@ -22,7 +22,7 @@ ***************************************************************************** ** @attention ** -**

© COPYRIGHT(c) 2019 STMicroelectronics

+**

© COPYRIGHT(c) 2025 STMicroelectronics

** ** Redistribution and use in source and binary forms, with or without modification, ** are permitted provided that the following conditions are met: @@ -103,44 +103,63 @@ SECTIONS . = ALIGN(4); } >FLASH - .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH - .ARM : { + .ARM.extab (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); + *(.ARM.extab* .gnu.linkonce.armextab.*) + . = ALIGN(4); + } >FLASH + + .ARM (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ + { + . = ALIGN(4); __exidx_start = .; *(.ARM.exidx*) __exidx_end = .; + . = ALIGN(4); } >FLASH - .preinit_array : + .preinit_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ { + . = ALIGN(4); PROVIDE_HIDDEN (__preinit_array_start = .); KEEP (*(.preinit_array*)) PROVIDE_HIDDEN (__preinit_array_end = .); + . = ALIGN(4); } >FLASH - .init_array : + + .init_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ { + . = ALIGN(4); PROVIDE_HIDDEN (__init_array_start = .); KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array*)) PROVIDE_HIDDEN (__init_array_end = .); + . = ALIGN(4); } >FLASH - .fini_array : + + .fini_array (READONLY) : /* The "READONLY" keyword is only supported in GCC11 and later, remove it if using GCC10 or earlier. */ { + . = ALIGN(4); PROVIDE_HIDDEN (__fini_array_start = .); KEEP (*(SORT(.fini_array.*))) KEEP (*(.fini_array*)) PROVIDE_HIDDEN (__fini_array_end = .); + . = ALIGN(4); } >FLASH /* used by the startup to initialize data */ _sidata = LOADADDR(.data); /* Initialized data sections goes into RAM, load LMA copy after code */ - .data : + .data : { . = ALIGN(4); _sdata = .; /* create a global symbol at data start */ *(.data) /* .data sections */ *(.data*) /* .data* sections */ + *(.RamFunc) /* .RamFunc sections */ + *(.RamFunc*) /* .RamFunc* sections */ . = ALIGN(4); _edata = .; /* define a global symbol at data end */ @@ -148,11 +167,11 @@ SECTIONS _siccmram = LOADADDR(.ccmram); - /* CCM-RAM section - * - * IMPORTANT NOTE! + /* CCM-RAM section + * + * IMPORTANT NOTE! * If initialized variables will be placed in this section, - * the startup code needs to be modified to copy the init-values. + * the startup code needs to be modified to copy the init-values. */ .ccmram : { @@ -160,12 +179,12 @@ SECTIONS _sccmram = .; /* create a global symbol at ccmram start */ *(.ccmram) *(.ccmram*) - + . = ALIGN(4); _eccmram = .; /* create a global symbol at ccmram end */ } >CCMRAM AT> FLASH - + /* Uninitialized data section */ . = ALIGN(4); .bss : @@ -193,7 +212,7 @@ SECTIONS . = ALIGN(8); } >RAM - + /* Remove information from the standard libraries */ /DISCARD/ : diff --git a/firmware/ui/cmake/FindSTM32Cryptographic.cmake b/firmware/ui/cmake/FindSTM32Cryptographic.cmake new file mode 100644 index 00000000..a5d218d5 --- /dev/null +++ b/firmware/ui/cmake/FindSTM32Cryptographic.cmake @@ -0,0 +1,87 @@ +set(PACKAGE_NAME "STM32Cryptographic") +set(LIBRARY_NAME "STM32Cryptographic") + +if (${PACKAGE_NAME}_FOUND) + return() +endif() + +# Locate the include directory +find_path( + ${PACKAGE_NAME}_INCLUDE_DIR + "cmox_crypto.h" + PATHS + ${${PACKAGE_NAME}_ROOT_DIR} + /usr/local/include + /usr/include + PATH_SUFFIXES include +) + +if(NOT ${PACKAGE_NAME}_INCLUDE_DIR) + message(WARNING "Could not find include dir for ${PACKAGE_NAME}") +else() + message(STATUS "Found include dir for ${PACKAGE_NAME}: ${${PACKAGE_NAME}_INCLUDE_DIR}") + include_directories(${${PACKAGE_NAME}_INCLUDE_DIR}) +endif() + +set(SUPPORTED_CORTICES "CM0_CM0PLUS;CM3;CM33;CM4;CM7") + +# Locate the libraries +foreach(STM32_CORTEX ${${PACKAGE_NAME}_FIND_COMPONENTS}) + find_library( + ${PACKAGE_NAME}_${STM32_CORTEX} + NAMES ${PACKAGE_NAME}_${STM32_CORTEX} + PATHS + ${${PACKAGE_NAME}_ROOT_DIR} + /usr/local/lib + /usr/lib + PATH_SUFFIXES lib + NO_DEFAULT_PATH + ) + + if(NOT ${PACKAGE_NAME}_${STM32_CORTEX}) + message(WARNING "Could not find library: ${PACKAGE_NAME}_${STM32_CORTEX}") + else() + message(STATUS "Found library for ${PACKAGE_NAME}: ${PACKAGE_NAME}_${STM32_CORTEX} at ${${PACKAGE_NAME}_${STM32_CORTEX}}") + endif() + + list(APPEND ${PACKAGE_NAME}_LIBRARY "${${PACKAGE_NAME}_${STM32_CORTEX}}") +endforeach() + +# Set the ${PACKAGE_NAME}_FOUND variable +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(${PACKAGE_NAME} + REQUIRED_VARS + ${PACKAGE_NAME}_INCLUDE_DIR + ${PACKAGE_NAME}_LIBRARY +) + +# Set output variables +if(${PACKAGE_NAME}_FOUND) + set(${PACKAGE_NAME}_INCLUDE_DIRS ${${PACKAGE_NAME}_INCLUDE_DIR}) + set(${PACKAGE_NAME}_LIBRARIES ${${PACKAGE_NAME}_LIBRARY}) +else() + set(${PACKAGE_NAME}_INCLUDE_DIRS "") + set(${PACKAGE_NAME}_LIBRARIES "") +endif() + +mark_as_advanced( + ${PACKAGE_NAME}_INCLUDE_DIR + ${PACKAGE_NAME}_LIBRARY +) + +if (${PACKAGE_NAME}_FOUND) + foreach(STM32_CORTEX ${${PACKAGE_NAME}_FIND_COMPONENTS}) + if(NOT TARGET ${PACKAGE_NAME}::${STM32_CORTEX} AND EXISTS "${${PACKAGE_NAME}_LIBRARY}") + add_library(${PACKAGE_NAME}::${STM32_CORTEX} UNKNOWN IMPORTED) + set_target_properties(${PACKAGE_NAME}::${STM32_CORTEX} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${${PACKAGE_NAME}_INCLUDE_DIR}" + ) + if(EXISTS "${${PACKAGE_NAME}_LIBRARY}") + set_target_properties(${PACKAGE_NAME}::${STM32_CORTEX} PROPERTIES + IMPORTED_LINK_INTERFACE_LANGUAGES "C" + IMPORTED_LOCATION "${${PACKAGE_NAME}_LIBRARY}" + ) + endif() + endif() + endforeach() +endif() diff --git a/firmware/ui/cmake/gcc-arm-none-eabi.cmake b/firmware/ui/cmake/gcc-arm-none-eabi.cmake new file mode 100644 index 00000000..a69dbe41 --- /dev/null +++ b/firmware/ui/cmake/gcc-arm-none-eabi.cmake @@ -0,0 +1,45 @@ +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_PROCESSOR ARM) + +set(CMAKE_C_COMPILER_ID GNU) +set(CMAKE_CXX_COMPILER_ID GNU) + +# Some default GCC settings +# arm-none-eabi- must be part of path environment +set(TOOLCHAIN_PREFIX arm-none-eabi-) + +set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc) +set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) +set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++) +set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}g++) +set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy) +set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size) + +set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf") +set(CMAKE_EXECUTABLE_SUFFIX_C ".elf") +set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf") + +set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) + +# MCU specific flags +set(TARGET_FLAGS "-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard") + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_FLAGS} -Wall -fdata-sections -ffunction-sections") +set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp -MMD -MP") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wpedantic") + +set(CMAKE_C_FLAGS_DEBUG "-O0 -g -gdwarf-2") +set(CMAKE_C_FLAGS_RELEASE "-Os") +set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -gdwarf-2") +set(CMAKE_CXX_FLAGS_RELEASE "-Os") + +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}") + +set(CMAKE_C_LINK_FLAGS "${TARGET_FLAGS}") +set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} --specs=nosys.specs") +set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -T \"${CMAKE_SOURCE_DIR}/STM32F405XX_FLASH.ld\"") +set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -lc -lm -lnosys") +set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,-Map=${CMAKE_PROJECT_NAME}.map,--cref -Wl,--gc-sections") +set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--print-memory-usage") + +set(CMAKE_CXX_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -Wl,--start-group -lstdc++ -lsupc++ -Wl,--end-group") diff --git a/firmware/ui/cmake/stm32cubemx/CMakeLists.txt b/firmware/ui/cmake/stm32cubemx/CMakeLists.txt new file mode 100644 index 00000000..b28bb47a --- /dev/null +++ b/firmware/ui/cmake/stm32cubemx/CMakeLists.txt @@ -0,0 +1,100 @@ +cmake_minimum_required(VERSION 3.22) +# Enable CMake support for ASM and C languages +enable_language(C ASM) +# STM32CubeMX generated symbols (macros) +set(MX_Defines_Syms + USE_HAL_DRIVER + STM32F405xx + $<$:DEBUG> +) + +# STM32CubeMX generated include paths +set(MX_Include_Dirs + ${CMAKE_SOURCE_DIR}/Core/Inc + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Inc + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy + ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Device/ST/STM32F4xx/Include + ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Include +) + +# STM32CubeMX generated application sources +set(MX_Application_Src + ${CMAKE_SOURCE_DIR}/Core/Src/main.c + ${CMAKE_SOURCE_DIR}/Core/Src/stm32f4xx_it.c + ${CMAKE_SOURCE_DIR}/Core/Src/stm32f4xx_hal_msp.c + ${CMAKE_SOURCE_DIR}/Core/Src/sysmem.c + ${CMAKE_SOURCE_DIR}/Core/Src/syscalls.c + ${CMAKE_SOURCE_DIR}/startup_stm32f405xx.s +) + +# STM32 HAL/LL Drivers +set(STM32_Drivers_Src + ${CMAKE_SOURCE_DIR}/Core/Src/system_stm32f4xx.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c + ${CMAKE_SOURCE_DIR}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c +) + +# Drivers Midllewares + + + +# Link directories setup +set(MX_LINK_DIRS + +) +# Project static libraries +set(MX_LINK_LIBS + STM32_Drivers + +) +# Interface library for includes and symbols +add_library(stm32cubemx INTERFACE) +target_include_directories(stm32cubemx INTERFACE ${MX_Include_Dirs}) +target_compile_definitions(stm32cubemx INTERFACE ${MX_Defines_Syms}) + +# Create STM32_Drivers static library +add_library(STM32_Drivers OBJECT) +target_sources(STM32_Drivers PRIVATE ${STM32_Drivers_Src}) +target_link_libraries(STM32_Drivers PUBLIC stm32cubemx) + + +# Add STM32CubeMX generated application sources to the project +target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${MX_Application_Src}) + +# Link directories setup +target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${MX_LINK_DIRS}) + +# Add libraries to the project +target_link_libraries(${CMAKE_PROJECT_NAME} ${MX_LINK_LIBS}) + +# Add the map file to the list of files to be removed with 'clean' target +set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map) + +# Validate that STM32CubeMX code is compatible with C standard +if((CMAKE_C_STANDARD EQUAL 90) OR (CMAKE_C_STANDARD EQUAL 99)) + message(ERROR "Generated code requires C11 or higher") +endif() diff --git a/firmware/ui/dependencies/cmox/LICENSE.txt b/firmware/ui/dependencies/cmox/LICENSE.txt new file mode 100644 index 00000000..a8900afd --- /dev/null +++ b/firmware/ui/dependencies/cmox/LICENSE.txt @@ -0,0 +1,86 @@ +This software component is provided to you as part of a software package and +applicable license terms are in the Package_license file. If you received this +software component outside of a package or without applicable license terms, +the terms of the SLA0044 license shall apply and are fully reproduced below: + +SLA0044 Rev5/February 2018 + +Software license agreement + +ULTIMATE LIBERTY SOFTWARE LICENSE AGREEMENT + +BY INSTALLING, COPYING, DOWNLOADING, ACCESSING OR OTHERWISE USING THIS SOFTWARE +OR ANY PART THEREOF (AND THE RELATED DOCUMENTATION) FROM STMICROELECTRONICS +INTERNATIONAL N.V, SWISS BRANCH AND/OR ITS AFFILIATED COMPANIES +(STMICROELECTRONICS), THE RECIPIENT, ON BEHALF OF HIMSELF OR HERSELF, OR ON +BEHALF OF ANY ENTITY BY WHICH SUCH RECIPIENT IS EMPLOYED AND/OR ENGAGED AGREES +TO BE BOUND BY THIS SOFTWARE LICENSE AGREEMENT. + +Under STMicroelectronics’ intellectual property rights, the redistribution, +reproduction and use in source and binary forms of the software or any part +thereof, with or without modification, are permitted provided that the following +conditions are met: + +1. Redistribution of source code (modified or not) must retain any copyright +notice, this list of conditions and the disclaimer set forth below as items 10 +and 11. + +2. Redistributions in binary form, except as embedded into microcontroller or +microprocessor device manufactured by or for STMicroelectronics or a software +update for such device, must reproduce any copyright notice provided with the +binary code, this list of conditions, and the disclaimer set forth below as +items 10 and 11, in documentation and/or other materials provided with the +distribution. + +3. Neither the name of STMicroelectronics nor the names of other contributors to +this software may be used to endorse or promote products derived from this +software or part thereof without specific written permission. + +4. This software or any part thereof, including modifications and/or derivative +works of this software, must be used and execute solely and exclusively on or in +combination with a microcontroller or microprocessor device manufactured by or +for STMicroelectronics. + +5. No use, reproduction or redistribution of this software partially or totally +may be done in any manner that would subject this software to any Open Source +Terms. “Open Source Terms” shall mean any open source license which requires as +part of distribution of software that the source code of such software is +distributed therewith or otherwise made available, or open source license that +substantially complies with the Open Source definition specified at +www.opensource.org and any other comparable open source license such as for +example GNU General Public License (GPL), Eclipse Public License (EPL), Apache +Software License, BSD license or MIT license. + +6. STMicroelectronics has no obligation to provide any maintenance, support or +updates for the software. + +7. The software is and will remain the exclusive property of STMicroelectronics +and its licensors. The recipient will not take any action that jeopardizes +STMicroelectronics and its licensors' proprietary rights or acquire any rights +in the software, except the limited rights specified hereunder. + +8. The recipient shall comply with all applicable laws and regulations affecting +the use of the software or any part thereof including any applicable export +control law or regulation. + +9. Redistribution and use of this software or any part thereof other than as +permitted under this license is void and will automatically terminate your +rights under this license. + +10. THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY RIGHTS, WHICH ARE +DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT SHALL +STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +11. EXCEPT AS EXPRESSLY PERMITTED HEREUNDER, NO LICENSE OR OTHER RIGHTS, WHETHER +EXPRESS OR IMPLIED, ARE GRANTED UNDER ANY PATENT OR OTHER INTELLECTUAL PROPERTY +RIGHTS OF STMICROELECTRONICS OR ANY THIRD PARTY. + diff --git a/firmware/ui/dependencies/cmox/ReadMe.txt b/firmware/ui/dependencies/cmox/ReadMe.txt new file mode 100644 index 00000000..71695239 --- /dev/null +++ b/firmware/ui/dependencies/cmox/ReadMe.txt @@ -0,0 +1,36 @@ +/************************************************************************************************* +* +* Software implementation of STM32 Cryptographic libraries: High Level API +* +************************************************************************************************** +* +* Copyright (c) 2021 STMicroelectronics. +* All rights reserved. +* +* This software is licensed under terms that can be found in the LICENSE file +* in the root directory of this software component. +************************************************************************************************** +* +* This directory contains the header files of the Cryptographic services exposed by the +* STM32 Cryptographic libraries. +* The Cryptographic services are the followings: +* * AES-128/192/256 Encryption/Decryption in ECB/CBC/CFB/CTR/OFB/XTS/KeyWrap modes +* * AES-128/192/256 CCM/GCM Authenticated Encryption/Decryption +* * SM4 Encryption/Decryption in ECB/CBC/CFB/CTR/OFB modes +* * ChaCha20-Poly1305 Authenticated Encryption/Decryption +* * CTR-DRBG based on AES-128/256 +* * SHA1/SHA2/SHA3/SHAKE/SM3 Hash functions +* * HMAC/CMAC/KMAC Message Authentication Code functions +* * RSA PKCS#1 v1.5/v2.2 Signature Creation/Verification and Encryption/Decryption functions +* * ECDSA/EdDSA/SM2 Key Generation and Signature Creation/Verification functions +* * ECDH Key Exchange functions +* +* Some parts of the STM32 Cryptographic libraries has been inspired from third-party SW components +* from: +* * muNaCl, by Michael Hutter, Peter Schwabe, Bjrn Haase and Ana Helena Snchez +* * libtomcrypt, by T. St-Denis +* * eXtended Keccak Code Package, by Guido Bertoni, Joan Daemen, Seth Hoffert, Michal Peeters, +* Gilles Van Assche and Ronny Van Keer +* * poly1305-donna, by Andrew Moon +* * chacha-ref.c, by D. J. Bernstein +***************************************************************************************************/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_blockcipher.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_blockcipher.h new file mode 100644 index 00000000..be759108 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_blockcipher.h @@ -0,0 +1,103 @@ +/** + ****************************************************************************** + * @file cmox_blockcipher.h + * @author MCD Application Team + * @brief Header file for the block cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_BLOCKCIPHER_H +#define CMOX_BLOCKCIPHER_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include + +#include "cmox_cipher_retvals.h" +#include "cmox_cipher.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_BLOCKCIPHER Block cipher definitions + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_BLOCKCIPHER_MACROS Block cipher macros + * @{ + */ + +#define CMOX_CIPHER_BLOCK_SIZE 4U /*!< Block size, in words, of a block cipher (AES and SM4) */ + +#define CMOX_SM4_EXPKEY_SIZE 32U /*!< Size, in words, of the expanded SM4 key */ +#define CMOX_AES128_EXPKEY_SIZE 44U /*!< Size, in words, of the expanded AES128 key */ +#define CMOX_AES192_EXPKEY_SIZE 52U /*!< Size, in words, of the expanded AES192 key */ +#define CMOX_AES256_EXPKEY_SIZE 60U /*!< Size, in words, of the expanded AES256 key */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_BLOCKCIPHER_PUBLIC_TYPES Block cipher public types + * @{ + */ + +/** + * @brief Block cipher algorithm type + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific algorithm, defined in the library internally + */ +typedef const struct cmox_blockcipher_vtableStruct_st *cmox_blockcipher_vtable_t; + +/** + * @brief Block cipher context handle + */ +typedef struct +{ + cmox_blockcipher_vtable_t table; /*!< Used block cipher algorithm */ + + cmox_cipher_keyLen_t keyLen; /*!< Size, in bytes, of the block cipher's key */ + uint32_t internalState; /*!< Internal state of the block cipher */ + uint32_t expandedKey[CMOX_AES256_EXPKEY_SIZE]; /*!< Buffer containing the expanded key */ +} cmox_blockcipher_handle_t; + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_BLOCKCIPHER_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_cbc.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_cbc.h new file mode 100644 index 00000000..d0f6c838 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_cbc.h @@ -0,0 +1,219 @@ +/** + ****************************************************************************** + * @file cmox_cbc.h + * @author MCD Application Team + * @brief Header file for the CBC cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CBC_H +#define CMOX_CBC_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_default_defs.h" +#include "cmox_check_default_aes.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_CBC CBC cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CBC_PUBLIC_TYPES CBC public types + * @{ + */ + +/** + * @brief CBC mode implementation + * + * This type specifies the used block cipher for the CBC construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_cbc_implStruct_st *cmox_cbc_impl_t; + +/** + * @brief CBC handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the Initialization Vector */ +} cmox_cbc_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CBC_PUBLIC_CONSTANTS CBC public constants + * @{ + */ + +/** @defgroup CMOX_CBC_IMPL CBC implementations + * @{ + */ + +/** + * @brief Implementation of CBC encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_cbc_impl_t CMOX_AESFAST_CBC_ENC; + +/** + * @brief Implementation of CBC decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_cbc_impl_t CMOX_AESFAST_CBC_DEC; + +/** + * @brief Implementation of CBC encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_cbc_impl_t CMOX_AESSMALL_CBC_ENC; + +/** + * @brief Implementation of CBC decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_cbc_impl_t CMOX_AESSMALL_CBC_DEC; + +/** + * @brief Implementation of CBC encryption using SM4 (Defined internally) + */ +extern const cmox_cbc_impl_t CMOX_SM4_CBC_ENC; + +/** + * @brief Implementation of CBC decryption using SM4 (Defined internally) + */ +extern const cmox_cbc_impl_t CMOX_SM4_CBC_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_CBC_ALGO CBC single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the CBC encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_CBC_ENC_ALGO; + +/** + * @brief Identifier of the CBC decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_CBC_DEC_ALGO; + +/** + * @brief Identifier of the CBC encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_CBC_ENC_ALGO; + +/** + * @brief Identifier of the CBC decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_CBC_DEC_ALGO; + +/** + * @brief Identifier of the CBC encryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_CBC_ENC_ALGO; + +/** + * @brief Identifier of the CBC decryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_CBC_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public method prototypes --------------------------------------------------*/ + +/** @defgroup CMOX_CBC_PUBLIC_METHODS CBC public method prototypes + * @{ + */ + +/** + * @brief CBC constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the CBC algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the CBC handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_CBC_ENC + * @arg CMOX_AESFAST_CBC_DEC + * @arg CMOX_AESSMALL_CBC_ENC + * @arg CMOX_AESSMALL_CBC_DEC + * @arg CMOX_SM4_CBC_ENC + * @arg CMOX_SM4_CBC_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_cbc_construct(cmox_cbc_handle_t *P_pThis, + cmox_cbc_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CBC_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_ccm.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_ccm.h new file mode 100644 index 00000000..a430c2ce --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_ccm.h @@ -0,0 +1,204 @@ +/** + ****************************************************************************** + * @file cmox_ccm.h + * @author MCD Application Team + * @brief Header file for the CCM AEAD cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CCM_H +#define CMOX_CCM_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_CCM CCM cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ + + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CCM_PUBLIC_TYPES CCM public types + * @{ + */ + +/** + * @brief CCM mode implementation + * + * This type specifies the used block cipher for the CCM construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_ccm_implStruct_st *cmox_ccm_impl_t; + +/** + * @brief CCM handle structure + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t ivCtr[4]; /*!< Current IV value for encryption.*/ + uint32_t ivCbc[4]; /*!< Current IV value for authentication.*/ + size_t tagLen; /*!< Size of the tag to return. Possible values are values are {4,6,8,10,12,14,16} */ + size_t AdLen; /*!< Len of the associated data to be processed */ + size_t payloadLen; /*!< Size of the total payload data to be processed */ + size_t nonceLen; /*!< Size of the Nonce in bytes. Possible values are {7,8,9,10,11,12,13} */ + uint32_t tmpBuf[4]; /*!< Temporary buffer used for internal computations */ + uint32_t tmpBufUse; /*!< Number of bytes in use for internal computations */ +} cmox_ccm_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CCM_PUBLIC_CONSTANTS CCM public constants + * @{ + */ + +/** @defgroup CMOX_CCM_IMPL CCM implementations + * @{ + */ + +/** + * @brief Implementation of CCM encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ccm_impl_t CMOX_AESFAST_CCM_ENC; + +/** + * @brief Implementation of CCM decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ccm_impl_t CMOX_AESFAST_CCM_DEC; + +/** + * @brief Implementation of CCM encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ccm_impl_t CMOX_AESSMALL_CCM_ENC; + +/** + * @brief Implementation of CCM decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ccm_impl_t CMOX_AESSMALL_CCM_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_CCM_ALGO CCM single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the CCM encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESSMALL_CCM_ENC_ALGO; + +/** + * @brief Identifier of the CCM decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESSMALL_CCM_DEC_ALGO; + +/** + * @brief Identifier of the CCM encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESFAST_CCM_ENC_ALGO; + +/** + * @brief Identifier of the CCM decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESFAST_CCM_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_CCM_PUBLIC_METHODS CCM public method prototypes + * @{ + */ + +/** + * @brief CCM constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the CCM algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the CCM handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_CCM_ENC + * @arg CMOX_AESFAST_CCM_DEC + * @arg CMOX_AESSMALL_CCM_ENC + * @arg CMOX_AESSMALL_CCM_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_ccm_construct(cmox_ccm_handle_t *P_pThis, + cmox_ccm_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CCM_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_cfb.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_cfb.h new file mode 100644 index 00000000..7fe1caa4 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_cfb.h @@ -0,0 +1,221 @@ +/** + ****************************************************************************** + * @file cmox_cfb.h + * @author MCD Application Team + * @brief Header file for the CFB cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CFB_H +#define CMOX_CFB_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_default_defs.h" +#include "cmox_check_default_aes.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_CFB CFB cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CFB_PUBLIC_TYPES CFB public types + * @{ + */ + +/** + * @brief CFB mode implementation + * + * This type specifies the used block cipher for the CFB construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_cfb_implStruct_st *cmox_cfb_impl_t; + +/** + * @brief CFB handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the Initialization Vector */ + size_t cfb_blockLen; /*!< Size of the CFB block. Currently only 16 bytes block supported */ +} cmox_cfb_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CFB_PUBLIC_CONSTANTS CFB public constants + * @{ + */ + +/** @defgroup CMOX_CFB_IMPL CFB implementations + * @{ + */ + +/** + * @brief Implementation of CFB encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_cfb_impl_t CMOX_AESFAST_CFB_ENC; + +/** + * @brief Implementation of CFB decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_cfb_impl_t CMOX_AESFAST_CFB_DEC; + +/** + * @brief Implementation of CFB encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_cfb_impl_t CMOX_AESSMALL_CFB_ENC; + +/** + * @brief Implementation of CFB decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_cfb_impl_t CMOX_AESSMALL_CFB_DEC; + +/** + * @brief Implementation of CFB encryption using SM4 (Defined internally) + */ +extern const cmox_cfb_impl_t CMOX_SM4_CFB_ENC; + +/** + * @brief Implementation of CFB decryption using SM4 (Defined internally) + */ +extern const cmox_cfb_impl_t CMOX_SM4_CFB_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_CFB_ALGO CFB single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the CFB encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_CFB_ENC_ALGO; + +/** + * @brief Identifier of the CFB decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_CFB_DEC_ALGO; + +/** + * @brief Identifier of the CFB encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_CFB_ENC_ALGO; + +/** + * @brief Identifier of the CFB decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_CFB_DEC_ALG; + +/** + * @brief Identifier of the CFB encryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_CFB_ENC_ALGO; + +/** + * @brief Identifier of the CFB decryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_CFB_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_CFB_PUBLIC_METHODS CFB public method prototypes + * @{ + */ + +/** + * @brief CFB constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the CFB algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the CFB handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_CFB_ENC + * @arg CMOX_AESFAST_CFB_DEC + * @arg CMOX_AESSMALL_CFB_ENC + * @arg CMOX_AESSMALL_CFB_DEC + * @arg CMOX_SM4_CFB_ENC + * @arg CMOX_SM4_CFB_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_cfb_construct(cmox_cfb_handle_t *P_pThis, + cmox_cfb_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CFB_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_chachapoly.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_chachapoly.h new file mode 100644 index 00000000..65f78359 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_chachapoly.h @@ -0,0 +1,151 @@ +/** + ****************************************************************************** + * @file cmox_chachapoly.h + * @author MCD Application Team + * @brief Header file for the ChaCha20-Poly1305 AEAD cipher functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CHACHA20_POLY1305_H +#define CMOX_CHACHA20_POLY1305_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_CHACHAPOLY ChaCha20-Poly1305 cipher + * @{ + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CHACHAPOLY_PUBLIC_TYPES ChaCha20-Poly1305 public types + * @{ + */ + +/** + * @brief ChaCha20-Poly1305 mode implementation + * + * This type specifies if the algorithm will be used for encryption or + * decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_chachapoly_implStruct_st *cmox_chachapoly_impl_t; + +/** + * @brief ChaCha20-Poly1305 handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + size_t mAadLen; /*!< Size of the processed AAD */ + size_t mCipherLen; /*!< Size of the processed CipherText */ + uint32_t rValue[5]; /*!< Internal: value of r */ + uint32_t hValue[5]; /*!< Internal: value of h */ + uint32_t pad[4]; /*!< Internal: value of Poly nonce */ + uint32_t internalState[16]; /*!< Internal: ChaCha Internal State */ +} cmox_chachapoly_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CHACHAPOLY_PUBLIC_CONSTANTS ChaCha20-Poly1305 public + * constants + * @{ + */ + +/** + * @brief Implementation of ChaCha20-Poly1305 encryption (Defined internally) + */ +extern const cmox_chachapoly_impl_t CMOX_CHACHAPOLY_ENC; + +/** + * @brief Implementation of ChaCha20-Poly1305 decryption (Defined internally) + */ +extern const cmox_chachapoly_impl_t CMOX_CHACHAPOLY_DEC; + +/** + * @} + */ + +/** + * @brief Identifier of the ChaCha20-Poly1305 encryption for single-call function + * (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_CHACHAPOLY_ENC_ALGO; + +/** + * @brief Identifier of the ChaCha20-Poly1305 decryption for single-call function + * (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_CHACHAPOLY_DEC_ALGO; + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_CHACHAPOLY_PUBLIC_METHODS ChaCha20-Poly1305 public method + * prototypes + * @{ + */ + +/** + * @brief ChaCha20-Poly1305 constructor + * + * The function is used for specifying if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the ChaCha20-Poly1305 handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_CHACHAPOLY_ENC + * @arg CMOX_CHACHAPOLY_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_chachapoly_construct(cmox_chachapoly_handle_t *P_pThis, + cmox_chachapoly_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CHACHA20_POLY1305_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_check_default_aes.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_check_default_aes.h new file mode 100644 index 00000000..53b5010e --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_check_default_aes.h @@ -0,0 +1,59 @@ +/** + ****************************************************************************** + * @file cmox_check_default_aes.h + * @author MCD Application Team + * @brief Header file for checking that the default values for AES are + * correctly set + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CHECK_DEFAULT_AES_H +#define CMOX_CHECK_DEFAULT_AES_H + +#if !defined(CMOX_DEFAULT_FILE) +#include "cmox_default_config.h" +#else +#include CMOX_DEFAULT_FILE +#endif /* CMOX_DEFAULT_FILE */ + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +#ifndef CMOX_AES_IMPLEMENTATION +#error CMOX_AES_IMPLEMENTATION must be defined for using AES +#endif /* CMOX_AES_IMPLEMENTATION */ + +#ifndef CMOX_AES_FAST +#error CMOX_AES_FAST must be defined for using AES +#endif /* CMOX_AES_FAST */ + +#ifndef CMOX_AES_SMALL +#error CMOX_AES_SMALL must be defined for using AES +#endif /* CMOX_AES_SMALL */ + +#if ((CMOX_AES_IMPLEMENTATION != CMOX_AES_FAST) && (CMOX_AES_IMPLEMENTATION != CMOX_AES_SMALL)) +#error CMOX_AES_IMPLEMENTATION value must be CMOX_AES_FAST or CMOX_AES_SMALL +#endif /* ((CMOX_AES_IMPLEMENTATION != CMOX_AES_FAST) && (CMOX_AES_IMPLEMENTATION != CMOX_AES_SMALL)) */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_CHECK_DEFAULT_AES_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_check_default_gcm.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_check_default_gcm.h new file mode 100644 index 00000000..7735c50e --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_check_default_gcm.h @@ -0,0 +1,59 @@ +/** + ****************************************************************************** + * @file cmox_check_default_gcm.h + * @author MCD Application Team + * @brief Header file for checking that the default values for AES are + * correctly set + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CHECK_DEFAULT_GCM_H +#define CMOX_CHECK_DEFAULT_GCM_H + +#if !defined(CMOX_DEFAULT_FILE) +#include "cmox_default_config.h" +#else +#include CMOX_DEFAULT_FILE +#endif /* CMOX_DEFAULT_FILE */ + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +#ifndef CMOX_GCM_IMPLEMENTATION +#error CMOX_GCM_IMPLEMENTATION must be defined for using GCM +#endif /* CMOX_GCM_IMPLEMENTATION */ + +#ifndef CMOX_GCM_FAST +#error CMOX_GCM_FAST must be defined for using GCM +#endif /* CMOX_GCM_FAST */ + +#ifndef CMOX_GCM_SMALL +#error CMOX_GCM_SMALL must be defined for using GCM +#endif /* CMOX_GCM_SMALL */ + +#if ((CMOX_GCM_IMPLEMENTATION != CMOX_GCM_FAST) && (CMOX_GCM_IMPLEMENTATION != CMOX_GCM_SMALL)) +#error CMOX_GCM_IMPLEMENTATION value must be CMOX_GCM_FAST or CMOX_GCM_SMALL +#endif /* ((CMOX_GCM_IMPLEMENTATION != CMOX_GCM_FAST) && (CMOX_GCM_IMPLEMENTATION != CMOX_GCM_SMALL)) */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_CHECK_DEFAULT_GCM_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_cipher.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_cipher.h new file mode 100644 index 00000000..41f63827 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_cipher.h @@ -0,0 +1,496 @@ +/** + ****************************************************************************** + * @file cmox_cipher.h + * @author MCD Application Team + * @brief Header file for the Cipher module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CIPHER_H +#define CMOX_CIPHER_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include +#include +#include "cmox_cipher_retvals.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_CIPHER_KEYSIZE Key sizes for the cipher module + * @{ + */ + +/** + * @brief Size in bytes of AES128 and SM4 keys + */ +#define CMOX_CIPHER_128_BIT_KEY ((cmox_cipher_keyLen_t)16U) + +/** + * @brief Size in bytes of AES192 keys + */ +#define CMOX_CIPHER_192_BIT_KEY ((cmox_cipher_keyLen_t)24U) + +/** + * @brief Size in bytes of AES256 keys + */ +#define CMOX_CIPHER_256_BIT_KEY ((cmox_cipher_keyLen_t)32U) + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CIPHER_PUBLIC_TYPES Cipher module public types + * @{ + */ + +/** + * @brief Type for specifying the key size for the cipher + */ +typedef size_t cmox_cipher_keyLen_t; + +/** + * @brief Cipher Virtual Table + * + * This type specifies a pointer to the virtual table containing the methods + * for a particular algorithm (e.g. CBC or CHACHA20-POLY1305) + */ +typedef const struct cmox_cipher_vtableStruct_st *cmox_cipher_vtable_t; + +/** + * @brief Cipher algorithm type + * + * This type specifies the algorithm to use with the cipher module (e.g. CBC). + * The type is defined as a pointer to a structure, that + * contains the functions for the specific algorithm, defined in the library + * internally + */ +typedef const struct cmox_cipher_algoStruct_st *cmox_cipher_algo_t; + +/** + * @brief AEAD algorithm type + * + * This type specifies the AEAD algorithm to use with the cipher module + * (e.g. CCM or CHACHA20-POLY1305). + * The type is defined as a pointer to a structure, that + * contains the functions for the specific algorithm, defined in the library + * internally + */ +typedef const struct cmox_aead_algoStruct_st *cmox_aead_algo_t; + +/** + * @brief Cipher handle structure definition + */ +typedef struct +{ + cmox_cipher_vtable_t table; /*!< Cipher virtual table */ + uint32_t internalState; /*!< internal state of the cipher handle */ +} cmox_cipher_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_CIPHER_PUBLIC_METHODS Cipher public method prototypes + * @{ + */ + +/** + * @brief Cleanup the cipher handle + * + * @param P_pThis Cipher handle to cleanup + * @return cmox_cipher_retval_t Cipher return value + */ +cmox_cipher_retval_t cmox_cipher_cleanup(cmox_cipher_handle_t *P_pThis); + +/** + * @brief Initialize the cipher handle for performing the specified algorithm + * + * @param P_pThis Cipher handle to initialize + * @return cmox_cipher_retval_t Cipher return value + * @note The cipher handle must be derived from an algorithm-specific handle + * using the correct constructor + */ +cmox_cipher_retval_t cmox_cipher_init(cmox_cipher_handle_t *P_pThis); + +/** + * @brief Se the key to be used with the cipher algorithm + * + * @param P_pThis Cipher handle to set + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Len in bytes of the key + * @return cmox_cipher_retval_t Cipher return value + * @note The XTS mode of operation requires a key with size double than the + * normal one (i.e. AES128-XTS will require a 32 bytes key, AES256-XTS + * will require a 64 bytes key and so on) + */ +cmox_cipher_retval_t cmox_cipher_setKey(cmox_cipher_handle_t *P_pThis, + const uint8_t *P_pKey, + cmox_cipher_keyLen_t P_keyLen); + +/** + * @brief Set the initialization vector/nonce to be used with the cipher + * algorithm + * + * @param P_pThis Cipher handle to set + * @param P_pIv Buffer of bytes containing the IV/nonce + * @param P_ivLen Size in bytes of the key + * @return cmox_cipher_retval_t Cipher return value + */ +cmox_cipher_retval_t cmox_cipher_setIV(cmox_cipher_handle_t *P_pThis, + const uint8_t *P_pIv, + size_t P_ivLen); + +/** + * @brief Set the size of the tag for AEAD cipher + * + * @param P_pThis Cipher handle to set + * @param P_tagLen Size in bytes of the tag + * @return cmox_cipher_retval_t Cipher return value + * @note This function must be used only for AEAD ciphers. If used with other + * cipher, it won't have any effects + * @note For ChaCha20-Poly1305 the tag size is fixed to 16 bytes, and this + * function has no effects + */ +cmox_cipher_retval_t cmox_cipher_setTagLen(cmox_cipher_handle_t *P_pThis, + size_t P_tagLen); + +/** + * @brief Set the total payload size (only for CCM AEAD cipher) + * + * @param P_pThis Cipher handle to set + * @param P_totalPayloadLen Size in bytes of the total payload + * @return cmox_cipher_retval_t Cipher return value + * @note This function must be used for CCM ciphers. If used with other + * cipher, it won't have any effects + */ +cmox_cipher_retval_t cmox_cipher_setPayloadLen(cmox_cipher_handle_t *P_pThis, + size_t P_totalPayloadLen); + +/** + * @brief Set the total authenticated data size (only for CCM AEAD cipher) + * + * @param P_pThis Cipher handle to set + * @param P_totalADLen Size in bytes of the total authenticated data + * @return cmox_cipher_retval_t Cipher return value + * @note This function must be used for CCM ciphers. If used with other + * cipher, it won't have any effects + */ +cmox_cipher_retval_t cmox_cipher_setADLen(cmox_cipher_handle_t *P_pThis, + size_t P_totalADLen); + +/** + * @brief Append additional authenticated data to the cipher handle + * + * @param P_pThis Cipher handle where the authenticated data will be appended + * @param P_pInput Buffer of bytes containing the data to append + * @param P_inputLen Size in bytes of the data to append + * @return cmox_cipher_retval_t Cipher return value + * @note This function must be used only for AEAD ciphers. If used with other + * cipher, it won't have any effects + */ +cmox_cipher_retval_t cmox_cipher_appendAD(cmox_cipher_handle_t *P_pThis, + const uint8_t *P_pInput, + size_t P_inputLen); + +/** + * @brief Append part or the totality of the plaintext/ciphertext and return the + * corresponding ciphertext/plaintext + * + * @param P_pThis Cipher handle to use for ciphering the data + * @param P_pInput Buffer of bytes containing the data to append + * @param P_inputLen Size in bytes of the data to append + * @param P_pOutput Buffer of bytes where there will be stored the encrypted or + * decrypted data + * @param P_pOutputLen Number of bytes that have been generated by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_cipher_retval_t Cipher return value + * @note ECB and CFB modes of operation for AES and SM4 require that the input + * length for each append is multiple of the block size (i.e. 16 bytes) + * @note CBC mode of operation will apply CS2 ciphertext stealing + * NIST SP 800-38A Addendum + * in case the input length is greater than 16 bytes and not multiple of + * 16 bytes. After CS2 is applied, no more appends are allowed. + * @note CTR, OFB, GCM and CCM modes of operation permits to have input not + * multiple of the block size (i.e. 16 bytes for AES and SM4), but after + * this no more further appends are allowed. + * @note For ChaCha20-Poly1305, this function can be called multiple times with + * P_inputLen multiple of 64 bytes. A single, last, call can be made + * with any value for P_inputLen. + * @note With the exception of KEYWRAP algorithm, the minimum size of P_pOutput + * buffer must be P_inputLen. + * @note For KEYWRAP mode, this function must be used to append the key to wrap, + * and must be done in a single call (no partial append of the key is + * supported). The P_pOutput buffer will contain the encrypted key AND the + * 8 bytes authentication tag. So the minimum size of this buffer must be + * P_inputLen + 8. + */ +cmox_cipher_retval_t cmox_cipher_append(cmox_cipher_handle_t *P_pThis, + const uint8_t *P_pInput, + size_t P_inputLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @brief Generate the authenticated tag in case the AEAD operation is an + * encryption + * + * @param P_pThis Cipher handle used for encrypting the data + * @param P_pTag Buffer of bytes where there will be stored the generated tag + * @param P_pTagLen Number of bytes that have been processed by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_cipher_retval_t Cipher return value + * @note This function must be used only for AEAD ciphers in encryption mode. If + * used with other cipher, it won't have any effects. If used with AEAD + * cipher in decryption mode, it will raise an error. + */ +cmox_cipher_retval_t cmox_cipher_generateTag(cmox_cipher_handle_t *P_pThis, + uint8_t *P_pTag, + size_t *P_pTagLen); + +/** + * @brief Authenticate the processed data with the given tag + * + * @param P_pThis Cipher used for decrypting the data + * @param P_pTag Buffer of bytes containing the tag to use for the verification + * @param P_pFaultCheck Optional value to check, together with the retval, + * to verify if some fault happened + * @return cmox_cipher_retval_t Cipher return value + * @note This function must be used only for AEAD ciphers in decryption mode. If + * used with other cipher, it won't have any effects. If used with AEAD + * cipher in encryption mode, it will raise an error. + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must + * be done against the success value, and not comparing the value with the + * failure value. Indeed, in presence of faults, especially P_pFaultCheck, + * could be a dirty value. + */ +cmox_cipher_retval_t cmox_cipher_verifyTag(cmox_cipher_handle_t *P_pThis, + const uint8_t *P_pTag, + uint32_t *P_pFaultCheck); + +/** + * @brief Encrypt or decrypt a message using a symmetric cipher + * + * @param P_algo Identifier of the cipher algorithm to use for the computation. + * This parameter can be one of the following: + * @arg CMOX_AESFAST_ECB_ENC_ALGO + * @arg CMOX_AESFAST_CBC_ENC_ALGO + * @arg CMOX_AESFAST_CTR_ENC_ALGO + * @arg CMOX_AESFAST_CFB_ENC_ALGO + * @arg CMOX_AESFAST_OFB_ENC_ALGO + * @arg CMOX_AESFAST_XTS_ENC_ALGO + * @arg CMOX_AESSMALL_ECB_ENC_ALGO + * @arg CMOX_AESSMALL_CBC_ENC_ALGO + * @arg CMOX_AESSMALL_CTR_ENC_ALGO + * @arg CMOX_AESSMALL_CFB_ENC_ALGO + * @arg CMOX_AESSMALL_OFB_ENC_ALGO + * @arg CMOX_AESSMALL_XTS_ENC_ALGO + * @arg CMOX_AESSMALL_KEYWRAP_ENC_ALGO + * @arg CMOX_SM4_ECB_ENC_ALGO + * @arg CMOX_SM4_CBC_ENC_ALGO + * @arg CMOX_SM4_CTR_ENC_ALGO + * @arg CMOX_SM4_CFB_ENC_ALGO + * @arg CMOX_SM4_OFB_ENC_ALGO + * @param P_pInput Buffer of bytes containing the data to encrypt or decrypt + * @param P_inputLen Length in bytes of the data to encrypt or decrypt + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Length in bytes of the key + * @param P_pIv Buffer of bytes containing the IV/nonce + * @param P_ivLen Length in bytes of the key + * @param P_pOutput Buffer of bytes where there will be stored the encrypted or + * decrypted data + * @param P_pOutputLen Number of bytes that have been processed by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_cipher_retval_t Cipher return value + * @note This single call function cannot be used for AEAD ciphers + */ +cmox_cipher_retval_t cmox_cipher_encrypt(cmox_cipher_algo_t P_algo, + const uint8_t *P_pInput, + size_t P_inputLen, + const uint8_t *P_pKey, + cmox_cipher_keyLen_t P_keyLen, + const uint8_t *P_pIv, + size_t P_ivLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @brief Decrypt a message using a symmetric cipher + * + * @param P_algo Identifier of the cipher algorithm to use for the computation. + * This parameter can be one of the following: + * @arg CMOX_AESFAST_ECB_DEC_ALGO + * @arg CMOX_AESFAST_CBC_DEC_ALGO + * @arg CMOX_AESFAST_CTR_DEC_ALGO + * @arg CMOX_AESFAST_CFB_DEC_ALGO + * @arg CMOX_AESFAST_OFB_DEC_ALGO + * @arg CMOX_AESFAST_XTS_DEC_ALGO + * @arg CMOX_AESFAST_KEYWRAP_DEC_ALGO + * @arg CMOX_AESSMALL_ECB_DEC_ALGO + * @arg CMOX_AESSMALL_CBC_DEC_ALGO + * @arg CMOX_AESSMALL_CTR_DEC_ALGO + * @arg CMOX_AESSMALL_CFB_DEC_ALGO + * @arg CMOX_AESSMALL_OFB_DEC_ALGO + * @arg CMOX_AESSMALL_XTS_DEC_ALGO + * @arg CMOX_AESSMALL_KEYWRAP_DEC_ALGO + * @arg CMOX_SM4_ECB_DEC_ALGO + * @arg CMOX_SM4_CBC_DEC_ALGO + * @arg CMOX_SM4_CTR_DEC_ALGO + * @arg CMOX_SM4_CFB_DEC_ALGO + * @arg CMOX_SM4_OFB_DEC_ALGO + * @param P_pInput Buffer of bytes containing the data to encrypt or decrypt + * @param P_inputLen Length in bytes of the data to encrypt or decrypt + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Length in bytes of the key + * @param P_pIv Buffer of bytes containing the IV/nonce + * @param P_ivLen Length in bytes of the key + * @param P_pOutput Buffer of bytes where there will be stored the decrypted + * data. + * @param P_pOutputLen Number of bytes that have been processed by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_cipher_retval_t Cipher return value + * @note This single call function cannot be used for AEAD ciphers + */ +cmox_cipher_retval_t cmox_cipher_decrypt(cmox_cipher_algo_t P_algo, + const uint8_t *P_pInput, + size_t P_inputLen, + const uint8_t *P_pKey, + cmox_cipher_keyLen_t P_keyLen, + const uint8_t *P_pIv, + size_t P_ivLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @brief Encrypt a message using AEAD cipher + * + * @param P_algo Identifier of the AEAD cipher algorithm to use for the + * computation. This parameter can be one of the following: + * @arg CMOX_AESFAST_GCMFAST_ENC_ALGO + * @arg CMOX_AESFAST_GCMSMALL_ENC_ALGO + * @arg CMOX_AESSMALL_GCMFAST_ENC_ALGO + * @arg CMOX_AESSMALL_GCMSMALL_ENC_ALGO + * @arg CMOX_AESFAST_CCM_ENC_ALGO + * @arg CMOX_AESSMALL_CCM_ENC_ALGO + * @arg CMOX_CHACHAPOLY_ENC_ALGO + * @param P_pInput Buffer of bytes containing the data to encrypt + * @param P_inputLen Length in bytes of the data to encrypt + * @param P_tagLen Length in bytes of the authentication tag to append to the + * ciphertext + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Length in bytes of the key + * @param P_pIv Buffer of bytes containing the IV/nonce + * @param P_ivLen Length in bytes of the key + * @param P_pAddData Buffer of bytes containing the additional data to be used + * for authentication + * @param P_addDataLen Length in bytes of the Additional data + * @param P_pOutput Buffer of bytes where there will be stored the encrypted and + * authenticated data (i.e. ciphertext + tag) + * @param P_pOutputLen Number of bytes that have been generated by the function. + * If correct, it will be the size of the encrypted and authenticated + * data (i.e. ciphertext + tag). + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_cipher_retval_t + * @note This single call function can be used for AEAD ciphers only + */ +cmox_cipher_retval_t cmox_aead_encrypt(cmox_aead_algo_t P_algo, + const uint8_t *P_pInput, + size_t P_inputLen, + size_t P_tagLen, + const uint8_t *P_pKey, + cmox_cipher_keyLen_t P_keyLen, + const uint8_t *P_pIv, + size_t P_ivLen, + const uint8_t *P_pAddData, + size_t P_addDataLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @brief Decrypt a message using AEAD cipher + * + * @param P_algo Identifier of the AEAD cipher algorithm to use for the + * computation. This parameter can be one of the following: + * @arg CMOX_AESFAST_GCMFAST_DEC_ALGO + * @arg CMOX_AESFAST_GCMSMALL_DEC_ALGO + * @arg CMOX_AESSMALL_GCMFAST_DEC_ALGO + * @arg CMOX_AESSMALL_GCMSMALL_DEC_ALGO + * @arg CMOX_AESFAST_CCM_DEC_ALGO + * @arg CMOX_AESSMALL_CCM_DEC_ALGO + * @arg CMOX_CHACHAPOLY_DEC_ALGO + * @param P_pInput Buffer of bytes containing the authenticated and encrypted + * data (i.e. ciphertext + tag) + * @param P_inputLen Length in bytes of the the authenticated and encrypted + * data (i.e. ciphertext + tag) + * @param P_tagLen Length in bytes of the tag + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Length in bytes of the key + * @param P_pIv Buffer of bytes containing the IV/nonce + * @param P_ivLen Length in bytes of the key + * @param P_pAddData Buffer of bytes containing the additional data to be used + * for authentication + * @param P_addDataLen Length in bytes of the Additional data + * @param P_pOutput Buffer of bytes where there will be stored the decrypted + * data + * @param P_pOutputLen Number of bytes that have been generated by the function. + * If correct, it will be the plaintext size. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_cipher_retval_t + * @note This single call function can be used for AEAD ciphers only + */ +cmox_cipher_retval_t cmox_aead_decrypt(cmox_aead_algo_t P_algo, + const uint8_t *P_pInput, + size_t P_inputLen, + size_t P_tagLen, + const uint8_t *P_pKey, + cmox_cipher_keyLen_t P_keyLen, + const uint8_t *P_pIv, + size_t P_ivLen, + const uint8_t *P_pAddData, + size_t P_addDataLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CIPHER_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_cipher_retvals.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_cipher_retvals.h new file mode 100644 index 00000000..6d38b3aa --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_cipher_retvals.h @@ -0,0 +1,105 @@ +/** + ****************************************************************************** + * @file cmox_cipher_retvals.h + * @author MCD Application Team + * @brief Header file containing the return values for the cipher module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CIPHER_RETVALS_H +#define CMOX_CIPHER_RETVALS_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_CIPHER_RETVALS Cipher return values + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** + * @brief Cipher operation successfully performed + */ +#define CMOX_CIPHER_SUCCESS ((cmox_cipher_retval_t)0x00010000U) + +/** + * @brief Some error happens internally in the cipher module + */ +#define CMOX_CIPHER_ERR_INTERNAL ((cmox_cipher_retval_t)0x00010001U) + +/** + * @brief The function is not implemented for the current algorithm + */ +#define CMOX_CIPHER_ERR_NOT_IMPLEMENTED ((cmox_cipher_retval_t)0x00010002U) + +/** + * @brief One or more parameter has been wrongly passed to the function + * (e.g. pointer to NULL) + */ +#define CMOX_CIPHER_ERR_BAD_PARAMETER ((cmox_cipher_retval_t)0x00010003U) + +/** + * @brief Error on performing the operation + * (e.g. an operation has been called before initializing the handle) + */ +#define CMOX_CIPHER_ERR_BAD_OPERATION ((cmox_cipher_retval_t)0x00010004U) + +/** + * @brief A buffer with a wrong size has been passed to the function + */ +#define CMOX_CIPHER_ERR_BAD_INPUT_SIZE ((cmox_cipher_retval_t)0x00010005U) + +/** + * @brief Authentication of the tag has been successful + */ +#define CMOX_CIPHER_AUTH_SUCCESS ((cmox_cipher_retval_t)0x0001C726U) + +/** + * @brief Authentication of the tag failed + */ +#define CMOX_CIPHER_AUTH_FAIL ((cmox_cipher_retval_t)0x00016E93U) + +/* Public types --------------------------------------------------------------*/ + +/** + * @brief Cipher module return value type + */ +typedef uint32_t cmox_cipher_retval_t; + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CIPHER_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_ctr.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_ctr.h new file mode 100644 index 00000000..f89e9822 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_ctr.h @@ -0,0 +1,219 @@ +/** + ****************************************************************************** + * @file cmox_ctr.h + * @author MCD Application Team + * @brief Header file for the CTR cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CTR_H +#define CMOX_CTR_H + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_CTR CTR cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CTR_PUBLIC_TYPES CTR public types + * @{ + */ + +/** + * @brief CTR mode implementation + * + * This type specifies the used block cipher for the CTR construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_ctr_implStruct_st *cmox_ctr_impl_t; + +/** + * @brief CTR handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the Initialization Vector */ +} cmox_ctr_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CTR_PUBLIC_CONSTANTS CTR public constants + * @{ + */ + +/** @defgroup CMOX_CTR_IMPL CTR implementations + * @{ + */ + +/** + * @brief Implementation of CTR encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ctr_impl_t CMOX_AESFAST_CTR_ENC; + +/** + * @brief Implementation of CTR decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ctr_impl_t CMOX_AESFAST_CTR_DEC; + +/** + * @brief Implementation of CTR encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ctr_impl_t CMOX_AESSMALL_CTR_ENC; + +/** + * @brief Implementation of CTR decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ctr_impl_t CMOX_AESSMALL_CTR_DEC; + +/** + * @brief Implementation of CTR encryption using SM4 (Defined internally) + */ +extern const cmox_ctr_impl_t CMOX_SM4_CTR_ENC; + +/** + * @brief Implementation of CTR decryption using SM4 (Defined internally) + */ +extern const cmox_ctr_impl_t CMOX_SM4_CTR_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_CTR_ALGO CTR single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the CTR encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_CTR_ENC_ALGO; + +/** + * @brief Identifier of the CTR decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_CTR_DEC_ALGO; + +/** + * @brief Identifier of the CTR encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_CTR_ENC_ALGO; + +/** + * @brief Identifier of the CTR decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_CTR_DEC_ALGO; + +/** + * @brief Identifier of the CTR encryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_CTR_ENC_ALGO; + +/** + * @brief Identifier of the CTR decryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_CTR_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_CTR_PUBLIC_METHODS CTR public method prototypes + * @{ + */ + +/** + * @brief CTR constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the CTR algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the CTR handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_CTR_ENC + * @arg CMOX_AESFAST_CTR_DEC + * @arg CMOX_AESSMALL_CTR_ENC + * @arg CMOX_AESSMALL_CTR_DEC + * @arg CMOX_SM4_CTR_ENC + * @arg CMOX_SM4_CTR_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_ctr_construct(cmox_ctr_handle_t *P_pThis, + cmox_ctr_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CTR_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_ecb.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_ecb.h new file mode 100644 index 00000000..4c592196 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_ecb.h @@ -0,0 +1,218 @@ +/** + ****************************************************************************** + * @file cmox_ecb.h + * @author MCD Application Team + * @brief Header file for the ECB cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_ECB_H +#define CMOX_ECB_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_ECB ECB cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_ECB_PUBLIC_TYPES ECB public types + * @{ + */ + +/** + * @brief ECB mode implementation + * + * This type specifies the used block cipher for the ECB construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_ecb_implStruct_st *cmox_ecb_impl_t; + +/** + * @brief ECB handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ +} cmox_ecb_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_ECB_PUBLIC_CONSTANTS ECB public constants + * @{ + */ + +/** @defgroup CMOX_ECB_IMPL ECB implementations + * @{ + */ + +/** + * @brief Implementation of ECB encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ecb_impl_t CMOX_AESFAST_ECB_ENC; + +/** + * @brief Implementation of ECB decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ecb_impl_t CMOX_AESFAST_ECB_DEC; + +/** + * @brief Implementation of ECB encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ecb_impl_t CMOX_AESSMALL_ECB_ENC; + +/** + * @brief Implementation of ECB decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ecb_impl_t CMOX_AESSMALL_ECB_DEC; + +/** + * @brief Implementation of ECB encryption using SM4 (Defined internally) + */ +extern const cmox_ecb_impl_t CMOX_SM4_ECB_ENC; + +/** + * @brief Implementation of ECB decryption using SM4 (Defined internally) + */ +extern const cmox_ecb_impl_t CMOX_SM4_ECB_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_ECB_ALGO ECB single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the ECB encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_ECB_ENC_ALGO; + +/** + * @brief Identifier of the ECB decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_ECB_DEC_ALGO; + +/** + * @brief Identifier of the ECB encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_ECB_ENC_ALGO; + +/** + * @brief Identifier of the ECB decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_ECB_DEC_ALGO; + +/** + * @brief Identifier of the ECB encryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_ECB_ENC_ALGO; + +/** + * @brief Identifier of the ECB decryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_ECB_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_ECB_PUBLIC_METHODS ECB public method prototypes + * @{ + */ + +/** + * @brief ECB constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the ECB algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the ECB handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_ECB_ENC + * @arg CMOX_AESFAST_ECB_DEC + * @arg CMOX_AESSMALL_ECB_ENC + * @arg CMOX_AESSMALL_ECB_DEC + * @arg CMOX_SM4_ECB_ENC + * @arg CMOX_SM4_ECB_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_ecb_construct(cmox_ecb_handle_t *P_pThis, + cmox_ecb_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_ECB_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_gcm.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_gcm.h new file mode 100644 index 00000000..bf1c7d02 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_gcm.h @@ -0,0 +1,414 @@ +/** + ****************************************************************************** + * @file cmox_gcm.h + * @author MCD Application Team + * @brief Header file for the GCM AEAD cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_GCM_H +#define CMOX_GCM_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_gcm.h" +#include "cmox_check_default_aes.h" + +#include "cmox_default_defs.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_GCM GCM cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_GCM_PUBLIC_TYPES GCM public types + * @{ + */ + +/** + * @brief Type for specifying the GFMUL operation to use (internally used) + */ +typedef const struct cmox_gcm_gfmulTable_st *cmox_gcm_gfmul_t; + +/** + * @brief GCM SMALL mode implementation + * + * This type specifies the used block cipher for the GCM construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_gcmSmall_implStruct_st *cmox_gcmSmall_impl_t; + + +/** + * @brief GCM FAST mode implementation + * + * This type specifies the used block cipher for the GCM construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_gcmFast_implStruct_st *cmox_gcmFast_impl_t; + +/** + * @brief Polynomial type used internally by GCM + */ +typedef uint32_t cmox_gcm_poly_t[4]; + +/** + * @brief Polynomial table type used by GCM SMALL implementation + */ +typedef cmox_gcm_poly_t cmox_gcm_table16_t[16]; + +/** + * @brief Polynomial table type used by GCM FAST implementation + */ +typedef cmox_gcm_poly_t cmox_gcm_table8x16_t[8][16]; + + +/** + * @brief Common fields for both GCM FAST and GCM SMALL handles + */ +typedef struct +{ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the IV */ + size_t tagLen; /*!< Size in bytes of the aithentication tag */ + size_t AdLen; /*!< Size in bytes of the Additional Data */ + size_t payloadLen; /*!< Processed payload size in bytes */ + cmox_gcm_poly_t partialAuth; /*!< Partial authentication value */ + cmox_gcm_gfmul_t gfmul; /*!< GF mul implementation */ +} cmox_gcm_common_t; + +/** + * @brief GCM SMALL handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_gcm_common_t common; /*!< Common values with the GCM FAST handle type */ + cmox_gcm_table16_t precomputedValues; /*!< Precomputation of polynomial according to Shoup's 4-bit table */ +} cmox_gcmSmall_handle_t; + +/** + * @brief GCM fast context + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_gcm_common_t common; /*!< Common values with the GCM SMALL handle type */ + cmox_gcm_table8x16_t precomputedValues; /*!< Precomputation of polynomial according to Shoup's 8-bit table */ +} cmox_gcmFast_handle_t; + +#if (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST) + +/** + * @brief Default GCM handle definition + */ +typedef cmox_gcmFast_handle_t cmox_gcm_handle_t; + +/** + * @brief Default GCM implementation definition + */ +typedef cmox_gcmFast_impl_t cmox_gcm_impl_t; + +#elif (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_SMALL) +/** + * @brief Default GCM handle definition + */ +typedef cmox_gcmSmall_handle_t cmox_gcm_handle_t; + +/** + * @brief Default GCM implementation definition + */ +typedef cmox_gcmSmall_impl_t cmox_gcm_impl_t; + +#endif /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST */ + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_GCM_PUBLIC_CONSTANTS GCM public constants + * @{ + */ + +/** @defgroup CMOX_GCM_IMPL GCM implementations + * @{ + */ + +/** + * @brief Implementation of GCMSMALL encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_gcmSmall_impl_t CMOX_AESFAST_GCMSMALL_ENC; + +/** + * @brief Implementation of GCMSMALL decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_gcmSmall_impl_t CMOX_AESFAST_GCMSMALL_DEC; + +/** + * @brief Implementation of GCMSMALL encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_gcmSmall_impl_t CMOX_AESSMALL_GCMSMALL_ENC; + +/** + * @brief Implementation of GCMSMALL decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_gcmSmall_impl_t CMOX_AESSMALL_GCMSMALL_DEC; + +/** + * @brief Implementation of GCMFAST encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_gcmFast_impl_t CMOX_AESFAST_GCMFAST_ENC; + +/** + * @brief Implementation of GCMFAST decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_gcmFast_impl_t CMOX_AESFAST_GCMFAST_DEC; + +/** + * @brief Implementation of GCMFAST encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_gcmFast_impl_t CMOX_AESSMALL_GCMFAST_ENC; + +/** + * @brief Implementation of GCMFAST decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_gcmFast_impl_t CMOX_AESSMALL_GCMFAST_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_GCM_ALGO GCM single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the GCM (fast implementation) encryption using AES + * (small implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESSMALL_GCMFAST_ENC_ALGO; + +/** + * @brief Identifier of the GCM (fast implementation) decryption using AES + * (small implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESSMALL_GCMFAST_DEC_ALGO; + +/** + * @brief Identifier of the GCM (fast implementation) encryption using AES + * (fast implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESFAST_GCMFAST_ENC_ALGO; + +/** + * @brief Identifier of the GCM (fast implementation) decryption using AES + * (fast implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESFAST_GCMFAST_DEC_ALGO; + +/** + * @brief Identifier of the GCM (small implementation) encryption using AES + * (small implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESSMALL_GCMSMALL_ENC_ALGO; + +/** + * @brief Identifier of the GCM (small implementation) decryption using AES + * (small implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESSMALL_GCMSMALL_DEC_ALGO; + +/** + * @brief Identifier of the GCM (small implementation) encryption using AES + * (fast implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESFAST_GCMSMALL_ENC_ALGO; + +/** + * @brief Identifier of the GCM (small implementation) decryption using AES + * (fast implementation) for single-call function (Defined internally) + */ +extern const cmox_aead_algo_t CMOX_AESFAST_GCMSMALL_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_GCM_PUBLIC_METHODS GCM public method prototypes + * @{ + */ + +/** + * @brief GCMSMALL constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the GCM algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the GCM handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_GCMSMALL_ENC + * @arg CMOX_AESFAST_GCMSMALL_DEC + * @arg CMOX_AESSMALL_GCMSMALL_ENC + * @arg CMOX_AESSMALL_GCMSMALL_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + * @note If CMOX_GCM_IMPLEMENTATION macro has been set to CMOX_GCM_SMALL, this + * constructor can be called also through the wrapper cmox_gcm_construct + */ +cmox_cipher_handle_t *cmox_gcmSmall_construct(cmox_gcmSmall_handle_t *P_pThis, + cmox_gcmSmall_impl_t P_impl); + +/** + * @brief GCMFAST constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the GCM algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the GCM handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_GCMFAST_ENC + * @arg CMOX_AESFAST_GCMFAST_DEC + * @arg CMOX_AESSMALL_GCMFAST_ENC + * @arg CMOX_AESSMALL_GCMFAST_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + * @note If CMOX_GCM_IMPLEMENTATION macro has been set to CMOX_GCM_FAST, this + * constructor can be called also through the wrapper cmox_gcm_construct + */ +cmox_cipher_handle_t *cmox_gcmFast_construct(cmox_gcmFast_handle_t *P_pThis, + cmox_gcmFast_impl_t P_impl); + +#if (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST) + +/** + * @brief GCM default constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the GCM algorithm (default implementation) and if the + * algorithm will be used for encryption or decryption. + * + * @param P_pThis Pointer to the GCM handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AES_GCM_ENC + * @arg CMOX_AES_GCM_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + * @note This function is just a wrapper of the default implementation of the + * GCM algorithm. It is possible to select the preferred GCM + * implementation from the cmox_default_config.h header file or using a custom + * default configuration header file, to be specified by the + * CMOX_DEFAULT_FILE macro + */ +static inline +cmox_cipher_handle_t *cmox_gcm_construct(cmox_gcm_handle_t *P_pThis, + cmox_gcm_impl_t P_impl) +{ + return cmox_gcmFast_construct(P_pThis, P_impl); +} + +#elif (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_SMALL) + +/** + * @brief GCM default constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the GCM algorithm (default implementation) and if the + * algorithm will be used for encryption or decryption. + * + * @param P_pThis Pointer to the GCM handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AES_GCM_ENC + * @arg CMOX_AES_GCM_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + * @note This function is just a wrapper of the default implementation of the + * GCM algorithm. It is possible to select the preferred GCM + * implementation from the cmox_default_config.h header file or using a custom + * default configuration header file, to be specified by the + * CMOX_DEFAULT_FILE macro + */ +static inline +cmox_cipher_handle_t *cmox_gcm_construct(cmox_gcm_handle_t *P_pThis, + cmox_gcm_impl_t P_impl) +{ + return cmox_gcmSmall_construct(P_pThis, P_impl); +} + +#endif /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_GCM_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_keywrap.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_keywrap.h new file mode 100644 index 00000000..9b20f8e5 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_keywrap.h @@ -0,0 +1,209 @@ +/** + ****************************************************************************** + * @file cmox_keywrap.h + * @author MCD Application Team + * @brief Header file for the KEYWRAP cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_KEYWRAP_H +#define CMOX_KEYWRAP_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_KEYWRAP KEYWRAP cipher + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_KEYWRAP_PUBLIC_MACROS KEYWRAP public macros + * @{ + */ + +/** + * @brief Size in bytes of the keywrap tag + */ +#define CMOX_KEYWRAP_TAG_LEN 8U + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_KEYWRAP_PUBLIC_TYPES KEYWRAP public types + * @{ + */ + +/** + * @brief KEYWRAP mode implementation + * + * This type specifies the used block cipher for the KEYWRAP construct and if + * the algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_keywrap_implStruct_st *cmox_keywrap_impl_t; + +/** + * @brief KEYWRAP handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the Initialization Vector */ +} cmox_keywrap_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_KEYWRAP_PUBLIC_CONSTANTS KEYWRAP public constants + * @{ + */ + +/** @defgroup CMOX_KEYWRAP_IMPL KEYWRAP implementations + * @{ + */ + +/** + * @brief Implementation of KEYWRAP encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_keywrap_impl_t CMOX_AESFAST_KEYWRAP_ENC; + +/** + * @brief Implementation of KEYWRAP decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_keywrap_impl_t CMOX_AESFAST_KEYWRAP_DEC; + +/** + * @brief Implementation of KEYWRAP encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_keywrap_impl_t CMOX_AESSMALL_KEYWRAP_ENC; + +/** + * @brief Implementation of KEYWRAP decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_keywrap_impl_t CMOX_AESSMALL_KEYWRAP_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_KEYWRAP_ALGO KEYWRAP single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the KEYWRAP encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_KEYWRAP_ENC_ALGO; + +/** + * @brief Identifier of the KEYWRAP decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_KEYWRAP_DEC_ALGO; + +/** + * @brief Identifier of the KEYWRAP encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_KEYWRAP_ENC_ALGO; + +/** + * @brief Identifier of the KEYWRAP decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_KEYWRAP_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_KEYWRAP_PUBLIC_METHODS KEYWRAP public method prototypes + * @{ + */ + +/** + * @brief KEYWRAP constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the KEYWRAP algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the KEYWRAP handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_KEYWRAP_ENC + * @arg CMOX_AESFAST_KEYWRAP_DEC + * @arg CMOX_AESSMALL_KEYWRAP_ENC + * @arg CMOX_AESSMALL_KEYWRAP_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_keywrap_construct(cmox_keywrap_handle_t *P_pThis, + cmox_keywrap_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_KEYWRAP_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_ofb.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_ofb.h new file mode 100644 index 00000000..4a3e2aed --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_ofb.h @@ -0,0 +1,219 @@ +/** + ****************************************************************************** + * @file cmox_ofb.h + * @author MCD Application Team + * @brief Header file for the OFB cipher definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_OFB_H +#define CMOX_OFB_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_OFB OFB cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_OFB_PUBLIC_TYPES OFB public types + * @{ + */ + +/** + * @brief OFB mode implementation + * + * This type specifies the used block cipher for the OFB construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_ofb_implStruct_st *cmox_ofb_impl_t; + +/** + * @brief OFB handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the Initialization Vector */ +} cmox_ofb_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_OFB_PUBLIC_CONSTANTS OFB public constants + * @{ + */ + +/** @defgroup CMOX_OFB_IMPL OFB implementations + * @{ + */ + +/** + * @brief Implementation of OFB encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ofb_impl_t CMOX_AESFAST_OFB_ENC; + +/** + * @brief Implementation of OFB decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_ofb_impl_t CMOX_AESFAST_OFB_DEC; + +/** + * @brief Implementation of OFB encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ofb_impl_t CMOX_AESSMALL_OFB_ENC; + +/** + * @brief Implementation of OFB decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_ofb_impl_t CMOX_AESSMALL_OFB_DEC; + +/** + * @brief Implementation of OFB encryption using SM4 (Defined internally) + */ +extern const cmox_ofb_impl_t CMOX_SM4_OFB_ENC; + +/** + * @brief Implementation of OFB decryption using SM4 (Defined internally) + */ +extern const cmox_ofb_impl_t CMOX_SM4_OFB_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_OFB_ALGO OFB single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the OFB encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_OFB_ENC_ALGO; + +/** + * @brief Identifier of the OFB decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_OFB_DEC_ALGO; + +/** + * @brief Identifier of the OFB encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_OFB_ENC_ALGO; + +/** + * @brief Identifier of the OFB decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_OFB_DEC_ALGO; + +/** + * @brief Identifier of the OFB encryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_OFB_ENC_ALGO; + +/** + * @brief Identifier of the OFB decryption using SM4 + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_SM4_OFB_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_OFB_PUBLIC_METHODS OFB public method prototypes + * @{ + */ + +/** + * @brief OFB constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the OFB algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the OFB handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_OFB_ENC + * @arg CMOX_AESFAST_OFB_DEC + * @arg CMOX_AESSMALL_OFB_ENC + * @arg CMOX_AESSMALL_OFB_DEC + * @arg CMOX_SM4_OFB_ENC + * @arg CMOX_SM4_OFB_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_ofb_construct(cmox_ofb_handle_t *P_pThis, + cmox_ofb_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_OFB_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cipher/cmox_xts.h b/firmware/ui/dependencies/cmox/include/cipher/cmox_xts.h new file mode 100644 index 00000000..5cdb9980 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cipher/cmox_xts.h @@ -0,0 +1,196 @@ +/** + ****************************************************************************** + * @file cmox_xts.h + * @author MCD Application Team + * @brief Header file for the XTS cipher + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_XTS_H +#define CMOX_XTS_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_cipher.h" +#include "cmox_blockcipher.h" + +#include "cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +/** @addtogroup CMOX_CIPHER + * @{ + */ + +/** @defgroup CMOX_XTS XTS cipher + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_XTS_PUBLIC_TYPES XTS public types + * @{ + */ + +/** + * @brief XTS mode implementation + * + * This type specifies the used block cipher for the XTS construct and if the + * algorithm will be used for encryption or decryption. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_xts_implStruct_st *cmox_xts_impl_t; + +/** + * @brief XTS handle structure definition + */ +typedef struct +{ + cmox_cipher_handle_t super; /*!< General cipher handle */ + cmox_blockcipher_handle_t blockCipher1; /*!< Block cipher handle */ + cmox_blockcipher_handle_t blockCipher2; /*!< Block cipher handle */ + uint32_t tweak[CMOX_CIPHER_BLOCK_SIZE]; /*!< Temporary result/tweak */ +} cmox_xts_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_XTS_PUBLIC_CONSTANTS XTS public constants + * @{ + */ + +/** @defgroup CMOX_XTS_IMPL XTS implementations + * @{ + */ + +/** + * @brief Implementation of XTS encryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_xts_impl_t CMOX_AESFAST_XTS_ENC; + +/** + * @brief Implementation of XTS decryption using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_xts_impl_t CMOX_AESFAST_XTS_DEC; + +/** + * @brief Implementation of XTS encryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_xts_impl_t CMOX_AESSMALL_XTS_ENC; + +/** + * @brief Implementation of XTS decryption using AES (small implementation) + * (Defined internally) + */ +extern const cmox_xts_impl_t CMOX_AESSMALL_XTS_DEC; + +/** + * @} + */ + +/** @defgroup CMOX_XTS_ALGO XTS single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the XTS encryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_XTS_ENC_ALGO; + +/** + * @brief Identifier of the XTS decryption using AES (small implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESSMALL_XTS_DEC_ALGO; + +/** + * @brief Identifier of the XTS encryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_XTS_ENC_ALGO; + +/** + * @brief Identifier of the XTS decryption using AES (fast implementation) + for single-call function (Defined internally) + */ +extern const cmox_cipher_algo_t CMOX_AESFAST_XTS_DEC_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public method prototypes --------------------------------------------------*/ + +/** @defgroup CMOX_XTS_PUBLIC_METHODS XTS public method prototypes + * @{ + */ + +/** + * @brief XTS constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the XTS algorithm and if the algorithm will be used for + * encryption or decryption. + * + * @param P_pThis Pointer to the XTS handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_AESFAST_XTS_ENC + * @arg CMOX_AESFAST_XTS_DEC + * @arg CMOX_AESSMALL_XTS_ENC + * @arg CMOX_AESSMALL_XTS_DEC + * @return cmox_cipher_handle_t* Pointer to a general cipher handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_cipher_handle_t *cmox_xts_construct(cmox_xts_handle_t *P_pThis, + cmox_xts_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_XTS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_common.h b/firmware/ui/dependencies/cmox/include/cmox_common.h new file mode 100644 index 00000000..53886970 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_common.h @@ -0,0 +1,75 @@ +/** + ****************************************************************************** + * @file cmox_common.h + * @author MCD Application Team + * @brief This file provides the types used within the RSA module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_COMMON_H +#define CMOX_COMMON_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @brief Structure to store information on the static memory + */ +typedef struct +{ + uint8_t *MemBuf; /*!< Pointer to the pre-allocated memory buffer */ + size_t MemBufSize; /*!< Total size of the pre-allocated memory buffer */ + size_t MemBufUsed; /*!< Currently used portion of the buffer */ + size_t MaxMemUsed; /*!< Max memory used */ +} cmox_membuf_handle_st; + + +/** + * @brief Mathematical functions customizations for RSA and ECC + */ +typedef const struct cmox_math_funcsStruct_st *cmox_math_funcs_t; + +extern const cmox_math_funcs_t CMOX_MATH_FUNCS_SMALL; /*!< Smaller footprint and slower performance */ +extern const cmox_math_funcs_t CMOX_MATH_FUNCS_FAST; /*!< Bigger footprint and faster performance */ +extern const cmox_math_funcs_t CMOX_MATH_FUNCS_SUPERFAST256; /*!< Bigger footprint and faster performance, + speed up those ECC curves whose length is in the + range [225, 256] bits, e.g. Curve25519, Secp256, + Bpp256, Ed25519, Frp256, SM2. */ + + +/** + * @brief Modular exponentiation functions customizations for RSA + */ +typedef const struct cmox_modexp_funcStruct_st *cmox_modexp_func_t; + +extern const cmox_modexp_func_t CMOX_MODEXP_PUBLIC; /*!< Suggested Modexp value for the target device public operations */ + +extern const cmox_modexp_func_t CMOX_MODEXP_PRIVATE_LOWMEM; /*!< Constant-time (for Private operations) using Low Memory */ +extern const cmox_modexp_func_t CMOX_MODEXP_PRIVATE_MIDMEM; /*!< Constant-time (for Private operations) using Mid Memory */ +extern const cmox_modexp_func_t CMOX_MODEXP_PRIVATE_HIGHMEM; /*!< Constant-time (for Private operations) using High Memory */ + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_COMMON_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_crypto.h b/firmware/ui/dependencies/cmox/include/cmox_crypto.h new file mode 100644 index 00000000..325f1494 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_crypto.h @@ -0,0 +1,142 @@ +/** + ****************************************************************************** + * @file cmox_crypto.h + * @author MCD Application Team + * @brief Header file including all the supported cryptographic algorithms + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_CRYPTO_H +#define CMOX_CRYPTO_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @defgroup CMOX_CRYPTO Cortex-M Optimized Crypto Stack + * @{ + */ + +/** @defgroup CMOX_HASH Hash module + * @{ + */ + +#include "hash/cmox_sha1.h" +#include "hash/cmox_sha224.h" +#include "hash/cmox_sha256.h" +#include "hash/cmox_sha384.h" +#include "hash/cmox_sha512.h" +#include "hash/cmox_sha3.h" +#include "hash/cmox_sm3.h" + +/** + * @} + */ + +/** @defgroup CMOX_CIPHER Cipher module + * @{ + */ + +#include "cipher/cmox_cbc.h" +#include "cipher/cmox_ccm.h" +#include "cipher/cmox_cfb.h" +#include "cipher/cmox_chachapoly.h" +#include "cipher/cmox_ctr.h" +#include "cipher/cmox_ecb.h" +#include "cipher/cmox_gcm.h" +#include "cipher/cmox_keywrap.h" +#include "cipher/cmox_ofb.h" +#include "cipher/cmox_xts.h" + +/** + * @} + */ + +/** @defgroup CMOX_MAC MAC module + * @{ + */ + +#include "mac/cmox_cmac.h" +#include "mac/cmox_hmac.h" +#include "mac/cmox_kmac.h" + +/** + * @} + */ + +/** @defgroup CMOX_RSA RSA module + * @{ + */ +#include "rsa/cmox_rsa_pkcs1v15.h" +#include "rsa/cmox_rsa_pkcs1v22.h" +/** + * @} + */ + +/** @defgroup CMOX_ECC ECC module + * @{ + */ +#include "ecc/cmox_ecdsa.h" +#include "ecc/cmox_eddsa.h" +#include "ecc/cmox_sm2.h" +#include "ecc/cmox_ecdh.h" +/** + * @} + */ + +/** @defgroup CMOX_DRBG DRBG module + * @{ + */ +#include "drbg/cmox_ctr_drbg.h" +/** + * @} + */ + +/** @defgroup CMOX_UTILS Utils module + * @{ + */ +#include "utils/cmox_utils_compare.h" +/** + * @} + */ + +/** @defgroup CMOX_INFO Information module + * @{ + */ +#include "cmox_info.h" +/** + * @} + */ + +/** @defgroup CMOX_INIT Initialization module + * @{ + */ +#include "cmox_init.h" +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_CRYPTO_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_cta.h b/firmware/ui/dependencies/cmox/include/cmox_cta.h new file mode 100644 index 00000000..54cba399 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_cta.h @@ -0,0 +1,50 @@ +/** + ****************************************************************************** + * @file cmox_cta.h + * @author MCD Application Team + * @brief CTA specific features + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CTA_H +#define CMOX_CTA_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @brief Provides a label in order to move tables into RAM/CCM/TCM and being + * protected against Cache Timing Attacks (CTA) + * @note This macro uses extended GCC preprocessor features, that can be used + * with ARMCC compiler and EWARM compiler with GCC extension enabled. + * If not supported by the user toolchain configuration, it is possible + * to replace the macro with the following: + * + * \code + * #define CMOX_CTA_RESISTANT _Pragma("location=\"CMOX_CTA_PROTECTED_DATA\"") + * \endcode + */ +#define CMOX_CTA_RESISTANT __attribute__((section("CMOX_CTA_PROTECTED_DATA"))) + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CTA_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_default_config.h b/firmware/ui/dependencies/cmox/include/cmox_default_config.h new file mode 100644 index 00000000..c07e3d07 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_default_config.h @@ -0,0 +1,363 @@ +/** + ****************************************************************************** + * @file cmox_default_config.h + * @author MCD Application Team + * @brief Header file for default configuration of some algorithms + * @note It is possible to replace this header file with a custom one with + * desired configuration. In this case it must be set the macro + * CMOX_DEFAULT_FILE with the name of the custom file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_DEFAULT_CONFIG_H +#define CMOX_DEFAULT_CONFIG_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_CRYPTO + * @{ + */ + +/** + * @defgroup CMOX_DEFAULT CMOX default configurations + * @{ + */ + +/** + * @defgroup CMOX_AES_DEFAULT Default AES configuration + * @{ + */ + +/** + * @brief Flag indicating the fast implementation of AES + */ +#define CMOX_AES_FAST 0x01U + +/** + * @brief Flag indicating the small implementation of AES + */ +#define CMOX_AES_SMALL 0x02U + +/** + * @brief Flag indicating the default implementation of AES + * @note Value can be + * - @ref CMOX_AES_SMALL + * - @ref CMOX_AES_FAST + */ +#define CMOX_AES_IMPLEMENTATION CMOX_AES_FAST + +/** + * @} + */ + +/** + * @defgroup CMOX_GCM_DEFAULT Default GCM configuration + * @{ + */ + +/** + * @brief Flag indicating the fast implementation of GCM + */ +#define CMOX_GCM_FAST 0x0AU + +/** + * @brief Flag indicating the small implementation of GCM + */ +#define CMOX_GCM_SMALL 0x0BU + +/** + * @brief Flag indicating the default implementation of GCM + * @note Value can be + * - @ref CMOX_GCM_SMALL + * - @ref CMOX_GCM_FAST + */ +#define CMOX_GCM_IMPLEMENTATION CMOX_GCM_FAST + +/** + * @} + */ + +/** + * @defgroup CMOX_MATH_DEFAULT Default Math customizations + * @{ + */ + +/** + * @brief Flag indicating the default implementation for RSA low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + */ +#define CMOX_RSA_MATH_FUNCS CMOX_MATH_FUNCS_FAST + +/** + * @brief Flag indicating the default implementation for ECC 256 bits curves + * low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + * - @ref CMOX_MATH_FUNCS_SUPERFAST256 + */ +#define CMOX_ECC256_MATH_FUNCS CMOX_MATH_FUNCS_SUPERFAST256 + +/** + * @brief Flag indicating the default implementation for ECC 128 bits multiple curves + * low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + */ +#define CMOX_ECC128MULT_MATH_FUNCS CMOX_MATH_FUNCS_FAST + +/** + * @brief Flag indicating the default implementation for ECC curves not defined by the above + * low level mathematical functions + * @note Value can be + * * for Cortex-M0/Cortex-M0+ devices + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + * * Others + * - @ref CMOX_MATH_FUNCS_SMALL + */ +#if defined(__TARGET_ARCH_6M) +#define CMOX_ECC_MATH_FUNCS CMOX_MATH_FUNCS_FAST +#else /* __TARGET_ARCH_6M */ +#define CMOX_ECC_MATH_FUNCS CMOX_MATH_FUNCS_SMALL +#endif /* __TARGET_ARCH_6M */ + +/** + * @} + */ + +/** + * @defgroup CMOX_ECC_DEFAULT Default ECC customizations + * @{ + */ + +/** + * @brief Flag indicating the default implementation for EDWARDS Ed25519 ECC curves + * @note Value can be + * - @ref CMOX_ECC_ED25519_OPT_LOWMEM + * - @ref CMOX_ECC_ED25519_HIGHMEM + * - @ref CMOX_ECC_ED25519_OPT_HIGHMEM + */ +#define CMOX_ECC_CURVE_ED25519 CMOX_ECC_ED25519_OPT_HIGHMEM +/** + * @brief Flag indicating the default implementation for EDWARDS Ed448 ECC curves + * @note Value can be + * - @ref CMOX_ECC_ED448_LOWMEM + * - @ref CMOX_ECC_ED448_HIGHMEM + */ +#define CMOX_ECC_CURVE_ED448 CMOX_ECC_ED448_HIGHMEM + +/** + * @brief Flag indicating the default implementation for NIST-R P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP224R1_LOWMEM + * - @ref CMOX_ECC_SECP224R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP224R1 CMOX_ECC_SECP224R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for NIST-R P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP256R1_LOWMEM + * - @ref CMOX_ECC_SECP256R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP256R1 CMOX_ECC_SECP256R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for NIST-R P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP384R1_LOWMEM + * - @ref CMOX_ECC_SECP384R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP384R1 CMOX_ECC_SECP384R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for NIST-R P-521 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP521R1_LOWMEM + * - @ref CMOX_ECC_SECP521R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP521R1 CMOX_ECC_SECP521R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for NIST-K P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP256K1_LOWMEM + * - @ref CMOX_ECC_SECP256K1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP256K1 CMOX_ECC_SECP256K1_HIGHMEM + +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-160 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP160R1_LOWMEM + * - @ref CMOX_ECC_BPP160R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP160R1 CMOX_ECC_BPP160R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-192 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP192R1_LOWMEM + * - @ref CMOX_ECC_BPP192R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP192R1 CMOX_ECC_BPP192R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP224R1_LOWMEM + * - @ref CMOX_ECC_BPP224R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP224R1 CMOX_ECC_BPP224R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP256R1_LOWMEM + * - @ref CMOX_ECC_BPP256R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP256R1 CMOX_ECC_BPP256R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-320 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP320R1_LOWMEM + * - @ref CMOX_ECC_BPP320R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP320R1 CMOX_ECC_BPP320R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP384R1_LOWMEM + * - @ref CMOX_ECC_BPP384R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP384R1 CMOX_ECC_BPP384R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-R P-512 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP512R1_LOWMEM + * - @ref CMOX_ECC_BPP512R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP512R1 CMOX_ECC_BPP512R1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-160 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP160T1_LOWMEM + * - @ref CMOX_ECC_BPP160T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP160T1 CMOX_ECC_BPP160T1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-192 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP192T1_LOWMEM + * - @ref CMOX_ECC_BPP192T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP192T1 CMOX_ECC_BPP192T1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP224T1_LOWMEM + * - @ref CMOX_ECC_BPP224T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP224T1 CMOX_ECC_BPP224T1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP256T1_LOWMEM + * - @ref CMOX_ECC_BPP256T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP256T1 CMOX_ECC_BPP256T1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-320 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP320T1_LOWMEM + * - @ref CMOX_ECC_BPP320T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP320T1 CMOX_ECC_BPP320T1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP384T1_LOWMEM + * - @ref CMOX_ECC_BPP384T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP384T1 CMOX_ECC_BPP384T1_HIGHMEM +/** + * @brief Flag indicating the default implementation for BRAINPOOL-T P-512 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP512T1_LOWMEM + * - @ref CMOX_ECC_BPP512T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP512T1 CMOX_ECC_BPP512T1_HIGHMEM + +/** + * @brief Flag indicating the default implementation for ANSSI P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_FRP256V1_LOWMEM + * - @ref CMOX_ECC_FRP256V1_HIGHMEM + */ +#define CMOX_ECC_CURVE_FRP256V1 CMOX_ECC_FRP256V1_HIGHMEM + +/** + * @brief Flag indicating the default implementation for OSCCA 256 bit ECC curves + * @note Value can be + * - @ref CMOX_ECC_SM2_LOWMEM + * - @ref CMOX_ECC_SM2_HIGHMEM + */ +#define CMOX_ECC_CURVE_SM2 CMOX_ECC_SM2_HIGHMEM + +/** + * @brief Flag indicating the default implementation for OSCCA 256 bit test ECC curves + * @note Value can be + * - @ref CMOX_ECC_SM2TEST_LOWMEM + * - @ref CMOX_ECC_SM2TEST_HIGHMEM + */ +#define CMOX_ECC_CURVE_SM2TEST CMOX_ECC_SM2TEST_HIGHMEM + +/** + * @} + */ + +/** + * @defgroup CMOX_RSA_DEFAULT Default RSA customizations + * @{ + */ + +/** + * @brief Flag indicating the default private modular exponentiation implementation + * @note Value can be + * - @ref CMOX_MODEXP_PRIVATE_LOWMEM + * - @ref CMOX_MODEXP_PRIVATE_MIDMEM + * - @ref CMOX_MODEXP_PRIVATE_HIGHMEM + */ +#define CMOX_MODEXP_PRIVATE CMOX_MODEXP_PRIVATE_HIGHMEM + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_DEFAULT_CONFIG_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_default_defs.h b/firmware/ui/dependencies/cmox/include/cmox_default_defs.h new file mode 100644 index 00000000..3f083f4e --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_default_defs.h @@ -0,0 +1,551 @@ +/** + ******************************************************************************* + * @file cmox_default_defs.h + * @author MCD Application Team + * @brief Header file that defines the macros for default algorithms + * optimizations + * @note Do not modify the content of this file for changing the default + * algorithm optimizations. Instead, modify the content of + * cmox_default_config.h or use a custom header file with desired + * configuration. In this case it must be set the macro + * CMOX_DEFAULT_FILE with the name of the custom file. + ******************************************************************************* + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ******************************************************************************* + */ + +#ifndef CMOX_DEFAULT_DEFS_H +#define CMOX_DEFAULT_DEFS_H + + +#if !defined(CMOX_DEFAULT_FILE) +#include "cmox_default_config.h" +#else +#include CMOX_DEFAULT_FILE +#endif /* CMOX_DEFAULT_FILE */ + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @defgroup CMOX_DEFAULT_MACROS Default algorithms optimizations + * @{ + */ + +/** @defgroup CMOX_DEFAULT_IMPL Default implementations + * @{ + */ + +#if (CMOX_AES_IMPLEMENTATION == CMOX_AES_FAST) + +/** + * @brief Implementation of CBC Encryption using AES + */ +#define CMOX_AES_CBC_ENC CMOX_AESFAST_CBC_ENC + +/** + * @brief Implementation of CBC Decryption using AES + */ +#define CMOX_AES_CBC_DEC CMOX_AESFAST_CBC_DEC + +/** + * @brief Implementation of CCM Encryption using AES + */ +#define CMOX_AES_CCM_ENC CMOX_AESFAST_CCM_ENC + +/** + * @brief Implementation of CCM Decryption using AES + */ +#define CMOX_AES_CCM_DEC CMOX_AESFAST_CCM_DEC + +/** + * @brief Implementation of CFB Encryption using AES + */ +#define CMOX_AES_CFB_ENC CMOX_AESFAST_CFB_ENC + +/** + * @brief Implementation of CFB Decryption using AES + */ +#define CMOX_AES_CFB_DEC CMOX_AESFAST_CFB_DEC + +/** + * @brief Implementation of CTR Encryption using AES + */ +#define CMOX_AES_CTR_ENC CMOX_AESFAST_CTR_ENC + +/** + * @brief Implementation of CTR Decryption using AES + */ +#define CMOX_AES_CTR_DEC CMOX_AESFAST_CTR_DEC + +/** + * @brief Implementation of ECB Encryption using AES + */ +#define CMOX_AES_ECB_ENC CMOX_AESFAST_ECB_ENC + +/** + * @brief Implementation of ECB Decryption using AES + */ +#define CMOX_AES_ECB_DEC CMOX_AESFAST_ECB_DEC + +#if (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST) + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC CMOX_AESFAST_GCMFAST_ENC + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC CMOX_AESFAST_GCMFAST_DEC + + +#else /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_SMALL */ + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC CMOX_AESFAST_GCMSMALL_ENC + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC CMOX_AESFAST_GCMSMALL_DEC + +#endif /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST */ + +/** + * @brief Implementation of KEYWRAP Encryption using AES + */ +#define CMOX_AES_KEYWRAP_ENC CMOX_AESFAST_KEYWRAP_ENC + +/** + * @brief Implementation of KEYWRAP Decryption using AES + */ +#define CMOX_AES_KEYWRAP_DEC CMOX_AESFAST_KEYWRAP_DEC + +/** + * @brief Implementation of OFB Encryption using AES + */ +#define CMOX_AES_OFB_ENC CMOX_AESFAST_OFB_ENC + +/** + * @brief Implementation of OFB Decryption using AES + */ +#define CMOX_AES_OFB_DEC CMOX_AESFAST_OFB_DEC + +/** + * @brief Implementation of XTS Encryption using AES + */ +#define CMOX_AES_XTS_ENC CMOX_AESFAST_XTS_ENC + +/** + * @brief Implementation of XTS Decryption using AES + */ +#define CMOX_AES_XTS_DEC CMOX_AESFAST_XTS_DEC + +/** + * @brief Implementation of CTR-DRBG Encryption using AES128 with derivation + * function and no prediction resistance + */ +#define CMOX_CTR_DRBG_AES128 CMOX_CTR_DRBG_AES128_FAST + +/** + * @brief Implementation of CTR-DRBG Encryption using AES256 with derivation + * function and no prediction resistance + */ +#define CMOX_CTR_DRBG_AES256 CMOX_CTR_DRBG_AES256_FAST + +/** + * @brief Implementation of AES-CMAC + */ +#define CMOX_CMAC_AES CMOX_CMAC_AESFAST + +#else /* CMOX_AES_IMPLEMENTATION == CMOX_AES_SMALL */ + +/** + * @brief Implementation of CBC Encryption using AES + */ +#define CMOX_AES_CBC_ENC CMOX_AESSMALL_CBC_ENC + +/** + * @brief Implementation of CBC Decryption using AES + */ +#define CMOX_AES_CBC_DEC CMOX_AESSMALL_CBC_DEC + +/** + * @brief Implementation of CCM Encryption using AES + */ +#define CMOX_AES_CCM_ENC CMOX_AESSMALL_CCM_ENC + +/** + * @brief Implementation of CCM Decryption using AES + */ +#define CMOX_AES_CCM_DEC CMOX_AESSMALL_CCM_DEC + +/** + * @brief Implementation of CFB Encryption using AES + */ +#define CMOX_AES_CFB_ENC CMOX_AESSMALL_CFB_ENC + +/** + * @brief Implementation of CFB Decryption using AES + */ +#define CMOX_AES_CFB_DEC CMOX_AESSMALL_CFB_DEC + +/** + * @brief Implementation of CTR Encryption using AES + */ +#define CMOX_AES_CTR_ENC CMOX_AESSMALL_CTR_ENC + +/** + * @brief Implementation of CTR Decryption using AES + */ +#define CMOX_AES_CTR_DEC CMOX_AESSMALL_CTR_DEC + +/** + * @brief Implementation of ECB Encryption using AES + */ +#define CMOX_AES_ECB_ENC CMOX_AESSMALL_ECB_ENC + +/** + * @brief Implementation of ECB Decryption using AES + */ +#define CMOX_AES_ECB_DEC CMOX_AESSMALL_ECB_DEC + +#if (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST) + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC CMOX_AESSMALL_GCMFAST_ENC + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC CMOX_AESSMALL_GCMFAST_DEC + +#else /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_SMALL */ + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC CMOX_AESSMALL_GCMSMALL_ENC + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC CMOX_AESSMALL_GCMSMALL_DEC + +#endif /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST */ + +/** + * @brief Implementation of KEYWRAP Encryption using AES + */ +#define CMOX_AES_KEYWRAP_ENC CMOX_AESSMALL_KEYWRAP_ENC + +/** + * @brief Implementation of KEYWRAP Decryption using AES + */ +#define CMOX_AES_KEYWRAP_DEC CMOX_AESSMALL_KEYWRAP_DEC + +/** + * @brief Implementation of OFB Encryption using AES + */ +#define CMOX_AES_OFB_ENC CMOX_AESSMALL_OFB_ENC + +/** + * @brief Implementation of OFB Decryption using AES + */ +#define CMOX_AES_OFB_DEC CMOX_AESSMALL_OFB_DEC + +/** + * @brief Implementation of XTS Encryption using AES + */ +#define CMOX_AES_XTS_ENC CMOX_AESSMALL_XTS_ENC + +/** + * @brief Implementation of XTS Decryption using AES + */ +#define CMOX_AES_XTS_DEC CMOX_AESSMALL_XTS_DEC + +/** + * @brief Implementation of CTR-DRBG Encryption using AES128 with derivation + * function and no prediction resistance + */ +#define CMOX_CTR_DRBG_AES128 CMOX_CTR_DRBG_AES128_SMALL + +/** + * @brief Implementation of CTR-DRBG Encryption using AES256 with derivation + * function and no prediction resistance + */ +#define CMOX_CTR_DRBG_AES256 CMOX_CTR_DRBG_AES256_SMALL + +/* + * @brief Implementation of AES-CMAC + */ +#define CMOX_CMAC_AES CMOX_CMAC_AESSMALL + +#endif /* CMOX_AES_IMPLEMENTATION == CMOX_AES_FAST */ + +/** + * @} + */ + +/** @defgroup CMOX_DEFAULT_ALGO Default single-call algorithms + * @{ + */ + +#if (CMOX_AES_IMPLEMENTATION == CMOX_AES_FAST) + +/** + * @brief Implementation of CBC Encryption using AES + */ +#define CMOX_AES_CBC_ENC_ALGO CMOX_AESFAST_CBC_ENC_ALGO + +/** + * @brief Implementation of CBC Decryption using AES + */ +#define CMOX_AES_CBC_DEC_ALGO CMOX_AESFAST_CBC_DEC_ALGO + +/** + * @brief Implementation of CCM Encryption using AES + */ +#define CMOX_AES_CCM_ENC_ALGO CMOX_AESFAST_CCM_ENC_ALGO + +/** + * @brief Implementation of CCM Decryption using AES + */ +#define CMOX_AES_CCM_DEC_ALGO CMOX_AESFAST_CCM_DEC_ALGO + +/** + * @brief Implementation of CFB Encryption using AES + */ +#define CMOX_AES_CFB_ENC_ALGO CMOX_AESFAST_CFB_ENC_ALGO + +/** + * @brief Implementation of CFB Decryption using AES + */ +#define CMOX_AES_CFB_DEC_ALGO CMOX_AESFAST_CFB_DEC_ALG + +/** + * @brief Implementation of CTR Encryption using AES + */ +#define CMOX_AES_CTR_ENC_ALGO CMOX_AESFAST_CTR_ENC_ALGO + +/** + * @brief Implementation of CTR Decryption using AES + */ +#define CMOX_AES_CTR_DEC_ALGO CMOX_AESFAST_CTR_DEC_ALGO + +/** + * @brief Implementation of ECB Encryption using AES + */ +#define CMOX_AES_ECB_ENC_ALGO CMOX_AESFAST_ECB_ENC_ALGO + +/** + * @brief Implementation of ECB Decryption using AES + */ +#define CMOX_AES_ECB_DEC_ALGO CMOX_AESFAST_ECB_DEC_ALGO + +#if (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST) + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC_ALGO CMOX_AESFAST_GCMFAST_ENC_ALGO + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC_ALGO CMOX_AESFAST_GCMFAST_DEC_ALGO + +#else /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_SMALL */ + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC_ALGO CMOX_AESFAST_GCMSMALL_ENC_ALGO + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC_ALGO CMOX_AESFAST_GCMSMALL_DEC_ALGO + +#endif /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST */ + +/** + * @brief Implementation of KEYWRAP Encryption using AES + */ +#define CMOX_AES_KEYWRAP_ENC_ALGO CMOX_AESFAST_KEYWRAP_ENC_ALGO + +/** + * @brief Implementation of KEYWRAP Decryption using AES + */ +#define CMOX_AES_KEYWRAP_DEC_ALGO CMOX_AESFAST_KEYWRAP_DEC_ALGO + +/** + * @brief Implementation of OFB Encryption using AES + */ +#define CMOX_AES_OFB_ENC_ALGO CMOX_AESFAST_OFB_ENC_ALGO + +/** + * @brief Implementation of OFB Decryption using AES + */ +#define CMOX_AES_OFB_DEC_ALGO CMOX_AESFAST_OFB_DEC_ALGO + +/** + * @brief Implementation of XTS Encryption using AES + */ +#define CMOX_AES_XTS_ENC_ALGO CMOX_AESFAST_XTS_ENC_ALGO + +/** + * @brief Implementation of XTS Decryption using AES + */ +#define CMOX_AES_XTS_DEC_ALGO CMOX_AESFAST_XTS_DEC_ALGO + +/** + * @brief Implementation of AES-CMAC algorithm + */ +#define CMOX_CMAC_AES_ALGO CMOX_CMAC_AESFAST_ALGO + +#else /* CMOX_AES_IMPLEMENTATION == CMOX_AES_SMALL */ + +/** + * @brief Implementation of CBC Encryption using AES + */ +#define CMOX_AES_CBC_ENC_ALGO CMOX_AESSMALL_CBC_ENC_ALGO + +/** + * @brief Implementation of CBC Decryption using AES + */ +#define CMOX_AES_CBC_DEC_ALGO CMOX_AESSMALL_CBC_DEC_ALGO + +/** + * @brief Implementation of CCM Encryption using AES + */ +#define CMOX_AES_CCM_ENC_ALGO CMOX_AESSMALL_CCM_ENC_ALGO + +/** + * @brief Implementation of CCM Decryption using AES + */ +#define CMOX_AES_CCM_DEC_ALGO CMOX_AESSMALL_CCM_DEC_ALGO + +/** + * @brief Implementation of CFB Encryption using AES + */ +#define CMOX_AES_CFB_ENC_ALGO CMOX_AESSMALL_CFB_ENC_ALGO + +/** + * @brief Implementation of CFB Decryption using AES + */ +#define CMOX_AES_CFB_DEC_ALGO CMOX_AESSMALL_CFB_DEC_ALGO + +/** + * @brief Implementation of CTR Encryption using AES + */ +#define CMOX_AES_CTR_ENC_ALGO CMOX_AESSMALL_CTR_ENC_ALGO + +/** + * @brief Implementation of CTR Decryption using AES + */ +#define CMOX_AES_CTR_DEC_ALGO CMOX_AESSMALL_CTR_DEC_ALGO + +/** + * @brief Implementation of ECB Encryption using AES + */ +#define CMOX_AES_ECB_ENC_ALGO CMOX_AESSMALL_ECB_ENC_ALGO + +/** + * @brief Implementation of ECB Decryption using AES + */ +#define CMOX_AES_ECB_DEC_ALGO CMOX_AESSMALL_ECB_DEC_ALGO + +#if (CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST) + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC_ALGO CMOX_AESSMALL_GCMFAST_ENC_ALGO + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC_ALGO CMOX_AESSMALL_GCMFAST_DEC_ALGO + +#else /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_SMALL */ + +/** + * @brief Implementation of GCM Encryption using AES + */ +#define CMOX_AES_GCM_ENC_ALGO CMOX_AESSMALL_GCMSMALL_ENC_ALGO + +/** + * @brief Implementation of GCM Decryption using AES + */ +#define CMOX_AES_GCM_DEC_ALGO CMOX_AESSMALL_GCMSMALL_DEC_ALGO + +#endif /* CMOX_GCM_IMPLEMENTATION == CMOX_GCM_FAST */ + +/** + * @brief Implementation of KEYWRAP Encryption using AES + */ +#define CMOX_AES_KEYWRAP_ENC_ALGO CMOX_AESSMALL_KEYWRAP_ENC_ALGO + +/** + * @brief Implementation of KEYWRAP Decryption using AES + */ +#define CMOX_AES_KEYWRAP_DEC_ALGO CMOX_AESSMALL_KEYWRAP_DEC_ALGO + +/** + * @brief Implementation of OFB Encryption using AES + */ +#define CMOX_AES_OFB_ENC_ALGO CMOX_AESSMALL_OFB_ENC_ALGO + +/** + * @brief Implementation of OFB Decryption using AES + */ +#define CMOX_AES_OFB_DEC_ALGO CMOX_AESSMALL_OFB_DEC_ALGO + +/** + * @brief Implementation of XTS Encryption using AES + */ +#define CMOX_AES_XTS_ENC_ALGO CMOX_AESSMALL_XTS_ENC_ALGO + +/** + * @brief Implementation of XTS Decryption using AES + */ +#define CMOX_AES_XTS_DEC_ALGO CMOX_AESSMALL_XTS_DEC_ALGO + +/** + * @brief Implementation of AES-CMAC algorithm + */ +#define CMOX_CMAC_AES_ALGO CMOX_CMAC_AESSMALL_ALGO + +#endif /* CMOX_AES_IMPLEMENTATION == CMOX_AES_FAST */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_DEFAULT_DEFS_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_fast_config.h b/firmware/ui/dependencies/cmox/include/cmox_fast_config.h new file mode 100644 index 00000000..d434232d --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_fast_config.h @@ -0,0 +1,360 @@ +/** + ****************************************************************************** + * @file cmox_fast_config.h + * @author MCD Application Team + * @brief Header file for fastest configuration of some algorithms + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_FAST_CONFIG_H +#define CMOX_FAST_CONFIG_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_CRYPTO + * @{ + */ + +/** + * @defgroup CMOX_FAST CMOX fast configurations + * @{ + */ + +/** + * @defgroup CMOX_AES_FAST Fast AES configuration + * @{ + */ + +/** + * @brief Flag indicating the fast implementation of AES + */ +#define CMOX_AES_FAST 0x01U + +/** + * @brief Flag indicating the small implementation of AES + */ +#define CMOX_AES_SMALL 0x02U + +/** + * @brief Flag indicating the fast implementation of AES + * @note Value can be + * - @ref CMOX_AES_SMALL + * - @ref CMOX_AES_FAST + */ +#define CMOX_AES_IMPLEMENTATION CMOX_AES_FAST + +/** + * @} + */ + +/** + * @defgroup CMOX_GCM_FAST Fast GCM configuration + * @{ + */ + +/** + * @brief Flag indicating the fast implementation of GCM + */ +#define CMOX_GCM_FAST 0x0AU + +/** + * @brief Flag indicating the small implementation of GCM + */ +#define CMOX_GCM_SMALL 0x0BU + +/** + * @brief Flag indicating the fast implementation of GCM + * @note Value can be + * - @ref CMOX_GCM_SMALL + * - @ref CMOX_GCM_FAST + */ +#define CMOX_GCM_IMPLEMENTATION CMOX_GCM_FAST + +/** + * @} + */ + +/** + * @defgroup CMOX_MATH_FAST Fast Math customizations + * @{ + */ + +/** + * @brief Flag indicating the fast implementation for RSA low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + */ +#define CMOX_RSA_MATH_FUNCS CMOX_MATH_FUNCS_FAST + +/** + * @brief Flag indicating the fast implementation for ECC 256 bits curves + * low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + * - @ref CMOX_MATH_FUNCS_SUPERFAST256 + */ +#define CMOX_ECC256_MATH_FUNCS CMOX_MATH_FUNCS_SUPERFAST256 + +/** + * @brief Flag indicating the fast implementation for ECC 128 bits multiple curves + * low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + */ +#define CMOX_ECC128MULT_MATH_FUNCS CMOX_MATH_FUNCS_FAST + +/** + * @brief Flag indicating the fast implementation for ECC curves not defined by the above + * low level mathematical functions + * @note Value can be + * * for Cortex-M0/Cortex-M0+ devices + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + * * Others + * - @ref CMOX_MATH_FUNCS_SMALL + */ +#if defined(__TARGET_ARCH_6M) +#define CMOX_ECC_MATH_FUNCS CMOX_MATH_FUNCS_FAST +#else /* __TARGET_ARCH_6M */ +#define CMOX_ECC_MATH_FUNCS CMOX_MATH_FUNCS_SMALL +#endif /* __TARGET_ARCH_6M */ + +/** + * @} + */ + +/** + * @defgroup CMOX_ECC_FAST Fast ECC customizations + * @{ + */ + +/** + * @brief Flag indicating the fast implementation for EDWARDS Ed25519 ECC curves + * @note Value can be + * - @ref CMOX_ECC_ED25519_OPT_LOWMEM + * - @ref CMOX_ECC_ED25519_HIGHMEM + * - @ref CMOX_ECC_ED25519_OPT_HIGHMEM + */ +#define CMOX_ECC_CURVE_ED25519 CMOX_ECC_ED25519_OPT_HIGHMEM +/** + * @brief Flag indicating the fast implementation for EDWARDS Ed448 ECC curves + * @note Value can be + * - @ref CMOX_ECC_ED448_LOWMEM + * - @ref CMOX_ECC_ED448_HIGHMEM + */ +#define CMOX_ECC_CURVE_ED448 CMOX_ECC_ED448_HIGHMEM + +/** + * @brief Flag indicating the fast implementation for NIST-R P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP224R1_LOWMEM + * - @ref CMOX_ECC_SECP224R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP224R1 CMOX_ECC_SECP224R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for NIST-R P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP256R1_LOWMEM + * - @ref CMOX_ECC_SECP256R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP256R1 CMOX_ECC_SECP256R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for NIST-R P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP384R1_LOWMEM + * - @ref CMOX_ECC_SECP384R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP384R1 CMOX_ECC_SECP384R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for NIST-R P-521 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP521R1_LOWMEM + * - @ref CMOX_ECC_SECP521R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP521R1 CMOX_ECC_SECP521R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for NIST-K P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP256K1_LOWMEM + * - @ref CMOX_ECC_SECP256K1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP256K1 CMOX_ECC_SECP256K1_HIGHMEM + +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-160 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP160R1_LOWMEM + * - @ref CMOX_ECC_BPP160R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP160R1 CMOX_ECC_BPP160R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-192 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP192R1_LOWMEM + * - @ref CMOX_ECC_BPP192R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP192R1 CMOX_ECC_BPP192R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP224R1_LOWMEM + * - @ref CMOX_ECC_BPP224R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP224R1 CMOX_ECC_BPP224R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP256R1_LOWMEM + * - @ref CMOX_ECC_BPP256R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP256R1 CMOX_ECC_BPP256R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-320 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP320R1_LOWMEM + * - @ref CMOX_ECC_BPP320R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP320R1 CMOX_ECC_BPP320R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP384R1_LOWMEM + * - @ref CMOX_ECC_BPP384R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP384R1 CMOX_ECC_BPP384R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-R P-512 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP512R1_LOWMEM + * - @ref CMOX_ECC_BPP512R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP512R1 CMOX_ECC_BPP512R1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-160 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP160T1_LOWMEM + * - @ref CMOX_ECC_BPP160T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP160T1 CMOX_ECC_BPP160T1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-192 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP192T1_LOWMEM + * - @ref CMOX_ECC_BPP192T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP192T1 CMOX_ECC_BPP192T1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP224T1_LOWMEM + * - @ref CMOX_ECC_BPP224T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP224T1 CMOX_ECC_BPP224T1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP256T1_LOWMEM + * - @ref CMOX_ECC_BPP256T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP256T1 CMOX_ECC_BPP256T1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-320 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP320T1_LOWMEM + * - @ref CMOX_ECC_BPP320T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP320T1 CMOX_ECC_BPP320T1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP384T1_LOWMEM + * - @ref CMOX_ECC_BPP384T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP384T1 CMOX_ECC_BPP384T1_HIGHMEM +/** + * @brief Flag indicating the fast implementation for BRAINPOOL-T P-512 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP512T1_LOWMEM + * - @ref CMOX_ECC_BPP512T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP512T1 CMOX_ECC_BPP512T1_HIGHMEM + +/** + * @brief Flag indicating the fast implementation for ANSSI P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_FRP256V1_LOWMEM + * - @ref CMOX_ECC_FRP256V1_HIGHMEM + */ +#define CMOX_ECC_CURVE_FRP256V1 CMOX_ECC_FRP256V1_HIGHMEM + +/** + * @brief Flag indicating the fast implementation for OSCCA 256 bit ECC curves + * @note Value can be + * - @ref CMOX_ECC_SM2_LOWMEM + * - @ref CMOX_ECC_SM2_HIGHMEM + */ +#define CMOX_ECC_CURVE_SM2 CMOX_ECC_SM2_HIGHMEM + +/** + * @brief Flag indicating the fast implementation for OSCCA 256 bit test ECC curves + * @note Value can be + * - @ref CMOX_ECC_SM2TEST_LOWMEM + * - @ref CMOX_ECC_SM2TEST_HIGHMEM + */ +#define CMOX_ECC_CURVE_SM2TEST CMOX_ECC_SM2TEST_HIGHMEM + +/** + * @} + */ + +/** + * @defgroup CMOX_RSA_FAST Fast RSA customizations + * @{ + */ + +/** + * @brief Flag indicating the fast private modular exponentiation implementation + * @note Value can be + * - @ref CMOX_MODEXP_PRIVATE_LOWMEM + * - @ref CMOX_MODEXP_PRIVATE_MIDMEM + * - @ref CMOX_MODEXP_PRIVATE_HIGHMEM + */ +#define CMOX_MODEXP_PRIVATE CMOX_MODEXP_PRIVATE_HIGHMEM + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_FAST_CONFIG_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_info.h b/firmware/ui/dependencies/cmox/include/cmox_info.h new file mode 100644 index 00000000..f87d8639 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_info.h @@ -0,0 +1,70 @@ +/** + ****************************************************************************** + * @file cmox_info.h + * @author MCD Application Team + * @brief This file exports symbols needed to use information module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_INFO_H +#define CMOX_INFO_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + + +/** @addtogroup CMOX_CRYPTO + * @{ + */ + +/** @addtogroup CMOX_INFO Information module + * @{ + */ + +/** + * @brief Structure to store information + */ +typedef struct +{ + uint32_t version; /*!< Library version */ + uint32_t build[7]; /*!< Build info */ +} cmox_info_st; + +/** + * @brief Get library information + * @param pInfo Library information + * @retval None + */ +void cmox_getInfos(cmox_info_st *pInfo); + +/** + * @} + */ +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_INFO_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_init.h b/firmware/ui/dependencies/cmox/include/cmox_init.h new file mode 100644 index 00000000..da9fce99 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_init.h @@ -0,0 +1,114 @@ +/** + ****************************************************************************** + * @file cmox_init.h + * @author MCD Application Team + * @brief This file exports symbols needed to use init module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_INIT_H +#define CMOX_INIT_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + + +/** @addtogroup CMOX_CRYPTO + * @{ + */ + +/** @addtogroup CMOX_INIT Initialization module + * @{ + */ + +/** + * @brief Initialization target type + */ +typedef uint32_t cmox_init_target_t; + +#define CMOX_INIT_TARGET_AUTO ((cmox_init_target_t)0x00000000) /*!< Let the cryptographic library auto-detect running STM32 target Series */ +#define CMOX_INIT_TARGET_F0 ((cmox_init_target_t)0x46300000) /*!< Select a STM32F0 Series target */ +#define CMOX_INIT_TARGET_F1 ((cmox_init_target_t)0x46310000) /*!< Select a STM32F1 Series target */ +#define CMOX_INIT_TARGET_F2 ((cmox_init_target_t)0x46320000) /*!< Select a STM32F2 Series target */ +#define CMOX_INIT_TARGET_F3 ((cmox_init_target_t)0x46330000) /*!< Select a STM32F3 Series target */ +#define CMOX_INIT_TARGET_F4 ((cmox_init_target_t)0x46340000) /*!< Select a STM32F4 Series target */ +#define CMOX_INIT_TARGET_F7 ((cmox_init_target_t)0x46370000) /*!< Select a STM32F7 Series target */ +#define CMOX_INIT_TARGET_H5 ((cmox_init_target_t)0x48350000) /*!< Select a STM32H5 Series target */ +#define CMOX_INIT_TARGET_H7 ((cmox_init_target_t)0x48370000) /*!< Select a STM32H72x/STM32H73x/STM32H74x/STM32H75x Series target */ +#define CMOX_INIT_TARGET_H7AB ((cmox_init_target_t)0x48378000) /*!< Select a STM32H7Ax/STM32H7Bx Series target */ +#define CMOX_INIT_TARGET_H7RS ((cmox_init_target_t)0x48370000) /*!< Select a STM32H7Rx/STM32H7Sx Series target */ +#define CMOX_INIT_TARGET_G0 ((cmox_init_target_t)0x47300000) /*!< Select a STM32G0 Series target */ +#define CMOX_INIT_TARGET_G4 ((cmox_init_target_t)0x47340000) /*!< Select a STM32G4 Series target */ +#define CMOX_INIT_TARGET_L0 ((cmox_init_target_t)0x4C300000) /*!< Select a STM32L0 Series target */ +#define CMOX_INIT_TARGET_L1 ((cmox_init_target_t)0x4C310000) /*!< Select a STM32L1 Series target */ +#define CMOX_INIT_TARGET_L4 ((cmox_init_target_t)0x4C340000) /*!< Select a STM32L4/4+ Series target */ +#define CMOX_INIT_TARGET_L5 ((cmox_init_target_t)0x4C350000) /*!< Select a STM32L5 Series target */ +#define CMOX_INIT_TARGET_U0 ((cmox_init_target_t)0x55300000) /*!< Select a STM32U0 Series target */ +#define CMOX_INIT_TARGET_WB ((cmox_init_target_t)0x57420000) /*!< Select a STM32WB Series target */ +#define CMOX_INIT_TARGET_WBA ((cmox_init_target_t)0x57424100) /*!< Select a STM32WBA Series target */ +#define CMOX_INIT_TARGET_WL ((cmox_init_target_t)0x574C0000) /*!< Select a STM32WL Series target */ + +/** @brief Initialization structure + */ +typedef struct +{ + cmox_init_target_t target; /*!< User target specification */ + void *pArg; /*!< User defined parameter that is transmitted to Low Level services */ +} cmox_init_arg_t; + +/** + * @brief Return value type for Initialization module + */ +typedef uint32_t cmox_init_retval_t; /*!< Initialization return value type */ + +#define CMOX_INIT_SUCCESS ((cmox_init_retval_t)0x00000000) /*!< Init operation successfully performed */ +#define CMOX_INIT_FAIL ((cmox_init_retval_t)0x00000001) /*!< Init operation failed */ + +/** + * @brief Initialize CMOX library + * @param pInitArg Initialization parameter see @ref cmox_init_arg_t + * @note pInitArg can be set to NULL: is equivalent to + * pInitArg->target = CMOX_INIT_TARGET_AUTO + * pInitArg->pArg = NULL + * @retval Initialization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL + */ +cmox_init_retval_t cmox_initialize(cmox_init_arg_t *pInitArg); + +/** + * @brief Finalize CMOX library + * @param pArg User defined parameter that is transmitted to Low Level services + * @retval Finalization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL + */ +cmox_init_retval_t cmox_finalize(void *pArg); + +/** + * @} + */ +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_INIT_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/cmox_low_level.h b/firmware/ui/dependencies/cmox/include/cmox_low_level.h new file mode 100644 index 00000000..4a689317 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_low_level.h @@ -0,0 +1,66 @@ +/** + ****************************************************************************** + * @file cmox_low_level.h + * @brief This file exports symbols needed to use low level module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_LOW_LEVEL_H +#define CMOX_LOW_LEVEL_H + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + + +/** @addtogroup CMOX_CRYPTO + * @{ + */ + +/** @addtogroup CMOX_INIT Initialization module + * @{ + */ + +/** + * @brief CMOX library low level initialization + * @note Implements low level initialization required by cryptographic + * library to operate properly + * @param pArg User defined parameter that is transmitted from Initialize service + * @retval Initialization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL + */ +cmox_init_retval_t cmox_ll_init(void *pArg); + +/** + * @brief CMOX library low level de-initialization + * @param pArg User defined parameter that is transmitted from Finalize service + * @retval De-initialization status: @ref CMOX_INIT_SUCCESS / @ref CMOX_INIT_FAIL + */ +cmox_init_retval_t cmox_ll_deInit(void *pArg); + +/** + * @} + */ +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_LOW_LEVEL_H */ diff --git a/firmware/ui/dependencies/cmox/include/cmox_small_config.h b/firmware/ui/dependencies/cmox/include/cmox_small_config.h new file mode 100644 index 00000000..f8a1c0ab --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/cmox_small_config.h @@ -0,0 +1,360 @@ +/** + ****************************************************************************** + * @file cmox_small_config.h + * @author MCD Application Team + * @brief Header file for smallest configuration of some algorithms + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SMALL_CONFIG_H +#define CMOX_SMALL_CONFIG_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_CRYPTO + * @{ + */ + +/** + * @defgroup CMOX_SMALL CMOX small configurations + * @{ + */ + +/** + * @defgroup CMOX_AES_SMALL Small AES configuration + * @{ + */ + +/** + * @brief Flag indicating the fast implementation of AES + */ +#define CMOX_AES_FAST 0x01U + +/** + * @brief Flag indicating the small implementation of AES + */ +#define CMOX_AES_SMALL 0x02U + +/** + * @brief Flag indicating the small implementation of AES + * @note Value can be + * - @ref CMOX_AES_SMALL + * - @ref CMOX_AES_FAST + */ +#define CMOX_AES_IMPLEMENTATION CMOX_AES_SMALL + +/** + * @} + */ + +/** + * @defgroup CMOX_GCM_SMALL Small GCM configuration + * @{ + */ + +/** + * @brief Flag indicating the fast implementation of GCM + */ +#define CMOX_GCM_FAST 0x0AU + +/** + * @brief Flag indicating the small implementation of GCM + */ +#define CMOX_GCM_SMALL 0x0BU + +/** + * @brief Flag indicating the small implementation of GCM + * @note Value can be + * - @ref CMOX_GCM_SMALL + * - @ref CMOX_GCM_FAST + */ +#define CMOX_GCM_IMPLEMENTATION CMOX_GCM_SMALL + +/** + * @} + */ + +/** + * @defgroup CMOX_MATH_SMALL Small Math customizations + * @{ + */ + +/** + * @brief Flag indicating the small implementation for RSA low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + */ +#define CMOX_RSA_MATH_FUNCS CMOX_MATH_FUNCS_SMALL + +/** + * @brief Flag indicating the small implementation for ECC 256 bits curves + * low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + * - @ref CMOX_MATH_FUNCS_SUPERFAST256 + */ +#define CMOX_ECC256_MATH_FUNCS CMOX_MATH_FUNCS_SMALL + +/** + * @brief Flag indicating the small implementation for ECC 128 bits multiple curves + * low level mathematical functions + * @note Value can be + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + */ +#define CMOX_ECC128MULT_MATH_FUNCS CMOX_MATH_FUNCS_SMALL + +/** + * @brief Flag indicating the small implementation for ECC curves not defined by the above + * low level mathematical functions + * @note Value can be + * * for Cortex-M0/Cortex-M0+ devices + * - @ref CMOX_MATH_FUNCS_SMALL + * - @ref CMOX_MATH_FUNCS_FAST + * * Others + * - @ref CMOX_MATH_FUNCS_SMALL + */ +#if defined(__TARGET_ARCH_6M) +#define CMOX_ECC_MATH_FUNCS CMOX_MATH_FUNCS_SMALL +#else /* __TARGET_ARCH_6M */ +#define CMOX_ECC_MATH_FUNCS CMOX_MATH_FUNCS_SMALL +#endif /* __TARGET_ARCH_6M */ + +/** + * @} + */ + +/** + * @defgroup CMOX_ECC_SMALL Small ECC customizations + * @{ + */ + +/** + * @brief Flag indicating the small implementation for EDWARDS Ed25519 ECC curves + * @note Value can be + * - @ref CMOX_ECC_ED25519_OPT_LOWMEM + * - @ref CMOX_ECC_ED25519_HIGHMEM + * - @ref CMOX_ECC_ED25519_OPT_HIGHMEM + */ +#define CMOX_ECC_CURVE_ED25519 CMOX_ECC_ED25519_OPT_LOWMEM +/** + * @brief Flag indicating the small implementation for EDWARDS Ed448 ECC curves + * @note Value can be + * - @ref CMOX_ECC_ED448_LOWMEM + * - @ref CMOX_ECC_ED448_HIGHMEM + */ +#define CMOX_ECC_CURVE_ED448 CMOX_ECC_ED448_LOWMEM + +/** + * @brief Flag indicating the small implementation for NIST-R P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP224R1_LOWMEM + * - @ref CMOX_ECC_SECP224R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP224R1 CMOX_ECC_SECP224R1_LOWMEM +/** + * @brief Flag indicating the small implementation for NIST-R P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP256R1_LOWMEM + * - @ref CMOX_ECC_SECP256R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP256R1 CMOX_ECC_SECP256R1_LOWMEM +/** + * @brief Flag indicating the small implementation for NIST-R P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP384R1_LOWMEM + * - @ref CMOX_ECC_SECP384R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP384R1 CMOX_ECC_SECP384R1_LOWMEM +/** + * @brief Flag indicating the small implementation for NIST-R P-521 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP521R1_LOWMEM + * - @ref CMOX_ECC_SECP521R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP521R1 CMOX_ECC_SECP521R1_LOWMEM +/** + * @brief Flag indicating the small implementation for NIST-K P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_SECP256K1_LOWMEM + * - @ref CMOX_ECC_SECP256K1_HIGHMEM + */ +#define CMOX_ECC_CURVE_SECP256K1 CMOX_ECC_SECP256K1_LOWMEM + +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-160 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP160R1_LOWMEM + * - @ref CMOX_ECC_BPP160R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP160R1 CMOX_ECC_BPP160R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-192 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP192R1_LOWMEM + * - @ref CMOX_ECC_BPP192R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP192R1 CMOX_ECC_BPP192R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP224R1_LOWMEM + * - @ref CMOX_ECC_BPP224R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP224R1 CMOX_ECC_BPP224R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP256R1_LOWMEM + * - @ref CMOX_ECC_BPP256R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP256R1 CMOX_ECC_BPP256R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-320 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP320R1_LOWMEM + * - @ref CMOX_ECC_BPP320R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP320R1 CMOX_ECC_BPP320R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP384R1_LOWMEM + * - @ref CMOX_ECC_BPP384R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP384R1 CMOX_ECC_BPP384R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-R P-512 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP512R1_LOWMEM + * - @ref CMOX_ECC_BPP512R1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP512R1 CMOX_ECC_BPP512R1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-160 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP160T1_LOWMEM + * - @ref CMOX_ECC_BPP160T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP160T1 CMOX_ECC_BPP160T1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-192 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP192T1_LOWMEM + * - @ref CMOX_ECC_BPP192T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP192T1 CMOX_ECC_BPP192T1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-224 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP224T1_LOWMEM + * - @ref CMOX_ECC_BPP224T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP224T1 CMOX_ECC_BPP224T1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP256T1_LOWMEM + * - @ref CMOX_ECC_BPP256T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP256T1 CMOX_ECC_BPP256T1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-320 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP320T1_LOWMEM + * - @ref CMOX_ECC_BPP320T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP320T1 CMOX_ECC_BPP320T1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-384 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP384T1_LOWMEM + * - @ref CMOX_ECC_BPP384T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP384T1 CMOX_ECC_BPP384T1_LOWMEM +/** + * @brief Flag indicating the small implementation for BRAINPOOL-T P-512 ECC curves + * @note Value can be + * - @ref CMOX_ECC_BPP512T1_LOWMEM + * - @ref CMOX_ECC_BPP512T1_HIGHMEM + */ +#define CMOX_ECC_CURVE_BPP512T1 CMOX_ECC_BPP512T1_LOWMEM + +/** + * @brief Flag indicating the small implementation for ANSSI P-256 ECC curves + * @note Value can be + * - @ref CMOX_ECC_FRP256V1_LOWMEM + * - @ref CMOX_ECC_FRP256V1_HIGHMEM + */ +#define CMOX_ECC_CURVE_FRP256V1 CMOX_ECC_FRP256V1_LOWMEM + +/** + * @brief Flag indicating the small implementation for OSCCA 256 bit ECC curves + * @note Value can be + * - @ref CMOX_ECC_SM2_LOWMEM + * - @ref CMOX_ECC_SM2_HIGHMEM + */ +#define CMOX_ECC_CURVE_SM2 CMOX_ECC_SM2_LOWMEM + +/** + * @brief Flag indicating the small implementation for OSCCA 256 bit test ECC curves + * @note Value can be + * - @ref CMOX_ECC_SM2TEST_LOWMEM + * - @ref CMOX_ECC_SM2TEST_HIGHMEM + */ +#define CMOX_ECC_CURVE_SM2TEST CMOX_ECC_SM2TEST_LOWMEM + +/** + * @} + */ + +/** + * @defgroup CMOX_RSA_SMALL Small RSA customizations + * @{ + */ + +/** + * @brief Flag indicating the small private modular exponentiation implementation + * @note Value can be + * - @ref CMOX_MODEXP_PRIVATE_LOWMEM + * - @ref CMOX_MODEXP_PRIVATE_MIDMEM + * - @ref CMOX_MODEXP_PRIVATE_HIGHMEM + */ +#define CMOX_MODEXP_PRIVATE CMOX_MODEXP_PRIVATE_LOWMEM + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SMALL_CONFIG_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/drbg/cmox_ctr_drbg.h b/firmware/ui/dependencies/cmox/include/drbg/cmox_ctr_drbg.h new file mode 100644 index 00000000..85b3d65c --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/drbg/cmox_ctr_drbg.h @@ -0,0 +1,180 @@ +/** + ****************************************************************************** + * @file cmox_ctr_drbg.h + * @author MCD Application Team + * @brief Header file for the CTR-DRBG module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CTR_DRBG_H +#define CMOX_CTR_DRBG_H + +/* Include files -------------------------------------------------------------*/ +#include "cmox_drbg.h" +#include "cipher/cmox_blockcipher.h" + +#include "cipher/cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_DRBG + * @{ + */ + +/** @defgroup CMOX_CTR_DRBG CTR-DRBG + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CTR_DRBG_PUBLIC_TYPES CTR-DRBG public types + * @{ + */ + +/** + * @brief CTR-DRBG mode implementation + * + * This type specifies the used block cipher for the CTR-DRBG construct. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_ctr_drbg_implStruct_st *cmox_ctr_drbg_impl_t; + +/** + * @brief Structure to store the state/context of the CTR_DRBG + */ +typedef struct +{ + uint32_t value[4]; /*!< V value, a 128 bit value */ + uint32_t key[8]; /*!< K value, contains the block cipher key */ + uint64_t reseed_counter; /*!< Reseed counter 32-bit of data */ +} cmox_ctr_drbg_state_t; + +/** + * @brief CTR-DRBG handle structure definition + */ +typedef struct +{ + cmox_drbg_handle_t super; /*!< General DRBG handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + cmox_blockcipher_handle_t blockCipher_df; /*!< Block cipher handle for derivation function */ + cmox_ctr_drbg_state_t state; /*!< DRBG state */ + uint32_t flag; /*!< DRBG flag */ + cmox_cipher_keyLen_t keyLen; /*!< Length in bytes of the block cipher key */ + size_t expKeyLen; /*!< Length in bytes of the expanded key */ + size_t minEntropyLen; /*!< Minimum entropy length */ + size_t maxBytesPerRequest; /*!< Maximum number of random bytes per request */ + const uint32_t *exp_aes_0_key; /*!< Pointer to the expanded key */ + const uint32_t *exp_bcc_aes_key; /*!< Pointer to the expanded key for BCC */ +} cmox_ctr_drbg_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CTR_DRBG_PUBLIC_CONSTANTS CTR-DRBG public constants + * @{ + */ + +/** @defgroup CMOX_CTR_DRBG_IMPL CTR-DRBG implementations + * @{ + */ + +/** + * @brief Implementation of CTR-DRBG using AES-128 (small implementation) + * (Defined internally) + */ +extern const cmox_ctr_drbg_impl_t CMOX_CTR_DRBG_AES128_SMALL; + +/** + * @brief Implementation of CTR-DRBG using AES-128 (fast implementation) + * (Defined internally) + */ +extern const cmox_ctr_drbg_impl_t CMOX_CTR_DRBG_AES128_FAST; + +/** + * @brief Implementation of CTR-DRBG using AES-256 (small implementation) + * (Defined internally) + */ +extern const cmox_ctr_drbg_impl_t CMOX_CTR_DRBG_AES256_SMALL; + +/** + * @brief Implementation of CTR-DRBG using AES-256 (fast implementation) + * (Defined internally) + */ +extern const cmox_ctr_drbg_impl_t CMOX_CTR_DRBG_AES256_FAST; + +/** + * @} + */ + +/** + * @} + */ + +/* Public method prototypes --------------------------------------------------*/ + +/** @defgroup CMOX_CTR_DRBG_PUBLIC_METHODS CTR-DRBG public method prototypes + * @{ + */ + +/** + * @brief CTR-DRBG constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the CTR-DRBG algorithm + * @param P_pThis Pointer to the CTR-DRBG handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_CTR_DRBG_AES128_FAST + * @arg CMOX_CTR_DRBG_AES192_FAST + * @arg CMOX_CTR_DRBG_AES256_FAST + * @arg CMOX_CTR_DRBG_AES128_SMALL + * @arg CMOX_CTR_DRBG_AES192_SMALL + * @arg CMOX_CTR_DRBG_AES256_SMALL + * @return cmox_drbg_handle_t* Pointer to a general DRBG handle. This will + * be used by the general purpose cipher functions in order to + * perform the algorithm + */ +cmox_drbg_handle_t *cmox_ctr_drbg_construct(cmox_ctr_drbg_handle_t *P_pThis, + cmox_ctr_drbg_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CTR_DRBG_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/drbg/cmox_drbg.h b/firmware/ui/dependencies/cmox/include/drbg/cmox_drbg.h new file mode 100644 index 00000000..007d96c9 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/drbg/cmox_drbg.h @@ -0,0 +1,147 @@ +/** + ****************************************************************************** + * @file cmox_drbg.h + * @author MCD Application Team + * @brief Header file for the DRBG module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_DRBG_H +#define CMOX_DRBG_H + +/* Include files -------------------------------------------------------------*/ +#include +#include +#include "cmox_drbg_retvals.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_DRBG + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_DRBG_PUBLIC_TYPES DRBG module public types + * @{ + */ + +/** + * @brief DRBG Virtual Table + * + * This type specifies a pointer to the virtual table containing the methods + * for a particular algorithm (currently the only supported is CTR-DRBG) + */ +typedef const struct cmox_drbg_vtableStruct_st *cmox_drbg_vtable_t; + +/** + * @brief DRBG handle structure definition + */ +typedef struct +{ + cmox_drbg_vtable_t table; /*!< DRBG virtual table */ +} cmox_drbg_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_DRBG_PUBLIC_METHODS DRBG public method prototypes + * @{ + */ + +/** + * @brief Cleanup the DRBG handle + * + * @param P_pThis DRBG handle to cleanup + * @return cmox_drbg_retval_t DRBG return value + */ +cmox_drbg_retval_t cmox_drbg_cleanup(cmox_drbg_handle_t *P_pThis); + +/** + * @brief Initialize the DRBG engine + * + * @param P_pThis DRBG handler to initialize + * @param P_pEntropy Buffer of bytes containing the entropy bytes + * @param P_entropyLen Number of entropy bytes that will be used to seed the DRBG + * @param P_pPersonalString Buffer of bytes containing the personalization string + * @param P_personalStringLen Size in bytes of the personalization string + * @param P_pNonce Buffer of bytes containing nonce data + * @param P_nonceLen Size in bytes of the nonce + * @return cmox_drbg_retval_t DRBG return value + */ +cmox_drbg_retval_t cmox_drbg_init(cmox_drbg_handle_t *P_pThis, + const uint8_t *P_pEntropy, + size_t P_entropyLen, + const uint8_t *P_pPersonalString, + size_t P_personalStringLen, + const uint8_t *P_pNonce, + size_t P_nonceLen); + +/** + * @brief Reseed the DRBG + * + * @param P_pThis DRBG handler to initialize + * @param P_pEntropy Buffer of bytes containing the entropy bytes + * @param P_entropyLen Number of entropy bytes that will be used to seed the DRBG + * @param P_pAdditionalInput Buffer of bytes containing additional input + * @param P_additionalInputLen Size in bytes of the additional input + * @return cmox_drbg_retval_t DRBG return value + */ +cmox_drbg_retval_t cmox_drbg_reseed(cmox_drbg_handle_t *P_pThis, + const uint8_t *P_pEntropy, + size_t P_entropyLen, + const uint8_t *P_pAdditionalInput, + size_t P_additionalInputLen); + +/** + * @brief Generation of pseudorandom octets to a buffer + * + * @param P_pThis DRBG handler to initialize + * @param P_pAdditionalInput Buffer of bytes containing additional input + * @param P_additionalInputLen Size in bytes of the additional input + * @param P_pOutput Buffer of bytes where the generated bytes will be stored + * @param P_desiredOutputLen Desired number of random bytes to produce + * @return cmox_drbg_retval_t DRBG return value + */ +cmox_drbg_retval_t cmox_drbg_generate(cmox_drbg_handle_t *P_pThis, + const uint8_t *P_pAdditionalInput, + size_t P_additionalInputLen, + uint8_t *P_pOutput, + size_t P_desiredOutputLen); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_DRBG_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/drbg/cmox_drbg_retvals.h b/firmware/ui/dependencies/cmox/include/drbg/cmox_drbg_retvals.h new file mode 100644 index 00000000..16f5d6c0 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/drbg/cmox_drbg_retvals.h @@ -0,0 +1,119 @@ +/** + ****************************************************************************** + * @file cmox_drbg_retvals.h + * @author MCD Application Team + * @brief Header file for the DRBG return values + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_DRBG_RETVALS_H +#define CMOX_DRBG_RETVALS_H + +/* Include files -------------------------------------------------------------*/ +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_DRBG + * @{ + */ + +/** @defgroup CMOX_DRBG_RETVALS DRBG return values + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** + * @brief DRBG operation successfully performed + */ +#define CMOX_DRBG_SUCCESS ((cmox_drbg_retval_t)0x00040000U) + +/** + * @brief DRBG generic internal error + */ +#define CMOX_DRBG_ERR_INTERNAL ((cmox_drbg_retval_t)0x00040001U) + +/** + * @brief DRBG One of the expected function parameters is invalid + */ +#define CMOX_DRBG_ERR_BAD_PARAMETER ((cmox_drbg_retval_t)0x00040003U) + +/** + * @brief DRBG Invalid operation + */ +#define CMOX_DRBG_ERR_BAD_OPERATION ((cmox_drbg_retval_t)0x00040004U) + +/** + * @brief DRBG has not been correctly initialized + */ +#define CMOX_DRBG_ERR_UNINIT_STATE ((cmox_drbg_retval_t)0x0004000DU) + +/** + * @brief DRBG Reseed is needed + */ +#define CMOX_DRBG_ERR_RESEED_NEEDED ((cmox_drbg_retval_t)0x0004000EU) + +/** + * @brief DRBG Check the size of the entropy string + */ +#define CMOX_DRBG_ERR_BAD_ENTROPY_SIZE ((cmox_drbg_retval_t)0x0004000FU) + +/** + * @brief DRBG Check the size of the personalization string + */ +#define CMOX_DRBG_ERR_BAD_PERS_STR_LEN ((cmox_drbg_retval_t)0x00040010U) + +/** + * @brief DRBG Check the size of the additional input string + */ +#define CMOX_DRBG_ERR_BAD_ADD_INPUT_LEN ((cmox_drbg_retval_t)0x00040011U) + +/** + * @brief DRBG Check the size of the random request + */ +#define CMOX_DRBG_ERR_BAD_REQUEST ((cmox_drbg_retval_t)0x00040012U) + +/** + * @brief DRBG Check the size of the nonce + */ +#define CMOX_DRBG_ERR_BAD_NONCE_SIZE ((cmox_drbg_retval_t)0x00040013U) + +/* Public types --------------------------------------------------------------*/ + +/** + * @brief DRBG module return value type + */ +typedef uint32_t cmox_drbg_retval_t; + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_DRBG_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc.h new file mode 100644 index 00000000..d4f375bb --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc.h @@ -0,0 +1,259 @@ +/** + ****************************************************************************** + * @file cmox_ecc.h + * @author MCD Application Team + * @brief This file provides common function for ECC module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_ECC_H +#define CMOX_ECC_H + +#include +#include + +#include "ecc/cmox_ecc_retvals.h" +#include "ecc/cmox_ecc_types.h" +#if !defined(CMOX_DEFAULT_FILE) +#include "cmox_default_config.h" +#else +#include CMOX_DEFAULT_FILE +#endif /* CMOX_DEFAULT_FILE */ + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_CURVES Supported curves implementations + * @{ + */ +extern const cmox_ecc_impl_t CMOX_ECC_CURVE25519; /*!< Suggested Curve25519 implementation (for X25519) for the target device */ +extern const cmox_ecc_impl_t CMOX_ECC_CURVE448; /*!< Suggested Curve448 implementation (for X448) for the target device */ + +extern const cmox_ecc_impl_t CMOX_ECC_ED25519_HIGHMEM; /*!< EDWARDS Ed25519 with general Edwards functions, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_ED25519_OPT_LOWMEM; /*!< EDWARDS Ed25519 with Edwards functions optimized for a = -1, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_ED25519_OPT_HIGHMEM; /*!< EDWARDS Ed25519 with Edwards functions optimized for a = -1, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_ED448_LOWMEM; /*!< EDWARDS Ed448 with general Edwards functions, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_ED448_HIGHMEM; /*!< EDWARDS Ed448 with general Edwards functions, high RAM usage */ + +extern const cmox_ecc_impl_t CMOX_ECC_SECP224R1_LOWMEM; /*!< NIST-R P-224, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP224R1_HIGHMEM; /*!< NIST-R P-224, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP256R1_LOWMEM; /*!< NIST-R P-256, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP256R1_HIGHMEM; /*!< NIST-R P-256, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP384R1_LOWMEM; /*!< NIST-R P-384, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP384R1_HIGHMEM; /*!< NIST-R P-384, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP521R1_LOWMEM; /*!< NIST-R P-521, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP521R1_HIGHMEM; /*!< NIST-R P-521, high RAM usage */ + +extern const cmox_ecc_impl_t CMOX_ECC_SECP256K1_LOWMEM; /*!< NIST-K P-256, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SECP256K1_HIGHMEM; /*!< NIST-K P-256, high RAM usage */ + +extern const cmox_ecc_impl_t CMOX_ECC_BPP160R1_LOWMEM; /*!< BRAINPOOL-R P-160, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP160R1_HIGHMEM; /*!< BRAINPOOL-R P-160, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP192R1_LOWMEM; /*!< BRAINPOOL-R P-192, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP192R1_HIGHMEM; /*!< BRAINPOOL-R P-192, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP224R1_LOWMEM; /*!< BRAINPOOL-R P-224, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP224R1_HIGHMEM; /*!< BRAINPOOL-R P-224, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP256R1_LOWMEM; /*!< BRAINPOOL-R P-256, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP256R1_HIGHMEM; /*!< BRAINPOOL-R P-256, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP320R1_LOWMEM; /*!< BRAINPOOL-R P-320, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP320R1_HIGHMEM; /*!< BRAINPOOL-R P-320, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP384R1_LOWMEM; /*!< BRAINPOOL-R P-384, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP384R1_HIGHMEM; /*!< BRAINPOOL-R P-384, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP512R1_LOWMEM; /*!< BRAINPOOL-R P-512, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP512R1_HIGHMEM; /*!< BRAINPOOL-R P-512, high RAM usage */ + +extern const cmox_ecc_impl_t CMOX_ECC_BPP160T1_LOWMEM; /*!< BRAINPOOL-T P-160, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP160T1_HIGHMEM; /*!< BRAINPOOL-T P-160, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP192T1_LOWMEM; /*!< BRAINPOOL-T P-192, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP192T1_HIGHMEM; /*!< BRAINPOOL-T P-192, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP224T1_LOWMEM; /*!< BRAINPOOL-T P-224, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP224T1_HIGHMEM; /*!< BRAINPOOL-T P-224, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP256T1_LOWMEM; /*!< BRAINPOOL-T P-256, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP256T1_HIGHMEM; /*!< BRAINPOOL-T P-256, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP320T1_LOWMEM; /*!< BRAINPOOL-T P-320, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP320T1_HIGHMEM; /*!< BRAINPOOL-T P-320, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP384T1_LOWMEM; /*!< BRAINPOOL-T P-384, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP384T1_HIGHMEM; /*!< BRAINPOOL-T P-384, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP512T1_LOWMEM; /*!< BRAINPOOL-T P-512, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_BPP512T1_HIGHMEM; /*!< BRAINPOOL-T P-512, high RAM usage */ + +extern const cmox_ecc_impl_t CMOX_ECC_FRP256V1_LOWMEM; /*!< ANSSI P-256, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_FRP256V1_HIGHMEM; /*!< ANSSI P-256, high RAM usage */ + +extern const cmox_ecc_impl_t CMOX_ECC_SM2_LOWMEM; /*!< OSCCA 256 bit curve, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SM2_HIGHMEM; /*!< OSCCA 256 bit curve, high RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SM2TEST_LOWMEM; /*!< OSCCA 256 bit test curve, low RAM usage */ +extern const cmox_ecc_impl_t CMOX_ECC_SM2TEST_HIGHMEM; /*!< OSCCA 256 bit test curve, high RAM usage */ +/** + * @} + */ + +/** + * @defgroup CMOX_ECC_OUTPUT_LENGTHS Macros for output buffers definitions + * @{ + */ +#define CMOX_ECC_CURVE25519_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a CURVE25519 private key */ +#define CMOX_ECC_CURVE25519_PUBKEY_LEN 32u /*!< Byte length for a byte buffer containing a CURVE25519 public key */ +#define CMOX_ECC_CURVE25519_SECRET_LEN 32u /*!< Byte length for a byte buffer containing a CURVE25519 ECDH secret */ +#define CMOX_ECC_CURVE448_PRIVKEY_LEN 56u /*!< Byte length for a byte buffer containing a CURVE448 private key */ +#define CMOX_ECC_CURVE448_PUBKEY_LEN 56u /*!< Byte length for a byte buffer containing a CURVE448 public key */ +#define CMOX_ECC_CURVE448_SECRET_LEN 56u /*!< Byte length for a byte buffer containing a CURVE448 ECDH secret */ +#define CMOX_ECC_ED25519_SIG_LEN 64u /*!< Byte length for a byte buffer containing a ED25519 signature */ +#define CMOX_ECC_ED25519_PRIVKEY_LEN 64u /*!< Byte length for a byte buffer containing a ED25519 private key */ +#define CMOX_ECC_ED25519_PUBKEY_LEN 32u /*!< Byte length for a byte buffer containing a ED25519 public key */ +#define CMOX_ECC_ED448_SIG_LEN 114u /*!< Byte length for a byte buffer containing a ED448 signature */ +#define CMOX_ECC_ED448_PRIVKEY_LEN 114u /*!< Byte length for a byte buffer containing a ED448 private key */ +#define CMOX_ECC_ED448_PUBKEY_LEN 57u /*!< Byte length for a byte buffer containing a ED448 public key */ +#define CMOX_ECC_SECP224R1_SIG_LEN 56u /*!< Byte length for a byte buffer containing a SECP224R1 signature */ +#define CMOX_ECC_SECP224R1_PRIVKEY_LEN 28u /*!< Byte length for a byte buffer containing a SECP224R1 private key */ +#define CMOX_ECC_SECP224R1_PUBKEY_LEN 56u /*!< Byte length for a byte buffer containing a SECP224R1 public key */ +#define CMOX_ECC_SECP224R1_SECRET_LEN 56u /*!< Byte length for a byte buffer containing a SECP224R1 ECDH secret */ +#define CMOX_ECC_SECP256R1_SIG_LEN 64u /*!< Byte length for a byte buffer containing a SECP256R1 signature */ +#define CMOX_ECC_SECP256R1_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a SECP256R1 private key */ +#define CMOX_ECC_SECP256R1_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a SECP256R1 public key */ +#define CMOX_ECC_SECP256R1_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a SECP256R1 ECDH secret */ +#define CMOX_ECC_SECP384R1_SIG_LEN 96u /*!< Byte length for a byte buffer containing a SECP384R1 signature */ +#define CMOX_ECC_SECP384R1_PRIVKEY_LEN 48u /*!< Byte length for a byte buffer containing a SECP384R1 private key */ +#define CMOX_ECC_SECP384R1_PUBKEY_LEN 96u /*!< Byte length for a byte buffer containing a SECP384R1 public key */ +#define CMOX_ECC_SECP384R1_SECRET_LEN 96u /*!< Byte length for a byte buffer containing a SECP384R1 ECDH secret */ +#define CMOX_ECC_SECP521R1_SIG_LEN 132u /*!< Byte length for a byte buffer containing a SECP521R1 signature */ +#define CMOX_ECC_SECP521R1_PRIVKEY_LEN 66u /*!< Byte length for a byte buffer containing a SECP521R1 private key */ +#define CMOX_ECC_SECP521R1_PUBKEY_LEN 132u /*!< Byte length for a byte buffer containing a SECP521R1 public key */ +#define CMOX_ECC_SECP521R1_SECRET_LEN 132u /*!< Byte length for a byte buffer containing a SECP521R1 ECDH secret */ +#define CMOX_ECC_SECP256K1_SIG_LEN 64u /*!< Byte length for a byte buffer containing a SECP256K1 signature */ +#define CMOX_ECC_SECP256K1_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a SECP256K1 private key */ +#define CMOX_ECC_SECP256K1_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a SECP256K1 public key */ +#define CMOX_ECC_SECP256K1_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a SECP256K1 ECDH secret */ +#define CMOX_ECC_BPP160R1_SIG_LEN 40u /*!< Byte length for a byte buffer containing a BPP160R1 signature */ +#define CMOX_ECC_BPP160R1_PRIVKEY_LEN 20u /*!< Byte length for a byte buffer containing a BPP160R1 private key */ +#define CMOX_ECC_BPP160R1_PUBKEY_LEN 40u /*!< Byte length for a byte buffer containing a BPP160R1 public key */ +#define CMOX_ECC_BPP160R1_SECRET_LEN 40u /*!< Byte length for a byte buffer containing a BPP160R1 ECDH secret */ +#define CMOX_ECC_BPP192R1_SIG_LEN 48u /*!< Byte length for a byte buffer containing a BPP192R1 signature */ +#define CMOX_ECC_BPP192R1_PRIVKEY_LEN 24u /*!< Byte length for a byte buffer containing a BPP192R1 private key */ +#define CMOX_ECC_BPP192R1_PUBKEY_LEN 48u /*!< Byte length for a byte buffer containing a BPP192R1 public key */ +#define CMOX_ECC_BPP192R1_SECRET_LEN 48u /*!< Byte length for a byte buffer containing a BPP192R1 ECDH secret */ +#define CMOX_ECC_BPP224R1_SIG_LEN 56u /*!< Byte length for a byte buffer containing a BPP224R1 signature */ +#define CMOX_ECC_BPP224R1_PRIVKEY_LEN 28u /*!< Byte length for a byte buffer containing a BPP224R1 private key */ +#define CMOX_ECC_BPP224R1_PUBKEY_LEN 56u /*!< Byte length for a byte buffer containing a BPP224R1 public key */ +#define CMOX_ECC_BPP224R1_SECRET_LEN 56u /*!< Byte length for a byte buffer containing a BPP224R1 ECDH secret */ +#define CMOX_ECC_BPP256R1_SIG_LEN 64u /*!< Byte length for a byte buffer containing a BPP256R1 signature */ +#define CMOX_ECC_BPP256R1_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a BPP256R1 private key */ +#define CMOX_ECC_BPP256R1_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a BPP256R1 public key */ +#define CMOX_ECC_BPP256R1_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a BPP256R1 ECDH secret */ +#define CMOX_ECC_BPP320R1_SIG_LEN 80u /*!< Byte length for a byte buffer containing a BPP320R1 signature */ +#define CMOX_ECC_BPP320R1_PRIVKEY_LEN 40u /*!< Byte length for a byte buffer containing a BPP320R1 private key */ +#define CMOX_ECC_BPP320R1_PUBKEY_LEN 80u /*!< Byte length for a byte buffer containing a BPP320R1 public key */ +#define CMOX_ECC_BPP320R1_SECRET_LEN 80u /*!< Byte length for a byte buffer containing a BPP320R1 ECDH secret */ +#define CMOX_ECC_BPP384R1_SIG_LEN 96u /*!< Byte length for a byte buffer containing a BPP384R1 signature */ +#define CMOX_ECC_BPP384R1_PRIVKEY_LEN 48u /*!< Byte length for a byte buffer containing a BPP384R1 private key */ +#define CMOX_ECC_BPP384R1_PUBKEY_LEN 96u /*!< Byte length for a byte buffer containing a BPP384R1 public key */ +#define CMOX_ECC_BPP384R1_SECRET_LEN 96u /*!< Byte length for a byte buffer containing a BPP384R1 ECDH secret */ +#define CMOX_ECC_BPP512R1_SIG_LEN 128u /*!< Byte length for a byte buffer containing a BPP512R1 signature */ +#define CMOX_ECC_BPP512R1_PRIVKEY_LEN 64u /*!< Byte length for a byte buffer containing a BPP512R1 private key */ +#define CMOX_ECC_BPP512R1_PUBKEY_LEN 128u /*!< Byte length for a byte buffer containing a BPP512R1 public key */ +#define CMOX_ECC_BPP512R1_SECRET_LEN 128u /*!< Byte length for a byte buffer containing a BPP512R1 ECDH secret */ +#define CMOX_ECC_BPP160T1_SIG_LEN 40u /*!< Byte length for a byte buffer containing a BPP160T1 signature */ +#define CMOX_ECC_BPP160T1_PRIVKEY_LEN 20u /*!< Byte length for a byte buffer containing a BPP160T1 private key */ +#define CMOX_ECC_BPP160T1_PUBKEY_LEN 40u /*!< Byte length for a byte buffer containing a BPP160T1 public key */ +#define CMOX_ECC_BPP160T1_SECRET_LEN 40u /*!< Byte length for a byte buffer containing a BPP160T1 ECDH secret */ +#define CMOX_ECC_BPP192T1_SIG_LEN 48u /*!< Byte length for a byte buffer containing a BPP192T1 signature */ +#define CMOX_ECC_BPP192T1_PRIVKEY_LEN 24u /*!< Byte length for a byte buffer containing a BPP192T1 private key */ +#define CMOX_ECC_BPP192T1_PUBKEY_LEN 48u /*!< Byte length for a byte buffer containing a BPP192T1 public key */ +#define CMOX_ECC_BPP192T1_SECRET_LEN 48u /*!< Byte length for a byte buffer containing a BPP192T1 ECDH secret */ +#define CMOX_ECC_BPP224T1_SIG_LEN 56u /*!< Byte length for a byte buffer containing a BPP224T1 signature */ +#define CMOX_ECC_BPP224T1_PRIVKEY_LEN 28u /*!< Byte length for a byte buffer containing a BPP224T1 private key */ +#define CMOX_ECC_BPP224T1_PUBKEY_LEN 56u /*!< Byte length for a byte buffer containing a BPP224T1 public key */ +#define CMOX_ECC_BPP224T1_SECRET_LEN 56u /*!< Byte length for a byte buffer containing a BPP224T1 ECDH secret */ +#define CMOX_ECC_BPP256T1_SIG_LEN 64u /*!< Byte length for a byte buffer containing a BPP256T1 signature */ +#define CMOX_ECC_BPP256T1_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a BPP256T1 private key */ +#define CMOX_ECC_BPP256T1_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a BPP256T1 public key */ +#define CMOX_ECC_BPP256T1_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a BPP256T1 ECDH secret */ +#define CMOX_ECC_BPP320T1_SIG_LEN 80u /*!< Byte length for a byte buffer containing a BPP320T1 signature */ +#define CMOX_ECC_BPP320T1_PRIVKEY_LEN 40u /*!< Byte length for a byte buffer containing a BPP320T1 private key */ +#define CMOX_ECC_BPP320T1_PUBKEY_LEN 80u /*!< Byte length for a byte buffer containing a BPP320T1 public key */ +#define CMOX_ECC_BPP320T1_SECRET_LEN 80u /*!< Byte length for a byte buffer containing a BPP320T1 ECDH secret */ +#define CMOX_ECC_BPP384T1_SIG_LEN 96u /*!< Byte length for a byte buffer containing a BPP384T1 signature */ +#define CMOX_ECC_BPP384T1_PRIVKEY_LEN 48u /*!< Byte length for a byte buffer containing a BPP384T1 private key */ +#define CMOX_ECC_BPP384T1_PUBKEY_LEN 96u /*!< Byte length for a byte buffer containing a BPP384T1 public key */ +#define CMOX_ECC_BPP384T1_SECRET_LEN 96u /*!< Byte length for a byte buffer containing a BPP384T1 ECDH secret */ +#define CMOX_ECC_BPP512T1_SIG_LEN 128u /*!< Byte length for a byte buffer containing a BPP512T1 signature */ +#define CMOX_ECC_BPP512T1_PRIVKEY_LEN 64u /*!< Byte length for a byte buffer containing a BPP512T1 private key */ +#define CMOX_ECC_BPP512T1_PUBKEY_LEN 128u /*!< Byte length for a byte buffer containing a BPP512T1 public key */ +#define CMOX_ECC_BPP512T1_SECRET_LEN 128u /*!< Byte length for a byte buffer containing a BPP512T1 ECDH secret */ +#define CMOX_ECC_FRP256V1_SIG_LEN 64u /*!< Byte length for a byte buffer containing a FRP256V1 signature */ +#define CMOX_ECC_FRP256V1_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a FRP256V1 private key */ +#define CMOX_ECC_FRP256V1_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a FRP256V1 public key */ +#define CMOX_ECC_FRP256V1_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a FRP256V1 ECDH secret */ +#define CMOX_ECC_SM2_SIG_LEN 64u /*!< Byte length for a byte buffer containing a SM2 signature */ +#define CMOX_ECC_SM2_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a SM2 private key */ +#define CMOX_ECC_SM2_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a SM2 public key */ +#define CMOX_ECC_SM2_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a SM2 ECDH secret */ +#define CMOX_ECC_SM2TEST_SIG_LEN 64u /*!< Byte length for a byte buffer containing a SM2TEST signature */ +#define CMOX_ECC_SM2TEST_PRIVKEY_LEN 32u /*!< Byte length for a byte buffer containing a SM2TEST private key */ +#define CMOX_ECC_SM2TEST_PUBKEY_LEN 64u /*!< Byte length for a byte buffer containing a SM2TEST public key */ +#define CMOX_ECC_SM2TEST_SECRET_LEN 64u /*!< Byte length for a byte buffer containing a SM2TEST ECDH secret */ +/** + * @} + */ + +/** + * @defgroup CMOX_ECC_PUBLIC_METHODS ECC public method prototypes + * @{ + */ + +/** + * @brief Mandatory function to set Low Level Mathematical Functions, and working memory buffer, for ECC + * @param[in,out] P_pEccCtx Context for ECC operations + * @param[in] P_Math Structure pointer with the Low-level Mathematical functions + * This parameter can be one of the following: + * @arg CMOX_MATH_FUNCS_SMALL + * @arg CMOX_MATH_FUNCS_FAST + * @arg CMOX_MATH_FUNCS_SUPERFAST256 + * @param[in] P_pBuf The preallocated static buffer that will be used + * @param[in] P_BufLen The size in bytes of the P_pBuf buffer + */ +void cmox_ecc_construct(cmox_ecc_handle_t *P_pEccCtx, + const cmox_math_funcs_t P_Math, + uint8_t *P_pBuf, + size_t P_BufLen); + +/** + * @brief Clean the ECC context and the internal temporary buffer + * @param[in,out] P_pEccCtx Context for ECC operations + */ +void cmox_ecc_cleanup(cmox_ecc_handle_t *P_pEccCtx); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_ECC_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_custom_curves.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_custom_curves.h new file mode 100644 index 00000000..f0b7b6b5 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_custom_curves.h @@ -0,0 +1,126 @@ +/** + ****************************************************************************** + * @file cmox_ecc_custom_curves.h + * @author MCD Application Team + * @brief This file provides everything to define custom ECC curves + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_ECC_CUSTOM_CURVES_H +#define CMOX_ECC_CUSTOM_CURVES_H + +#include +#include "cmox_common.h" +#include "ecc/cmox_ecc_types.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_CUSTOM_CURVES_OPT speed/RAM optimizations for custom curves implementations + * @{ + */ + +struct cmox_ecc_customCurve_optStr_st; /*!< struct for speed/RAM optimizations */ +typedef const struct cmox_ecc_customCurve_optStr_st *cmox_ecc_customCurve_opt_t; /*!< struct ptr for speed/RAM opt */ + +/** + * @brief Low RAM usage (slowest) + */ +extern const cmox_ecc_customCurve_opt_t cmox_ecc_customCurve_opt_low; +/** + * @brief Mid RAM usage + */ +extern const cmox_ecc_customCurve_opt_t cmox_ecc_customCurve_opt_mid; +/** + * @brief High RAM usage (fastest) + */ +extern const cmox_ecc_customCurve_opt_t cmox_ecc_customCurve_opt_high; + +/** + * @} + */ + +/** + * @defgroup CMOX_ECC_CUSTOM_CURVES_METHODS ECC public method prototypes to define custom curves + * @{ + */ + +/** + * @brief Function to create a custom Short-Weierstrass curve + * @param[in] P_pEccCtx Context for ECC operations + * @param[out] P_pCurve Pointer to a curve that will be built and filled + * @param[in] P_OptLevel Optimization level (speed vs. RAM usage) + * This parameter can be one of the following: + * @arg cmox_ecc_customCurve_opt_low + * @arg cmox_ecc_customCurve_opt_mid + * @arg cmox_ecc_customCurve_opt_high + * @param[in] P_pP Modulus of the curve + * @param[in] P_PLen Byte length of the modulus + * @param[in] P_pN Order of the curve + * @param[in] P_NLen Byte length of the order + * @param[in] P_pA Parameter A of the curve + * @param[in] P_ALen Byte length of the parameter A + * @param[in] P_pB Parameter B of the curve + * @param[in] P_BLen Byte length of the parameter B + * @param[in] P_pGx X coordinate of the Generator Point + * @param[in] P_GxLen Byte length of the X coordinate + * @param[in] P_pGy Y coordinate of the Generator Point + * @param[in] P_GyLen Byte length of the Y coordinate + * @note All the array parameters are big-endian arrays, where the first byte is the most significant one. + * @retval CMOX_ECC_SUCCESS + * @retval CMOX_ECC_ERR_BAD_PARAMETERS + * @retval CMOX_ECC_ERR_MEMORY_FAIL + * @warning This function does not check if the parameters effectively form an elliptic curve, + * so the user must be sure about the parameters that are passed to the function + */ +cmox_ecc_retval_t cmox_ecc_customCurveConstruct(cmox_ecc_handle_t *P_pEccCtx, + cmox_ecc_impl_t *P_pCurve, + cmox_ecc_customCurve_opt_t P_OptLevel, + const uint8_t *P_pP, size_t P_PLen, + const uint8_t *P_pN, size_t P_NLen, + const uint8_t *P_pA, size_t P_ALen, + const uint8_t *P_pB, size_t P_BLen, + const uint8_t *P_pGx, size_t P_GxLen, + const uint8_t *P_pGy, size_t P_GyLen); + +/** + * @brief Destroy (and release the allocated memory) a custom curve + * @param[in] P_pEccCtx Context for ECC operations + * @param[in,out] P_pCurve Pointer to the curve to destroy + */ +void cmox_ecc_customCurveCleanup(cmox_ecc_handle_t *P_pEccCtx, cmox_ecc_impl_t *P_pCurve); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_ECC_CUSTOM_CURVES_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_retvals.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_retvals.h new file mode 100644 index 00000000..acce0c36 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_retvals.h @@ -0,0 +1,72 @@ +/** + ****************************************************************************** + * @file cmox_ecc_retvals.h + * @author MCD Application Team + * @brief This file provides the error types and code for the ECC modules + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_ECC_RETVALS_H +#define CMOX_ECC_RETVALS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_RETVALS ECC return values + * @{ + */ + +/** + * @brief Return value type for ECC module + */ +typedef uint32_t cmox_ecc_retval_t; + +#define CMOX_ECC_SUCCESS ((cmox_ecc_retval_t)0x00060000) /*!< Success */ +#define CMOX_ECC_ERR_INTERNAL ((cmox_ecc_retval_t)0x00060001) /*!< Internal computat. error (e.g. hash) */ +#define CMOX_ECC_ERR_BAD_PARAMETERS ((cmox_ecc_retval_t)0x00060003) /*!< Bad input parameters */ +#define CMOX_ECC_ERR_INVALID_PUBKEY ((cmox_ecc_retval_t)0x00060008) /*!< Invalid Public Key value */ +#define CMOX_ECC_ERR_INVALID_SIGNATURE ((cmox_ecc_retval_t)0x00060009) /*!< Invalid Signature value */ +#define CMOX_ECC_ERR_WRONG_RANDOM ((cmox_ecc_retval_t)0x0006000B) /*!< Random not compliant with the API (Recall with other random material) */ +#define CMOX_ECC_ERR_MEMORY_FAIL ((cmox_ecc_retval_t)0x0006000C) /*!< Not enough memory */ +#define CMOX_ECC_ERR_MATHCURVE_MISMATCH ((cmox_ecc_retval_t)0x0006000E) /*!< Math customization not supported by current ECC curve */ +#define CMOX_ECC_ERR_ALGOCURVE_MISMATCH ((cmox_ecc_retval_t)0x0006000F) /*!< ECC curve not supported by current functionality */ +#define CMOX_ECC_AUTH_SUCCESS ((cmox_ecc_retval_t)0x0006C726) /*!< ECC signature successful verification */ +#define CMOX_ECC_AUTH_FAIL ((cmox_ecc_retval_t)0x00066E93) /*!< ECC signature not verified */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_ECC_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_types.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_types.h new file mode 100644 index 00000000..d04a6601 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecc_types.h @@ -0,0 +1,52 @@ +/** + ****************************************************************************** + * @file cmox_ecc_types.h + * @author MCD Application Team + * @brief This file provides the types used within the ECC module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_ECC_TYPES_H +#define CMOX_ECC_TYPES_H + +#include "cmox_common.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @brief Structure for the ECC context + */ +typedef struct +{ + cmox_membuf_handle_st membuf_str; /*!< Memory buffer structure */ + cmox_math_funcs_t math_ptr; /*!< Math customization structure */ + uint32_t magic_num_check; /*!< Magic number for diagnostic checks */ +} cmox_ecc_handle_t; + +/** + * @brief Pointer type for the ECC curve function parameter + */ +typedef const struct cmox_ecc_implStruct_st *cmox_ecc_impl_t; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_ECC_TYPES_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_ecdh.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecdh.h new file mode 100644 index 00000000..3ff3e3c1 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecdh.h @@ -0,0 +1,129 @@ +/** + ****************************************************************************** + * @file cmox_ecdh.h + * @author MCD Application Team + * @brief Header file for ECDH key exchange definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_ECDH_H +#define CMOX_ECDH_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ecc/cmox_ecc.h" + +/** @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_ECDH ECDH algorithm + * @{ + */ + +/** @defgroup CMOX_ECC_ECDH_PUBLIC_METHODS ECDH public method prototypes + * @{ + */ + +/** + * @brief Generate a shared secret from a private key and a public key + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_CURVE25519 + * @arg CMOX_ECC_CURVE448 + * @arg CMOX_ECC_SECP224R1_LOWMEM + * @arg CMOX_ECC_SECP224R1_HIGHMEM + * @arg CMOX_ECC_SECP256R1_LOWMEM + * @arg CMOX_ECC_SECP256R1_HIGHMEM + * @arg CMOX_ECC_SECP384R1_LOWMEM + * @arg CMOX_ECC_SECP384R1_HIGHMEM + * @arg CMOX_ECC_SECP521R1_LOWMEM + * @arg CMOX_ECC_SECP521R1_HIGHMEM + * @arg CMOX_ECC_SECP256K1_LOWMEM + * @arg CMOX_ECC_SECP256K1_HIGHMEM + * @arg CMOX_ECC_BPP160R1_LOWMEM + * @arg CMOX_ECC_BPP160R1_HIGHMEM + * @arg CMOX_ECC_BPP192R1_LOWMEM + * @arg CMOX_ECC_BPP192R1_HIGHMEM + * @arg CMOX_ECC_BPP224R1_LOWMEM + * @arg CMOX_ECC_BPP224R1_HIGHMEM + * @arg CMOX_ECC_BPP256R1_LOWMEM + * @arg CMOX_ECC_BPP256R1_HIGHMEM + * @arg CMOX_ECC_BPP320R1_LOWMEM + * @arg CMOX_ECC_BPP320R1_HIGHMEM + * @arg CMOX_ECC_BPP384R1_LOWMEM + * @arg CMOX_ECC_BPP384R1_HIGHMEM + * @arg CMOX_ECC_BPP512R1_LOWMEM + * @arg CMOX_ECC_BPP512R1_HIGHMEM + * @arg CMOX_ECC_BPP160T1_LOWMEM + * @arg CMOX_ECC_BPP160T1_HIGHMEM + * @arg CMOX_ECC_BPP192T1_LOWMEM + * @arg CMOX_ECC_BPP192T1_HIGHMEM + * @arg CMOX_ECC_BPP224T1_LOWMEM + * @arg CMOX_ECC_BPP224T1_HIGHMEM + * @arg CMOX_ECC_BPP256T1_LOWMEM + * @arg CMOX_ECC_BPP256T1_HIGHMEM + * @arg CMOX_ECC_BPP320T1_LOWMEM + * @arg CMOX_ECC_BPP320T1_HIGHMEM + * @arg CMOX_ECC_BPP384T1_LOWMEM + * @arg CMOX_ECC_BPP384T1_HIGHMEM + * @arg CMOX_ECC_BPP512T1_LOWMEM + * @arg CMOX_ECC_BPP512T1_HIGHMEM + * @arg CMOX_ECC_FRP256V1_LOWMEM + * @arg CMOX_ECC_FRP256V1_HIGHMEM + * @param[in] P_pPrivKey Buffer with the private key + * @param[in] P_PrivKeyLen Private key length + * @param[in] P_pPubKey Buffer with the public key + * @param[in] P_PubKeyLen Public key length + * @param[out] P_pSharedSecret Buffer for the shared secret + * @param[out] P_pSharedSecretLen Shared secret length + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_ECC_ERR_INVALID_PUBKEY Public key not in a valid format + */ +cmox_ecc_retval_t cmox_ecdh(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pPrivKey, + size_t P_PrivKeyLen, + const uint8_t *P_pPubKey, + size_t P_PubKeyLen, + uint8_t *P_pSharedSecret, + size_t *P_pSharedSecretLen); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* CMOX_ECDH_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_ecdsa.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecdsa.h new file mode 100644 index 00000000..ac206c38 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_ecdsa.h @@ -0,0 +1,295 @@ +/** + ****************************************************************************** + * @file cmox_ecdsa.h + * @author MCD Application Team + * @brief Header file for ECDSA signature creation and verification + functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_ECDSA_H +#define CMOX_ECDSA_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ecc/cmox_ecc.h" + +/** @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_ECDSA ECDSA algorithm + * @{ + */ + +/** @defgroup CMOX_ECC_ECDSA_PUBLIC_METHODS ECDSA public method prototypes + * @{ + */ + +/** + * @brief Generate private and public keys to use with ECDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SECP224R1_LOWMEM + * @arg CMOX_ECC_SECP224R1_HIGHMEM + * @arg CMOX_ECC_SECP256R1_LOWMEM + * @arg CMOX_ECC_SECP256R1_HIGHMEM + * @arg CMOX_ECC_SECP384R1_LOWMEM + * @arg CMOX_ECC_SECP384R1_HIGHMEM + * @arg CMOX_ECC_SECP521R1_LOWMEM + * @arg CMOX_ECC_SECP521R1_HIGHMEM + * @arg CMOX_ECC_SECP256K1_LOWMEM + * @arg CMOX_ECC_SECP256K1_HIGHMEM + * @arg CMOX_ECC_BPP160R1_LOWMEM + * @arg CMOX_ECC_BPP160R1_HIGHMEM + * @arg CMOX_ECC_BPP192R1_LOWMEM + * @arg CMOX_ECC_BPP192R1_HIGHMEM + * @arg CMOX_ECC_BPP224R1_LOWMEM + * @arg CMOX_ECC_BPP224R1_HIGHMEM + * @arg CMOX_ECC_BPP256R1_LOWMEM + * @arg CMOX_ECC_BPP256R1_HIGHMEM + * @arg CMOX_ECC_BPP320R1_LOWMEM + * @arg CMOX_ECC_BPP320R1_HIGHMEM + * @arg CMOX_ECC_BPP384R1_LOWMEM + * @arg CMOX_ECC_BPP384R1_HIGHMEM + * @arg CMOX_ECC_BPP512R1_LOWMEM + * @arg CMOX_ECC_BPP512R1_HIGHMEM + * @arg CMOX_ECC_BPP160T1_LOWMEM + * @arg CMOX_ECC_BPP160T1_HIGHMEM + * @arg CMOX_ECC_BPP192T1_LOWMEM + * @arg CMOX_ECC_BPP192T1_HIGHMEM + * @arg CMOX_ECC_BPP224T1_LOWMEM + * @arg CMOX_ECC_BPP224T1_HIGHMEM + * @arg CMOX_ECC_BPP256T1_LOWMEM + * @arg CMOX_ECC_BPP256T1_HIGHMEM + * @arg CMOX_ECC_BPP320T1_LOWMEM + * @arg CMOX_ECC_BPP320T1_HIGHMEM + * @arg CMOX_ECC_BPP384T1_LOWMEM + * @arg CMOX_ECC_BPP384T1_HIGHMEM + * @arg CMOX_ECC_BPP512T1_LOWMEM + * @arg CMOX_ECC_BPP512T1_HIGHMEM + * @arg CMOX_ECC_FRP256V1_LOWMEM + * @arg CMOX_ECC_FRP256V1_HIGHMEM + * @param[in] P_pRandom Buffer of random bytes + * @param[in] P_RandomLen Byte length of the random buffer + * @param[out] P_pPrivKey Buffer with the private key (in range [1; N - 1]) + * @param[out] P_pPrivKeyLen Byte length of the private key + * @param[out] P_pPubKey Buffer with the public key + * @param[out] P_pPubKeyLen Byte length of the public key + * @note the private key is derived from the random in the + * following way: \verbatim privkey = (random >> ((WORD_NBIT - N_MSW_NBIT) & 7)) + 1 \endverbatim, + * where: random is the input random buffer (interpreted as a big-endian integer), + * WORD_NBIT is the bitsize of a cpu word, + * N_MSW_NBIT is the bitlength of the most significant word of the parameter N of the curve. + * @note If P_RandomLen is not enough, an error is returned and P_pPrivKeyLen + * contains the minimum number of bytes of random to provide (and is also + * the minimum length of P_pPrivKey), while P_pPubKeyLen contains the + * minimum length of P_pPubKey. + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_WRONG_RANDOM Random material too short or not valid for the functionality + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + */ +cmox_ecc_retval_t cmox_ecdsa_keyGen(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pRandom, + size_t P_RandomLen, + uint8_t *P_pPrivKey, + size_t *P_pPrivKeyLen, + uint8_t *P_pPubKey, + size_t *P_pPubKeyLen); + +/** + * @brief Generate a signature, using ECDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SECP224R1_LOWMEM + * @arg CMOX_ECC_SECP224R1_HIGHMEM + * @arg CMOX_ECC_SECP256R1_LOWMEM + * @arg CMOX_ECC_SECP256R1_HIGHMEM + * @arg CMOX_ECC_SECP384R1_LOWMEM + * @arg CMOX_ECC_SECP384R1_HIGHMEM + * @arg CMOX_ECC_SECP521R1_LOWMEM + * @arg CMOX_ECC_SECP521R1_HIGHMEM + * @arg CMOX_ECC_SECP256K1_LOWMEM + * @arg CMOX_ECC_SECP256K1_HIGHMEM + * @arg CMOX_ECC_BPP160R1_LOWMEM + * @arg CMOX_ECC_BPP160R1_HIGHMEM + * @arg CMOX_ECC_BPP192R1_LOWMEM + * @arg CMOX_ECC_BPP192R1_HIGHMEM + * @arg CMOX_ECC_BPP224R1_LOWMEM + * @arg CMOX_ECC_BPP224R1_HIGHMEM + * @arg CMOX_ECC_BPP256R1_LOWMEM + * @arg CMOX_ECC_BPP256R1_HIGHMEM + * @arg CMOX_ECC_BPP320R1_LOWMEM + * @arg CMOX_ECC_BPP320R1_HIGHMEM + * @arg CMOX_ECC_BPP384R1_LOWMEM + * @arg CMOX_ECC_BPP384R1_HIGHMEM + * @arg CMOX_ECC_BPP512R1_LOWMEM + * @arg CMOX_ECC_BPP512R1_HIGHMEM + * @arg CMOX_ECC_BPP160T1_LOWMEM + * @arg CMOX_ECC_BPP160T1_HIGHMEM + * @arg CMOX_ECC_BPP192T1_LOWMEM + * @arg CMOX_ECC_BPP192T1_HIGHMEM + * @arg CMOX_ECC_BPP224T1_LOWMEM + * @arg CMOX_ECC_BPP224T1_HIGHMEM + * @arg CMOX_ECC_BPP256T1_LOWMEM + * @arg CMOX_ECC_BPP256T1_HIGHMEM + * @arg CMOX_ECC_BPP320T1_LOWMEM + * @arg CMOX_ECC_BPP320T1_HIGHMEM + * @arg CMOX_ECC_BPP384T1_LOWMEM + * @arg CMOX_ECC_BPP384T1_HIGHMEM + * @arg CMOX_ECC_BPP512T1_LOWMEM + * @arg CMOX_ECC_BPP512T1_HIGHMEM + * @arg CMOX_ECC_FRP256V1_LOWMEM + * @arg CMOX_ECC_FRP256V1_HIGHMEM + * @param[in] P_pRandom Buffer of random bytes + * @param[in] P_RandomLen Byte length of the random buffer + * @param[in] P_pPrivKey Buffer with the private key + * @param[in] P_PrivKeyLen Byte length of the private key + * @param[in] P_pDigest Buffer with the digest to sign + * @param[in] P_DigestLen Byte length of the digest + * @param[out] P_pSignature Buffer with the signature (concatenation of R and S) + * @param[out] P_pSignatureLen Byte length of the signature + * @note the internal value k, used as scalar and multiplied by the base point, is derived from the random in the + * following way: \verbatim k = (random >> ((WORD_NBIT - N_MSW_NBIT) & 7)) + 1 \endverbatim, + * where: random is the input random buffer (interpreted as a big-endian integer), + * WORD_NBIT is the bitsize of a cpu word, + * N_MSW_NBIT is the bitlength of the most significant word of the parameter N of the curve. + * @note If P_RandomLen is not enough, an error is returned and + * P_pSignatureLen contains the minimum number of bytes of random + * to provide (and is also the minimum length of P_pPrivKey, and + * half the length of P_pSignature). + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_WRONG_RANDOM Random material too short or not valid for the functionality + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + */ +cmox_ecc_retval_t cmox_ecdsa_sign(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pRandom, + size_t P_RandomLen, + const uint8_t *P_pPrivKey, + size_t P_PrivKeyLen, + const uint8_t *P_pDigest, + size_t P_DigestLen, + uint8_t *P_pSignature, + size_t *P_pSignatureLen); + +/** + * @brief Verify a signature, using ECDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SECP224R1_LOWMEM + * @arg CMOX_ECC_SECP224R1_HIGHMEM + * @arg CMOX_ECC_SECP256R1_LOWMEM + * @arg CMOX_ECC_SECP256R1_HIGHMEM + * @arg CMOX_ECC_SECP384R1_LOWMEM + * @arg CMOX_ECC_SECP384R1_HIGHMEM + * @arg CMOX_ECC_SECP521R1_LOWMEM + * @arg CMOX_ECC_SECP521R1_HIGHMEM + * @arg CMOX_ECC_SECP256K1_LOWMEM + * @arg CMOX_ECC_SECP256K1_HIGHMEM + * @arg CMOX_ECC_BPP160R1_LOWMEM + * @arg CMOX_ECC_BPP160R1_HIGHMEM + * @arg CMOX_ECC_BPP192R1_LOWMEM + * @arg CMOX_ECC_BPP192R1_HIGHMEM + * @arg CMOX_ECC_BPP224R1_LOWMEM + * @arg CMOX_ECC_BPP224R1_HIGHMEM + * @arg CMOX_ECC_BPP256R1_LOWMEM + * @arg CMOX_ECC_BPP256R1_HIGHMEM + * @arg CMOX_ECC_BPP320R1_LOWMEM + * @arg CMOX_ECC_BPP320R1_HIGHMEM + * @arg CMOX_ECC_BPP384R1_LOWMEM + * @arg CMOX_ECC_BPP384R1_HIGHMEM + * @arg CMOX_ECC_BPP512R1_LOWMEM + * @arg CMOX_ECC_BPP512R1_HIGHMEM + * @arg CMOX_ECC_BPP160T1_LOWMEM + * @arg CMOX_ECC_BPP160T1_HIGHMEM + * @arg CMOX_ECC_BPP192T1_LOWMEM + * @arg CMOX_ECC_BPP192T1_HIGHMEM + * @arg CMOX_ECC_BPP224T1_LOWMEM + * @arg CMOX_ECC_BPP224T1_HIGHMEM + * @arg CMOX_ECC_BPP256T1_LOWMEM + * @arg CMOX_ECC_BPP256T1_HIGHMEM + * @arg CMOX_ECC_BPP320T1_LOWMEM + * @arg CMOX_ECC_BPP320T1_HIGHMEM + * @arg CMOX_ECC_BPP384T1_LOWMEM + * @arg CMOX_ECC_BPP384T1_HIGHMEM + * @arg CMOX_ECC_BPP512T1_LOWMEM + * @arg CMOX_ECC_BPP512T1_HIGHMEM + * @arg CMOX_ECC_FRP256V1_LOWMEM + * @arg CMOX_ECC_FRP256V1_HIGHMEM + * @param[in] P_pPubKey Buffer with the public key + * @param[in] P_PubKeyLen Byte length of the public key + * @param[in] P_pDigest Buffer with the digest to sign + * @param[in] P_DigestLen Byte length of the digest + * @param[in] P_pSignature Buffer with the signature (concatenation of R and S) + * @param[in] P_SignatureLen Byte length of the signature + * @param[out] P_pFaultCheck Optional value to check, together with the retval, to verify if some fault happened + * @retval CMOX_ECC_AUTH_SUCCESS Signature verified + * @retval CMOX_ECC_AUTH_FAIL Signature NOT verified + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_ECC_ERR_INVALID_SIGNATURE Input signature corrupted or not in the expected format + * @retval CMOX_ECC_ERR_INVALID_PUBKEY Public key not in a valid format + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must be done against + * the success value, and not comparing the value with the failure value. Indeed, in presence of faults, + * especially P_pFaultCheck, could be a dirty value. + */ +cmox_ecc_retval_t cmox_ecdsa_verify(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pPubKey, + size_t P_PubKeyLen, + const uint8_t *P_pDigest, + size_t P_DigestLen, + const uint8_t *P_pSignature, + size_t P_SignatureLen, + uint32_t *P_pFaultCheck); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* CMOX_ECDSA_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_eddsa.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_eddsa.h new file mode 100644 index 00000000..8c224ebd --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_eddsa.h @@ -0,0 +1,175 @@ +/** + ****************************************************************************** + * @file cmox_eddsa.h + * @author MCD Application Team + * @brief Header file for EdDSA signature creation and verification + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_EDDSA_H +#define CMOX_EDDSA_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ecc/cmox_ecc.h" + +/** @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_EDDSA EDDSA algorithm + * @{ + */ + +/** @defgroup CMOX_ECC_EDDSA_PUBLIC_METHODS EDDSA public method prototypes + * @{ + */ + +/** + * @brief Generate private and public keys to use with EDDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_ED25519_HIGHMEM + * @arg CMOX_ECC_ED25519_OPT_LOWMEM + * @arg CMOX_ECC_ED25519_OPT_HIGHMEM + * @arg CMOX_ECC_ED448_LOWMEM + * @arg CMOX_ECC_ED448_HIGHMEM + * @param[in] P_pRandom Buffer of random bytes + * @param[in] P_RandomLen Byte length of the random buffer + * @param[out] P_pPrivKey Buffer with the private key, whose first half will be the secret value and the second + * half will contain the public key + * @param[out] P_pPrivKeyLen Byte length of the private key + * @param[out] P_pPubKey Buffer with the public key + * @param[out] P_pPubKeyLen Byte length of the public key + * @note If P_RandomLen is not enough, an error is returned and P_pPrivKeyLen + * contains the minimum number of bytes of random to provide (and is also + * half the minimum length of P_pPrivKey), while P_pPubKeyLen contains the + * minimum length of P_pPubKey. + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_WRONG_RANDOM Random material too short or not valid for the functionality + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_ECC_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + */ +cmox_ecc_retval_t cmox_eddsa_keyGen(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pRandom, + size_t P_RandomLen, + uint8_t *P_pPrivKey, + size_t *P_pPrivKeyLen, + uint8_t *P_pPubKey, + size_t *P_pPubKeyLen); + +/** + * @brief Generate a signature, using EDDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_ED25519_HIGHMEM + * @arg CMOX_ECC_ED25519_OPT_LOWMEM + * @arg CMOX_ECC_ED25519_OPT_HIGHMEM + * @arg CMOX_ECC_ED448_LOWMEM + * @arg CMOX_ECC_ED448_HIGHMEM + * @param[in] P_pPrivKey Buffer with the private key, whose first half is the secret value and the second half + * contains the public key + * @param[in] P_PrivKeyLen Byte length of the private key + * @param[in] P_pMessage Buffer with the message to sign + * @param[in] P_MessageLen Byte length of the message + * @param[out] P_pSignature Buffer with the signature (concatenation of R and S) + * @param[out] P_pSignatureLen Byte length of the signature + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_ECC_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + */ +cmox_ecc_retval_t cmox_eddsa_sign(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pPrivKey, + size_t P_PrivKeyLen, + const uint8_t *P_pMessage, + size_t P_MessageLen, + uint8_t *P_pSignature, + size_t *P_pSignatureLen); + +/** + * @brief Verify a signature, using EDDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_ED25519_HIGHMEM + * @arg CMOX_ECC_ED25519_OPT_LOWMEM + * @arg CMOX_ECC_ED25519_OPT_HIGHMEM + * @arg CMOX_ECC_ED448_LOWMEM + * @arg CMOX_ECC_ED448_HIGHMEM + * @param[in] P_pPubKey Buffer with the public key + * @param[in] P_PubKeyLen Byte length of the public key + * @param[in] P_pMessage Buffer with the message to verify + * @param[in] P_MessageLen Byte length of the message + * @param[in] P_pSignature Buffer with the signature (concatenation of R and S) + * @param[in] P_SignatureLen Byte length of the signature + * @param[out] P_pFaultCheck Optional value to check, together with the retval, to verify if some fault happened + * @retval CMOX_ECC_AUTH_SUCCESS Signature verified + * @retval CMOX_ECC_AUTH_FAIL Signature NOT verified + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_ECC_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + * @retval CMOX_ECC_ERR_INVALID_SIGNATURE Input signature corrupted or not in the expected format + * @retval CMOX_ECC_ERR_INVALID_PUBKEY Public key not in a valid format + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must be done against + * the success value, and not comparing the value with the failure value. Indeed, in presence of faults, + * especially P_pFaultCheck, could be a dirty value. + */ +cmox_ecc_retval_t cmox_eddsa_verify(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pPubKey, + size_t P_PubKeyLen, + const uint8_t *P_pMessage, + size_t P_MessageLen, + const uint8_t *P_pSignature, + size_t P_SignatureLen, + uint32_t *P_pFaultCheck); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* CMOX_EDDSA_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/ecc/cmox_sm2.h b/firmware/ui/dependencies/cmox/include/ecc/cmox_sm2.h new file mode 100644 index 00000000..5f1a9776 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/ecc/cmox_sm2.h @@ -0,0 +1,218 @@ +/** + ****************************************************************************** + * @file cmox_sm2.h + * @author MCD Application Team + * @brief Header file for SM2 signature creation and verification functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_SM2_H +#define CMOX_SM2_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ecc/cmox_ecc.h" + +/** @addtogroup CMOX_ECC + * @{ + */ + +/** + * @defgroup CMOX_ECC_SM2 SM2 algorithm + * @{ + */ + +/** @defgroup CMOX_ECC_SM2_PUBLIC_METHODS SM2 public method prototypes + * @{ + */ + +/** + * @brief Generate private and public keys to use with ECDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SM2_LOWMEM + * @arg CMOX_ECC_SM2_HIGHMEM + * @arg CMOX_ECC_SM2TEST_LOWMEM + * @arg CMOX_ECC_SM2TEST_HIGHMEM + * @param[in] P_pRandom Buffer of random bytes + * @param[in] P_RandomLen Byte length of the random buffer + * @param[out] P_pPrivKey Buffer with the private key (in range [1; N - 2]) + * @param[out] P_pPrivKeyLen Byte length of the private key + * @param[out] P_pPubKey Buffer with the public key + * @param[out] P_pPubKeyLen Byte length of the public key + * @note the private key is derived from the random in the + * following way: \verbatim privkey = (random >> ((WORD_NBIT - N_MSW_NBIT) & 7)) + 1 \endverbatim, + * where: random is the input random buffer (interpreted as a big-endian integer), + * WORD_NBIT is the bitsize of a cpu word, + * N_MSW_NBIT is the bitlength of the most significant word of the parameter N of the curve. + * @note If P_RandomLen is not enough, an error is returned and P_pPrivKeyLen + * contains the minimum number of bytes of random to provide (and is also + * the minimum length of P_pPrivKey), while P_pPubKeyLen contains the + * minimum length of P_pPubKey. + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_WRONG_RANDOM Random material too short or not valid for the functionality + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + */ +cmox_ecc_retval_t cmox_sm2_keyGen(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pRandom, + size_t P_RandomLen, + uint8_t *P_pPrivKey, + size_t *P_pPrivKeyLen, + uint8_t *P_pPubKey, + size_t *P_pPubKeyLen); + +/** + * @brief Compute ZA = HASH(ENTLA || IDA || a || b || xG || yG || xA || yA), that is the value to hash, together + * with the message, to obtain the digest to provide to \ref cmox_sm2_sign and \ref cmox_sm2_verify + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SM2_LOWMEM + * @arg CMOX_ECC_SM2_HIGHMEM + * @arg CMOX_ECC_SM2TEST_LOWMEM + * @arg CMOX_ECC_SM2TEST_HIGHMEM + * @param[in] P_pIDA Buffer with ID (e.g. email "user@domain.com") of the user + * @param[in] P_ENTLA Bit length of the user ID (usually, multiple of 8) + * @param[in] P_pPubKey Buffer with the public key (both X and Y) + * @param[in] P_PubKeyLen Byte length of the public key + * @param[out] P_pZA Buffer with ZA + * @param[out] P_pZALen Byte length of ZA (optional parameter) + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + */ +cmox_ecc_retval_t cmox_sm2_computeZA(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pIDA, + uint16_t P_ENTLA, + const uint8_t *P_pPubKey, + size_t P_PubKeyLen, + uint8_t *P_pZA, + size_t *P_pZALen); + +/** + * @brief Generate a signature, using ECDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SM2_LOWMEM + * @arg CMOX_ECC_SM2_HIGHMEM + * @arg CMOX_ECC_SM2TEST_LOWMEM + * @arg CMOX_ECC_SM2TEST_HIGHMEM + * @param[in] P_pRandom Buffer of random bytes + * @param[in] P_RandomLen Byte length of the random buffer + * @param[in] P_pPrivKey Buffer with the private key + * @param[in] P_PrivKeyLen Byte length of the private key + * @param[in] P_pDigest Buffer with the digest to sign + * @param[in] P_DigestLen Byte length of the digest + * @param[out] P_pSignature Buffer with the signature (concatenation of R and S) + * @param[out] P_pSignatureLen Byte length of the signature + * @note the internal value k, used as scalar and multiplied by the base point, is derived from the random in the + * following way: \verbatim k = (random >> ((WORD_NBIT - N_MSW_NBIT) & 7)) + 1 \endverbatim, + * where: random is the input random buffer (interpreted as a big-endian integer), + * WORD_NBIT is the bitsize of a cpu word, + * N_MSW_NBIT is the bitlength of the most significant word of the parameter N of the curve. + * @note If P_RandomLen is not enough, an error is returned and + * P_pSignatureLen contains the minimum number of bytes of random + * to provide (and is also the minimum length of P_pPrivKey, and + * half the length of P_pSignature). + * @retval CMOX_ECC_SUCCESS Everything OK + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_WRONG_RANDOM Random material too short or not valid for the functionality + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + */ +cmox_ecc_retval_t cmox_sm2_sign(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pRandom, + size_t P_RandomLen, + const uint8_t *P_pPrivKey, + size_t P_PrivKeyLen, + const uint8_t *P_pDigest, + size_t P_DigestLen, + uint8_t *P_pSignature, + size_t *P_pSignatureLen); + +/** + * @brief Verify a signature, using ECDSA + * @param[in] P_pEccCtx Context for ECC operations + * @param[in] P_CurveParams Curve Parameters and point functions + * This parameter can be one of the following: + * @arg CMOX_ECC_SM2_LOWMEM + * @arg CMOX_ECC_SM2_HIGHMEM + * @arg CMOX_ECC_SM2TEST_LOWMEM + * @arg CMOX_ECC_SM2TEST_HIGHMEM + * @param[in] P_pPubKey Buffer with the public key + * @param[in] P_PubKeyLen Byte length of the public key + * @param[in] P_pDigest Buffer with the digest to sign + * @param[in] P_DigestLen Byte length of the digest + * @param[in] P_pSignature Buffer with the signature (concatenation of R and S) + * @param[in] P_SignatureLen Byte length of the signature + * @param[out] P_pFaultCheck Optional value to check, together with the retval, to verify if some fault happened + * @retval CMOX_ECC_AUTH_SUCCESS Signature verified + * @retval CMOX_ECC_AUTH_FAIL Signature NOT verified + * @retval CMOX_ECC_ERR_MATHCURVE_MISMATCH Mathematical function set is not compatible with current ECC curve + * @retval CMOX_ECC_ERR_ALGOCURVE_MISMATCH Curve is not compatible with current functionality + * @retval CMOX_ECC_ERR_BAD_PARAMETERS Some NULL/wrong/empty parameter or Construct API not called + * @retval CMOX_ECC_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_ECC_ERR_INVALID_SIGNATURE Input signature corrupted or not in the expected format + * @retval CMOX_ECC_ERR_INVALID_PUBKEY Public key not in a valid format + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must be done against + * the success value, and not comparing the value with the failure value. Indeed, in presence of faults, + * especially P_pFaultCheck, could be a dirty value. + */ +cmox_ecc_retval_t cmox_sm2_verify(cmox_ecc_handle_t *P_pEccCtx, + const cmox_ecc_impl_t P_CurveParams, + const uint8_t *P_pPubKey, + size_t P_PubKeyLen, + const uint8_t *P_pDigest, + size_t P_DigestLen, + const uint8_t *P_pSignature, + size_t P_SignatureLen, + uint32_t *P_pFaultCheck); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* CMOX_SM2_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_hash.h b/firmware/ui/dependencies/cmox/include/hash/cmox_hash.h new file mode 100644 index 00000000..4faad30e --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_hash.h @@ -0,0 +1,278 @@ +/** + ****************************************************************************** + * @file cmox_hash.h + * @author MCD Application Team + * @brief Header file for the Hash module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_HASH_H +#define CMOX_HASH_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include +#include +#include "cmox_hash_retvals.h" + +/** @addtogroup CMOX_HASH + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_HASH_PUBLIC_TYPES Hash module public types + * @{ + */ + +/** + * @brief Hash Virtual Table + * + * This type specifies a pointer to the virtual table containing the methods + * for a particular algorithm (e.g. SHA256 or SM3) + */ +typedef const struct cmox_hash_vtableStruct_st *cmox_hash_vtable_t; + +/** + * @brief Hash algorithm type + * + * This type specifies the algorithm to use with the hash module (e.g. SHA256). + * The type is defined as a pointer to a structure, that + * contains the functions for the specific algorithm, defined in the library + * internally + */ +typedef const struct cmox_hash_algoStruct_st *cmox_hash_algo_t; + + +/** + * @brief Hash handle structure definition + */ +typedef struct +{ + cmox_hash_vtable_t table; /*!< Hash virtual table */ + size_t tagLen; /*!< Size in bytes of the digest */ +} cmox_hash_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_HASH_PUBLIC_CONSTANTS Hash public constants + * @{ + */ + +/** + * @brief Identifier of the SHA1 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA1_ALGO; + +/** + * @brief Identifier of the SHA224 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA224_ALGO; + +/** + * @brief Identifier of the SHA256 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA256_ALGO; + +/** + * @brief Identifier of the SHA384 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA384_ALGO; + +/** + * @brief Identifier of the SHA512 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA512_ALGO; + +/** + * @brief Identifier of the SHA512/224 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA512_224_ALGO; + +/** + * @brief Identifier of the SHA512/256 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA512_256_ALGO; + +/** + * @brief Identifier of the SHA3-224 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA3_224_ALGO; + +/** + * @brief Identifier of the SHA3-256 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA3_256_ALGO; + +/** + * @brief Identifier of the SHA3-384 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA3_384_ALGO; + +/** + * @brief Identifier of the SHA3-512 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHA3_512_ALGO; + +/** + * @brief Identifier of the SHAKE128 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHAKE128_ALGO; + +/** + * @brief Identifier of the SHAKE256 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SHAKE256_ALGO; + +/** + * @brief Identifier of the SM3 hash function for single-call function + * (Defined internally) + */ +extern const cmox_hash_algo_t CMOX_SM3_ALGO; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_HASH_PUBLIC_METHODS Hash public method prototypes + * @{ + */ + +/** + * @brief Cleanup the hash handler + * + * @param P_pThis Hash handler to cleanup + * @return cmox_hash_retval_t Hash return value + */ +cmox_hash_retval_t cmox_hash_cleanup(cmox_hash_handle_t *P_pThis); + +/** + * @brief Initialize the hash handle based on the selected algorithm + * + * @param P_pThis Hash handle to initialize + * @return cmox_hash_retval_t Hash return value + * @note The hash handle must be derived from an algorithm-specific handle + * using the correct construct + */ +cmox_hash_retval_t cmox_hash_init(cmox_hash_handle_t *P_pThis); + +/** + * @brief Set the size of the digest + * + * @param P_pThis Hash handle to set + * @param P_tagLen Size in bytes of the tag + * @return cmox_hash_retval_t Hash return value + */ +cmox_hash_retval_t cmox_hash_setTagLen(cmox_hash_handle_t *P_pThis, + size_t P_tagLen); + +/** + * @brief Append part or the totality of the plaintext to the hash handle + * + * @param P_pThis Hash handle to use for hashing the data + * @param P_pInput Buffer of bytes containing the data to hash + * @param P_inputLen Size in bytes of the data to hash + * @return cmox_hash_retval_t Hash return value + */ +cmox_hash_retval_t cmox_hash_append(cmox_hash_handle_t *P_pThis, + const uint8_t *P_pInput, + size_t P_inputLen); + +/** + * @brief Generate the digest of the already appended data + * + * @param P_pThis Hash handle used for appending the data to hash + * @param P_pDigest Buffer of bytes where the digest will be stored + * @param P_pDigestLen Number of bytes generated by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_hash_retval_t Hash return value + */ +cmox_hash_retval_t cmox_hash_generateTag(cmox_hash_handle_t *P_pThis, + uint8_t *P_pDigest, + size_t *P_pDigestLen); + +/** + * @brief Compute the digest of a message using a hash algorithm + * + * @param P_algo Identifier of the hash algorithm to use for the computation. + * This parameter can be one of the following: + * @arg CMOX_SHA1_ALGO + * @arg CMOX_SHA224_ALGO + * @arg CMOX_SHA256_ALGO + * @arg CMOX_SHA384_ALGO + * @arg CMOX_SHA512_ALGO + * @arg CMOX_SHA512_224_ALGO + * @arg CMOX_SHA512_256_ALGO + * @arg CMOX_SHA3_224_ALGO + * @arg CMOX_SHA3_256_ALGO + * @arg CMOX_SHA3_384_ALGO + * @arg CMOX_SHA3_512_ALGO + * @arg CMOX_SHAKE128_ALGO + * @arg CMOX_SHAKE256_ALGO + * @arg CMOX_SM3_ALGO + * @param P_pPlaintext Buffer of bytes containing the message to hash + * @param P_plaintextLen Size in bytes of the message to hash + * @param P_pDigest Buffer of bytes that will contain the computed digest + * @param P_expectedDigestLen Desired size in bytes of the digest to compute + * @param P_pComputedDigestLen Number of bytes generated by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_hash_retval_t + */ +cmox_hash_retval_t cmox_hash_compute(cmox_hash_algo_t P_algo, + const uint8_t *P_pPlaintext, + size_t P_plaintextLen, + uint8_t *P_pDigest, + const size_t P_expectedDigestLen, + size_t *P_pComputedDigestLen); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_HASH_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_hash_retvals.h b/firmware/ui/dependencies/cmox/include/hash/cmox_hash_retvals.h new file mode 100644 index 00000000..efae74d4 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_hash_retvals.h @@ -0,0 +1,90 @@ +/** + ****************************************************************************** + * @file cmox_hash_retvals.h + * @author MCD Application Team + * @brief Header file containing the return values for the hash module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_HASH_RETVALS_H +#define CMOX_HASH_RETVALS_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include + +/** @addtogroup CMOX_HASH + * @{ + */ + +/** @defgroup CMOX_HASH_RETVALS Hash return values + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** + * @brief Hash operation successfully performed + */ +#define CMOX_HASH_SUCCESS ((cmox_hash_retval_t)0x00020000U) + +/** + * @brief Some error happens internally in the hash module + */ +#define CMOX_HASH_ERR_INTERNAL ((cmox_hash_retval_t)0x00020001U) + +/** + * @brief One or more parameter has been wrongly passed to the function + * (e.g. pointer to NULL) + */ +#define CMOX_HASH_ERR_BAD_PARAMETER ((cmox_hash_retval_t)0x00020003U) + +/** + * @brief Error on performing the operation + * (e.g. an operation has been called before initializing the handle) + */ +#define CMOX_HASH_ERR_BAD_OPERATION ((cmox_hash_retval_t)0x00020004U) + +/** + * @brief The desired digest size is not supported by the hash alforithm + */ +#define CMOX_HASH_ERR_BAD_TAG_SIZE ((cmox_hash_retval_t)0x00020006U) + +/* Public types --------------------------------------------------------------*/ + +/** + * @brief Hash module return value type + */ +typedef uint32_t cmox_hash_retval_t; + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_HASH_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_md.h b/firmware/ui/dependencies/cmox/include/hash/cmox_md.h new file mode 100644 index 00000000..cf62e1b3 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_md.h @@ -0,0 +1,127 @@ +/** + ****************************************************************************** + * @file cmox_md.h + * @author MCD Application Team + * @brief Header file containing the definitions for MD hash functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_MD_H +#define CMOX_MD_H + +#include "cmox_hash.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_HASH + * @{ + */ + +/** + * @defgroup CMOX_MD MD hash module + * @{ + */ + +/* Public types --------------------------------------------------------------*/ + +/** + * @defgroup CMOX_MD_PUBLIC_TYPES MD public types + * @{ + */ + +/** + * @brief MD Virtual Table + * + * This type specifies a pointer to the virtual table containing the methods + * for the particular MD algorithm + */ +typedef const struct cmox_md_vtableStruct_st *cmox_md_vtable_t; + +/** + * @brief MD engine common structure definition + */ +typedef struct +{ + cmox_md_vtable_t table; /*!< MD virtual table */ + uint32_t flags; /*!< 32 bit flags, used to perform keyschedule */ + size_t tagLen; /*!< Size of the required Digest */ + size_t blockLen; /*!< Internal: Size of processed blocks */ + size_t labelLen; /*!< Internal: Size of the final label of the MD padding scheme */ + uint32_t bitCount[2]; /*!< Internal: Keeps the count of processed bits */ +} cmox_md_engineCommon_t; + +/** + * @brief MD small engine structure definition + * (Used by SHA1, SHA224, SHA256, SM3) + */ +typedef struct +{ + cmox_md_engineCommon_t engine; /*!< Engine of the MD function */ + uint8_t internalBuffer[64]; /*!< Internal: It's a buffer with the data to be hashed */ + uint32_t internalState[8]; /*!< Internal: Keeps the internal state */ +} cmox_mdSmall_engine_t; + +/** + * @brief MD large engine structure definition (Used by SHA384, SHA512) + */ +typedef struct +{ + cmox_md_engineCommon_t engine; /*!< Engine of the MD function */ + uint8_t internalBuffer[128]; /*!< Internal: It's a buffer with the data to be hashed */ + uint64_t internalState[8]; /*!< Internal: Keeps the internal state */ +} cmox_mdLarge_engine_t; + +/** + * @brief Generic MD small handle structure + */ +typedef struct +{ + cmox_hash_handle_t super; /*!< General hash module */ + cmox_mdSmall_engine_t md; /*!< MD small engine */ +} cmox_mdSmall_handle_t; + +/** + * @brief Generic MD large handle structure + */ +typedef struct +{ + cmox_hash_handle_t super; /*!< General hash module */ + cmox_mdLarge_engine_t md; /*!< MD large engine */ +} cmox_mdLarge_handle_t; + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_MD_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sha1.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sha1.h new file mode 100644 index 00000000..25a18ead --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sha1.h @@ -0,0 +1,104 @@ +/** + ****************************************************************************** + * @file cmox_sha1.h + * @author MCD Application Team + * @brief Header file for the SHA1 hash definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SHA1_H +#define CMOX_SHA1_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_md.h" + +/** + * @addtogroup CMOX_MD + * @{ + */ + +/** + * @defgroup CMOX_SHA1 SHA1 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA1_MACROS SHA1 macros + * @{ + */ + +#define CMOX_SHA1_SIZE 20U /*!< Maximum size in bytes of a SHA1 digest */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA1_PUBLIC_TYPES SHA1 module public types + * @{ + */ + +/** + * @brief SHA1 handle type definition + */ +typedef cmox_mdSmall_handle_t cmox_sha1_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SHA1_PUBLIC_METHODS SHA1 module public method prototypes + * @{ + */ + +/** + * @brief SHA1 constructor + * + * @param P_pThis Pointer to the SHA1 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha1_construct(cmox_sha1_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SHA1_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sha224.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sha224.h new file mode 100644 index 00000000..2ef1d1e2 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sha224.h @@ -0,0 +1,104 @@ +/** + ****************************************************************************** + * @file cmox_sha224.h + * @author MCD Application Team + * @brief Header file for the SHA224 hash definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SHA224_H +#define CMOX_SHA224_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_md.h" + +/** + * @addtogroup CMOX_MD + * @{ + */ + +/** + * @defgroup CMOX_SHA224 SHA224 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA224_MACROS SHA224 macros + * @{ + */ + +#define CMOX_SHA224_SIZE 28U /*!< Maximum size in bytes of a SHA224 digest */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA224_PUBLIC_TYPES SHA224 module public types + * @{ + */ + +/** + * @brief SHA224 handle type definition + */ +typedef cmox_mdSmall_handle_t cmox_sha224_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SHA224_PUBLIC_METHODS SHA224 module public method prototypes + * @{ + */ + +/** + * @brief SHA224 constructor + * + * @param P_pThis Pointer to the SHA224 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha224_construct(cmox_sha224_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SHA224_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sha256.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sha256.h new file mode 100644 index 00000000..b36b49d1 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sha256.h @@ -0,0 +1,104 @@ +/** + ****************************************************************************** + * @file cmox_sha256.h + * @author MCD Application Team + * @brief Header file for the SHA256 hash definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SHA256_H +#define CMOX_SHA256_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_md.h" + +/** + * @addtogroup CMOX_MD + * @{ + */ + +/** + * @defgroup CMOX_SHA256 SHA256 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA256_MACROS SHA256 macros + * @{ + */ + +#define CMOX_SHA256_SIZE 32U /*!< Maximum size in bytes of a SHA256 digest */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA256_PUBLIC_TYPES SHA256 module public types + * @{ + */ + +/** + * @brief SHA256 handle type definition + */ +typedef cmox_mdSmall_handle_t cmox_sha256_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SHA256_PUBLIC_METHODS SHA256 module public method prototypes + * @{ + */ + +/** + * @brief SHA256 constructor + * + * @param P_pThis Pointer to the SHA256 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha256_construct(cmox_sha256_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SHA256_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sha3.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sha3.h new file mode 100644 index 00000000..7c055716 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sha3.h @@ -0,0 +1,162 @@ +/** + ****************************************************************************** + * @file cmox_sha3.h + * @author MCD Application Team + * @brief Header file for SHA3 and SHAKE hash functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SHA3_H +#define CMOX_SHA3_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_sponge.h" + +/** + * @addtogroup CMOX_HASH + * @{ + */ + +/** + * @defgroup CMOX_SHA3 SHA3 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA3_MACROS SHA3 macros + * @{ + */ + +#define CMOX_SHA3_224_SIZE 28U /*!< Number of bytes (uint8_t) to store a SHA3-224 digest. */ +#define CMOX_SHA3_256_SIZE 32U /*!< Number of bytes (uint8_t) to store a SHA3-256 digest. */ +#define CMOX_SHA3_384_SIZE 48U /*!< Number of bytes (uint8_t) to store a SHA3-384 digest. */ +#define CMOX_SHA3_512_SIZE 64U /*!< Number of bytes (uint8_t) to store a SHA3-512 digest. */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA3_PUBLIC_TYPES SHA3 module public types + * @{ + */ + +/** + * @brief SHA3 handle type definition + */ +typedef struct +{ + cmox_hash_handle_t super; /*!< General hash module */ + uint32_t flags; /*!< Internal flag */ + cmox_sponge_handle_t keccak; /*!< Keccak P-1600 handler */ +} cmox_sha3_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SHA3_PUBLIC_METHODS SHA3 module public method prototypes + * @{ + */ + +/** + * @brief SHA3-224 constructor + * + * @param P_pThis Pointer to the SHA3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha3_224_construct(cmox_sha3_handle_t *P_pThis); + +/** + * @brief SHA3-256 constructor + * + * @param P_pThis Pointer to the SHA3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha3_256_construct(cmox_sha3_handle_t *P_pThis); + +/** + * @brief SHA3-384 constructor + * + * @param P_pThis Pointer to the SHA3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha3_384_construct(cmox_sha3_handle_t *P_pThis); + +/** + * @brief SHA3-512 constructor + * + * @param P_pThis Pointer to the SHA3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha3_512_construct(cmox_sha3_handle_t *P_pThis); + +/** + * @brief SHAKE128 constructor + * + * @param P_pThis Pointer to the SHA3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_shake128_construct(cmox_sha3_handle_t *P_pThis); + +/** + * @brief SHAKE256 constructor + * + * @param P_pThis Pointer to the SHA3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_shake256_construct(cmox_sha3_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SHA3_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sha384.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sha384.h new file mode 100644 index 00000000..dc994085 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sha384.h @@ -0,0 +1,104 @@ +/** + ****************************************************************************** + * @file cmox_sha384.h + * @author MCD Application Team + * @brief Header file for the SHA384 hash definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SHA384_H +#define CMOX_SHA384_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_md.h" + +/** + * @addtogroup CMOX_MD + * @{ + */ + +/** + * @defgroup CMOX_SHA384 SHA384 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA384_MACROS SHA384 macros + * @{ + */ + +#define CMOX_SHA384_SIZE 48U /*!< Maximum size in bytes of a SHA384 digest */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA384_PUBLIC_TYPES SHA384 module public types + * @{ + */ + +/** + * @brief SHA384 handle type definition + */ +typedef cmox_mdLarge_handle_t cmox_sha384_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SHA384_PUBLIC_METHODS SHA384 module public method prototypes + * @{ + */ + +/** + * @brief SHA384 constructor + * + * @param P_pThis Pointer to the SHA384 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha384_construct(cmox_sha384_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SHA384_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sha512.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sha512.h new file mode 100644 index 00000000..28e46b08 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sha512.h @@ -0,0 +1,126 @@ +/** + ****************************************************************************** + * @file cmox_sha512.h + * @author MCD Application Team + * @brief Header file for the SHA512 hash definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SHA512_H +#define CMOX_SHA512_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_md.h" + +/** + * @addtogroup CMOX_MD + * @{ + */ + +/** + * @defgroup CMOX_SHA512 SHA512 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA512_MACROS SHA512 macros + * @{ + */ + +#define CMOX_SHA512_SIZE 64U /*!< Maximum size in bytes of a SHA512 digest */ +#define CMOX_SHA512_224_SIZE 28U /*!< Maximum size in bytes of a SHA512/224 digest */ +#define CMOX_SHA512_256_SIZE 32U /*!< Maximum size in bytes of a SHA512/256 digest */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SHA512_PUBLIC_TYPES SHA512 module public types + * @{ + */ + +/** + * @brief SHA512 handle type definition + */ +typedef cmox_mdLarge_handle_t cmox_sha512_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SHA512_PUBLIC_METHODS SHA512 module public method prototypes + * @{ + */ + +/** + * @brief SHA512 constructor + * + * @param P_pThis Pointer to the SHA512 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha512_construct(cmox_sha512_handle_t *P_pThis); + +/** + * @brief SHA512/224 constructor + * + * @param P_pThis Pointer to the SHA512 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha512_224_construct(cmox_sha512_handle_t *P_pThis); + +/** + * @brief SHA512/256 constructor + * + * @param P_pThis Pointer to the SHA512 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sha512_256_construct(cmox_sha512_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SHA512_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sm3.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sm3.h new file mode 100644 index 00000000..fe0c3667 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sm3.h @@ -0,0 +1,105 @@ +/** + ****************************************************************************** + * @file cmox_sm3.h + * @author MCD Application Team + * @brief Header file for the SM3 hash definition and function + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_SM3_H +#define CMOX_SM3_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include "cmox_hash.h" +#include "cmox_md.h" + +/** + * @addtogroup CMOX_MD + * @{ + */ + +/** + * @defgroup CMOX_SM3 SM3 hash module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SM3_MACROS SM3 macros + * @{ + */ + +#define CMOX_SM3_SIZE 32U /*!< Maximum size in bytes of a SM3 digest */ + +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SM3_PUBLIC_TYPES SM3 module public types + * @{ + */ + +/** + * @brief SM3 handle type definition + */ +typedef cmox_mdSmall_handle_t cmox_sm3_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_SM3_PUBLIC_METHODS SM3 module public method prototypes + * @{ + */ + +/** + * @brief SM3 constructor + * + * @param P_pThis Pointer to the SM3 handle to initialize + * @return cmox_hash_handle_t* Pointer to a general hash handle. This will be + * used by the general purpose hash functions in order to perform + * the algorithm + */ +cmox_hash_handle_t *cmox_sm3_construct(cmox_sm3_handle_t *P_pThis); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_SM3_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/hash/cmox_sponge.h b/firmware/ui/dependencies/cmox/include/hash/cmox_sponge.h new file mode 100644 index 00000000..46580821 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/hash/cmox_sponge.h @@ -0,0 +1,85 @@ +/** + ****************************************************************************** + * @file cmox_sponge.h + * @author MCD Application Team + * @brief Header file for the Keccak sponge definitions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_SPONGE_H +#define CMOX_SPONGE_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_SHA3 + * @{ + */ + +/** + * @defgroup CMOX_SPONGE Sponge module + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** @defgroup CMOX_SPONGE_MACROS Sponge macros + * @{ + */ +#define CMOX_SHA3_STATE_SIZE 200U /*!< Keccak-P 1600 state size in bytes */ +/** + * @} + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_SPONGE_PUBLIC_TYPES Sponge module public types + * @{ + */ + +/** + * @brief Structure for Keccak-P 1600 sponge + */ +typedef struct +{ + uint8_t state[CMOX_SHA3_STATE_SIZE]; /*!< Internal state for Keccak P-1600 */ + uint32_t rate; /*!< Rate */ + uint32_t byteIOIndex; /*!< Index for the IO bytes */ + uint32_t squeezing; /*!< flag identifying if the operation is a squeezing */ +} cmox_sponge_handle_t; + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_SPONGE_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/mac/cmox_cmac.h b/firmware/ui/dependencies/cmox/include/mac/cmox_cmac.h new file mode 100644 index 00000000..0aeb3565 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/mac/cmox_cmac.h @@ -0,0 +1,169 @@ +/** + ****************************************************************************** + * @file cmox_cmac.h + * @author MCD Application Team + * @brief Header file for the CMAC algorithm definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_CMAC_H +#define CMOX_CMAC_H + +/* Include files -------------------------------------------------------------*/ +#include "cmox_mac.h" +#include "cipher/cmox_blockcipher.h" + +#include "cipher/cmox_check_default_aes.h" +#include "cmox_default_defs.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_MAC + * @{ + */ + +/** @defgroup CMOX_CMAC CMAC + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_CMAC_PUBLIC_TYPES CMAC public types + * @{ + */ + +/** + * @brief CMAC implementation + * + * This type specifies the used block cipher for the CMAC construct. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_cmac_implStruct_st *cmox_cmac_impl_t; + +/** + * @brief CMAC handle structure definition + */ +typedef struct +{ + cmox_mac_handle_t super; /*!< General MAC handle */ + cmox_blockcipher_handle_t blockCipher; /*!< Block cipher handle */ + uint32_t iv[CMOX_CIPHER_BLOCK_SIZE]; /*!< Buffer containing the Initialization Vector */ + uint8_t temp_buffer[16]; /*!< Temporary buffer to storing unprocessed data */ + uint32_t unprocessed_bytes; /*!< Number of bytes in the temporary buffer */ +} cmox_cmac_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_CMAC_PUBLIC_CONSTANTS CMAC public constants + * @{ + */ + +/** @defgroup CMOX_CMAC_IMPL CMAC implementations + * @{ + */ + +/** + * @brief Implementation of CMAC using AES (small implementation) + * (Defined internally) + */ +extern const cmox_cmac_impl_t CMOX_CMAC_AESSMALL; + +/** + * @brief Implementation of CMAC using AES (fast implementation) + * (Defined internally) + */ +extern const cmox_cmac_impl_t CMOX_CMAC_AESFAST; + +/** + * @} + */ + +/** @defgroup CMOX_CMAC_ALGO CMAC single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the CMAC using AES (fast implementation) + * for single-call function (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_CMAC_AESFAST_ALGO; + +/** + * @brief Identifier of the CMAC using AES (small implementation) + * for single-call function (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_CMAC_AESSMALL_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_CMAC_PUBLIC_METHODS CMAC public method prototypes + * @{ + */ + +/** + * @brief CMAC constructor + * + * The function is used for specifying which block cipher algorithm to use in + * order to implement the CMAC algorithm. + * + * @param P_pThis Pointer to the CMAC handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter shall be set with the following value: + * @arg CMOX_CMAC_AESFAST + * @arg CMOX_CMAC_AESSMALL + * @return cmox_mac_handle_t* Pointer to a general MAC handle. This will + * be used by the general purpose MAC functions in order to + * perform the algorithm + */ +cmox_mac_handle_t *cmox_cmac_construct(cmox_cmac_handle_t *P_pThis, + cmox_cmac_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_CMAC_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/mac/cmox_hmac.h b/firmware/ui/dependencies/cmox/include/mac/cmox_hmac.h new file mode 100644 index 00000000..55c823ec --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/mac/cmox_hmac.h @@ -0,0 +1,259 @@ +/** + ****************************************************************************** + * @file cmox_hmac.h + * @author MCD Application Team + * @brief Header file for the HMAC algorithm + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_HMAC_H +#define CMOX_HMAC_H + +/* Include files -------------------------------------------------------------*/ +#include "cmox_mac.h" +#include "hash/cmox_md.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_MAC + * @{ + */ + +/** @defgroup CMOX_HMAC HMAC + * @{ + */ + +/* Public macros -------------------------------------------------------------*/ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_HMAC_PUBLIC_TYPES HMAC public types + * @{ + */ + +/** + * @brief HMAC implementation + * + * This type specifies the used hash function for the HMAC construct. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_hmac_implStruct_st *cmox_hmac_impl_t; + +/** + * @brief HMAC internal support functions + * + * This type specifies some functions used internally by the HMAC in order to + * correctly compute the MAC. The structure pointed by the type is defined + * in the library internally + */ +typedef const struct cmox_hmac_supportStruct_st *cmox_hmac_support_t; + +/** + * @brief HMAC handle structure definition + */ +typedef struct +{ + cmox_mac_handle_t super; /*!< General MAC handle */ + + /** + * @brief Hash function handle + * + * Depending on the chosen hash function, one of the two fields of the union + * will be used. + */ + union + { + cmox_mdSmall_handle_t md_small; /*!< Handle for SHA-224, SHA-256, SM3 */ + cmox_mdLarge_handle_t md_large; /*!< Handle for SHA-384, SHA-512 */ + } hash_context; + + cmox_hash_handle_t *hash; /*!< Pointer to general hash handle */ + uint8_t key[128]; /*!< Buffer of bytes containing the derivated key */ + cmox_hmac_support_t support; /*!< Support functions for HMAC computation */ +} cmox_hmac_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_HMAC_PUBLIC_CONSTANTS HMAC public constants + * @{ + */ + +/** @defgroup CMOX_HMAC_IMPL HMAC implementations + * @{ + */ + +/** + * @brief Implementation of HMAC using SHA-1 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA1; + +/** + * @brief Implementation of HMAC using SHA-224 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA224; + +/** + * @brief Implementation of HMAC using SHA-256 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA256; + +/** + * @brief Implementation of HMAC using SHA-384 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA384; + +/** + * @brief Implementation of HMAC using SHA-512 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA512; + +/** + * @brief Implementation of HMAC using SHA-512/224 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA512_224; + +/** + * @brief Implementation of HMAC using SHA-512/256 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SHA512_256; + +/** + * @brief Implementation of HMAC using SM3 (Defined internally) + */ +extern const cmox_hmac_impl_t CMOX_HMAC_SM3; + + +/** + * @} + */ + +/** @defgroup CMOX_HMAC_ALGO HMAC single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the HMAC-SHA1 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA1_ALGO; + +/** + * @brief Identifier of the HMAC-SHA224 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA224_ALGO; + +/** + * @brief Identifier of the HMAC-SHA256 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA256_ALGO; + +/** + * @brief Identifier of the HMAC-SHA384 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA384_ALGO; + +/** + * @brief Identifier of the HMAC-SHA512 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA512_ALGO; + +/** + * @brief Identifier of the HMAC-SHA512/224 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA512_224_ALGO; + +/** + * @brief Identifier of the HMAC-SHA512/256 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SHA512_256_ALGO; + +/** + * @brief Identifier of the HMAC-SM3 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_HMAC_SM3_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_HMAC_PUBLIC_METHODS HMAC public method prototypes + * @{ + */ + +/** + * @brief HMAC constructor + * + * The function is used for specifying which hash function to use in + * order to implement the HMAC algorithm. + * + * @param P_pThis Pointer to the HMAC handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_HMAC_SHA1 + * @arg CMOX_HMAC_SHA224 + * @arg CMOX_HMAC_SHA256 + * @arg CMOX_HMAC_SHA384 + * @arg CMOX_HMAC_SHA512 + * @arg CMOX_HMAC_SHA512_224 + * @arg CMOX_HMAC_SHA512_256 + * @arg CMOX_HMAC_SM3 + * @return cmox_mac_handle_t* Pointer to a general MAC handle. This will + * be used by the general purpose MAC functions in order to + * perform the algorithm + */ +cmox_mac_handle_t *cmox_hmac_construct(cmox_hmac_handle_t *P_pThis, + cmox_hmac_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_HMAC_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/mac/cmox_kmac.h b/firmware/ui/dependencies/cmox/include/mac/cmox_kmac.h new file mode 100644 index 00000000..1f952444 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/mac/cmox_kmac.h @@ -0,0 +1,190 @@ +/** + ****************************************************************************** + * @file cmox_kmac.h + * @author MCD Application Team + * @brief Header file for the KMAC algorithm definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_KMAC_H +#define CMOX_KMAC_H + +/* Include files -------------------------------------------------------------*/ +#include "cmox_mac.h" +#include "hash/cmox_sponge.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_MAC + * @{ + */ + +/** @defgroup CMOX_KMAC KMAC + * @{ + */ + +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_KMAC_PUBLIC_TYPES KMAC public types + * @{ + */ + +/** + * @brief KMAC implementation + * + * This type specifies the implementation to use for the KMAC construct. + * This type is defined as a pointer to a structure, that contains the + * functions needed for the specific implementation, defined in the library + * internally + */ +typedef const struct cmox_kmac_implStruct_st *cmox_kmac_impl_t; + +/** + * @brief Keccak-P phases + */ +typedef uint32_t cmox_keccak_phase_t; + +/** + * @brief Structure defining the cSHAKE context + */ +typedef struct +{ + cmox_sponge_handle_t sponge; /*!< Sponge context */ + uint32_t fixedOutputLength; /*!< Fixed output length in bits */ + uint32_t lastByteBitLen; /*!< Last byte bit length */ + uint8_t lastByteValue; /*!< Last byte value */ + cmox_keccak_phase_t phase; /*!< Phase */ +} cmox_cshake_handle_t; + +/** + * @brief Structure defining the KMAC inner context + */ +typedef struct +{ + cmox_cshake_handle_t csi; /*!< cSHAKE context */ + size_t outputBitLen; /*!< Output length in bits */ +} cmox_kmac_inner_t; + + +/** + * @brief KMAC handle structure definition + */ +typedef struct +{ + cmox_mac_handle_t super; /*!< General MAC handle */ + const uint8_t *custom_data; /*!< Pointer to buffer containing custom data */ + size_t customDataLen; /*!< Size in bytes of the custom data */ + cmox_kmac_inner_t internal_ctx; /*!< Internal handle for KMAC */ + cmox_kmac_impl_t impl; /*!< Implementation of KMAC */ +} cmox_kmac_handle_t; + +/** + * @} + */ + +/* Public constants ----------------------------------------------------------*/ + +/** @defgroup CMOX_KMAC_PUBLIC_CONSTANTS KMAC public constants + * @{ + */ + +/** @defgroup CMOX_KMAC_IMPL KMAC implementations + * @{ + */ + +/** + * @brief Implementation of KMAC-128 algorithm (Defined internally) + */ +extern const cmox_kmac_impl_t CMOX_KMAC_128; + +/** + * @brief Implementation of KMAC-256 algorithm (Defined internally) + */ +extern const cmox_kmac_impl_t CMOX_KMAC_256; + +/** + * @} + */ + +/** @defgroup CMOX_KMAC_ALGO KMAC single-call algorithms + * @{ + */ + +/** + * @brief Identifier of the KMAC-128 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_KMAC_128_ALGO; + +/** + * @brief Identifier of the KMAC-256 mac algorithm for single-call function + * (Defined internally) + */ +extern const cmox_mac_algo_t CMOX_KMAC_256_ALGO; + +/** + * @} + */ + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_KMAC_PUBLIC_METHODS KMAC public method prototypes + * @{ + */ + +/** + * @brief KMAC constructor + * + * The function is used for specifying which KMAC implementation to use in + * order to implement the MAC algorithm. + * + * @param P_pThis Pointer to the KMAC handle to initialize + * @param P_impl Constant that specifies the implementation to use. + * This parameter can be one of the following values: + * @arg CMOX_KMAC_128 + * @arg CMOX_KMAC_256 + * @return cmox_mac_handle_t* Pointer to a general MAC handle. This will + * be used by the general purpose MAC functions in order to + * perform the algorithm + */ +cmox_mac_handle_t *cmox_kmac_construct(cmox_kmac_handle_t *P_pThis, + cmox_kmac_impl_t P_impl); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_KMAC_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/mac/cmox_mac.h b/firmware/ui/dependencies/cmox/include/mac/cmox_mac.h new file mode 100644 index 00000000..f83b6b75 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/mac/cmox_mac.h @@ -0,0 +1,284 @@ +/** + ****************************************************************************** + * @file cmox_mac.h + * @author MCD Application Team + * @brief Header file for the MAC module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_MAC_H +#define CMOX_MAC_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/* Include files -------------------------------------------------------------*/ +#include +#include +#include "cmox_mac_retvals.h" + +/** @addtogroup CMOX_MAC + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ +/* Public types --------------------------------------------------------------*/ + +/** @defgroup CMOX_MAC_PUBLIC_TYPES MAC module public types + * @{ + */ + +/** + * @brief MAC Virtual Table + * + * This type specifies a pointer to the virtual table containing the methods + * for a particular algorithm (e.g. HMAC-256 or CMAC) + */ +typedef const struct cmox_mac_vtableStruct_st *cmox_mac_vtable_t; + +/** + * @brief MAC algorithm type + * + * This type specifies the algorithm to use with the MAC module + * (e.g. HMAC-SHA256). The type is defined as a pointer to a structure, that + * contains the functions for the specific algorithm, defined in the library + * internally + */ +typedef const struct cmox_mac_algoStruct_st *cmox_mac_algo_t; + + +/** + * @brief MAC handle structure definition + */ +typedef struct +{ + cmox_mac_vtable_t table; /*!< MAC virtual table */ + size_t tagLen; /*!< Size in bytes of the authenticated tag */ + uint32_t internalState; /*!< MAC internal state */ +} cmox_mac_handle_t; + +/** + * @} + */ + +/* Public methods prototypes -------------------------------------------------*/ + +/** @defgroup CMOX_MAC_PUBLIC_METHODS MAC public method prototypes + * @{ + */ + +/** + * @brief Cleanup the MAC handler + * + * @param P_pThis MAC handler to cleanup + * @return cmox_mac_retval_t MAC return value + */ +cmox_mac_retval_t cmox_mac_cleanup(cmox_mac_handle_t *P_pThis); + +/** + * @brief Initialize the MAC handle based on the selected algorithm + * + * @param P_pThis MAC handle to initialize + * @return cmox_mac_retval_t Hash return value + * @note The MAC handle must be derived from an algorithm-specific handle + * using the correct construct + */ +cmox_mac_retval_t cmox_mac_init(cmox_mac_handle_t *P_pThis); + +/** + * @brief Set the size of the tag + * + * @param P_pThis MAC handle to set + * @param P_tagLen Size in bytes of the tag + * @return cmox_mac_retval_t MAC return value + * @note This function is optional when used for HMAC or CMAC algorithms. If not + * called, the tag length will be set as the default specified by the + * algorithm (e.g. 16 bytes for CMAC) during the initialization phase. + * @note With KMAC algorithm, this function is mandatory and must be called + * before the cmox_mac_setKey function + */ +cmox_mac_retval_t cmox_mac_setTagLen(cmox_mac_handle_t *P_pThis, + size_t P_tagLen); + +/** + * @brief Set the custom data to be combined with the plaintext for the MAC + * computation + * + * @param P_pThis MAC handle to use for computing the authenticated tag + * @param P_pCustomData Buffer of bytes containing the custom data + * @param P_customDataLen Size in bytes of the custom data + * @return cmox_mac_retval_t MAC return value + * @note This function is useful only for KMAC algorithm. It can be called for + * other algorithms but will not produce any results + * @note If the KMAC to compute doesn't need any custom data, this function can + * be skipped. + */ +cmox_mac_retval_t cmox_mac_setCustomData(cmox_mac_handle_t *P_pThis, + const uint8_t *P_pCustomData, + size_t P_customDataLen); + +/** + * @brief Set the key to be used for computing the authenticated tag + * + * @param P_pThis MAC handle to use for computing the authenticated tag + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Size in bytes of the key + * @return cmox_mac_retval_t MAC return value + * @note The KMAC algorithm is specified to be used even without key. However, + * this function is mandatory to be called with the parameter P_keyLen + * set to 0. + */ +cmox_mac_retval_t cmox_mac_setKey(cmox_mac_handle_t *P_pThis, + const uint8_t *P_pKey, + size_t P_keyLen); + +/** + * @brief Append part or the totality of the plaintext to the MAC handle + * + * @param P_pThis MAC handle to use for computing the authenticated tag + * @param P_pInput Buffer of bytes containing the data to append + * @param P_inputLen Size in bytes of the data to append + * @return cmox_hash_retval_t MAC return value + */ +cmox_mac_retval_t cmox_mac_append(cmox_mac_handle_t *P_pThis, + const uint8_t *P_pInput, + size_t P_inputLen); + +/** + * @brief Compute the authenticated tag of the already appended data + * + * @param P_pThis MAC handle where the data has been appended + * @param P_pTag Buffer of bytes where the authenticated tag will be stored + * @param P_pTagLen Number of bytes generated by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_mac_retval_t MAC return value + */ +cmox_mac_retval_t cmox_mac_generateTag(cmox_mac_handle_t *P_pThis, + uint8_t *P_pTag, + size_t *P_pTagLen); + +/** + * @brief Verify the already appended data with the reference tag + * + * @param P_pThis MAC handle where the data has been appended + * @param P_pTag Buffer of bytes containing the reference tag + * @param P_pFaultCheck Optional value to check, together with the retval, + * to verify if some fault happened + * @return cmox_mac_retval_t + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must + * be done against the success value, and not comparing the value with the + * failure value. Indeed, in presence of faults, especially P_pFaultCheck, + * could be a dirty value. + */ +cmox_mac_retval_t cmox_mac_verifyTag(cmox_mac_handle_t *P_pThis, + const uint8_t *P_pTag, + uint32_t *P_pFaultCheck); + +/** + * @brief Compute the authenticated tag of a message using a MAC algorithm + * + * @param P_algo Identifier of the hash algorithm to use for the computation. + * This parameter can be one of the following: + * @arg CMOX_CMAC_AES_ALGO + * @arg CMOX_HMAC_SHA1_ALGO + * @arg CMOX_HMAC_SHA224_ALGO + * @arg CMOX_HMAC_SHA256_ALGO + * @arg CMOX_HMAC_SHA384_ALGO + * @arg CMOX_HMAC_SHA512_ALGO + * @arg CMOX_HMAC_SHA512_224_ALGO + * @arg CMOX_HMAC_SHA512_256_ALGO + * @arg CMOX_HMAC_SM3_ALGO + * @arg CMOX_KMAC_128_ALGO + * @arg CMOX_KMAC_256_ALGO + * @param P_pInput Buffer of bytes containing the message to process + * @param P_inputLen Size in bytes of the message to process + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Size in bytes of the key + * @param P_pCustomData Buffer of bytes containing the custom data + * @param P_customDataLen Size in bytes of the custom data + * @param P_pTag Buffer of bytes where the authenticated tag will be stored + * @param P_expectedTagLen Size in bytes of the tag to compute + * @param P_pComputedTagLen Number of bytes generated by the function. + * It is an optional parameter and can be set to NULL if not needed. + * @return cmox_mac_retval_t + */ +cmox_mac_retval_t cmox_mac_compute(cmox_mac_algo_t P_algo, + const uint8_t *P_pInput, + size_t P_inputLen, + const uint8_t *P_pKey, + size_t P_keyLen, + const uint8_t *P_pCustomData, + size_t P_customDataLen, + uint8_t *P_pTag, + size_t P_expectedTagLen, + size_t *P_pComputedTagLen); + +/** + * @brief Verify the authenticity of a message using a MAC algorithm + * + * @param P_algo Identifier of the hash algorithm to use for the computation. + * This parameter can be one of the following: + * @arg CMOX_CMAC_AES_ALGO + * @arg CMOX_HMAC_SHA1_ALGO + * @arg CMOX_HMAC_SHA224_ALGO + * @arg CMOX_HMAC_SHA256_ALGO + * @arg CMOX_HMAC_SHA384_ALGO + * @arg CMOX_HMAC_SHA512_ALGO + * @arg CMOX_HMAC_SHA512_224_ALGO + * @arg CMOX_HMAC_SHA512_256_ALGO + * @arg CMOX_HMAC_SM3_ALGO + * @arg CMOX_KMAC_128_ALGO + * @arg CMOX_KMAC_256_ALGO + * @param P_pInput Buffer of bytes containing the message to process + * @param P_inputLen Size in bytes of the message to process + * @param P_pKey Buffer of bytes containing the key + * @param P_keyLen Size in bytes of the key + * @param P_pCustomData Buffer of bytes containing the custom data + * @param P_customDataLen Size in bytes of the custom data + * @param P_pReceivedTag Buffer of bytes containing the received tag + * @param P_receivedTagLen Size in bytes of the received tag + * @return cmox_mac_retval_t + */ +cmox_mac_retval_t cmox_mac_verify(cmox_mac_algo_t P_algo, + const uint8_t *P_pInput, + size_t P_inputLen, + const uint8_t *P_pKey, + size_t P_keyLen, + const uint8_t *P_pCustomData, + size_t P_customDataLen, + const uint8_t *P_pReceivedTag, + size_t P_receivedTagLen); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_MAC_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/mac/cmox_mac_retvals.h b/firmware/ui/dependencies/cmox/include/mac/cmox_mac_retvals.h new file mode 100644 index 00000000..e2dac17d --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/mac/cmox_mac_retvals.h @@ -0,0 +1,97 @@ +/** + ****************************************************************************** + * @file cmox_mac_retvals.h + * @author MCD Application Team + * @brief return values for MAC functionality + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef CMOX_MAC_RETVALS_H +#define CMOX_MAC_RETVALS_H + +/* Include files -------------------------------------------------------------*/ +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_MAC + * @{ + */ + +/** @defgroup CMOX_MAC_RETVALS MAC return values + * @{ + */ + +/* Macros --------------------------------------------------------------------*/ + +/** + * @brief MAC operation successfully performed + */ +#define CMOX_MAC_SUCCESS ((cmox_mac_retval_t)0x00030000U) + + +/** + * @brief Some error happens internally in the MAC module + */ +#define CMOX_MAC_ERR_INTERNAL ((cmox_mac_retval_t)0x00030001U) + +/** + * @brief One or more parameter has been wrongly passed to the function + * (e.g. pointer to NULL) + */ +#define CMOX_MAC_ERR_BAD_PARAMETER ((cmox_mac_retval_t)0x00030002U) + +/** + * @brief Error on performing the operation + * (e.g. an operation has been called before initializing the handle) + */ +#define CMOX_MAC_ERR_BAD_OPERATION ((cmox_mac_retval_t)0x00030003U) + +/** + * @brief Authentication of the tag has been successful + */ +#define CMOX_MAC_AUTH_SUCCESS ((cmox_mac_retval_t)0x0003C726U) + +/** + * @brief Authentication of the tag failed + */ +#define CMOX_MAC_AUTH_FAIL ((cmox_mac_retval_t)0x00036E93U) + +/* Public types --------------------------------------------------------------*/ + +/** + * @brief MAC module return value type + */ +typedef uint32_t cmox_mac_retval_t; + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_MAC_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa.h b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa.h new file mode 100644 index 00000000..8d1a46a4 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa.h @@ -0,0 +1,175 @@ +/** + ****************************************************************************** + * @file cmox_rsa.h + * @author MCD Application Team + * @brief This file provides common function for RSA module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_RSA_H +#define CMOX_RSA_H + +#include +#include + +#include "rsa/cmox_rsa_types.h" +#if !defined(CMOX_DEFAULT_FILE) +#include "cmox_default_config.h" +#else +#include CMOX_DEFAULT_FILE +#endif /* CMOX_DEFAULT_FILE */ + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_RSA + * @{ + */ + +/** + * @defgroup CMOX_RSA_PUBLIC_METHODS RSA public method prototypes + * @{ + */ + +/** + * @brief Mandatory function to set Low Level Mathematical Functions, and working memory buffer, for RSA + * @param[in,out] P_pRsaCtx Context for RSA operations + * @param[in] P_Math Structure pointer with the Low-level Mathematical functions + * This parameter can be one of the following: + * @arg CMOX_MATH_FUNCS_SMALL + * @arg CMOX_MATH_FUNCS_FAST + * @param[in] P_Modexp Structure pointer with the Modular Exponentiation function + * This parameter can be one of the following: + * @arg CMOX_MODEXP_PUBLIC + * @arg CMOX_MODEXP_PRIVATE_LOWMEM + * @arg CMOX_MODEXP_PRIVATE_MIDMEM + * @arg CMOX_MODEXP_PRIVATE_HIGHMEM + * @param[in] P_pBuf The preallocated static buffer that will be used + * @param[in] P_BufLen The size in bytes of the P_pBuf buffer + */ +void cmox_rsa_construct(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_math_funcs_t P_Math, + const cmox_modexp_func_t P_Modexp, + uint8_t *P_pBuf, + size_t P_BufLen); + +/** + * @brief Clean the RSA context and the internal temporary buffer + * @param[in,out] P_pRsaCtx Context for RSA operations + */ +void cmox_rsa_cleanup(cmox_rsa_handle_t *P_pRsaCtx); + +/** + * @brief Set the key (public or private) in the key structure + * @param[out] P_pKey Key to set + * @param[in] P_pModulus Modulus + * @param[in] P_ModulusLen Modulus Length (in Bytes) + * @param[in] P_pExp Exponent (private or public) + * @param[in] P_ExpLen Exponent Length (in Bytes) + * @retval CMOX_RSA_SUCCESS + * @retval CMOX_RSA_ERR_BAD_PARAMETER + */ +cmox_rsa_retval_t cmox_rsa_setKey(cmox_rsa_key_t *P_pKey, + const uint8_t *P_pModulus, + size_t P_ModulusLen, + const uint8_t *P_pExp, + size_t P_ExpLen); + +/** + * @brief Set the private CRT key in the key structure + * @param[out] P_pPrivKey Private Key to set + * @param[in] P_ModulusBitLen Modulus Length (in bits) + * @param[in] P_pExpP Secret exponent (mod P) dP + * @param[in] P_ExpPLen dP Length (in Bytes) + * @param[in] P_pExpQ Secret exponent (mod Q) dQ + * @param[in] P_ExpQLen dQ Length (in Bytes) + * @param[in] P_pP First secret prime P + * @param[in] P_PLen P Length (in Bytes) + * @param[in] P_pQ Second secret prime Q + * @param[in] P_QLen Q Length (in Bytes) + * @param[in] P_pIq Inverse of Q (mod P) invQ + * @param[in] P_IqLen invQ Length (in Bytes) + * @retval CMOX_RSA_SUCCESS + * @retval CMOX_RSA_ERR_BAD_PARAMETER + */ +cmox_rsa_retval_t cmox_rsa_setKeyCRT(cmox_rsa_key_t *P_pPrivKey, + size_t P_ModulusBitLen, + const uint8_t *P_pExpP, + size_t P_ExpPLen, + const uint8_t *P_pExpQ, + size_t P_ExpQLen, + const uint8_t *P_pP, + size_t P_PLen, + const uint8_t *P_pQ, + size_t P_QLen, + const uint8_t *P_pIq, + size_t P_IqLen); + +/** + * @brief Set the private CRT key in the key structure, enabling the Fault Attacks Countermeasure. + * @param[out] P_pPrivKey Private Key to set + * @param[in] P_ModulusBitLen Modulus Length (in bits) + * @param[in] P_pExpP Secret exponent (mod P) dP + * @param[in] P_ExpPLen dP Length (in Bytes) + * @param[in] P_pExpQ Secret exponent (mod Q) dQ + * @param[in] P_ExpQLen dQ Length (in Bytes) + * @param[in] P_pP First secret prime P + * @param[in] P_PLen P Length (in Bytes) + * @param[in] P_pQ Second secret prime Q + * @param[in] P_QLen Q Length (in Bytes) + * @param[in] P_pIq Inverse of Q (mod P) invQ + * @param[in] P_IqLen invQ Length (in Bytes) + * @param[in] P_pPubKey Public Key + * @param[in] P_PubKeyLen Public Key Length (in Bytes) + * @retval CMOX_RSA_SUCCESS + * @retval CMOX_RSA_ERR_BAD_PARAMETER + * @note This function enables the protection against the Bellcore attack for RSA-CRT computation. + * This countermeasure is useful to protect RSA-CRT using PKCS#1 v1.5 for Signature Creation. + * Indeed, PKCS#1 v2.2 Signature Creation is not vulnerable because of the presence of an input random, + * and Decryption processes (both for PKCS#1 v1.5 and v2.2) are not vulnerable because the plaintext + * is not returned if not correct (therefore not exploitable by an attacker). + */ +cmox_rsa_retval_t cmox_rsa_setKeyCRTwithFACM(cmox_rsa_key_t *P_pPrivKey, + size_t P_ModulusBitLen, + const uint8_t *P_pExpP, + size_t P_ExpPLen, + const uint8_t *P_pExpQ, + size_t P_ExpQLen, + const uint8_t *P_pP, + size_t P_PLen, + const uint8_t *P_pQ, + size_t P_QLen, + const uint8_t *P_pIq, + size_t P_IqLen, + const uint8_t *P_pPubKey, + size_t P_PubKeyLen); + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_RSA_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_pkcs1v15.h b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_pkcs1v15.h new file mode 100644 index 00000000..d994d9eb --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_pkcs1v15.h @@ -0,0 +1,206 @@ +/** + ****************************************************************************** + * @file cmox_rsa_pkcs1v15.h + * @author MCD Application Team + * @brief Header file for RSA PKCS#1 v1.5 definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_RSA_PKCS1V15_H +#define CMOX_RSA_PKCS1V15_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +#include "rsa/cmox_rsa.h" +#include "utils/cmox_utils_retvals.h" + +/** @addtogroup CMOX_RSA + * @{ + */ + +/** + * @defgroup CMOX_RSA_PKCS1V15 RSA PKCS#1 v1.5 standard + * @{ + */ + +/** + * @brief Hash algorithms to use with RSA PKCS#1 API + * @defgroup CMOX_RSA_PKCS1V15_HASH Hash functions to use in RSA PKCS#1 v1.5 + * @{ + */ + +/** + * @brief SHA1 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA1; +/** + * @brief SHA224 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA224; +/** + * @brief SHA256 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA256; +/** + * @brief SHA384 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA384; +/** + * @brief SHA512 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA512; +/** + * @brief SHA512/224 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA512_224; +/** + * @brief SHA512/256 Hash structure for the RSA PKCS#1 v1.5 functions + */ +extern const cmox_rsa_pkcs1v15_hash_t CMOX_RSA_PKCS1V15_HASH_SHA512_256; + +/** + * @} + */ + +/** + * @defgroup CMOX_RSA_PKCS1V15_PUBLIC_METHODS RSA PKCS#1 v1.5 public method prototypes + * @{ + */ + +/** + * @brief Sign a message using PKCS#1 v1.5 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPrivKey Private Key (standard or CRT) + * @param[in] P_pDigest Message to sign + * @param[in] P_HashId Hash to use + * @param[out] P_pSignature Output signature + * @param[out] P_pSignatureLen Output signature Length (in Bytes) + * @retval CMOX_RSA_SUCCESS Everything OK + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_MODULUS_TOO_SHORT Modulus too short for the message to sign + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v15_sign(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPrivKey, + const uint8_t *P_pDigest, + const cmox_rsa_pkcs1v15_hash_t P_HashId, + uint8_t *P_pSignature, + size_t *P_pSignatureLen); + +/** + * @brief Verify a message signed with PKCS#1 v1.5 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPubKey Public Key + * @param[in] P_pDigest Message to verify + * @param[in] P_HashId Hash to use + * @param[in] P_pSignature Signature + * @param[in] P_SignatureLen Signature Length (in Bytes) + * @param[out] P_pFaultCheck Optional value to check, together with the retval, to verify if some fault happened + * @retval CMOX_RSA_AUTH_SUCCESS Signature verified + * @retval CMOX_RSA_AUTH_FAIL Signature NOT verified + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_INVALID_SIGNATURE Input signature corrupted or not in the expected format + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_MODULUS_TOO_SHORT Modulus too short for the message to verify + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must be done against + * the success value, and not comparing the value with the failure value. Indeed, in presence of faults, + * especially P_pFaultCheck, could be a dirty value. + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v15_verify(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPubKey, + const uint8_t *P_pDigest, + const cmox_rsa_pkcs1v15_hash_t P_HashId, + const uint8_t *P_pSignature, + size_t P_SignatureLen, + uint32_t *P_pFaultCheck); + +/** + * @brief Encrypt a message using PKCS#1 v1.5 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPubKey Public Key + * @param[in] P_pInput Message to encrypt + * @param[in] P_InputLen Message Length (in Bytes) + * @param[in] P_pRandom Random buffer + * @param[in] P_RandomLen Random Length (in Bytes) + * @param[out] P_pOutput Output encrypted buffer + * @param[out] P_pOutputLen Output Length (in Bytes) + * @retval CMOX_RSA_SUCCESS Everything OK + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_MODULUS_TOO_SHORT Modulus too short for the message to encrypt + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_WRONG_RANDOM Random material too short or not valid for the functionality + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v15_encrypt(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPubKey, + const uint8_t *P_pInput, + size_t P_InputLen, + const uint8_t *P_pRandom, + size_t P_RandomLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @brief Decrypt a message using PKCS#1 v1.5 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPrivKey Private Key (standard or CRT) + * @param[in] P_pInput Message to decrypt + * @param[in] P_InputLen Message Length (in Bytes) + * @param[out] P_pOutput Output decrypted buffer + * @param[out] P_pOutputLen Output Length (in Bytes) + * @retval CMOX_RSA_SUCCESS Everything OK + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_WRONG_DECRYPTION Decryption failed, probably due to a wrong private key + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v15_decrypt(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPrivKey, + const uint8_t *P_pInput, + size_t P_InputLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_RSA_PKCS1V15_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_pkcs1v22.h b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_pkcs1v22.h new file mode 100644 index 00000000..ae868633 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_pkcs1v22.h @@ -0,0 +1,226 @@ +/** + ****************************************************************************** + * @file cmox_rsa_pkcs1v22.h + * @author MCD Application Team + * @brief Header file for RSA PKCS#1 v2.2 definitions and functions + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_RSA_PKCS1V22_H +#define CMOX_RSA_PKCS1V22_H + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +#include "rsa/cmox_rsa.h" +#include "utils/cmox_utils_retvals.h" + +/** @addtogroup CMOX_RSA + * @{ + */ + +/** + * @defgroup CMOX_RSA_PKCS1V22 RSA PKCS#1 v2.2 standard + * @{ + */ + +/** + * @brief Hash algorithms to use with RSA PKCS#1 API + * @defgroup CMOX_RSA_PKCS1V22_HASH Hash functions to use in RSA PKCS#1 v2.2 + * @{ + */ + +/** + * @brief SHA1 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA1; +/** + * @brief SHA224 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA224; +/** + * @brief SHA256 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA256; +/** + * @brief SHA384 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA384; +/** + * @brief SHA512 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA512; +/** + * @brief SHA512/224 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA512_224; +/** + * @brief SHA512/256 Hash structure for the RSA PKCS#1 v2.2 functions + */ +extern const cmox_rsa_pkcs1v22_hash_t CMOX_RSA_PKCS1V22_HASH_SHA512_256; + +/** + * @} + */ + +/** + * @defgroup CMOX_RSA_PKCS1V22_PUBLIC_METHODS RSA PKCS#1 v2.2 public method prototypes + * @{ + */ + +/** + * @brief Sign a message using PKCS#1 v2.2 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPrivKey Private Key (standard or CRT) + * @param[in] P_pDigest Message to sign + * @param[in] P_HashId Hash to use + * @param[in] P_pRandom Random buffer + * @param[in] P_RandomLen Random Length (in Bytes) + * @param[out] P_pSignature Output signature + * @param[out] P_pSignatureLen Output signature Length (in Bytes) + * @retval CMOX_RSA_SUCCESS Everything OK + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_MODULUS_TOO_SHORT Modulus too short for the message to sign + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v22_sign(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPrivKey, + const uint8_t *P_pDigest, + const cmox_rsa_pkcs1v22_hash_t P_HashId, + const uint8_t *P_pRandom, + size_t P_RandomLen, + uint8_t *P_pSignature, + size_t *P_pSignatureLen); + +/** + * @brief Verify a message signed with PKCS#1 v2.2 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPubKey Public Key + * @param[in] P_pDigest Message to verify + * @param[in] P_HashId Hash to use + * @param[in] P_RandomLen Random Length (in Bytes) + * @param[in] P_pSignature Signature + * @param[in] P_SignatureLen Signature Length (in Bytes) + * @param[out] P_pFaultCheck Optional value to check, together with the retval, to verify if some fault happened + * @retval CMOX_RSA_AUTH_SUCCESS Signature verified + * @retval CMOX_RSA_AUTH_FAIL Signature NOT verified + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_INVALID_SIGNATURE Input signature corrupted or not in the expected format + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must be done against + * the success value, and not comparing the value with the failure value. Indeed, in presence of faults, + * especially P_pFaultCheck, could be a dirty value. + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v22_verify(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPubKey, + const uint8_t *P_pDigest, + const cmox_rsa_pkcs1v22_hash_t P_HashId, + size_t P_RandomLen, + const uint8_t *P_pSignature, + size_t P_SignatureLen, + uint32_t *P_pFaultCheck); + +/** + * @brief Encrypt a message using PKCS#1 v2.2 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPubKey Public Key + * @param[in] P_pInput Message to encrypt + * @param[in] P_InputLen Message Length (in Bytes) + * @param[in] P_HashId Hash to use + * @param[in] P_pRandom Random buffer + * @param[in] P_RandomLen Random Length (in Bytes) + * @param[in] P_pLabel Label (Optional) + * @param[in] P_LabelLen Label Length (in Bytes) + * @param[out] P_pOutput Output encrypted buffer + * @param[out] P_pOutputLen Output Length (in Bytes) + * @retval CMOX_RSA_SUCCESS Everything OK + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_MODULUS_TOO_SHORT Modulus too short for the message to encrypt + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v22_encrypt(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPubKey, + const uint8_t *P_pInput, + size_t P_InputLen, + const cmox_rsa_pkcs1v22_hash_t P_HashId, + const uint8_t *P_pRandom, + size_t P_RandomLen, + const uint8_t *P_pLabel, + size_t P_LabelLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @brief Decrypt a message using PKCS#1 v2.2 + * @param[in] P_pRsaCtx Context for RSA operations + * @param[in] P_pPrivKey Private Key (standard or CRT) + * @param[in] P_pInput Message to decrypt + * @param[in] P_InputLen Message Length (in Bytes) + * @param[in] P_HashId Hash to use + * @param[in] P_pLabel Label (Optional) + * @param[in] P_LabelLen Label Length (in Bytes) + * @param[out] P_pOutput Output decrypted buffer + * @param[out] P_pOutputLen Output Length (in Bytes) + * @retval CMOX_RSA_SUCCESS Everything OK + * @retval CMOX_RSA_ERR_MATH_ALGO_MISMATCH Mathematical function set is incompatible with current functionality + * @retval CMOX_RSA_ERR_MEXP_ALGO_MISMATCH Modexp function set is not compatible with current functionality + * @retval CMOX_RSA_ERR_BAD_PARAMETER Some NULL/wrong/empty parameter + * @retval CMOX_RSA_ERR_MEMORY_FAIL Not enough memory + * @retval CMOX_RSA_ERR_INTERNAL Something went wrong during internal computations (e.g. hash) + * @retval CMOX_RSA_ERR_WRONG_DECRYPTION Decryption failed, probably due to a wrong private key + * @retval CMOX_RSA_ERR_MODULUS_TOO_SHORT Modulus too short for the message to decrypt + */ +cmox_rsa_retval_t cmox_rsa_pkcs1v22_decrypt(cmox_rsa_handle_t *P_pRsaCtx, + const cmox_rsa_key_t *P_pPrivKey, + const uint8_t *P_pInput, + size_t P_InputLen, + const cmox_rsa_pkcs1v22_hash_t P_HashId, + const uint8_t *P_pLabel, + size_t P_LabelLen, + uint8_t *P_pOutput, + size_t *P_pOutputLen); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_RSA_PKCS1V22_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_retvals.h b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_retvals.h new file mode 100644 index 00000000..ee41f738 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_retvals.h @@ -0,0 +1,73 @@ +/** + ****************************************************************************** + * @file cmox_rsa_retvals.h + * @author MCD Application Team + * @brief This file provides the error types and code for RSA modules + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_RSA_RETVALS_H +#define CMOX_RSA_RETVALS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_RSA + * @{ + */ + +/** + * @defgroup CMOX_RSA_RETVALS RSA return values + * @{ + */ + +/** + * @brief Return value type for RSA module + */ +typedef uint32_t cmox_rsa_retval_t; + +#define CMOX_RSA_SUCCESS ((cmox_rsa_retval_t)0x00050000) /*!< RSA operation successfully performed */ +#define CMOX_RSA_ERR_INTERNAL ((cmox_rsa_retval_t)0x00050001) /*!< Internal computat. error (e.g. hash) */ +#define CMOX_RSA_ERR_BAD_PARAMETER ((cmox_rsa_retval_t)0x00050003) /*!< One of the expected parameter is invalid */ +#define CMOX_RSA_ERR_MODULUS_TOO_SHORT ((cmox_rsa_retval_t)0x00050007) /*!< Input too long for the current modulus */ +#define CMOX_RSA_ERR_INVALID_SIGNATURE ((cmox_rsa_retval_t)0x00050009) /*!< RSA invalid signature value */ +#define CMOX_RSA_ERR_WRONG_DECRYPTION ((cmox_rsa_retval_t)0x0005000A) /*!< RSA invalid decryption, due to mismatch between private key and input */ +#define CMOX_RSA_ERR_WRONG_RANDOM ((cmox_rsa_retval_t)0x0005000B) /*!< Random not compliant with the API (Recall with other random material) */ +#define CMOX_RSA_ERR_MEMORY_FAIL ((cmox_rsa_retval_t)0x0005000C) /*!< Not enough memory */ +#define CMOX_RSA_ERR_MATH_ALGO_MISMATCH ((cmox_rsa_retval_t)0x00050010) /*!< Math customization not supported by current functionality */ +#define CMOX_RSA_ERR_MEXP_ALGO_MISMATCH ((cmox_rsa_retval_t)0x00050011) /*!< Modexp function not supported by current functionality */ +#define CMOX_RSA_AUTH_SUCCESS ((cmox_rsa_retval_t)0x0005C726) /*!< RSA signature successful verification */ +#define CMOX_RSA_AUTH_FAIL ((cmox_rsa_retval_t)0x00056E93) /*!< RSA signature not verified */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_RSA_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_types.h b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_types.h new file mode 100644 index 00000000..f70949ed --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/rsa/cmox_rsa_types.h @@ -0,0 +1,118 @@ +/** + ****************************************************************************** + * @file cmox_rsa_types.h + * @author MCD Application Team + * @brief This file provides the types used within the RSA module + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_RSA_TYPES_H +#define CMOX_RSA_TYPES_H + +#include +#include +#include "cmox_common.h" +#include "rsa/cmox_rsa_retvals.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + + +/** + * @brief Structure for the RSA context + */ +typedef struct +{ + cmox_membuf_handle_st membuf_str; /*!< Memory buffer structure */ + cmox_modexp_func_t modexp_ptr; /*!< Modexp customization structure */ + cmox_math_funcs_t math_ptr; /*!< Math customization structure */ + uint32_t magic_num_check; /*!< Magic number for diagnostic checks */ +} cmox_rsa_handle_t; + +/** + * @brief Internal structure type for the RSA modular exponentiation function + */ +struct cmox_rsa_intfuncStruct_t; +/** + * @brief Public pointer type for the RSA modular exponentiation function + */ +typedef struct cmox_rsa_intfuncStruct_t *cmox_rsa_intfunc_t; + +/** + * @brief Internal structure type for the Fault Attacks countermeasure + */ +struct cmox_rsa_facmStruct_t; +/** + * @brief Public pointer type for the Fault Attacks countermeasure + */ +typedef const struct cmox_rsa_facmStruct_t *cmox_rsa_facm_t; + +/** + * @brief Structure to hold public or private key parameters + */ +typedef struct +{ + cmox_rsa_intfunc_t f; /*!< Function executing CRT/standard modexp */ + size_t mod_bitlen; /*!< Length (in bytes) of Modulus */ + + union + { + /* Standard public/private fields */ + struct + { + const uint8_t *mod; /*!< Modulus */ + const uint8_t *exp; /*!< Public/secret Exponent */ + size_t exp_len; /*!< Length (in bytes) of Exponent */ + } std; + + /* CRT fields */ + struct + { + const uint8_t *p; /*!< Parameter P (in case of CRT) or normal modulus */ + size_t p_len; /*!< Length (in bytes) of P/Modulus */ + const uint8_t *q; /*!< Parameter Q */ + size_t q_len; /*!< Length (in bytes) of Q */ + const uint8_t *dp; /*!< Secret exponent (mod P) (in case of CRT), or normal secret exponent */ + size_t dp_len; /*!< Length (in bytes) of dP/Exponent */ + const uint8_t *dq; /*!< Secret exponent (mod Q) */ + size_t dq_len; /*!< Length (in bytes) of dQ */ + const uint8_t *iq; /*!< Q^(-1) (mod P) */ + size_t iq_len; /*!< Length (in bytes) of iQ */ + const uint8_t *pub_exp; /*!< public exponent */ + size_t pub_exp_len;/*!< Length (in bytes) of the public exponent */ + cmox_rsa_facm_t facm_flag; /*!< Parameter to enable/disable the countermeasure to the Fault Attacks */ + } crt; + } fields; /*!< Union for STD or CRT key structure */ +} cmox_rsa_key_t; + +/** + * @brief Pointer type linked to the structure to hold Hash information for PKCS#1 v1.5 + */ +typedef const struct cmox_rsa_pkcs1v15_hash_st *cmox_rsa_pkcs1v15_hash_t; + +/** + * @brief Pointer type linked to the structure to hold Hash information for PKCS#1 v2.2 + */ +typedef const struct cmox_rsa_pkcs1v22_hash_st *cmox_rsa_pkcs1v22_hash_t; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_RSA_TYPES_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/utils/cmox_utils_compare.h b/firmware/ui/dependencies/cmox/include/utils/cmox_utils_compare.h new file mode 100644 index 00000000..f95bf456 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/utils/cmox_utils_compare.h @@ -0,0 +1,69 @@ +/** + ****************************************************************************** + * @file cmox_utils_compare.h + * @author MCD Application Team + * @brief Header file for public comparison function and utilities + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_UTILS_COMPARE_H +#define CMOX_UTILS_COMPARE_H + +#include "utils/cmox_utils_retvals.h" + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** @addtogroup CMOX_UTILS + * @{ + */ + +/** + * @brief Compares two buffers in a fault-secure way + * @param[in] P_pBuf1 First buffer + * @param[in] P_Len1 Length of the first input buffer + * @param[in] P_pBuf2 Second buffer + * @param[in] P_Len2 Length of the second input buffer + * @param[out] P_pFaultCheck Optional value to check, together with the retval, to verify if some fault happened + * @retval CMOX_UTILS_AUTH_SUCCESS The two buffers are equal + * @retval CMOX_UTILS_AUTH_FAIL The two buffers are different + * @warning If it's necessary to be protected against fault attacks, it's very important that P_Len1 and P_Len2 + * are different memory locations (or registers), otherwise a single fault could be fatal. + * @note P_pFaultCheck value, if the parameter is provided, MUST be checked to + * be equal to the retval, and both MUST be equal to the successful value. + * P_pFaultCheck MUST be checked only if the main result is successful, + * and has no relevance if the main result is not successful. + * Every comparison (both for the return value and for P_pFaultCheck) must be done against + * the success value, and not comparing the value with the failure value. Indeed, in presence of faults, + * especially P_pFaultCheck, could be a dirty value. + */ +cmox_utils_retval_t cmox_utils_compare(const uint8_t *P_pBuf1, + uint32_t P_Len1, + const uint8_t *P_pBuf2, + uint32_t P_Len2, + uint32_t *P_pFaultCheck); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CMOX_UTILS_COMPARE_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/include/utils/cmox_utils_retvals.h b/firmware/ui/dependencies/cmox/include/utils/cmox_utils_retvals.h new file mode 100644 index 00000000..79088b99 --- /dev/null +++ b/firmware/ui/dependencies/cmox/include/utils/cmox_utils_retvals.h @@ -0,0 +1,62 @@ +/** + ****************************************************************************** + * @file cmox_utils_retvals.h + * @author MCD Application Team + * @brief This file provides the error types and code for UTILS modules + ****************************************************************************** + * @attention + * + * Copyright (c) 2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +#ifndef CMOX_UTILS_RETVALS_H +#define CMOX_UTILS_RETVALS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + +/** + * @addtogroup CMOX_UTILS + * @{ + */ + +/** @defgroup CMOX_UTILS_RETVALS Utils return values + * @{ + */ + +/** + * Return value type for the UTILS module + */ +typedef uint32_t cmox_utils_retval_t; + +#define CMOX_UTILS_AUTH_SUCCESS ((cmox_utils_retval_t)0x0007C726) /*!< UTILS operation successfully performed */ +#define CMOX_UTILS_AUTH_FAIL ((cmox_utils_retval_t)0x00076E93) /*!< Input buffers are different */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* CMOX_UTILS_RETVALS_H */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/ui/dependencies/cmox/lib/libSTM32Cryptographic_CM4.a b/firmware/ui/dependencies/cmox/lib/libSTM32Cryptographic_CM4.a new file mode 100644 index 00000000..24369751 Binary files /dev/null and b/firmware/ui/dependencies/cmox/lib/libSTM32Cryptographic_CM4.a differ diff --git a/firmware/ui/dependencies/sframe b/firmware/ui/dependencies/sframe new file mode 160000 index 00000000..9df6dda1 --- /dev/null +++ b/firmware/ui/dependencies/sframe @@ -0,0 +1 @@ +Subproject commit 9df6dda186a138820afecefa3a6d3dac84d4c5ac diff --git a/firmware/ui/inc/app_main.hh b/firmware/ui/inc/app_main.hh index b1d81977..bd16e19d 100644 --- a/firmware/ui/inc/app_main.hh +++ b/firmware/ui/inc/app_main.hh @@ -45,8 +45,8 @@ inline void LEDR(GPIO_PinState r); inline void LEDG(GPIO_PinState g); inline void LEDB(GPIO_PinState b); inline void LEDS(GPIO_PinState r, GPIO_PinState g, GPIO_PinState b); -inline void RaiseFlag(Timer_Flags flag); -inline void LowerFlag(Timer_Flags flag); +inline void RaiseFlag(enum Timer_Flags flag); +inline void LowerFlag(enum Timer_Flags flag); inline void LowPowerMode(); inline void WakeUp(); inline void CheckFlags(); diff --git a/firmware/ui/inc/gsl/gsl-lite.hpp b/firmware/ui/inc/gsl/gsl-lite.hpp deleted file mode 100644 index 859f9ccc..00000000 --- a/firmware/ui/inc/gsl/gsl-lite.hpp +++ /dev/null @@ -1,4156 +0,0 @@ -// -// gsl-lite is based on GSL: Guidelines Support Library. -// For more information see https://github.com/gsl-lite/gsl-lite -// -// Copyright (c) 2015-2018 Martin Moene -// Copyright (c) 2015-2018 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// 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 GSL_GSL_LITE_HPP_INCLUDED -#define GSL_GSL_LITE_HPP_INCLUDED - -#include // for swap() [pre-C++11], equal(), lexicographical_compare() -#include // for exception, terminate(), uncaught_exceptions() -#include // for data(), size(), reverse_iterator<>, iterator_traits<> -#include -#include // for addressof(), unique_ptr<>, shared_ptr<> -#include // for basic_ostream<> -#include // for ios_base, streamsize -#include // for logic_error -#include -#include // for move(), forward<>(), swap() -#include // for size_t, ptrdiff_t, nullptr_t - -#define gsl_lite_MAJOR 0 -#define gsl_lite_MINOR 37 -#define gsl_lite_PATCH 0 - -#define gsl_lite_VERSION gsl_STRINGIFY(gsl_lite_MAJOR) "." gsl_STRINGIFY(gsl_lite_MINOR) "." gsl_STRINGIFY(gsl_lite_PATCH) - -// gsl-lite backward compatibility: - -#if !defined( gsl_CONFIG_DEFAULTS_VERSION ) -# define gsl_CONFIG_DEFAULTS_VERSION gsl_lite_MAJOR -#endif - -#ifdef gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR -# define gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR -# pragma message ("gsl_CONFIG_ALLOWS_SPAN_CONTAINER_CTOR is deprecated since gsl-lite 0.7; replace with gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR, or consider span(with_container, cont).") -#endif - -#if defined( gsl_CONFIG_CONTRACT_LEVEL_ON ) -# pragma message ("gsl_CONFIG_CONTRACT_LEVEL_ON is deprecated since gsl-lite 0.36; replace with gsl_CONFIG_CONTRACT_CHECKING_ON.") -# define gsl_CONFIG_CONTRACT_CHECKING_ON -#endif -#if defined( gsl_CONFIG_CONTRACT_LEVEL_OFF ) -# pragma message ("gsl_CONFIG_CONTRACT_LEVEL_OFF is deprecated since gsl-lite 0.36; replace with gsl_CONFIG_CONTRACT_CHECKING_OFF.") -# define gsl_CONFIG_CONTRACT_CHECKING_OFF -#endif -#if defined( gsl_CONFIG_CONTRACT_LEVEL_EXPECTS_ONLY ) -# pragma message ("gsl_CONFIG_CONTRACT_LEVEL_EXPECTS_ONLY is deprecated since gsl-lite 0.36; replace with gsl_CONFIG_CONTRACT_CHECKING_ENSURES_OFF.") -# define gsl_CONFIG_CONTRACT_CHECKING_ON -# define gsl_CONFIG_CONTRACT_CHECKING_ENSURES_OFF -#elif defined( gsl_CONFIG_CONTRACT_LEVEL_ENSURES_ONLY ) -# pragma message ("gsl_CONFIG_CONTRACT_LEVEL_ENSURES_ONLY is deprecated since gsl-lite 0.36; replace with gsl_CONFIG_CONTRACT_CHECKING_EXPECTS_OFF.") -# define gsl_CONFIG_CONTRACT_CHECKING_ON -# define gsl_CONFIG_CONTRACT_CHECKING_EXPECTS_OFF -#endif - -// M-GSL compatibility: - -#if defined( GSL_THROW_ON_CONTRACT_VIOLATION ) -# define gsl_CONFIG_CONTRACT_VIOLATION_THROWS -#endif - -#if defined( GSL_TERMINATE_ON_CONTRACT_VIOLATION ) -# define gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES -#endif - -#if defined( GSL_UNENFORCED_ON_CONTRACT_VIOLATION ) -# define gsl_CONFIG_CONTRACT_CHECKING_OFF -#endif - -// Configuration: Features - -#ifndef gsl_FEATURE_WITH_CONTAINER_TO_STD -# define gsl_FEATURE_WITH_CONTAINER_TO_STD 99 -#endif - -#ifndef gsl_FEATURE_MAKE_SPAN_TO_STD -# define gsl_FEATURE_MAKE_SPAN_TO_STD 99 -#endif - -#ifndef gsl_FEATURE_BYTE_SPAN_TO_STD -# define gsl_FEATURE_BYTE_SPAN_TO_STD 99 -#endif - -#ifndef gsl_FEATURE_IMPLICIT_MACRO -# define gsl_FEATURE_IMPLICIT_MACRO 0 -#endif - -#ifndef gsl_FEATURE_OWNER_MACRO -# define gsl_FEATURE_OWNER_MACRO (gsl_CONFIG_DEFAULTS_VERSION == 0) -#endif - -#ifndef gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD -# define gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD 0 -#endif - -#ifndef gsl_FEATURE_GSL_LITE_NAMESPACE -# define gsl_FEATURE_GSL_LITE_NAMESPACE (gsl_CONFIG_DEFAULTS_VERSION >= 1) -#endif - -// Configuration: Other - -#if defined( gsl_CONFIG_TRANSPARENT_NOT_NULL ) && gsl_CONFIG_TRANSPARENT_NOT_NULL && defined( gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF ) -# error configuration option gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF is meaningless if gsl_CONFIG_TRANSPARENT_NOT_NULL=1 -#endif - -#ifndef gsl_CONFIG_DEPRECATE_TO_LEVEL -# if gsl_CONFIG_DEFAULTS_VERSION >= 1 -# define gsl_CONFIG_DEPRECATE_TO_LEVEL 6 -# else -# define gsl_CONFIG_DEPRECATE_TO_LEVEL 0 -# endif -#endif - -#ifndef gsl_CONFIG_SPAN_INDEX_TYPE -# define gsl_CONFIG_SPAN_INDEX_TYPE std::size_t -#endif - -#ifndef gsl_CONFIG_INDEX_TYPE -# if gsl_CONFIG_DEFAULTS_VERSION >= 1 -// p0122r3 uses std::ptrdiff_t -# define gsl_CONFIG_INDEX_TYPE std::ptrdiff_t -# else -# define gsl_CONFIG_INDEX_TYPE gsl_CONFIG_SPAN_INDEX_TYPE -# endif -#endif - -#ifndef gsl_CONFIG_NOT_NULL_EXPLICIT_CTOR -# define gsl_CONFIG_NOT_NULL_EXPLICIT_CTOR (gsl_CONFIG_DEFAULTS_VERSION >= 1) -#endif - -#ifndef gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF -# define gsl_CONFIG_NOT_NULL_GET_BY_CONST_REF 0 -#endif - -#ifndef gsl_CONFIG_TRANSPARENT_NOT_NULL -# define gsl_CONFIG_TRANSPARENT_NOT_NULL (gsl_CONFIG_DEFAULTS_VERSION >= 1) -#endif - -#ifndef gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS -# define gsl_CONFIG_CONFIRMS_COMPILATION_ERRORS 0 -#endif - -#ifndef gsl_CONFIG_ALLOWS_SPAN_COMPARISON -# define gsl_CONFIG_ALLOWS_SPAN_COMPARISON (gsl_CONFIG_DEFAULTS_VERSION == 0) -#endif - -#ifndef gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON -# define gsl_CONFIG_ALLOWS_NONSTRICT_SPAN_COMPARISON 1 -#endif - -#ifndef gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR -# define gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR 0 -#endif - -#ifndef gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION -# define gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION (gsl_CONFIG_DEFAULTS_VERSION >= 1) -#endif - -#if 1 < defined( gsl_CONFIG_CONTRACT_CHECKING_AUDIT ) + defined( gsl_CONFIG_CONTRACT_CHECKING_ON ) + defined( gsl_CONFIG_CONTRACT_CHECKING_OFF ) -# error only one of gsl_CONFIG_CONTRACT_CHECKING_AUDIT, gsl_CONFIG_CONTRACT_CHECKING_ON, and gsl_CONFIG_CONTRACT_CHECKING_OFF may be defined -#endif -#if 1 < defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) + defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES ) + defined( gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER ) -# error only one of gsl_CONFIG_CONTRACT_VIOLATION_THROWS, gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES, and gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER may be defined -#endif -#if 1 < defined( gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME ) + defined( gsl_CONFIG_UNENFORCED_CONTRACTS_ELIDE ) -# error only one of gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME and gsl_CONFIG_UNENFORCED_CONTRACTS_ELIDE may be defined -#endif - -// C++ language version detection (C++20 is speculative): -// Note: VC14.0/1900 (VS2015) lacks too much from C++14. - -#ifndef gsl_CPLUSPLUS -# if defined(_MSVC_LANG ) && !defined(__clang__) -# define gsl_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG ) -# else -# define gsl_CPLUSPLUS __cplusplus -# endif -#endif - -#define gsl_CPP98_OR_GREATER ( gsl_CPLUSPLUS >= 199711L ) -#define gsl_CPP11_OR_GREATER ( gsl_CPLUSPLUS >= 201103L ) -#define gsl_CPP14_OR_GREATER ( gsl_CPLUSPLUS >= 201402L ) -#define gsl_CPP17_OR_GREATER ( gsl_CPLUSPLUS >= 201703L ) -#define gsl_CPP20_OR_GREATER ( gsl_CPLUSPLUS >= 202000L ) - -// C++ language version (represent 98 as 3): - -#define gsl_CPLUSPLUS_V ( gsl_CPLUSPLUS / 100 - (gsl_CPLUSPLUS > 200000 ? 2000 : 1994) ) - -// half-open range [lo..hi): -#define gsl_BETWEEN( v, lo, hi ) ( (lo) <= (v) && (v) < (hi) ) - -// Compiler versions: - -// MSVC++ 6.0 _MSC_VER == 1200 gsl_COMPILER_MSVC_VERSION == 60 (Visual Studio 6.0) -// MSVC++ 7.0 _MSC_VER == 1300 gsl_COMPILER_MSVC_VERSION == 70 (Visual Studio .NET 2002) -// MSVC++ 7.1 _MSC_VER == 1310 gsl_COMPILER_MSVC_VERSION == 71 (Visual Studio .NET 2003) -// MSVC++ 8.0 _MSC_VER == 1400 gsl_COMPILER_MSVC_VERSION == 80 (Visual Studio 2005) -// MSVC++ 9.0 _MSC_VER == 1500 gsl_COMPILER_MSVC_VERSION == 90 (Visual Studio 2008) -// MSVC++ 10.0 _MSC_VER == 1600 gsl_COMPILER_MSVC_VERSION == 100 (Visual Studio 2010) -// MSVC++ 11.0 _MSC_VER == 1700 gsl_COMPILER_MSVC_VERSION == 110 (Visual Studio 2012) -// MSVC++ 12.0 _MSC_VER == 1800 gsl_COMPILER_MSVC_VERSION == 120 (Visual Studio 2013) -// MSVC++ 14.0 _MSC_VER == 1900 gsl_COMPILER_MSVC_VERSION == 140 (Visual Studio 2015) -// MSVC++ 14.1 _MSC_VER >= 1910 gsl_COMPILER_MSVC_VERSION == 141 (Visual Studio 2017) -// MSVC++ 14.2 _MSC_VER >= 1920 gsl_COMPILER_MSVC_VERSION == 142 (Visual Studio 2019) - -#if defined(_MSC_VER ) && !defined(__clang__) -# define gsl_COMPILER_MSVC_VER (_MSC_VER ) -# define gsl_COMPILER_MSVC_VERSION (_MSC_VER / 10 - 10 * ( 5 + (_MSC_VER < 1900 ) ) ) -# define gsl_COMPILER_MSVC_VERSION_FULL (_MSC_VER - 100 * ( 5 + (_MSC_VER < 1900 ) ) ) -#else -# define gsl_COMPILER_MSVC_VER 0 -# define gsl_COMPILER_MSVC_VERSION 0 -# define gsl_COMPILER_MSVC_VERSION_FULL 0 -#endif - -#define gsl_COMPILER_VERSION( major, minor, patch ) ( 10 * ( 10 * (major) + (minor) ) + (patch) ) - -// AppleClang 7.0.0 __apple_build_version__ == 7000172 gsl_COMPILER_APPLECLANG_VERSION == 700 (Xcode 7.0, 7.0.1) (LLVM 3.7.0) -// AppleClang 7.0.0 __apple_build_version__ == 7000176 gsl_COMPILER_APPLECLANG_VERSION == 700 (Xcode 7.1) (LLVM 3.7.0) -// AppleClang 7.0.2 __apple_build_version__ == 7000181 gsl_COMPILER_APPLECLANG_VERSION == 702 (Xcode 7.2, 7.2.1) (LLVM 3.7.0) -// AppleClang 7.3.0 __apple_build_version__ == 7030029 gsl_COMPILER_APPLECLANG_VERSION == 730 (Xcode 7.3) (LLVM 3.8.0) -// AppleClang 7.3.0 __apple_build_version__ == 7030031 gsl_COMPILER_APPLECLANG_VERSION == 730 (Xcode 7.3.1) (LLVM 3.8.0) -// AppleClang 8.0.0 __apple_build_version__ == 8000038 gsl_COMPILER_APPLECLANG_VERSION == 800 (Xcode 8.0) (LLVM 3.9.0) -// AppleClang 8.0.0 __apple_build_version__ == 8000042 gsl_COMPILER_APPLECLANG_VERSION == 800 (Xcode 8.1, 8.2, 8.2.1) (LLVM 3.9.0) -// AppleClang 8.1.0 __apple_build_version__ == 8020038 gsl_COMPILER_APPLECLANG_VERSION == 810 (Xcode 8.3) (LLVM 3.9.0) -// AppleClang 8.1.0 __apple_build_version__ == 8020041 gsl_COMPILER_APPLECLANG_VERSION == 810 (Xcode 8.3.1) (LLVM 3.9.0) -// AppleClang 8.1.0 __apple_build_version__ == 8020042 gsl_COMPILER_APPLECLANG_VERSION == 810 (Xcode 8.3.2, 8.3.3) (LLVM 3.9.0) -// AppleClang 9.0.0 __apple_build_version__ == 9000037 gsl_COMPILER_APPLECLANG_VERSION == 900 (Xcode 9.0) (LLVM 4.0.0?) -// AppleClang 9.0.0 __apple_build_version__ == 9000038 gsl_COMPILER_APPLECLANG_VERSION == 900 (Xcode 9.1) (LLVM 4.0.0?) -// AppleClang 9.0.0 __apple_build_version__ == 9000039 gsl_COMPILER_APPLECLANG_VERSION == 900 (Xcode 9.2) (LLVM 4.0.0?) -// AppleClang 9.1.0 __apple_build_version__ == 9020039 gsl_COMPILER_APPLECLANG_VERSION == 910 (Xcode 9.3, 9.3.1) (LLVM 5.0.2?) -// AppleClang 9.1.0 __apple_build_version__ == 9020039 gsl_COMPILER_APPLECLANG_VERSION == 910 (Xcode 9.4, 9.4.1) (LLVM 5.0.2?) -// AppleClang 10.0.0 __apple_build_version__ == 10001145 gsl_COMPILER_APPLECLANG_VERSION == 1000 (Xcode 10.0, 10.1) (LLVM 6.0.1?) -// AppleClang 10.0.1 __apple_build_version__ == 10010046 gsl_COMPILER_APPLECLANG_VERSION == 1001 (Xcode 10.2, 10.2.1, 10.3) (LLVM 7.0.0?) -// AppleClang 11.0.0 __apple_build_version__ == 11000033 gsl_COMPILER_APPLECLANG_VERSION == 1100 (Xcode 11.1, 11.2, 11.3) (LLVM 8.0.0?) - -#if defined( __apple_build_version__ ) -# define gsl_COMPILER_APPLECLANG_VERSION gsl_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ ) -# define gsl_COMPILER_CLANG_VERSION 0 -#elif defined( __clang__ ) -# define gsl_COMPILER_APPLECLANG_VERSION 0 -# define gsl_COMPILER_CLANG_VERSION gsl_COMPILER_VERSION( __clang_major__, __clang_minor__, __clang_patchlevel__ ) -#else -# define gsl_COMPILER_APPLECLANG_VERSION 0 -# define gsl_COMPILER_CLANG_VERSION 0 -#endif - -#if defined(__GNUC__) && !defined(__clang__) -# define gsl_COMPILER_GNUC_VERSION gsl_COMPILER_VERSION( __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__ ) -#else -# define gsl_COMPILER_GNUC_VERSION 0 -#endif - -// Compiler non-strict aliasing: - -#if defined(__clang__) || defined(__GNUC__) -# define gsl_may_alias __attribute__((__may_alias__)) -#else -# define gsl_may_alias -#endif - -// Presence of gsl, language and library features: - -#define gsl_IN_STD( v ) ( ((v) == 98 ? 3 : (v)) >= gsl_CPLUSPLUS_V ) - -#define gsl_DEPRECATE_TO_LEVEL( level ) ( level <= gsl_CONFIG_DEPRECATE_TO_LEVEL ) -#define gsl_FEATURE_TO_STD( feature ) ( gsl_IN_STD( gsl_FEATURE( feature##_TO_STD ) ) ) -#define gsl_FEATURE( feature ) ( gsl_FEATURE_##feature ) -#define gsl_CONFIG( feature ) ( gsl_CONFIG_##feature ) -#define gsl_HAVE( feature ) ( gsl_HAVE_##feature ) - -// Presence of wide character support: - -#ifdef __DJGPP__ -# define gsl_HAVE_WCHAR 0 -#else -# define gsl_HAVE_WCHAR 1 -#endif - -// Presence of language & library features: - -#if gsl_BETWEEN(gsl_COMPILER_GNUC_VERSION, 1, 500) || gsl_BETWEEN(gsl_COMPILER_CLANG_VERSION, 1, 360) || gsl_COMPILER_APPLECLANG_VERSION -# ifdef __EXCEPTIONS -# define gsl_HAVE_EXCEPTIONS 1 -# else -# define gsl_HAVE_EXCEPTIONS 0 -# endif // __EXCEPTIONS -#elif gsl_COMPILER_GNUC_VERSION >= 500 || gsl_COMPILER_CLANG_VERSION >= 500 -# ifdef __cpp_exceptions -# define gsl_HAVE_EXCEPTIONS 1 -# else -# define gsl_HAVE_EXCEPTIONS 0 -# endif // __cpp_exceptions -#elif gsl_COMPILER_MSVC_VERSION -# ifdef _CPPUNWIND -# define gsl_HAVE_EXCEPTIONS 1 -# else -# define gsl_HAVE_EXCEPTIONS 0 -# endif // _CPPUNWIND -#else -// For all other compilers, assume exceptions are always enabled. -# define gsl_HAVE_EXCEPTIONS 1 -#endif - -#if defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) && !gsl_HAVE( EXCEPTIONS ) -# error Cannot use gsl_CONFIG_CONTRACT_VIOLATION_THROWS if exceptions are disabled. -#endif // defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) && !gsl_HAVE( EXCEPTIONS ) - -#ifdef _HAS_CPP0X -# define gsl_HAS_CPP0X _HAS_CPP0X -#else -# define gsl_HAS_CPP0X 0 -#endif - -#define gsl_CPP11_100 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1600) -#define gsl_CPP11_110 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1700) -#define gsl_CPP11_120 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1800) -#define gsl_CPP11_140 (gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1900) - -#define gsl_CPP14_000 (gsl_CPP14_OR_GREATER) -#define gsl_CPP14_120 (gsl_CPP14_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1800) -#define gsl_CPP14_140 (gsl_CPP14_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1900) - -#define gsl_CPP17_000 (gsl_CPP17_OR_GREATER) -#define gsl_CPP17_140 (gsl_CPP17_OR_GREATER || gsl_COMPILER_MSVC_VER >= 1900) - -#define gsl_CPP11_140_CPP0X_90 (gsl_CPP11_140 || (gsl_COMPILER_MSVC_VER >= 1500 && gsl_HAS_CPP0X)) -#define gsl_CPP11_140_CPP0X_100 (gsl_CPP11_140 || (gsl_COMPILER_MSVC_VER >= 1600 && gsl_HAS_CPP0X)) - -// Presence of C++11 language features: - -#define gsl_HAVE_AUTO gsl_CPP11_100 -#define gsl_HAVE_NULLPTR gsl_CPP11_100 -#define gsl_HAVE_RVALUE_REFERENCE gsl_CPP11_100 -#define gsl_HAVE_FUNCTION_REF_QUALIFIER ( gsl_CPP14_140 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 481 ) ) - -#define gsl_HAVE_ENUM_CLASS gsl_CPP11_110 - -#define gsl_HAVE_ALIAS_TEMPLATE gsl_CPP11_120 -#define gsl_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG gsl_CPP11_120 -#define gsl_HAVE_EXPLICIT gsl_CPP11_120 -#define gsl_HAVE_INITIALIZER_LIST gsl_CPP11_120 -#define gsl_HAVE_VARIADIC_TEMPLATE gsl_CPP11_120 -#define gsl_HAVE_IS_DELETE gsl_CPP11_120 - -#define gsl_HAVE_CONSTEXPR_11 gsl_CPP11_140 -#define gsl_HAVE_IS_DEFAULT gsl_CPP11_140 -#define gsl_HAVE_NOEXCEPT gsl_CPP11_140 -#define gsl_HAVE_NORETURN ( gsl_CPP11_140 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 480 ) ) - -#define gsl_HAVE_EXPRESSION_SFINAE gsl_CPP11_140 - -#if gsl_CPP11_OR_GREATER -// see above -#endif - -// Presence of C++14 language features: - -#define gsl_HAVE_CONSTEXPR_14 ( gsl_CPP14_000 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 600 ) ) -#define gsl_HAVE_DECLTYPE_AUTO gsl_CPP14_140 -#define gsl_HAVE_DEPRECATED ( gsl_CPP14_140 && ! gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 142 ) ) - -// Presence of C++17 language features: -// MSVC: template parameter deduction guides since Visual Studio 2017 v15.7 - -#define gsl_HAVE_ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE gsl_CPP17_000 -#define gsl_HAVE_DEDUCTION_GUIDES ( gsl_CPP17_000 && ! gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION_FULL, 1, 1414 ) ) -#define gsl_HAVE_NODISCARD gsl_CPP17_000 -#define gsl_HAVE_CONSTEXPR_17 gsl_CPP17_OR_GREATER - -// Presence of C++20 language features: - -#define gsl_HAVE_CONSTEXPR_20 gsl_CPP20_OR_GREATER - -// Presence of C++ library features: - -#define gsl_HAVE_ADDRESSOF gsl_CPP17_000 -#define gsl_HAVE_ARRAY gsl_CPP11_110 -#define gsl_HAVE_TYPE_TRAITS gsl_CPP11_110 -#define gsl_HAVE_TR1_TYPE_TRAITS gsl_CPP11_110 - -#define gsl_HAVE_CONTAINER_DATA_METHOD gsl_CPP11_140_CPP0X_90 -#define gsl_HAVE_STD_DATA gsl_CPP17_000 -#ifdef __cpp_lib_ssize -# define gsl_HAVE_STD_SSIZE 1 -#else -# define gsl_HAVE_STD_SSIZE ( gsl_COMPILER_GNUC_VERSION >= 1000 && __cplusplus > 201703L ) -#endif - -#define gsl_HAVE_SIZED_TYPES gsl_CPP11_140 - -#define gsl_HAVE_MAKE_SHARED gsl_CPP11_140_CPP0X_100 -#define gsl_HAVE_SHARED_PTR gsl_CPP11_140_CPP0X_100 -#define gsl_HAVE_UNIQUE_PTR gsl_CPP11_140_CPP0X_100 - -#define gsl_HAVE_MAKE_UNIQUE gsl_CPP14_120 - -#define gsl_HAVE_UNCAUGHT_EXCEPTIONS gsl_CPP17_140 - -#define gsl_HAVE_ADD_CONST gsl_HAVE_TYPE_TRAITS -#define gsl_HAVE_INTEGRAL_CONSTANT gsl_HAVE_TYPE_TRAITS -#define gsl_HAVE_REMOVE_CONST gsl_HAVE_TYPE_TRAITS -#define gsl_HAVE_REMOVE_REFERENCE gsl_HAVE_TYPE_TRAITS -#define gsl_HAVE_REMOVE_CVREF gsl_CPP20_OR_GREATER - -#define gsl_HAVE_TR1_ADD_CONST gsl_HAVE_TR1_TYPE_TRAITS -#define gsl_HAVE_TR1_INTEGRAL_CONSTANT gsl_HAVE_TR1_TYPE_TRAITS -#define gsl_HAVE_TR1_REMOVE_CONST gsl_HAVE_TR1_TYPE_TRAITS -#define gsl_HAVE_TR1_REMOVE_REFERENCE gsl_HAVE_TR1_TYPE_TRAITS - -// C++ feature usage: - -#if gsl_HAVE( ADDRESSOF ) -# define gsl_ADDRESSOF(x) std::addressof(x) -#else -# define gsl_ADDRESSOF(x) (&x) -#endif - -#if gsl_HAVE( CONSTEXPR_11 ) -# define gsl_constexpr constexpr -#else -# define gsl_constexpr /*constexpr*/ -#endif - -#if gsl_HAVE( CONSTEXPR_14 ) -# define gsl_constexpr14 constexpr -#else -# define gsl_constexpr14 /*constexpr*/ -#endif - -#if gsl_HAVE( CONSTEXPR_17 ) -# define gsl_constexpr17 constexpr -#else -# define gsl_constexpr17 /*constexpr*/ -#endif - -#if gsl_HAVE( CONSTEXPR_20 ) -# define gsl_constexpr20 constexpr -#else -# define gsl_constexpr20 /*constexpr*/ -#endif - -#if gsl_HAVE( EXPLICIT ) -# define gsl_explicit explicit -#else -# define gsl_explicit /*explicit*/ -#endif - -#if gsl_FEATURE( IMPLICIT_MACRO ) -# define implicit /*implicit*/ -#endif - -#if gsl_HAVE( IS_DELETE ) -# define gsl_is_delete = delete -#else -# define gsl_is_delete -#endif - -#if gsl_HAVE( IS_DELETE ) -# define gsl_is_delete_access public -#else -# define gsl_is_delete_access private -#endif - -#if !gsl_HAVE( NOEXCEPT ) || defined( gsl_TESTING_ ) -# define gsl_noexcept /*noexcept*/ -#else -# define gsl_noexcept noexcept -#endif - -#if gsl_HAVE( NULLPTR ) -# define gsl_nullptr nullptr -#else -# define gsl_nullptr NULL -#endif - -#if gsl_HAVE( NODISCARD ) -# define gsl_NODISCARD [[nodiscard]] -#else -# define gsl_NODISCARD -#endif - -#if gsl_HAVE( NORETURN ) -# define gsl_NORETURN [[noreturn]] -#elif defined(_MSC_VER) -# define gsl_NORETURN __declspec(noreturn) -#else -# define gsl_NORETURN -#endif - -#if gsl_HAVE( DEPRECATED ) && !defined( gsl_TESTING_ ) -# define gsl_DEPRECATED [[deprecated]] -# define gsl_DEPRECATED_MSG( msg ) [[deprecated( msg )]] -#else -# define gsl_DEPRECATED -# define gsl_DEPRECATED_MSG( msg ) -#endif - -#if gsl_HAVE( TYPE_TRAITS ) - -#define gsl_DEFINE_ENUM_BITMASK_OPERATORS_( ENUM ) \ - gsl_NODISCARD gsl_api inline gsl_constexpr ENUM \ - operator~( ENUM val ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return ENUM( ~U( val ) ); \ - } \ - gsl_NODISCARD gsl_api inline gsl_constexpr ENUM \ - operator|( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return ENUM( U( lhs ) | U( rhs ) ); \ - } \ - gsl_NODISCARD gsl_api inline gsl_constexpr ENUM \ - operator&( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return ENUM( U( lhs ) & U( rhs ) ); \ - } \ - gsl_NODISCARD gsl_api inline gsl_constexpr ENUM \ - operator^( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return ENUM( U( lhs ) ^ U( rhs ) ); \ - } \ - gsl_api inline gsl_constexpr14 ENUM & \ - operator|=( ENUM & lhs, ENUM rhs ) gsl_noexcept \ - { \ - return lhs = lhs | rhs; \ - } \ - gsl_api inline gsl_constexpr14 ENUM & \ - operator&=( ENUM & lhs, ENUM rhs ) gsl_noexcept \ - { \ - return lhs = lhs & rhs; \ - } \ - gsl_api inline gsl_constexpr14 ENUM & \ - operator^=( ENUM & lhs, ENUM rhs ) gsl_noexcept \ - { \ - return lhs = lhs ^ rhs; \ - } - -#define gsl_DEFINE_ENUM_RELATIONAL_OPERATORS_( ENUM ) \ - gsl_NODISCARD gsl_api inline gsl_constexpr bool \ - operator<( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return U( lhs ) < U( rhs ); \ - } \ - gsl_NODISCARD gsl_api inline gsl_constexpr bool \ - operator>( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return U( lhs ) > U( rhs ); \ - } \ - gsl_NODISCARD gsl_api inline gsl_constexpr bool \ - operator<=( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return U( lhs ) <= U( rhs ); \ - } \ - gsl_NODISCARD gsl_api inline gsl_constexpr bool \ - operator>=( ENUM lhs, ENUM rhs ) gsl_noexcept \ - { \ - typedef typename ::gsl::std11::underlying_type::type U; \ - return U( lhs ) >= U( rhs ); \ - } - - // - // Defines bitmask operators `|`, `&`, `^`, `~`, `|=`, `&=`, and `^=` for the given enum type. - //ᅟ - //ᅟ enum class Vegetables { - //ᅟ tomato = 0b001, - //ᅟ onion = 0b010, - //ᅟ eggplant = 0b100 - //ᅟ }; - //ᅟ gsl_DEFINE_ENUM_BITMASK_OPERATORS( Vegetables ) - // -#define gsl_DEFINE_ENUM_BITMASK_OPERATORS( ENUM ) gsl_DEFINE_ENUM_BITMASK_OPERATORS_( ENUM ) - - // - // Defines relational operators `<`, `>`, `<=`, `>=` for the given enum type. - //ᅟ - //ᅟ enum class OperatorPrecedence { - //ᅟ additive = 0, - //ᅟ multiplicative = 1, - //ᅟ power = 2 - //ᅟ }; - //ᅟ gsl_DEFINE_ENUM_RELATIONAL_OPERATORS( OperatorPrecedence ) - // -#define gsl_DEFINE_ENUM_RELATIONAL_OPERATORS( ENUM ) gsl_DEFINE_ENUM_RELATIONAL_OPERATORS_( ENUM ) - -#endif // gsl_HAVE( TYPE_TRAITS ) - -#define gsl_DIMENSION_OF( a ) ( sizeof(a) / sizeof(0[a]) ) - - -// Method enabling (C++98, VC120 (VS2013) cannot use __VA_ARGS__) - -#if gsl_HAVE( EXPRESSION_SFINAE ) -# define gsl_DECLTYPE_(T, EXPR) decltype( EXPR ) -#else -# define gsl_DECLTYPE_(T, EXPR) T -#endif - -// NOTE: When using SFINAE in gsl-lite, please note that overloads of function templates must always use SFINAE with non-type default arguments -// as explained in https://en.cppreference.com/w/cpp/types/enable_if#Notes. `gsl_ENABLE_IF_()` implements graceful fallback to default -// type arguments (for compilers that don't support non-type default arguments); please verify that this is appropriate in the given -// situation, and add additional checks if necessary. -// -// Also, please note that `gsl_ENABLE_IF_()` doesn't enforce the constraint at all if no compiler/library support is available (i.e. pre-C++11). - -#if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) -# if !gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 140 ) // VS 2013 seems to have trouble with SFINAE for default non-type arguments -# define gsl_ENABLE_IF_(VA) , typename std::enable_if< ( VA ), int >::type = 0 -# else -# define gsl_ENABLE_IF_(VA) , typename = typename std::enable_if< ( VA ), ::gsl::detail::enabler >::type -# endif -#else -# define gsl_ENABLE_IF_(VA) -#endif - - -// Other features: - -#define gsl_HAVE_CONSTRAINED_SPAN_CONTAINER_CTOR \ - ( gsl_HAVE_DEFAULT_FUNCTION_TEMPLATE_ARG && gsl_HAVE_CONTAINER_DATA_METHOD ) - -// Note: !defined(__NVCC__) doesn't work with nvcc here: -#define gsl_HAVE_UNCONSTRAINED_SPAN_CONTAINER_CTOR \ - ( gsl_CONFIG_ALLOWS_UNCONSTRAINED_SPAN_CONTAINER_CTOR && (__NVCC__== 0) ) - -// GSL API (e.g. for CUDA platform): - -// Guidelines for using `gsl_api`: -// -// NVCC imposes the restriction that a function annotated `__host__ __device__` cannot call host-only or device-only functions. -// This makes `gsl_api` inappropriate for generic functions that call unknown code, e.g. the template constructors of `span<>` -// or functions like `finally()` which accept an arbitrary function object. -// It is often preferable to annotate functions only with `gsl_constexpr` or `gsl_constexpr14`. The "extended constexpr" mode -// of NVCC (currently an experimental feature) will implicitly consider constexpr functions `__host__ __device__` functions -// but tolerates calls to host-only or device-only functions. - -#ifndef gsl_api -# ifdef __CUDACC__ -# define gsl_api __host__ __device__ -# else -# define gsl_api /*gsl_api*/ -# endif -#endif - -// Additional includes: - -#if gsl_HAVE( ARRAY ) -# include -#endif - -#if !gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) || !gsl_HAVE( AUTO ) -# include -#endif - -#if gsl_HAVE( INITIALIZER_LIST ) -# include -#endif - -#if gsl_HAVE( TYPE_TRAITS ) -# include // for enable_if<>, - // add_const<>, add_pointer<>, common_type<>, make_signed<>, remove_cv<>, remove_const<>, remove_volatile<>, remove_reference<>, remove_cvref<>, remove_pointer<>, underlying_type<>, - // is_assignable<>, is_constructible<>, is_const<>, is_convertible<>, is_integral<>, is_pointer<>, is_signed<>, - // integral_constant<>, declval() -#elif gsl_HAVE( TR1_TYPE_TRAITS ) -# include // for add_const<>, remove_cv<>, remove_const<>, remove_volatile<>, remove_reference<>, integral_constant<> -#endif - -// MSVC warning suppression macros: - -#if gsl_COMPILER_MSVC_VERSION >= 140 && !defined(__NVCC__) -# define gsl_SUPPRESS_MSGSL_WARNING(expr) [[gsl::suppress(expr)]] -# define gsl_SUPPRESS_MSVC_WARNING(code, descr) __pragma(warning(suppress: code) ) -# define gsl_DISABLE_MSVC_WARNINGS(codes) __pragma(warning(push)) __pragma(warning(disable: codes)) -# define gsl_RESTORE_MSVC_WARNINGS() __pragma(warning(pop )) -#else -// TODO: define for Clang -# define gsl_SUPPRESS_MSGSL_WARNING(expr) -# define gsl_SUPPRESS_MSVC_WARNING(code, descr) -# define gsl_DISABLE_MSVC_WARNINGS(codes) -# define gsl_RESTORE_MSVC_WARNINGS() -#endif - -// Suppress the following MSVC GSL warnings: -// - C26432: gsl::c.21 : if you define or delete any default operation in the type '...', define or delete them all -// - C26410: gsl::r.32 : the parameter 'ptr' is a reference to const unique pointer, use const T* or const T& instead -// - C26415: gsl::r.30 : smart pointer parameter 'ptr' is used only to access contained pointer. Use T* or T& instead -// - C26418: gsl::r.36 : shared pointer parameter 'ptr' is not copied or moved. Use T* or T& instead -// - C26472: gsl::t.1 : don't use a static_cast for arithmetic conversions; -// use brace initialization, gsl::narrow_cast or gsl::narrow -// - C26439: gsl::f.6 : special function 'function' can be declared 'noexcept' -// - C26440: gsl::f.6 : function 'function' can be declared 'noexcept' -// - C26455: gsl::f.6 : default constructor may not throw. Declare it 'noexcept' -// - C26473: gsl::t.1 : don't cast between pointer types where the source type and the target type are the same -// - C26481: gsl::b.1 : don't use pointer arithmetic. Use span instead -// - C26482: gsl::b.2 : only index into arrays using constant expressions -// - C26446: gdl::b.4 : prefer to use gsl::at() instead of unchecked subscript operator -// - C26490: gsl::t.1 : don't use reinterpret_cast -// - C26487: gsl::l.4 : don't return a pointer '('s result)' that may be invalid - -gsl_DISABLE_MSVC_WARNINGS( 26432 26410 26415 26418 26472 26439 26440 26455 26473 26481 26482 26446 26490 26487 ) - -namespace gsl { - -// forward declare span<>: - -template< class T > -class span; - -// C++11 emulation: - -namespace std11 { - -#if gsl_HAVE( ADD_CONST ) - -using std::add_const; - -#elif gsl_HAVE( TR1_ADD_CONST ) - -using std::tr1::add_const; - -#else - -template< class T > struct add_const { typedef const T type; }; - -#endif // gsl_HAVE( ADD_CONST ) - -#if gsl_HAVE( REMOVE_CONST ) - -using std::remove_cv; -using std::remove_const; -using std::remove_volatile; - -#elif gsl_HAVE( TR1_REMOVE_CONST ) - -using std::tr1::remove_cv; -using std::tr1::remove_const; -using std::tr1::remove_volatile; - -#else - -template< class T > struct remove_const { typedef T type; }; -template< class T > struct remove_const { typedef T type; }; - -template< class T > struct remove_volatile { typedef T type; }; -template< class T > struct remove_volatile { typedef T type; }; - -template< class T > -struct remove_cv -{ - typedef typename remove_volatile::type>::type type; -}; - -#endif // gsl_HAVE( REMOVE_CONST ) - -#if gsl_HAVE( REMOVE_REFERENCE ) - -using std::remove_reference; - -#elif gsl_HAVE( TR1_REMOVE_REFERENCE ) - -using std::tr1::remove_reference; - -#else - -template< class T > struct remove_reference { typedef T type; }; -template< class T > struct remove_reference { typedef T type; }; -# if gsl_HAVE( RVALUE_REFERENCE ) -template< class T > struct remove_reference { typedef T type; }; -# endif - -#endif // gsl_HAVE( REMOVE_REFERENCE ) - - -#if gsl_HAVE( INTEGRAL_CONSTANT ) - -using std::integral_constant; -using std::true_type; -using std::false_type; - -#elif gsl_HAVE( TR1_INTEGRAL_CONSTANT ) - -using std::tr1::integral_constant; -using std::tr1::true_type; -using std::tr1::false_type; - -#else - -template< class T, T v > struct integral_constant { enum { value = v }; }; -typedef integral_constant< bool, true > true_type; -typedef integral_constant< bool, false > false_type; - -#endif - -#if gsl_HAVE( TYPE_TRAITS ) - -using std::underlying_type; - -#elif gsl_HAVE( TR1_TYPE_TRAITS ) - -using std::tr1::underlying_type; - -#else - -// We could try to define `underlying_type<>` for pre-C++11 here, but let's not until someone actually needs it. - -#endif - -} // namespace std11 - -// C++14 emulation: - -namespace std14 { - -#if gsl_HAVE( UNIQUE_PTR ) -# if gsl_HAVE( MAKE_UNIQUE ) - -using std::make_unique; - -# elif gsl_HAVE( VARIADIC_TEMPLATE ) - -template< class T, class... Args > -std::unique_ptr make_unique( Args &&... args ) -{ - return std::unique_ptr( new T( std::forward( args )... ) ); -} - -# endif // gsl_HAVE( MAKE_UNIQUE ), gsl_HAVE( VARIADIC_TEMPLATE ) -#endif // gsl_HAVE( UNIQUE_PTR ) - -} // namespace std14 - -namespace detail { - -#if gsl_HAVE( VARIADIC_TEMPLATE ) - -template < bool V0, class T0, class... Ts > struct conjunction_ { using type = T0; }; -template < class T0, class T1, class... Ts > struct conjunction_ : conjunction_ { }; -template < bool V0, class T0, class... Ts > struct disjunction_ { using type = T0; }; -template < class T0, class T1, class... Ts > struct disjunction_ : disjunction_ { }; - -#endif - -} // namespace detail - -// C++17 emulation: - -namespace std17 { - -template< bool v > struct bool_constant : std11::integral_constant{}; - -#if gsl_CPP11_120 - -template < class... Ts > struct conjunction; -template < > struct conjunction< > : std11::true_type { }; -template < class T0, class... Ts > struct conjunction : detail::conjunction_::type { }; -template < class... Ts > struct disjunction; -template < > struct disjunction< > : std11::false_type { }; -template < class T0, class... Ts > struct disjunction : detail::disjunction_::type { }; -template < class T > struct negation : std11::integral_constant { }; - -# if gsl_CPP14_OR_GREATER - -template < class... Ts > constexpr bool conjunction_v = conjunction::value; -template < class... Ts > constexpr bool disjunction_v = disjunction::value; -template < class T > constexpr bool negation_v = negation::value; - -# endif // gsl_CPP14_OR_GREATER - -template< class... Ts > -struct make_void { typedef void type; }; - -template< class... Ts > -using void_t = typename make_void< Ts... >::type; - -#endif // gsl_CPP11_120 - -#if gsl_HAVE( STD_DATA ) - -using std::data; -using std::size; - -#elif gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) - -template< class T, size_t N > -gsl_api inline gsl_constexpr auto size( T const(&)[N] ) gsl_noexcept -> size_t -{ - return N; -} - -template< class C > -inline gsl_constexpr auto size( C const & cont ) -> decltype( cont.size() ) -{ - return cont.size(); -} - -template< class T, size_t N > -gsl_api inline gsl_constexpr auto data( T(&arr)[N] ) gsl_noexcept -> T* -{ - return &arr[0]; -} - -template< class C > -inline gsl_constexpr auto data( C & cont ) -> decltype( cont.data() ) -{ - return cont.data(); -} - -template< class C > -inline gsl_constexpr auto data( C const & cont ) -> decltype( cont.data() ) -{ - return cont.data(); -} - -template< class E > -inline gsl_constexpr auto data( std::initializer_list il ) gsl_noexcept -> E const * -{ - return il.begin(); -} - -#endif // span_HAVE( DATA ) - -} // namespace std17 - -// C++20 emulation: - -namespace std20 { - -#if gsl_CPP11_100 - -struct identity -{ - template < class T > - gsl_constexpr T && operator ()( T && arg ) const gsl_noexcept - { - return std::forward( arg ); - } -}; - -#endif // gsl_CPP11_100 - -template< class T > -struct type_identity -{ - typedef T type; -}; -#if gsl_HAVE( ALIAS_TEMPLATE ) -template< class T > -using type_identity_t = typename type_identity::type; -#endif // gsl_HAVE( ALIAS_TEMPLATE ) - -#if gsl_HAVE( STD_SSIZE ) - -using std::ssize; - -#elif gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) - -template < class C > -gsl_constexpr auto ssize( C const & c ) - -> typename std::common_type::type>::type -{ - using R = typename std::common_type::type>::type; - return static_cast( c.size() ); -} - -template -gsl_constexpr auto ssize( T const(&)[N] ) gsl_noexcept -> std::ptrdiff_t -{ - return std::ptrdiff_t( N ); -} - -#endif // gsl_HAVE( STD_SSIZE ) - -#if gsl_HAVE( REMOVE_CVREF ) - -using std::remove_cvref; - -#else - -template< class T > struct remove_cvref { typedef typename std11::remove_cv< typename std11::remove_reference< T >::type >::type type; }; - -#endif // gsl_HAVE( REMOVE_CVREF ) - -} // namespace std20 - -namespace detail { - -/// for nsel_REQUIRES_T - -/*enum*/ class enabler{}; - -#if gsl_HAVE( TYPE_TRAITS ) - -template< class Q > -struct is_span_oracle : std::false_type{}; - -template< class T> -struct is_span_oracle< span > : std::true_type{}; - -template< class Q > -struct is_span : is_span_oracle< typename std::remove_cv::type >{}; - -template< class Q > -struct is_std_array_oracle : std::false_type{}; - -#if gsl_HAVE( ARRAY ) - -template< class T, std::size_t Extent > -struct is_std_array_oracle< std::array > : std::true_type{}; - -#endif - -template< class Q > -struct is_std_array : is_std_array_oracle< typename std::remove_cv::type >{}; - -template< class Q > -struct is_array : std::false_type{}; - -template< class T > -struct is_array : std::true_type{}; - -template< class T, std::size_t N > -struct is_array : std::true_type{}; - -# if gsl_CPP11_140 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 500 ) - -template< class, class = void > -struct has_size_and_data : std::false_type{}; - -template< class C > -struct has_size_and_data -< - C, std17::void_t< - decltype( std17::size(std::declval()) ), - decltype( std17::data(std::declval()) ) > -> : std::true_type{}; - -template< class, class, class = void > -struct is_compatible_element : std::false_type {}; - -template< class C, class E > -struct is_compatible_element -< - C, E, std17::void_t< - decltype( std17::data(std::declval()) ), - typename std::remove_pointer() ) )>::type(*)[] > -> : std::is_convertible< typename std::remove_pointer() ) )>::type(*)[], E(*)[] >{}; - -template< class C > -struct is_container : std17::bool_constant -< - ! is_span< C >::value - && ! is_array< C >::value - && ! is_std_array< C >::value - && has_size_and_data< C >::value ->{}; - -template< class C, class E > -struct is_compatible_container : std17::bool_constant -< - is_container::value - && is_compatible_element::value ->{}; - -# else // ^^^ gsl_CPP11_140 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 500 ) ^^^ / vvv ! gsl_CPP11_140 || gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 500 ) vvv - -template< - class C, class E - , typename = typename std::enable_if< - ! is_span< C >::value - && ! is_array< C >::value - && ! is_std_array< C >::value - && ( std::is_convertible< typename std::remove_pointer() ) )>::type(*)[], E(*)[] >::value) - // && has_size_and_data< C >::value - , enabler>::type - , class = decltype( std17::size(std::declval()) ) - , class = decltype( std17::data(std::declval()) ) -> -# if gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 140 ) -// VS2013 has insufficient support for expression SFINAE; we cannot make `is_compatible_container<>` a proper type trait here -struct is_compatible_container : std::true_type { }; -# else -struct is_compatible_container_r { is_compatible_container_r(int); }; -template< class C, class E > -std::true_type is_compatible_container_f( is_compatible_container_r ); -template< class C, class E > -std::false_type is_compatible_container_f( ... ); - -template< class C, class E > -struct is_compatible_container : decltype( is_compatible_container_f< C, E >( 0 ) ) { }; -# endif // gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 140 ) - -# endif // gsl_CPP11_140 && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 1, 500 ) - -#endif // gsl_HAVE( TYPE_TRAITS ) - -} // namespace detail - -// -// GSL.util: utilities -// - -// Integer type for indices (e.g. in a loop). -typedef gsl_CONFIG_INDEX_TYPE index; - -// -// GSL.owner: ownership pointers -// -#if gsl_HAVE( SHARED_PTR ) - using std::unique_ptr; - using std::shared_ptr; - using std::make_shared; -# if gsl_HAVE( MAKE_UNIQUE ) || gsl_HAVE( VARIADIC_TEMPLATE ) - using std14::make_unique; -# endif -#endif - -#if gsl_HAVE( ALIAS_TEMPLATE ) - template< class T -#if gsl_HAVE( TYPE_TRAITS ) - , typename = typename std::enable_if< std::is_pointer::value >::type -#endif - > - using owner = T; -#elif gsl_CONFIG_DEFAULTS_VERSION == 0 - // TODO vNext: remove - template< class T > struct owner { typedef T type; }; -#endif - -#define gsl_HAVE_OWNER_TEMPLATE gsl_HAVE_ALIAS_TEMPLATE - -// TODO vNext: remove -#if gsl_FEATURE( OWNER_MACRO ) -# if gsl_HAVE( OWNER_TEMPLATE ) -# define Owner(t) ::gsl::owner -# else -# define Owner(t) ::gsl::owner::type -# endif -#endif - -// -// GSL.assert: assertions -// - -#if gsl_HAVE( TYPE_TRAITS ) -# define gsl_ELIDE_CONTRACT_( x ) static_assert(::std::is_constructible::value, "argument of contract check must be convertible to bool") -#else -# define gsl_ELIDE_CONTRACT_( x ) -#endif - -#if defined( __CUDACC__ ) && defined( __CUDA_ARCH__ ) -# define gsl_ASSUME( x ) gsl_ELIDE_CONTRACT_( x ) /* there is no assume intrinsic in CUDA device code */ -#elif gsl_COMPILER_MSVC_VERSION -# define gsl_ASSUME( x ) __assume( x ) -#elif gsl_COMPILER_GNUC_VERSION -# define gsl_ASSUME( x ) (( x ) ? static_cast(0) : __builtin_unreachable()) -#elif defined(__has_builtin) -# if __has_builtin(__builtin_unreachable) -# define gsl_ASSUME( x ) (( x ) ? static_cast(0) : __builtin_unreachable()) -# endif -#else -# define gsl_ASSUME( x ) gsl_ELIDE_CONTRACT_( x ) /* unknown compiler; cannot rely on assume intrinsic */ -#endif - -#if defined( gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER ) -# define gsl_CONTRACT_CHECK_( str, x ) ( ( x ) ? static_cast(0) : ::gsl::fail_fast_assert_handler( #x, "GSL: " str, __FILE__, __LINE__ ) ) -#elif defined( __CUDACC__ ) && defined( __CUDA_ARCH__ ) -# define gsl_CONTRACT_CHECK_( str, x ) assert( ( x ) && str ) -#elif defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) -# define gsl_CONTRACT_CHECK_( str, x ) ( ( x ) ? static_cast(0) : ::gsl::detail::fail_fast_throw( "GSL: " str " at " __FILE__ ":" gsl_STRINGIFY(__LINE__) ) ) -#else // defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES ) [default] -# define gsl_CONTRACT_CHECK_( str, x ) ( ( x ) ? static_cast(0) : ::gsl::detail::fail_fast_terminate() ) -#endif - -#if defined( gsl_CONFIG_CONTRACT_CHECKING_OFF ) || defined( gsl_CONFIG_CONTRACT_CHECKING_EXPECTS_OFF ) -# if defined( gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME ) -# define gsl_Expects( x ) gsl_ASSUME( x ) -# else // defined( gsl_CONFIG_UNENFORCED_CONTRACTS_ELIDE ) [default] -# define gsl_Expects( x ) gsl_ELIDE_CONTRACT_( x ) -# endif -#else -# define gsl_Expects( x ) gsl_CONTRACT_CHECK_( "Precondition failure", x ) -#endif -#define Expects( x ) gsl_Expects( x ) - -#if !defined( gsl_CONFIG_CONTRACT_CHECKING_AUDIT ) || defined( gsl_CONFIG_CONTRACT_CHECKING_EXPECTS_OFF ) -# define gsl_ExpectsAudit( x ) gsl_ELIDE_CONTRACT_( x ) -#else -# define gsl_ExpectsAudit( x ) gsl_CONTRACT_CHECK_( "Precondition failure (audit)", x ) -#endif - -#if defined( gsl_CONFIG_CONTRACT_CHECKING_OFF ) || defined( gsl_CONFIG_CONTRACT_CHECKING_ENSURES_OFF ) -# if defined( gsl_CONFIG_UNENFORCED_CONTRACTS_ASSUME ) -# define gsl_Ensures( x ) gsl_ASSUME( x ) -# else // defined( gsl_CONFIG_UNENFORCED_CONTRACTS_ELIDE ) [default] -# define gsl_Ensures( x ) gsl_ELIDE_CONTRACT_( x ) -# endif -#else -# define gsl_Ensures( x ) gsl_CONTRACT_CHECK_( "Postcondition failure", x ) -#endif -#define Ensures( x ) gsl_Ensures( x ) - -#if !defined( gsl_CONFIG_CONTRACT_CHECKING_AUDIT ) || defined( gsl_CONFIG_CONTRACT_CHECKING_ENSURES_OFF ) -# define gsl_EnsuresAudit( x ) gsl_ELIDE_CONTRACT_( x ) -#else -# define gsl_EnsuresAudit( x ) gsl_CONTRACT_CHECK_( "Postcondition failure (audit)", x ) -#endif - -#define gsl_STRINGIFY( x ) gsl_STRINGIFY_( x ) -#define gsl_STRINGIFY_( x ) #x - -struct fail_fast : public std::logic_error -{ - explicit fail_fast( char const * message ) - : std::logic_error( message ) {} -}; - -namespace detail { - - -#if gsl_HAVE( EXCEPTIONS ) -gsl_NORETURN inline void fail_fast_throw( char const * message ) -{ - throw fail_fast( message ); -} -#endif // gsl_HAVE( EXCEPTIONS ) -gsl_NORETURN inline void fail_fast_terminate() gsl_noexcept -{ - std::terminate(); -} - -} // namespace detail - -// Should be defined by user -gsl_api void fail_fast_assert_handler( char const * const expression, char const * const message, char const * const file, int line ); - -#if defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) - -# if gsl_HAVE( EXCEPTIONS ) -gsl_DEPRECATED_MSG("don't call gsl::fail_fast_assert() directly; use contract checking macros instead") -gsl_constexpr14 inline -void fail_fast_assert( bool cond, char const * const message ) -{ - if ( !cond ) - throw fail_fast( message ); -} -# endif // gsl_HAVE( EXCEPTIONS ) - -#elif defined( gsl_CONFIG_CONTRACT_VIOLATION_CALLS_HANDLER ) - -gsl_DEPRECATED_MSG("don't call gsl::fail_fast_assert() directly; use contract checking macros instead") -gsl_api gsl_constexpr14 inline -void fail_fast_assert( bool cond, char const * const expression, char const * const message, char const * const file, int line ) -{ - if ( !cond ) - ::gsl::fail_fast_assert_handler( expression, message, file, line ); -} - -#else // defined( gsl_CONFIG_CONTRACT_VIOLATION_TERMINATES ) [default] - -gsl_DEPRECATED_MSG("don't call gsl::fail_fast_assert() directly; use contract checking macros instead") -gsl_constexpr14 inline -void fail_fast_assert( bool cond ) gsl_noexcept -{ - if ( !cond ) - std::terminate(); -} - -#endif - - -// -// GSL.util: utilities -// - -#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) - -// Add uncaught_exceptions for pre-2017 MSVC, GCC and Clang -// Return unsigned char to save stack space, uncaught_exceptions can only increase by 1 in a scope - -namespace detail { - -gsl_api inline unsigned char to_uchar( unsigned x ) gsl_noexcept -{ - return static_cast( x ); -} - -} // namespace detail - -namespace std11 { - -#if gsl_HAVE( UNCAUGHT_EXCEPTIONS ) - -inline unsigned char uncaught_exceptions() gsl_noexcept -{ - return detail::to_uchar( std::uncaught_exceptions() ); -} - -#elif gsl_COMPILER_MSVC_VERSION - -extern "C" char * __cdecl _getptd(); -inline unsigned char uncaught_exceptions() gsl_noexcept -{ - return detail::to_uchar( *reinterpret_cast(_getptd() + (sizeof(void*) == 8 ? 0x100 : 0x90) ) ); -} - -#elif gsl_COMPILER_CLANG_VERSION || gsl_COMPILER_GNUC_VERSION || gsl_COMPILER_APPLECLANG_VERSION - -extern "C" char * __cxa_get_globals(); -inline unsigned char uncaught_exceptions() gsl_noexcept -{ - return detail::to_uchar( *reinterpret_cast(__cxa_get_globals() + sizeof(void*) ) ); -} -#endif -} // namespace std11 -#endif - -#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 110 - -template< class F > -class final_action -{ -public: - explicit final_action( F action ) gsl_noexcept - : action_( std::move( action ) ) - , invoke_( true ) - {} - - final_action( final_action && other ) gsl_noexcept - : action_( std::move( other.action_ ) ) - , invoke_( other.invoke_ ) - { - other.invoke_ = false; - } - - gsl_SUPPRESS_MSGSL_WARNING(f.6) - virtual ~final_action() gsl_noexcept - { - if ( invoke_ ) - action_(); - } - -gsl_is_delete_access: - final_action( final_action const & ) gsl_is_delete; - final_action & operator=( final_action const & ) gsl_is_delete; - final_action & operator=( final_action && ) gsl_is_delete; - -protected: - void dismiss() gsl_noexcept - { - invoke_ = false; - } - -private: - F action_; - bool invoke_; -}; - -template< class F > -inline final_action finally( F const & action ) gsl_noexcept -{ - return final_action( action ); -} - -template< class F > -inline final_action finally( F && action ) gsl_noexcept -{ - return final_action( std::forward( action ) ); -} - -#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) - -template< class F > -class final_action_return : public final_action -{ -public: - explicit final_action_return( F && action ) gsl_noexcept - : final_action( std::move( action ) ) - , exception_count( std11::uncaught_exceptions() ) - {} - - final_action_return( final_action_return && other ) gsl_noexcept - : final_action( std::move( other ) ) - , exception_count( std11::uncaught_exceptions() ) - {} - - ~final_action_return() override - { - if ( std11::uncaught_exceptions() != exception_count ) - this->dismiss(); - } - -gsl_is_delete_access: - final_action_return( final_action_return const & ) gsl_is_delete; - final_action_return & operator=( final_action_return const & ) gsl_is_delete; - -private: - unsigned char exception_count; -}; - -template< class F > -inline final_action_return on_return( F const & action ) gsl_noexcept -{ - return final_action_return( action ); -} - -template< class F > -inline final_action_return on_return( F && action ) gsl_noexcept -{ - return final_action_return( std::forward( action ) ); -} - -template< class F > -class final_action_error : public final_action -{ -public: - explicit final_action_error( F && action ) gsl_noexcept - : final_action( std::move( action ) ) - , exception_count( std11::uncaught_exceptions() ) - {} - - final_action_error( final_action_error && other ) gsl_noexcept - : final_action( std::move( other ) ) - , exception_count( std11::uncaught_exceptions() ) - {} - - ~final_action_error() override - { - if ( std11::uncaught_exceptions() == exception_count ) - this->dismiss(); - } - -gsl_is_delete_access: - final_action_error( final_action_error const & ) gsl_is_delete; - final_action_error & operator=( final_action_error const & ) gsl_is_delete; - -private: - unsigned char exception_count; -}; - -template< class F > -inline final_action_error on_error( F const & action ) gsl_noexcept -{ - return final_action_error( action ); -} - -template< class F > -inline final_action_error on_error( F && action ) gsl_noexcept -{ - return final_action_error( std::forward( action ) ); -} - -#endif // gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) - -#else // gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 110 - -class final_action -{ -public: - typedef void (*Action)(); - - final_action( Action action ) - : action_( action ) - , invoke_( true ) - {} - - final_action( final_action const & other ) - : action_( other.action_ ) - , invoke_( other.invoke_ ) - { - other.invoke_ = false; - } - - virtual ~final_action() - { - if ( invoke_ ) - action_(); - } - -protected: - void dismiss() - { - invoke_ = false; - } - -private: - final_action & operator=( final_action const & ); - -private: - Action action_; - mutable bool invoke_; -}; - -template< class F > -inline final_action finally( F const & f ) -{ - return final_action(( f )); -} - -#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) - -class final_action_return : public final_action -{ -public: - explicit final_action_return( Action action ) - : final_action( action ) - , exception_count( std11::uncaught_exceptions() ) - {} - - ~final_action_return() - { - if ( std11::uncaught_exceptions() != exception_count ) - this->dismiss(); - } - -private: - final_action_return & operator=( final_action_return const & ); - -private: - unsigned char exception_count; -}; - -template< class F > -inline final_action_return on_return( F const & action ) -{ - return final_action_return( action ); -} - -class final_action_error : public final_action -{ -public: - explicit final_action_error( Action action ) - : final_action( action ) - , exception_count( std11::uncaught_exceptions() ) - {} - - ~final_action_error() - { - if ( std11::uncaught_exceptions() == exception_count ) - this->dismiss(); - } - -private: - final_action_error & operator=( final_action_error const & ); - -private: - unsigned char exception_count; -}; - -template< class F > -inline final_action_error on_error( F const & action ) -{ - return final_action_error( action ); -} - -#endif // gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) - -#endif // gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION == 110 - -#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 120 - -template< class T, class U > -gsl_api inline gsl_constexpr T narrow_cast( U && u ) gsl_noexcept -{ - return static_cast( std::forward( u ) ); -} - -#else - -template< class T, class U > -gsl_api inline T narrow_cast( U u ) gsl_noexcept -{ - return static_cast( u ); -} - -#endif // gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 120 - -struct narrowing_error : public std::exception {}; - -#if gsl_HAVE( TYPE_TRAITS ) - -namespace detail { - - template< class T, class U > - struct is_same_signedness : public std::integral_constant::value == std::is_signed::value> - {}; - -# if defined( __NVCC__ ) - // We do this to circumvent NVCC warnings about pointless unsigned comparisons with 0. - template< class T > - gsl_constexpr gsl_api bool is_negative( T value, std::true_type /*isSigned*/ ) gsl_noexcept - { - return value < T(); - } - template< class T > - gsl_constexpr gsl_api bool is_negative( T /*value*/, std::false_type /*isUnsigned*/ ) gsl_noexcept - { - return false; - } - template< class T, class U > - gsl_constexpr gsl_api bool have_same_sign( T t, U u, std::true_type /*isSameSignedness*/ ) gsl_noexcept - { - return true; - } - template< class T, class U > - gsl_constexpr gsl_api bool have_same_sign( T t, U u, std::false_type /*isSameSignedness*/ ) gsl_noexcept - { - return detail::is_negative( t, std::is_signed() ) == detail::is_negative( u, std::is_signed() ); - } -# endif // defined( __NVCC__ ) - -} // namespace detail - -#endif - -#if gsl_HAVE( EXCEPTIONS ) || !gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION -template< class T, class U > -# if !gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION && !defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) -gsl_api -# endif // !gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION && !defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) -inline T narrow( U u ) -{ - T t = static_cast( u ); - - if ( static_cast( t ) != u ) - { -# if gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION || defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) - throw narrowing_error(); -# else - std::terminate(); -# endif - } - -# if gsl_HAVE( TYPE_TRAITS ) -# if defined( __NVCC__ ) - if ( ! detail::have_same_sign( t, u, detail::is_same_signedness() ) ) -# else - gsl_SUPPRESS_MSVC_WARNING( 4127, "conditional expression is constant" ) - if ( ! detail::is_same_signedness::value && ( t < T() ) != ( u < U() ) ) -# endif -# else - // Don't assume T() works: - gsl_SUPPRESS_MSVC_WARNING( 4127, "conditional expression is constant" ) - if ( ( t < 0 ) != ( u < 0 ) ) -# endif - { -# if gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION || defined( gsl_CONFIG_CONTRACT_VIOLATION_THROWS ) - throw narrowing_error(); -# else - std::terminate(); -# endif - } - - return t; -} -#endif // gsl_HAVE( EXCEPTIONS ) || !gsl_CONFIG_NARROW_THROWS_ON_TRUNCATION - -template< class T, class U > -gsl_api inline T narrow_failfast( U u ) -{ - T t = static_cast( u ); - - gsl_Expects( static_cast( t ) == u ); - -#if gsl_HAVE( TYPE_TRAITS ) -# if defined( __NVCC__ ) - gsl_Expects( ::gsl::detail::have_same_sign( t, u, ::gsl::detail::is_same_signedness() ) ); -# else - gsl_SUPPRESS_MSVC_WARNING( 4127, "conditional expression is constant" ) - gsl_Expects( ( ::gsl::detail::is_same_signedness::value || ( t < T() ) == ( u < U() ) ) ); -# endif -#else - // Don't assume T() works: - gsl_SUPPRESS_MSVC_WARNING( 4127, "conditional expression is constant" ) - gsl_Expects( ( t < 0 ) == ( u < 0 ) ); -#endif - - return t; -} - - -// -// at() - Bounds-checked way of accessing static arrays, std::array, std::vector. -// - -template< class T, size_t N > -gsl_api inline gsl_constexpr14 T & at( T(&arr)[N], size_t pos ) -{ - gsl_Expects( pos < N ); - return arr[pos]; -} - -template< class Container > -inline gsl_constexpr14 typename Container::value_type & at( Container & cont, size_t pos ) -{ - gsl_Expects( pos < cont.size() ); - return cont[pos]; -} - -template< class Container > -inline gsl_constexpr14 typename Container::value_type const & at( Container const & cont, size_t pos ) -{ - gsl_Expects( pos < cont.size() ); - return cont[pos]; -} - -#if gsl_HAVE( INITIALIZER_LIST ) - -template< class T > -inline const gsl_constexpr14 T at( std::initializer_list cont, size_t pos ) -{ - gsl_Expects( pos < cont.size() ); - return *( cont.begin() + pos ); -} -#endif - -template< class T > -gsl_api inline gsl_constexpr14 T & at( span s, size_t pos ) -{ - return s[ pos ]; -} - -// -// GSL.views: views -// - -// -// not_null<> - Wrap any indirection and enforce non-null. -// - -template< class T > -class not_null; - -namespace detail { - -// helper class to figure out the pointed-to type of a pointer -#if gsl_CPP11_OR_GREATER -template< class T, class E = void > -struct element_type_helper -{ - // For types without a member element_type (this will handle raw pointers) - typedef typename std::remove_reference< decltype( *std::declval() ) >::type type; -}; - -template< class T > -struct element_type_helper< T, std17::void_t< typename T::element_type > > -{ - // For types with a member element_type - typedef typename T::element_type type; -}; -#else -// Pre-C++11, we cannot have decltype, so we cannot handle types without a member element_type -template< class T, class E = void > -struct element_type_helper -{ - typedef typename T::element_type type; -}; - -template< class T > -struct element_type_helper< T* > -{ - typedef T type; -}; -#endif - -template< class T > -struct is_not_null_oracle : std11::false_type { }; -template< class T > -struct is_not_null_oracle< not_null > : std11::true_type { }; - -template< class T, bool IsCopyable = true > -struct not_null_data; -#if gsl_HAVE( RVALUE_REFERENCE ) && gsl_HAVE( TYPE_TRAITS ) -template< class T > -struct not_null_data< T, false > -{ - T ptr_; - - gsl_constexpr14 not_null_data( T && _ptr ) gsl_noexcept - : ptr_( std::move( _ptr ) ) - { - } - - gsl_constexpr14 not_null_data( not_null_data && other ) gsl_noexcept - : ptr_( std::move( other.ptr_ ) ) - { - } - gsl_constexpr14 not_null_data & operator=( not_null_data && other ) gsl_noexcept - { - ptr_ = std::move( other.ptr_ ); - return *this; - } - -gsl_is_delete_access: - not_null_data( not_null_data const & other ) gsl_is_delete; - not_null_data & operator=( not_null_data const & other ) gsl_is_delete; -}; -#endif // gsl_HAVE( RVALUE_REFERENCE ) && gsl_HAVE( TYPE_TRAITS ) -template< class T > -struct not_null_data< T, true > -{ - T ptr_; - - gsl_constexpr14 not_null_data( T const & _ptr ) gsl_noexcept - : ptr_( _ptr ) - { - } - -#if gsl_HAVE( RVALUE_REFERENCE ) - gsl_constexpr14 not_null_data( T && _ptr ) gsl_noexcept - : ptr_( std::move( _ptr ) ) - { - } - - gsl_constexpr14 not_null_data( not_null_data && other ) gsl_noexcept - : ptr_( std::move( other.ptr_ ) ) - { - } - gsl_constexpr14 not_null_data & operator=( not_null_data && other ) gsl_noexcept - { - ptr_ = std::move( other.ptr_ ); - return *this; - } -#endif // gsl_HAVE( RVALUE_REFERENCE ) - - gsl_constexpr14 not_null_data( not_null_data const & other ) - : ptr_( other.ptr_ ) - { - gsl_Expects( ptr_ != gsl_nullptr ); - } - gsl_constexpr14 not_null_data & operator=( not_null_data const & other ) - { - gsl_Expects( other.ptr_ != gsl_nullptr ); - ptr_ = other.ptr_; - return *this; - } -}; - -template< class T > -struct is_copyable -#if gsl_HAVE( TYPE_TRAITS ) -: std11::integral_constant< bool, std::is_copy_constructible::value && std::is_copy_assignable::value > -#else -: std11::true_type -#endif -{ -}; -#if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( UNIQUE_PTR ) && gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 140 ) -// Type traits are buggy in VC++ 2013, so we explicitly declare `unique_ptr<>` non-copyable. -template< class T, class Deleter > -struct is_copyable< std::unique_ptr< T, Deleter > > : std11::false_type -{ -}; -#endif - -} // namespace detail - -template< class T > -class not_null -{ -private: - detail::not_null_data< T, detail::is_copyable< T >::value > data_; - - // need to access `not_null::data_` - template< class U > - friend class not_null; - -public: - typedef typename detail::element_type_helper::type element_type; - -#if gsl_HAVE( TYPE_TRAITS ) - static_assert( std::is_assignable::value, "T cannot be assigned nullptr." ); -#endif - -#if gsl_CONFIG( NOT_NULL_EXPLICIT_CTOR ) -# if gsl_HAVE( RVALUE_REFERENCE ) - template< class U - // In Clang 3.x, `is_constructible>, unique_ptr>` tries to instantiate the copy constructor of `unique_ptr<>`, triggering an error. - // Note that Apple Clang's `__clang_major__` etc. are different from regular Clang. -# if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_constructible::value ), int >::type = 0 -# endif - > - gsl_constexpr14 explicit not_null( U other ) - : data_( T( std::move( other ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } -# else // a.k.a. !gsl_HAVE( RVALUE_REFERENCE ) - template< class U > - gsl_constexpr14 explicit not_null( U const& other ) - : data_( T( other ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } -# endif // gsl_HAVE( RVALUE_REFERENCE ) -#else // a.k.a. !gsl_CONFIG( NOT_NULL_EXPLICIT_CTOR ) -# if gsl_HAVE( RVALUE_REFERENCE ) - // In Clang 3.x, `is_constructible>, unique_ptr>` tries to instantiate the copy constructor of `unique_ptr<>`, triggering an error. - // Note that Apple Clang's `__clang_major__` etc. are different from regular Clang. -# if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_constructible::value && !std::is_convertible::value ), int >::type = 0 - > - gsl_constexpr14 explicit not_null( U other ) - : data_( T( std::move( other ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } - - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_convertible::value ), int >::type = 0 - > - gsl_constexpr14 not_null( U other ) - : data_( T( std::move( other ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } -# else // a.k.a. !( gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) - // If type_traits are not available, then we can't distinguish `is_convertible<>` and `is_constructible<>`, so we unconditionally permit implicit construction. - template< class U > - gsl_constexpr14 not_null( U other ) - : data_( T( std::move( other ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } -# endif // gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) -# else // a.k.a. !gsl_HAVE( RVALUE_REFERENCE ) - template< class U > - gsl_constexpr14 not_null( U const& other ) - : data_( T( other ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } -# endif // gsl_HAVE( RVALUE_REFERENCE ) -#endif // gsl_CONFIG( NOT_NULL_EXPLICIT_CTOR ) - -# if gsl_HAVE( RVALUE_REFERENCE ) - // In Clang 3.x, `is_constructible>, unique_ptr>` tries to instantiate the copy constructor of `unique_ptr<>`, triggering an error. - // Note that Apple Clang's `__clang_major__` etc. are different from regular Clang. -# if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_constructible::value && !std::is_convertible::value ), int >::type = 0 - > - gsl_constexpr14 explicit not_null( not_null other ) - : data_( T( std::move( other.data_.ptr_ ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } - - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_convertible::value ), int >::type = 0 - > - gsl_constexpr14 not_null( not_null other ) - : data_( T( std::move( other.data_.ptr_ ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } -# else // a.k.a. !( gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) - // If type_traits are not available, then we can't distinguish `is_convertible<>` and `is_constructible<>`, so we unconditionally permit implicit construction. - template< class U > - gsl_constexpr14 not_null( not_null other ) - : data_( T( std::move( other.data_.ptr_ ) ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } - template< class U > - gsl_constexpr14 not_null& operator=( not_null other ) - { - gsl_Expects( other.data_.ptr_ != gsl_nullptr ); - data_.ptr_ = std::move( other.data_.ptr_ ); - return *this; - } -# endif // gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && !gsl_BETWEEN( gsl_COMPILER_CLANG_VERSION, 1, 400 ) && !gsl_BETWEEN( gsl_COMPILER_APPLECLANG_VERSION, 1, 1001 ) -# else // a.k.a. !gsl_HAVE( RVALUE_REFERENCE ) - template< class U > - gsl_constexpr14 not_null( not_null const& other ) - : data_( T( other.data_.ptr_ ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } - template< class U > - gsl_constexpr14 not_null& operator=( not_null const & other ) - { - gsl_Expects( other.data_.ptr_ != gsl_nullptr ); - data_.ptr_ = other.data_.ptr_; - return *this; - } -# endif // gsl_HAVE( RVALUE_REFERENCE ) - -#if gsl_CONFIG( TRANSPARENT_NOT_NULL ) - gsl_constexpr14 element_type * - get() const - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return data_.ptr_.get(); - } -#else -# if gsl_CONFIG( NOT_NULL_GET_BY_CONST_REF ) - gsl_constexpr14 T const & get() const - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return data_.ptr_; - } -# else - gsl_constexpr14 T get() const - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return data_.ptr_; - } -# endif -#endif - - // We want an implicit conversion operator that can be used to convert from both lvalues (by - // const reference or by copy) and rvalues (by move). So it seems like we could define - // - // template< class U > - // operator U const &() const & { ... } - // template< class U > - // operator U &&() && { ... } - // - // However, having two conversion operators with different return types renders the assignment - // operator of the result type ambiguous: - // - // not_null> p( ... ); - // std::unique_ptr q; - // q = std::move( p ); // ambiguous - // - // To avoid this ambiguity, we have both overloads of the conversion operator return `U` - // rather than `U const &` or `U &&`. This implies that converting an lvalue always induces - // a copy, which can cause unnecessary copies or even fail to compile in some situations: - // - // not_null> sp( ... ); - // std::shared_ptr const & rs = sp; // unnecessary copy - // std::unique_ptr const & ru = p; // error: cannot copy `unique_ptr` - // - // However, these situations are rather unusual, and the following, more frequent situations - // remain unimpaired: - // - // std::shared_ptr vs = sp; // no extra copy - // std::unique_ptr vu = std::move( p ); - -#if gsl_HAVE( RVALUE_REFERENCE ) && gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && gsl_HAVE( EXPLICIT ) - // explicit conversion operator - - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_constructible::value && !std::is_convertible::value && !detail::is_not_null_oracle::value ), int >::type = 0 - > - gsl_constexpr14 explicit - operator U() const -# if gsl_HAVE( FUNCTION_REF_QUALIFIER ) - & -# endif - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return U( data_.ptr_ ); - } -# if gsl_HAVE( FUNCTION_REF_QUALIFIER ) - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_constructible::value && !std::is_convertible::value && !detail::is_not_null_oracle::value ), int >::type = 0 - > - gsl_constexpr14 explicit - operator U() && - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return U( std::move( data_.ptr_ ) ); - } -# endif - - // implicit conversion operator - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_constructible::value && std::is_convertible::value && !detail::is_not_null_oracle::value ), int >::type = 0 - > - gsl_constexpr14 - operator U() const -# if gsl_HAVE( FUNCTION_REF_QUALIFIER ) - & -# endif - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return data_.ptr_; - } -# if gsl_HAVE( FUNCTION_REF_QUALIFIER ) - template< class U - // We *have* to use SFINAE with an NTTP arg here, otherwise the overload is ambiguous. - , typename std::enable_if< ( std::is_convertible::value && !detail::is_not_null_oracle::value ), int >::type = 0 - > - gsl_constexpr14 - operator U() && - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return std::move( data_.ptr_ ); - } -# endif -#else // a.k.a. #if !( gsl_HAVE( RVALUE_REFERENCE ) && gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && gsl_HAVE( EXPLICIT ) ) - template< class U > - gsl_constexpr14 - operator U() const - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return data_.ptr_; - } -#endif // gsl_HAVE( RVALUE_REFERENCE ) && gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) && gsl_HAVE( EXPLICIT ) - - gsl_constexpr14 T const & - operator->() const - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return data_.ptr_; - } - - gsl_constexpr14 element_type & - operator*() const - { - gsl_Ensures( data_.ptr_ != gsl_nullptr ); - return *data_.ptr_; - } - -#if gsl_HAVE( RVALUE_REFERENCE ) - // Visual C++ 2013 doesn't generate default move constructors, so we declare them explicitly. - gsl_constexpr14 not_null( not_null && other ) gsl_noexcept - : data_( std::move( other.data_ ) ) - { - gsl_Expects( data_.ptr_ != gsl_nullptr ); - } - gsl_constexpr14 not_null & operator=( not_null && other ) gsl_noexcept - { - gsl_Expects( other.data_.ptr_ != gsl_nullptr ); - data_ = std::move( other.data_ ); - return *this; - } -#endif // gsl_HAVE( RVALUE_REFERENCE ) - -#if gsl_HAVE( IS_DEFAULT ) - gsl_constexpr14 not_null( not_null const & other ) = default; - gsl_constexpr14 not_null & operator=( not_null const & other ) = default; -#endif - -gsl_is_delete_access: - not_null() gsl_is_delete; - // prevent compilation when initialized with a nullptr or literal 0: -#if gsl_HAVE( NULLPTR ) - not_null( std::nullptr_t ) gsl_is_delete; - not_null & operator=( std::nullptr_t ) gsl_is_delete; -#else - not_null( int ) gsl_is_delete; - not_null & operator=( int ) gsl_is_delete; -#endif - - // unwanted operators...pointers only point to single objects! - not_null & operator++() gsl_is_delete; - not_null & operator--() gsl_is_delete; - not_null operator++( int ) gsl_is_delete; - not_null operator--( int ) gsl_is_delete; - not_null & operator+ ( size_t ) gsl_is_delete; - not_null & operator+=( size_t ) gsl_is_delete; - not_null & operator- ( size_t ) gsl_is_delete; - not_null & operator-=( size_t ) gsl_is_delete; - not_null & operator+=( std::ptrdiff_t ) gsl_is_delete; - not_null & operator-=( std::ptrdiff_t ) gsl_is_delete; - void operator[]( std::ptrdiff_t ) const gsl_is_delete; -}; -#if gsl_HAVE( DEDUCTION_GUIDES ) -template< class U > -not_null( U ) -> not_null; -template< class U > -not_null( not_null ) -> not_null; -#endif - -#if gsl_HAVE( NULLPTR ) -void make_not_null( std::nullptr_t ) gsl_is_delete; -#endif // gsl_HAVE( NULLPTR ) -#if gsl_HAVE( RVALUE_REFERENCE ) -template< class U > -not_null make_not_null( U u ) -{ - return not_null( std::move( u ) ); -} -template< class U > -not_null make_not_null( not_null u ) -{ - return std::move( u ); -} -#else // a.k.a. !gsl_HAVE( RVALUE_REFERENCE ) -template< class U > -not_null make_not_null( U const & u ) -{ - return not_null( u ); -} -template< class U > -not_null make_not_null( not_null const & u ) -{ - return u; -} -#endif // gsl_HAVE( RVALUE_REFERENCE ) - - -// not_null with implicit constructor, allowing copy-initialization: - -template< class T > -class not_null_ic : public not_null -{ -public: - template< class U - gsl_ENABLE_IF_(( std::is_constructible::value )) - > - gsl_constexpr14 -#if gsl_HAVE( RVALUE_REFERENCE ) - not_null_ic( U && u ) - : not_null( std::forward( u ) ) -#else - not_null_ic( U const & u ) - : not_null( u ) -#endif - {} -}; - -// more not_null unwanted operators - -template< class T, class U > -std::ptrdiff_t operator-( not_null const &, not_null const & ) gsl_is_delete; - -template< class T > -not_null operator-( not_null const &, std::ptrdiff_t ) gsl_is_delete; - -template< class T > -not_null operator+( not_null const &, std::ptrdiff_t ) gsl_is_delete; - -template< class T > -not_null operator+( std::ptrdiff_t, not_null const & ) gsl_is_delete; - -// not_null comparisons - -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() == std::declval() ) -operator==( not_null const & l, not_null const & r ) -{ - return l.operator->() == r.operator->(); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() == std::declval() ) -operator==( not_null const & l, U const & r ) -{ - return l.operator->() == r; -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() == std::declval() ) -operator==( T const & l, not_null const & r ) -{ - return l == r.operator->(); -} - -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() < std::declval() ) -operator<( not_null const & l, not_null const & r ) -{ - return l.operator->() < r.operator->(); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() < std::declval() ) -operator<( not_null const & l, U const & r ) -{ - return l.operator->() < r; -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() < std::declval() ) -operator<( T const & l, not_null const & r ) -{ - return l < r.operator->(); -} - -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() == std::declval() ) ) -operator!=( not_null const & l, not_null const & r ) -{ - return !( l == r ); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() == std::declval() ) ) -operator!=( not_null const & l, U const & r ) -{ - return !( l == r ); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() == std::declval() ) ) -operator!=( T const & l, not_null const & r ) -{ - return !( l == r ); -} - -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() < std::declval() ) ) -operator<=( not_null const & l, not_null const & r ) -{ - return !( r < l ); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() < std::declval() ) ) -operator<=( not_null const & l, U const & r ) -{ - return !( r < l ); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() < std::declval() ) ) -operator<=( T const & l, not_null const & r ) -{ - return !( r < l ); -} - -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() < std::declval() ) -operator>( not_null const & l, not_null const & r ) -{ - return r < l; -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() < std::declval() ) -operator>( not_null const & l, U const & r ) -{ - return r < l; -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, std::declval() < std::declval() ) -operator>( T const & l, not_null const & r ) -{ - return r < l; -} - -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() < std::declval() ) ) -operator>=( not_null const & l, not_null const & r ) -{ - return !( l < r ); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() < std::declval() ) ) -operator>=( not_null const & l, U const & r ) -{ - return !( l < r ); -} -template< class T, class U > -inline gsl_constexpr gsl_DECLTYPE_( bool, !( std::declval() < std::declval() ) ) -operator>=( T const & l, not_null const & r ) -{ - return !( l < r ); -} - -// print not_null - -template< class CharType, class Traits, class T > -std::basic_ostream< CharType, Traits > & operator<<( std::basic_ostream< CharType, Traits > & os, not_null const & p ) -{ - return os << p.operator->(); -} - - -// -// Byte-specific type. -// -#if gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) - enum class gsl_may_alias byte : unsigned char {}; -#else - struct gsl_may_alias byte { typedef unsigned char type; type v; }; -#endif - -template< class T > -gsl_api inline gsl_constexpr byte to_byte( T v ) gsl_noexcept -{ -#if gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) - return static_cast( v ); -#elif gsl_HAVE( CONSTEXPR_11 ) - return { static_cast( v ) }; -#else - byte b = { static_cast( v ) }; return b; -#endif -} - -template< class IntegerType gsl_ENABLE_IF_(( std::is_integral::value )) > -gsl_api inline gsl_constexpr IntegerType to_integer( byte b ) gsl_noexcept -{ -#if gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) - return static_cast::type>( b ); -#else - return b.v; -#endif -} - -gsl_api inline gsl_constexpr unsigned char to_uchar( byte b ) gsl_noexcept -{ - return to_integer( b ); -} - -gsl_api inline gsl_constexpr unsigned char to_uchar( int i ) gsl_noexcept -{ - return static_cast( i ); -} - -template< class IntegerType gsl_ENABLE_IF_(( std::is_integral::value )) > -gsl_api inline gsl_constexpr14 byte & operator<<=( byte & b, IntegerType shift ) gsl_noexcept -{ -#if gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) - return b = ::gsl::to_byte( ::gsl::to_uchar( b ) << shift ); -#else - b.v = ::gsl::to_uchar( b.v << shift ); return b; -#endif -} - -template< class IntegerType gsl_ENABLE_IF_(( std::is_integral::value )) > -gsl_api inline gsl_constexpr byte operator<<( byte b, IntegerType shift ) gsl_noexcept -{ - return ::gsl::to_byte( ::gsl::to_uchar( b ) << shift ); -} - -template< class IntegerType gsl_ENABLE_IF_(( std::is_integral::value )) > -gsl_api inline gsl_constexpr14 byte & operator>>=( byte & b, IntegerType shift ) gsl_noexcept -{ -#if gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) - return b = ::gsl::to_byte( ::gsl::to_uchar( b ) >> shift ); -#else - b.v = ::gsl::to_uchar( b.v >> shift ); return b; -#endif -} - -template< class IntegerType gsl_ENABLE_IF_(( std::is_integral::value )) > -gsl_api inline gsl_constexpr byte operator>>( byte b, IntegerType shift ) gsl_noexcept -{ - return ::gsl::to_byte( ::gsl::to_uchar( b ) >> shift ); -} - -#if gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) -gsl_DEFINE_ENUM_BITMASK_OPERATORS( byte ) -gsl_DEFINE_ENUM_RELATIONAL_OPERATORS( byte ) -#else // a.k.a. !gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) -gsl_api inline gsl_constexpr bool operator==( byte l, byte r ) gsl_noexcept -{ - return l.v == r.v; -} - -gsl_api inline gsl_constexpr bool operator!=( byte l, byte r ) gsl_noexcept -{ - return !( l == r ); -} - -gsl_api inline gsl_constexpr bool operator< ( byte l, byte r ) gsl_noexcept -{ - return l.v < r.v; -} - -gsl_api inline gsl_constexpr bool operator<=( byte l, byte r ) gsl_noexcept -{ - return !( r < l ); -} - -gsl_api inline gsl_constexpr bool operator> ( byte l, byte r ) gsl_noexcept -{ - return ( r < l ); -} - -gsl_api inline gsl_constexpr bool operator>=( byte l, byte r ) gsl_noexcept -{ - return !( l < r ); -} - -gsl_api inline gsl_constexpr14 byte & operator|=( byte & l, byte r ) gsl_noexcept -{ - l.v |= r.v; return l; -} - -gsl_api inline gsl_constexpr byte operator|( byte l, byte r ) gsl_noexcept -{ - return ::gsl::to_byte( l.v | r.v ); -} - -gsl_api inline gsl_constexpr14 byte & operator&=( byte & l, byte r ) gsl_noexcept -{ - l.v &= r.v; return l; -} - -gsl_api inline gsl_constexpr byte operator&( byte l, byte r ) gsl_noexcept -{ - return ::gsl::to_byte( l.v & r.v ); -} - -gsl_api inline gsl_constexpr14 byte & operator^=( byte & l, byte r ) gsl_noexcept -{ - l.v ^= r.v; return l; -} - -gsl_api inline gsl_constexpr byte operator^( byte l, byte r ) gsl_noexcept -{ - return ::gsl::to_byte( l.v ^ r.v ); -} - -gsl_api inline gsl_constexpr byte operator~( byte b ) gsl_noexcept -{ - return ::gsl::to_byte( ~b.v ); -} -#endif // gsl_HAVE( ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE ) - -#if gsl_FEATURE_TO_STD( WITH_CONTAINER ) - -// Tag to select span constructor taking a container: - -struct with_container_t { gsl_constexpr with_container_t() gsl_noexcept {} }; -const gsl_constexpr with_container_t with_container; // TODO: this can lead to ODR violations because the symbol will be defined in multiple translation units - -#endif - -// -// span<> - A 1D view of contiguous T's, replace (*,len). -// -template< class T > -class span -{ - template< class U > friend class span; - -public: - typedef gsl_CONFIG_SPAN_INDEX_TYPE index_type; - - typedef T element_type; - typedef typename std11::remove_cv< T >::type value_type; - - typedef T & reference; - typedef T * pointer; - typedef T const * const_pointer; - typedef T const & const_reference; - - typedef pointer iterator; - typedef const_pointer const_iterator; - - typedef std::reverse_iterator< iterator > reverse_iterator; - typedef std::reverse_iterator< const_iterator > const_reverse_iterator; - - typedef typename std::iterator_traits< iterator >::difference_type difference_type; - - // 26.7.3.2 Constructors, copy, and assignment [span.cons] - - gsl_api gsl_constexpr14 span() gsl_noexcept - : first_( gsl_nullptr ) - , last_ ( gsl_nullptr ) - { - } - -#if ! gsl_DEPRECATE_TO_LEVEL( 5 ) - -#if gsl_HAVE( NULLPTR ) - gsl_api gsl_constexpr14 span( std::nullptr_t, index_type size_in ) - : first_( nullptr ) - , last_ ( nullptr ) - { - gsl_Expects( size_in == 0 ); - } -#endif - -#if gsl_HAVE( IS_DELETE ) - gsl_DEPRECATED - gsl_api gsl_constexpr span( reference data_in ) - : span( &data_in, 1 ) - {} - - gsl_api gsl_constexpr span( element_type && ) = delete; -#endif - -#endif // deprecate - - gsl_api gsl_constexpr14 span( pointer data_in, index_type size_in ) - : first_( data_in ) - , last_ ( data_in + size_in ) - { - gsl_Expects( size_in == 0 || ( size_in > 0 && data_in != gsl_nullptr ) ); - } - - gsl_api gsl_constexpr14 span( pointer first_in, pointer last_in ) - : first_( first_in ) - , last_ ( last_in ) - { - gsl_Expects( first_in <= last_in ); - } - -#if ! gsl_DEPRECATE_TO_LEVEL( 5 ) - - template< class U > - gsl_api gsl_constexpr14 span( U * data_in, index_type size_in ) - : first_( data_in ) - , last_ ( data_in + size_in ) - { - gsl_Expects( size_in == 0 || ( size_in > 0 && data_in != gsl_nullptr ) ); - } - -#endif // deprecate - -#if ! gsl_DEPRECATE_TO_LEVEL( 5 ) - template< class U, size_t N > - gsl_api gsl_constexpr span( U (&arr)[N] ) gsl_noexcept - : first_( gsl_ADDRESSOF( arr[0] ) ) - , last_ ( gsl_ADDRESSOF( arr[0] ) + N ) - {} -#else - template< size_t N - gsl_ENABLE_IF_(( std::is_convertible::value )) - > - gsl_api gsl_constexpr span( element_type (&arr)[N] ) gsl_noexcept - : first_( gsl_ADDRESSOF( arr[0] ) ) - , last_ ( gsl_ADDRESSOF( arr[0] ) + N ) - {} -#endif // deprecate - -#if gsl_HAVE( ARRAY ) -#if ! gsl_DEPRECATE_TO_LEVEL( 5 ) - - template< class U, size_t N > - gsl_constexpr span( std::array< U, N > & arr ) - : first_( arr.data() ) - , last_ ( arr.data() + N ) - {} - - template< class U, size_t N > - gsl_constexpr span( std::array< U, N > const & arr ) - : first_( arr.data() ) - , last_ ( arr.data() + N ) - {} - -#else - - template< size_t N - gsl_ENABLE_IF_(( std::is_convertible::value )) - > - gsl_constexpr span( std::array< value_type, N > & arr ) - : first_( arr.data() ) - , last_ ( arr.data() + N ) - {} - - template< size_t N - gsl_ENABLE_IF_(( std::is_convertible::value )) - > - gsl_constexpr span( std::array< value_type, N > const & arr ) - : first_( arr.data() ) - , last_ ( arr.data() + N ) - {} - -#endif // deprecate -#endif // gsl_HAVE( ARRAY ) - -#if gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) - template< class Container - gsl_ENABLE_IF_(( detail::is_compatible_container< Container, element_type >::value )) - > - gsl_constexpr span( Container & cont ) gsl_noexcept - : first_( std17::data( cont ) ) - , last_ ( std17::data( cont ) + std17::size( cont ) ) - {} - - template< class Container - gsl_ENABLE_IF_(( - std::is_const< element_type >::value - && detail::is_compatible_container< Container, element_type >::value - )) - > - gsl_constexpr span( Container const & cont ) gsl_noexcept - : first_( std17::data( cont ) ) - , last_ ( std17::data( cont ) + std17::size( cont ) ) - {} - -#elif gsl_HAVE( UNCONSTRAINED_SPAN_CONTAINER_CTOR ) - - template< class Container > - gsl_constexpr span( Container & cont ) - : first_( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) ) - , last_ ( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) + cont.size() ) - {} - - template< class Container > - gsl_constexpr span( Container const & cont ) - : first_( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) ) - , last_ ( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) + cont.size() ) - {} - -#endif - -#if gsl_FEATURE_TO_STD( WITH_CONTAINER ) - - template< class Container > - gsl_constexpr span( with_container_t, Container & cont ) gsl_noexcept - : first_( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) ) - , last_ ( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) + cont.size() ) - {} - - template< class Container > - gsl_constexpr span( with_container_t, Container const & cont ) gsl_noexcept - : first_( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) ) - , last_ ( cont.size() == 0 ? gsl_nullptr : gsl_ADDRESSOF( cont[0] ) + cont.size() ) - {} - -#endif - -#if !gsl_DEPRECATE_TO_LEVEL( 4 ) - // constructor taking shared_ptr deprecated since 0.29.0 - -# if gsl_HAVE( SHARED_PTR ) - gsl_DEPRECATED - gsl_constexpr span( shared_ptr const & ptr ) - : first_( ptr.get() ) - , last_ ( ptr.get() ? ptr.get() + 1 : gsl_nullptr ) - {} -# endif - - // constructors taking unique_ptr deprecated since 0.29.0 - -# if gsl_HAVE( UNIQUE_PTR ) -# if gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) - template< class ArrayElementType = typename std::add_pointer::type > -# else - template< class ArrayElementType > -# endif - gsl_DEPRECATED - gsl_constexpr span( unique_ptr const & ptr, index_type count ) - : first_( ptr.get() ) - , last_ ( ptr.get() + count ) - {} - - gsl_DEPRECATED - gsl_constexpr span( unique_ptr const & ptr ) - : first_( ptr.get() ) - , last_ ( ptr.get() ? ptr.get() + 1 : gsl_nullptr ) - {} -# endif - -#endif // deprecate shared_ptr, unique_ptr - -#if gsl_HAVE( IS_DEFAULT ) && ! gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 430, 600) - gsl_constexpr span( span && ) gsl_noexcept = default; - gsl_constexpr span( span const & ) = default; -#else - gsl_api gsl_constexpr span( span const & other ) - : first_( other.begin() ) - , last_ ( other.end() ) - {} -#endif - -#if gsl_HAVE( IS_DEFAULT ) - gsl_constexpr14 span & operator=( span && ) gsl_noexcept = default; - gsl_constexpr14 span & operator=( span const & ) gsl_noexcept = default; -#else - gsl_constexpr14 span & operator=( span other ) gsl_noexcept - { - first_ = other.first_; - last_ = other.last_; - return *this; - } -#endif - - template< class U - gsl_ENABLE_IF_(( std::is_convertible::value )) - > - gsl_api gsl_constexpr span( span const & other ) - : first_( other.begin() ) - , last_ ( other.end() ) - {} - -#if 0 - // Converting from other span ? - template< class U > operator=(); -#endif - - // 26.7.3.3 Subviews [span.sub] - - gsl_api gsl_constexpr14 span first( index_type count ) const - { - gsl_Expects( std::size_t( count ) <= std::size_t( this->size() ) ); - return span( this->data(), count ); - } - - gsl_api gsl_constexpr14 span last( index_type count ) const - { - gsl_Expects( std::size_t( count ) <= std::size_t( this->size() ) ); - return span( this->data() + this->size() - count, count ); - } - - gsl_api gsl_constexpr14 span subspan( index_type offset ) const - { - gsl_Expects( std::size_t( offset ) <= std::size_t( this->size() ) ); - return span( this->data() + offset, this->size() - offset ); - } - - gsl_api gsl_constexpr14 span subspan( index_type offset, index_type count ) const - { - gsl_Expects( - std::size_t( offset ) <= std::size_t( this->size() ) && - std::size_t( count ) <= std::size_t( this->size() - offset ) ); - return span( this->data() + offset, count ); - } - - // 26.7.3.4 Observers [span.obs] - - gsl_api gsl_constexpr index_type size() const gsl_noexcept - { - return narrow_cast( last_ - first_ ); - } - - gsl_api gsl_constexpr std::ptrdiff_t ssize() const gsl_noexcept - { - return narrow_cast( last_ - first_ ); - } - - gsl_api gsl_constexpr index_type size_bytes() const gsl_noexcept - { - return size() * narrow_cast( sizeof( element_type ) ); - } - - gsl_api gsl_constexpr bool empty() const gsl_noexcept - { - return size() == 0; - } - - // 26.7.3.5 Element access [span.elem] - - gsl_api gsl_constexpr14 reference operator[]( index_type pos ) const - { - gsl_Expects( pos < size() ); - return first_[ pos ]; - } - -#if ! gsl_DEPRECATE_TO_LEVEL( 6 ) - gsl_DEPRECATED_MSG("use subscript indexing instead") - gsl_api gsl_constexpr14 reference operator()( index_type pos ) const - { - return (*this)[ pos ]; - } - - gsl_DEPRECATED_MSG("use subscript indexing instead") - gsl_api gsl_constexpr14 reference at( index_type pos ) const - { - return (*this)[ pos ]; - } -#endif // deprecate - - gsl_api gsl_constexpr14 reference front() const - { - gsl_Expects( first_ != last_ ); - return *first_; - } - - gsl_api gsl_constexpr14 reference back() const - { - gsl_Expects( first_ != last_ ); - return *(last_ - 1); - } - - gsl_api gsl_constexpr pointer data() const gsl_noexcept - { - return first_; - } - - // 26.7.3.6 Iterator support [span.iterators] - - gsl_api gsl_constexpr iterator begin() const gsl_noexcept - { - return iterator( first_ ); - } - - gsl_api gsl_constexpr iterator end() const gsl_noexcept - { - return iterator( last_ ); - } - - gsl_api gsl_constexpr const_iterator cbegin() const gsl_noexcept - { -#if gsl_CPP11_OR_GREATER - return { begin() }; -#else - return const_iterator( begin() ); -#endif - } - - gsl_api gsl_constexpr const_iterator cend() const gsl_noexcept - { -#if gsl_CPP11_OR_GREATER - return { end() }; -#else - return const_iterator( end() ); -#endif - } - - gsl_constexpr17 reverse_iterator rbegin() const gsl_noexcept - { - return reverse_iterator( end() ); - } - - gsl_constexpr17 reverse_iterator rend() const gsl_noexcept - { - return reverse_iterator( begin() ); - } - - gsl_constexpr17 const_reverse_iterator crbegin() const gsl_noexcept - { - return const_reverse_iterator( cend() ); - } - - gsl_constexpr17 const_reverse_iterator crend() const gsl_noexcept - { - return const_reverse_iterator( cbegin() ); - } - - gsl_constexpr14 void swap( span & other ) gsl_noexcept - { - std::swap( first_, other.first_ ); - std::swap( last_ , other.last_ ); - } - -#if ! gsl_DEPRECATE_TO_LEVEL( 3 ) - // member length() deprecated since 0.29.0 - - gsl_DEPRECATED_MSG("use size() instead") - gsl_api gsl_constexpr index_type length() const gsl_noexcept - { - return size(); - } - - // member length_bytes() deprecated since 0.29.0 - - gsl_DEPRECATED_MSG("use size_bytes() instead") - gsl_api gsl_constexpr index_type length_bytes() const gsl_noexcept - { - return size_bytes(); - } -#endif - -#if ! gsl_DEPRECATE_TO_LEVEL( 2 ) - // member as_bytes(), as_writeable_bytes deprecated since 0.17.0 - - gsl_DEPRECATED_MSG("use free function gsl::as_bytes() instead") - gsl_api span< const byte > as_bytes() const gsl_noexcept - { - return span< const byte >( reinterpret_cast( data() ), size_bytes() ); // NOLINT - } - - gsl_DEPRECATED_MSG("use free function gsl::as_writable_bytes() instead") - gsl_api span< byte > as_writeable_bytes() const gsl_noexcept - { - return span< byte >( reinterpret_cast( data() ), size_bytes() ); // NOLINT - } - -#endif - - template< class U > - gsl_api span< U > as_span() const - { - gsl_Expects( ( this->size_bytes() % sizeof(U) ) == 0 ); - return span< U >( reinterpret_cast( this->data() ), this->size_bytes() / sizeof( U ) ); // NOLINT - } - -private: - pointer first_; - pointer last_; -}; - -// class template argument deduction guides: - -#if gsl_HAVE( DEDUCTION_GUIDES ) // gsl_CPP17_OR_GREATER - -template< class T, size_t N > -span( T (&)[N] ) -> span; - -template< class T, size_t N > -span( std::array & ) -> span; - -template< class T, size_t N > -span( std::array const & ) -> span; - -template< class Container > -span( Container& ) -> span; - -template< class Container > -span( Container const & ) -> span; - -#endif // gsl_HAVE( DEDUCTION_GUIDES ) - -// 26.7.3.7 Comparison operators [span.comparison] - -#if gsl_CONFIG( ALLOWS_SPAN_COMPARISON ) -# if gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - -template< class T, class U > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr bool operator==( span const & l, span const & r ) -{ - return l.size() == r.size() - && (l.begin() == r.begin() || std::equal( l.begin(), l.end(), r.begin() ) ); -} - -template< class T, class U > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr bool operator< ( span const & l, span const & r ) -{ - return std::lexicographical_compare( l.begin(), l.end(), r.begin(), r.end() ); -} - -# else // a.k.a. !gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - -template< class T > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr bool operator==( span const & l, span const & r ) -{ - return l.size() == r.size() - && (l.begin() == r.begin() || std::equal( l.begin(), l.end(), r.begin() ) ); -} - -template< class T > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr bool operator< ( span const & l, span const & r ) -{ - return std::lexicographical_compare( l.begin(), l.end(), r.begin(), r.end() ); -} -# endif // gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - -template< class T, class U > -inline gsl_constexpr bool operator!=( span const & l, span const & r ) -{ - return !( l == r ); -} - -template< class T, class U > -inline gsl_constexpr bool operator<=( span const & l, span const & r ) -{ - return !( r < l ); -} - -template< class T, class U > -inline gsl_constexpr bool operator> ( span const & l, span const & r ) -{ - return ( r < l ); -} - -template< class T, class U > -inline gsl_constexpr bool operator>=( span const & l, span const & r ) -{ - return !( l < r ); -} -#endif // gsl_CONFIG( ALLOWS_SPAN_COMPARISON ) - -// span algorithms - -template< class T > -gsl_api inline gsl_constexpr std::size_t size( span const & spn ) -{ - return static_cast( spn.size() ); -} - -template< class T > -gsl_api inline gsl_constexpr std::ptrdiff_t ssize( span const & spn ) -{ - return spn.ssize(); -} - -namespace detail { - -template< class II, class N, class OI > -gsl_api gsl_constexpr14 inline OI copy_n( II first, N count, OI result ) -{ - if ( count > 0 ) - { - *result++ = *first; - for ( N i = 1; i < count; ++i ) - { - *result++ = *++first; - } - } - return result; -} -} - -template< class T, class U > -gsl_api gsl_constexpr14 inline void copy( span src, span dest ) -{ -#if gsl_CPP14_OR_GREATER // gsl_HAVE( TYPE_TRAITS ) (circumvent Travis clang 3.4) - static_assert( std::is_assignable::value, "Cannot assign elements of source span to elements of destination span" ); -#endif - gsl_Expects( dest.size() >= src.size() ); - detail::copy_n( src.data(), src.size(), dest.data() ); -} - -// span creator functions (see ctors) - -template< class T > -gsl_api inline span< const byte > as_bytes( span spn ) gsl_noexcept -{ - return span< const byte >( reinterpret_cast( spn.data() ), spn.size_bytes() ); // NOLINT -} - -template< class T> -gsl_api inline span< byte > as_writable_bytes( span spn ) gsl_noexcept -{ - return span< byte >( reinterpret_cast( spn.data() ), spn.size_bytes() ); // NOLINT -} - -#if ! gsl_DEPRECATE_TO_LEVEL( 6 ) -template< class T> -gsl_DEPRECATED_MSG("use as_writable_bytes() (different spelling) instead") -gsl_api inline span< byte > as_writeable_bytes( span spn ) gsl_noexcept -{ - return span< byte >( reinterpret_cast( spn.data() ), spn.size_bytes() ); // NOLINT -} -#endif // deprecate - -#if gsl_FEATURE_TO_STD( MAKE_SPAN ) - -template< class T > -gsl_api inline gsl_constexpr span -make_span( T * ptr, typename span::index_type count ) -{ - return span( ptr, count ); -} - -template< class T > -gsl_api inline gsl_constexpr span -make_span( T * first, T * last ) -{ - return span( first, last ); -} - -template< class T, size_t N > -gsl_api inline gsl_constexpr span -make_span( T (&arr)[N] ) -{ - return span( gsl_ADDRESSOF( arr[0] ), N ); -} - -#if gsl_HAVE( ARRAY ) - -template< class T, size_t N > -inline gsl_constexpr span -make_span( std::array & arr ) -{ - return span( arr ); -} - -template< class T, size_t N > -inline gsl_constexpr span -make_span( std::array const & arr ) -{ - return span( arr ); -} -#endif - -#if gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) && gsl_HAVE( AUTO ) - -template< class Container, class EP = decltype( std17::data(std::declval())) > -inline gsl_constexpr auto -make_span( Container & cont ) -> span< typename std::remove_pointer::type > -{ - return span< typename std::remove_pointer::type >( cont ); -} - -template< class Container, class EP = decltype( std17::data(std::declval())) > -inline gsl_constexpr auto -make_span( Container const & cont ) -> span< const typename std::remove_pointer::type > -{ - return span< const typename std::remove_pointer::type >( cont ); -} - -#else - -template< class T > -inline span -make_span( std::vector & cont ) -{ - return span( with_container, cont ); -} - -template< class T > -inline span -make_span( std::vector const & cont ) -{ - return span( with_container, cont ); -} -#endif - -#if gsl_FEATURE_TO_STD( WITH_CONTAINER ) - -template< class Container > -inline gsl_constexpr span -make_span( with_container_t, Container & cont ) gsl_noexcept -{ - return span< typename Container::value_type >( with_container, cont ); -} - -template< class Container > -inline gsl_constexpr span -make_span( with_container_t, Container const & cont ) gsl_noexcept -{ - return span< const typename Container::value_type >( with_container, cont ); -} - -#endif // gsl_FEATURE_TO_STD( WITH_CONTAINER ) - -#if !gsl_DEPRECATE_TO_LEVEL( 4 ) -template< class Ptr > -gsl_DEPRECATED -inline span -make_span( Ptr & ptr ) -{ - return span( ptr ); -} -#endif // !gsl_DEPRECATE_TO_LEVEL( 4 ) - -template< class Ptr > -gsl_DEPRECATED -inline span -make_span( Ptr & ptr, typename span::index_type count ) -{ - return span( ptr, count ); -} - -#endif // gsl_FEATURE_TO_STD( MAKE_SPAN ) - -#if gsl_FEATURE_TO_STD( BYTE_SPAN ) - -template< class T > -gsl_api inline gsl_constexpr span -byte_span( T & t ) gsl_noexcept -{ - return span( reinterpret_cast( &t ), sizeof(T) ); -} - -template< class T > -gsl_api inline gsl_constexpr span -byte_span( T const & t ) gsl_noexcept -{ - return span( reinterpret_cast( &t ), sizeof(T) ); -} - -#endif // gsl_FEATURE_TO_STD( BYTE_SPAN ) - -// -// basic_string_span: -// - -template< class T > -class basic_string_span; - -namespace detail { - -template< class T > -struct is_basic_string_span_oracle : std11::false_type {}; - -template< class T > -struct is_basic_string_span_oracle< basic_string_span > : std11::true_type {}; - -template< class T > -struct is_basic_string_span : is_basic_string_span_oracle< typename std11::remove_cv::type > {}; - -template< class T > -gsl_api inline gsl_constexpr14 std::size_t string_length( T * ptr, std::size_t max ) -{ - if ( ptr == gsl_nullptr || max <= 0 ) - return 0; - - std::size_t len = 0; - while ( len < max && ptr[len] ) // NOLINT - ++len; - - return len; -} - -} // namespace detail - -// -// basic_string_span<> - A view of contiguous characters, replace (*,len). -// -template< class T > -class basic_string_span -{ -public: - typedef T element_type; - typedef span span_type; - - typedef typename span_type::index_type index_type; - typedef typename span_type::difference_type difference_type; - - typedef typename span_type::pointer pointer ; - typedef typename span_type::reference reference ; - - typedef typename span_type::iterator iterator ; - typedef typename span_type::const_iterator const_iterator ; - typedef typename span_type::reverse_iterator reverse_iterator; - typedef typename span_type::const_reverse_iterator const_reverse_iterator; - - // construction: - -#if gsl_HAVE( IS_DEFAULT ) - gsl_constexpr basic_string_span() gsl_noexcept = default; -#else - gsl_api gsl_constexpr basic_string_span() gsl_noexcept {} -#endif - -#if gsl_HAVE( NULLPTR ) - gsl_api gsl_constexpr basic_string_span( std::nullptr_t ) gsl_noexcept - : span_( nullptr, static_cast( 0 ) ) - {} -#endif - -#ifdef __CUDACC_RELAXED_CONSTEXPR__ - gsl_api -#endif // __CUDACC_RELAXED_CONSTEXPR__ - gsl_constexpr basic_string_span( pointer ptr ) - : span_( remove_z( ptr, (std::numeric_limits::max)() ) ) - {} - - gsl_api gsl_constexpr basic_string_span( pointer ptr, index_type count ) - : span_( ptr, count ) - {} - - gsl_api gsl_constexpr basic_string_span( pointer firstElem, pointer lastElem ) - : span_( firstElem, lastElem ) - {} - - template< std::size_t N > - gsl_api gsl_constexpr basic_string_span( element_type (&arr)[N] ) - : span_( remove_z( gsl_ADDRESSOF( arr[0] ), N ) ) - {} - -#if gsl_HAVE( ARRAY ) - - template< std::size_t N > - gsl_constexpr basic_string_span( std::array< typename std11::remove_const::type, N> & arr ) - : span_( remove_z( arr ) ) - {} - - template< std::size_t N > - gsl_constexpr basic_string_span( std::array< typename std11::remove_const::type, N> const & arr ) - : span_( remove_z( arr ) ) - {} - -#endif - -#if gsl_HAVE( CONSTRAINED_SPAN_CONTAINER_CTOR ) - - // Exclude: array, [basic_string,] basic_string_span - - template< class Container - gsl_ENABLE_IF_(( - ! detail::is_std_array< Container >::value - && ! detail::is_basic_string_span< Container >::value - && std::is_convertible< typename Container::pointer, pointer >::value - && std::is_convertible< typename Container::pointer, decltype(std::declval().data()) >::value - )) - > - gsl_constexpr basic_string_span( Container & cont ) - : span_( ( cont ) ) - {} - - // Exclude: array, [basic_string,] basic_string_span - - template< class Container - gsl_ENABLE_IF_(( - ! detail::is_std_array< Container >::value - && ! detail::is_basic_string_span< Container >::value - && std::is_convertible< typename Container::pointer, pointer >::value - && std::is_convertible< typename Container::pointer, decltype(std::declval().data()) >::value - )) - > - gsl_constexpr basic_string_span( Container const & cont ) - : span_( ( cont ) ) - {} - -#elif gsl_HAVE( UNCONSTRAINED_SPAN_CONTAINER_CTOR ) - - template< class Container > - gsl_constexpr basic_string_span( Container & cont ) - : span_( cont ) - {} - - template< class Container > - gsl_constexpr basic_string_span( Container const & cont ) - : span_( cont ) - {} - -#else - - template< class U > - gsl_api gsl_constexpr basic_string_span( span const & rhs ) - : span_( rhs ) - {} - -#endif - -#if gsl_FEATURE_TO_STD( WITH_CONTAINER ) - - template< class Container > - gsl_constexpr basic_string_span( with_container_t, Container & cont ) - : span_( with_container, cont ) - {} -#endif - -#if gsl_HAVE( IS_DEFAULT ) -# if gsl_BETWEEN( gsl_COMPILER_GNUC_VERSION, 440, 600 ) - gsl_constexpr basic_string_span( basic_string_span const & rhs ) = default; - - gsl_constexpr basic_string_span( basic_string_span && rhs ) = default; -# else - gsl_constexpr basic_string_span( basic_string_span const & rhs ) gsl_noexcept = default; - - gsl_constexpr basic_string_span( basic_string_span && rhs ) gsl_noexcept = default; -# endif -#endif - - template< class U - gsl_ENABLE_IF_(( std::is_convertible::pointer, pointer>::value )) - > - gsl_api gsl_constexpr basic_string_span( basic_string_span const & rhs ) - : span_( reinterpret_cast( rhs.data() ), rhs.length() ) // NOLINT - {} - -#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 120 - template< class U - gsl_ENABLE_IF_(( std::is_convertible::pointer, pointer>::value )) - > - gsl_api gsl_constexpr basic_string_span( basic_string_span && rhs ) - : span_( reinterpret_cast( rhs.data() ), rhs.length() ) // NOLINT - {} -#endif - - template< class CharTraits, class Allocator > - gsl_constexpr basic_string_span( - std::basic_string< typename std11::remove_const::type, CharTraits, Allocator > & str ) - : span_( gsl_ADDRESSOF( str[0] ), str.length() ) - {} - - template< class CharTraits, class Allocator > - gsl_constexpr basic_string_span( - std::basic_string< typename std11::remove_const::type, CharTraits, Allocator > const & str ) - : span_( gsl_ADDRESSOF( str[0] ), str.length() ) - {} - - // assignment: - -#if gsl_HAVE( IS_DEFAULT ) - gsl_constexpr14 basic_string_span & operator=( basic_string_span const & rhs ) gsl_noexcept = default; - - gsl_constexpr14 basic_string_span & operator=( basic_string_span && rhs ) gsl_noexcept = default; -#endif - - // sub span: - - /*gsl_api*/ // currently disabled due to an apparent NVCC bug - gsl_constexpr14 basic_string_span first( index_type count ) const - { - return span_.first( count ); - } - - /*gsl_api*/ // currently disabled due to an apparent NVCC bug - gsl_constexpr14 basic_string_span last( index_type count ) const - { - return span_.last( count ); - } - - /*gsl_api*/ // currently disabled due to an apparent NVCC bug - gsl_constexpr14 basic_string_span subspan( index_type offset ) const - { - return span_.subspan( offset ); - } - - /*gsl_api*/ // currently disabled due to an apparent NVCC bug - gsl_constexpr14 basic_string_span subspan( index_type offset, index_type count ) const - { - return span_.subspan( offset, count ); - } - - // observers: - - gsl_api gsl_constexpr index_type length() const gsl_noexcept - { - return span_.size(); - } - - gsl_api gsl_constexpr index_type size() const gsl_noexcept - { - return span_.size(); - } - - gsl_api gsl_constexpr index_type length_bytes() const gsl_noexcept - { - return span_.size_bytes(); - } - - gsl_api gsl_constexpr index_type size_bytes() const gsl_noexcept - { - return span_.size_bytes(); - } - - gsl_api gsl_constexpr bool empty() const gsl_noexcept - { - return size() == 0; - } - - gsl_api gsl_constexpr14 reference operator[]( index_type idx ) const - { - return span_[idx]; - } - -#if ! gsl_DEPRECATE_TO_LEVEL( 6 ) - gsl_DEPRECATED_MSG("use subscript indexing instead") - gsl_api gsl_constexpr14 reference operator()( index_type idx ) const - { - return span_[idx]; - } -#endif // deprecate - - gsl_api gsl_constexpr14 reference front() const - { - return span_.front(); - } - - gsl_api gsl_constexpr14 reference back() const - { - return span_.back(); - } - - gsl_api gsl_constexpr pointer data() const gsl_noexcept - { - return span_.data(); - } - - gsl_api gsl_constexpr iterator begin() const gsl_noexcept - { - return span_.begin(); - } - - gsl_api gsl_constexpr iterator end() const gsl_noexcept - { - return span_.end(); - } - - gsl_constexpr17 reverse_iterator rbegin() const gsl_noexcept - { - return span_.rbegin(); - } - - gsl_constexpr17 reverse_iterator rend() const gsl_noexcept - { - return span_.rend(); - } - - // const version not in p0123r2: - - gsl_api gsl_constexpr const_iterator cbegin() const gsl_noexcept - { - return span_.cbegin(); - } - - gsl_api gsl_constexpr const_iterator cend() const gsl_noexcept - { - return span_.cend(); - } - - gsl_constexpr17 const_reverse_iterator crbegin() const gsl_noexcept - { - return span_.crbegin(); - } - - gsl_constexpr17 const_reverse_iterator crend() const gsl_noexcept - { - return span_.crend(); - } - -private: - gsl_api static gsl_constexpr14 span_type remove_z( pointer const & sz, std::size_t max ) - { - return span_type( sz, detail::string_length( sz, max ) ); - } - -#if gsl_HAVE( ARRAY ) - template< size_t N > - static gsl_constexpr14 span_type remove_z( std::array::type, N> & arr ) - { - return remove_z( gsl_ADDRESSOF( arr[0] ), narrow_cast< std::size_t >( N ) ); - } - - template< size_t N > - static gsl_constexpr14 span_type remove_z( std::array::type, N> const & arr ) - { - return remove_z( gsl_ADDRESSOF( arr[0] ), narrow_cast< std::size_t >( N ) ); - } -#endif - -private: - span_type span_; -}; - -// basic_string_span comparison functions: - -#if gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - -template< class T, class U > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr14 bool operator==( basic_string_span const & l, U const & u ) gsl_noexcept -{ - const basic_string_span< typename std11::add_const::type > r( u ); - - return l.size() == r.size() - && std::equal( l.begin(), l.end(), r.begin() ); -} - -template< class T, class U > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr14 bool operator<( basic_string_span const & l, U const & u ) gsl_noexcept -{ - const basic_string_span< typename std11::add_const::type > r( u ); - - return std::lexicographical_compare( l.begin(), l.end(), r.begin(), r.end() ); -} - -#if gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) - -template< class T, class U - gsl_ENABLE_IF_(( !detail::is_basic_string_span::value )) -> -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr14 bool operator==( U const & u, basic_string_span const & r ) gsl_noexcept -{ - const basic_string_span< typename std11::add_const::type > l( u ); - - return l.size() == r.size() - && std::equal( l.begin(), l.end(), r.begin() ); -} - -template< class T, class U - gsl_ENABLE_IF_(( !detail::is_basic_string_span::value )) -> -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr14 bool operator<( U const & u, basic_string_span const & r ) gsl_noexcept -{ - const basic_string_span< typename std11::add_const::type > l( u ); - - return std::lexicographical_compare( l.begin(), l.end(), r.begin(), r.end() ); -} -#endif - -#else //gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - -template< class T > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr14 bool operator==( basic_string_span const & l, basic_string_span const & r ) gsl_noexcept -{ - return l.size() == r.size() - && std::equal( l.begin(), l.end(), r.begin() ); -} - -template< class T > -gsl_SUPPRESS_MSGSL_WARNING(stl.1) -inline gsl_constexpr14 bool operator<( basic_string_span const & l, basic_string_span const & r ) gsl_noexcept -{ - return std::lexicographical_compare( l.begin(), l.end(), r.begin(), r.end() ); -} - -#endif // gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - -template< class T, class U > -inline gsl_constexpr14 bool operator!=( basic_string_span const & l, U const & r ) gsl_noexcept -{ - return !( l == r ); -} - -template< class T, class U > -inline gsl_constexpr14 bool operator<=( basic_string_span const & l, U const & r ) gsl_noexcept -{ -#if gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) || ! gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - return !( r < l ); -#else - basic_string_span< typename std11::add_const::type > rr( r ); - return !( rr < l ); -#endif -} - -template< class T, class U > -inline gsl_constexpr14 bool operator>( basic_string_span const & l, U const & r ) gsl_noexcept -{ -#if gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) || ! gsl_CONFIG( ALLOWS_NONSTRICT_SPAN_COMPARISON ) - return ( r < l ); -#else - basic_string_span< typename std11::add_const::type > rr( r ); - return ( rr < l ); -#endif -} - -template< class T, class U > -inline gsl_constexpr14 bool operator>=( basic_string_span const & l, U const & r ) gsl_noexcept -{ - return !( l < r ); -} - -#if gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) - -template< class T, class U - gsl_ENABLE_IF_(( !detail::is_basic_string_span::value )) -> -inline gsl_constexpr14 bool operator!=( U const & l, basic_string_span const & r ) gsl_noexcept -{ - return !( l == r ); -} - -template< class T, class U - gsl_ENABLE_IF_(( !detail::is_basic_string_span::value )) -> -inline gsl_constexpr14 bool operator<=( U const & l, basic_string_span const & r ) gsl_noexcept -{ - return !( r < l ); -} - -template< class T, class U - gsl_ENABLE_IF_(( !detail::is_basic_string_span::value )) -> -inline gsl_constexpr14 bool operator>( U const & l, basic_string_span const & r ) gsl_noexcept -{ - return ( r < l ); -} - -template< class T, class U - gsl_ENABLE_IF_(( !detail::is_basic_string_span::value )) -> -inline gsl_constexpr14 bool operator>=( U const & l, basic_string_span const & r ) gsl_noexcept -{ - return !( l < r ); -} - -#endif // gsl_HAVE( DEFAULT_FUNCTION_TEMPLATE_ARG ) - -// convert basic_string_span to byte span: - -template< class T > -gsl_api inline span< const byte > as_bytes( basic_string_span spn ) gsl_noexcept -{ - return span< const byte >( reinterpret_cast( spn.data() ), spn.size_bytes() ); // NOLINT -} - -// -// String types: -// - -typedef char * zstring; -typedef const char * czstring; - -#if gsl_HAVE( WCHAR ) -typedef wchar_t * wzstring; -typedef const wchar_t * cwzstring; -#endif - -typedef basic_string_span< char > string_span; -typedef basic_string_span< char const > cstring_span; - -#if gsl_HAVE( WCHAR ) -typedef basic_string_span< wchar_t > wstring_span; -typedef basic_string_span< wchar_t const > cwstring_span; -#endif - -// to_string() allow (explicit) conversions from string_span to string - -#if 0 - -template< class T > -inline std::basic_string< typename std::remove_const::type > to_string( basic_string_span spn ) -{ - std::string( spn.data(), spn.length() ); -} - -#else - -inline std::string to_string( string_span const & spn ) -{ - return std::string( spn.data(), spn.length() ); -} - -inline std::string to_string( cstring_span const & spn ) -{ - return std::string( spn.data(), spn.length() ); -} - -#if gsl_HAVE( WCHAR ) - -inline std::wstring to_string( wstring_span const & spn ) -{ - return std::wstring( spn.data(), spn.length() ); -} - -inline std::wstring to_string( cwstring_span const & spn ) -{ - return std::wstring( spn.data(), spn.length() ); -} - -#endif // gsl_HAVE( WCHAR ) -#endif // to_string() - -// -// Stream output for string_span types -// - -namespace detail { - -template< class Stream > -void write_padding( Stream & os, std::streamsize n ) -{ - for ( std::streamsize i = 0; i < n; ++i ) - os.rdbuf()->sputc( os.fill() ); -} - -template< class Stream, class Span > -Stream & write_to_stream( Stream & os, Span const & spn ) -{ - typename Stream::sentry sentry( os ); - - if ( !os ) - return os; - - const std::streamsize length = narrow( spn.length() ); - - // Whether, and how, to pad - const bool pad = ( length < os.width() ); - const bool left_pad = pad && ( os.flags() & std::ios_base::adjustfield ) == std::ios_base::right; - - if ( left_pad ) - write_padding( os, os.width() - length ); - - // Write span characters - os.rdbuf()->sputn( spn.begin(), length ); - - if ( pad && !left_pad ) - write_padding( os, os.width() - length ); - - // Reset output stream width - os.width(0); - - return os; -} - -} // namespace detail - -template< typename Traits > -std::basic_ostream< char, Traits > & operator<<( std::basic_ostream< char, Traits > & os, string_span const & spn ) -{ - return detail::write_to_stream( os, spn ); -} - -template< typename Traits > -std::basic_ostream< char, Traits > & operator<<( std::basic_ostream< char, Traits > & os, cstring_span const & spn ) -{ - return detail::write_to_stream( os, spn ); -} - -#if gsl_HAVE( WCHAR ) - -template< typename Traits > -std::basic_ostream< wchar_t, Traits > & operator<<( std::basic_ostream< wchar_t, Traits > & os, wstring_span const & spn ) -{ - return detail::write_to_stream( os, spn ); -} - -template< typename Traits > -std::basic_ostream< wchar_t, Traits > & operator<<( std::basic_ostream< wchar_t, Traits > & os, cwstring_span const & spn ) -{ - return detail::write_to_stream( os, spn ); -} - -#endif // gsl_HAVE( WCHAR ) - -// -// ensure_sentinel() -// -// Provides a way to obtain a span from a contiguous sequence -// that ends with a (non-inclusive) sentinel value. -// -// Will fail-fast if sentinel cannot be found before max elements are examined. -// -namespace detail { - -template< class T, class SizeType, const T Sentinel > -gsl_api static span ensure_sentinel( T * seq, SizeType max = (std::numeric_limits::max)() ) -{ - typedef T * pointer; - - gsl_SUPPRESS_MSVC_WARNING( 26429, "f.23: symbol 'cur' is never tested for nullness, it can be marked as not_null" ) - pointer cur = seq; - - while ( static_cast( cur - seq ) < max && *cur != Sentinel ) - ++cur; - - gsl_Expects( *cur == Sentinel ); - - return span( seq, narrow_cast< typename span::index_type >( cur - seq ) ); -} -} // namespace detail - -// -// ensure_z - creates a string_span for a czstring or cwzstring. -// Will fail fast if a null-terminator cannot be found before -// the limit of size_type. -// - -template< class T > -gsl_api inline span ensure_z( T * const & sz, size_t max = (std::numeric_limits::max)() ) -{ - return detail::ensure_sentinel( sz, max ); -} - -template< class T, size_t N > -gsl_api inline span ensure_z( T (&sz)[N] ) -{ - return ::gsl::ensure_z( gsl_ADDRESSOF( sz[0] ), N ); -} - -# if gsl_HAVE( TYPE_TRAITS ) - -template< class Container > -inline span< typename std::remove_pointer::type > -ensure_z( Container & cont ) -{ - return ::gsl::ensure_z( cont.data(), cont.length() ); -} -# endif - -// -// basic_zstring_span<> - A view of contiguous null-terminated characters, replace (*,len). -// - -template -class basic_zstring_span -{ -public: - typedef T element_type; - typedef span span_type; - - typedef typename span_type::index_type index_type; - typedef typename span_type::difference_type difference_type; - - typedef element_type * czstring_type; - typedef basic_string_span string_span_type; - - gsl_api gsl_constexpr14 basic_zstring_span( span_type s ) - : span_( s ) - { - // expects a zero-terminated span - gsl_Expects( s[s.size() - 1] == '\0'); - } - -#if gsl_HAVE( IS_DEFAULT ) - gsl_constexpr basic_zstring_span( basic_zstring_span const & other ) = default; - gsl_constexpr basic_zstring_span( basic_zstring_span && other ) = default; - gsl_constexpr14 basic_zstring_span & operator=( basic_zstring_span const & other ) = default; - gsl_constexpr14 basic_zstring_span & operator=( basic_zstring_span && other ) = default; -#else - gsl_api gsl_constexpr basic_zstring_span( basic_zstring_span const & other) : span_ ( other.span_ ) {} - gsl_api gsl_constexpr basic_zstring_span & operator=( basic_zstring_span const & other ) { span_ = other.span_; return *this; } -#endif - - gsl_api gsl_constexpr bool empty() const gsl_noexcept - { - return span_.size() == 0; - } - - gsl_api gsl_constexpr string_span_type as_string_span() const gsl_noexcept - { - return string_span_type( span_.data(), span_.size() > 1 ? span_.size() - 1 : 0 ); - } - - /*gsl_api*/ // currently disabled due to an apparent NVCC bug - gsl_constexpr string_span_type ensure_z() const - { - return ::gsl::ensure_z(span_.data(), span_.size()); - } - - gsl_api gsl_constexpr czstring_type assume_z() const gsl_noexcept - { - return span_.data(); - } - -private: - span_type span_; -}; - -// -// zString types: -// - -typedef basic_zstring_span< char > zstring_span; -typedef basic_zstring_span< char const > czstring_span; - -#if gsl_HAVE( WCHAR ) -typedef basic_zstring_span< wchar_t > wzstring_span; -typedef basic_zstring_span< wchar_t const > cwzstring_span; -#endif - -} // namespace gsl - -#if gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 120 - -namespace std { - -template<> -struct hash< ::gsl::byte > -{ -public: - gsl_constexpr std::size_t operator()( ::gsl::byte v ) const gsl_noexcept - { - return ::gsl::to_integer( v ); - } -}; - -} // namespace std - -#endif - -#if gsl_FEATURE_GSL_LITE_NAMESPACE - -// gsl_lite namespace: - -// gsl-lite currently keeps all symbols in the namespace `gsl`. The `gsl_lite` namespace contains all the symbols in the -// `gsl` namespace, plus some extensions that are not specified in the Core Guidelines. -// -// Going forward, we want to support coexistence of gsl-lite with M-GSL, so we want to encourage using the `gsl_lite` -// namespace when consuming gsl-lite. Typical use in library code would be: -// -// #include // instead of -// -// namespace foo { -// namespace gsl = ::gsl_lite; // convenience alias -// double mean(gsl::span elements) { -// gsl_Expects(!elements.empty()); // instead of Expects() -// ... -// } -// } // namespace foo -// -// In a future version, the new header will only define the `gsl_lite` namespace and no -// unprefixed `Expects()` and `Ensures()` macros to avoid collision with M-GSL. To ensure backward compatibility, the -// old header will keep defining the `gsl` namespace and the `Expects()` and `Ensures()` macros. - -namespace gsl_lite -{ - -namespace std11 = ::gsl::std11; -namespace std14 = ::gsl::std14; -namespace std17 = ::gsl::std17; -namespace std20 = ::gsl::std20; - -using namespace std11; -using namespace std14; -using namespace std17; -using namespace std20; - -#if gsl_HAVE( SHARED_PTR ) -using std::unique_ptr; -using std::shared_ptr; -using std::make_shared; -#endif - -using ::gsl::index; - -// Integer type for dimensions. -typedef gsl_CONFIG_INDEX_TYPE dim; - -// Integer type for array strides. -typedef gsl_CONFIG_INDEX_TYPE stride; - -// Integer type for pointer, iterator, or index differences. -typedef gsl_CONFIG_INDEX_TYPE diff; - -#if gsl_HAVE( ALIAS_TEMPLATE ) -using ::gsl::owner; -#endif - -using ::gsl::fail_fast; - -using ::gsl::finally; -#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) -using ::gsl::on_return; -using ::gsl::on_error; -#endif // gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD ) - -using ::gsl::narrow_cast; -using ::gsl::narrowing_error; -using ::gsl::narrow; -using ::gsl::narrow_failfast; - - -using ::gsl::at; - -using ::gsl::not_null; -using ::gsl::make_not_null; - -using ::gsl::byte; - -using ::gsl::with_container_t; -using ::gsl::with_container; - -using ::gsl::span; -using ::gsl::make_span; -using ::gsl::byte_span; -using ::gsl::copy; -using ::gsl::as_bytes; -using ::gsl::as_writable_bytes; -#if ! gsl_DEPRECATE_TO_LEVEL( 6 ) -using ::gsl::as_writeable_bytes; -#endif - -using ::gsl::basic_string_span; -using ::gsl::string_span; -using ::gsl::cstring_span; - -using ::gsl::basic_zstring_span; -using ::gsl::zstring_span; -using ::gsl::czstring_span; - -using ::gsl::zstring; -using ::gsl::czstring; - -#if gsl_HAVE( WCHAR ) -using ::gsl::wzstring; -using ::gsl::cwzstring; - -using ::gsl::wzstring_span; -using ::gsl::cwzstring_span; -#endif // gsl_HAVE( WCHAR ) - -} // namespace gsl_lite - -#endif // gsl_FEATURE_GSL_LITE_NAMESPACE - -gsl_RESTORE_MSVC_WARNINGS() - -#endif // GSL_GSL_LITE_HPP_INCLUDED - -// end of file diff --git a/firmware/ui/inc/sframe/map.h b/firmware/ui/inc/sframe/map.h deleted file mode 100644 index b404181e..00000000 --- a/firmware/ui/inc/sframe/map.h +++ /dev/null @@ -1,111 +0,0 @@ -#pragma once - -#ifdef NO_ALLOC - -#include - -namespace sframe { - -template -class map : private vector>, N> -{ -public: - template - void emplace(Args&&... args) - { - const auto pos = std::find_if( - this->begin(), this->end(), [&](const auto& pair) { return !pair; }); - if (pos == this->end()) { - throw std::out_of_range("map out of space"); - } - - pos->emplace(args...); - } - - auto find(const K& key) - { - return std::find_if(this->begin(), this->end(), [&](const auto& pair) { - return pair && pair.value().first == key; - }); - } - - auto find(const K& key) const - { - return std::find_if(this->begin(), this->end(), [&](const auto& pair) { - return pair && pair.value().first == key; - }); - } - - bool contains(const K& key) const { return find(key) != this->end(); } - - const V& at(const K& key) const - { - const auto pos = find(key); - if (pos == this->end()) { - throw std::out_of_range("map key not found"); - } - - return pos->value().second; - } - - V& at(const K& key) - { - auto pos = find(key); - if (pos == this->end()) { - throw std::out_of_range("map key not found"); - } - - return pos->value().second; - } - - template - void erase_if_key(F&& f) - { - const auto to_erase = [&f](const auto& maybe_pair) { - return maybe_pair && f(maybe_pair.value().first); - }; - - std::replace_if(this->begin(), this->end(), to_erase, std::nullopt); - } -}; - -} // namespace sframe - -#else // ifdef NO_ALLOC - -#include - -namespace sframe { - -// NOTE: NOT RECOMMENDED FOR USE OUTSIDE THIS LIBRARY -// -// We have used public inheritance from std::map to simplify the interface -// here. This works fine for the use cases we have within this library. If you -// choose to use this map type outside this library, you MUST NOT store it as a -// std::map pointer or reference. This will cause memory leaks, because the -// destructor ~std::map is not virtual. -template -class map : public std::map -{ -private: - using parent = std::map; - -public: - bool contains(const K& key) const { return this->count(key) > 0; } - - template - void erase_if_key(F&& f) - { - for (auto iter = this->begin(); iter != this->end();) { - if (f(iter->first)) { - iter = this->erase(iter); - } else { - ++iter; - } - } - } -}; - -} // namespace sframe - -#endif // def NO_ALLOC diff --git a/firmware/ui/inc/sframe/sframe.h b/firmware/ui/inc/sframe/sframe.h deleted file mode 100644 index ba38e2ac..00000000 --- a/firmware/ui/inc/sframe/sframe.h +++ /dev/null @@ -1,205 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -// These constants define the size of certain internal data structures if -// we are configured not to depend on dynamic allocations, i.e., if the NO_ALLOC -// flag is set. If you are using an allocator, you can ignore them. -// -// Note that these constants must be the same at the time when the library is -// built as at the time when it is used. If you are using a pre-built binary, -// you must make sure that these parameters have the same values as when the -// library was built. -#ifndef SFRAME_MAX_KEYS -#define SFRAME_MAX_KEYS 200 -#endif - -#ifndef SFRAME_EPOCH_BITS -#define SFRAME_EPOCH_BITS 4 -#endif - -namespace sframe { - -struct crypto_error : std::runtime_error -{ - crypto_error(); -}; - -struct unsupported_ciphersuite_error : std::runtime_error -{ - unsupported_ciphersuite_error(); -}; - -struct authentication_error : std::runtime_error -{ - authentication_error(); -}; - -struct buffer_too_small_error : std::runtime_error -{ - using parent = std::runtime_error; - using parent::parent; -}; - -struct invalid_parameter_error : std::runtime_error -{ - using parent = std::runtime_error; - using parent::parent; -}; - -struct invalid_key_usage_error : std::runtime_error -{ - using parent = std::runtime_error; - using parent::parent; -}; - -enum class CipherSuite : uint16_t -{ - AES_128_CTR_HMAC_SHA256_80 = 1, - AES_128_CTR_HMAC_SHA256_64 = 2, - AES_128_CTR_HMAC_SHA256_32 = 3, - AES_GCM_128_SHA256 = 4, - AES_GCM_256_SHA512 = 5, -}; - -using input_bytes = gsl::span; -using output_bytes = gsl::span; - -template -using owned_bytes = vector; - -using KeyID = uint64_t; -using Counter = uint64_t; -class Header; - -enum struct KeyUsage -{ - protect, - unprotect, -}; - -struct KeyRecord -{ - static KeyRecord from_base_key(CipherSuite suite, - KeyID key_id, - KeyUsage usage, - input_bytes base_key); - - static constexpr size_t max_key_size = 48; - static constexpr size_t max_salt_size = 12; - - owned_bytes key; - owned_bytes salt; - KeyUsage usage; - Counter counter; -}; - -// Context applies the full SFrame transform. It tracks a counter for each key -// to ensure nonce uniqueness, adds the SFrame header on protect, and -// reads/strips the SFrame header on unprotect. -class Context -{ -public: - Context(CipherSuite suite); - virtual ~Context(); - - void add_key(KeyID kid, KeyUsage usage, input_bytes key); - - output_bytes protect(KeyID key_id, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata); - output_bytes unprotect(output_bytes plaintext, - input_bytes ciphertext, - input_bytes metadata); - - static constexpr size_t max_overhead = 17 + 16; - static constexpr size_t max_metadata_size = 512; - -protected: - CipherSuite suite; - map keys; - - output_bytes protect_inner(const Header& header, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata); - output_bytes unprotect_inner(const Header& header, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata); -}; - -// MLSContext augments Context with logic for deriving keys from MLS. Instead -// of adding individual keys, salts, and key IDs, the caller adds a secret for -// an epoch, and keys / salts / key IDs are derived as needed. -class MLSContext : protected Context -{ -public: - using EpochID = uint64_t; - using SenderID = uint64_t; - using ContextID = uint64_t; - - MLSContext(CipherSuite suite_in, size_t epoch_bits_in); - - void add_epoch(EpochID epoch_id, input_bytes sframe_epoch_secret); - void add_epoch(EpochID epoch_id, - input_bytes sframe_epoch_secret, - size_t sender_bits); - void purge_before(EpochID keeper); - - output_bytes protect(EpochID epoch_id, - SenderID sender_id, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata); - output_bytes protect(EpochID epoch_id, - SenderID sender_id, - ContextID context_id, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata); - - output_bytes unprotect(output_bytes plaintext, - input_bytes ciphertext, - input_bytes metadata); - -private: - struct EpochKeys - { - static constexpr size_t max_secret_size = 64; - - EpochID full_epoch; - owned_bytes sframe_epoch_secret; - size_t sender_bits; - size_t context_bits; - uint64_t max_sender_id; - uint64_t max_context_id; - - EpochKeys(EpochID full_epoch_in, - input_bytes sframe_epoch_secret_in, - size_t epoch_bits, - size_t sender_bits_in); - owned_bytes base_key(CipherSuite suite, - SenderID sender_id) const; - }; - - void purge_epoch(EpochID epoch_id); - - KeyID form_key_id(EpochID epoch_id, - SenderID sender_id, - ContextID context_id) const; - void ensure_key(KeyID key_id, KeyUsage usage); - - const size_t epoch_bits; - const size_t epoch_mask; - - static constexpr size_t max_epochs = 1 << SFRAME_EPOCH_BITS; - vector, max_epochs> epoch_cache; -}; - -} // namespace sframe diff --git a/firmware/ui/inc/sframe/vector.h b/firmware/ui/inc/sframe/vector.h deleted file mode 100644 index 649d2c37..00000000 --- a/firmware/ui/inc/sframe/vector.h +++ /dev/null @@ -1,142 +0,0 @@ -#pragma once - -#include - -#ifdef NO_ALLOC - -namespace sframe { - -template -class vector -{ -private: - std::array _data; - size_t _size; - -public: - constexpr vector() - : _size(N) - { - std::fill(_data.begin(), _data.end(), T()); - } - - constexpr vector(size_t size) - { - std::fill(_data.begin(), _data.end(), T()); - resize(size); - } - - constexpr vector(std::initializer_list content) - { - resize(content.size()); - std::copy(content.begin(), content.end(), _data.begin()); - } - - constexpr vector(gsl::span content) - { - resize(content.size()); - std::copy(content.begin(), content.end(), _data.begin()); - } - - // XXX(RLB) This constructor seems redundant with the prior one, but for some - // reason the compiler won't auto-convert from vector to span. - template - constexpr vector(const vector& content) - { - resize(content.size()); - std::copy(content.begin(), content.end(), _data.begin()); - } - - uint8_t* data() { return _data.data(); } - - auto begin() const { return _data.begin(); } - auto begin() { return _data.begin(); } - - auto end() const { return _data.begin() + _size; } - auto end() { return _data.begin() + _size; } - - auto size() const { return _size; } - auto capacity() const { return N; } - void resize(size_t size) - { - if (size > N) { - throw std::out_of_range("vector out of space"); - } - - _size = size; - } - - void push(T&& item) - { - resize(_size + 1); - _data.at(_size - 1) = item; - } - - void append(gsl::span content) - { - const auto start = _size; - resize(_size + content.size()); - std::copy(content.begin(), content.end(), begin() + start); - } - - auto& operator[](size_t i) { return _data.at(i); } - const auto& operator[](size_t i) const { return _data.at(i); } - - operator gsl::span() const { return gsl::span(_data).first(_size); } - operator gsl::span() { return gsl::span(_data).first(_size); } -}; - -} // namespace sframe - -#else // ifdef NO_ALLOC - -#include - -namespace sframe { - -// NOTE: NOT RECOMMENDED FOR USE OUTSIDE THIS LIBRARY -// -// We have used public inheritance from std::vector to simplify the interface -// here. This works fine for the use cases we have within this library. If you -// choose to use this vector type outside this library, you MUST NOT store it as -// a std::vector pointer or reference. This will cause memory leaks, because -// the destructor ~std::vector is not virtual. -template -class vector : public std::vector -{ -private: - using parent = std::vector; - -public: - constexpr vector() - : parent(N) - { - } - - constexpr vector(size_t size) - : parent(size) - { - } - - constexpr vector(gsl::span content) - : parent(content.begin(), content.end()) - { - } - - template - constexpr vector(const vector& content) - : parent(content) - { - } - - void append(gsl::span content) - { - const auto start = this->size(); - this->resize(start + content.size()); - std::copy(content.begin(), content.end(), this->begin() + start); - } -}; - -} // namespace sframe - -#endif // def NO_ALLOC diff --git a/firmware/ui/src/sframe/crypto.cc b/firmware/ui/src/sframe/crypto.cc deleted file mode 100644 index 92f9ca02..00000000 --- a/firmware/ui/src/sframe/crypto.cc +++ /dev/null @@ -1,97 +0,0 @@ -#include "crypto.h" -#include "header.h" - -namespace sframe { - -size_t -cipher_digest_size(CipherSuite suite) -{ - switch (suite) { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - case CipherSuite::AES_GCM_128_SHA256: - return 32; - - case CipherSuite::AES_GCM_256_SHA512: - return 64; - - default: - throw unsupported_ciphersuite_error(); - } -} - -size_t -cipher_key_size(CipherSuite suite) -{ - switch (suite) { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - // 16-byte AES key + 32-byte HMAC key - return 48; - - case CipherSuite::AES_GCM_128_SHA256: - return 16; - - case CipherSuite::AES_GCM_256_SHA512: - return 32; - - default: - throw unsupported_ciphersuite_error(); - } -} - -size_t -cipher_enc_key_size(CipherSuite suite) -{ - switch (suite) { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - return 16; - - default: - throw unsupported_ciphersuite_error(); - } -} - -size_t -cipher_nonce_size(CipherSuite suite) -{ - switch (suite) { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - case CipherSuite::AES_GCM_128_SHA256: - case CipherSuite::AES_GCM_256_SHA512: - return 12; - - default: - throw unsupported_ciphersuite_error(); - } -} - -size_t -cipher_overhead(CipherSuite suite) -{ - switch (suite) { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - return 10; // 80-bit tag - - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - return 8; // 64-bit tag - - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - return 4; // 32-bit tag - - case CipherSuite::AES_GCM_128_SHA256: - case CipherSuite::AES_GCM_256_SHA512: - return 16; // Full 128-bit AES-GCM tag - - default: - throw unsupported_ciphersuite_error(); - } -} - -} // namespace sframe diff --git a/firmware/ui/src/sframe/crypto.h b/firmware/ui/src/sframe/crypto.h deleted file mode 100644 index b39b66dd..00000000 --- a/firmware/ui/src/sframe/crypto.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include - -namespace sframe { - -size_t -cipher_digest_size(CipherSuite suite); -size_t -cipher_key_size(CipherSuite suite); -size_t -cipher_enc_key_size(CipherSuite suite); -size_t -cipher_nonce_size(CipherSuite suite); -size_t -cipher_overhead(CipherSuite suite); - -/// -/// HKDF -/// - -static constexpr size_t max_hkdf_extract_size = 64; -static constexpr size_t max_hkdf_expand_size = 64; - -owned_bytes -hkdf_extract(CipherSuite suite, input_bytes salt, input_bytes ikm); - -owned_bytes -hkdf_expand(CipherSuite suite, input_bytes prk, input_bytes info, size_t size); - -/// -/// AEAD Algorithms -/// - -output_bytes -seal(CipherSuite suite, - input_bytes key, - input_bytes nonce, - output_bytes ct, - input_bytes aad, - input_bytes pt); - -output_bytes -open(CipherSuite suite, - input_bytes key, - input_bytes nonce, - output_bytes pt, - input_bytes aad, - input_bytes ct); - -} // namespace sframe diff --git a/firmware/ui/src/sframe/crypto_stm32.cc b/firmware/ui/src/sframe/crypto_stm32.cc deleted file mode 100644 index 0570d443..00000000 --- a/firmware/ui/src/sframe/crypto_stm32.cc +++ /dev/null @@ -1,273 +0,0 @@ -#ifdef CRYPTO - -#include "crypto.h" -#include "header.h" -#include -#include -#include - -namespace sframe -{ - -#define VERIFY_CMOX_CALL(func, success_code) \ - do \ - { \ - auto retval = func; \ - if (retval != success_code) \ - { \ - cmox_error = retval; \ - throw crypto_error(); \ - } \ - } while (0) - -/// -/// Convert between native identifiers / errors and cmox ones -/// - -std::optional cmox_error; - -crypto_error::crypto_error() : - std::runtime_error(cmox_error.has_value() - ? "CMOX crypto error (error=" + std::to_string(cmox_error.value()) + ")" - : "unknown CMOX crypto error") -{ -} - -cmox_mac_algo_t cmox_hmac_algo(CipherSuite suite) -{ - switch (suite) - { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - case CipherSuite::AES_GCM_128_SHA256: - return CMOX_HMAC_SHA256_ALGO; - case CipherSuite::AES_GCM_256_SHA512: - return CMOX_HMAC_SHA512_ALGO; - default: - throw unsupported_ciphersuite_error(); - } -} - -cmox_hmac_impl_t cmox_hmac_impl(CipherSuite suite) -{ - switch (suite) - { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - case CipherSuite::AES_GCM_128_SHA256: - return CMOX_HMAC_SHA256; - case CipherSuite::AES_GCM_256_SHA512: - return CMOX_HMAC_SHA512; - default: - throw unsupported_ciphersuite_error(); - } -} - -std::size_t cmox_hmac_size(CipherSuite suite) -{ - switch (suite) - { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - case CipherSuite::AES_GCM_128_SHA256: - return CMOX_SHA256_SIZE; - case CipherSuite::AES_GCM_256_SHA512: - return CMOX_SHA512_SIZE; - default: - throw unsupported_ciphersuite_error(); - } -} - -/// -/// HKDF -/// - -owned_bytes -hkdf_extract(CipherSuite suite, input_bytes salt, input_bytes ikm) -{ - std::size_t computed_size = 0; - const auto hmac_size = cmox_hmac_size(suite); - auto out = owned_bytes(hmac_size); - VERIFY_CMOX_CALL(cmox_mac_compute(cmox_hmac_algo(suite), ikm.data(), ikm.size(), salt.data(), - salt.size(), nullptr, 0, out.data(), out.size(), - &computed_size), - CMOX_MAC_SUCCESS); - - return out; -} - -owned_bytes -hkdf_expand(CipherSuite suite, input_bytes prk, input_bytes info, size_t size) -{ - cmox_hmac_handle_t Hmac_Ctx; - cmox_mac_handle_t* mac_ctx; - - // Compute N = ceil(L/HashLen) - const auto digest_size = cmox_hmac_size(suite); - auto N = static_cast(size / digest_size); - if ((N * digest_size) < size) - { - N++; - } - - mac_ctx = cmox_hmac_construct(&Hmac_Ctx, cmox_hmac_impl(suite)); - if (mac_ctx == nullptr) - { - throw crypto_error(); - } - - owned_bytes computed_hash(size); - owned_bytes out(size); - - uint32_t index = 0; - for (uint8_t i = 1; i <= N; i++) - { - VERIFY_CMOX_CALL(cmox_mac_init(mac_ctx), CMOX_MAC_SUCCESS); - - /* For the last iteration, tag length may be reduced to fit requested length L */ - if (i == N) - { - VERIFY_CMOX_CALL(cmox_mac_setTagLen(mac_ctx, size - index), CMOX_MAC_SUCCESS); - } - - VERIFY_CMOX_CALL(cmox_mac_setKey(mac_ctx, prk.data(), prk.size()), CMOX_MAC_SUCCESS); - - if (i > 1) - { - VERIFY_CMOX_CALL(cmox_mac_append(mac_ctx, computed_hash.data(), digest_size), - CMOX_MAC_SUCCESS); - } - - VERIFY_CMOX_CALL(cmox_mac_append(mac_ctx, info.data(), info.size()), CMOX_MAC_SUCCESS); - VERIFY_CMOX_CALL(cmox_mac_append(mac_ctx, &i, 1), CMOX_MAC_SUCCESS); - VERIFY_CMOX_CALL(cmox_mac_generateTag(mac_ctx, computed_hash.data(), nullptr), - CMOX_MAC_SUCCESS); - - if (i == N) - { - memcpy(&out[index], computed_hash.data(), size - index); - index = size; - } - else - { - memcpy(&out[index], computed_hash.data(), digest_size); - index += digest_size; - } - } - - VERIFY_CMOX_CALL(cmox_mac_cleanup(mac_ctx), CMOX_MAC_SUCCESS); - - return out; -} - -static output_bytes seal_aead(CipherSuite suite, - input_bytes key, - input_bytes nonce, - output_bytes ct, - input_bytes aad, - input_bytes pt) -{ - auto tag_size = cipher_overhead(suite); - if (ct.size() < pt.size() + tag_size) - { - throw buffer_too_small_error("Ciphertext buffer too small"); - } - - std::size_t computed_size = 0; - - VERIFY_CMOX_CALL(cmox_aead_encrypt(CMOX_AESFAST_GCMFAST_ENC_ALGO, pt.data(), pt.size(), - tag_size, key.data(), key.size(), nonce.data(), nonce.size(), - aad.data(), aad.size(), ct.data(), &computed_size), - CMOX_CIPHER_SUCCESS); - - return ct.subspan(0, computed_size); -} - -output_bytes seal(CipherSuite suite, - input_bytes key, - input_bytes nonce, - output_bytes ct, - input_bytes aad, - input_bytes pt) -{ - switch (suite) - { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - { - throw unsupported_ciphersuite_error(); - // return seal_ctr(suite, key, nonce, ct, aad, pt); - } - - case CipherSuite::AES_GCM_128_SHA256: - case CipherSuite::AES_GCM_256_SHA512: - { - return seal_aead(suite, key, nonce, ct, aad, pt); - } - - default: - throw unsupported_ciphersuite_error(); - } - - throw unsupported_ciphersuite_error(); -} - -static output_bytes open_aead(CipherSuite suite, - input_bytes key, - input_bytes nonce, - output_bytes pt, - input_bytes aad, - input_bytes ct) -{ - auto tag_size = cipher_overhead(suite); - if (ct.size() < tag_size) - { - throw buffer_too_small_error("Ciphertext buffer too small"); - } - - std::size_t computed_size = 0; - - VERIFY_CMOX_CALL(cmox_aead_decrypt(CMOX_AESFAST_GCMFAST_DEC_ALGO, ct.data(), ct.size(), - tag_size, key.data(), key.size(), nonce.data(), nonce.size(), - aad.data(), aad.size(), pt.data(), &computed_size), - CMOX_CIPHER_AUTH_SUCCESS); - - return pt.subspan(0, computed_size); -} - -output_bytes open(CipherSuite suite, - input_bytes key, - input_bytes nonce, - output_bytes pt, - input_bytes aad, - input_bytes ct) -{ - switch (suite) - { - case CipherSuite::AES_128_CTR_HMAC_SHA256_80: - case CipherSuite::AES_128_CTR_HMAC_SHA256_64: - case CipherSuite::AES_128_CTR_HMAC_SHA256_32: - { - throw unsupported_ciphersuite_error(); - // return open_ctr(suite, key, nonce, pt, aad, ct); - } - - case CipherSuite::AES_GCM_128_SHA256: - case CipherSuite::AES_GCM_256_SHA512: - { - return open_aead(suite, key, nonce, pt, aad, ct); - } - default: - throw unsupported_ciphersuite_error(); - } - - throw unsupported_ciphersuite_error(); -} - -} // namespace sframe - -#endif diff --git a/firmware/ui/src/sframe/header.cc b/firmware/ui/src/sframe/header.cc deleted file mode 100644 index ab4c46cc..00000000 --- a/firmware/ui/src/sframe/header.cc +++ /dev/null @@ -1,203 +0,0 @@ -#include "header.h" - -namespace sframe { - -static size_t -uint_size(uint64_t val) -{ - if (val < 0x08) { - // Fits in the config byte - return 0; - } - - for (unsigned int i = 0; i < 8; i += 1) { - if ((val >> (8 * i)) == 0) { - return i; - } - } - - return 8; -} - -void -encode_uint(uint64_t val, output_bytes buffer) -{ - size_t size = buffer.size(); - for (size_t i = 0; i < size && i < 8; i++) { - buffer[size - i - 1] = uint8_t(val >> (8 * i)); - } -} - -static uint64_t -decode_uint(input_bytes data) -{ - if (!data.empty() && data[0] == 0) { - throw invalid_parameter_error("Integer is not minimally encoded"); - } - - uint64_t val = 0; - for (size_t i = 0; i < data.size(); i++) { - val = (val << 8) + static_cast(data[i]); - } - return val; -} - -struct ValueOrLength -{ - bool is_length = false; - uint8_t value_or_length = 0; - - static ValueOrLength for_u64(uint64_t value) - { - if (value >= 0x08) { - return { true, static_cast(uint_size(value) - 1) }; - } else { - return { false, static_cast(value) }; - } - } - - static ValueOrLength decode(uint8_t encoded) - { - const auto is_length = (encoded & 0x08) != 0; - const auto value_or_length = (encoded & 0x07); - return { is_length, static_cast(value_or_length) }; - } - - uint8_t encode() const - { - return (((is_length) ? 1 : 0) << 3) | (value_or_length & 0x07); - } - - size_t value_size() const - { - if (!is_length) { - return 0; - } - - return value_or_length + 1; - } - - std::tuple read(input_bytes data) const - { - if (!is_length) { - // Nothing to read; value is already in config byte - return { value_or_length, data }; - } - - const auto size = value_size(); - const auto value = decode_uint(data.subspan(0, size)); - const auto remaining = data.subspan(size); - return { value, remaining }; - } - -private: - ValueOrLength(bool is_length_in, uint8_t value_or_length_in) - : is_length(is_length_in) - , value_or_length(value_or_length_in) - { - } -}; - -struct ConfigByte -{ - ValueOrLength kid; - ValueOrLength ctr; - - ConfigByte(uint64_t kid_in, uint64_t ctr_in) - : kid(ValueOrLength::for_u64(kid_in)) - , ctr(ValueOrLength::for_u64(ctr_in)) - { - } - - explicit ConfigByte(uint8_t encoded) - : kid(ValueOrLength::decode(encoded >> 4)) - , ctr(ValueOrLength::decode(encoded & 0x0f)) - { - } - - size_t encoded_size() const - { - return 1 + kid.value_size() + ctr.value_size(); - } - - uint8_t encode() const { return (kid.encode() << 4) | ctr.encode(); } -}; - -Header::Header(KeyID key_id_in, Counter counter_in) - : key_id(key_id_in) - , counter(counter_in) -{ - const auto cfg = ConfigByte{ key_id, counter }; - - _encoded[0] = cfg.encode(); - _encoded.resize(cfg.encoded_size()); - - const auto encoded = output_bytes(_encoded); - const auto after_cfg = encoded.subspan(1); - encode_uint(key_id, after_cfg.subspan(0, cfg.kid.value_size())); - - const auto after_kid = after_cfg.subspan(cfg.kid.value_size()); - encode_uint(counter, after_kid.subspan(0, cfg.ctr.value_size())); -} - -Header -Header::parse(input_bytes buffer) -{ - if (buffer.size() < Header::min_size) { - throw buffer_too_small_error("Ciphertext too small to decode header"); - } - - const auto cfg = ConfigByte{ buffer[0] }; - const auto after_cfg = buffer.subspan(1); - const auto [key_id, after_kid] = cfg.kid.read(after_cfg); - const auto [counter, _] = cfg.ctr.read(after_kid); - const auto encoded = buffer.subspan(0, cfg.encoded_size()); - - return Header(key_id, counter, encoded); -} - -Header::Header(KeyID key_id_in, Counter counter_in, input_bytes encoded_in) - : key_id(key_id_in) - , counter(counter_in) - , _encoded(encoded_in) -{ -} - -#if 0 -std::tuple -decode(input_bytes buffer) -{ - if (buffer.size() < Header::min_size) { - throw buffer_too_small_error("Ciphertext too small to decode header"); - } - - const auto cfg = ConfigByte{ buffer[0] }; - const auto after_cfg = buffer.subspan(1); - const auto [kid, after_kid] = cfg.kid.read(after_cfg); - const auto [ctr, after_ctr] = cfg.ctr.read(after_kid); - const auto header = Header{ KeyID(kid), Counter(ctr) }; - - return { header, after_ctr }; -} - -size_t -Header::encode(output_bytes buffer) const -{ - if (buffer.size() < size()) { - throw buffer_too_small_error("Buffer too small to encode header"); - } - - const auto cfg = ConfigByte{ key_id, counter }; - buffer[0] = cfg.encode(); - - const auto after_cfg = buffer.subspan(1); - encode_uint(key_id, after_cfg.subspan(0, cfg.kid.size())); - - const auto after_kid = after_cfg.subspan(cfg.kid.size()); - encode_uint(counter, after_kid.subspan(0, cfg.ctr.size())); - - return size(); -} -#endif - -} // namespace sframe diff --git a/firmware/ui/src/sframe/header.h b/firmware/ui/src/sframe/header.h deleted file mode 100644 index f431d7da..00000000 --- a/firmware/ui/src/sframe/header.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include - -namespace sframe { - -void -encode_uint(uint64_t val, output_bytes buffer); - -class Header -{ -public: - const KeyID key_id; - const Counter counter; - - Header(KeyID key_id_in, Counter counter_in); - static Header parse(input_bytes buffer); - - input_bytes encoded() const { return _encoded; } - size_t size() const { return _encoded.size(); } - - // Configuration byte plus 8-byte KID and CTR - static constexpr size_t max_size = 1 + 8 + 8; - -private: - // Just the configuration byte - static constexpr size_t min_size = 1; - - owned_bytes _encoded; - - Header(KeyID key_id_in, Counter counter_in, input_bytes encoded_in); -}; - -} // namespace sframe diff --git a/firmware/ui/src/sframe/sframe.cc b/firmware/ui/src/sframe/sframe.cc deleted file mode 100644 index 0e430c35..00000000 --- a/firmware/ui/src/sframe/sframe.cc +++ /dev/null @@ -1,358 +0,0 @@ -#include "crypto.h" -#include "header.h" -#include - -namespace sframe -{ - -/// -/// Errors -/// - -unsupported_ciphersuite_error::unsupported_ciphersuite_error() : - std::runtime_error("Unsupported ciphersuite") -{ -} - -authentication_error::authentication_error() : - std::runtime_error("AEAD authentication failure") -{ -} - -/// -/// KeyRecord -/// - -static auto from_ascii(const char* str, size_t len) -{ - const auto ptr = reinterpret_cast(str); - return input_bytes(ptr, len); -} - -static const auto base_label = from_ascii("SFrame 1.0 Secret ", 18); -static const auto key_label = from_ascii("key ", 4); -static const auto salt_label = from_ascii("salt ", 5); - -static owned_bytes<32> sframe_key_label(CipherSuite suite, KeyID key_id) -{ - auto label = owned_bytes<32>(base_label); - label.append(key_label); - label.resize(32); - - auto label_data = output_bytes(label); - encode_uint(key_id, label_data.subspan(22).first(8)); - encode_uint(static_cast(suite), label_data.subspan(30)); - - return label; -} - -static owned_bytes<33> sframe_salt_label(CipherSuite suite, KeyID key_id) -{ - auto label = owned_bytes<33>(base_label); - label.append(salt_label); - label.resize(33); - - auto label_data = output_bytes(label); - encode_uint(key_id, label_data.last(10).first(8)); - encode_uint(static_cast(suite), label_data.last(2)); - - return label; -} - -KeyRecord -KeyRecord::from_base_key(CipherSuite suite, KeyID key_id, KeyUsage usage, input_bytes base_key) -{ - auto key_size = cipher_key_size(suite); - auto nonce_size = cipher_nonce_size(suite); - - const auto empty_byte_string = owned_bytes<1>(0); - const auto key_label = sframe_key_label(suite, key_id); - const auto salt_label = sframe_salt_label(suite, key_id); - - auto secret = hkdf_extract(suite, empty_byte_string, base_key); - auto key = hkdf_expand(suite, secret, key_label, key_size); - auto salt = hkdf_expand(suite, secret, salt_label, nonce_size); - - return KeyRecord{key, salt, usage, 0}; -} - -/// -/// Context -/// - -Context::Context(CipherSuite suite_in) : - suite(suite_in) -{ -} - -Context::~Context() = default; - -void Context::add_key(KeyID key_id, KeyUsage usage, input_bytes base_key) -{ - keys.emplace(key_id, KeyRecord::from_base_key(suite, key_id, usage, base_key)); -} - -static owned_bytes form_nonce(Counter ctr, input_bytes salt) -{ - auto nonce = owned_bytes(salt); - for (size_t i = 0; i < sizeof(ctr); i++) - { - nonce[nonce.size() - i - 1] ^= uint8_t(ctr >> (8 * i)); - } - - return nonce; -} - -static constexpr auto max_aad_size = Header::max_size + Context::max_metadata_size; - -static owned_bytes form_aad(const Header& header, input_bytes metadata) -{ - if (metadata.size() > Context::max_metadata_size) - { - throw buffer_too_small_error("Metadata too large"); - } - - auto aad = owned_bytes(0); - aad.append(header.encoded()); - aad.append(metadata); - return aad; -} - -output_bytes -Context::protect(KeyID key_id, output_bytes ciphertext, input_bytes plaintext, input_bytes metadata) -{ - auto& key_record = keys.at(key_id); - const auto counter = key_record.counter; - key_record.counter += 1; - - const auto header = Header{key_id, counter}; - const auto header_data = header.encoded(); - if (ciphertext.size() < header_data.size()) - { - throw buffer_too_small_error("Ciphertext too small for SFrame header"); - } - - std::copy(header_data.begin(), header_data.end(), ciphertext.begin()); - auto inner_ciphertext = ciphertext.subspan(header_data.size()); - auto final_ciphertext = Context::protect_inner(header, inner_ciphertext, plaintext, metadata); - return ciphertext.first(header_data.size() + final_ciphertext.size()); -} - -output_bytes -Context::unprotect(output_bytes plaintext, input_bytes ciphertext, input_bytes metadata) -{ - const auto header = Header::parse(ciphertext); - const auto inner_ciphertext = ciphertext.subspan(header.size()); - return Context::unprotect_inner(header, plaintext, inner_ciphertext, metadata); -} - -output_bytes Context::protect_inner(const Header& header, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata) -{ - if (ciphertext.size() < plaintext.size() + cipher_overhead(suite)) - { - throw buffer_too_small_error("Ciphertext too small for cipher overhead"); - } - - const auto& key_and_salt = keys.at(header.key_id); - - const auto aad = form_aad(header, metadata); - const auto nonce = form_nonce(header.counter, key_and_salt.salt); - return seal(suite, key_and_salt.key, nonce, ciphertext, aad, plaintext); -} - -output_bytes Context::unprotect_inner(const Header& header, - output_bytes plaintext, - input_bytes ciphertext, - input_bytes metadata) -{ - if (ciphertext.size() < cipher_overhead(suite)) - { - throw buffer_too_small_error("Ciphertext too small for cipher overhead"); - } - - if (plaintext.size() < ciphertext.size() - cipher_overhead(suite)) - { - throw buffer_too_small_error("Plaintext too small for decrypted value"); - } - - const auto& key_and_salt = keys.at(header.key_id); - - const auto aad = form_aad(header, metadata); - const auto nonce = form_nonce(header.counter, key_and_salt.salt); - return open(suite, key_and_salt.key, nonce, plaintext, aad, ciphertext); -} - -/// -/// MLSContext -/// - -MLSContext::MLSContext(CipherSuite suite_in, size_t epoch_bits_in) : - Context(suite_in), - epoch_bits(epoch_bits_in), - epoch_mask((size_t(1) << epoch_bits_in) - 1) -{ - epoch_cache.resize(1 << epoch_bits_in); -} - -void MLSContext::add_epoch(EpochID epoch_id, input_bytes sframe_epoch_secret) -{ - add_epoch(epoch_id, sframe_epoch_secret, 0); -} - -void MLSContext::add_epoch(EpochID epoch_id, input_bytes sframe_epoch_secret, size_t sender_bits) -{ - auto epoch_index = epoch_id & epoch_mask; - auto& epoch = epoch_cache[epoch_index]; - - if (epoch) - { - purge_epoch(epoch->full_epoch); - } - - epoch.emplace(epoch_id, sframe_epoch_secret, epoch_bits, sender_bits); -} - -void MLSContext::purge_before(EpochID keeper) -{ - for (auto& ptr : epoch_cache) - { - if (ptr && ptr->full_epoch < keeper) - { - purge_epoch(ptr->full_epoch); - ptr.reset(); - } - } -} - -output_bytes MLSContext::protect(EpochID epoch_id, - SenderID sender_id, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata) -{ - return protect(epoch_id, sender_id, 0, ciphertext, plaintext, metadata); -} - -output_bytes MLSContext::protect(EpochID epoch_id, - SenderID sender_id, - ContextID context_id, - output_bytes ciphertext, - input_bytes plaintext, - input_bytes metadata) -{ - auto key_id = form_key_id(epoch_id, sender_id, context_id); - ensure_key(key_id, KeyUsage::protect); - return Context::protect(key_id, ciphertext, plaintext, metadata); -} - -output_bytes -MLSContext::unprotect(output_bytes plaintext, input_bytes ciphertext, input_bytes metadata) -{ - const auto header = Header::parse(ciphertext); - const auto inner_ciphertext = ciphertext.subspan(header.size()); - - ensure_key(header.key_id, KeyUsage::unprotect); - return Context::unprotect_inner(header, plaintext, inner_ciphertext, metadata); -} - -MLSContext::EpochKeys::EpochKeys(MLSContext::EpochID full_epoch_in, - input_bytes sframe_epoch_secret_in, - size_t epoch_bits, - size_t sender_bits_in) : - full_epoch(full_epoch_in), - sframe_epoch_secret(sframe_epoch_secret_in), - sender_bits(sender_bits_in) -{ - static constexpr uint64_t one = 1; - static constexpr size_t key_id_bits = 64; - - if (sender_bits > key_id_bits - epoch_bits) - { - throw invalid_parameter_error("Sender ID field too large"); - } - - // XXX(RLB) We use 0 as a signifier that the sender takes the rest of the key - // ID, and context IDs are not allowed. This would be more explicit if we - // used std::optional, but would require more modern C++. - if (sender_bits == 0) - { - sender_bits = key_id_bits - epoch_bits; - } - - context_bits = key_id_bits - sender_bits - epoch_bits; - max_sender_id = (one << sender_bits) - 1; - max_context_id = (one << context_bits) - 1; -} - -owned_bytes -MLSContext::EpochKeys::base_key(CipherSuite ciphersuite, SenderID sender_id) const -{ - const auto hash_size = cipher_digest_size(ciphersuite); - auto enc_sender_id = owned_bytes<8>(); - encode_uint(sender_id, enc_sender_id); - - return hkdf_expand(ciphersuite, sframe_epoch_secret, enc_sender_id, hash_size); -} - -void MLSContext::purge_epoch(EpochID epoch_id) -{ - const auto drop_bits = epoch_id & epoch_bits; - - keys.erase_if_key([&](const auto& epoch) { return (epoch & epoch_bits) == drop_bits; }); -} - -KeyID MLSContext::form_key_id(EpochID epoch_id, SenderID sender_id, ContextID context_id) const -{ - auto epoch_index = epoch_id & epoch_mask; - auto& epoch = epoch_cache[epoch_index]; - if (!epoch) - { - throw invalid_parameter_error("Unknown epoch. epoch_index: " + std::to_string(epoch_index) - + ", sender_id:" + std::to_string(sender_id)); - } - - if (sender_id > epoch->max_sender_id) - { - throw invalid_parameter_error("Sender ID overflow"); - } - - if (context_id > epoch->max_context_id) - { - throw invalid_parameter_error("Context ID overflow"); - } - - auto sender_part = uint64_t(sender_id) << epoch_bits; - auto context_part = uint64_t(0); - if (epoch->context_bits > 0) - { - context_part = uint64_t(context_id) << (epoch_bits + epoch->sender_bits); - } - - return KeyID(context_part | sender_part | epoch_index); -} - -void MLSContext::ensure_key(KeyID key_id, KeyUsage usage) -{ - // If the required key already exists, we are done - const auto epoch_index = key_id & epoch_mask; - auto& epoch = epoch_cache[epoch_index]; - if (!epoch) - { - throw invalid_parameter_error("Unknown epoch: " + std::to_string(epoch_index)); - } - - if (keys.contains(key_id)) - { - return; - } - - // Otherwise, derive a key and implant it - const auto sender_id = key_id >> epoch_bits; - Context::add_key(key_id, usage, epoch->base_key(suite, sender_id)); - return; -} - -} // namespace sframe diff --git a/firmware/ui/ui.ioc b/firmware/ui/ui.ioc index e6f83ff0..12028810 100644 --- a/firmware/ui/ui.ioc +++ b/firmware/ui/ui.ioc @@ -173,8 +173,8 @@ Mcu.PinsNb=52 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F405RGTx -MxCube.Version=6.13.0 -MxDb.Version=DB.6.0.130 +MxCube.Version=6.14.1 +MxDb.Version=DB.6.0.141 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.DMA1_Stream0_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA1_Stream5_IRQn=true\:1\:0\:true\:false\:true\:false\:true\:true @@ -400,6 +400,7 @@ PH1-OSC_OUT.Signal=RCC_OSC_OUT PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false +ProjectManager.CompilerLinker=GCC ProjectManager.CompilerOptimize=6 ProjectManager.ComputerToolchain=false ProjectManager.CoupleFile=false @@ -412,7 +413,7 @@ ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=true +ProjectManager.LastFirmware=false ProjectManager.LibraryCopy=0 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false @@ -423,12 +424,12 @@ ProjectManager.ProjectName=ui ProjectManager.ProjectStructure= ProjectManager.RegisterCallBack= ProjectManager.StackSize=0x400 -ProjectManager.TargetToolchain=Makefile +ProjectManager.TargetToolchain=CMake ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_ADC1_Init-ADC1-false-HAL-true,5-MX_I2C1_Init-I2C1-false-HAL-true,6-MX_I2S3_Init-I2S3-false-HAL-true,7-MX_SPI1_Init-SPI1-false-HAL-true,8-MX_USART1_UART_Init-USART1-false-HAL-true,9-MX_USART2_UART_Init-USART2-false-HAL-true,10-MX_TIM2_Init-TIM2-false-HAL-true,11-MX_RNG_Init-RNG-false-HAL-true,12-MX_CRC_Init-CRC-false-HAL-true,13-MX_TIM3_Init-TIM3-false-HAL-true,14-MX_TIM6_Init-TIM6-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_ADC1_Init-ADC1-false-HAL-true,5-MX_I2C1_Init-I2C1-false-HAL-true,6-MX_I2S3_Init-I2S3-false-HAL-true,7-MX_SPI1_Init-SPI1-false-HAL-true,8-MX_USART1_UART_Init-USART1-false-HAL-true,9-MX_USART2_UART_Init-USART2-false-HAL-true,10-MX_TIM2_Init-TIM2-false-HAL-true,11-MX_RNG_Init-RNG-false-HAL-true,12-MX_CRC_Init-CRC-false-HAL-true,13-MX_TIM3_Init-TIM3-false-HAL-true RCC.48MHZClocksFreq_Value=48000000 RCC.AHBFreq_Value=168000000 RCC.APB1CLKDivider=RCC_HCLK_DIV4