/* * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and * its subsidiaries and affiliates (collectly called MKSEMI). * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into an MKSEMI * integrated circuit in a product or a software update for such product, * must reproduce the above copyright notice, this list of conditions and * the following disclaimer in the documentation and/or other materials * provided with the distribution. * * 3. Neither the name of MKSEMI nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * 4. This software, with or without modification, must only be used with a * MKSEMI integrated circuit. * * 5. Any software provided in binary form under this license must not be * reverse engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef MK_AES_H_ #define MK_AES_H_ #include "mk_common.h" #include "mk_dma.h" #ifndef AES_INT_MODE_EN #define AES_INT_MODE_EN (1) #endif #ifndef AES_DMA_MODE_EN #define AES_DMA_MODE_EN (1) #endif #ifndef AES_POLL_MODE_EN #define AES_POLL_MODE_EN (1) #endif /** * @addtogroup MK8000_AES * @{ */ /** * @brief AES device IDs enumeration */ enum AES_DEV_T { AES_ID0 = 0, AES_MAX_NUM }; /** * @brief AES algorithm mode enumeration */ enum AES_MODE_T { AES_MODE_ECB = 0, /*!< Electronic codebook chaining algorithm */ AES_MODE_CBC, /*!< Cipher block chaining algorithm */ AES_MODE_CTR, /*!< Counter mode chaining algorithm */ AES_MODE_CCM, /*!< Counter with Cipher Mode */ AES_MODE_DKEY, /*!< Calculate decipher key for ECB and CBC */ }; /** * @brief AES work mode enumeration */ enum AES_DIR_T { AES_DIR_ENCRYPT = 0, AES_DIR_DECRYPT, }; /** * @brief AES CCM mode enumeration */ enum AES_CCM_MODE { AES_CCM_ENCRYPT_AND_AUTH = 0, /*!< CCM encryption and authentication */ AES_CCM_DECRYPT_AND_AUTH, /*!< CCM decryption and authentication */ AES_CCM_STAR_ENCRYPT_AND_AUTH, /*!< CCM* encryption and authentication */ AES_CCM_STAR_DECRYPT_AND_AUTH, /*!< CCM* decryption and authentication */ }; /** * @brief AES state enumeration */ enum AES_STATE_T { AES_STATE_RESET = 0x00U, /*!< AES not yet initialized or disabled */ AES_STATE_READY = 0x01U, /*!< AES initialized and ready for use */ AES_STATE_BREAK = 0x02U, AES_STATE_CONFLICT = 0x04U, AES_STATE_BUSY = 0x10U, /*!< AES internal processing is ongoing */ AES_STATE_TIMEOUT = 0x40U, AES_STATE_ERROR = 0x80U, /*!< AES error state */ }; /** * @brief AES CCM authentication checking result enumeration */ enum AES_MIN_CHE_T { AES_MIN_VALID = 1, /*!< MACTag verification is valid */ AES_MIN_INVALID = 0, /*!< MACTag verification is invalid */ }; /** * @brief AES configure Structure */ struct AES_CFG_T { uint8_t key[16]; /*!< The key used for encryption/decryption */ uint8_t int_en; /*!< Specifies whether the interrupt is enabled or disabled This parameter will be one of the following values: @arg true is enable @arg false is disable */ uint8_t dma_en; /*!< Specifies whether the dma is enabled or disabled. This parameter shoule be one of the following values: @arg true is enable @arg false is disable */ uint8_t din_burst_size; /*!< Specifies the Burst transfer configuration for the input memory. This parameter can be a value of @ref DMA_SRC_BURST_SIZE_T */ uint8_t dout_burst_size; /*!< Specifies the Burst transfer configuration for the output memory. This parameter can be a value of @ref DMA_SRC_BURST_SIZE_T */ }; #define MK_AES_CCM_ERROR_MAC_SIZE 1 /*!< The MAC (Message Authentication Code) size is invalid. */ #define MK_AES_CCM_ERROR_NONCE_SIZE 2 /*!< The nonce size is invalid. */ #define MK_AES_CCM_ERROR_INPUT_BUF_NOT_ENOUGH 3 /*!< Input buffer space is too small. */ #define MK_AES_CCM_ERROR_OUTPUT_BUF_NOT_ENOUGH 4 /*!< Output buffer space is too small. */ #define MK_AES_CCM_ERROR_ADATA_LEN_NOT_SUPPORT 5 /*!< Additional authentication data is too long. */ /** * @brief AES CCM context Structure */ struct AES_CCM_CONTEXT_T { uint8_t mac_size; /*!< MAC (Message Authentication Code) byte size. Valid values for supported follow: CCM [4, 6, 8, 10, 12, 14, 16] CCM* [0, 4, 8, 16] */ uint8_t nonce_size; /*!< Nonce byte size. Valid values for supported follow: CCM [7 ... 13] CCM* [13] */ const uint8_t *nonce; /*!< Pointer to nonce. */ const uint8_t *adata; /*!< Pointer to additional authenticated data */ const uint8_t *data_in; /*!< Pointer to the input data for encryption or decryption */ uint8_t *input_buf; /*!< Pointer to the input data buffer, This buffer is used to temporarily store verification data and plaintext data or to store verification data and ciphertext data. The buffer size must be greater or equal than the sum of adata_len + data_in_len + 18 */ uint8_t *output_buf; /*!< Pointer to the output buffer where encrypted or decrypted data will be stored If encrypted, the length of the buffer is at least the sum of data_in_len + mac_size If decrypted, the length of the buffer is at least data_in_len - mac_size */ uint32_t input_buf_size; /*!< Length of Input data buffer in bytes (input_buf_size >= adata_len + data_in_len + 18) */ uint32_t output_buf_size; /*!< Length of output data buffer in bytes: Encrypt mode: output_buf_size >= data_in_len + mac_size Decrypt mode: output_buf_size >= data_in_len */ uint16_t adata_len; /*!< Length of additional authenticated data in bytes For MK8000, adata_len shall be limited to 65517 bytes */ uint16_t data_in_len; /*!< Length of encryption or decryption data in bytes For MK8000, adata_len shall be limited to 65535 bytes */ }; /** * @brief AES handle Structure */ struct AES_HANDLE_T { AES_TypeDef *const base; /*!< AES registers base address */ const IRQn_Type irq; /*!< AES interrupt number */ enum DMA_CH_T dma_in_ch; /*!< AES data input DMA channel */ enum DMA_CH_T dma_out_ch; /*!< AES data output DMA channel */ enum AES_STATE_T state; /*!< AES state */ __IOM enum AES_MIN_CHE_T is_valid; /*!< AES CCM authentication checking result */ struct AES_CFG_T config; /*!< AES configure Structure */ uint8_t *input; /*!< Pointer to AES encryption or decryption input buffer */ uint8_t *output; /*!< Pointer to AES encryption or decryption output buffer */ uint16_t input_len; /*!< Length of input data */ uint16_t output_len; /*!< Length of output data */ __IOM uint16_t input_count; /*!< Counter of input data */ __IOM uint16_t output_count; /*!< Counter of output data */ drv_callback_t callback; /*!< Callback function provided by the user */ }; #ifdef __cplusplus extern "C" { #endif /** * @brief Function for initializing the AES. * * @param[in] id AES device instance ID. * @param[in] config Pointer to a AES_CFG_T structure that contains the configuration information for AES. * If this cfg is NULL, this function will use the default config. * @return * @arg DEV_ERROR error id * @arg DEV_OK succeed */ int aes_open(enum AES_DEV_T id, struct AES_CFG_T *config); /** * @brief Function for uninitializing the AES. * * @param[in] id AES device instance ID. * @return * @arg DEV_ERROR error id * @arg DEV_OK succeed */ int aes_close(enum AES_DEV_T id); /** * @brief Function for restoring the AES busy state to the ready state. * * @param[in] id AES device instance ID. * @return * @arg DEV_OK succeed */ int aes_clear(enum AES_DEV_T id); /** * @brief Function for updating the key. * * @param[in] id AES device instance ID. * @param[in] key Array to store keys. * @return * @arg DEV_OK succeed */ int aes_update_key(enum AES_DEV_T id, uint8_t key[16]); /** * @brief Function for setting AES state * * @param[in] id AES device instance ID. * @param[in] state AES state value to be set. */ void aes_set_state(enum AES_DEV_T id, enum AES_STATE_T state); /** * @brief Function for clearing AES state * * @param[in] id AES device instance ID. * @param[in] state AES state value to clear. */ void aes_clr_state(enum AES_DEV_T id, enum AES_STATE_T state); /** * @brief Function for getting the current state of AES. * * @param[in] id AES device instance ID. * @return * This result can be a value of @ref AES_STATE_T */ uint8_t aes_get_state(enum AES_DEV_T id); /** * @brief AES encrypts or decrypts data in AES ECB mode. * * @param[in] id AES device instance ID. * @param[in] mode AES encryption and decryption mode settings. * This parameter can be a value of @ref AES_DIR_T * @param[in] input Pointer to the input (plaintext or cyphertext) buffer. * @param[out] output Pointer to the output (plaintext or cyphertext) buffer. * @param[in] len Length of input and output data (in bytes), must be a multiple of 16. * @param[in] callback Callback function provided by the user. * @return * @arg DEV_ERROR error id * @arg DRV_BUSY AES internal processing is ongoing * @arg DEV_OK AES operation is successful */ int aes_crypt_ecb(enum AES_DEV_T id, enum AES_DIR_T mode, uint8_t *input, uint8_t *output, uint16_t len, drv_callback_t callback); /** * @brief AES encrypts or decrypts data in AES CBC mode. * * @param[in] id AES device instance ID. * @param[in] mode AES encryption and decryption mode settings. * This parameter can be a value of @ref AES_DIR_T * @param[in] iv Array to store IV. * @param[in] input Pointer to the input (plaintext or cyphertext) buffer. * @param[out] output Pointer to the output (plaintext or cyphertext) buffer. * @param[in] len Length of input and output data (in bytes), must be a multiple of 16. * @param[in] callback Callback function provided by the user. * @return * @arg DEV_ERROR error id * @arg DRV_BUSY AES internal processing is ongoing * @arg DEV_OK AES operation is successful */ int aes_crypt_cbc(enum AES_DEV_T id, enum AES_DIR_T mode, uint8_t iv[16], uint8_t *input, uint8_t *output, uint16_t len, drv_callback_t callback); /** * @brief AES encrypts or decrypts data in AES CTR mode. * * @param[in] id AES device instance ID. * @param[in] initial_counter Array to store initial counter. * @param[in] input Pointer to the input (plaintext or cyphertext) buffer. * @param[out] output Pointer to the output (plaintext or cyphertext) buffer. * @param[in] len Length of input and output data (in bytes), must be a multiple of 16. * @param[in] callback Callback function provided by the user. * @return * @arg DEV_ERROR error id * @arg DRV_BUSY AES internal processing is ongoing * @arg DEV_OK AES operation is successful */ int aes_crypt_ctr(enum AES_DEV_T id, uint8_t initial_counter[16], uint8_t *input, uint8_t *output, uint16_t len, drv_callback_t callback); /** * @brief AES encrypts or decrypts data in AES CCM mode. * * @param[in] id AES device instance ID. * @param[in] opt AES CCM mode enumeration. * @param[in] cxt Pointer to struct AES_CCM_CONTEXT_T @ref AES_CCM_CONTEXT_T. * @param[in] callback Callback function provided by the user. * @return * @arg DEV_ERROR error id * @arg DRV_BUSY AES internal processing is ongoing * @arg DEV_OK AES operation is successful */ int aes_crypt_ccm(enum AES_DEV_T id, enum AES_CCM_MODE opt, struct AES_CCM_CONTEXT_T *cxt, drv_callback_t callback); /** * @brief Get AES CCM authentication checking result * * @param[in] id AES device instance ID. * @return * @ref AES_MIN_CHE_T * @arg AES_MIN_VALID is valid * @arg AES_MIN_INVALID is invalid */ enum AES_MIN_CHE_T aes_ccm_dec_mic_isvalid(enum AES_DEV_T id); /** * @brief Function for Interrupt handler for AES. */ void AES_IRQHandler(void); #ifdef __cplusplus } #endif /** * @} */ #endif /* MK_AES_H_ */