1 /* 2 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /****************************************************************************** 18 * @file drv_aes.h 19 * @brief Header File for AES Driver 20 * @version V1.0 21 * @date 02. June 2017 22 ******************************************************************************/ 23 #ifndef _CSI_AES_H_ 24 #define _CSI_AES_H_ 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #include <stdint.h> 31 #include <drv_common.h> 32 #include <drv_errno.h> 33 34 35 /// definition for aes handle. 36 typedef void *aes_handle_t; 37 38 /****** AES specific error codes *****/ 39 typedef enum { 40 AES_ERROR_MODE = (EDRV_SPECIFIC + 1) , ///< Specified Mode not supported 41 AES_ERROR_DATA_BITS , ///< Specified number of Data bits not supported 42 AES_ERROR_ENDIAN ///< Specified endian not supported 43 } drv_aes_error_e; 44 45 /*----- AES Control Codes: Mode -----*/ 46 typedef enum { 47 AES_MODE_ECB = 0, ///< ECB Mode 48 AES_MODE_CBC , ///< CBC Mode 49 AES_MODE_CFB , ///< CFB Mode 50 AES_MODE_OFB , ///< OFB Mode 51 AES_MODE_CTR ///< CTR Mode 52 } aes_mode_e; 53 54 /*----- AES Control Codes: Crypto Mode -----*/ 55 typedef enum { 56 AES_CRYPTO_MODE_ENCRYPT = 0, ///< encrypt Mode 57 AES_CRYPTO_MODE_DECRYPT , ///< decrypt Mode 58 } aes_crypto_mode_e; 59 60 /*----- AES Control Codes: Padding Mode -----*/ 61 typedef enum { 62 AES_PADDING_MODE_NO = 0, ///< NO-PADDING 63 AES_PADDING_MODE_ZERO , ///< ZERO-PADDING 64 AES_PADDING_MODE_PKCS5 ///< PKCS5-PADDING 65 } aes_padding_mode_e; 66 67 /*----- AES Control Codes: Mode Parameters: Key length -----*/ 68 typedef enum { 69 AES_KEY_LEN_BITS_128 = 0, ///< 128 Data bits 70 AES_KEY_LEN_BITS_192 , ///< 192 Data bits 71 AES_KEY_LEN_BITS_256 ///< 256 Data bits 72 } aes_key_len_bits_e; 73 74 /*----- AES Control Codes: Mode Parameters: Endian -----*/ 75 typedef enum { 76 AES_ENDIAN_LITTLE = 0, ///< Little Endian 77 AES_ENDIAN_BIG ///< Big Endian 78 } aes_endian_mode_e; 79 80 /** 81 \brief AES Status 82 */ 83 typedef struct { 84 uint32_t busy : 1; ///< busy flag 85 } aes_status_t; 86 87 /****** AES Event *****/ 88 typedef enum { 89 AES_EVENT_CRYPTO_COMPLETE = 0 ///< Encrypt completed 90 } aes_event_e; 91 typedef void (*aes_event_cb_t)(aes_event_e event); ///< Pointer to \ref aes_event_cb_t : AES Event call back. 92 93 94 /** 95 \brief AES Device Driver Capabilities. 96 */ 97 typedef struct { 98 uint32_t ecb_mode : 1; ///< supports ECB mode 99 uint32_t cbc_mode : 1; ///< supports CBC mode 100 uint32_t cfb_mode : 1; ///< supports CFB mode 101 uint32_t ofb_mode : 1; ///< supports OFB mode 102 uint32_t ctr_mode : 1; ///< supports CTR mode 103 uint32_t bits_128 : 1; ///< supports 128bits key length 104 uint32_t bits_192 : 1; ///< supports 192bits key length 105 uint32_t bits_256 : 1; ///< supports 256bits key length 106 } aes_capabilities_t; 107 108 109 // Function documentation 110 111 /** 112 \brief get aes instance count. 113 \return aes handle count 114 */ 115 int32_t csi_aes_get_instance_count(void); 116 117 /** 118 \brief Initialize AES Interface. 1. Initializes the resources needed for the AES interface 2.registers event callback function 119 \param[in] idx must not exceed return value of csi_aes_get_instance_count(). 120 \param[in] cb_event Pointer to \ref aes_event_cb_t 121 \return return aes handle if success 122 */ 123 aes_handle_t csi_aes_initialize(int32_t idx, aes_event_cb_t cb_event); 124 125 /** 126 \brief De-initialize AES Interface. stops operation and releases the software resources used by the interface 127 \param[in] handle aes handle to operate. 128 \return error code 129 */ 130 int32_t csi_aes_uninitialize(aes_handle_t handle); 131 132 /** 133 \brief Get driver capabilities. 134 \param[in] handle aes handle to operate. 135 \return \ref aes_capabilities_t 136 */ 137 aes_capabilities_t csi_aes_get_capabilities(aes_handle_t handle); 138 139 /** 140 \brief config aes mode. 141 \param[in] handle aes handle to operate. 142 \param[in] mode \ref aes_mode_e 143 \param[in] keylen_bits \ref aes_key_len_bits_e 144 \param[in] endian \ref aes_endian_mode_e 145 \param[in] arg Pointer to the iv address when mode is cbc_mode 146 \return error code 147 */ 148 int32_t csi_aes_config(aes_handle_t handle, 149 aes_mode_e mode, 150 aes_key_len_bits_e keylen_bits, 151 aes_endian_mode_e endian, 152 uint32_t arg 153 ); 154 155 /** 156 \brief set crypto key. 157 \param[in] handle aes handle to operate. 158 \param[in] context aes information context(NULL when hardware implementation) 159 \param[in] key Pointer to the key buf 160 \param[in] key_len the key len 161 \param[in] enc \ref aes_crypto_mode_e 162 \return error code 163 */ 164 int32_t csi_aes_set_key(aes_handle_t handle, void *context, void *key, uint32_t key_len, aes_crypto_mode_e enc); 165 166 /** 167 \brief encrypt or decrypt 168 \param[in] handle aes handle to operate. 169 \param[in] context aes information context(NULL when hardware implementation) 170 \param[in] in Pointer to the Source data 171 \param[out] out Pointer to the Result data. 172 \param[in] len the Source data len. 173 \param[in] padding \ref aes_padding_mode_e. 174 \return error code 175 */ 176 int32_t csi_aes_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, aes_padding_mode_e padding); 177 178 /** 179 \brief Get AES status. 180 \param[in] handle aes handle to operate. 181 \return AES status \ref aes_status_t 182 */ 183 aes_status_t csi_aes_get_status(aes_handle_t handle); 184 185 #ifdef __cplusplus 186 } 187 #endif 188 189 #endif /* _CSI_AES_H_ */ 190