drv_aes.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @file drv_aes.h
  18. * @brief Header File for AES Driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_AES_H_
  23. #define _CSI_AES_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <drv_common.h>
  29. #include <drv_errno.h>
  30. /// definition for aes handle.
  31. typedef void *aes_handle_t;
  32. /****** AES specific error codes *****/
  33. typedef enum {
  34. AES_ERROR_MODE = (EDRV_SPECIFIC + 1) , ///< Specified Mode not supported
  35. AES_ERROR_DATA_BITS , ///< Specified number of Data bits not supported
  36. AES_ERROR_ENDIAN ///< Specified endian not supported
  37. } drv_aes_error_e;
  38. /*----- AES Control Codes: Mode -----*/
  39. typedef enum {
  40. AES_MODE_ECB = 0, ///< ECB Mode
  41. AES_MODE_CBC , ///< CBC Mode
  42. AES_MODE_CFB , ///< CFB Mode
  43. AES_MODE_OFB , ///< OFB Mode
  44. AES_MODE_CTR ///< CTR Mode
  45. } aes_mode_e;
  46. /*----- AES Control Codes: Crypto Mode -----*/
  47. typedef enum {
  48. AES_CRYPTO_MODE_ENCRYPT = 0, ///< encrypt Mode
  49. AES_CRYPTO_MODE_DECRYPT , ///< decrypt Mode
  50. } aes_crypto_mode_e;
  51. /*----- AES Control Codes: Padding Mode -----*/
  52. typedef enum {
  53. AES_PADDING_MODE_NO = 0, ///< NO-PADDING
  54. AES_PADDING_MODE_ZERO , ///< ZERO-PADDING
  55. AES_PADDING_MODE_PKCS5 ///< PKCS5-PADDING
  56. } aes_padding_mode_e;
  57. /*----- AES Control Codes: Mode Parameters: Key length -----*/
  58. typedef enum {
  59. AES_KEY_LEN_BITS_128 = 0, ///< 128 Data bits
  60. AES_KEY_LEN_BITS_192 , ///< 192 Data bits
  61. AES_KEY_LEN_BITS_256 ///< 256 Data bits
  62. } aes_key_len_bits_e;
  63. /*----- AES Control Codes: Mode Parameters: Endian -----*/
  64. typedef enum {
  65. AES_ENDIAN_LITTLE = 0, ///< Little Endian
  66. AES_ENDIAN_BIG ///< Big Endian
  67. } aes_endian_mode_e;
  68. /**
  69. \brief AES Status
  70. */
  71. typedef struct {
  72. uint32_t busy : 1; ///< busy flag
  73. } aes_status_t;
  74. /****** AES Event *****/
  75. typedef enum {
  76. AES_EVENT_CRYPTO_COMPLETE = 0 ///< Encrypt completed
  77. } aes_event_e;
  78. typedef void (*aes_event_cb_t)(aes_event_e event); ///< Pointer to \ref aes_event_cb_t : AES Event call back.
  79. /**
  80. \brief AES Device Driver Capabilities.
  81. */
  82. typedef struct {
  83. uint32_t ecb_mode : 1; ///< supports ECB mode
  84. uint32_t cbc_mode : 1; ///< supports CBC mode
  85. uint32_t cfb_mode : 1; ///< supports CFB mode
  86. uint32_t ofb_mode : 1; ///< supports OFB mode
  87. uint32_t ctr_mode : 1; ///< supports CTR mode
  88. uint32_t bits_128 : 1; ///< supports 128bits key length
  89. uint32_t bits_192 : 1; ///< supports 192bits key length
  90. uint32_t bits_256 : 1; ///< supports 256bits key length
  91. } aes_capabilities_t;
  92. // Function documentation
  93. /**
  94. \brief get aes instance count.
  95. \return aes handle count
  96. */
  97. int32_t csi_aes_get_instance_count(void);
  98. /**
  99. \brief Initialize AES Interface. 1. Initializes the resources needed for the AES interface 2.registers event callback function
  100. \param[in] idx must not exceed return value of csi_aes_get_instance_count().
  101. \param[in] cb_event Pointer to \ref aes_event_cb_t
  102. \return return aes handle if success
  103. */
  104. aes_handle_t csi_aes_initialize(int32_t idx, aes_event_cb_t cb_event);
  105. /**
  106. \brief De-initialize AES Interface. stops operation and releases the software resources used by the interface
  107. \param[in] handle aes handle to operate.
  108. \return error code
  109. */
  110. int32_t csi_aes_uninitialize(aes_handle_t handle);
  111. /**
  112. \brief Get driver capabilities.
  113. \param[in] handle aes handle to operate.
  114. \return \ref aes_capabilities_t
  115. */
  116. aes_capabilities_t csi_aes_get_capabilities(aes_handle_t handle);
  117. /**
  118. \brief config aes mode.
  119. \param[in] handle aes handle to operate.
  120. \param[in] mode \ref aes_mode_e
  121. \param[in] keylen_bits \ref aes_key_len_bits_e
  122. \param[in] endian \ref aes_endian_mode_e
  123. \param[in] arg Pointer to the iv address when mode is cbc_mode
  124. \return error code
  125. */
  126. int32_t csi_aes_config(aes_handle_t handle,
  127. aes_mode_e mode,
  128. aes_key_len_bits_e keylen_bits,
  129. aes_endian_mode_e endian,
  130. uint32_t arg
  131. );
  132. /**
  133. \brief set crypto key.
  134. \param[in] handle aes handle to operate.
  135. \param[in] context aes information context(NULL when hardware implementation)
  136. \param[in] key Pointer to the key buf
  137. \param[in] key_len the key len
  138. \param[in] enc \ref aes_crypto_mode_e
  139. \return error code
  140. */
  141. int32_t csi_aes_set_key(aes_handle_t handle, void *context, void *key, uint32_t key_len, aes_crypto_mode_e enc);
  142. /**
  143. \brief encrypt or decrypt
  144. \param[in] handle aes handle to operate.
  145. \param[in] context aes information context(NULL when hardware implementation)
  146. \param[in] in Pointer to the Source data
  147. \param[out] out Pointer to the Result data.
  148. \param[in] len the Source data len.
  149. \param[in] padding \ref aes_padding_mode_e.
  150. \return error code
  151. */
  152. int32_t csi_aes_crypto(aes_handle_t handle, void *context, void *in, void *out, uint32_t len, aes_padding_mode_e padding);
  153. /**
  154. \brief Get AES status.
  155. \param[in] handle aes handle to operate.
  156. \return AES status \ref aes_status_t
  157. */
  158. aes_status_t csi_aes_get_status(aes_handle_t handle);
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif /* _CSI_AES_H_ */