diff --git a/README.md b/README.md index 1104b9864..0f5fbc265 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ FSP uses an open software ecosystem and provides flexibility in using your prefe ### Current Release -[FSP v3.0.0](https://github.com/renesas/fsp/releases/tag/v3.0.0) +[FSP v3.0.1](https://github.com/renesas/fsp/releases/tag/v3.0.1) ### Supported RA MCU Kits @@ -48,7 +48,7 @@ If you have already installed a previous FSP release that included e² studio th #### For new users that are using FSP with e² studio -1. Download the FSP with e² studio Installer from the Assets section of the [current release](https://github.com/renesas/fsp/releases/tag/v3.0.0). +1. Download the FSP with e² studio Installer from the Assets section of the [current release](https://github.com/renesas/fsp/releases/tag/v3.0.1). 2. Run the installer. This will install the e² studio tool, FSP packs, GCC toolchain and other tools required to use this software. No additional installations are required. #### If using RA Smart Configurator (RASC) with IAR Embedded Workbench or Keil MDK #### diff --git a/ra/fsp/inc/api/r_ptp_api.h b/ra/fsp/inc/api/r_ptp_api.h index 443cbe4f1..d8436a594 100644 --- a/ra/fsp/inc/api/r_ptp_api.h +++ b/ra/fsp/inc/api/r_ptp_api.h @@ -114,7 +114,7 @@ typedef enum e_ptp_port_state /** Configure the PTP instance to operate as a E2E Master. */ PTP_PORT_STATE_E2E_MASTER = (PTP_PORT_STATE_GENERATE_ANNOUNCE | PTP_PORT_STATE_GENERATE_SYNC | - PTP_PORT_STATE_PROCESS_DELAY_RESP | + PTP_PORT_STATE_PROCESS_DELAY_REQ | PTP_PORT_STATE_PASSIVE), /** Configure the PTP instance to operate as a E2E Slave. */ @@ -128,7 +128,8 @@ typedef enum e_ptp_port_state PTP_PORT_STATE_P2P_MASTER = (PTP_PORT_STATE_GENERATE_ANNOUNCE | PTP_PORT_STATE_GENERATE_SYNC | PTP_PORT_STATE_GENERATE_PDELAY_REQ | - PTP_PORT_STATE_PROCESS_DELAY_RESP | + PTP_PORT_STATE_PROCESS_PDELAY_REQ | + PTP_PORT_STATE_PROCESS_PDELAY_RESP | PTP_PORT_STATE_PROCESS_PDELAY_RESP_FOLLOW_UP | PTP_PORT_STATE_PASSIVE), @@ -136,7 +137,8 @@ typedef enum e_ptp_port_state PTP_PORT_STATE_P2P_SLAVE = (PTP_PORT_STATE_GENERATE_PDELAY_REQ | PTP_PORT_STATE_PROCESS_SYNC | PTP_PORT_STATE_PROCESS_FOLLOW_UP | - PTP_PORT_STATE_PROCESS_DELAY_RESP | + PTP_PORT_STATE_PROCESS_PDELAY_REQ | + PTP_PORT_STATE_PROCESS_PDELAY_RESP | PTP_PORT_STATE_PROCESS_PDELAY_RESP_FOLLOW_UP | PTP_PORT_STATE_PASSIVE), diff --git a/ra/fsp/inc/fsp_version.h b/ra/fsp/inc/fsp_version.h index aea644402..0bd3921ff 100644 --- a/ra/fsp/inc/fsp_version.h +++ b/ra/fsp/inc/fsp_version.h @@ -44,16 +44,16 @@ #define FSP_VERSION_MINOR (0U) /** FSP pack patch version. */ -#define FSP_VERSION_PATCH (0U) +#define FSP_VERSION_PATCH (1U) /** FSP pack version build number (currently unused). */ #define FSP_VERSION_BUILD (0U) /** Public FSP version name. */ -#define FSP_VERSION_STRING ("3.0.0") +#define FSP_VERSION_STRING ("3.0.1") /** Unique FSP version ID. */ -#define FSP_VERSION_BUILD_STRING ("Built with Renesas Advanced Flexible Software Package version 3.0.0") +#define FSP_VERSION_BUILD_STRING ("Built with Renesas Advanced Flexible Software Package version 3.0.1") /********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include/renesas.h b/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include/renesas.h index 7e4a05bfe..f0be79d2c 100644 --- a/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include/renesas.h +++ b/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include/renesas.h @@ -989,52 +989,52 @@ typedef struct { union { - __IM uint32_t ID; /*!< (@ 0x00000000) Common FIFO Access ID Register */ + __IOM uint32_t ID; /*!< (@ 0x00000000) Common FIFO Access ID Register */ struct { - __IM uint32_t CFID : 29; /*!< [28..0] Common FIFO Buffer ID Field */ - uint32_t : 1; - __IM uint32_t CFRTR : 1; /*!< [30..30] Common FIFO Buffer RTR Frame */ - __IM uint32_t CFIDE : 1; /*!< [31..31] Common FIFO Buffer IDE Bit */ + __IOM uint32_t CFID : 29; /*!< [28..0] Common FIFO Buffer ID Field */ + uint32_t : 1; + __IOM uint32_t CFRTR : 1; /*!< [30..30] Common FIFO Buffer RTR Frame */ + __IOM uint32_t CFIDE : 1; /*!< [31..31] Common FIFO Buffer IDE Bit */ } ID_b; }; union { - __IM uint32_t PTR; /*!< (@ 0x00000004) Common FIFO Access Pointer Register */ + __IOM uint32_t PTR; /*!< (@ 0x00000004) Common FIFO Access Pointer Register */ struct { - __IM uint32_t CFTS : 16; /*!< [15..0] Common FIFO Timestamp Field */ - uint32_t : 12; - __IM uint32_t CFDLC : 4; /*!< [31..28] Common FIFO Buffer DLC Field */ + __IOM uint32_t CFTS : 16; /*!< [15..0] Common FIFO Timestamp Field */ + uint32_t : 12; + __IOM uint32_t CFDLC : 4; /*!< [31..28] Common FIFO Buffer DLC Field */ } PTR_b; }; union { - __IM uint32_t FDSTS; /*!< (@ 0x00000008) Common FIFO Access CAN-FD Status Register */ + __IOM uint32_t FDSTS; /*!< (@ 0x00000008) Common FIFO Access CAN-FD Status Register */ struct { - __IM uint32_t CFESI : 1; /*!< [0..0] Error State Indicator bit */ - __IM uint32_t CFBRS : 1; /*!< [1..1] Bit Rate Switch bit */ - __IM uint32_t CFFDF : 1; /*!< [2..2] CAN FD Format bit */ - uint32_t : 5; - __IM uint32_t CFIFL : 2; /*!< [9..8] Common FIFO Buffer Information Label Field */ - uint32_t : 6; - __IM uint32_t CFPTR : 16; /*!< [31..16] Common FIFO Buffer Pointer Field */ + __IOM uint32_t CFESI : 1; /*!< [0..0] Error State Indicator bit */ + __IOM uint32_t CFBRS : 1; /*!< [1..1] Bit Rate Switch bit */ + __IOM uint32_t CFFDF : 1; /*!< [2..2] CAN FD Format bit */ + uint32_t : 5; + __IOM uint32_t CFIFL : 2; /*!< [9..8] Common FIFO Buffer Information Label Field */ + uint32_t : 6; + __IOM uint32_t CFPTR : 16; /*!< [31..16] Common FIFO Buffer Pointer Field */ } FDSTS_b; }; union { - __IM uint8_t DF[64]; /*!< (@ 0x0000000C) Common FIFO Access Data Field Registers */ + __IOM uint8_t DF[64]; /*!< (@ 0x0000000C) Common FIFO Access Data Field Registers */ struct { - __IM uint8_t CFDB : 8; /*!< [7..0] Common FIFO Buffer Data Byte */ + __IOM uint8_t CFDB : 8; /*!< [7..0] Common FIFO Buffer Data Byte */ } DF_b[64]; }; __IM uint32_t RESERVED[13]; @@ -6235,24 +6235,23 @@ typedef struct /*!< (@ 0x400B0000) R_CANFD Structure __IOM R_CANFD_CFDRM_Type CFDRM[32]; /*!< (@ 0x00002000) RX Message Buffer Access Registers */ __IM uint32_t RESERVED41[3072]; __IOM R_CANFD_CFDRF_Type CFDRF[8]; /*!< (@ 0x00006000) RX FIFO Access Registers */ - __IM uint32_t RESERVED42[8]; - __IOM R_CANFD_CFDCF_Type CFDCF[5]; /*!< (@ 0x00006420) Common FIFO Access Registers */ - __IM uint32_t RESERVED43[1624]; + __IOM R_CANFD_CFDCF_Type CFDCF[5]; /*!< (@ 0x00006400) Common FIFO Access Registers */ + __IM uint32_t RESERVED42[1632]; __IOM R_CANFD_CFDTHL_Type CFDTHL[2]; /*!< (@ 0x00008000) Channel TX History List */ - __IM uint32_t RESERVED44[252]; + __IM uint32_t RESERVED43[252]; union { - __IOM uint32_t CFDRPGACC[64]; /*!< (@ 0x00008400) RAM Test Page Access Registers */ + __IOM uint32_t CFDRPGACC[64]; /*!< (@ 0x00008400) RAM Test Page Access Registers */ struct { - __IOM uint32_t RDTA : 32; /*!< [31..0] RAM Data Test Access */ + __IOM uint32_t RDTA : 32; /*!< [31..0] RAM Data Test Access */ } CFDRPGACC_b[64]; }; - __IM uint32_t RESERVED45[7872]; - __IOM R_CANFD_CFDTM_Type CFDTM[32]; /*!< (@ 0x00010000) TX Message Buffer Access Registers */ -} R_CANFD_Type; /*!< Size = 69632 (0x11000) */ + __IM uint32_t RESERVED44[7872]; + __IOM R_CANFD_CFDTM_Type CFDTM[128]; /*!< (@ 0x00010000) TX Message Buffer Access Registers */ +} R_CANFD_Type; /*!< Size = 81920 (0x14000) */ /* =========================================================================================================================== */ /* ================ R_CRC ================ */ diff --git a/ra/fsp/src/bsp/mcu/ra2e1/bsp_feature.h b/ra/fsp/src/bsp/mcu/ra2e1/bsp_feature.h index 96b9d9ea4..070ae62d2 100644 --- a/ra/fsp/src/bsp/mcu/ra2e1/bsp_feature.h +++ b/ra/fsp/src/bsp/mcu/ra2e1/bsp_feature.h @@ -199,7 +199,7 @@ #define BSP_FEATURE_GPT_VALID_CHANNEL_MASK (0x3F1) #define BSP_FEATURE_ICU_HAS_WUPEN1 (0) // Feature not available on this MCU -#define BSP_FEATURE_ICU_IRQ_CHANNELS_MASK (0x0FU) +#define BSP_FEATURE_ICU_IRQ_CHANNELS_MASK (0xFFU) #define BSP_FEATURE_ICU_WUPEN_MASK (0xF38F00FFU) #define BSP_FEATURE_IIC_FAST_MODE_PLUS (0U) diff --git a/ra/fsp/src/bsp/mcu/ra4m2/bsp_feature.h b/ra/fsp/src/bsp/mcu/ra4m2/bsp_feature.h index e76ad1c59..980d68434 100644 --- a/ra/fsp/src/bsp/mcu/ra4m2/bsp_feature.h +++ b/ra/fsp/src/bsp/mcu/ra4m2/bsp_feature.h @@ -235,7 +235,7 @@ #define BSP_FEATURE_LPM_DPSIEGR_MASK (0x13DFF3U) #define BSP_FEATURE_LPM_DPSIER_MASK (0x0D1FDFF3) #define BSP_FEATURE_LPM_HAS_DEEP_STANDBY (1U) -#define BSP_FEATURE_LPM_HAS_SBYCR_OPE (1U) +#define BSP_FEATURE_LPM_HAS_SBYCR_OPE (0U) #define BSP_FEATURE_LPM_HAS_SNZEDCR1 (1U) #define BSP_FEATURE_LPM_HAS_SNZREQCR1 (1U) #define BSP_FEATURE_LPM_HAS_STCONR (0U) diff --git a/ra/fsp/src/bsp/mcu/ra4m3/bsp_feature.h b/ra/fsp/src/bsp/mcu/ra4m3/bsp_feature.h index 73e1cf6c6..cdfcbbf76 100644 --- a/ra/fsp/src/bsp/mcu/ra4m3/bsp_feature.h +++ b/ra/fsp/src/bsp/mcu/ra4m3/bsp_feature.h @@ -201,7 +201,7 @@ #define BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE (64U) #define BSP_FEATURE_FLASH_HP_DF_WRITE_SIZE (4U) #define BSP_FEATURE_FLASH_HP_HAS_FMEPROT (1) -#define BSP_FEATURE_FLASH_HP_SUPPORTS_DUAL_BANK (1) +#define BSP_FEATURE_FLASH_HP_SUPPORTS_DUAL_BANK (0) // Feature not available on this MCU #define BSP_FEATURE_FLASH_HP_VERSION (40U) #define BSP_FEATURE_FLASH_LP_AWS_FAW_MASK (0) // Feature not available on this MCU #define BSP_FEATURE_FLASH_LP_AWS_FAW_SHIFT (0) // Feature not available on this MCU @@ -236,7 +236,7 @@ #define BSP_FEATURE_LPM_DPSIEGR_MASK (0x13FFFFU) #define BSP_FEATURE_LPM_DPSIER_MASK (0x0D1FFFFFU) #define BSP_FEATURE_LPM_HAS_DEEP_STANDBY (1U) -#define BSP_FEATURE_LPM_HAS_SBYCR_OPE (1U) +#define BSP_FEATURE_LPM_HAS_SBYCR_OPE (0U) #define BSP_FEATURE_LPM_HAS_SNZEDCR1 (1U) #define BSP_FEATURE_LPM_HAS_SNZREQCR1 (1U) #define BSP_FEATURE_LPM_HAS_STCONR (0U) diff --git a/ra/fsp/src/r_canfd/r_canfd.c b/ra/fsp/src/r_canfd/r_canfd.c index f1e0c306a..e247ce084 100644 --- a/ra/fsp/src/r_canfd/r_canfd.c +++ b/ra/fsp/src/r_canfd/r_canfd.c @@ -1053,7 +1053,7 @@ static void r_canfd_mode_transition (canfd_instance_ctrl_t * p_ctrl, can_operati /* Enable RX FIFOs */ for (uint32_t i = 0; i < CANFD_PRV_RX_FIFO_MAX; i++) { - R_CANFD->CFDRFCC_b[i].RFE = p_global_cfg->rx_fifo_config[i] & 1U; + R_CANFD->CFDRFCC[i] = p_global_cfg->rx_fifo_config[i]; } } } diff --git a/ra/fsp/src/r_ospi/r_ospi.c b/ra/fsp/src/r_ospi/r_ospi.c index b93db72b1..d71bbafb4 100644 --- a/ra/fsp/src/r_ospi/r_ospi.c +++ b/ra/fsp/src/r_ospi/r_ospi.c @@ -465,7 +465,7 @@ fsp_err_t R_OSPI_Erase (spi_flash_ctrl_t * p_ctrl, uint8_t * const p_device_addr /* If requested byte_count is supported by underlying flash, store the command. */ if (byte_count == p_cfg->p_erase_command_list[index].size) { - if (p_cfg_extend->memory_size == byte_count) + if (SPI_FLASH_ERASE_SIZE_CHIP_ERASE == byte_count) { /* Don't send address for chip erase. */ send_address = false; diff --git a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_func000.c b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_func000.c index 1abe1ecbf..dac804b4e 100644 --- a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_func000.c +++ b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_func000.c @@ -75,7 +75,7 @@ void R_SCE_func000(uint32_t *InData_PaddedMsg, int32_t MAX_CNT) (void)OFS_ADR; (void)MAX_CNT2; SCE->REG_104H = 0x000000b4U; - for (iLoop = 0; iLoop < MAX_CNT; iLoop = iLoop + 16) + for (iLoop = 0; iLoop < (uint32_t)MAX_CNT; iLoop = iLoop + 16) { /* WAIT_LOOP */ while (1U != SCE->REG_104H_b.B31) diff --git a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2a.c b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2a.c index de13035be..41556ad20 100644 --- a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2a.c +++ b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2a.c @@ -1177,7 +1177,7 @@ fsp_err_t R_SCE_GenerateRsa1024RandomKeyIndexSub(uint32_t MAX_CNT, uint32_t *Out } S_RAM[0+1 + 0] = change_endian_long(SCE->REG_100H); SCE->REG_ECH = 0x000037e9U; - for(jLoop = 0; jLoop < S_RAM[0+1]; jLoop = jLoop + 1) + for(jLoop = 0; jLoop < (int32_t)S_RAM[0+1]; jLoop = jLoop + 1) { SCE->REG_24H = 0x000009c0U; /* WAIT_LOOP */ diff --git a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2b.c b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2b.c index e16a08c97..fa0e6eadd 100644 --- a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2b.c +++ b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/primitive/r_sce_p2b.c @@ -1099,7 +1099,7 @@ fsp_err_t R_SCE_GenerateRsa2048RandomKeyIndexSub(uint32_t MAX_CNT, uint32_t *Out } S_RAM[0+1 + 0] = change_endian_long(SCE->REG_100H); SCE->REG_ECH = 0x000037e9U; - for(jLoop = 0; jLoop < S_RAM[0+1]; jLoop = jLoop + 1) + for(jLoop = 0; jLoop < (int32_t)S_RAM[0+1]; jLoop = jLoop + 1) { SCE->REG_24H = 0x000009c0U; /* WAIT_LOOP */ diff --git a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/public/r_sce_aes.c b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/public/r_sce_aes.c index 55d7e65fd..7c3b5c454 100644 --- a/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/public/r_sce_aes.c +++ b/ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/public/r_sce_aes.c @@ -3848,6 +3848,7 @@ static fsp_err_t prepare_gcm_iv (uint8_t * initial_vector, } else /* if (SCE_KEY_INDEX_TYPE_AES256 == wrapped_key->type) */ { + ret = R_SCE_Aes256EncryptDecryptInitSub(&indata_cmd, wrapped_key->value, zero); if (FSP_SUCCESS == ret) { R_SCE_Aes256EncryptDecryptUpdateSub(zero, hash_subkey, 4); diff --git a/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d.c b/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d.c index 8fa0d138f..87833a36b 100644 --- a/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d.c +++ b/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d.c @@ -383,10 +383,10 @@ extern void * rm_guix_port_jpeg_instance_get(ULONG display_handle); /*********************************************************************************************************************** * GUIX display driver function prototypes (called by GUIX) **********************************************************************************************************************/ -VOID _gx_synergy_buffer_toggle(GX_CANVAS * canvas, GX_RECTANGLE * dirty); +VOID _gx_ra_buffer_toggle(GX_CANVAS * canvas, GX_RECTANGLE * dirty); #if (GX_USE_SYNERGY_JPEG == 1) -VOID _gx_synergy_jpeg_draw(GX_DRAW_CONTEXT * p_context, INT x, INT y, GX_PIXELMAP * p_pixelmap); +VOID _gx_renesas_jpeg_draw(GX_DRAW_CONTEXT * p_context, INT x, INT y, GX_PIXELMAP * p_pixelmap); #endif @@ -2573,7 +2573,7 @@ VOID _gx_dave2d_buffer_toggle (GX_CANVAS * canvas, GX_RECTANGLE * dirty) * @param[in] canvas Pointer to a GUIX canvas * @param[in] dirty Pointer to a dirty rectangle area **********************************************************************************************************************/ -VOID _gx_synergy_buffer_toggle (GX_CANVAS * canvas, GX_RECTANGLE * dirty) +VOID _gx_ra_buffer_toggle (GX_CANVAS * canvas, GX_RECTANGLE * dirty) { GX_PARAMETER_NOT_USED(dirty); @@ -2669,7 +2669,7 @@ void _gx_renesas_jpeg_callback (jpeg_callback_args_t * p_args) * @param y[in] y axis pixel offset * @param p_pixelmap[in] Pointer to a pixelmap **********************************************************************************************************************/ -VOID _gx_synergy_jpeg_draw (GX_DRAW_CONTEXT * p_context, INT x, INT y, GX_PIXELMAP * p_pixelmap) +VOID _gx_renesas_jpeg_draw (GX_DRAW_CONTEXT * p_context, INT x, INT y, GX_PIXELMAP * p_pixelmap) { INT ret; jpeg_instance_t * p_jpeg; @@ -4300,7 +4300,7 @@ static VOID gx_renesas_display_driver_16bpp_32argb_pixelmap_rotate (GX_DRAW_CONT /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Subroutine for Hardware accelerated JPEG draw to open JPEG driver. - * This function is called by _gx_synergy_jpeg_draw(). + * This function is called by _gx_renesas_jpeg_draw(). * @param p_context[in] Pointer to a GUIX draw context * @param p_jpeg[in,out] Pointer to a JPEG driver instance * @param p_jpeg[in,out] Pointer to a JPEG driver instance @@ -4334,7 +4334,7 @@ static INT gx_renesas_jpeg_draw_open (GX_DRAW_CONTEXT * p_context, jpeg_instance /*******************************************************************************************************************//** * @brief Subroutine for YCBCR444 specific height which is called by Hardware accelerated JPEG draw that performs * JPEG decoding in output streaming mode. - * This function is called by _gx_synergy_jpeg_draw_minimum_height(). + * This function is called by _gx_renesas_jpeg_draw_minimum_height(). * @param width[in] Image width * @param height[in] Image height * @retval minimum_height Minimum height for JPEG decoding @@ -4359,7 +4359,7 @@ static UINT gx_renesas_jpeg_draw_minimum_height_ycbcr444 (GX_VALUE width, GX_VAL /*******************************************************************************************************************//** * @brief Subroutine for YCBCR422 specific height which is called by Hardware accelerated JPEG draw that performs * JPEG decoding in output streaming mode. - * This function is called by _gx_synergy_jpeg_draw_minimum_height(). + * This function is called by _gx_renesas_jpeg_draw_minimum_height(). * @param width[in] Image width * @param height[in] Image height * @retval minimum_height Minimum height for JPEG decoding @@ -4384,7 +4384,7 @@ static UINT gx_renesas_jpeg_draw_minimum_height_ycbcr422 (GX_VALUE width, GX_VAL /*******************************************************************************************************************//** * @brief Subroutine for YCBCR411 specific height which is called by Hardware accelerated JPEG draw that performs * JPEG decoding in output streaming mode. - * This function is called by _gx_synergy_jpeg_draw_minimum_height(). + * This function is called by _gx_renesas_jpeg_draw_minimum_height(). * @param width[in] Image width * @param height[in] Image height * @retval minimum_height Minimum height for JPEG decoding @@ -4409,7 +4409,7 @@ static UINT gx_renesas_jpeg_draw_minimum_height_ycbcr411 (GX_VALUE width, GX_VAL /*******************************************************************************************************************//** * @brief Subroutine for YCBCR420 specific height which is called by Hardware accelerated JPEG draw that performs * JPEG decoding in output streaming mode. - * This function is called by _gx_synergy_jpeg_draw_minimum_height(). + * This function is called by _gx_renesas_jpeg_draw_minimum_height(). * @param width[in] Image width * @param height[in] Image height * @retval minimum_height Minimum height for JPEG decoding @@ -4433,7 +4433,7 @@ static UINT gx_renesas_jpeg_draw_minimum_height_ycbcr420 (GX_VALUE width, GX_VAL /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Subroutine for Hardware accelerated JPEG draw to get minimum height. - * This function is called by _gx_synergy_jpeg_draw(). + * This function is called by _gx_renesas_jpeg_draw(). * @param format[in] JPEG color format (YCbCr) * @param width[in] Image width * @param height[in] Image height @@ -4483,7 +4483,7 @@ static UINT gx_renesas_jpeg_draw_minimum_height_get (jpeg_color_space_t format, /*******************************************************************************************************************//** * @brief Subroutine for Hardware accelerated JPEG draw to perform JPEG decoding in output streaming mode. - * This function is called by _gx_synergy_jpeg_draw_output_streaming(). + * This function is called by _gx_renesas_jpeg_draw_output_streaming(). * @param p_jpeg[in] Pointer to a parameter set for the JPEG decode framework instance * @retval FSP_SUCCESS Display device was opened successfully. * @retval Others See @ref Common_Error_Codes for other possible return codes. This function calls @@ -4518,7 +4518,7 @@ static INT gx_renesas_jpeg_draw_output_streaming_wait () /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Subroutine for Hardware accelerated JPEG draw to perform JPEG decoding in * output streaming mode. - * This function is called by _gx_synergy_jpeg_draw(). + * This function is called by _gx_renesas_jpeg_draw(). * @param p_param[in] Pointer to a parameter set for the JPEG decode output steaming **********************************************************************************************************************/ static VOID gx_renesas_jpeg_draw_output_streaming (jpeg_output_streaming_param_t * p_param) @@ -4644,8 +4644,8 @@ static VOID gx_renesas_jpeg_draw_output_streaming (jpeg_output_streaming_param_t /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Frame buffer toggle operation with copying data by software without - * D/AVE 2D acceleration and screen rotation. This function is called by _gx_synergy_buffer_toggle(). - * This function is called by _gx_synergy_buffer_toggle. + * D/AVE 2D acceleration and screen rotation. This function is called by _gx_ra_buffer_toggle(). + * This function is called by _gx_ra_buffer_toggle. * @param[in] canvas Pointer to a canvas * @param[in] copy Pointer to a rectangle region to copy. **********************************************************************************************************************/ @@ -4883,7 +4883,7 @@ static VOID gx_rotate_canvas_to_working_16bpp_draw_rorate270 (USHORT * pGetRow, /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Frame buffer software copy operation with screen rotation for 16bpp color - * format. This function is called by _gx_synergy_buffer_toggle(). + * format. This function is called by _gx_ra_buffer_toggle(). * @param[in] canvas Pointer to a canvas * @param[in] copy Pointer to a rectangle region to copy. * @param[in] angle Rotation angle (0, 90, 180 or 270) @@ -5123,7 +5123,7 @@ static VOID gx_rotate_canvas_to_working_32bpp_draw_rorate270 (ULONG * pGetRow, /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Frame buffer software copy operation with screen rotation for 32bpp color - * format. This function is called by _gx_synergy_buffer_toggle(). + * format. This function is called by _gx_ra_buffer_toggle(). * @param[in] canvas Pointer to a canvas * @param[in] copy Pointer to a rectangle region to copy. * @param[in] angle Rotation angle (0, 90, 180 or 270) diff --git a/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d_8bit_palette.c b/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d_8bit_palette.c index 892f31400..43bb8704d 100644 --- a/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d_8bit_palette.c +++ b/ra/fsp/src/rm_guix_port/gx_display_driver_dave2d_8bit_palette.c @@ -187,7 +187,7 @@ extern void rm_guix_port_display_8bit_palette_assign(ULONG display_handle); **********************************************************************************************************************/ extern VOID _gx_dave2d_drawing_initiate(GX_DISPLAY * display, GX_CANVAS * canvas); -VOID _gx_synergy_buffer_toggle_8bpp(GX_CANVAS * canvas, GX_RECTANGLE * dirty); +VOID _gx_ra_buffer_toggle_8bpp(GX_CANVAS * canvas, GX_RECTANGLE * dirty); VOID _gx_display_driver_8bit_palette_assign(GX_DISPLAY * display, GX_COLOR * palette, INT count); #if (GX_USE_SYNERGY_DRW == 1) @@ -1133,7 +1133,7 @@ VOID _gx_display_driver_8bit_palette_assign (GX_DISPLAY * display, GX_COLOR * pa * @param[in] canvas Pointer to a GUIX canvas * @param[in] dirty Pointer to a dirty rectangle area **********************************************************************************************************************/ -VOID _gx_synergy_buffer_toggle_8bpp (GX_CANVAS * canvas, GX_RECTANGLE * dirty) +VOID _gx_ra_buffer_toggle_8bpp (GX_CANVAS * canvas, GX_RECTANGLE * dirty) { GX_PARAMETER_NOT_USED(dirty); @@ -1633,7 +1633,7 @@ static VOID gx_sw_8bpp_glyph_1bit_draw (GX_DRAW_CONTEXT * context, /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, Frame buffer toggle operation with copying data by software without * D/AVE 2D acceleration and screen rotation. - * This function is called by _gx_synergy_buffer_toggle_8bpp(). + * This function is called by _gx_ra_buffer_toggle_8bpp(). * @param[in] canvas Pointer to a canvas * @param[in] copy Pointer to a rectangle region to copy **********************************************************************************************************************/ @@ -1851,7 +1851,7 @@ static VOID gx_rotate_canvas_to_working_8bpp_draw_rorate270 (GX_UBYTE * pGetRow, /*******************************************************************************************************************//** * @brief GUIX display driver for FSP, 8bpp Frame buffer software copy operation with screen rotation. - * This function is called by _gx_synergy_buffer_toggle_8bpp(). + * This function is called by _gx_ra_buffer_toggle_8bpp(). * @param[in] canvas Pointer to a canvas * @param[in] copy Pointer to a rectangle region to copy. * @param[in] angle Rotation angle (0, 90, 180 or 270) diff --git a/ra/fsp/src/rm_guix_port/rm_guix_port.c b/ra/fsp/src/rm_guix_port/rm_guix_port.c index 23d00b375..7e188a4c1 100644 --- a/ra/fsp/src/rm_guix_port/rm_guix_port.c +++ b/ra/fsp/src/rm_guix_port/rm_guix_port.c @@ -74,7 +74,7 @@ static fsp_err_t rm_guix_port_d2_close(rm_guix_port_instance_ctrl_t * const p_ct #endif /* GUIX common callback functions setup function */ -extern UINT _gx_synergy_display_driver_setup(GX_DISPLAY * display); +extern UINT _gx_ra_display_driver_setup(GX_DISPLAY * display); /* GUIX FSP Port callback functions */ void rm_guix_port_frame_pointers_get(ULONG display_handle, GX_UBYTE ** pp_visible, GX_UBYTE ** pp_working); @@ -324,7 +324,7 @@ static fsp_err_t rm_guix_port_display_open (rm_guix_port_instance_ctrl_t * const /*******************************************************************************************************************//** * @brief Setups GUIX display driver. - * This function calls _gx_synergy_display_driver_setup(), which is generated by GUIX Studio, and calls subroutines + * This function calls _gx_ra_display_driver_setup(), which is generated by GUIX Studio, and calls subroutines * rm_guix_port_d2_open() and rm_guix_port_display_open() to setup graphics hardwares, also calls rm_guix_port_canvas_clear to clear * a canvas. * This function is called by rm_guix_port_hw_initialize(). @@ -342,7 +342,7 @@ static fsp_err_t rm_guix_port_driver_setup (GX_DISPLAY * p_display, rm_guix_port fsp_err_t error; /** Setups GUIX draw functions */ - _gx_synergy_display_driver_setup(p_display); + _gx_ra_display_driver_setup(p_display); #if GX_USE_SYNERGY_DRW @@ -601,7 +601,7 @@ void * rm_guix_port_jpeg_buffer_get (ULONG display_handle, INT * p_memory_size) /*******************************************************************************************************************//** * @brief GUIX Port for FSP, get the instance of JPEG Framework module. - * This function is called by _gx_synergy_jpeg_draw(). + * This function is called by _gx_renesas_jpeg_draw(). * @param[in] display_handle Pointer to the RM_GUIX_PORT control block. * @retval Address Pointer to an instance of JPEG Framework module. **********************************************************************************************************************/ diff --git a/ra/fsp/src/rm_netx_secure_crypto/nx_crypto_rsa_alt_process.c b/ra/fsp/src/rm_netx_secure_crypto/nx_crypto_rsa_alt_process.c index b65120c71..83f73c95f 100644 --- a/ra/fsp/src/rm_netx_secure_crypto/nx_crypto_rsa_alt_process.c +++ b/ra/fsp/src/rm_netx_secure_crypto/nx_crypto_rsa_alt_process.c @@ -59,6 +59,7 @@ * @retval NX_CRYPTO_SUCCESS Encryption/decryption operation was successful. * @retval NX_CRYPTO_NOT_SUCCESSFUL Encryption/decryption operation failed. * @retval NX_CRYPTO_UNSUPPORTED_KEY_SIZE Unsupported key size based on the exponent length passed. + * @retval NX_CRYPTO_SIZE_ERROR Invalid modulus length. **********************************************************************************************************************/ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, UINT exponent_length, @@ -73,6 +74,20 @@ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, /* Check exponent length to determine key type and format */ if (exponent_length <= RM_NETX_SECURE_CRYPTO_RSA_EXPONENT_LENGTH_BYTES) { + UINT modulus_size_max = 0; + #if (1U == NETX_SECURE_CRYPTO_NX_CRYPTO_METHODS_RSA_4096_ALT) + uint32_t aligned_modulus[HW_SCE_RSA_4096_KEY_D_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + modulus_size_max = (UINT) HW_SCE_RSA_4096_KEY_D_LENGTH_BYTE_SIZE; + #elif (1U == NETX_SECURE_CRYPTO_NX_CRYPTO_METHODS_RSA_3072_ALT) + uint32_t aligned_modulus[HW_SCE_RSA_3072_KEY_D_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + modulus_size_max = (UINT) HW_SCE_RSA_3072_KEY_D_LENGTH_BYTE_SIZE; + #else + uint32_t aligned_modulus[HW_SCE_RSA_2048_KEY_D_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + modulus_size_max = (UINT) HW_SCE_RSA_2048_KEY_D_LENGTH_BYTE_SIZE; + #endif + FSP_ERROR_RETURN((modulus_size_max >= modulus_length), NX_CRYPTO_SIZE_ERROR); + NX_CRYPTO_MEMCPY(aligned_modulus, modulus, modulus_length); + /* Unformatted public key : This is an Encryption operation */ uint8_t padded_exponent[4U] = {0}; NX_CRYPTO_MEMCPY(&padded_exponent[RM_NETX_SECURE_CRYPTO_RSA_EXPONENT_LENGTH_BYTES - exponent_length], @@ -85,11 +100,12 @@ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, case HW_SCE_RSA_4096_KEY_N_LENGTH_BYTE_SIZE: { #if (1U == NETX_SECURE_CRYPTO_NX_CRYPTO_METHODS_RSA_4096_ALT) + uint32_t aligned_work_buffer[HW_SCE_RSA_4096_KEY_N_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + NX_CRYPTO_MEMCPY(aligned_work_buffer, input, sizeof(aligned_work_buffer)); err = - HW_SCE_RSA_4096PublicKeyEncrypt((uint32_t *) input, - (uint32_t *) padded_exponent, - (uint32_t *) modulus, - (uint32_t *) output); + HW_SCE_RSA_4096PublicKeyEncrypt((uint32_t *) aligned_work_buffer, (uint32_t *) padded_exponent, + (uint32_t *) aligned_modulus, (uint32_t *) aligned_work_buffer); + NX_CRYPTO_MEMCPY(output, aligned_work_buffer, sizeof(aligned_work_buffer)); #endif break; } @@ -97,11 +113,12 @@ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, case HW_SCE_RSA_3072_KEY_N_LENGTH_BYTE_SIZE: { #if (1U == NETX_SECURE_CRYPTO_NX_CRYPTO_METHODS_RSA_3072_ALT) + uint32_t aligned_work_buffer[HW_SCE_RSA_3072_KEY_N_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + NX_CRYPTO_MEMCPY(aligned_work_buffer, input, sizeof(aligned_work_buffer)); err = - HW_SCE_RSA_3072PublicKeyEncrypt((uint32_t *) input, - (uint32_t *) padded_exponent, - (uint32_t *) modulus, - (uint32_t *) output); + HW_SCE_RSA_3072PublicKeyEncrypt((uint32_t *) aligned_work_buffer, (uint32_t *) padded_exponent, + (uint32_t *) aligned_modulus, (uint32_t *) aligned_work_buffer); + NX_CRYPTO_MEMCPY(output, aligned_work_buffer, sizeof(aligned_work_buffer)); #endif break; } @@ -109,11 +126,14 @@ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, case HW_SCE_RSA_2048_KEY_N_LENGTH_BYTE_SIZE: { #if (1U == NETX_SECURE_CRYPTO_NX_CRYPTO_METHODS_RSA_2048_ALT) + uint32_t aligned_work_buffer[HW_SCE_RSA_2048_KEY_N_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + NX_CRYPTO_MEMCPY(aligned_work_buffer, input, sizeof(aligned_work_buffer)); err = - HW_SCE_RSA_2048PublicKeyEncrypt((uint32_t *) input, + HW_SCE_RSA_2048PublicKeyEncrypt((uint32_t *) aligned_work_buffer, (uint32_t *) padded_exponent, - (uint32_t *) modulus, - (uint32_t *) output); + aligned_modulus, + (uint32_t *) aligned_work_buffer); + NX_CRYPTO_MEMCPY(output, aligned_work_buffer, sizeof(aligned_work_buffer)); #endif break; } @@ -129,15 +149,25 @@ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, else if (HW_SCE_RSA_2048_KEY_D_LENGTH_BYTE_SIZE == exponent_length) { uint32_t key[HW_SCE_RSA2048_ND_KEY_BYTE_SIZE >> 2U] = {0}; + uint32_t aligned_work_buffer[HW_SCE_RSA_2048_KEY_N_LENGTH_BYTE_SIZE / sizeof(uint32_t)] = {0}; + NX_CRYPTO_MEMCPY(aligned_work_buffer, input, sizeof(aligned_work_buffer)); NX_CRYPTO_MEMCPY(key, modulus, modulus_length); NX_CRYPTO_MEMCPY(&key[modulus_length >> 2U], exponent, exponent_length); /* Plain private key: This is a decryption operation */ - err = HW_SCE_RSA_2048PrivateKeyDecrypt((uint32_t *) input, NULL, key, (uint32_t *) output); + err = HW_SCE_RSA_2048PrivateKeyDecrypt((uint32_t *) aligned_work_buffer, + NULL, + key, + (uint32_t *) aligned_work_buffer); + NX_CRYPTO_MEMCPY(output, aligned_work_buffer, sizeof(aligned_work_buffer)); } /* This is a special case where the entire wrapped key is passed through the exponent parameter */ else if (RM_NETX_SECURE_CRYPTO_WORDS_TO_BYTES(HW_SCE_RSA2048_ND_KEY_INDEX_WORD_SIZE) == exponent_length) { + /* Input parameters are expected to be word aligned here as this section is executed when + * the user application directly. NetX Secure does not get to this section. + */ + /* Check the input message length to ensure its not larger than the modulus size. * Note: Since the wrapped key (and not the plain modulus) is passed during init API, * a larger length is allowed by the caller during the operation API. @@ -148,7 +178,7 @@ UINT sce_nx_crypto_rsa_operation (const UCHAR * exponent, return NX_CRYPTO_PTR_ERROR; } - /* Wrapped private key : This is a decryption operation */ + /* Wrapped private key : This is a decryption operation. */ err = HW_SCE_HRK_RSA_2048PrivateKeyDecrypt((uint32_t *) input, (uint32_t *) exponent, NULL, (uint32_t *) output); } diff --git a/ra/fsp/src/rm_netxduo_ether/rm_netxduo_ether.c b/ra/fsp/src/rm_netxduo_ether/rm_netxduo_ether.c index 4175ee0d6..092520a36 100644 --- a/ra/fsp/src/rm_netxduo_ether/rm_netxduo_ether.c +++ b/ra/fsp/src/rm_netxduo_ether/rm_netxduo_ether.c @@ -176,6 +176,12 @@ void rm_netxduo_ether (NX_IP_DRIVER * driver_req_ptr, rm_netxduo_ether_instance_ /* THe link has not been established yet. */ driver_req_ptr->nx_ip_driver_status = NX_INVALID_INTERFACE; + /* Release the NetX packet because it cannot be sent. */ + if (NX_SUCCESS != nx_packet_transmit_release(driver_req_ptr->nx_ip_driver_packet)) + { + FSP_LOG_PRINT("Failed to release transmit packet."); + } + return; } @@ -391,141 +397,149 @@ void rm_netxduo_ether_receive_packet (rm_netxduo_ether_instance_t * p_netxduo_et NX_PACKET * p_nx_packet = NULL; do { - /* Allocate NetX packet to copy data into. */ - if (NX_SUCCESS != - nx_packet_allocate(p_netxduo_ether_instance->p_ctrl->p_ip->nx_ip_default_packet_pool, &p_nx_packet, - NX_RECEIVE_PACKET, NX_NO_WAIT)) - { - /* - * Buffer overflow. - * Wait until more packets are available. - */ - FSP_LOG_PRINT("Failed to allocate NetX Packet."); - break; - } - - /* - * Make sure that the buffer is 32 byte aligned (See section 31.3.1.2 "Receive descriptor" in the RA6M3 User Manual R01UH0886EJ0100) - */ - p_nx_packet->nx_packet_prepend_ptr = - (UCHAR *) (((uint32_t) p_nx_packet->nx_packet_prepend_ptr + 31U) & ~(31U)); - /* Get a pointer to the packet received. */ uint8_t * p_buffer_out; uint32_t length; err = p_ether_instance->p_api->read(p_ether_instance->p_ctrl, &p_buffer_out, &length); if (FSP_SUCCESS != err) { - /* No data to read. */ - if (NX_SUCCESS != nx_packet_release(p_nx_packet)) - { - FSP_LOG_PRINT("Failed to release NetX Packet."); - } - break; } - /* Update the buffer pointer in the buffer descriptor. */ - if (FSP_SUCCESS != - p_ether_instance->p_api->rxBufferUpdate(p_ether_instance->p_ctrl, p_nx_packet->nx_packet_prepend_ptr)) + /* Allocate NetX packet to copy data into. */ + if (NX_SUCCESS != + nx_packet_allocate(p_netxduo_ether_instance->p_ctrl->p_ip->nx_ip_default_packet_pool, &p_nx_packet, + NX_RECEIVE_PACKET, NX_NO_WAIT)) { - FSP_LOG_PRINT("Failed to update buffer in r_ether driver."); - break; + /* If a NetX packet could not be allocated, then the received packet must be dropped in order to receive the next packet. */ + FSP_LOG_PRINT("Failed to allocate NetX Packet."); + + /* Update the buffer pointer in the buffer descriptor. */ + if (FSP_SUCCESS != p_ether_instance->p_api->rxBufferUpdate(p_ether_instance->p_ctrl, p_buffer_out)) + { + FSP_LOG_PRINT("Failed to update buffer in r_ether driver."); + } } + else + { + /* + * Make sure that the buffer is 32 byte aligned (See section 31.3.1.2 "Receive descriptor" in the RA6M3 User Manual R01UH0886EJ0100) + */ + p_nx_packet->nx_packet_prepend_ptr = + (UCHAR *) (((uint32_t) p_nx_packet->nx_packet_prepend_ptr + 31U) & ~(31U)); - uint32_t index = p_netxduo_ether_instance->p_ctrl->rx_packet_index; + /* Update the buffer pointer in the buffer descriptor. */ + if (FSP_SUCCESS != + p_ether_instance->p_api->rxBufferUpdate(p_ether_instance->p_ctrl, p_nx_packet->nx_packet_prepend_ptr)) + { + FSP_LOG_PRINT("Failed to update buffer in r_ether driver."); + break; + } - /* Pick up the destination MAC address from the packet. */ - ULONG destination_address_msw = (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr); - destination_address_msw = (destination_address_msw << 8) | - (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 1); - ULONG destination_address_lsw = (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 2); - destination_address_lsw = (destination_address_lsw << 8) | - (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 3); - destination_address_lsw = (destination_address_lsw << 8) | - (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 4); - destination_address_lsw = (destination_address_lsw << 8) | - (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 5); + uint32_t index = p_netxduo_ether_instance->p_ctrl->rx_packet_index; - bool multicast_group = false; + /* Pick up the destination MAC address from the packet. */ + ULONG destination_address_msw = (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr); + destination_address_msw = (destination_address_msw << 8) | + (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 1); + ULONG destination_address_lsw = (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 2); + destination_address_lsw = (destination_address_lsw << 8) | + (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 3); + destination_address_lsw = (destination_address_lsw << 8) | + (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 4); + destination_address_lsw = (destination_address_lsw << 8) | + (ULONG) *(p_nx_buffers[index]->nx_packet_prepend_ptr + 5); - /* Check if the packet is an IPv4 Multicast packet. */ - if ((destination_address_msw == 0x00000100U) && ((destination_address_lsw >> 24U) == 0x5EU)) // NOLINT(readability-magic-numbers) - { - /* Check if the IP instance is a member of the group. */ - for (uint32_t i = 0; i < NX_MAX_MULTICAST_GROUPS; i++) + bool multicast_group = false; + + /* Check if the packet is an IPv4 Multicast packet. */ + if ((destination_address_msw == 0x00000100U) && ((destination_address_lsw >> 24U) == 0x5EU)) // NOLINT(readability-magic-numbers) { - /* IPv4 multicast MAC addreses always begin with 0x0100 so only destination_address_lsw needs to be checked. */ - if (destination_address_lsw == - p_netxduo_ether_instance->p_ctrl->multicast_mac_addresses[i].mac_address_lsw) + /* Check if the IP instance is a member of the group. */ + for (uint32_t i = 0; i < NX_MAX_MULTICAST_GROUPS; i++) { - multicast_group = true; - break; + /* IPv4 multicast MAC addreses always begin with 0x0100 so only destination_address_lsw needs to be checked. */ + if (destination_address_lsw == + p_netxduo_ether_instance->p_ctrl->multicast_mac_addresses[i].mac_address_lsw) + { + multicast_group = true; + break; + } } } - } - /* Only process packets that are meant for this mac address (dest=Broadcast/mac_address). */ - if (((destination_address_msw == ((ULONG) 0x0000FFFF)) && // NOLINT(readability-magic-numbers) - (destination_address_lsw == ((ULONG) 0xFFFFFFFF))) || // NOLINT(readability-magic-numbers) - ((destination_address_msw == mac_msw) && // NOLINT(readability-magic-numbers) - (destination_address_lsw == mac_lsw)) || // NOLINT(readability-magic-numbers) - (destination_address_msw == ((ULONG) 0x00003333)) || // NOLINT(readability-magic-numbers) - ((destination_address_msw == 0) && (destination_address_lsw == 0)) || - multicast_group) - { - /* Get the Ethernet packet id. */ - UINT packet_type = (((UINT) (*(p_nx_buffers[index]->nx_packet_prepend_ptr + 12))) << 8) | - ((UINT) (*(p_nx_buffers[index]->nx_packet_prepend_ptr + 13))); + /* Only process packets that are meant for this mac address (dest=Broadcast/mac_address). */ + if (((destination_address_msw == ((ULONG) 0x0000FFFF)) && // NOLINT(readability-magic-numbers) + (destination_address_lsw == ((ULONG) 0xFFFFFFFF))) || // NOLINT(readability-magic-numbers) + ((destination_address_msw == mac_msw) && // NOLINT(readability-magic-numbers) + (destination_address_lsw == mac_lsw)) || // NOLINT(readability-magic-numbers) + (destination_address_msw == ((ULONG) 0x00003333)) || // NOLINT(readability-magic-numbers) + ((destination_address_msw == 0) && (destination_address_lsw == 0)) || + multicast_group) + { + /* Get the Ethernet packet id. */ + UINT packet_type = (((UINT) (*(p_nx_buffers[index]->nx_packet_prepend_ptr + 12))) << 8) | + ((UINT) (*(p_nx_buffers[index]->nx_packet_prepend_ptr + 13))); - if ((packet_type == NX_ETHERNET_IP) || - (packet_type == NX_ETHERNET_IPV6) || - (packet_type == NX_ETHERNET_ARP) + if ((packet_type == NX_ETHERNET_IP) || + (packet_type == NX_ETHERNET_IPV6) || + (packet_type == NX_ETHERNET_ARP) #if RM_NETXDUO_ETHER_RARP_SUPPORT - || (packet_type == NX_ETHERNET_RARP) + || (packet_type == NX_ETHERNET_RARP) #endif - ) - { - /* Move the append ptr to the new end of data. */ - p_nx_buffers[index]->nx_packet_append_ptr = p_nx_buffers[index]->nx_packet_prepend_ptr + - p_nx_buffers[index]->nx_packet_length; + ) + { + /* Move the append ptr to the new end of data. */ + p_nx_buffers[index]->nx_packet_append_ptr = p_nx_buffers[index]->nx_packet_prepend_ptr + + p_nx_buffers[index]->nx_packet_length; - /* Remove the Ethernet packet header. */ - uint32_t padding = p_ether_instance->p_cfg->padding; - p_nx_buffers[index]->nx_packet_prepend_ptr += (NX_ETHERNET_SIZE + padding); - p_nx_buffers[index]->nx_packet_length -= (NX_ETHERNET_SIZE + padding); + /* Remove the Ethernet packet header. */ + uint32_t padding = p_ether_instance->p_cfg->padding; + p_nx_buffers[index]->nx_packet_prepend_ptr += (NX_ETHERNET_SIZE + padding); + p_nx_buffers[index]->nx_packet_length -= (NX_ETHERNET_SIZE + padding); - switch (packet_type) - { - case NX_ETHERNET_IP: + switch (packet_type) + { + case NX_ETHERNET_IP: #ifndef NX_DISABLE_IPV6 - case NX_ETHERNET_IPV6: + case NX_ETHERNET_IPV6: #endif + { + /* Process the IP packet. */ + _nx_ip_packet_deferred_receive(p_netxduo_ether_instance->p_ctrl->p_ip, + p_nx_buffers[index]); + break; + } + + case NX_ETHERNET_ARP: { - /* Process the IP packet. */ - _nx_ip_packet_deferred_receive(p_netxduo_ether_instance->p_ctrl->p_ip, p_nx_buffers[index]); + /* Process the ARP packet. */ + _nx_arp_packet_deferred_receive(p_netxduo_ether_instance->p_ctrl->p_ip, + p_nx_buffers[index]); break; } - case NX_ETHERNET_ARP: - { - /* Process the ARP packet. */ - _nx_arp_packet_deferred_receive(p_netxduo_ether_instance->p_ctrl->p_ip, p_nx_buffers[index]); - break; - } - #if RM_NETXDUO_ETHER_RARP_SUPPORT - case NX_ETHERNET_RARP: - { - /* Process the RARP packet. */ - _nx_rarp_packet_deferred_receive(p_netxduo_ether_instance->p_ctrl->p_ip, p_nx_buffers[index]); - break; - } + case NX_ETHERNET_RARP: + { + /* Process the RARP packet. */ + _nx_rarp_packet_deferred_receive(p_netxduo_ether_instance->p_ctrl->p_ip, + p_nx_buffers[index]); + break; + } #endif - default: + default: + { + break; + } + } + } + else + { + if (NX_SUCCESS != nx_packet_release(p_nx_buffers[index])) { - break; + FSP_LOG_PRINT("Failed to release NetX Packet."); } } } @@ -536,17 +550,10 @@ void rm_netxduo_ether_receive_packet (rm_netxduo_ether_instance_t * p_netxduo_et FSP_LOG_PRINT("Failed to release NetX Packet."); } } - } - else - { - if (NX_SUCCESS != nx_packet_release(p_nx_buffers[index])) - { - FSP_LOG_PRINT("Failed to release NetX Packet."); - } - } - /* Store pointer to the newly allocated NetX Packet at the index where it was written into the r_ether buffer descriptor. */ - p_nx_buffers[index] = p_nx_packet; + /* Store pointer to the newly allocated NetX Packet at the index where it was written into the r_ether buffer descriptor. */ + p_nx_buffers[index] = p_nx_packet; + } p_netxduo_ether_instance->p_ctrl->rx_packet_index++; if (p_netxduo_ether_instance->p_ctrl->rx_packet_index == p_ether_instance->p_cfg->num_rx_descriptors) @@ -653,8 +660,8 @@ void rm_netxduo_ether_callback (ether_callback_args_t * p_args) case ETHER_EVENT_LINK_ON: { - FSP_CRITICAL_SECTION_DEFINE; - FSP_CRITICAL_SECTION_ENTER; + /* Disable Ethernet IRQs so that the receive buffer descriptor can be initialized without being interrupted. */ + R_BSP_IrqDisable(p_ether_instance->p_cfg->irq); /* Allocate NetX Packets required for receiving data. */ NX_PACKET ** p_rx_buffers = p_netxduo_ether_instance->p_cfg->p_rx_packets; @@ -662,7 +669,7 @@ void rm_netxduo_ether_callback (ether_callback_args_t * p_args) { if (NX_SUCCESS != nx_packet_allocate(p_netxduo_ether_instance->p_ctrl->p_ip->nx_ip_default_packet_pool, - &p_rx_buffers[i], NX_RECEIVE_PACKET, TX_WAIT_FOREVER)) + &p_rx_buffers[i], NX_RECEIVE_PACKET, TX_NO_WAIT)) { FSP_LOG_PRINT("Failed to allocate NetX Packet."); } @@ -682,7 +689,7 @@ void rm_netxduo_ether_callback (ether_callback_args_t * p_args) } } - FSP_CRITICAL_SECTION_EXIT; + R_BSP_IrqEnable(p_ether_instance->p_cfg->irq); /* Notify NetX that the link is up. */ p_netxduo_ether_instance->p_ctrl->p_interface->nx_interface_link_up = NX_TRUE; @@ -692,11 +699,10 @@ void rm_netxduo_ether_callback (ether_callback_args_t * p_args) case ETHER_EVENT_LINK_OFF: { - /* Release NetX Packets (New packets will be allocated when the link is up). */ - - FSP_CRITICAL_SECTION_DEFINE; - FSP_CRITICAL_SECTION_ENTER; - + /* + * When the link is re-established, the Ethernet driver will reset all of the buffer descriptors. + * Release NetX Packets (New packets will be allocated when the link is up). + */ p_netxduo_ether_instance->p_ctrl->tx_packet_index = 0; p_netxduo_ether_instance->p_ctrl->tx_packet_transmitted_index = 0; NX_PACKET ** p_tx_buffers = p_netxduo_ether_instance->p_cfg->p_tx_packets; @@ -730,8 +736,6 @@ void rm_netxduo_ether_callback (ether_callback_args_t * p_args) } } - FSP_CRITICAL_SECTION_EXIT; - /* Notify NetX that the link is down. */ p_netxduo_ether_instance->p_ctrl->p_interface->nx_interface_link_up = NX_FALSE; _nx_ip_driver_link_status_event(p_netxduo_ether_instance->p_ctrl->p_ip, p_args->channel); diff --git a/ra/fsp/src/rm_psa_crypto/platform_alt.c b/ra/fsp/src/rm_psa_crypto/platform_alt.c index 8d99919f9..e94481001 100644 --- a/ra/fsp/src/rm_psa_crypto/platform_alt.c +++ b/ra/fsp/src/rm_psa_crypto/platform_alt.c @@ -25,7 +25,7 @@ #endif #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) - #include "platform.h" + #include "mbedtls/platform.h" #include "hw_sce_private.h" #if defined(CONFIG_MEDTLS_USE_AFR_MEMORY) && defined(MBEDTLS_PLATFORM_MEMORY) && \ diff --git a/ra/fsp/src/rm_psa_crypto/rm_psa_crypto.c b/ra/fsp/src/rm_psa_crypto/rm_psa_crypto.c index c074cb369..5edb01d5d 100644 --- a/ra/fsp/src/rm_psa_crypto/rm_psa_crypto.c +++ b/ra/fsp/src/rm_psa_crypto/rm_psa_crypto.c @@ -25,7 +25,7 @@ #endif #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) - #include "platform.h" + #include "mbedtls/platform.h" #include "rm_psa_crypto.h" #define RM_PSA_CRYPTO_TRNG_REGISTER_SIZE_WORDS (4U) diff --git a/ra/fsp/src/rm_psa_crypto/trng_entropy.c b/ra/fsp/src/rm_psa_crypto/trng_entropy.c index b2cb0f78b..2d09fb791 100644 --- a/ra/fsp/src/rm_psa_crypto/trng_entropy.c +++ b/ra/fsp/src/rm_psa_crypto/trng_entropy.c @@ -24,10 +24,10 @@ #include MBEDTLS_CONFIG_FILE #endif -#include "entropy.h" -#include "entropy_poll.h" +#include "mbedtls/entropy.h" +#include "mbedtls/entropy_poll.h" -#include "platform.h" +#include "mbedtls/platform.h" #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) #include "rm_psa_crypto.h" diff --git a/ra/fsp/src/rm_tfm_port/ra/boot_hal.c b/ra/fsp/src/rm_tfm_port/ra/boot_hal.c index fe02290ac..2a0fbdd8e 100644 --- a/ra/fsp/src/rm_tfm_port/ra/boot_hal.c +++ b/ra/fsp/src/rm_tfm_port/ra/boot_hal.c @@ -21,7 +21,7 @@ #include "psa/crypto.h" #if defined(MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT) - #include "platform.h" + #include "mbedtls/platform.h" #endif /* Flash device name must be specified by target */ diff --git a/ra/fsp/src/rm_tfm_port/ra/inc/rm_tfm_port.h b/ra/fsp/src/rm_tfm_port/ra/inc/rm_tfm_port.h index cc0149de1..d18560b2a 100644 --- a/ra/fsp/src/rm_tfm_port/ra/inc/rm_tfm_port.h +++ b/ra/fsp/src/rm_tfm_port/ra/inc/rm_tfm_port.h @@ -24,7 +24,7 @@ #include "tfm_common_config.h" #include "tfm_arch.h" #include "system_core_init.h" -#include "platform.h" +#include "mbedtls/platform.h" extern void tfm_main(void); diff --git a/ra/fsp/src/rm_touch/rm_touch.c b/ra/fsp/src/rm_touch/rm_touch.c index 0924ce0fb..e2901fba6 100644 --- a/ra/fsp/src/rm_touch/rm_touch.c +++ b/ra/fsp/src/rm_touch/rm_touch.c @@ -152,6 +152,11 @@ #define TOUCH_UART_CTSUSNUM_SHIFT (10) #define TOUCH_UART_CTSUSDPA_SHIFT (8) #endif + +/* UART Command version */ + #define TOUCH_UART_VERSION_MAJOR ((uint8_t) 0x01U) + #define TOUCH_UART_VERSION_MINOR ((uint8_t) 0x00U) + #endif /* Method Number Maximum */ @@ -200,6 +205,11 @@ static void touch_wheel_decode(touch_wheel_info_t * p_winfo, #endif +#if (TOUCH_CFG_PAD_ENABLE) +static void touch_pad_decode(touch_pad_info_t * p_pinfo, uint8_t num_x, uint8_t num_y, uint8_t max_touch); + +#endif + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) void touch_uart_callback(uart_callback_args_t * p_args); @@ -240,6 +250,8 @@ static uint8_t g_touch_pad_max_touch; static uint16_t g_touch_pad_drift_count; static int32_t g_touch_pad_drift_buf[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX]; static uint16_t g_touch_pad_base[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX]; +static uint16_t g_touch_pad_buf[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX * 2]; +static uint8_t g_touch_base_set_falg = 0; #endif #if TOUCH_CFG_MONITOR_ENABLE static volatile uint8_t g_touch_monitor_buf[TOUCH_MONITOR_BUFFER_SIZE]; @@ -266,11 +278,12 @@ uint8_t g_touch_uart_monitor_num; **********************************************************************************************************************/ const touch_api_t g_touch_on_ctsu = { - .open = RM_TOUCH_Open, - .scanStart = RM_TOUCH_ScanStart, - .dataGet = RM_TOUCH_DataGet, - .padDataGet = RM_TOUCH_PadDataGet, - .close = RM_TOUCH_Close, + .open = RM_TOUCH_Open, + .scanStart = RM_TOUCH_ScanStart, + .dataGet = RM_TOUCH_DataGet, + .padDataGet = RM_TOUCH_PadDataGet, + .callbackSet = RM_TOUCH_CallbackSet, + .close = RM_TOUCH_Close, }; /*******************************************************************************************************************//** @@ -599,13 +612,22 @@ fsp_err_t RM_TOUCH_DataGet (touch_ctrl_t * const p_ctrl, #if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) FSP_ASSERT(p_instance_ctrl); #if (TOUCH_CFG_NUM_BUTTONS != 0) - FSP_ASSERT(p_button_status); + if (0 != p_instance_ctrl->p_touch_cfg->num_buttons) + { + FSP_ASSERT(p_button_status); + } #endif #if (TOUCH_CFG_NUM_SLIDERS != 0) - FSP_ASSERT(p_slider_position); + if (0 != p_instance_ctrl->p_touch_cfg->num_sliders) + { + FSP_ASSERT(p_slider_position); + } #endif #if (TOUCH_CFG_NUM_WHEELS != 0) - FSP_ASSERT(p_wheel_position); + if (0 != p_instance_ctrl->p_touch_cfg->num_wheels) + { + FSP_ASSERT(p_wheel_position); + } #endif TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); #endif @@ -817,7 +839,7 @@ fsp_err_t RM_TOUCH_DataGet (touch_ctrl_t * const p_ctrl, g_touch_monitor_buf[3] = (uint8_t) (index >> 8); g_touch_uart_transmit_flag = 1; gp_touch_uart_instance->p_api->write(gp_touch_uart_instance->p_ctrl, - (uint8_t const * const) &g_touch_monitor_buf, + (uint8_t *) g_touch_monitor_buf, index); } #endif @@ -843,37 +865,17 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, fsp_err_t err = FSP_SUCCESS; touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; #if (TOUCH_CFG_PAD_ENABLE) - static uint16_t pad_buf[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX * 2]; - static uint8_t base_flag = 0; - uint16_t i; - uint16_t j; - uint8_t loop; - int32_t max_diff; - uint16_t max_x; - uint16_t max_y; - int32_t x_parameter; - int32_t y_parameter; - int16_t heat_map[TOUCH_MAP_X * TOUCH_MAP_Y]; - uint8_t use_map[TOUCH_MAP_X * TOUCH_MAP_Y]; - uint8_t num_x; - uint8_t num_y; - uint16_t pitch_x; - uint16_t pitch_y; - int16_t tmp_count; - uint16_t tmp_value; - int32_t tmp_diff; - int32_t tmp_heat; - int32_t tmp_x1; /* Work for calculating x parameter1 */ - int32_t tmp_x2; /* Work for calculating x parameter2 */ - int32_t tmp_x3; /* Work for calculating x parameter3 */ - int32_t tmp_x4; /* Work for calculating x parameter4 */ - int32_t tmp_y1; /* Work for calculating y parameter1 */ - int32_t tmp_y2; /* Work for calculating y parameter2 */ - int32_t tmp_y3; /* Work for calculating y parameter3 */ - int32_t tmp_y4; /* Work for calculating y parameter4 */ - uint16_t element_num; - int32_t drift_diff; - uint8_t max_touch; /* number of max touch */ + uint16_t i; + uint16_t j; + uint8_t loop; + uint8_t num_x; + uint8_t num_y; + int16_t tmp_count; + uint16_t tmp_value; + int32_t tmp_diff; + uint16_t element_num; + int32_t drift_diff; + uint8_t max_touch; /* number of max touch */ #if (TOUCH_CFG_MONITOR_ENABLE) uint16_t index = 0; #endif @@ -886,19 +888,20 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); #endif - /* initialize touch number */ - *(p_instance_ctrl->pinfo.p_num_touch) = 0; + /* initialize touch number and corrdinate */ + *(p_instance_ctrl->pinfo.p_num_touch) = 0; + *(p_instance_ctrl->pinfo.p_rx_coordinate) = TOUCH_OFF_VALUE; + *(p_instance_ctrl->pinfo.p_tx_coordinate) = TOUCH_OFF_VALUE; /* Get local variable (TS number & data pinch) */ - num_x = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx; - num_y = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_tx; + num_x = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx; + TOUCH_ERROR_RETURN(0 != num_x, FSP_ERR_ASSERTION); + num_y = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_tx; + TOUCH_ERROR_RETURN(0 != num_y, FSP_ERR_ASSERTION); element_num = (uint16_t) (num_x * num_y); - pitch_x = (uint16_t) (*(p_instance_ctrl->pinfo.p_rx_pixel) / num_x); - pitch_y = (uint16_t) (*(p_instance_ctrl->pinfo.p_tx_pixel) / num_y); - /* Data get */ - err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, pad_buf); + err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, g_touch_pad_buf); FSP_ERROR_RETURN(FSP_ERR_CTSU_SCANNING != err, FSP_ERR_CTSU_SCANNING); /* check for max touch */ @@ -915,11 +918,11 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (i = 0; i < element_num; i++) { /* save to buffer in the first half */ - pad_buf[i] = (uint16_t) (pad_buf[(i * 2) + 1] - pad_buf[i * 2]); + g_touch_pad_buf[i] = (uint16_t) (g_touch_pad_buf[(i * 2) + 1] - g_touch_pad_buf[i * 2]); } /* Data get section */ - if (!base_flag) + if (!g_touch_base_set_falg) { /* format base value , changing the order */ for (j = 0; j < num_x; j++) @@ -927,12 +930,12 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (i = 0; i < num_y; i++) { *(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x)) = - (pad_buf[*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_rx + j) + - (*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_tx + i) * num_x)]); + (g_touch_pad_buf[*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_rx + j) + + (*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_tx + i) * num_x)]); } } - base_flag = 1; + g_touch_base_set_falg = 1; } else { @@ -942,14 +945,14 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (i = 0; i < num_y; i++) { /* get count data ,and changing the order */ - tmp_value = pad_buf[*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_rx + j) + - (*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_tx + i) * num_x)]; + tmp_value = g_touch_pad_buf[*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_rx + j) + + (*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_tx + i) * num_x)]; /* make difference from base value */ tmp_diff = *(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x)) - tmp_value; /* save difference value to buffer in the second half */ - pad_buf[element_num + j + (i * num_x)] = (uint16_t) tmp_diff; + g_touch_pad_buf[element_num + j + (i * num_x)] = (uint16_t) tmp_diff; } } } @@ -959,238 +962,13 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, /* copy from second half to first half for monitor */ for (i = 0; i < element_num; i++) { - pad_buf[i] = pad_buf[element_num + i]; + g_touch_pad_buf[i] = g_touch_pad_buf[element_num + i]; } #endif - *(p_instance_ctrl->pinfo.p_rx_coordinate) = TOUCH_OFF_VALUE; - *(p_instance_ctrl->pinfo.p_tx_coordinate) = TOUCH_OFF_VALUE; - - if (base_flag) + if (g_touch_base_set_falg) { - /* Get coordinate data for the Max touch */ - for (loop = 0; loop < max_touch; loop++) - { - /* initialize heat-map variables */ - max_diff = 0; - max_y = 0; - max_x = 0; - - /* Clear heat map ,and initial use map */ - for (i = 0; i < (TOUCH_MAP_X * TOUCH_MAP_Y); i++) - { - heat_map[i] = 0; - use_map[i] = 1; - } - - /* Maximum value search */ - for (j = 0; j < num_x; j++) - { - for (i = 0; i < num_y; i++) - { - if ((int16_t) pad_buf[element_num + j + (i * num_x)] > max_diff) - { - max_diff = (int16_t) pad_buf[element_num + j + (i * num_x)]; - max_y = i; - max_x = j; - } - } - } - - /* touch check */ - if (max_diff >= *(p_instance_ctrl->pinfo.p_threshold)) - { - /* make use map */ - if (0 == max_y) - { - /* If map position is Top. */ - use_map[0] = 0; - use_map[1] = 0; - use_map[2] = 0; - } - else if ((num_y - 1) == max_y) - { - /* If map position is Bottom. */ - use_map[6] = 0; - use_map[7] = 0; - use_map[8] = 0; - } - else - { - } - - if (0 == max_x) - { - /* If map position is Left. */ - use_map[0] = 0; - use_map[3] = 0; - use_map[6] = 0; - } - else if ((num_x - 1) == max_x) - { - /* If map position is Right. */ - use_map[2] = 0; - use_map[5] = 0; - use_map[8] = 0; - } - else - { - } - - /* make heat mapping */ - for (i = 0; i < TOUCH_MAP_X; i++) - { - for (j = 0; j < TOUCH_MAP_Y; j++) - { - if (use_map[j + (i * TOUCH_MAP_X)]) - { - heat_map[j + (i * TOUCH_MAP_X)] = - (int16_t) pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x]; - } - } - } - - /* get x value */ - /* Calculate right + bottom value. (x1/y1) */ - tmp_heat = heat_map[5] + heat_map[7]; - if (tmp_heat == 0) - { - /* When dividing by zero, set the calculation result to zero */ - tmp_x1 = 0; /* x parameter1 */ - tmp_y1 = 0; /* y parameter1 */ - } - else - { - tmp_x1 = (heat_map[8] * heat_map[5]) / tmp_heat; /* x parameter1 */ - tmp_y1 = (heat_map[8] * heat_map[7]) / tmp_heat; /* y parameter1 */ - } - - /* Calculate right + up value. (x2/y4) */ - tmp_heat = heat_map[5] + heat_map[1]; - if (tmp_heat == 0) - { - /* When dividing by zero, set the calculation result to zero */ - tmp_x2 = 0; /* x parameter2 */ - tmp_y4 = 0; /* y parameter4 */ - } - else - { - tmp_x2 = (heat_map[2] * heat_map[5]) / tmp_heat; /* x parameter2 */ - tmp_y4 = (heat_map[2] * heat_map[1]) / tmp_heat; /* y parameter4 */ - } - - /* Calculate left + up value. (x3/y3) */ - tmp_heat = heat_map[3] + heat_map[1]; - if (tmp_heat == 0) - { - /* When dividing by zero, set the calculation result to zero */ - tmp_x3 = 0; /* x parameter3 */ - tmp_y3 = 0; /* y parameter3 */ - } - else - { - tmp_x3 = (heat_map[0] * heat_map[3]) / tmp_heat; /* x parameter3 */ - tmp_y3 = (heat_map[0] * heat_map[1]) / tmp_heat; /* y parameter3 */ - } - - /* Calculate left + down value. (x4/y2) */ - tmp_heat = heat_map[3] + heat_map[7]; - if (tmp_heat == 0) - { - /* When dividing by zero, set the calculation result to zero */ - tmp_x4 = 0; /* x parameter4 */ - tmp_y2 = 0; /* y parameter2 */ - } - else - { - tmp_x4 = (heat_map[6] * heat_map[3]) / tmp_heat; /* x parameter4 */ - tmp_y2 = (heat_map[6] * heat_map[7]) / tmp_heat; /* y parameter2 */ - } - - if (heat_map[4] == 0) - { - x_parameter = 0; - } - else - { - /* x coordinate value calculation*/ - x_parameter = ((pitch_x / 2) * - (heat_map[5] + tmp_x1 + tmp_x2 - - heat_map[3] - tmp_x3 - tmp_x4) / - heat_map[4]); - } - - /* Fit to pitch */ - if (x_parameter > pitch_x / 2) - { - x_parameter = pitch_x / 2; - } - else if (x_parameter < -(pitch_x / 2)) - { - x_parameter = -(pitch_x / 2); - } - else - { - /* no operation */ - } - - /* Coordinate x value based on pitch */ - *(p_instance_ctrl->pinfo.p_rx_coordinate + loop) = - (uint16_t) ((pitch_x * (max_x + 1) - pitch_x / 2) + x_parameter); - - /* get y value */ - if (heat_map[4] == 0) - { - y_parameter = 0; - } - else - { - /* y coordinate value calculation*/ - y_parameter = ((pitch_y / 2) * - (heat_map[7] + tmp_y1 + tmp_y2 - - heat_map[1] - tmp_y3 - tmp_y4) / - heat_map[4]); - } - - /* Fit to pitch */ - if (y_parameter > pitch_y / 2) - { - y_parameter = pitch_y / 2; - } - else if (y_parameter < -(pitch_y / 2)) - { - y_parameter = -(pitch_y / 2); - } - else - { - /* no operation */ - } - - /* Coordinate y value based on pitch */ - *(p_instance_ctrl->pinfo.p_tx_coordinate + loop) = - (uint16_t) ((pitch_y * (max_y + 1) - pitch_y / 2) + y_parameter); - - /* If touching ,then counter increment */ - (*(p_instance_ctrl->pinfo.p_num_touch))++; - - /* Clear the processed heat map area */ - for (i = 0; i < TOUCH_MAP_X; i++) - { - for (j = 0; j < TOUCH_MAP_Y; j++) - { - if (use_map[i * TOUCH_MAP_X + j]) - { - pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x] = 0; - } - } - } - } - else - { - *(p_instance_ctrl->pinfo.p_rx_coordinate + loop) = TOUCH_OFF_VALUE; - *(p_instance_ctrl->pinfo.p_tx_coordinate + loop) = TOUCH_OFF_VALUE; - } - } + touch_pad_decode(&p_instance_ctrl->pinfo, num_x, num_y, max_touch); /* the drift correction process. */ if (0 < p_instance_ctrl->pinfo.num_drift) @@ -1205,7 +983,7 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (j = 0; j < num_x; j++) { /* It is an addition for the drift correction average calculation */ - tmp_count = (int16_t) pad_buf[element_num + j + (i * num_x)]; + tmp_count = (int16_t) g_touch_pad_buf[element_num + j + (i * num_x)]; *(p_instance_ctrl->pinfo.p_drift_buf + (j + (i * num_x))) += (int32_t) tmp_count; @@ -1284,7 +1062,7 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (i = 0; i < element_num; i++) { - tmp_value = pad_buf[i]; + tmp_value = g_touch_pad_buf[i]; if (tmp_value & TOUCH_PAD_TEMP_VALUE_OVERFLOW_BIT) { tmp_value = 0; @@ -1336,7 +1114,7 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, g_touch_monitor_buf[3] = (uint8_t) (index >> 8); g_touch_uart_transmit_flag = 1; gp_touch_uart_instance->p_api->write(gp_touch_uart_instance->p_ctrl, - (uint8_t const * const) &g_touch_monitor_buf, + (uint8_t *) g_touch_monitor_buf, index); } #endif @@ -1870,6 +1648,273 @@ void touch_wheel_decode (touch_wheel_info_t * p_winfo, uint16_t * wheel_data, ui #endif +#if (TOUCH_CFG_PAD_ENABLE) + +/*********************************************************************************************************************** + * Function Name: touch_pad_decode + * Description : Pad Decode function + * Arguments : touch_pad_info_t p_pinfo : Pointer to Pad Information structure + * : uint16_t *wheel_data : Pointer to Wheel data array + * : uint8_t num_elements : Number of element on wheel + * : uint8_t wheel_id : Wheel ID + * Return Value : None + ***********************************************************************************************************************/ +void touch_pad_decode (touch_pad_info_t * p_pinfo, uint8_t num_x, uint8_t num_y, uint8_t max_touch) +{ + uint16_t i; + uint16_t j; + uint8_t loop; + uint16_t pitch_x; + uint16_t pitch_y; + uint16_t element_num; + int32_t max_diff; + uint16_t max_x; + uint16_t max_y; + int32_t x_parameter; + int32_t y_parameter; + int16_t heat_map[TOUCH_MAP_X * TOUCH_MAP_Y]; + uint8_t use_map[TOUCH_MAP_X * TOUCH_MAP_Y]; + int32_t tmp_heat; + int32_t tmp_x1; /* Work for calculating x parameter1 */ + int32_t tmp_x2; /* Work for calculating x parameter2 */ + int32_t tmp_x3; /* Work for calculating x parameter3 */ + int32_t tmp_x4; /* Work for calculating x parameter4 */ + int32_t tmp_y1; /* Work for calculating y parameter1 */ + int32_t tmp_y2; /* Work for calculating y parameter2 */ + int32_t tmp_y3; /* Work for calculating y parameter3 */ + int32_t tmp_y4; /* Work for calculating y parameter4 */ + + element_num = (uint16_t) (num_x * num_y); + pitch_x = (uint16_t) (*(p_pinfo->p_rx_pixel) / num_x); + pitch_y = (uint16_t) (*(p_pinfo->p_tx_pixel) / num_y); + + /* Get coordinate data for the Max touch */ + for (loop = 0; loop < max_touch; loop++) + { + /* initialize heat-map variables */ + max_diff = 0; + max_y = 0; + max_x = 0; + + /* Clear heat map ,and initial use map */ + for (i = 0; i < (TOUCH_MAP_X * TOUCH_MAP_Y); i++) + { + heat_map[i] = 0; + use_map[i] = 1; + } + + /* Maximum value search */ + for (j = 0; j < num_x; j++) + { + for (i = 0; i < num_y; i++) + { + if ((int16_t) g_touch_pad_buf[element_num + j + (i * num_x)] > max_diff) + { + max_diff = (int16_t) g_touch_pad_buf[element_num + j + (i * num_x)]; + max_y = i; + max_x = j; + } + } + } + + /* touch check */ + if (max_diff >= *(p_pinfo->p_threshold)) + { + /* make use map */ + if (0 == max_y) + { + /* If map position is Top. */ + use_map[0] = 0; + use_map[1] = 0; + use_map[2] = 0; + } + else if ((num_y - 1) == max_y) + { + /* If map position is Bottom. */ + use_map[6] = 0; + use_map[7] = 0; + use_map[8] = 0; + } + else + { + } + + if (0 == max_x) + { + /* If map position is Left. */ + use_map[0] = 0; + use_map[3] = 0; + use_map[6] = 0; + } + else if ((num_x - 1) == max_x) + { + /* If map position is Right. */ + use_map[2] = 0; + use_map[5] = 0; + use_map[8] = 0; + } + else + { + } + + /* make heat mapping */ + for (i = 0; i < TOUCH_MAP_X; i++) + { + for (j = 0; j < TOUCH_MAP_Y; j++) + { + if (use_map[j + (i * TOUCH_MAP_X)]) + { + heat_map[j + (i * TOUCH_MAP_X)] = + (int16_t) g_touch_pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x]; + } + } + } + + /* get x value */ + /* Calculate right + bottom value. (x1/y1) */ + tmp_heat = heat_map[5] + heat_map[7]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x1 = 0; /* x parameter1 */ + tmp_y1 = 0; /* y parameter1 */ + } + else + { + tmp_x1 = (heat_map[8] * heat_map[5]) / tmp_heat; /* x parameter1 */ + tmp_y1 = (heat_map[8] * heat_map[7]) / tmp_heat; /* y parameter1 */ + } + + /* Calculate right + up value. (x2/y4) */ + tmp_heat = heat_map[5] + heat_map[1]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x2 = 0; /* x parameter2 */ + tmp_y4 = 0; /* y parameter4 */ + } + else + { + tmp_x2 = (heat_map[2] * heat_map[5]) / tmp_heat; /* x parameter2 */ + tmp_y4 = (heat_map[2] * heat_map[1]) / tmp_heat; /* y parameter4 */ + } + + /* Calculate left + up value. (x3/y3) */ + tmp_heat = heat_map[3] + heat_map[1]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x3 = 0; /* x parameter3 */ + tmp_y3 = 0; /* y parameter3 */ + } + else + { + tmp_x3 = (heat_map[0] * heat_map[3]) / tmp_heat; /* x parameter3 */ + tmp_y3 = (heat_map[0] * heat_map[1]) / tmp_heat; /* y parameter3 */ + } + + /* Calculate left + down value. (x4/y2) */ + tmp_heat = heat_map[3] + heat_map[7]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x4 = 0; /* x parameter4 */ + tmp_y2 = 0; /* y parameter2 */ + } + else + { + tmp_x4 = (heat_map[6] * heat_map[3]) / tmp_heat; /* x parameter4 */ + tmp_y2 = (heat_map[6] * heat_map[7]) / tmp_heat; /* y parameter2 */ + } + + if (heat_map[4] == 0) + { + x_parameter = 0; + } + else + { + /* x coordinate value calculation*/ + x_parameter = ((pitch_x / 2) * + (heat_map[5] + tmp_x1 + tmp_x2 - + heat_map[3] - tmp_x3 - tmp_x4) / + heat_map[4]); + } + + /* Fit to pitch */ + if (x_parameter > pitch_x / 2) + { + x_parameter = pitch_x / 2; + } + else if (x_parameter < -(pitch_x / 2)) + { + x_parameter = -(pitch_x / 2); + } + else + { + /* no operation */ + } + + /* Coordinate x value based on pitch */ + *(p_pinfo->p_rx_coordinate + loop) = + (uint16_t) ((pitch_x * (max_x + 1) - pitch_x / 2) + x_parameter); + + /* get y value */ + if (heat_map[4] == 0) + { + y_parameter = 0; + } + else + { + /* y coordinate value calculation*/ + y_parameter = ((pitch_y / 2) * + (heat_map[7] + tmp_y1 + tmp_y2 - + heat_map[1] - tmp_y3 - tmp_y4) / + heat_map[4]); + } + + /* Fit to pitch */ + if (y_parameter > pitch_y / 2) + { + y_parameter = pitch_y / 2; + } + else if (y_parameter < -(pitch_y / 2)) + { + y_parameter = -(pitch_y / 2); + } + else + { + /* no operation */ + } + + /* Coordinate y value based on pitch */ + *(p_pinfo->p_tx_coordinate + loop) = + (uint16_t) ((pitch_y * (max_y + 1) - pitch_y / 2) + y_parameter); + + /* If touching ,then counter increment */ + (*(p_pinfo->p_num_touch))++; + + /* Clear the processed heat map area */ + for (i = 0; i < TOUCH_MAP_X; i++) + { + for (j = 0; j < TOUCH_MAP_Y; j++) + { + if (use_map[i * TOUCH_MAP_X + j]) + { + g_touch_pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x] = 0; + } + } + } + } + else + { + *(p_pinfo->p_rx_coordinate + loop) = TOUCH_OFF_VALUE; + *(p_pinfo->p_tx_coordinate + loop) = TOUCH_OFF_VALUE; + } + } +} + +#endif + #if TOUCH_CFG_MONITOR_ENABLE #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) @@ -2161,8 +2206,8 @@ void touch_uart_callback (uart_callback_args_t * p_args) } else if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_VERSION) { - g_touch_monitor_buf[index++] = g_touch_version.code_version_minor; - g_touch_monitor_buf[index++] = g_touch_version.code_version_major; + g_touch_monitor_buf[index++] = TOUCH_UART_VERSION_MINOR; + g_touch_monitor_buf[index++] = TOUCH_UART_VERSION_MAJOR; } else { @@ -2178,7 +2223,7 @@ void touch_uart_callback (uart_callback_args_t * p_args) /* Start transmission */ g_touch_uart_transmit_flag = 1; gp_touch_uart_instance->p_api->write(gp_touch_uart_instance->p_ctrl, - (uint8_t const * const) &g_touch_monitor_buf, + (uint8_t *) g_touch_monitor_buf, index); /* Restart reception */ diff --git a/ra/fsp/src/rm_usbx_port/rm_usbx_port.c b/ra/fsp/src/rm_usbx_port/rm_usbx_port.c index 083c40cc2..ec9012f85 100644 --- a/ra/fsp/src/rm_usbx_port/rm_usbx_port.c +++ b/ra/fsp/src/rm_usbx_port/rm_usbx_port.c @@ -1037,12 +1037,27 @@ static void usb_host_usbx_transfer_complete_cb (usb_utr_t * p_utr, uint16_t data (void) data2; uint8_t pipe; UX_TRANSFER * transfer_request; + uint16_t pipe_reg; pipe = (uint8_t) p_utr->keyword; transfer_request = g_p_usb_host_usbx_transfer_request[p_utr->ip][pipe]; tx_semaphore_put(&g_usb_host_usbx_sem[p_utr->ip][pipe]); - *g_p_usb_host_actural_length[p_utr->ip][pipe] = p_utr->read_req_len - p_utr->tranlen; + + hw_usb_write_pipesel(p_utr, pipe); + pipe_reg = hw_usb_read_pipecfg(p_utr); + + if (0 != (pipe_reg & USB_DIRFIELD)) + { + /* transmission */ + *g_p_usb_host_actural_length[p_utr->ip][pipe] = p_utr->read_req_len; + } + else + { + /* reception */ + *g_p_usb_host_actural_length[p_utr->ip][pipe] = p_utr->read_req_len - p_utr->tranlen; + } + _ux_utility_semaphore_put(&transfer_request->ux_transfer_request_semaphore); } /* End of function usb_hstd_transfer_complete_cb() */ diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_aes.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_aes.c deleted file mode 100644 index fed6e737c..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_aes.c +++ /dev/null @@ -1,2985 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** AES Encryption */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - - -/* Include necessary system files. */ - -#include "nx_crypto_aes.h" -#include "nx_crypto_xcbc_mac.h" - -#if !defined(NX_CRYPTO_LITTLE_ENDIAN) -/* - Encryption table for BIG ENDIAN architecture. - - Lookup table for computing the MixColumns() Transformation with SubBytes applied to input value S. - Each entry of this table represents the result of {02} dot S[x,c] | {01} dot S[x,c] | {01} dot S[x,c] | {03} dot S[x,c]. See Equation - 5.6 on page 18 AES specification(Pub 197) - - The most significant byte is the result of {02} dot S, and the least significant byte is the result - of {03} dot S[x,c]. - */ -static const ULONG aes_encryption_table[256] = -{ - 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, - 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, - 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, - 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, - 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, - 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, - 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, - 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, - 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, - 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, - 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, - 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, - 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, - 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, - 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, - 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, - 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, - 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, - 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, - 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, - 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, - 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, - 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, - 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, - 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, - 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, - 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, - 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, - 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, - 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, - 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, - 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a -}; - - -/* - Decryption table for LITTLE ENDIAN architecture. - - Lookup table for computing the InvMixColumns() Transformation with InvSubBytes applied to input - value S. Each entry of this table represents - the result of {0e} dot S[x,c] | {09} dot S[x,c] | {0d} dot S[x,c] | {0b} dot S[x,c]. See Equation - 5.10 on page 23 AES specification(Pub 197) - - The most significant byte is the result of {0e} dot S, and the least significant byte is the result - of {0b} dot S[x,c]. - */ -static const ULONG aes_decryption_table[256] = -{ - 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, - 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, - 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, - 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, - 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, - 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, - 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, - 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, - 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, - 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, - 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, - 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, - 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, - 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, - 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, - 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, - 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, - 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, - 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, - 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, - 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, - 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, - 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, - 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, - 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, - 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, - 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, - 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, - 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, - 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, - 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, - 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742 -}; - -/* - Inverse Mix Table for LITTLE ENDIAN architecture. - Lookup table for computing the InvMixColumns() Transformation. Each entry of this table represents - the result of {0e} dot S[x,c] | {09} dot S[x,c] | {0d} dot S[x,c] | {0b} dot S[x,c]. See Equation - 5.10 on page 23 AES specification(Pub 197) - - The most significant byte is the result of {0e} dot S, and the least significant byte is the result - of {0b} dot S[x,c]. - */ -static const ULONG aes_inv_mix_table[256] = -{ - 0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927, 0x24362e3a, 0x2a3f2331, - 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45, 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, - 0xe090d0b0, 0xee99ddbb, 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381, - 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf, 0xb4ee96d2, 0xbae79bd9, - 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66, 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, - 0xab73d323, 0xa57ade28, 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012, - 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec, 0x1f9d45f1, 0x119448fa, - 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e, 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, - 0xad766df6, 0xa37f60fd, 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7, - 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89, 0xf9082b94, 0xf701269f, - 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b, 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, - 0x3daed51e, 0x33a7d815, 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f, - 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa, 0x527bf8b7, 0x5c72f5bc, - 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8, 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, - 0x96dd063d, 0x98d40b36, 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c, - 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742, 0xc2a3405f, 0xccaa4d54, - 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea, 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, - 0x31a4b2af, 0x3fadbfa4, 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e, - 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360, 0x854a247d, 0x8b432976, - 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502, 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, - 0x9ad7618c, 0x94de6c87, 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd, - 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3, 0xcea927ee, 0xc0a02ae5, - 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621, 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, - 0x0a0fd964, 0x0406d46f, 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55, - 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26, 0xc8ac993b, 0xc6a59430, - 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844, 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, - 0x0c0a67b1, 0x02036aba, 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480, - 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce, 0x587421d3, 0x567d2cd8, - 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67, 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, - 0x47e96422, 0x49e06929, 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713, - 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed, 0xf307f2f0, 0xfd0efffb, - 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f, 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3 -}; - -#else - -/* - Encryption table for LITTLE ENDIAN architecture. - - Lookup table for computing the MixColumns() Transformation with SubBytes applied to input value S. - Each entry of this table represents the result of {02} dot S[x,c] | {01} dot S[x,c] | {01} dot S[x,c] | {03} dot S[x,c]. See Equation - 5.6 on page 18 AES specification(Pub 197) - - The most significant byte is the result of {02} dot S, and the least significant byte is the result - of {03} dot S[x,c]. - */ - -static const ULONG aes_encryption_table[256] = -{ - 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591, - 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56, 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec, - 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa, 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb, - 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b, - 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c, 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83, - 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9, 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a, - 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f, - 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, - 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b, - 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d, 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413, - 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1, 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6, - 0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972, 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85, - 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed, 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511, - 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe, 0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b, - 0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05, 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1, - 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142, 0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf, - 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3, 0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e, - 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a, 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6, - 0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3, 0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b, - 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428, 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad, - 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14, 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8, - 0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4, 0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2, - 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda, 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949, - 0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf, 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810, - 0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c, 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697, - 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e, 0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f, - 0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc, 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c, - 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969, 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27, - 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122, 0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433, - 0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9, 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5, - 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a, 0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0, - 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e, 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c -}; - -/* - Decryption table for LITTLE ENDIAN architecture. - - Lookup table for computing the InvMixColumns() Transformation with InvSubBytes applied to input - value S. Each entry of this table represents - the result of {0e} dot S[x,c] | {09} dot S[x,c] | {0d} dot S[x,c] | {0b} dot S[x,c]. See Equation - 5.10 on page 23 AES specification(Pub 197) - - The most significant byte is the result of {0e} dot S, and the least significant byte is the result - of {0b} dot S[x,c]. - */ -static const ULONG aes_decryption_table[256] = -{ - 0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a, 0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b, - 0x55fa3020, 0xf66d76ad, 0x9176cc88, 0x254c02f5, 0xfcd7e54f, 0xd7cb2ac5, 0x80443526, 0x8fa362b5, - 0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d, 0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b, - 0xe75f8f03, 0x959c9215, 0xeb7a6dbf, 0xda595295, 0x2d83bed4, 0xd3217458, 0x2969e049, 0x44c8c98e, - 0x6a89c275, 0x78798ef4, 0x6b3e5899, 0xdd71b927, 0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d, - 0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362, 0xe07764b1, 0x84ae6bbb, 0x1ca081fe, 0x942b08f9, - 0x58684870, 0x19fd458f, 0x876cde94, 0xb7f87b52, 0x23d373ab, 0xe2024b72, 0x578f1fe3, 0x2aab5566, - 0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3, 0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed, - 0x2b1ccf8a, 0x92b479a7, 0xf0f207f3, 0xa1e2694e, 0xcdf4da65, 0xd5be0506, 0x1f6234d1, 0x8afea6c4, - 0x9d532e34, 0xa055f3a2, 0x32e18a05, 0x75ebf6a4, 0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd, - 0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d, 0xb58d5491, 0x055dc471, 0x6fd40604, 0xff155060, - 0x24fb9819, 0x97e9bdd6, 0xcc434089, 0x779ed967, 0xbd42e8b0, 0x888b8907, 0x385b19e7, 0xdbeec879, - 0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000, 0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c, - 0xfbff0efd, 0x5638850f, 0x1ed5ae3d, 0x27392d36, 0x64d90f0a, 0x21a65c68, 0xd1545b9b, 0x3a2e3624, - 0xb1670a0c, 0x0fe75793, 0xd296eeb4, 0x9e919b1b, 0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c, - 0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12, 0x0b0d090e, 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14, - 0x8519f157, 0x4c0775af, 0xbbdd99ee, 0xfd607fa3, 0x9f2601f7, 0xbcf5725c, 0xc53b6644, 0x347efb5b, - 0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8, 0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684, - 0x7d244a85, 0xf83dbbd2, 0x1132f9ae, 0x6da129c7, 0x4b2f9e1d, 0xf330b2dc, 0xec52860d, 0xd0e3c177, - 0x6c16b32b, 0x99b970a9, 0xfa489411, 0x2264e947, 0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322, - 0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498, 0xcf81f5a6, 0x28de7aa5, 0x268eb7da, 0xa4bfad3f, - 0xe49d3a2c, 0x0d927850, 0x9bcc5f6a, 0x62467e54, 0xc2138df6, 0xe8b8d890, 0x5ef7392e, 0xf5afc382, - 0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf, 0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb, - 0x097826cd, 0xf418596e, 0x01b79aec, 0xa89a4f83, 0x656e95e6, 0x7ee6ffaa, 0x08cfbc21, 0xe6e815ef, - 0xd99be7ba, 0xce366f4a, 0xd4099fea, 0xd67cb029, 0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235, - 0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733, 0x4a9804f1, 0xf7daec41, 0x0e50cd7f, 0x2ff69117, - 0x8dd64d76, 0x4db0ef43, 0x544daacc, 0xdf0496e4, 0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, 0x7f516546, - 0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb, 0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d, - 0x8c61d79a, 0x7a0ca137, 0x8e14f859, 0x893c13eb, 0xee27a9ce, 0x35c961b7, 0xede51ce1, 0x3cb1477a, - 0x59dfd29c, 0x3f73f255, 0x79ce1418, 0xbf37c773, 0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478, - 0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2, 0x72c31d16, 0x0c25e2bc, 0x8b493c28, 0x41950dff, - 0x7101a839, 0xdeb30c08, 0x9ce4b4d8, 0x90c15664, 0x6184cb7b, 0x70b632d5, 0x745c6c48, 0x4257b8d0 -}; - -/* - Inverse Mix Table for LITTLE ENDIAN architecture. - Lookup table for computing the InvMixColumns() Transformation. Each entry of this table represents - the result of {0e} dot S[x,c] | {09} dot S[x,c] | {0d} dot S[x,c] | {0b} dot S[x,c]. See Equation - 5.10 on page 23 AES specification(Pub 197) - - The most significant byte is the result of {0e} dot S, and the least significant byte is the result - of {0b} dot S[x,c]. - */ - -static const ULONG aes_inv_mix_table[256] = -{ - 0x00000000, 0x0b0d090e, 0x161a121c, 0x1d171b12, 0x2c342438, 0x27392d36, 0x3a2e3624, 0x31233f2a, - 0x58684870, 0x5365417e, 0x4e725a6c, 0x457f5362, 0x745c6c48, 0x7f516546, 0x62467e54, 0x694b775a, - 0xb0d090e0, 0xbbdd99ee, 0xa6ca82fc, 0xadc78bf2, 0x9ce4b4d8, 0x97e9bdd6, 0x8afea6c4, 0x81f3afca, - 0xe8b8d890, 0xe3b5d19e, 0xfea2ca8c, 0xf5afc382, 0xc48cfca8, 0xcf81f5a6, 0xd296eeb4, 0xd99be7ba, - 0x7bbb3bdb, 0x70b632d5, 0x6da129c7, 0x66ac20c9, 0x578f1fe3, 0x5c8216ed, 0x41950dff, 0x4a9804f1, - 0x23d373ab, 0x28de7aa5, 0x35c961b7, 0x3ec468b9, 0x0fe75793, 0x04ea5e9d, 0x19fd458f, 0x12f04c81, - 0xcb6bab3b, 0xc066a235, 0xdd71b927, 0xd67cb029, 0xe75f8f03, 0xec52860d, 0xf1459d1f, 0xfa489411, - 0x9303e34b, 0x980eea45, 0x8519f157, 0x8e14f859, 0xbf37c773, 0xb43ace7d, 0xa92dd56f, 0xa220dc61, - 0xf66d76ad, 0xfd607fa3, 0xe07764b1, 0xeb7a6dbf, 0xda595295, 0xd1545b9b, 0xcc434089, 0xc74e4987, - 0xae053edd, 0xa50837d3, 0xb81f2cc1, 0xb31225cf, 0x82311ae5, 0x893c13eb, 0x942b08f9, 0x9f2601f7, - 0x46bde64d, 0x4db0ef43, 0x50a7f451, 0x5baafd5f, 0x6a89c275, 0x6184cb7b, 0x7c93d069, 0x779ed967, - 0x1ed5ae3d, 0x15d8a733, 0x08cfbc21, 0x03c2b52f, 0x32e18a05, 0x39ec830b, 0x24fb9819, 0x2ff69117, - 0x8dd64d76, 0x86db4478, 0x9bcc5f6a, 0x90c15664, 0xa1e2694e, 0xaaef6040, 0xb7f87b52, 0xbcf5725c, - 0xd5be0506, 0xdeb30c08, 0xc3a4171a, 0xc8a91e14, 0xf98a213e, 0xf2872830, 0xef903322, 0xe49d3a2c, - 0x3d06dd96, 0x360bd498, 0x2b1ccf8a, 0x2011c684, 0x1132f9ae, 0x1a3ff0a0, 0x0728ebb2, 0x0c25e2bc, - 0x656e95e6, 0x6e639ce8, 0x737487fa, 0x78798ef4, 0x495ab1de, 0x4257b8d0, 0x5f40a3c2, 0x544daacc, - 0xf7daec41, 0xfcd7e54f, 0xe1c0fe5d, 0xeacdf753, 0xdbeec879, 0xd0e3c177, 0xcdf4da65, 0xc6f9d36b, - 0xafb2a431, 0xa4bfad3f, 0xb9a8b62d, 0xb2a5bf23, 0x83868009, 0x888b8907, 0x959c9215, 0x9e919b1b, - 0x470a7ca1, 0x4c0775af, 0x51106ebd, 0x5a1d67b3, 0x6b3e5899, 0x60335197, 0x7d244a85, 0x7629438b, - 0x1f6234d1, 0x146f3ddf, 0x097826cd, 0x02752fc3, 0x335610e9, 0x385b19e7, 0x254c02f5, 0x2e410bfb, - 0x8c61d79a, 0x876cde94, 0x9a7bc586, 0x9176cc88, 0xa055f3a2, 0xab58faac, 0xb64fe1be, 0xbd42e8b0, - 0xd4099fea, 0xdf0496e4, 0xc2138df6, 0xc91e84f8, 0xf83dbbd2, 0xf330b2dc, 0xee27a9ce, 0xe52aa0c0, - 0x3cb1477a, 0x37bc4e74, 0x2aab5566, 0x21a65c68, 0x10856342, 0x1b886a4c, 0x069f715e, 0x0d927850, - 0x64d90f0a, 0x6fd40604, 0x72c31d16, 0x79ce1418, 0x48ed2b32, 0x43e0223c, 0x5ef7392e, 0x55fa3020, - 0x01b79aec, 0x0aba93e2, 0x17ad88f0, 0x1ca081fe, 0x2d83bed4, 0x268eb7da, 0x3b99acc8, 0x3094a5c6, - 0x59dfd29c, 0x52d2db92, 0x4fc5c080, 0x44c8c98e, 0x75ebf6a4, 0x7ee6ffaa, 0x63f1e4b8, 0x68fcedb6, - 0xb1670a0c, 0xba6a0302, 0xa77d1810, 0xac70111e, 0x9d532e34, 0x965e273a, 0x8b493c28, 0x80443526, - 0xe90f427c, 0xe2024b72, 0xff155060, 0xf418596e, 0xc53b6644, 0xce366f4a, 0xd3217458, 0xd82c7d56, - 0x7a0ca137, 0x7101a839, 0x6c16b32b, 0x671bba25, 0x5638850f, 0x5d358c01, 0x40229713, 0x4b2f9e1d, - 0x2264e947, 0x2969e049, 0x347efb5b, 0x3f73f255, 0x0e50cd7f, 0x055dc471, 0x184adf63, 0x1347d66d, - 0xcadc31d7, 0xc1d138d9, 0xdcc623cb, 0xd7cb2ac5, 0xe6e815ef, 0xede51ce1, 0xf0f207f3, 0xfbff0efd, - 0x92b479a7, 0x99b970a9, 0x84ae6bbb, 0x8fa362b5, 0xbe805d9f, 0xb58d5491, 0xa89a4f83, 0xa397468d -}; - - -#endif - -/* S-Box. Refer to figure 7 on page 16, AES specification(Pub 197) */ -static const UCHAR sub_bytes_sbox[] = -{ - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 -}; - -/* Inverse S-Box. Refer to figure 14 on page 22, AES specification(Pub 197) */ -static const UCHAR inverse_sub_bytes_sbox[] = -{ - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d -}; - - -/* Rcon array, used for key expansion. Refer to Appendix A on page 27, AES specification(Pub 197) */ -static const UCHAR aes_rcon_array[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36}; - -/* This array is generated using the nx_crypto_aes_multiply function, as powers of x in the - AES polynomial field. The array is shifted such that the indexes line up so that x**1 is - at index 1. The powers of x are cyclical, so the element at index 0 is actually x**255. */ -/* For Little endian */ -#ifdef NX_CRYPTO_LITTLE_ENDIAN -#define LEFT_ROTATE8(val) (((val) >> 8) | ((val) << 24)) -#define LEFT_ROTATE16(val) (((val) >> 16) | ((val) << 16)) -#define LEFT_ROTATE24(val) (((val) >> 24) | ((val) << 8)) -#define SET_MSB_BYTE(val) ((UINT)(val)) -#define SET_2ND_BYTE(val) ((UINT)(val) << 8) -#define SET_3RD_BYTE(val) ((UINT)(val) << 16) -#define SET_LSB_BYTE(val) ((UINT)(val) << 24) -#define EXTRACT_MSB_BYTE(val) ((val) & 0xFF) -#define EXTRACT_2ND_BYTE(val) (((val) >> 8) & 0xFF) -#define EXTRACT_3RD_BYTE(val) (((val) >> 16) & 0xFF) -#define EXTRACT_LSB_BYTE(val) ((val) >> 24) - -#define SWAP_ENDIAN(val) ((((val) & 0xFF000000) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24)) - - - -/* End of for little endian */ -/* For Big Endian */ -#else -#define LEFT_ROTATE8(val) (((val) << 8) | ((val) >> 24)) -#define LEFT_ROTATE16(val) (((val) >> 16) | ((val) << 16)) -#define LEFT_ROTATE24(val) (((val) << 24) | ((val) >> 8)) - -#define SET_MSB_BYTE(val) ((val) << 24) -#define SET_2ND_BYTE(val) ((val) << 16) -#define SET_3RD_BYTE(val) ((val) << 8) -#define SET_LSB_BYTE(val) ((UINT)(val)) -#define EXTRACT_MSB_BYTE(val) ((val) >> 24) -#define EXTRACT_2ND_BYTE(val) (((val) >> 16) & 0xFF) -#define EXTRACT_3RD_BYTE(val) (((val) >> 8) & 0xFF) -#define EXTRACT_LSB_BYTE(val) ((val) & 0xFF) - -#endif - -#ifdef NX_CRYPTO_FIPS -extern UINT _nx_crypto_library_state; -#endif /* NX_CRYPTO_FIPS */ - -/**************************************************************************/ -/* Utility routines */ -/**************************************************************************/ - - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_add_round_key PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function implements AddRoundKey() function described in */ -/* section 5.1.4 of the AES specification(Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* round_key Pointer to key schedule */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_encrypt Perform AES mode encryption */ -/* _nx_crypto_aes_decrypt Perform AES mode decryption */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), updated */ -/* constants and conditionals, */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_add_round_key(NX_CRYPTO_AES *aes_ptr, UINT *round_key) -{ -UINT i; - - for (i = 0; i < 4; ++i) - { - aes_ptr -> nx_crypto_aes_state[i] ^= round_key[i]; - } -} - - -/**************************************************************************/ -/* Encryption routines */ -/**************************************************************************/ -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_sub_shift_roundkey PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function combines the following operations: */ -/* SubBytes(state) */ -/* Shiftrows(state) */ -/* AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1]) */ -/* Refer to Figure 5 on page 15 of the AES Specification (Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* round_key Pointer to round key */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_encrypt Perform AES mode encryption */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_sub_shift_roundkey(NX_CRYPTO_AES *aes_ptr, UINT *round_key) -{ -/* - S00 S04 S08 S12 S00 S04 S08 S12 - - S01 S05 S09 S13 S05 S09 S13 S01 - ---> - S02 S06 S10 S14 S10 S14 S02 S06 - - S03 S07 S11 S15 S15 S03 S07 S11 - */ - - -UINT S0, S1, S2, S3; -UCHAR *S; - - S = (UCHAR *)aes_ptr -> nx_crypto_aes_state; - - S0 = ((SET_MSB_BYTE(sub_bytes_sbox[S[0]])) | - (SET_2ND_BYTE(sub_bytes_sbox[S[5]])) | - (SET_3RD_BYTE(sub_bytes_sbox[S[10]])) | - (SET_LSB_BYTE(sub_bytes_sbox[S[15]]))) ^ round_key[0]; - - S1 = ((SET_MSB_BYTE(sub_bytes_sbox[S[4]])) | - (SET_2ND_BYTE(sub_bytes_sbox[S[9]])) | - (SET_3RD_BYTE(sub_bytes_sbox[S[14]])) | - (SET_LSB_BYTE(sub_bytes_sbox[S[03]]))) ^ round_key[1]; - - S2 = ((SET_MSB_BYTE(sub_bytes_sbox[S[8]])) | - (SET_2ND_BYTE(sub_bytes_sbox[S[13]])) | - (SET_3RD_BYTE(sub_bytes_sbox[S[02]])) | - (SET_LSB_BYTE(sub_bytes_sbox[S[07]]))) ^ round_key[2]; - - S3 = ((SET_MSB_BYTE(sub_bytes_sbox[S[12]])) | - (SET_2ND_BYTE(sub_bytes_sbox[S[1]])) | - (SET_3RD_BYTE(sub_bytes_sbox[S[06]])) | - (SET_LSB_BYTE(sub_bytes_sbox[S[11]]))) ^ round_key[3]; - - aes_ptr -> nx_crypto_aes_state[0] = S0; - aes_ptr -> nx_crypto_aes_state[1] = S1; - aes_ptr -> nx_crypto_aes_state[2] = S2; - aes_ptr -> nx_crypto_aes_state[3] = S3; - -#ifdef NX_SECURE_KEY_CLEAR - S0 = 0; S1 = 0; S2 = 0; S3 = 0; -#endif /* NX_SECURE_KEY_CLEAR */ -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_inv_sub_shift_roundkey PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function combines the following operations in EqInvCipher: */ -/* InvSubBytes(state) */ -/* InvShiftrows(state) */ -/* AddRoundKey(state, dw[0, Nb-1]) */ -/* Refer to Figure 15 on page 25 of the AES Specification (Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* round_key Pointer to round key */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_decrypt Perform AES mode decryption */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_inv_sub_shift_roundkey(NX_CRYPTO_AES *aes_ptr, UINT *round_key) -{ -/* - S00 S04 S08 S12 S00 S04 S08 S12 - - S01 S05 S09 S13 S13 S01 S05 S09 - ---> - S02 S06 S10 S14 S10 S14 S02 S06 - - S03 S07 S11 S15 S07 S11 S15 S03 - */ - - -UINT S0, S1, S2, S3; -UCHAR *S; - - S = (UCHAR *)aes_ptr -> nx_crypto_aes_state; - - S0 = ((SET_MSB_BYTE(inverse_sub_bytes_sbox[S[0]])) | - (SET_2ND_BYTE(inverse_sub_bytes_sbox[S[13]])) | - (SET_3RD_BYTE(inverse_sub_bytes_sbox[S[10]])) | - (SET_LSB_BYTE(inverse_sub_bytes_sbox[S[7]]))) ^ round_key[0]; - - S1 = ((SET_MSB_BYTE(inverse_sub_bytes_sbox[S[4]])) | - (SET_2ND_BYTE(inverse_sub_bytes_sbox[S[1]])) | - (SET_3RD_BYTE(inverse_sub_bytes_sbox[S[14]])) | - (SET_LSB_BYTE(inverse_sub_bytes_sbox[S[11]]))) ^ round_key[1]; - - S2 = ((SET_MSB_BYTE(inverse_sub_bytes_sbox[S[8]])) | - (SET_2ND_BYTE(inverse_sub_bytes_sbox[S[5]])) | - (SET_3RD_BYTE(inverse_sub_bytes_sbox[S[2]])) | - (SET_LSB_BYTE(inverse_sub_bytes_sbox[S[15]]))) ^ round_key[2]; - - S3 = ((SET_MSB_BYTE(inverse_sub_bytes_sbox[S[12]])) | - (SET_2ND_BYTE(inverse_sub_bytes_sbox[S[9]])) | - (SET_3RD_BYTE(inverse_sub_bytes_sbox[S[6]])) | - (SET_LSB_BYTE(inverse_sub_bytes_sbox[S[3]]))) ^ round_key[3]; - - - aes_ptr -> nx_crypto_aes_state[0] = S0; - aes_ptr -> nx_crypto_aes_state[1] = S1; - aes_ptr -> nx_crypto_aes_state[2] = S2; - aes_ptr -> nx_crypto_aes_state[3] = S3; - -#ifdef NX_SECURE_KEY_CLEAR - S0= 0; S1 = 0; S2 = 0; S3 = 0; -#endif /* NX_SECURE_KEY_CLEAR */ -} - - -/* - - __ __ __ __ __ __ - | S0'| | 02 03 01 01| | S0 | - | S1'| | 01 02 03 01| | S1 | - | S2'| = | 01 01 02 03| | S2 | - | S3'| | 03 01 01 02| | S3 | - -- -- -- -- -- -- - - S' = V0 ^ V1 ^ V2 ^ V3 - S0' = {02}*S0 ^ {03}*S1 ^ {01}*S2 ^ {01}*S3 - S1' = {01}*S0 ^ {02}*S1 ^ {03}*S2 ^ {01}*S3 - S2' = {01}*S0 ^ {01}*S1 ^ {02}*S2 ^ {03}*S3 - S3' = {03}*S0 ^ {01}*S1 ^ {01}*S2 ^ {02}*S3 - - - */ - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_encryption_round PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function combines the following operations: */ -/* SubBytes(state) */ -/* Shiftrows(state) */ -/* MixColumns(state) */ -/* AddRoundKey(state, w[round*Nb, (round+1)*Nb-1]) */ -/* This step is the body of the loop in the middle of Cipher. */ -/* Refer to Figure 5 on page 15 of the AES Specification (Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* num_rounds Indicates total number of */ -/* rounds for this operation. */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_encrypt Perform AES mode encryption */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_encryption_round(NX_CRYPTO_AES *aes_ptr, int num_rounds) -{ -UINT state_data[4]; -UINT saved_state[4]; - -int round; - - - state_data[0] = aes_ptr -> nx_crypto_aes_state[0]; - state_data[1] = aes_ptr -> nx_crypto_aes_state[1]; - state_data[2] = aes_ptr -> nx_crypto_aes_state[2]; - state_data[3] = aes_ptr -> nx_crypto_aes_state[3]; - - for (round = 1; round < num_rounds; round++) - { - /* make a copy of the state data. */ - - saved_state[0] = state_data[0]; - saved_state[1] = state_data[1]; - saved_state[2] = state_data[2]; - saved_state[3] = state_data[3]; - - /* - The MixColumns operates on the state after rotation. The table below - illustrates the bytes before and after the ShiftRows operation. - This routine picks up bytes from its position BEFORE the rotation, - effectively combining SubBytes, ShiftRows, MixColumns, AddRoundKey - into one routine. - - S00 S04 S08 S12 S00 S04 S08 S12 - - S01 S05 S09 S13 S05 S09 S13 S01 - ---> - S02 S06 S10 S14 S10 S14 S02 S06 - - S03 S07 S11 S15 S15 S03 S07 S11 - - The following code segment fully spells out the AES encryption, step by step. - - - - S = bytes[ 0]; - V0 = aes_encryption_table[S]; - S = bytes[ 5]; - val = aes_encryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[10]; - val = aes_encryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[15]; - val = aes_encryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[0] = V0 ^ V1 ^ V2 ^ V3 ^ (aes_ptr -> nx_crypto_aes_key_schedule[round * 4]); - - S = bytes[ 4]; - V0 = aes_encryption_table[S]; - S = bytes[ 9]; - val = aes_encryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[14]; - val = aes_encryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[ 3]; - val = aes_encryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[1] = V0 ^ V1 ^ V2 ^ V3 ^ (aes_ptr -> nx_crypto_aes_key_schedule[round * 4 + 1]); - - - S = bytes[ 8]; - V0 = aes_encryption_table[S]; - S = bytes[13]; - val = aes_encryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[ 2]; - val = aes_encryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[ 7]; - val = aes_encryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[2] = V0 ^ V1 ^ V2 ^ V3 ^ (aes_ptr -> nx_crypto_aes_key_schedule[round * 4 + 2]); - - S = bytes[12]; - V0 = aes_encryption_table[S]; - S = bytes[ 1]; - val = aes_encryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[ 6]; - val = aes_encryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[11]; - val = aes_encryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[3] = V0 ^ V1 ^ V2 ^ V3 ^ (aes_ptr -> nx_crypto_aes_key_schedule[round * 4 + 3]); - - */ - - /* The following logic implements the steps above but could allow compiler to produce more - efficient code. */ - - state_data[0] = aes_encryption_table[EXTRACT_MSB_BYTE(saved_state[0])] ^ - (LEFT_ROTATE24(aes_encryption_table[EXTRACT_2ND_BYTE(saved_state[1])])) ^ - (LEFT_ROTATE16(aes_encryption_table[EXTRACT_3RD_BYTE(saved_state[2])])) ^ - (LEFT_ROTATE8(aes_encryption_table[EXTRACT_LSB_BYTE(saved_state[3])])) ^ - (aes_ptr -> nx_crypto_aes_key_schedule[round * 4]); - - state_data[1] = aes_encryption_table[EXTRACT_MSB_BYTE(saved_state[1])] ^ - (LEFT_ROTATE24(aes_encryption_table[EXTRACT_2ND_BYTE(saved_state[2])])) ^ - (LEFT_ROTATE16(aes_encryption_table[EXTRACT_3RD_BYTE(saved_state[3])])) ^ - (LEFT_ROTATE8(aes_encryption_table[EXTRACT_LSB_BYTE(saved_state[0])])) ^ - (aes_ptr -> nx_crypto_aes_key_schedule[round * 4 + 1]); - - state_data[2] = aes_encryption_table[EXTRACT_MSB_BYTE(saved_state[2])] ^ - (LEFT_ROTATE24(aes_encryption_table[EXTRACT_2ND_BYTE(saved_state[3])])) ^ - (LEFT_ROTATE16(aes_encryption_table[EXTRACT_3RD_BYTE(saved_state[0])])) ^ - (LEFT_ROTATE8(aes_encryption_table[EXTRACT_LSB_BYTE(saved_state[1])])) ^ - (aes_ptr -> nx_crypto_aes_key_schedule[round * 4 + 2]); - - state_data[3] = aes_encryption_table[EXTRACT_MSB_BYTE(saved_state[3])] ^ - (LEFT_ROTATE24(aes_encryption_table[EXTRACT_2ND_BYTE(saved_state[0])])) ^ - (LEFT_ROTATE16(aes_encryption_table[EXTRACT_3RD_BYTE(saved_state[1])])) ^ - (LEFT_ROTATE8(aes_encryption_table[EXTRACT_LSB_BYTE(saved_state[2])])) ^ - (aes_ptr -> nx_crypto_aes_key_schedule[round * 4 + 3]); - } - - /* Write the data back to state structure. */ - aes_ptr -> nx_crypto_aes_state[0] = state_data[0]; - aes_ptr -> nx_crypto_aes_state[1] = state_data[1]; - aes_ptr -> nx_crypto_aes_state[2] = state_data[2]; - aes_ptr -> nx_crypto_aes_state[3] = state_data[3]; - -#ifdef NX_SECURE_KEY_CLEAR - NX_CRYPTO_MEMSET(state_data, 0, sizeof(state_data)); - NX_CRYPTO_MEMSET(saved_state, 0, sizeof(saved_state)); -#endif /* NX_SECURE_KEY_CLEAR */ -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_decryption_round PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function combines the following operations: */ -/* InvSubBytes(state) */ -/* InvShiftrows(state) */ -/* InvMixColumns(state) */ -/* AddRoundKey(state, dw[round*Nb, (round+1)*Nb-1]) */ -/* This step is the body of the loop in the middle of EqInvCipher. */ -/* Refer to Figure 15 on page 25 of the AES Specification (Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* round Indicates the number of */ -/* rounds of this iteration. */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_decrypt Perform AES mode decryption */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), updated */ -/* constants, resulting */ -/* in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_decryption_round(NX_CRYPTO_AES *aes_ptr, int round) -{ -UINT temp_state[4]; -UCHAR *bytes = (UCHAR *)aes_ptr -> nx_crypto_aes_state; -UINT *w = aes_ptr -> nx_crypto_aes_decrypt_key_schedule; -UINT V0, V1, V2, V3; - -UCHAR S; -ULONG val; - - -/* - The InvMixColumns operates on the state after rotation. The table below - illustrates the bytes before and after the InvShiftRows operation. - This routine picks up bytes from its position BEFORE the rotation, - effectively combining InvSubBytes, InvShiftRows, InvMixColumns, AddRoundKey - into one routine. - - S00 S04 S08 S12 S00 S04 S08 S12 - - S01 S05 S09 S13 S13 S01 S05 S09 - ---> - S02 S06 S10 S14 S10 S14 S02 S06 - - S03 S07 S11 S15 S07 S11 S15 S03 - */ - S = bytes[0]; - V0 = aes_decryption_table[S]; - S = bytes[13]; - val = aes_decryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[10]; - val = aes_decryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[7]; - val = aes_decryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[0] = V0 ^ V1 ^ V2 ^ V3 ^ (w[round * 4]); - - S = bytes[4]; - V0 = aes_decryption_table[S]; - S = bytes[1]; - val = aes_decryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[14]; - val = aes_decryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[11]; - val = aes_decryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[1] = V0 ^ V1 ^ V2 ^ V3 ^ (w[round * 4 + 1]); - - S = bytes[8]; - V0 = aes_decryption_table[S]; - S = bytes[5]; - val = aes_decryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[2]; - val = aes_decryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[15]; - val = aes_decryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[2] = V0 ^ V1 ^ V2 ^ V3 ^ (w[round * 4 + 2]); - - S = bytes[12]; - V0 = aes_decryption_table[S]; - S = bytes[9]; - val = aes_decryption_table[S]; - V1 = LEFT_ROTATE24(val); - S = bytes[6]; - val = aes_decryption_table[S]; - V2 = LEFT_ROTATE16(val); - S = bytes[3]; - val = aes_decryption_table[S]; - V3 = LEFT_ROTATE8(val); - temp_state[3] = V0 ^ V1 ^ V2 ^ V3 ^ (w[round * 4 + 3]); - - aes_ptr -> nx_crypto_aes_state[0] = temp_state[0]; - aes_ptr -> nx_crypto_aes_state[1] = temp_state[1]; - aes_ptr -> nx_crypto_aes_state[2] = temp_state[2]; - aes_ptr -> nx_crypto_aes_state[3] = temp_state[3]; - -#ifdef NX_SECURE_KEY_CLEAR - NX_CRYPTO_MEMSET(temp_state, 0, sizeof(temp_state)); -#endif /* NX_SECURE_KEY_CLEAR */ -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_encrypt PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs AES encryption on a block of 16 byte message */ -/* pointed to by "input", and the encrypted text is stored in buffer */ -/* pointed to by "output". The size of the output buffer must be at */ -/* least 16 bytes. The output buffer may point to the same input */ -/* buffer, in which case the encrypted text overwrites the input */ -/* message. */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* input Pointer to an input message */ -/* of 16 bytes. */ -/* output Pointer to an output buffer */ -/* for storing the encrypted */ -/* message. The output buffer */ -/* must be at least 16 bytes. */ -/* length The length of output buffer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_seucre_aes_add_round_key Perform AddRoundKey operation */ -/* _nx_crypto_aes_encryption_round The main body of AES */ -/* encryption */ -/* _nx_crypto_aes_sub_shift_roundkey Perform the last step in AES */ -/* encryption operation */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), disabled */ -/* unaligned access by default,*/ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_aes_encrypt(NX_CRYPTO_AES *aes_ptr, UCHAR *input, UCHAR *output, UINT length) -{ -UINT num_rounds; -UINT *w; -#ifndef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS -UCHAR *aes_state; -#else -UINT *buf; -#endif - - - NX_CRYPTO_PARAMETER_NOT_USED(length); - - w = aes_ptr -> nx_crypto_aes_key_schedule; - - num_rounds = aes_ptr -> nx_crypto_aes_rounds; - - if (num_rounds < 10 || num_rounds > 14) - { - return(NX_CRYPTO_INVALID_PARAMETER); - } - -#ifndef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS - aes_state = (UCHAR *)aes_ptr -> nx_crypto_aes_state; - aes_state[0] = input[0]; - aes_state[1] = input[1]; - aes_state[2] = input[2]; - aes_state[3] = input[3]; - aes_state[4] = input[4]; - aes_state[5] = input[5]; - aes_state[6] = input[6]; - aes_state[7] = input[7]; - aes_state[8] = input[8]; - aes_state[9] = input[9]; - aes_state[10] = input[10]; - aes_state[11] = input[11]; - aes_state[12] = input[12]; - aes_state[13] = input[13]; - aes_state[14] = input[14]; - aes_state[15] = input[15]; -#else - buf = (UINT *)input; - aes_ptr -> nx_crypto_aes_state[0] = buf[0]; - aes_ptr -> nx_crypto_aes_state[1] = buf[1]; - aes_ptr -> nx_crypto_aes_state[2] = buf[2]; - aes_ptr -> nx_crypto_aes_state[3] = buf[3]; -#endif - - _nx_crypto_aes_add_round_key(aes_ptr, &w[0]); - - _nx_crypto_aes_encryption_round(aes_ptr, (INT)num_rounds); - - _nx_crypto_aes_sub_shift_roundkey(aes_ptr, &w[num_rounds * 4]); - - -#ifndef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS - output[0] = aes_state[0]; - output[1] = aes_state[1]; - output[2] = aes_state[2]; - output[3] = aes_state[3]; - output[4] = aes_state[4]; - output[5] = aes_state[5]; - output[6] = aes_state[6]; - output[7] = aes_state[7]; - output[8] = aes_state[8]; - output[9] = aes_state[9]; - output[10] = aes_state[10]; - output[11] = aes_state[11]; - output[12] = aes_state[12]; - output[13] = aes_state[13]; - output[14] = aes_state[14]; - output[15] = aes_state[15]; -#else - buf = (UINT *)output; - buf[0] = aes_ptr -> nx_crypto_aes_state[0]; - buf[1] = aes_ptr -> nx_crypto_aes_state[1]; - buf[2] = aes_ptr -> nx_crypto_aes_state[2]; - buf[3] = aes_ptr -> nx_crypto_aes_state[3]; -#endif - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* Key expansion routines */ -/**************************************************************************/ -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_subword PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs SubBytes(state) operation according to */ -/* section 5.1.1 on page 15 of the AES specification (Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* word The input 4-byte word */ -/* */ -/* OUTPUT */ -/* */ -/* UINT word The value after being */ -/* substituted. */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_key_expansion AES key expansion */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static UINT _nx_crypto_aes_subword(UINT word) -{ -UINT result; - - result = sub_bytes_sbox[word & 0xFF]; - result |= (UINT)((sub_bytes_sbox[(word & 0x0000FF00) >> 8]) << 8); - result |= (UINT)((sub_bytes_sbox[(word & 0x00FF0000) >> 16]) << 16); - result |= (UINT)((sub_bytes_sbox[(word & 0xFF000000) >> 24]) << 24); - return result; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_key_expansion PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This routine performs AES Key Expansion as outlined in seciton 5.2 */ -/* on page 195 of the AES specification (Pub 197) */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_aes_subword Apply sbox substitution */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_key_set Set AES crypto key */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_key_expansion(NX_CRYPTO_AES *aes_ptr) -{ -UINT temp; -UINT i; -UINT iterations = 0; -UINT loop_count; -UINT *expanded_key; -UINT key_size; - - expanded_key = aes_ptr -> nx_crypto_aes_key_schedule; - - key_size = aes_ptr -> nx_crypto_aes_key_size; - - switch (key_size) - { - case NX_CRYPTO_AES_KEY_SIZE_128_BITS: - iterations = 9; - break; - case NX_CRYPTO_AES_KEY_SIZE_192_BITS: - iterations = 7; - break; - - /* case NX_CRYPTO_AES_KEY_SIZE_256_BITS: */ - default: - iterations = 6; - break; - } - - temp = expanded_key[key_size - 1]; - /* Expand the key schedule from the initial key. */ - i = key_size; - for (loop_count = 0; loop_count < iterations; loop_count++) - { - - temp = LEFT_ROTATE8(temp); /* (temp << 8) | (temp >> 24); */ - temp = _nx_crypto_aes_subword(temp); - temp ^= SET_MSB_BYTE((UINT)aes_rcon_array[loop_count]); /* (((UINT)aes_rcon_array[loop_count]) << 24); */ - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - switch (key_size) - { - case NX_CRYPTO_AES_KEY_SIZE_256_BITS: - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - /* AES KEY 256 bits: fall through the rest of the statement to complete - one iteration of key expansion. */ /* fallthrough */ - case NX_CRYPTO_AES_KEY_SIZE_192_BITS: - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - - /* Special case for AES 256: Need an extra subword operation on 4th step. */ - if (key_size == NX_CRYPTO_AES_KEY_SIZE_256_BITS) - { - temp = _nx_crypto_aes_subword(temp); - } - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - /* AES KEY 256 and 192 bits: fall through the rest of the statement to complete - one iteration of key expansion. */ /* fallthrough */ - /* case NX_CRYPTO_AES_KEY_SIZE_128_BITS: */ - default: - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - break; - } - } - /* Last round. */ - temp = LEFT_ROTATE8(temp); /* (temp << 8) | (temp >> 24); */ - temp = _nx_crypto_aes_subword(temp); - temp ^= SET_MSB_BYTE((UINT)aes_rcon_array[loop_count]); /* (((UINT)aes_rcon_array[loop_count]) << 24); */ - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - temp = expanded_key[i - key_size] ^ temp; expanded_key[i] = temp; i++; - -#ifdef NX_SECURE_KEY_CLEAR - temp = 0; -#endif /* NX_SECURE_KEY_CLEAR */ - - return; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_key_expansion_inverse PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This routine performs AES Key Expansion as outlined in seciton 5.2 */ -/* on page 195 of the AES specification (Pub 197) */ -/* */ -/* Note on the decryption side, NetX Crypto AES uses Equivalent */ -/* Inverse Cipher (section 5.3.5 in Pub 197). Therefore the inverse */ -/* key expansion starts with the regular key expansion then apply */ -/* InvMixClumns(state). (Page 24 Pub 197). */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_aes_key_set Set AES crypto key */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_aes_key_expansion_inverse(NX_CRYPTO_AES *aes_ptr) -{ -UINT i; -UINT num_rounds; -UINT *expanded_key; -UINT V0, V1, V2, V3; - -UCHAR S; -ULONG val; -ULONG key; - - expanded_key = aes_ptr -> nx_crypto_aes_decrypt_key_schedule; - - num_rounds = aes_ptr -> nx_crypto_aes_rounds; - - /* Copy the 1st set of keys */ - for (i = 0; i < 4; i++) - { - aes_ptr -> nx_crypto_aes_decrypt_key_schedule[i] = aes_ptr -> nx_crypto_aes_key_schedule[i]; - } - - for (; i < 4 * (num_rounds); i++) - { - - /* Pick up the key value from key_schedule[]. */ - key = aes_ptr -> nx_crypto_aes_key_schedule[i]; - S = EXTRACT_MSB_BYTE(key); /* (key >> 24) & 0xFF; */ - - /* Performs InvMixColumns() operation by using the look up table. */ - V0 = aes_inv_mix_table[S]; - - S = EXTRACT_2ND_BYTE(key); /* (key >> 16) & 0xFF; */ - val = aes_inv_mix_table[S]; - V1 = LEFT_ROTATE24(val); /* (val >> 8) | (val << 24); */ - - S = EXTRACT_3RD_BYTE(key); /* (key >> 8) & 0xFF;*/ - val = aes_inv_mix_table[S]; - V2 = LEFT_ROTATE16(val); /* (val >> 16) | (val << 16); */ - - S = (UCHAR)EXTRACT_LSB_BYTE(key); /* key & 0xFF; */ - val = aes_inv_mix_table[S]; - V3 = LEFT_ROTATE8(val); /* (val >> 24) | (val << 8);*/ - - /* Put values together */ - key = V0 ^ V1 ^ V2 ^ V3; - - /* Stores the expanded key (after applying InvMixColumns()) */ - expanded_key[i] = key; - } - - /* No need to perform InvMixColums() on the last 4 words. */ - expanded_key[i] = aes_ptr -> nx_crypto_aes_key_schedule[i]; - expanded_key[i + 1] = aes_ptr -> nx_crypto_aes_key_schedule[i + 1]; - expanded_key[i + 2] = aes_ptr -> nx_crypto_aes_key_schedule[i + 2]; - expanded_key[i + 3] = aes_ptr -> nx_crypto_aes_key_schedule[i + 3]; - -#ifdef NX_SECURE_KEY_CLEAR - key = 0; -#endif /* NX_SECURE_KEY_CLEAR */ - - return; -} - - -/**************************************************************************/ -/* Decryption routines */ -/**************************************************************************/ - - - - -/* - - __ __ __ __ __ __ - | S0'| | 0e 0b 0d 09| | S0 | - | S1'| | 09 0e 0b 0d| | S1 | - | S2'| = | 0d 09 0e 0b| | S2 | - | S3'| | 0b 0d 09 0e| | S3 | - -- -- -- -- -- -- - - S' = V0 ^ V1 ^ V2 ^ V3 - S0' = {0e}*S0 ^ {0b}*S1 ^ {0d}*S2 ^ {09}*S3 - S1' = {09}*S0 ^ {0e}*S1 ^ {0b}*S2 ^ {0d}*S3 - S2' = {0d}*S0 ^ {09}*S1 ^ {0e}*S2 ^ {0b}*S3 - S3' = {0b}*S0 ^ {0d}*S1 ^ {09}*S2 ^ {0e}*S3 - - - */ - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_decrypt PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs AES decryption on a block of 16 byte message */ -/* pointed to by "input", and the decrypted text is stored in buffer */ -/* pointed to by "output". The size of the output buffer must be at */ -/* least 16 bytes. The output buffer may point to the same input */ -/* buffer, in which case the decrypted text overwrites the input */ -/* message. */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* input Pointer to an input message */ -/* of 16 bytes. */ -/* output Pointer to an output buffer */ -/* for storing the decrypted */ -/* message. The output buffer */ -/* must be at least 16 bytes. */ -/* length The length of output buffer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_seucre_aes_add_round_key Perform AddRoundKey operation */ -/* _nx_crypto_aes_decryption_round The main body of AES */ -/* decryption */ -/* _nx_crypto_aes_inv_sub_shift_roundkey Perform the last step in AES */ -/* decryption operation */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_aes_decrypt(NX_CRYPTO_AES *aes_ptr, UCHAR *input, UCHAR *output, UINT length) -{ -UINT num_rounds; -UINT round; -UINT *w; -UINT *v; -#ifndef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS -UCHAR *aes_state; -#else -UINT *buf; -#endif - - - NX_CRYPTO_PARAMETER_NOT_USED(length); - - w = aes_ptr -> nx_crypto_aes_decrypt_key_schedule; - v = aes_ptr -> nx_crypto_aes_key_schedule; - -#ifndef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS - aes_state = (UCHAR *)aes_ptr -> nx_crypto_aes_state; - aes_state[0] = input[0]; - aes_state[1] = input[1]; - aes_state[2] = input[2]; - aes_state[3] = input[3]; - aes_state[4] = input[4]; - aes_state[5] = input[5]; - aes_state[6] = input[6]; - aes_state[7] = input[7]; - aes_state[8] = input[8]; - aes_state[9] = input[9]; - aes_state[10] = input[10]; - aes_state[11] = input[11]; - aes_state[12] = input[12]; - aes_state[13] = input[13]; - aes_state[14] = input[14]; - aes_state[15] = input[15]; -#else - buf = (UINT *)input; - aes_ptr -> nx_crypto_aes_state[0] = buf[0]; - aes_ptr -> nx_crypto_aes_state[1] = buf[1]; - aes_ptr -> nx_crypto_aes_state[2] = buf[2]; - aes_ptr -> nx_crypto_aes_state[3] = buf[3]; -#endif - - - num_rounds = aes_ptr -> nx_crypto_aes_rounds; - - if (num_rounds < 10 || num_rounds > 14) - { - return(NX_CRYPTO_INVALID_PARAMETER); - } - - _nx_crypto_aes_add_round_key(aes_ptr, &v[num_rounds * 4]); - - for (round = num_rounds - 1; round >= 1; --round) - { - - _nx_crypto_aes_decryption_round(aes_ptr, (INT)round); - } - - _nx_crypto_aes_inv_sub_shift_roundkey(aes_ptr, &w[0]); - - /* Extract the output encrypted block. */ -#ifndef NX_CRYPTO_ENABLE_UNALIGNED_ACCESS - output[0] = aes_state[0]; - output[1] = aes_state[1]; - output[2] = aes_state[2]; - output[3] = aes_state[3]; - output[4] = aes_state[4]; - output[5] = aes_state[5]; - output[6] = aes_state[6]; - output[7] = aes_state[7]; - output[8] = aes_state[8]; - output[9] = aes_state[9]; - output[10] = aes_state[10]; - output[11] = aes_state[11]; - output[12] = aes_state[12]; - output[13] = aes_state[13]; - output[14] = aes_state[14]; - output[15] = aes_state[15]; -#else - buf = (UINT *)output; - buf[0] = aes_ptr -> nx_crypto_aes_state[0]; - buf[1] = aes_ptr -> nx_crypto_aes_state[1]; - buf[2] = aes_ptr -> nx_crypto_aes_state[2]; - buf[3] = aes_ptr -> nx_crypto_aes_state[3]; -#endif - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_aes_key_set PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function configures key for AES encryption and decryption. */ -/* */ -/* INPUT */ -/* */ -/* aes_ptr Pointer to AES control block */ -/* key Pointer to key string */ -/* key_size Size of the key, valid values */ -/* are: */ -/* NX_CRYPTO_AES_KEY_SIZE_128_BITS */ -/* NX_CRYPTO_AES_KEY_SIZE_192_BITS */ -/* NX_CRYPTO_AES_KEY_SIZE_256_BITS */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_aes_key_expansion Key expansion for encryption */ -/* _nx_crypto_aes_key_expansion_inverse Key expansion for decryption */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_aes_key_set(NX_CRYPTO_AES *aes_ptr, UCHAR *key, UINT key_size) -{ -UCHAR *expanded_key; -UINT i; - - /* Set the AES key size (should be in 32-bit *words*). */ - aes_ptr -> nx_crypto_aes_key_size = (USHORT)key_size; - - expanded_key = (UCHAR *)aes_ptr -> nx_crypto_aes_key_schedule; - - /* Copy the key into the beginning of the expanded key buffer. */ - for (i = 0; i < key_size * 4; ++i) - { - expanded_key[i] = key[i]; - } - - /* Based on the key size, determine the number of rounds. */ - switch (key_size) - { - case NX_CRYPTO_AES_KEY_SIZE_128_BITS: - aes_ptr -> nx_crypto_aes_rounds = 10; - break; - case NX_CRYPTO_AES_KEY_SIZE_192_BITS: - aes_ptr -> nx_crypto_aes_rounds = 12; - break; - /* case NX_CRYPTO_AES_KEY_SIZE_256_BITS: */ - default: - aes_ptr -> nx_crypto_aes_rounds = 14; - break; - } - - - _nx_crypto_aes_key_expansion(aes_ptr); - - _nx_crypto_aes_key_expansion_inverse(aes_ptr); - - return(NX_CRYPTO_SUCCESS); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_init PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function initializes the AES crypto module. */ -/* */ -/* INPUT */ -/* */ -/* method Crypto Method Object */ -/* key Key */ -/* key_size_in_bits Size of the key, in bits */ -/* handle Handle, specified by user */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Size of the metadata area */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_aes_key_set Set the key for AES */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_operation Handle AES encrypt or decrypt */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_init(struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - VOID **handle, - VOID *crypto_metadata, - ULONG crypto_metadata_size) -{ - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - - NX_CRYPTO_STATE_CHECK - - - if ((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((((ULONG)crypto_metadata) & 0x3) != 0) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* We only support 128-bit or 256-bit key size for the time-being. */ - if ((key_size_in_bits != NX_CRYPTO_AES_128_KEY_LEN_IN_BITS) && (key_size_in_bits != NX_CRYPTO_AES_192_KEY_LEN_IN_BITS) && (key_size_in_bits != NX_CRYPTO_AES_256_KEY_LEN_IN_BITS)) - { - return(NX_CRYPTO_UNSUPPORTED_KEY_SIZE); - } - - NX_CRYPTO_MEMSET(&((NX_CRYPTO_AES *)crypto_metadata) -> nx_crypto_aes_mode_context, 0, sizeof(((NX_CRYPTO_AES *)crypto_metadata) -> nx_crypto_aes_mode_context)); - - _nx_crypto_aes_key_set((NX_CRYPTO_AES *)crypto_metadata, key, key_size_in_bits >> 5); - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_cleanup PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function cleans up the crypto metadata. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Crypto metadata */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* NX_CRYPTO_MEMSET Set the memory */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_cleanup(VOID *crypto_metadata) -{ - - NX_CRYPTO_STATE_CHECK - -#ifdef NX_SECURE_KEY_CLEAR - if (!crypto_metadata) - return (NX_CRYPTO_SUCCESS); - - /* Clean up the crypto metadata. */ - NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_AES)); -#else - NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); -#endif /* NX_SECURE_KEY_CLEAR */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the AES algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op AES operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_method_aes_cbc_operation Handle AES CBC operation */ -/* _nx_crypto_method_aes_ccm_operation Handle AES CCM operation */ -/* _nx_crypto_method_aes_gcm_operation Handle AES GCM operation */ -/* _nx_crypto_method_aes_ctr_operation Handle AES CTR operation */ -/* _nx_crypto_method_aes_xcbc_operation Handle AES XCBC operation */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ -UINT status; - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Check if the algorithm is cbc or ctr. */ - if (method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CBC) - { - - /* AES CBC */ - status = _nx_crypto_method_aes_cbc_operation(op, handle, method, key, key_size_in_bits, - input, input_length_in_byte, iv_ptr, - output, output_length_in_byte, - crypto_metadata, crypto_metadata_size, - packet_ptr, nx_crypto_hw_process_callback); - } - else if ((method -> nx_crypto_algorithm >= NX_CRYPTO_ENCRYPTION_AES_CCM_8) && - (method -> nx_crypto_algorithm <= NX_CRYPTO_ENCRYPTION_AES_CCM)) - { - - /* AES CCM */ - status = _nx_crypto_method_aes_ccm_operation(op, handle, method, key, key_size_in_bits, - input, input_length_in_byte, iv_ptr, - output, output_length_in_byte, - crypto_metadata, crypto_metadata_size, - packet_ptr, nx_crypto_hw_process_callback); - - } - else if ((method -> nx_crypto_algorithm >= NX_CRYPTO_ENCRYPTION_AES_GCM_8) && - (method -> nx_crypto_algorithm <= NX_CRYPTO_ENCRYPTION_AES_GCM_16)) - { - - /* AES GCM */ - status = _nx_crypto_method_aes_gcm_operation(op, handle, method, key, key_size_in_bits, - input, input_length_in_byte, iv_ptr, - output, output_length_in_byte, - crypto_metadata, crypto_metadata_size, - packet_ptr, nx_crypto_hw_process_callback); - - } - else if (method -> nx_crypto_algorithm == NX_CRYPTO_ENCRYPTION_AES_CTR) - { - - /* AES_CTR */ - status = _nx_crypto_method_aes_ctr_operation(op, handle, method, key, key_size_in_bits, - input, input_length_in_byte, iv_ptr, - output, output_length_in_byte, - crypto_metadata, crypto_metadata_size, - packet_ptr, nx_crypto_hw_process_callback); - } - else if (method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_AES_XCBC_MAC_96) - { - status = _nx_crypto_method_aes_xcbc_operation(op, handle, method, key, key_size_in_bits, - input, input_length_in_byte, iv_ptr, - output, output_length_in_byte, - crypto_metadata, crypto_metadata_size, - packet_ptr, nx_crypto_hw_process_callback); - } - else - { - status = NX_CRYPTO_INVALID_ALGORITHM; - } - - return status; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_cbc_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the AES CBC algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op AES operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_cbc_encrypt Perform CBC mode encryption */ -/* _nx_crypto_cbc_decrypt Perform CBC mode decryption */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_cbc_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ - -NX_CRYPTO_AES *ctx; -UINT status; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_AES *)crypto_metadata; - - switch (op) - { - case NX_CRYPTO_DECRYPT: - { - status = _nx_crypto_cbc_decrypt_init(&(ctx -> nx_crypto_aes_mode_context.cbc), - iv_ptr, method -> nx_crypto_IV_size_in_bits >> 3); - if (status) - { - break; - } - - status = _nx_crypto_cbc_decrypt(ctx, &(ctx -> nx_crypto_aes_mode_context.cbc), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_decrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT: - { - status = _nx_crypto_cbc_encrypt_init(&(ctx -> nx_crypto_aes_mode_context.cbc), - iv_ptr, method -> nx_crypto_IV_size_in_bits >> 3); - if (status) - { - break; - } - - status = _nx_crypto_cbc_encrypt(ctx, &(ctx -> nx_crypto_aes_mode_context.cbc), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_DECRYPT_INITIALIZE: - { - status = _nx_crypto_cbc_decrypt_init(&(ctx -> nx_crypto_aes_mode_context.cbc), - iv_ptr, method -> nx_crypto_IV_size_in_bits >> 3); - } break; - - case NX_CRYPTO_ENCRYPT_INITIALIZE: - { - status = _nx_crypto_cbc_encrypt_init(&(ctx -> nx_crypto_aes_mode_context.cbc), - iv_ptr, method -> nx_crypto_IV_size_in_bits >> 3); - } break; - - case NX_CRYPTO_DECRYPT_UPDATE: - { - status = _nx_crypto_cbc_decrypt(ctx, &(ctx -> nx_crypto_aes_mode_context.cbc), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_decrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_UPDATE: - { - status = _nx_crypto_cbc_encrypt(ctx, &(ctx -> nx_crypto_aes_mode_context.cbc), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_CALCULATE: - /* fallthrough */ - case NX_CRYPTO_DECRYPT_CALCULATE: - { - - /* Nothing to do. */ - status = NX_CRYPTO_SUCCESS; - } break; - - default: - { - status = NX_CRYPTO_INVALID_ALGORITHM; - } break; - } - - return(status); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_ccm_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the AES CCM algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op AES operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_cbc_encrypt Perform CBC mode encryption */ -/* _nx_crypto_cbc_decrypt Perform CBC mode decryption */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_operation Handle AES encrypt or decrypt */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_ccm_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ - -NX_CRYPTO_AES *ctx; -UINT status; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_AES *)crypto_metadata; - - if ((method -> nx_crypto_algorithm < NX_CRYPTO_ENCRYPTION_AES_CCM_8) || - (method -> nx_crypto_algorithm > NX_CRYPTO_ENCRYPTION_AES_CCM)) - { - return(NX_CRYPTO_INVALID_ALGORITHM); - } - - /* IV : Nonce length(1 byte) + Nonce - nx_crypto_ICV_size_in_bits: authentication tag length in bits */ - switch (op) - { - case NX_CRYPTO_DECRYPT: - { - if (iv_ptr == NX_CRYPTO_NULL || - (ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data_len > 0 && - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data == NX_CRYPTO_NULL)) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - if (input_length_in_byte < (method -> nx_crypto_ICV_size_in_bits >> 3) || - output_length_in_byte < input_length_in_byte - (method -> nx_crypto_ICV_size_in_bits >> 3)) - { - status = NX_CRYPTO_INVALID_BUFFER_SIZE; - break; - } - - status = _nx_crypto_ccm_decrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data, - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data_len, - input_length_in_byte - (method -> nx_crypto_ICV_size_in_bits >> 3), iv_ptr, - (UCHAR)(method -> nx_crypto_ICV_size_in_bits >> 3), - NX_CRYPTO_AES_BLOCK_SIZE); - - if (status) - { - break; - } - - status = _nx_crypto_ccm_decrypt_update(NX_CRYPTO_DECRYPT_UPDATE, - ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte - (method -> nx_crypto_ICV_size_in_bits >> 3), - NX_CRYPTO_AES_BLOCK_SIZE); - if (status) - { - break; - } - - status = _nx_crypto_ccm_decrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input + input_length_in_byte - (method -> nx_crypto_ICV_size_in_bits >> 3), - NX_CRYPTO_AES_BLOCK_SIZE); - if (status) - { - break; - } - } break; - - case NX_CRYPTO_ENCRYPT: - { - if (iv_ptr == NX_CRYPTO_NULL || - (ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data_len > 0 && - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data == NX_CRYPTO_NULL)) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - if (output_length_in_byte < input_length_in_byte + (method -> nx_crypto_ICV_size_in_bits >> 3)) - { - status = NX_CRYPTO_INVALID_BUFFER_SIZE; - break; - } - - status = _nx_crypto_ccm_encrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data, - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data_len, - input_length_in_byte, iv_ptr, - (UCHAR)(method -> nx_crypto_ICV_size_in_bits >> 3), - NX_CRYPTO_AES_BLOCK_SIZE); - - if (status) - { - break; - } - - status = _nx_crypto_ccm_encrypt_update(NX_CRYPTO_ENCRYPT_UPDATE, - ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - if (status) - { - break; - } - - status = _nx_crypto_ccm_encrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - output + input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - if (status) - { - break; - } - } break; - - case NX_CRYPTO_SET_ADDITIONAL_DATA: - { - - /* Set additonal data pointer. */ - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data = (VOID *)input; - - /* Set additional data length. */ - ctx -> nx_crypto_aes_mode_context.ccm.nx_crypto_ccm_additional_data_len = input_length_in_byte; - - status = NX_CRYPTO_SUCCESS; - } break; - - case NX_CRYPTO_DECRYPT_INITIALIZE: - { - if (iv_ptr == NX_CRYPTO_NULL) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - status = _nx_crypto_ccm_decrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, /* pointers to AAD */ - input_length_in_byte, /* length of AAD */ - output_length_in_byte, /* total length of message */ - iv_ptr, - (UCHAR)(method -> nx_crypto_ICV_size_in_bits >> 3), - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_DECRYPT_UPDATE: - { - status = _nx_crypto_ccm_decrypt_update(NX_CRYPTO_DECRYPT_UPDATE, - ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - - } break; - - case NX_CRYPTO_DECRYPT_CALCULATE: - { - status = _nx_crypto_ccm_decrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_INITIALIZE: - { - if (iv_ptr == NX_CRYPTO_NULL) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - status = _nx_crypto_ccm_encrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, /* pointers to AAD */ - input_length_in_byte, /* length of AAD */ - output_length_in_byte, /* total length of message */ - iv_ptr, - (UCHAR)(method -> nx_crypto_ICV_size_in_bits >> 3), - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_UPDATE: - { - - status = _nx_crypto_ccm_encrypt_update(NX_CRYPTO_ENCRYPT_UPDATE, - ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_CALCULATE: - { - - status = _nx_crypto_ccm_encrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.ccm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - output, NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - default: - { - status = NX_CRYPTO_INVALID_ALGORITHM; - } break; - } - - return(status); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_gcm_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the AES GCM algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op AES operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_gcm_operation Perform GCM encryption or */ -/* decryption */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_operation Handle AES encrypt or decrypt */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_gcm_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ - -NX_CRYPTO_AES *ctx; -UINT icv_len; -UINT message_len; -UINT status; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_AES *)crypto_metadata; - - if ((method -> nx_crypto_algorithm < NX_CRYPTO_ENCRYPTION_AES_GCM_8) || - (method -> nx_crypto_algorithm > NX_CRYPTO_ENCRYPTION_AES_GCM_16)) - { - return(NX_CRYPTO_INVALID_ALGORITHM); - } - - /* IV : Nonce length(1 byte) + Nonce - nx_crypto_ICV_size_in_bits: authentication tag length in bits */ - switch (op) - { - case NX_CRYPTO_DECRYPT: - { - if (iv_ptr == NX_CRYPTO_NULL || - (ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data_len > 0 && - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data == NX_CRYPTO_NULL)) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - icv_len = (method -> nx_crypto_ICV_size_in_bits >> 3); - - if (input_length_in_byte < icv_len || output_length_in_byte < input_length_in_byte - icv_len) - { - status = NX_CRYPTO_INVALID_BUFFER_SIZE; - break; - } - - message_len = input_length_in_byte - icv_len; - status = _nx_crypto_gcm_decrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data, - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data_len, - iv_ptr, NX_CRYPTO_AES_BLOCK_SIZE); - - if (status) - { - break; - } - - status = _nx_crypto_gcm_decrypt_update(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, message_len, - NX_CRYPTO_AES_BLOCK_SIZE); - - if (status) - { - break; - } - - status = _nx_crypto_gcm_decrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input + message_len, icv_len, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT: - { - if (iv_ptr == NX_CRYPTO_NULL || - (ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data_len > 0 && - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data == NX_CRYPTO_NULL)) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - icv_len = (method -> nx_crypto_ICV_size_in_bits >> 3); - - if (output_length_in_byte < input_length_in_byte + icv_len) - { - status = NX_CRYPTO_INVALID_BUFFER_SIZE; - break; - } - - status = _nx_crypto_gcm_encrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data, - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data_len, - iv_ptr, NX_CRYPTO_AES_BLOCK_SIZE); - - if (status) - { - break; - } - - status = _nx_crypto_gcm_encrypt_update(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - - if (status) - { - break; - } - - status = _nx_crypto_gcm_encrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - output + input_length_in_byte, icv_len, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_DECRYPT_INITIALIZE: - { - if (iv_ptr == NX_CRYPTO_NULL) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - status = _nx_crypto_gcm_decrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, /* pointers to AAD */ - input_length_in_byte, /* length of AAD */ - iv_ptr, NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_DECRYPT_UPDATE: - { - status = _nx_crypto_gcm_decrypt_update(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_DECRYPT_CALCULATE: - { - icv_len = (method -> nx_crypto_ICV_size_in_bits >> 3); - if (input_length_in_byte < icv_len) - { - status = NX_CRYPTO_INVALID_BUFFER_SIZE; - break; - } - - status = _nx_crypto_gcm_decrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, icv_len, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_INITIALIZE: - { - if (iv_ptr == NX_CRYPTO_NULL) - { - status = NX_CRYPTO_PTR_ERROR; - break; - } - - status = _nx_crypto_gcm_encrypt_init(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, /* pointers to AAD */ - input_length_in_byte, /* length of AAD */ - iv_ptr, NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_UPDATE: - { - status = _nx_crypto_gcm_encrypt_update(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_CALCULATE: - { - icv_len = (method -> nx_crypto_ICV_size_in_bits >> 3); - if (output_length_in_byte < icv_len) - { - status = NX_CRYPTO_INVALID_BUFFER_SIZE; - break; - } - - status = _nx_crypto_gcm_encrypt_calculate(ctx, &(ctx -> nx_crypto_aes_mode_context.gcm), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - output, icv_len, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_SET_ADDITIONAL_DATA: - { - - /* Set additonal data pointer. */ - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data = (VOID *)input; - - /* Set additional data length. */ - ctx -> nx_crypto_aes_mode_context.gcm.nx_crypto_gcm_additional_data_len = input_length_in_byte; - - status = NX_CRYPTO_SUCCESS; - } break; - - default: - { - status = NX_CRYPTO_INVALID_ALGORITHM; - } break; - } - - return(status); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_ctr_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the AES CTR algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op AES operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_ctr_encrypt Perform CTR mode encryption */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_operation Handle AES encrypt or decrypt */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_ctr_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ - -NX_CRYPTO_AES *ctx; -UINT status; - - NX_CRYPTO_PARAMETER_NOT_USED(op); - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_AES *)crypto_metadata; - - switch (op) - { - case NX_CRYPTO_ENCRYPT: - /* fallthrough */ - case NX_CRYPTO_DECRYPT: - { - status = _nx_crypto_ctr_encrypt_init(&(ctx -> nx_crypto_aes_mode_context.ctr), - iv_ptr, 8, &key[key_size_in_bits >> 3], 4); - if (status) - { - break; - } - - status = _nx_crypto_ctr_encrypt(ctx, &(ctx -> nx_crypto_aes_mode_context.ctr), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_INITIALIZE: - /* fallthrough */ - case NX_CRYPTO_DECRYPT_INITIALIZE: - { - status = _nx_crypto_ctr_encrypt_init(&(ctx -> nx_crypto_aes_mode_context.ctr), - iv_ptr, 8, input, /* input pointers to nonce */ - input_length_in_byte); - } break; - - case NX_CRYPTO_ENCRYPT_UPDATE: - /* fallthrough */ - case NX_CRYPTO_DECRYPT_UPDATE: - { - status = _nx_crypto_ctr_encrypt(ctx, &(ctx -> nx_crypto_aes_mode_context.ctr), - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - input, output, input_length_in_byte, - NX_CRYPTO_AES_BLOCK_SIZE); - } break; - - case NX_CRYPTO_ENCRYPT_CALCULATE: - /* fallthrough */ - case NX_CRYPTO_DECRYPT_CALCULATE: - { - - /* Nothing to do. */ - status = NX_CRYPTO_SUCCESS; - } break; - - default: - { - status = NX_CRYPTO_INVALID_ALGORITHM; - } break; - } - - return status; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_aes_xcbc_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the AES XCBC algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op AES operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_xcbc_mac Perform XCBC mode */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_operation Handle AES encrypt or decrypt */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_aes_xcbc_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ - -NX_CRYPTO_AES *ctx; -UINT status; - - NX_CRYPTO_PARAMETER_NOT_USED(op); - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_AES)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_AES *)crypto_metadata; - - status = _nx_crypto_xcbc_mac(ctx, - (UINT (*)(VOID *, UCHAR *, UCHAR *, UINT))_nx_crypto_aes_encrypt, - (UINT (*)(VOID *, UCHAR *, UINT))_nx_crypto_aes_key_set, - key, key_size_in_bits, - input, output, input_length_in_byte, - NX_CRYPTO_NULL, NX_CRYPTO_AUTHENTICATION_ICV_TRUNC_BITS >> 3, - NX_CRYPTO_AES_BLOCK_SIZE); - - return status; -} diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_cbc.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_cbc.c deleted file mode 100644 index 344375a58..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_cbc.c +++ /dev/null @@ -1,336 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** CBC Mode */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - -#include "nx_crypto_cbc.h" - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_cbc_xor PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs XOR operation on the output buffer. */ -/* */ -/* INPUT */ -/* */ -/* plaintext Pointer to input plantext */ -/* key Value to be xor'ed */ -/* ciphertext Output buffer */ -/* block_size Block size */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_cbc_encrypt Perform CBC mode encryption */ -/* _nx_crypto_cbc_decrypt Perform CBC mode decryption */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 09-30-2020 Timothy Stapko Initial Version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP static VOID _nx_crypto_cbc_xor(UCHAR *plaintext, UCHAR *key, UCHAR *ciphertext, UCHAR block_size) -{ -UINT i; - - for (i = 0; i < block_size; i++) - { - ciphertext[i] = plaintext[i] ^ key[i]; - } -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_cbc_encrypt PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs CBC mode encryption. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Pointer to crypto metadata */ -/* cbc_metadata Pointer to CBC metadata */ -/* crypto_function Pointer to crypto function */ -/* input Pointer to clear text input */ -/* output Pointer to encrypted output */ -/* The size of the output */ -/* buffer must be at least */ -/* the size of input message. */ -/* length Length of the input message. */ -/* block_size Block size */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_cbc_xor Perform CBC XOR operation */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_cbc_operation Handle AES encrypt or decrypt */ -/* _nx_crypto_method_des_operation Handle DES encrypt or decrypt */ -/* _nx_crypto_method_3des_operation Handle 3DES encrypt or decrypt*/ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* verified memcpy use cases, */ -/* and updated constants, */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_cbc_encrypt(VOID *crypto_metadata, NX_CRYPTO_CBC *cbc_metadata, - UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), - UCHAR *input, UCHAR *output, UINT length, UCHAR block_size) -{ -UCHAR *last_cipher; -UINT i; - - if (block_size == 0) - { - return(NX_CRYPTO_INVALID_PARAMETER); - } - - /* Determine if data length is multiple of block size. */ - if (length % block_size) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Determine if block size is larger than the size of save_input. */ - if (block_size > sizeof(cbc_metadata -> nx_crypto_cbc_last_block)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Pick up last cipher. */ - last_cipher = cbc_metadata -> nx_crypto_cbc_last_block; - - for (i = 0; i < length; i += block_size) - { - - /* XOR. */ - _nx_crypto_cbc_xor(&input[i], last_cipher, output, block_size); - - /* Encrypt the block. */ - crypto_function(crypto_metadata, output, output, block_size); - - /* Remember the previous encrypt block result. */ - last_cipher = output; - - output += block_size; - } - - /* Store the last cipher for next round. */ - NX_CRYPTO_MEMCPY(cbc_metadata -> nx_crypto_cbc_last_block, last_cipher, block_size); /* Use case of memcpy is verified. */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_cbc_decrypt PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs CBC mode decryption. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Pointer to crypto metadata */ -/* cbc_metadata Pointer to CBC metadata */ -/* crypto_function Pointer to crypto function */ -/* input Pointer to clear text input */ -/* output Pointer to encrypted output */ -/* The size of the output */ -/* buffer must be at least */ -/* the size of input message. */ -/* length Length of the input message. */ -/* block_size Block size */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_cbc_xor Perform CBC XOR operation */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_cbc_operation Handle AES encrypt or decrypt */ -/* _nx_crypto_method_des_operation Handle DES encrypt or decrypt */ -/* _nx_crypto_method_3des_operation Handle 3DES encrypt or decrypt*/ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* verified memcpy use cases, */ -/* and updated constants, */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_cbc_decrypt(VOID *crypto_metadata, NX_CRYPTO_CBC *cbc_metadata, - UINT (*crypto_function)(VOID *, UCHAR *, UCHAR *, UINT), - UCHAR *input, UCHAR *output, UINT length, UCHAR block_size) -{ -UCHAR *last_cipher; -UCHAR save_input[16]; -UINT i; - - if (block_size == 0) - { - return(NX_CRYPTO_INVALID_PARAMETER); - } - - /* Determine if data length is multiple of block size. */ - if (length % block_size) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Determine if block size is larger than the size of save_input. */ - if (block_size > sizeof(cbc_metadata -> nx_crypto_cbc_last_block)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - last_cipher = cbc_metadata -> nx_crypto_cbc_last_block; - - for (i = 0; i < length; i += block_size) - { - /* If input == output, the xor clobbers the input buffer so we need to save off our last ciphertext - before doing the xor. */ - NX_CRYPTO_MEMCPY(save_input, &input[i], block_size); /* Use case of memcpy is verified. */ - - /* Decrypt the block. */ - crypto_function(crypto_metadata, &input[i], &output[i], block_size); - - /* XOR. */ - _nx_crypto_cbc_xor(&output[i], last_cipher, &output[i], block_size); - - NX_CRYPTO_MEMCPY(last_cipher, save_input, block_size); /* Use case of memcpy is verified. */ - } - -#ifdef NX_SECURE_KEY_CLEAR - NX_CRYPTO_MEMSET(save_input, 0, sizeof(save_input)); -#endif /* NX_SECURE_KEY_CLEAR */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_cbc_encrypt_init PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs CBC mode initialization. */ -/* */ -/* INPUT */ -/* */ -/* cbc_metadata Pointer to CBC metadata */ -/* iv Pointer to Initial Vector */ -/* iv_len Length of Initial Vector */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_aes_cbc_operation Handle AES encrypt or decrypt */ -/* _nx_crypto_method_des_operation Handle DES encrypt or decrypt */ -/* _nx_crypto_method_3des_operation Handle 3DES encrypt or decrypt*/ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* verified memcpy use cases, */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_cbc_encrypt_init(NX_CRYPTO_CBC *cbc_metadata, UCHAR *iv, UINT iv_len) -{ - - /* Determine if IV size is larger than the size of save_input. */ - if (iv_len > sizeof(cbc_metadata -> nx_crypto_cbc_last_block)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Copy IV to last cipher. */ - NX_CRYPTO_MEMCPY(cbc_metadata -> nx_crypto_cbc_last_block, iv, iv_len); /* Use case of memcpy is verified. */ - - return(NX_CRYPTO_SUCCESS); -} diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_ecdh.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_ecdh.c deleted file mode 100644 index 0375847a4..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_ecdh.c +++ /dev/null @@ -1,750 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** Elliptic-curve Diffie-Hellman (ECDH) */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - -#define NX_CRYPTO_SOURCE_CODE - - -/* Include necessary system files. */ - -#include "nx_crypto_ecdh.h" - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_ecdh_key_pair_import PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function sets up a Elliptic-curve Diffie-Hellman context by */ -/* importing a local key pair. */ -/* */ -/* INPUT */ -/* */ -/* ecdh_ptr ECDH context */ -/* curve Elliptic Curve */ -/* local_private_key_ptr Pointer to local private key */ -/* local_private_key_len Local private key length */ -/* local_public_key_ptr Pointer to local public key */ -/* local_public_key_len Remote public key length */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_ecdh_key_pair_import(NX_CRYPTO_ECDH *ecdh_ptr, - NX_CRYPTO_EC *curve, - UCHAR *local_private_key_ptr, - ULONG local_private_key_len, - UCHAR *local_public_key_ptr, - ULONG local_public_key_len) -{ -UINT public_key_len; -NX_CRYPTO_HUGE_NUMBER private_key; - - NX_CRYPTO_PARAMETER_NOT_USED(local_public_key_ptr); - - if (local_private_key_len > sizeof(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - public_key_len = 1 + (((curve -> nx_crypto_ec_bits + 7) >> 3) << 1); - if (local_public_key_len > public_key_len) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - /* Assign the desired key size based on the chosen elliptic curve. */ - ecdh_ptr -> nx_crypto_ecdh_key_size = curve -> nx_crypto_ec_n.nx_crypto_huge_buffer_size; - - /* Clear the private key buffer. */ - NX_CRYPTO_MEMSET(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer, 0, - sizeof(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer)); - - /* Setup the private key. */ - private_key.nx_crypto_huge_number_data = ecdh_ptr -> nx_crypto_ecdh_private_key_buffer; - private_key.nx_crypto_huge_buffer_size = sizeof(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer); - _nx_crypto_huge_number_setup(&private_key, local_private_key_ptr, local_private_key_len); - - return(NX_CRYPTO_SUCCESS); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_ecdh_private_key_export PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function exports the local private key in Elliptic-curve */ -/* Diffie-Hellman context. */ -/* */ -/* INPUT */ -/* */ -/* ecdh_ptr ECDH context */ -/* local_private_key_ptr Pointer to local private key */ -/* local_private_key_len Local private key length */ -/* actual_local_private_key_len Pointer to private key length */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -UINT _nx_crypto_ecdh_private_key_export(NX_CRYPTO_ECDH *ecdh_ptr, - UCHAR *local_private_key_ptr, - ULONG local_private_key_len, - ULONG *actual_local_private_key_len) -{ -UINT status; -UINT key_size; -UINT clen; -NX_CRYPTO_EC *curve; -NX_CRYPTO_HUGE_NUMBER private_key; - - /* Make sure the key size was assigned before we do anything else. Generally, this means - _nx_crypto_ecdh_setup was not called to set up the NX_CRYPTO_ECDH structure prior to this call. */ - if (0 == ecdh_ptr -> nx_crypto_ecdh_key_size) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - curve = ecdh_ptr -> nx_crypto_ecdh_curve; - - /* Figure out the sizes of our keys and buffers. */ - key_size = ecdh_ptr -> nx_crypto_ecdh_key_size; - - /* Check to make sure the buffer is large enough to hold the private key. */ - clen = (curve -> nx_crypto_ec_bits + 7) >> 3; - if (local_private_key_len < clen) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - /* Private key buffer - note that no scratch is required for the private key, but we set it in case - it is needed in the future. */ - private_key.nx_crypto_huge_number_data = (HN_UBASE *)ecdh_ptr -> nx_crypto_ecdh_private_key_buffer; - private_key.nx_crypto_huge_number_size = key_size >> HN_SIZE_SHIFT; - private_key.nx_crypto_huge_buffer_size = key_size; - private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE; - - /* Copy the private key into the return buffer. */ - status = _nx_crypto_huge_number_extract_fixed_size(&private_key, - local_private_key_ptr, clen); - *actual_local_private_key_len = clen; - - return(status); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_ecdh_setup PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function sets up a Elliptic-curve Diffie-Hellman context by */ -/* generating a local key pair. */ -/* */ -/* INPUT */ -/* */ -/* ecdh_ptr ECDH context */ -/* share_secret_key_ptr Shared secret buffer pointer */ -/* share_secret_key_len_ptr Length of shared secret */ -/* local_public_key_ptr Pointer to local public key */ -/* local_public_key_len Remote public key length */ -/* scratch_buf_ptr Pointer to scratch buffer, */ -/* which cannot be smaller */ -/* than 6 times of the key */ -/* size (in bytes). This */ -/* scratch buffer can be */ -/* reused after this function */ -/* returns. */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_ec_key_pair_generation_extra */ -/* Generate EC Key Pair */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_ecdh_setup(NX_CRYPTO_ECDH *ecdh_ptr, - UCHAR *local_public_key_ptr, - ULONG local_public_key_len, - ULONG *actual_local_public_key_len, - NX_CRYPTO_EC *curve, - HN_UBASE *scratch_buf_ptr) -{ -UINT public_key_len; -/* Actual huge numbers used in calculations */ -NX_CRYPTO_HUGE_NUMBER private_key; -NX_CRYPTO_EC_POINT public_key; - - public_key_len = 1 + (((curve -> nx_crypto_ec_bits + 7) >> 3) << 1); - if (local_public_key_len < public_key_len) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - ecdh_ptr -> nx_crypto_ecdh_curve = curve; - - /* Assign the desired key size based on the chosen elliptic curve. */ - ecdh_ptr -> nx_crypto_ecdh_key_size = curve -> nx_crypto_ec_n.nx_crypto_huge_buffer_size; - - /* Public key buffer (and scratch). */ - NX_CRYPTO_EC_POINT_INITIALIZE(&public_key, NX_CRYPTO_EC_POINT_AFFINE, scratch_buf_ptr, - ecdh_ptr -> nx_crypto_ecdh_key_size); - - /* Private key buffer - note that no scratch is required for the private key. */ - private_key.nx_crypto_huge_number_data = ecdh_ptr -> nx_crypto_ecdh_private_key_buffer; - private_key.nx_crypto_huge_number_size = ecdh_ptr -> nx_crypto_ecdh_key_size >> HN_SIZE_SHIFT; - private_key.nx_crypto_huge_buffer_size = sizeof(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer); - private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE; - - /* Clear the private key buffer. */ - NX_CRYPTO_MEMSET(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer, 0, - sizeof(ecdh_ptr -> nx_crypto_ecdh_private_key_buffer)); - - /* Generate Key Pair. */ - _nx_crypto_ec_key_pair_generation_extra(curve, &curve -> nx_crypto_ec_g, &private_key, - &public_key, scratch_buf_ptr); - - /* Copy the public key into the return buffer. */ - _nx_crypto_ec_point_extract_uncompressed(curve, &public_key, local_public_key_ptr, - local_public_key_len, &public_key_len); - - if (public_key_len == 0) - { - return(NX_CRYPTO_SIZE_ERROR); - } - *actual_local_public_key_len = public_key_len; - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_ecdh_compute_secret PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function computes the Elliptic-curve Diffie-Hellman shared */ -/* secret using an existing Elliptic-curve Diffie-Hellman context */ -/* and a public key received from a remote entity. */ -/* */ -/* INPUT */ -/* */ -/* ecdh_ptr ECDH context */ -/* share_secret_key_ptr Shared secret buffer pointer */ -/* share_secret_key_len_ptr Length of shared secret */ -/* remote_public_key Pointer to remote public key */ -/* remote_public_key_len Remote public key length */ -/* scratch_buf_ptr Pointer to scratch buffer, */ -/* which cannot be smaller */ -/* than 8 times of the key */ -/* size (in bytes). This */ -/* scratch buffer can be */ -/* reused after this function */ -/* returns. */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_huge_number_extract Extract huge number */ -/* _nx_crypto_huge_number_setup Setup huge number */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), and */ -/* added public key validation,*/ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_ecdh_compute_secret(NX_CRYPTO_ECDH *ecdh_ptr, - UCHAR *share_secret_key_ptr, - ULONG share_secret_key_len_ptr, - ULONG *actual_share_secret_key_len, - UCHAR *remote_public_key, - ULONG remote_public_key_len, - HN_UBASE *scratch_buf_ptr) -{ -UINT status; -UINT key_size; -UINT clen; -NX_CRYPTO_EC *curve; -/* Actual huge numbers used in calculations */ -NX_CRYPTO_HUGE_NUMBER private_key; -NX_CRYPTO_EC_POINT public_key, shared_secret; - - /* Make sure the key size was assigned before we do anything else. Generally, this means - _nx_crypto_ecdh_setup was not called to set up the NX_CRYPTO_ECDH structure prior to this call. */ - if (0 == ecdh_ptr -> nx_crypto_ecdh_key_size) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - curve = ecdh_ptr -> nx_crypto_ecdh_curve; - - /* Figure out the sizes of our keys and buffers. We need 4X the key size for our buffer space. */ - key_size = ecdh_ptr -> nx_crypto_ecdh_key_size; - - /* Make sure the remote public key is small enough to fit into the huge number buffer. */ - if (remote_public_key_len > 1 + 2 * key_size) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - /* Check to make sure the buffer is large enough to hold the shared secret key. */ - clen = (curve -> nx_crypto_ec_bits + 7) >> 3; - if (share_secret_key_len_ptr < clen) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - NX_CRYPTO_EC_POINT_INITIALIZE(&public_key, NX_CRYPTO_EC_POINT_AFFINE, scratch_buf_ptr, key_size); - NX_CRYPTO_EC_POINT_INITIALIZE(&shared_secret, NX_CRYPTO_EC_POINT_AFFINE, scratch_buf_ptr, key_size); - - - /* Copy the remote public key from the caller's buffer. */ - status = _nx_crypto_ec_point_setup(&public_key, remote_public_key, remote_public_key_len); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - -#ifndef NX_CRYPTO_ECC_DISABLE_KEY_VALIDATION - status = _nx_crypto_ec_validate_public_key(&public_key, curve, NX_CRYPTO_TRUE, scratch_buf_ptr); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } -#endif /* NX_CRYPTO_ECC_DISABLE_KEY_VALIDATION */ - - /* Private key buffer - note that no scratch is required for the private key, but we set it in case - it is needed in the future. */ - private_key.nx_crypto_huge_number_data = (HN_UBASE *)ecdh_ptr -> nx_crypto_ecdh_private_key_buffer; - private_key.nx_crypto_huge_number_size = key_size >> HN_SIZE_SHIFT; - private_key.nx_crypto_huge_buffer_size = key_size; - private_key.nx_crypto_huge_number_is_negative = NX_CRYPTO_FALSE; - - /* Finally, generate shared secret from the remote public key, our generated private key, and the curve. - The actual calculation is "shared_secret = private_key * public_key". */ - curve -> nx_crypto_ec_multiple(curve, &public_key, &private_key, &shared_secret, scratch_buf_ptr); - - /* The public key size is simply the key size for this group. */ - - /* Copy the shared secret into the return buffer. */ - status = _nx_crypto_huge_number_extract_fixed_size(&shared_secret.nx_crypto_ec_point_x, - share_secret_key_ptr, clen); - *actual_share_secret_key_len = clen; - - return(status); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_ecdh_init PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function is the common crypto method init callback for */ -/* Microsoft supported ECDH cryptographic algorithm. */ -/* */ -/* INPUT */ -/* */ -/* method Pointer to crypto method */ -/* key Pointer to key */ -/* key_size_in_bits Length of key size in bits */ -/* handler Returned crypto handler */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Size of the metadata area */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_ecdh_init(struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - VOID **handle, - VOID *crypto_metadata, - ULONG crypto_metadata_size) -{ - - NX_CRYPTO_STATE_CHECK - - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(handle); - - if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((((ULONG)crypto_metadata) & 0x3) != 0) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_ECDH)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_ecdh_cleanup PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function cleans up the crypto metadata. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Crypto metadata */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* NX_CRYPTO_MEMSET Set the memory */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_ecdh_cleanup(VOID *crypto_metadata) -{ - - NX_CRYPTO_STATE_CHECK - -#ifdef NX_SECURE_KEY_CLEAR - if (!crypto_metadata) - return (NX_CRYPTO_SUCCESS); - - /* Clean up the crypto metadata. */ - NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_ECDH)); -#else - NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); -#endif/* NX_SECURE_KEY_CLEAR */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_ecdh_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs an ECDH operation. */ -/* */ -/* INPUT */ -/* */ -/* op ECDH operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_ecdh_setup Setup local key pair */ -/* _nx_crypto_ecdh_compute_secret Compute shared secret */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_ecdh_operation(UINT op, - VOID *handle, - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, ULONG output_length_in_byte, - VOID *crypto_metadata, ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *, UINT)) -{ -NX_CRYPTO_ECDH *ecdh; -UINT status = NX_CRYPTO_SUCCESS; -NX_CRYPTO_EXTENDED_OUTPUT - *extended_output; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(output_length_in_byte); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_ECDH)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ecdh = (NX_CRYPTO_ECDH *)crypto_metadata; - - if (op == NX_CRYPTO_EC_CURVE_SET) - { - /* Set curve to be used in the ECDH. */ - status = ((NX_CRYPTO_METHOD *)input) -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_GET, - NX_CRYPTO_NULL, - (NX_CRYPTO_METHOD *)input, - NX_CRYPTO_NULL, 0, - NX_CRYPTO_NULL, 0, - NX_CRYPTO_NULL, - (UCHAR *)&ecdh -> nx_crypto_ecdh_curve, - sizeof(NX_CRYPTO_EC *), - NX_CRYPTO_NULL, 0, - NX_CRYPTO_NULL, NX_CRYPTO_NULL); - - } - else if (op == NX_CRYPTO_EC_KEY_PAIR_GENERATE) - { - if (ecdh -> nx_crypto_ecdh_curve == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - status = _nx_crypto_ec_key_pair_stream_generate(ecdh -> nx_crypto_ecdh_curve, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size, - ecdh -> nx_crypto_ecdh_scratch_buffer); - } - else if (op == NX_CRYPTO_DH_SETUP) - { - /* Setup local key pair. */ - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - status = _nx_crypto_ecdh_setup(ecdh, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size, - ecdh -> nx_crypto_ecdh_curve, - ecdh -> nx_crypto_ecdh_scratch_buffer); - } - else if (op == NX_CRYPTO_DH_KEY_PAIR_IMPORT) - { - if (key == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Import local key pair. */ - status = _nx_crypto_ecdh_key_pair_import(ecdh, ecdh -> nx_crypto_ecdh_curve, - key, (key_size_in_bits >> 3), - input, input_length_in_byte); - } - else if (op == NX_CRYPTO_DH_PRIVATE_KEY_EXPORT) - { - /* Export local private key. */ - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - status = _nx_crypto_ecdh_private_key_export(ecdh, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size); - } - else if (op == NX_CRYPTO_DH_CALCULATE) - { - /* Compute shared secret. */ - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - status = _nx_crypto_ecdh_compute_secret(ecdh, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size, - input, - input_length_in_byte, - ecdh -> nx_crypto_ecdh_scratch_buffer); - } - else - { - status = NX_CRYPTO_NOT_SUCCESSFUL; - } - - return(status); -} diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_ecdsa.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_ecdsa.c deleted file mode 100644 index ef4843d18..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_ecdsa.c +++ /dev/null @@ -1,898 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** Elliptic Curve Digital Signature Algorithm (ECDSA) */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - -#include "nx_crypto_ecdsa.h" - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_ecdsa_sign PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function computes a signature of the hash data using the */ -/* private key. */ -/* */ -/* */ -/* INPUT */ -/* */ -/* curve Curve used in the ECDSA */ -/* hash Hash data to be signed */ -/* hash_length Length of hash data */ -/* private_key Pointer to EC private key */ -/* signature Pointer to signature output */ -/* scratch Pointer to scratch buffer. */ -/* This scratch buffer can be */ -/* reused after this function */ -/* returns. */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_ec_key_pair_generation_extra */ -/* Generate EC Key Pair */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* verified memmove use cases, */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_ecdsa_sign(NX_CRYPTO_EC *curve, UCHAR *hash, UINT hash_length, - UCHAR *private_key, UINT private_key_length, - UCHAR *signature, ULONG signature_length, - ULONG *actual_signature_length, HN_UBASE *scratch) -{ -UINT status; -NX_CRYPTO_HUGE_NUMBER privkey; -NX_CRYPTO_HUGE_NUMBER z; -NX_CRYPTO_HUGE_NUMBER k; -NX_CRYPTO_HUGE_NUMBER ik; -NX_CRYPTO_HUGE_NUMBER temp; -NX_CRYPTO_EC_POINT pt; -UINT buffer_size = curve -> nx_crypto_ec_n.nx_crypto_huge_buffer_size; -UINT i; -UINT curve_size; -UINT r_size; -UINT s_size; -UCHAR pad_zero_r; -UCHAR pad_zero_s; -UINT sequence_size; -UCHAR *signature_r; -UCHAR *signature_s; - - /* Signature format follows ASN1 DER encoding as per RFC 4492, section 5.8: - * Size: 1 | 1 or 2 | 1 | 1 | 0 or 1 | N | 1 | 1 | 0 or 1 | M - * Data: SEQ | Size | INT | Size | 0x00 | r | INT | Size | 0x00 | s */ - - curve_size = curve -> nx_crypto_ec_bits >> 3; - if (curve -> nx_crypto_ec_bits & 7) - { - curve_size++; - } - - /* Check the signature_length for worst case. */ - if (signature_length < ((curve_size << 1) + 9)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - if (private_key_length > buffer_size) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&privkey, scratch, buffer_size); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&z, scratch, buffer_size); - /* Initialize per-message secret k buffer with 64 bits more buffer size. */ - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&k, scratch, buffer_size + 8); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&ik, scratch, buffer_size); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&temp, scratch, buffer_size * 2); - NX_CRYPTO_EC_POINT_INITIALIZE(&pt, NX_CRYPTO_EC_POINT_AFFINE, scratch, buffer_size); - - /* Copy the private key from the caller's buffer. */ - status = _nx_crypto_huge_number_setup(&privkey, private_key, private_key_length); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - - /* Truncate the hash data to the size of group order. */ - if (hash_length > buffer_size) - { - hash_length = buffer_size; - } - - status = _nx_crypto_huge_number_setup(&z, hash, hash_length); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - - /* The bit length of the hash data used is not longer than the group order. */ - if (curve -> nx_crypto_ec_bits < hash_length << 3) - { - _nx_crypto_huge_number_shift_right(&z, (hash_length << 3) - curve -> nx_crypto_ec_bits); - } - - do - { - do - { - /* Generate Key Pair. */ - _nx_crypto_ec_key_pair_generation_extra(curve, &curve -> nx_crypto_ec_g, &k, &pt, - scratch); - - /* Calculate r = pt.x mod n */ - _nx_crypto_huge_number_modulus(&pt.nx_crypto_ec_point_x, &curve -> nx_crypto_ec_n); - - } while (_nx_crypto_huge_number_is_zero(&pt.nx_crypto_ec_point_x)); - - /* Calculate s = k^-1 * (z + r * private_key) */ - _nx_crypto_huge_number_inverse_modulus(&k, &curve -> nx_crypto_ec_n, &ik, scratch); - _nx_crypto_huge_number_multiply(&pt.nx_crypto_ec_point_x, &privkey, &temp); - _nx_crypto_huge_number_add_unsigned(&temp, &z); - _nx_crypto_huge_number_modulus(&temp, &curve -> nx_crypto_ec_n); - NX_CRYPTO_HUGE_NUMBER_COPY(&k, &temp); - _nx_crypto_huge_number_multiply(&ik, &k, &temp); - _nx_crypto_huge_number_modulus(&temp, &curve -> nx_crypto_ec_n); - - } while (_nx_crypto_huge_number_is_zero(&temp)); - - /* Output r and s as two INTEGER in ASN.1 encoding */ - signature_r = signature + 3; - status = _nx_crypto_huge_number_extract(&pt.nx_crypto_ec_point_x, signature_r, (curve_size + 3), &r_size); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - - signature_s = signature + (curve_size + 6); - status = _nx_crypto_huge_number_extract(&temp, signature_s, (curve_size + 3), &s_size); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - - /* Trim prefix zeros. */ - for (i = 0; i < r_size; i++) - { - if (signature_r[i]) - { - - /* Loop until none zero byte. */ - break; - } - } - signature_r += i; - r_size -= i; - - /* The most significant bit must be zero to indicate positive integer. */ - /* Pad zero at the front if necessary. */ - pad_zero_r = (signature_r[0] & 0x80) ? 1 : 0; - - for (i = 0; i < s_size; i++) - { - if (signature_s[i]) - { - - /* Loop until none zero byte. */ - break; - } - } - signature_s += i; - s_size -= i; - - /* The most significant bit must be zero to indicate positive integer. */ - /* Pad zero at the front if necessary. */ - pad_zero_s = (signature_s[0] & 0x80) ? 1 : 0; - - /* Size of sequence. */ - sequence_size = r_size + pad_zero_r + s_size + pad_zero_s + 4; - - signature[0] = 0x30; /* SEQUENCE */ - if (sequence_size < 0x80) - { - signature[1] = (UCHAR)sequence_size; - signature += 2; - *actual_signature_length = sequence_size + 2; - } - else - { - signature[1] = 0x81; - signature[2] = (UCHAR)sequence_size; - signature += 3; - *actual_signature_length = sequence_size + 3; - } - - /* Setup r. */ - NX_CRYPTO_MEMMOVE(&signature[2 + pad_zero_r], signature_r, r_size); /* Use case of memmove is verified. */ - signature[0] = 0x02; /* Integer */ - signature[1] = (UCHAR)(r_size + pad_zero_r); - if (pad_zero_r) - { - signature[2] = 0; - } - signature += (2u + pad_zero_r + r_size); - - /* Setup s. */ - NX_CRYPTO_MEMMOVE(&signature[2 + pad_zero_s], signature_s, s_size); /* Use case of memmove is verified. */ - signature[0] = 0x02; /* Integer */ - signature[1] = (UCHAR)(s_size + pad_zero_s); - if (pad_zero_s) - { - signature[2] = 0; - } - - return NX_CRYPTO_SUCCESS; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_ecdsa_verify PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function verifies the signature of the hash data using the */ -/* public key. */ -/* */ -/* INPUT */ -/* */ -/* curve Curve used in the ECDSA */ -/* hash Hash data to be verified */ -/* hash_length Length of hash data */ -/* public_key Pointer to EC public key */ -/* signature Signature to be verified */ -/* scratch Pointer to scratch buffer. */ -/* This scratch buffer can be */ -/* reused after this function */ -/* returns. */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_huge_number_setup Generate private key */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), and */ -/* fixed input validation, */ -/* added public key validation,*/ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_ecdsa_verify(NX_CRYPTO_EC *curve, UCHAR *hash, UINT hash_length, - UCHAR *public_key, UINT public_key_length, - UCHAR *signature, UINT signature_length, HN_UBASE *scratch) -{ -UINT status; -NX_CRYPTO_HUGE_NUMBER r; -NX_CRYPTO_HUGE_NUMBER s; -NX_CRYPTO_HUGE_NUMBER z; -NX_CRYPTO_HUGE_NUMBER w; -NX_CRYPTO_HUGE_NUMBER u1; -NX_CRYPTO_HUGE_NUMBER u2; -NX_CRYPTO_EC_POINT pubkey; -NX_CRYPTO_EC_POINT pt; -NX_CRYPTO_EC_POINT pt2; -UINT buffer_size = curve -> nx_crypto_ec_n.nx_crypto_huge_buffer_size; - - /* Signature format follows ASN1 DER encoding as per RFC 4492, section 5.8: - * Size: 1 | 1 or 2 | 1 | 1 | 0 or 1 | N | 1 | 1 | 0 or 1 | M - * Data: SEQ | Size | INT | Size | 0x00 | r | INT | Size | 0x00 | s */ - - - if (public_key_length > 1 + (buffer_size << 1)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - /* Buffer should contain the signature data sequence. */ - if (signature[0] != 0x30) - { - return(NX_CRYPTO_AUTHENTICATION_FAILED); - } - - /* Check the size in SEQUENCE. */ - if (signature[1] & 0x80) - { - if (signature_length < (signature[2] + 3u)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - signature_length = signature[2]; - signature += 3; - } - else - { - if (signature_length < (signature[1] + 2u)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - signature_length = signature[1]; - signature += 2; - } - - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&r, scratch, buffer_size); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&s, scratch, buffer_size); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&z, scratch, buffer_size); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&w, scratch, buffer_size); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&u1, scratch, buffer_size << 1); - NX_CRYPTO_HUGE_NUMBER_INITIALIZE(&u2, scratch, buffer_size << 1); - NX_CRYPTO_EC_POINT_INITIALIZE(&pubkey, NX_CRYPTO_EC_POINT_AFFINE, scratch, buffer_size); - NX_CRYPTO_EC_POINT_INITIALIZE(&pt, NX_CRYPTO_EC_POINT_AFFINE, scratch, buffer_size); - NX_CRYPTO_EC_POINT_INITIALIZE(&pt2, NX_CRYPTO_EC_POINT_AFFINE, scratch, buffer_size); - - /* Copy the public key from the caller's buffer. */ - status = _nx_crypto_ec_point_setup(&pubkey, public_key, public_key_length); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - -#ifndef NX_CRYPTO_ECC_DISABLE_KEY_VALIDATION - status = _nx_crypto_ec_validate_public_key(&pubkey, curve, NX_CRYPTO_FALSE, scratch); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } -#endif /* NX_CRYPTO_ECC_DISABLE_KEY_VALIDATION */ - - if (signature_length < (signature[1] + 2u)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - /* Read r value from input signature. */ - status = _nx_crypto_huge_number_setup(&r, &signature[2], signature[1]); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - signature_length -= (signature[1] + 2u); - signature += signature[1] + 2; - - if (signature_length < (signature[1] + 2u)) - { - return(NX_CRYPTO_SIZE_ERROR); - } - - /* Read s value from input signature. */ - status = _nx_crypto_huge_number_setup(&s, &signature[2], signature[1]); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - - /* r and s must be in the range [1..n-1] */ - if(_nx_crypto_huge_number_is_zero(&r) || _nx_crypto_huge_number_is_zero(&s)) - { - return(NX_CRYPTO_NOT_SUCCESSFUL); - } - - if(NX_CRYPTO_HUGE_NUMBER_LESS != _nx_crypto_huge_number_compare_unsigned(&r, &curve -> nx_crypto_ec_n)) - { - return(NX_CRYPTO_NOT_SUCCESSFUL); - } - - if(NX_CRYPTO_HUGE_NUMBER_LESS != _nx_crypto_huge_number_compare_unsigned(&s, &curve -> nx_crypto_ec_n)) - { - return(NX_CRYPTO_NOT_SUCCESSFUL); - } - - /* Truncate the hash data to the size of group order. */ - if (hash_length > buffer_size) - { - hash_length = buffer_size; - } - - status = _nx_crypto_huge_number_setup(&z, hash, hash_length); - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - - if (curve -> nx_crypto_ec_bits < hash_length << 3) - { - _nx_crypto_huge_number_shift_right(&z, (hash_length << 3) - curve -> nx_crypto_ec_bits); - } - - /* Calculate w = s^-1 mod n */ - _nx_crypto_huge_number_inverse_modulus(&s, &curve -> nx_crypto_ec_n, &w, scratch); - - /* Calculate u1 = zw mod n */ - _nx_crypto_huge_number_multiply(&z, &w, &u1); - _nx_crypto_huge_number_modulus(&u1, &curve -> nx_crypto_ec_n); - - /* Calculate u2 = rw mod n */ - _nx_crypto_huge_number_multiply(&r, &w, &u2); - _nx_crypto_huge_number_modulus(&u2, &curve -> nx_crypto_ec_n); - - /* Calculate (x1,y1) = u1*G + u2*public_key */ - curve -> nx_crypto_ec_multiple(curve, &curve -> nx_crypto_ec_g, &u1, &pt, scratch); - curve -> nx_crypto_ec_multiple(curve, &pubkey, &u2, &pt2, scratch); - - curve -> nx_crypto_ec_add(curve, &pt, &pt2, scratch); - - _nx_crypto_huge_number_modulus(&pt.nx_crypto_ec_point_x, &curve -> nx_crypto_ec_n); - - /* Check r == x1 mod n */ - if (NX_CRYPTO_HUGE_NUMBER_EQUAL != _nx_crypto_huge_number_compare_unsigned(&pt.nx_crypto_ec_point_x, &r)) - { - return NX_CRYPTO_NOT_SUCCESSFUL; - } - return NX_CRYPTO_SUCCESS; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_ecdsa_init PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function is the common crypto method init callback for */ -/* Microsoft supported ECDSA cryptographic algorithm. */ -/* */ -/* INPUT */ -/* */ -/* method Pointer to crypto method */ -/* key Pointer to key */ -/* key_size_in_bits Length of key size in bits */ -/* handler Returned crypto handler */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Size of the metadata area */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_ecdsa_init(struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - VOID **handle, - VOID *crypto_metadata, - ULONG crypto_metadata_size) -{ -NX_CRYPTO_ECDSA *ecdsa; - - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(handle); - - NX_CRYPTO_STATE_CHECK - - if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((((ULONG)crypto_metadata) & 0x3) != 0) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_ECDSA)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ecdsa = (NX_CRYPTO_ECDSA *)crypto_metadata; - - /* Reset ECDSA metadata. */ - ecdsa -> nx_crypto_ecdsa_curve = NX_CRYPTO_NULL; - ecdsa -> nx_crypto_ecdsa_hash_method = NX_CRYPTO_NULL; - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_ecdsa_cleanup PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function cleans up the crypto metadata. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Crypto metadata */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* NX_CRYPTO_MEMSET Set the memory */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_ecdsa_cleanup(VOID *crypto_metadata) -{ - - NX_CRYPTO_STATE_CHECK - -#ifdef NX_SECURE_KEY_CLEAR - if (!crypto_metadata) - return (NX_CRYPTO_SUCCESS); - - /* Clean up the crypto metadata. */ - NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_ECDSA)); -#else - NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); -#endif/* NX_SECURE_KEY_CLEAR */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_ecdsa_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs an ECDSA operation. */ -/* */ -/* INPUT */ -/* */ -/* op ECDSA operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_ecdsa_sign Sign using ECDSA */ -/* _nx_crypto_ecdsa_verify Verify ECDSA signature */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_ecdsa_operation(UINT op, - VOID *handle, - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, ULONG output_length_in_byte, - VOID *crypto_metadata, ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *, UINT)) -{ -NX_CRYPTO_ECDSA *ecdsa; -UINT status = NX_CRYPTO_SUCCESS; -NX_CRYPTO_EXTENDED_OUTPUT - *extended_output; -NX_CRYPTO_METHOD *hash_method; -VOID *hash_handler = NX_CRYPTO_NULL; -UCHAR *hash_output = NX_CRYPTO_NULL; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_ECDSA)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ecdsa = (NX_CRYPTO_ECDSA *)crypto_metadata; - - if (op == NX_CRYPTO_EC_CURVE_SET) - { - status = ((NX_CRYPTO_METHOD *)input) -> nx_crypto_operation(NX_CRYPTO_EC_CURVE_GET, - NX_CRYPTO_NULL, - (NX_CRYPTO_METHOD *)input, - NX_CRYPTO_NULL, 0, - NX_CRYPTO_NULL, 0, - NX_CRYPTO_NULL, - (UCHAR *)&ecdsa -> nx_crypto_ecdsa_curve, - sizeof(NX_CRYPTO_METHOD *), - NX_CRYPTO_NULL, 0, - NX_CRYPTO_NULL, NX_CRYPTO_NULL); - - if (status) - { - return(status); - } - } - else if (op == NX_CRYPTO_AUTHENTICATE) - { - if ((key == NX_CRYPTO_NULL) || (ecdsa -> nx_crypto_ecdsa_curve == NX_CRYPTO_NULL)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - - status = _nx_crypto_ecdsa_sign(ecdsa -> nx_crypto_ecdsa_curve, - input, - input_length_in_byte, - key, - key_size_in_bits >> 3, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size, - ecdsa -> nx_crypto_ecdsa_scratch_buffer); - } - else if (op == NX_CRYPTO_VERIFY) - { - if (key == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - status = _nx_crypto_ecdsa_verify(ecdsa->nx_crypto_ecdsa_curve, - input, - input_length_in_byte, - key, - key_size_in_bits >> 3, - output, output_length_in_byte, - ecdsa -> nx_crypto_ecdsa_scratch_buffer); - } - else if (op == NX_CRYPTO_EC_KEY_PAIR_GENERATE) - { - if (ecdsa->nx_crypto_ecdsa_curve == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - status = _nx_crypto_ec_key_pair_stream_generate(ecdsa->nx_crypto_ecdsa_curve, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size, - ecdsa -> nx_crypto_ecdsa_scratch_buffer); - } - else if (op == NX_CRYPTO_HASH_METHOD_SET) - { - - /* Setup hash method used by ECDSA. */ - hash_method = (NX_CRYPTO_METHOD *)input; - - /* ECDSA scratch buffer shares with metadata of hash method. - * Check the size required by metadata of hash method. */ - if ((hash_method -> nx_crypto_metadata_area_size + - (hash_method -> nx_crypto_ICV_size_in_bits >> 3)) > - sizeof(ecdsa -> nx_crypto_ecdsa_scratch_buffer)) - { - status = NX_CRYPTO_SIZE_ERROR; - } - else - { - ecdsa -> nx_crypto_ecdsa_hash_method = hash_method; - } - } - else if ((op == NX_CRYPTO_SIGNATURE_GENERATE) || (op == NX_CRYPTO_SIGNATURE_VERIFY)) - { - hash_method = ecdsa -> nx_crypto_ecdsa_hash_method; - if (hash_method == NX_CRYPTO_NULL) - { - - /* Hash method is not set successfully. */ - status = NX_CRYPTO_PTR_ERROR; - } - else - { - - /* Put the hash at the end of scratch buffer. */ - hash_output = (UCHAR *)(ecdsa -> nx_crypto_ecdsa_scratch_buffer) + - (sizeof(ecdsa -> nx_crypto_ecdsa_scratch_buffer) - (hash_method -> nx_crypto_ICV_size_in_bits >> 3)); - - /* First, calculate hash value of input message. */ - if (hash_method -> nx_crypto_init) - { - status = hash_method -> nx_crypto_init(hash_method, - NX_CRYPTO_NULL, - 0, - &hash_handler, - ecdsa -> nx_crypto_ecdsa_scratch_buffer, - hash_method -> nx_crypto_metadata_area_size); - } - - if (status == NX_CRYPTO_SUCCESS) - { - status = hash_method -> nx_crypto_operation(NX_CRYPTO_AUTHENTICATE, - NX_CRYPTO_NULL, - hash_method, - NX_CRYPTO_NULL, - 0, - input, - input_length_in_byte, - NX_CRYPTO_NULL, - hash_output, - (hash_method -> nx_crypto_ICV_size_in_bits >> 3), - ecdsa -> nx_crypto_ecdsa_scratch_buffer, - hash_method -> nx_crypto_metadata_area_size, - NX_CRYPTO_NULL, NX_CRYPTO_NULL); - - if (status != NX_CRYPTO_SUCCESS) - { - return(status); - } - } - - if (hash_method -> nx_crypto_cleanup) - { - status = hash_method -> nx_crypto_cleanup(ecdsa -> nx_crypto_ecdsa_scratch_buffer); - } - } - - if (status == NX_CRYPTO_SUCCESS) - { - - /* Second, generate/verify signature. */ - if ((key == NX_CRYPTO_NULL) || (ecdsa -> nx_crypto_ecdsa_curve == NX_CRYPTO_NULL)) - { - status = NX_CRYPTO_PTR_ERROR; - } - else if (op == NX_CRYPTO_SIGNATURE_GENERATE) - { - - /* Signature generation. */ - extended_output = (NX_CRYPTO_EXTENDED_OUTPUT *)output; - - status = _nx_crypto_ecdsa_sign(ecdsa -> nx_crypto_ecdsa_curve, - hash_output, - (hash_method -> nx_crypto_ICV_size_in_bits >> 3), - key, - key_size_in_bits >> 3, - extended_output -> nx_crypto_extended_output_data, - extended_output -> nx_crypto_extended_output_length_in_byte, - &extended_output -> nx_crypto_extended_output_actual_size, - ecdsa -> nx_crypto_ecdsa_scratch_buffer); - } - else - { - - /* Signature verification. */ - status = _nx_crypto_ecdsa_verify(ecdsa->nx_crypto_ecdsa_curve, - hash_output, - (hash_method -> nx_crypto_ICV_size_in_bits >> 3), - key, - key_size_in_bits >> 3, - output, output_length_in_byte, - ecdsa -> nx_crypto_ecdsa_scratch_buffer); - } - } - } - else - { - status = NX_CRYPTO_NOT_SUCCESSFUL; - } - - return(status); -} diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_initialize.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_initialize.c deleted file mode 100644 index 73f36e9c1..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_initialize.c +++ /dev/null @@ -1,379 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** Crypto Initialization */ -/** */ -/**************************************************************************/ -/**************************************************************************/ -#define _NX_CRYPTO_INITIALIZE_ - -#include "nx_crypto.h" - - -#ifdef NX_CRYPTO_FIPS - -/* Include necessary system files. */ - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_fips_memcpy PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs memory copy function for the FIPS 140-2 */ -/* compliance build. */ -/* */ -/* INPUT */ -/* */ -/* dest Pointer to the destination */ -/* memory */ -/* value value (in byte) to set to the */ -/* memory location */ -/* size Number of bytes to copy */ -/* */ -/* OUTPUT */ -/* */ -/* void * Pointer to the destination */ -/* memory */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP VOID *_nx_crypto_fips_memcpy(void *dest, const void *src, size_t size) -{ - char *from, *to; - unsigned int i; - - from = (char*)src; - to = (char*)dest; - - for(i = 0; i < size; i++) - { - to[i] = from[i]; - } - - return dest; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_fips_memset PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs memory set function for the FIPS 140-2 */ -/* compliance build. */ -/* */ -/* INPUT */ -/* */ -/* dest Pointer to the destination */ -/* memory */ -/* value value (in byte) to set to the */ -/* memory location */ -/* size Number of bytes to set */ -/* */ -/* OUTPUT */ -/* */ -/* void * Pointer to the destination */ -/* memory */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP VOID *_nx_crypto_fips_memset(void *dest, int value, size_t size) -{ - char *to; - unsigned int i; - char v; - - to = (char*)dest; - v = (char)(value & 0xFF); - for(i = 0; i < size; i++) - { - to[i] = v; - } - - return dest; -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_fips_memcmp PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs memory comparison function for the */ -/* FIPS 140-2 compliance build. */ -/* */ -/* INPUT */ -/* */ -/* str1 Pointer to the first string */ -/* for the comparison. */ -/* str2 Pointer to the second string */ -/* for the comparison. */ -/* size Number of bytes to compare. */ -/* */ -/* OUTPUT */ -/* */ -/* Result 0: str1 and str2 are the */ -/* same. */ -/* 0: str1 and str2 are */ -/* different. */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP int _nx_crypto_fips_memcmp(const void *str1, const void *str2, size_t size) -{ - char *string1; - char *string2; - unsigned int i; - - string1 = (char*)str1; - string2 = (char*)str2; - for(i = 0; i < size; i++) - { - if(*string1++ != *string2++) - return(1); - } - - return(0); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_fips_memmove PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs memory move function for the FIPS 140-2 */ -/* compliance build. */ -/* */ -/* INPUT */ -/* */ -/* dest Pointer to the destination */ -/* memory */ -/* src Pointer to the source memory */ -/* size Number of bytes to move */ -/* */ -/* OUTPUT */ -/* */ -/* void * Pointer to the destination */ -/* memory */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP void* _nx_crypto_fips_memmove(void *dest, const void *src, size_t size) -{ - char *from, *to; - unsigned int i; - - from = (char*)src; - to = (char*)dest; - - if((ULONG)dest < (ULONG)src) - { - for(i = 0; i < size; i++) - { - to[i] = from[i]; - } - } - else if((ULONG)dest > (ULONG)src) - { - - for(i = size; i != 0; i--) - to[i - 1] = from[i - 1]; - } - return(dest); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_module_state_get PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function retrieves the current crypto library state. */ -/* */ -/* INPUT */ -/* */ -/* None */ -/* */ -/* OUTPUT */ -/* */ -/* status The bitmap of the current */ -/* status. Valid bits are: */ -/* NX_CRYPTO_LIBRARY_STATE_UNINITIALIZED */ -/* NX_CRYPTO_LIBRARY_STATE_POST_IN_PROGRESS */ -/* NX_CRYPTO_LIBRARY_STATE_POST_FAILED */ -/* NX_CRYPTO_LIBRARY_STATE_OPERATIONAL */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_module_state_get(VOID) -{ - return(_nx_crypto_library_state); - -} - -#endif - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_initialize PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function initializes the NetX Crypto module. */ -/* */ -/* INPUT */ -/* */ -/* None */ -/* */ -/* OUTPUT */ -/* */ -/* status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_initialize(VOID) -{ - - - return(NX_CRYPTO_SUCCESS); - -} diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_rsa.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_rsa.c deleted file mode 100644 index f15c350db..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_rsa.c +++ /dev/null @@ -1,453 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** RSA public-key encryption algorithm */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - -#include "nx_crypto_rsa.h" -#include "nx_crypto_huge_number.h" - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_rsa_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function performs an RSA encryption/decryption operation - */ -/* for RSA the operation is the same but with different values */ -/* for the exponent. */ -/* */ -/* The output is always the same length as the modulus. */ -/* */ -/* If NULL is passed for the scratch buffer pointer, an internal */ -/* scratch buffer is used. */ -/* */ -/* INPUT */ -/* */ -/* exponent RSA exponent */ -/* exponent_length Length of exponent in bytes */ -/* modulus RSA modulus */ -/* modulus_length Length of modulus in bytes */ -/* p RSA prime p */ -/* p_length Length of p in bytes */ -/* q RSA prime q */ -/* q_length Length of q in bytes */ -/* input Input data */ -/* input_length Length of input in bytes */ -/* output Output buffer */ -/* scratch_buf_ptr Pointer to scratch buffer */ -/* scratch_buf_length Length of scratch buffer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_huge_number_setup Setup huge number */ -/* _nx_crypto_huge_number_crt_power_modulus */ -/* Raise a huge number for CRT */ -/* _nx_crypto_huge_number_mont_power_modulus */ -/* Raise a huge number for */ -/* montgomery reduction */ -/* _nx_crypto_huge_number_extract Extract huge number */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_rsa_operation Handle RSA operation */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_rsa_operation(const UCHAR *exponent, UINT exponent_length, const UCHAR *modulus, UINT modulus_length, - const UCHAR *p, UINT p_length, UCHAR *q, UINT q_length, - const UCHAR *input, UINT input_length, UCHAR *output, - USHORT *scratch_buf_ptr, UINT scratch_buf_length) -{ -UCHAR *scratch; -UINT mod_length; -NX_CRYPTO_HUGE_NUMBER modulus_hn, exponent_hn, input_hn, output_hn, p_hn, q_hn; - - NX_CRYPTO_PARAMETER_NOT_USED(scratch_buf_length); - - /* The RSA operation is reversible so both encryption and decryption can be done with the same operation. */ - /* Local pointer for pointer arithmetic. */ - scratch = (UCHAR *)scratch_buf_ptr; - - /* Set up each of the buffers - point into the scratch buffer at increments of the DH buffer size. */ - modulus_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch; - scratch += modulus_length; - modulus_hn.nx_crypto_huge_buffer_size = modulus_length; - - /* Input buffer(and scratch). */ - input_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch; - scratch += modulus_length; - input_hn.nx_crypto_huge_buffer_size = modulus_length; - - /* Exponent buffer (and scratch). */ - exponent_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch; - scratch += modulus_length; - exponent_hn.nx_crypto_huge_buffer_size = modulus_length; - - /* Output buffer (and scratch). */ - output_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch; - scratch += modulus_length * 2; - output_hn.nx_crypto_huge_buffer_size = modulus_length * 2; - - /* Copy the exponent from the caller's buffer. */ - _nx_crypto_huge_number_setup(&exponent_hn, exponent, exponent_length); - - /* Copy the input from the caller's buffer. */ - _nx_crypto_huge_number_setup(&input_hn, input, input_length); - - /* Copy the modulus from the caller's buffer. */ - _nx_crypto_huge_number_setup(&modulus_hn, modulus, modulus_length); - - if (p && q) - { - - p_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch; - scratch += (modulus_length >> 1); - p_hn.nx_crypto_huge_buffer_size = (modulus_length >> 1); - - q_hn.nx_crypto_huge_number_data = (HN_UBASE *)scratch; - scratch += (modulus_length >> 1); - q_hn.nx_crypto_huge_buffer_size = (modulus_length >> 1); - - /* Copy the prime p and q from the caller's buffer. */ - _nx_crypto_huge_number_setup(&p_hn, p, p_length); - _nx_crypto_huge_number_setup(&q_hn, q, q_length); - - /* Finally, generate shared secret from the remote public key, our generated private key, and the modulus, modulus. - The actual calculation is "shared_secret = (public_key**private_key) % modulus" - where the "**" denotes exponentiation. */ - _nx_crypto_huge_number_crt_power_modulus(&input_hn, &exponent_hn, &p_hn, &q_hn, - &modulus_hn, &output_hn, - (HN_UBASE *)scratch); - } - else - { - - /* Finally, generate shared secret from the remote public key, our generated private key, and the modulus, modulus. - The actual calculation is "shared_secret = (public_key**private_key) % modulus" - where the "**" denotes exponentiation. */ - _nx_crypto_huge_number_mont_power_modulus(&input_hn, &exponent_hn, &modulus_hn, - &output_hn, (HN_UBASE *)scratch); - } - - /* Copy the shared secret into the return buffer. */ - _nx_crypto_huge_number_extract(&output_hn, output, modulus_length, &mod_length); - - return(NX_CRYPTO_SUCCESS); -} - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_rsa_init PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function initializes the modulus for RSA context. */ -/* */ -/* INPUT */ -/* */ -/* method Pointer to RSA crypto method */ -/* key Pointer to modulus */ -/* key_size_in_bits Length of modulus in bits */ -/* handle Handle of method */ -/* crypto_metadata Pointer to RSA context */ -/* crypto_metadata_size Size of RSA context */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_rsa_init(struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - VOID **handle, - VOID *crypto_metadata, - ULONG crypto_metadata_size) -{ -NX_CRYPTO_RSA *ctx; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - - NX_CRYPTO_STATE_CHECK - - if ((method == NX_CRYPTO_NULL) || (key == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((((ULONG)crypto_metadata) & 0x3) != 0) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_RSA)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_RSA *)crypto_metadata; - - ctx -> nx_crypto_rsa_modulus = key; - ctx -> nx_crypto_rsa_modulus_length = key_size_in_bits >> 3; - ctx -> nx_crypto_rsa_prime_p = NX_CRYPTO_NULL; - ctx -> nx_crypto_rsa_prime_p_length = 0; - ctx -> nx_crypto_rsa_prime_q = NX_CRYPTO_NULL; - ctx -> nx_crypto_rsa_prime_q_length = 0; - - /* Call _nx_crypto_crypto_rsa_set_prime() to set p and q for private key. - * Chinese Remainder Theorem will be used when p and q are set. */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_rsa_cleanup PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function cleans up the crypto metadata. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Crypto metadata */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* NX_CRYPTO_MEMSET Set the memory */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_rsa_cleanup(VOID *crypto_metadata) -{ - - NX_CRYPTO_STATE_CHECK - -#ifdef NX_SECURE_KEY_CLEAR - if (!crypto_metadata) - return (NX_CRYPTO_SUCCESS); - - /* Clean up the crypto metadata. */ - NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_RSA)); -#else - NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); -#endif/* NX_SECURE_KEY_CLEAR */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_rsa_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function is the RSA operation function for crypto method. */ -/* */ -/* INPUT */ -/* */ -/* op Operation */ -/* handle Handle to method */ -/* method Pointer to RSA crypto method */ -/* key Exponent of RSA operation */ -/* key_size_in_bits Size of exponent in bits */ -/* input Input stream */ -/* input_length_in_byte Length of input in byte */ -/* iv_ptr Initial Vector (not used) */ -/* output Output stream */ -/* output_length_in_byte Length of output in byte */ -/* crypto_metadata Pointer to RSA context */ -/* crypto_metadata_size Size of RSA context */ -/* packet_ptr Pointer to packet (not used) */ -/* nx_crypto_hw_process_callback Pointer to callback function */ -/* (not used) */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_rsa_operation Perform RSA operation */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_rsa_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ -NX_CRYPTO_RSA *ctx; -UINT return_value = NX_CRYPTO_SUCCESS; - - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_RSA)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_RSA *)crypto_metadata; - - - if (op == NX_CRYPTO_SET_PRIME_P) - { - ctx -> nx_crypto_rsa_prime_p = input; - ctx -> nx_crypto_rsa_prime_p_length = input_length_in_byte; - } - else if (op == NX_CRYPTO_SET_PRIME_Q) - { - ctx -> nx_crypto_rsa_prime_q = input; - ctx -> nx_crypto_rsa_prime_q_length = input_length_in_byte; - } - else - { - - if (key == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(output_length_in_byte < (key_size_in_bits >> 3)) - return(NX_CRYPTO_INVALID_BUFFER_SIZE); - - if (input_length_in_byte > (ctx -> nx_crypto_rsa_modulus_length)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - return_value = _nx_crypto_rsa_operation(key, - key_size_in_bits >> 3, - ctx -> nx_crypto_rsa_modulus, - ctx -> nx_crypto_rsa_modulus_length, - ctx -> nx_crypto_rsa_prime_p, - ctx -> nx_crypto_rsa_prime_p_length, - ctx -> nx_crypto_rsa_prime_q, - ctx -> nx_crypto_rsa_prime_q_length, - input, input_length_in_byte, - output, - ctx -> nx_crypto_rsa_scratch_buffer, - NX_CRYPTO_RSA_SCRATCH_BUFFER_SIZE); - } - - return(return_value); -} - diff --git a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_sha2.c b/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_sha2.c deleted file mode 100644 index 4434cb6c0..000000000 --- a/ra/microsoft/azure-rtos/netxduo/crypto_libraries/src/nx_crypto_sha2.c +++ /dev/null @@ -1,786 +0,0 @@ -/**************************************************************************/ -/* */ -/* Copyright (c) Microsoft Corporation. All rights reserved. */ -/* */ -/* This software is licensed under the Microsoft Software License */ -/* Terms for Microsoft Azure RTOS. Full text of the license can be */ -/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ -/* and in the root directory of this software. */ -/* */ -/**************************************************************************/ - - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** NetX Crypto Component */ -/** */ -/** SHA-256 Digest Algorithm (SHA2) */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - -#include "nx_crypto_sha2.h" - -/* Constants used in the SHA-256 digest calculation. */ -const ULONG _sha2_round_constants[64] = -{ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 -}; - - -/* Define the SHA2 logic functions. */ -#define CH_FUNC(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) -#define MAJ_FUNC(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) - -#define RIGHT_SHIFT_CIRCULAR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) -#define LARGE_SIGMA_0(x) (RIGHT_SHIFT_CIRCULAR((x), 2) ^ RIGHT_SHIFT_CIRCULAR((x), 13) ^ RIGHT_SHIFT_CIRCULAR((x), 22)) -#define LARGE_SIGMA_1(x) (RIGHT_SHIFT_CIRCULAR((x), 6) ^ RIGHT_SHIFT_CIRCULAR((x), 11) ^ RIGHT_SHIFT_CIRCULAR((x), 25)) -#define SMALL_SIGMA_0(x) (RIGHT_SHIFT_CIRCULAR((x), 7) ^ RIGHT_SHIFT_CIRCULAR((x), 18) ^ ((x) >> 3)) -#define SMALL_SIGMA_1(x) (RIGHT_SHIFT_CIRCULAR((x), 17) ^ RIGHT_SHIFT_CIRCULAR((x), 19) ^ ((x) >> 10)) - -/* Define the padding array. This is used to pad the message such that its length is - 64 bits shy of being a multiple of 512 bits long. */ -const UCHAR _nx_crypto_sha256_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_sha256_initialize PORTABLE C */ -/* 6.1 */ -/* */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function initializes the SHA256 context. It must be called */ -/* prior to creating a SHA256 digest. */ -/* */ -/* INPUT */ -/* */ -/* context SHA256 context pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_sha256_operation Handle SHA256 operation */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_sha256_initialize(NX_CRYPTO_SHA256 *context, UINT algorithm ) -{ - /* Determine if the context is non-null. */ - if (context == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* First, clear the bit count for this context. */ - context -> nx_sha256_bit_count[0] = 0; /* Clear the lower 32-bits of the count.*/ - context -> nx_sha256_bit_count[1] = 0; /* Clear the upper 32-bits of the count.*/ - - if ((algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256) || - (algorithm == NX_CRYPTO_HASH_SHA256)) - { - - /* Initialize SHA-256 state. */ - context -> nx_sha256_states[0] = 0x6a09e667; /* A H0 */ - context -> nx_sha256_states[1] = 0xbb67ae85; /* B H1 */ - context -> nx_sha256_states[2] = 0x3c6ef372; /* C H2 */ - context -> nx_sha256_states[3] = 0xa54ff53a; /* D H3 */ - context -> nx_sha256_states[4] = 0x510e527f; /* E H4 */ - context -> nx_sha256_states[5] = 0x9b05688c; /* F H5 */ - context -> nx_sha256_states[6] = 0x1f83d9ab; /* G H6 */ - context -> nx_sha256_states[7] = 0x5be0cd19; /* H H7 */ - } - else - { - - /* Initialize SHA-224 state. */ - context -> nx_sha256_states[0] = 0xc1059ed8; /* A H0 */ - context -> nx_sha256_states[1] = 0x367cd507; /* B H1 */ - context -> nx_sha256_states[2] = 0x3070dd17; /* C H2 */ - context -> nx_sha256_states[3] = 0xf70e5939; /* D H3 */ - context -> nx_sha256_states[4] = 0xffc00b31; /* E H4 */ - context -> nx_sha256_states[5] = 0x68581511; /* F H5 */ - context -> nx_sha256_states[6] = 0x64f98fa7; /* G H6 */ - context -> nx_sha256_states[7] = 0xbefa4fa4; /* H H7 */ - } - - /* Return success. */ - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_sha256_update PORTABLE C */ -/* 6.1 */ -/* */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function updates the SHA256 digest with new input from the */ -/* caller. */ -/* */ -/* INPUT */ -/* */ -/* context SHA256 context pointer */ -/* input_ptr Pointer to input data */ -/* input_length Number of bytes in input */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_sha256_process_buffer Process complete buffer */ -/* using SHA256 */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_sha256_digest_calculate Calculate the SHA256 digest */ -/* _nx_crypto_method_sha256_operation Handle SHA256 operation */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* verified memcpy use cases, */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_sha256_update(NX_CRYPTO_SHA256 *context, UCHAR *input_ptr, UINT input_length) -{ -ULONG current_bytes; -ULONG needed_fill_bytes; - - /* Determine if the context is non-null. */ - if (context == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Determine if there is a length. */ - if (input_length == 0) - { - return(NX_CRYPTO_SUCCESS); - } - - /* Calculate the current byte count mod 64. Note the reason for the - shift by 3 is to account for the 8 bits per byte. */ - current_bytes = (context -> nx_sha256_bit_count[0] >> 3) & 0x3F; - - /* Calculate the current number of bytes needed to be filled. */ - needed_fill_bytes = 64 - current_bytes; - - /* Update the total bit count based on the input length. */ - context -> nx_sha256_bit_count[0] += (input_length << 3); - - /* Determine if there is roll-over of the bit count into the MSW. */ - if (context -> nx_sha256_bit_count[0] < (input_length << 3)) - { - - /* Yes, increment the MSW of the bit count. */ - context -> nx_sha256_bit_count[1]++; - } - - /* Update upper total bit count word. */ - context -> nx_sha256_bit_count[1] += (input_length >> 29); - - /* Check for a partial buffer that needs to be transformed. */ - if ((current_bytes) && (input_length >= needed_fill_bytes)) - { - /* Yes, we can complete the buffer and transform it. */ - - /* Copy the appropriate portion of the input buffer into the internal - buffer of the context. */ - NX_CRYPTO_MEMCPY((void *)&(context -> nx_sha256_buffer[current_bytes]), (void *)input_ptr, needed_fill_bytes); /* Use case of memcpy is verified. */ - - /* Process the 64-byte (512 bit) buffer. */ - _nx_crypto_sha256_process_buffer(context, context -> nx_sha256_buffer); - - /* Adjust the pointers and length accordingly. */ - input_length = input_length - needed_fill_bytes; - input_ptr = input_ptr + needed_fill_bytes; - - /* Clear the remaining bits, since the buffer was processed. */ - current_bytes = 0; - } - - /* Process any and all whole blocks of input. */ - while (input_length >= 64) - { - - /* Process this 64-byte (512 bit) buffer. */ - _nx_crypto_sha256_process_buffer(context, input_ptr); - - /* Adjust the pointers and length accordingly. */ - input_length = input_length - 64; - input_ptr = input_ptr + 64; - } - - /* Determine if there is anything left. */ - if (input_length) - { - /* Save the remaining bytes in the internal buffer after any remaining bytes - so that it is processed later. */ - NX_CRYPTO_MEMCPY((void *)&(context -> nx_sha256_buffer[current_bytes]), (void *)input_ptr, input_length); /* Use case of memcpy is verified. */ - } - - /* Return success. */ - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_sha256_digest_calculate PORTABLE C */ -/* 6.1 */ -/* */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function calculates the final SHA256 digest. It is called */ -/* when there is no more input for the digest and returns the 32-byte */ -/* (256-bit) SHA256 digest to the caller. */ -/* */ -/* INPUT */ -/* */ -/* context SHA256 context pointer */ -/* digest Pointer to return buffer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_sha256_update Final update to the digest */ -/* with padding and length */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_method_sha256_operation Handle SHA256 operation */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_sha256_digest_calculate(NX_CRYPTO_SHA256 *context, UCHAR *digest, UINT algorithm) -{ -UCHAR bit_count_string[8]; -ULONG current_byte_count; -ULONG padding_bytes; - - - /* Move the lower portion of the bit count into the array. */ - bit_count_string[0] = (UCHAR)(context -> nx_sha256_bit_count[1] >> 24); - bit_count_string[1] = (UCHAR)(context -> nx_sha256_bit_count[1] >> 16); - bit_count_string[2] = (UCHAR)(context -> nx_sha256_bit_count[1] >> 8); - bit_count_string[3] = (UCHAR)(context -> nx_sha256_bit_count[1]); - bit_count_string[4] = (UCHAR)(context -> nx_sha256_bit_count[0] >> 24); - bit_count_string[5] = (UCHAR)(context -> nx_sha256_bit_count[0] >> 16); - bit_count_string[6] = (UCHAR)(context -> nx_sha256_bit_count[0] >> 8); - bit_count_string[7] = (UCHAR)(context -> nx_sha256_bit_count[0]); - - /* Calculate the current byte count. */ - current_byte_count = (context -> nx_sha256_bit_count[0] >> 3) & 0x3F; - - /* Calculate the padding bytes needed. */ - padding_bytes = (current_byte_count < 56) ? (56 - current_byte_count) : (120 - current_byte_count); - - /* Add any padding required. */ - _nx_crypto_sha256_update(context, (UCHAR*)_nx_crypto_sha256_padding, padding_bytes); - - /* Add the in the length. */ - _nx_crypto_sha256_update(context, bit_count_string, 8); - - /* Now store the digest in the caller specified destination. */ - digest[0] = (UCHAR)(context -> nx_sha256_states[0] >> 24); - digest[1] = (UCHAR)(context -> nx_sha256_states[0] >> 16); - digest[2] = (UCHAR)(context -> nx_sha256_states[0] >> 8); - digest[3] = (UCHAR)(context -> nx_sha256_states[0]); - digest[4] = (UCHAR)(context -> nx_sha256_states[1] >> 24); - digest[5] = (UCHAR)(context -> nx_sha256_states[1] >> 16); - digest[6] = (UCHAR)(context -> nx_sha256_states[1] >> 8); - digest[7] = (UCHAR)(context -> nx_sha256_states[1]); - digest[8] = (UCHAR)(context -> nx_sha256_states[2] >> 24); - digest[9] = (UCHAR)(context -> nx_sha256_states[2] >> 16); - digest[10] = (UCHAR)(context -> nx_sha256_states[2] >> 8); - digest[11] = (UCHAR)(context -> nx_sha256_states[2]); - digest[12] = (UCHAR)(context -> nx_sha256_states[3] >> 24); - digest[13] = (UCHAR)(context -> nx_sha256_states[3] >> 16); - digest[14] = (UCHAR)(context -> nx_sha256_states[3] >> 8); - digest[15] = (UCHAR)(context -> nx_sha256_states[3]); - digest[16] = (UCHAR)(context -> nx_sha256_states[4] >> 24); - digest[17] = (UCHAR)(context -> nx_sha256_states[4] >> 16); - digest[18] = (UCHAR)(context -> nx_sha256_states[4] >> 8); - digest[19] = (UCHAR)(context -> nx_sha256_states[4]); - digest[20] = (UCHAR)(context -> nx_sha256_states[5] >> 24); - digest[21] = (UCHAR)(context -> nx_sha256_states[5] >> 16); - digest[22] = (UCHAR)(context -> nx_sha256_states[5] >> 8); - digest[23] = (UCHAR)(context -> nx_sha256_states[5]); - digest[24] = (UCHAR)(context -> nx_sha256_states[6] >> 24); - digest[25] = (UCHAR)(context -> nx_sha256_states[6] >> 16); - digest[26] = (UCHAR)(context -> nx_sha256_states[6] >> 8); - digest[27] = (UCHAR)(context -> nx_sha256_states[6]); - if ((algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256) || - (algorithm == NX_CRYPTO_HASH_SHA256)) - { - digest[28] = (UCHAR)(context -> nx_sha256_states[7] >> 24); - digest[29] = (UCHAR)(context -> nx_sha256_states[7] >> 16); - digest[30] = (UCHAR)(context -> nx_sha256_states[7] >> 8); - digest[31] = (UCHAR)(context -> nx_sha256_states[7]); - } - -#ifdef NX_SECURE_KEY_CLEAR - NX_CRYPTO_MEMSET(bit_count_string, 0, sizeof(bit_count_string)); -#endif /* NX_SECURE_KEY_CLEAR */ - - /* Return successful completion. */ - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_sha256_process_buffer PORTABLE C */ -/* 6.1 */ -/* */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function implements the SHA256 algorithm which works on */ -/* 64-byte (512-bit) blocks of data. */ -/* */ -/* INPUT */ -/* */ -/* context SHA256 context pointer */ -/* buffer Pointer to 64-byte buffer */ -/* */ -/* OUTPUT */ -/* */ -/* None */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* _nx_crypto_sha256_update Final update to the digest */ -/* with padding and length */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP VOID _nx_crypto_sha256_process_buffer(NX_CRYPTO_SHA256 *context, UCHAR buffer[64]) -{ -ULONG *w; -UINT t; -ULONG temp1, temp2; -ULONG a, b, c, d, e, f, g, h; - - - /* Setup pointers to the word array. */ - w = context -> nx_sha256_word_array; - - /* Initialize the first 16 words of the word array, taking care of the - endian issues at the same time. */ - for (t = 0; t < 16; t++) - { - /* Setup each entry. */ - w[t] = (((ULONG)buffer[t * 4]) << 24) | (((ULONG)buffer[(t * 4) + 1]) << 16) | (((ULONG)buffer[(t * 4) + 2]) << 8) | ((ULONG)buffer[(t * 4) + 3]); - } - - /* Setup the remaining entries of the word array. */ - for (t = 16; t < 64; t++) - { - /* Setup each entry. */ - w[t] = SMALL_SIGMA_1(w[t - 2]) + w[t - 7] + SMALL_SIGMA_0(w[t - 15]) + w[t - 16]; - } - - /* Initialize the state variables. */ - a = context -> nx_sha256_states[0]; - b = context -> nx_sha256_states[1]; - c = context -> nx_sha256_states[2]; - d = context -> nx_sha256_states[3]; - e = context -> nx_sha256_states[4]; - f = context -> nx_sha256_states[5]; - g = context -> nx_sha256_states[6]; - h = context -> nx_sha256_states[7]; - - /* Now, perform Round operations. */ - for (t = 0; t < 64; t++) - { - temp1 = h + LARGE_SIGMA_1(e) + CH_FUNC(e, f, g) + _sha2_round_constants[t] + w[t]; - temp2 = LARGE_SIGMA_0(a) + MAJ_FUNC(a, b, c); - h = g; - g = f; - f = e; - e = d + temp1; - d = c; - c = b; - b = a; - a = temp1 + temp2; - } - - /* Save the resulting in this SHA256 context. */ - context -> nx_sha256_states[0] += a; - context -> nx_sha256_states[1] += b; - context -> nx_sha256_states[2] += c; - context -> nx_sha256_states[3] += d; - context -> nx_sha256_states[4] += e; - context -> nx_sha256_states[5] += f; - context -> nx_sha256_states[6] += g; - context -> nx_sha256_states[7] += h; - -#ifdef NX_SECURE_KEY_CLEAR - a = 0; b = 0; c = 0; d = 0; - e = 0; f = 0; g = 0; h = 0; - temp1 = 0; temp2 = 0; -#endif /* NX_SECURE_KEY_CLEAR */ -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_sha256_init PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function is the common crypto method init callback for */ -/* Microsoft supported SHA256 cryptographic algorithm. */ -/* */ -/* INPUT */ -/* */ -/* method Pointer to crypto method */ -/* key Pointer to key */ -/* key_size_in_bits Length of key size in bits */ -/* handler Returned crypto handler */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Size of the metadata area */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* None */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_sha256_init(struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, NX_CRYPTO_KEY_SIZE key_size_in_bits, - VOID **handle, - VOID *crypto_metadata, - ULONG crypto_metadata_size) -{ - - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(handle); - - NX_CRYPTO_STATE_CHECK - - if ((method == NX_CRYPTO_NULL) || (crypto_metadata == NX_CRYPTO_NULL)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((((ULONG)crypto_metadata) & 0x3) != 0) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_SHA256)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_sha256_cleanup PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function cleans up the crypto metadata. */ -/* */ -/* INPUT */ -/* */ -/* crypto_metadata Crypto metadata */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* NX_CRYPTO_MEMSET Set the memory */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_sha256_cleanup(VOID *crypto_metadata) -{ - - NX_CRYPTO_STATE_CHECK - -#ifdef NX_SECURE_KEY_CLEAR - if (!crypto_metadata) - return (NX_CRYPTO_SUCCESS); - - /* Clean up the crypto metadata. */ - NX_CRYPTO_MEMSET(crypto_metadata, 0, sizeof(NX_CRYPTO_SHA256)); -#else - NX_CRYPTO_PARAMETER_NOT_USED(crypto_metadata); -#endif/* NX_SECURE_KEY_CLEAR */ - - return(NX_CRYPTO_SUCCESS); -} - - -/**************************************************************************/ -/* */ -/* FUNCTION RELEASE */ -/* */ -/* _nx_crypto_method_sha256_operation PORTABLE C */ -/* 6.1 */ -/* AUTHOR */ -/* */ -/* Timothy Stapko, Microsoft Corporation */ -/* */ -/* DESCRIPTION */ -/* */ -/* This function encrypts and decrypts a message using */ -/* the SHA256 algorithm. */ -/* */ -/* INPUT */ -/* */ -/* op SHA256 operation */ -/* handle Crypto handle */ -/* method Cryption Method Object */ -/* key Encryption Key */ -/* key_size_in_bits Key size in bits */ -/* input Input data */ -/* input_length_in_byte Input data size */ -/* iv_ptr Initial vector */ -/* output Output buffer */ -/* output_length_in_byte Output buffer size */ -/* crypto_metadata Metadata area */ -/* crypto_metadata_size Metadata area size */ -/* packet_ptr Pointer to packet */ -/* nx_crypto_hw_process_callback Callback function pointer */ -/* */ -/* OUTPUT */ -/* */ -/* status Completion status */ -/* */ -/* CALLS */ -/* */ -/* _nx_crypto_sha256_initialize Initialize the SHA256 context */ -/* _nx_crypto_sha256_update Update the digest with padding*/ -/* and length of digest */ -/* _nx_crypto_sha256_digest_calculate Calculate the SHA256 digest */ -/* */ -/* CALLED BY */ -/* */ -/* Application Code */ -/* */ -/* RELEASE HISTORY */ -/* */ -/* DATE NAME DESCRIPTION */ -/* */ -/* 05-19-2020 Timothy Stapko Initial Version 6.0 */ -/* 09-30-2020 Timothy Stapko Modified comment(s), */ -/* resulting in version 6.1 */ -/* */ -/**************************************************************************/ -NX_CRYPTO_KEEP UINT _nx_crypto_method_sha256_operation(UINT op, /* Encrypt, Decrypt, Authenticate */ - VOID *handle, /* Crypto handler */ - struct NX_CRYPTO_METHOD_STRUCT *method, - UCHAR *key, - NX_CRYPTO_KEY_SIZE key_size_in_bits, - UCHAR *input, - ULONG input_length_in_byte, - UCHAR *iv_ptr, - UCHAR *output, - ULONG output_length_in_byte, - VOID *crypto_metadata, - ULONG crypto_metadata_size, - VOID *packet_ptr, - VOID (*nx_crypto_hw_process_callback)(VOID *packet_ptr, UINT status)) -{ -NX_CRYPTO_SHA256 *ctx; - - NX_CRYPTO_PARAMETER_NOT_USED(handle); - NX_CRYPTO_PARAMETER_NOT_USED(key); - NX_CRYPTO_PARAMETER_NOT_USED(key_size_in_bits); - NX_CRYPTO_PARAMETER_NOT_USED(iv_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(packet_ptr); - NX_CRYPTO_PARAMETER_NOT_USED(nx_crypto_hw_process_callback); - - NX_CRYPTO_STATE_CHECK - - if (method == NX_CRYPTO_NULL) - { - return(NX_CRYPTO_PTR_ERROR); - } - - /* Verify the metadata addrsss is 4-byte aligned. */ - if((crypto_metadata == NX_CRYPTO_NULL) || ((((ULONG)crypto_metadata) & 0x3) != 0)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - if(crypto_metadata_size < sizeof(NX_CRYPTO_SHA256)) - { - return(NX_CRYPTO_PTR_ERROR); - } - - ctx = (NX_CRYPTO_SHA256 *)crypto_metadata; - - if ((method -> nx_crypto_algorithm != NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256) && - (method -> nx_crypto_algorithm != NX_CRYPTO_HASH_SHA224) && - (method -> nx_crypto_algorithm != NX_CRYPTO_HASH_SHA256)) - { - /* Incorrect method. */ - return(NX_CRYPTO_NOT_SUCCESSFUL); - } - - switch (op) - { - case NX_CRYPTO_HASH_INITIALIZE: - _nx_crypto_sha256_initialize((NX_CRYPTO_SHA256 *)crypto_metadata, method -> nx_crypto_algorithm); - break; - - case NX_CRYPTO_HASH_UPDATE: - _nx_crypto_sha256_update((NX_CRYPTO_SHA256 *)crypto_metadata, input, input_length_in_byte); - break; - - case NX_CRYPTO_HASH_CALCULATE: - if ((method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256) || - (method -> nx_crypto_algorithm == NX_CRYPTO_HASH_SHA256)) - { - if(output_length_in_byte < 32) - return(NX_CRYPTO_INVALID_BUFFER_SIZE); - } - else if(output_length_in_byte < 28) - return(NX_CRYPTO_INVALID_BUFFER_SIZE); - _nx_crypto_sha256_digest_calculate((NX_CRYPTO_SHA256 *)crypto_metadata, output, - method -> nx_crypto_algorithm); - break; - - default: - if ((method -> nx_crypto_algorithm == NX_CRYPTO_AUTHENTICATION_HMAC_SHA2_256) || - (method -> nx_crypto_algorithm == NX_CRYPTO_HASH_SHA256)) - { - if(output_length_in_byte < 32) - return(NX_CRYPTO_INVALID_BUFFER_SIZE); - } - else if(output_length_in_byte < 28) - return(NX_CRYPTO_INVALID_BUFFER_SIZE); - - _nx_crypto_sha256_initialize(ctx, method -> nx_crypto_algorithm); - _nx_crypto_sha256_update(ctx, input, input_length_in_byte); - _nx_crypto_sha256_digest_calculate(ctx, output, method -> nx_crypto_algorithm); - break; - } - - return NX_CRYPTO_SUCCESS; -} -