des.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C) 2017-2020 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file drv/des.h
  6. * @brief Header File for DES Driver
  7. * @version V1.0
  8. * @date 24. Oct 2022
  9. * @model des
  10. ******************************************************************************/
  11. #ifndef _DRV_DES_H_
  12. #define _DRV_DES_H_
  13. #include <stdint.h>
  14. #include "common.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /*----- Encrypt & Decrypt: Config key length -----*/
  19. /**
  20. \brief DES data transfer mode config
  21. */
  22. typedef enum {
  23. DES_SLAVE_MODE = 0U, ///< slave mode
  24. DES_DMA_MODE, ///< dma mode
  25. } csi_des_trans_mode_t;
  26. /**
  27. \brief DES key-len-bits type
  28. */
  29. typedef enum {
  30. DES_KEY_LEN_BITS_64 = 0, ///< 64 Data bits
  31. DES_KEY_LEN_BITS_128, ///< 128 Data bits
  32. DES_KEY_LEN_BITS_192, ///< 192 Data bits
  33. } csi_des_key_bits_t;
  34. typedef enum{
  35. DES_MODE_ECB = 0x00000000,
  36. DES_MODE_CBC = 0x20000020,
  37. TDES_MODE_ECB = 0x00000008,
  38. TDES_MODE_CBC = 0x20000028,
  39. } des_mode_t;
  40. #define DES_KEY_LEN_BYTES_32 (32)
  41. #define DES_KEY_LEN_BYTES_16 (16)
  42. #define DES_KEY_LEN_BYTES_24 (24)
  43. #define DES_KEY_LEN_BYTES_8 (8)
  44. #define DES_BLOCK_IV_SIZE (16)
  45. #define DES_BLOCK_CRYPTO_SIZE (8)
  46. #define TDES_BLOCK_CRYPTO_SIZE (16)
  47. #define DES_DIR_ENCRYPT (1)
  48. #define DES_DIR_DECRYPT (0)
  49. #define DES_KEY_128_BITS (0x8)
  50. #define DES_KEY_192_BITS (0x10)
  51. /**
  52. \brief DES State
  53. */
  54. typedef struct {
  55. uint32_t busy : 1; ///< Calculate busy flag
  56. uint32_t error : 1; ///< Calculate error flag
  57. } csi_des_state_t;
  58. /**
  59. \brief DES Context
  60. */
  61. typedef struct {
  62. uint32_t key_len_byte;
  63. uint8_t key[32]; ///< Data block being processed
  64. uint32_t sca;
  65. uint32_t is_kdf;
  66. uint32_t is_dma;
  67. } csi_des_context_t;
  68. /**
  69. \brief DES Ctrl Block
  70. */
  71. typedef struct {
  72. csi_des_state_t state;
  73. csi_des_context_t context;
  74. csi_dev_t dev;
  75. void *priv;
  76. } csi_des_t;
  77. /**
  78. \brief Initialize DES interface. Initializes the resources needed for the DES interface
  79. \param[in] des Handle to operate
  80. \param[in] idx Device id
  81. \return Error code \ref csi_error_t
  82. */
  83. csi_error_t csi_des_init(csi_des_t *des, uint32_t idx);
  84. /**
  85. \brief De-initialize DES interface. Stops operation and releases the software resources used by the interface
  86. \param[in] des Dandle to operate
  87. \return None
  88. */
  89. void csi_des_uninit(csi_des_t *des);
  90. /**
  91. \brief Set encrypt key
  92. \param[in] des Handle to operate
  93. \param[in] key Pointer to the key buf
  94. \param[in] key_len Pointer to \ref csi_des_key_bits_t
  95. \return Error code \ref Csi_error_t
  96. */
  97. csi_error_t csi_des_set_encrypt_key(csi_des_t *des, void *key, csi_des_key_bits_t key_len);
  98. /**
  99. \brief Set decrypt key
  100. \param[in] des Handle to operate
  101. \param[in] key Pointer to the key buf
  102. \param[in] key_len Pointer to \ref csi_des_key_bits_t
  103. \return Error code \ref Csi_error_t
  104. */
  105. csi_error_t csi_des_set_decrypt_key(csi_des_t *des, void *key, csi_des_key_bits_t key_len);
  106. /**
  107. \brief DES ecb encrypt
  108. \param[in] des Handle to operate
  109. \param[in] in Pointer to the source data
  110. \param[out] out Pointer to the result data
  111. \param[in] size The source data size
  112. \return Error code \ref Csi_error_t
  113. */
  114. csi_error_t csi_des_ecb_encrypt(csi_des_t *des, void *in, void *out, uint32_t size);
  115. /**
  116. \brief DES ecb decrypt
  117. \param[in] des Handle to operate
  118. \param[in] in Pointer to the source data
  119. \param[out] out Pointer to the result data
  120. \param[in] size The source data size
  121. \return Error code \ref Csi_error_t
  122. */
  123. csi_error_t csi_des_ecb_decrypt(csi_des_t *des, void *in, void *out, uint32_t size);
  124. /**
  125. \brief DES cbc encrypt
  126. \param[in] des Handle to operate
  127. \param[in] in Pointer to the source data
  128. \param[out] out Pointer to the result data
  129. \param[in] size The source data size
  130. \param[in] iv Init vector
  131. \return Error code \ref Csi_error_t
  132. */
  133. csi_error_t csi_des_cbc_encrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv) ;
  134. /**
  135. \brief DES cbc decrypt
  136. \param[in] des Handle to operate
  137. \param[in] in Pointer to the source data
  138. \param[out] out Pointer to the result data
  139. \param[in] size The source data size
  140. \param[in] iv Init vector
  141. \return Error code \ref Csi_error_t
  142. */
  143. csi_error_t csi_des_cbc_decrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv);
  144. /**
  145. \brief TDES ecb encrypt
  146. \param[in] des Handle to operate
  147. \param[in] in Pointer to the source data
  148. \param[out] out Pointer to the result data
  149. \param[in] size The source data size
  150. \return Error code \ref Csi_error_t
  151. */
  152. csi_error_t csi_tdes_ecb_encrypt(csi_des_t *des, void *in, void *out, uint32_t size);
  153. /**
  154. \brief TDES ecb decrypt
  155. \param[in] des Handle to operate
  156. \param[in] in Pointer to the source data
  157. \param[out] out Pointer to the result data
  158. \param[in] size The source data size
  159. \return Error code \ref Csi_error_t
  160. */
  161. csi_error_t csi_tdes_ecb_decrypt(csi_des_t *des, void *in, void *out, uint32_t size);
  162. /**
  163. \brief TDES cbc encrypt
  164. \param[in] des Handle to operate
  165. \param[in] in Pointer to the source data
  166. \param[out] out Pointer to the result data
  167. \param[in] size The source data size
  168. \param[in] iv Init vector
  169. \return Error code \ref Csi_error_t
  170. */
  171. csi_error_t csi_tdes_cbc_encrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv) ;
  172. /**
  173. \brief TDES cbc decrypt
  174. \param[in] des Handle to operate
  175. \param[in] in Pointer to the source data
  176. \param[out] out Pointer to the result data
  177. \param[in] size The source data size
  178. \param[in] iv Init vector
  179. \return Error code \ref Csi_error_t
  180. */
  181. csi_error_t csi_tdes_cbc_decrypt(csi_des_t *des, void *in, void *out, uint32_t size, void *iv);
  182. /**
  183. \brief Config DES mode dma or slave
  184. \param[in] mode \ref csi_des_trans_mode_t
  185. \return None
  186. */
  187. void csi_des_trans_config(csi_des_t *des, csi_des_trans_mode_t mode);
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif /* _DRV_AES_H_ */