sec_crypto_des.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2017-2022 Alibaba Group Holding Limited
  3. */
  4. /******************************************************************************
  5. * @file sec_crypt0_des.h
  6. * @brief Header File for DES
  7. * @version V1.0
  8. * @date 24. Oct 2022
  9. * @model des
  10. ******************************************************************************/
  11. #ifndef _SC_DES_H_
  12. #define _SC_DES_H_
  13. #include "sec_include_config.h"
  14. #include <stdint.h>
  15. #include "sec_crypto_errcode.h"
  16. #ifdef CONFIG_SYSTEM_SECURE
  17. #ifdef SEC_LIB_VERSION
  18. #include <drv/des.h>
  19. #else
  20. #include "des.h"
  21. #endif
  22. #endif
  23. #ifdef CONFIG_SEC_CRYPTO_DES_SW
  24. #include "crypto_des.h"
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. \brief DES data transfer mode config
  31. */
  32. typedef enum {
  33. SC_DES_SLAVE_MODE = 0U, ///< slave mode
  34. SC_DES_DMA_MODE, ///< dma mode
  35. } sc_des_trans_mode_t;
  36. /**
  37. \brief DES key-len-bits type
  38. */
  39. typedef enum {
  40. SC_DES_KEY_LEN_BITS_64 = 0U, ///< 64 Data bits
  41. SC_DES_KEY_LEN_BITS_128, ///< 128 Data bits
  42. SC_TDES_KEY_LEN_BITS_192, ///< 192 Data bits
  43. } sc_des_key_bits_t;
  44. /**
  45. \brief DES Ctrl Block
  46. */
  47. typedef struct {
  48. #ifdef CONFIG_SYSTEM_SECURE
  49. #ifdef CONFIG_CSI_V1
  50. des_handle_t handle;
  51. unsigned char key[32];
  52. unsigned int key_len;
  53. #endif
  54. #ifdef CONFIG_CSI_V2
  55. csi_des_t csi_des;
  56. //unsigned char sc_ctx[SC_DES_CTX_SIZE];
  57. #endif
  58. #endif
  59. #if defined(CONFIG_TEE_CA)
  60. unsigned char key[32];
  61. unsigned int key_len;
  62. #endif
  63. #if defined(CONFIG_SEC_CRYPTO_DES_SW)
  64. sc_mbedtls_des_context des_ctx;
  65. #endif
  66. //void *ctx;
  67. } sc_des_t;
  68. // Function documentation
  69. /**
  70. \brief Initialize DES Interface. Initializes the resources needed for the DES interface
  71. \param[in] des operate handle
  72. \param[in] idx device id
  73. \return error code \ref uint32_t
  74. */
  75. uint32_t sc_des_init(sc_des_t *des, uint32_t idx);
  76. /**
  77. \brief De-initialize DES Interface. stops operation and releases the software resources used by the interface
  78. \param[in] des handle to operate
  79. \return None
  80. */
  81. void sc_des_uninit(sc_des_t *des);
  82. /**
  83. \brief Set encrypt key
  84. \param[in] des handle to operate
  85. \param[in] key Pointer to the key buf
  86. \param[in] key_len Pointer to \ref sc_des_key_bits_t
  87. \return error code \ref uint32_t
  88. */
  89. uint32_t sc_des_set_encrypt_key(sc_des_t *des, void *key, sc_des_key_bits_t key_len);
  90. /**
  91. \brief Set decrypt 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 sc_des_key_bits_t
  95. \return error code \ref uint32_t
  96. */
  97. uint32_t sc_des_set_decrypt_key(sc_des_t *des, void *key, sc_des_key_bits_t key_len);
  98. /**
  99. \brief Des ecb encrypt
  100. \param[in] des handle to operate
  101. \param[in] in Pointer to the Source data
  102. \param[out] out Pointer to the Result data
  103. \param[in] size the Source data size
  104. \return error code \ref uint32_t
  105. */
  106. uint32_t sc_des_ecb_encrypt(sc_des_t *des, void *in, void *out, uint32_t size);
  107. /**
  108. \brief Des ecb decrypt
  109. \param[in] des handle to operate
  110. \param[in] in Pointer to the Source data
  111. \param[out] out Pointer to the Result data
  112. \param[in] size the Source data size
  113. \return error code \ref uint32_t
  114. */
  115. uint32_t sc_des_ecb_decrypt(sc_des_t *des, void *in, void *out, uint32_t size);
  116. /**
  117. \brief Des cbc encrypt
  118. \param[in] des handle to operate
  119. \param[in] in Pointer to the Source data
  120. \param[out] out Pointer to the Result data
  121. \param[in] size the Source data size
  122. \param[in] iv init vector
  123. \return error code \ref uint32_t
  124. */
  125. uint32_t sc_des_cbc_encrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv);
  126. /**
  127. \brief Des cbc decrypt
  128. \param[in] des handle to operate
  129. \param[in] in Pointer to the Source data
  130. \param[out] out Pointer to the Result data
  131. \param[in] size the Source data size
  132. \param[in] iv init vector
  133. \return error code \ref uint32_t
  134. */
  135. uint32_t sc_des_cbc_decrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv);
  136. /**
  137. \brief TDes ecb encrypt
  138. \param[in] des handle to operate
  139. \param[in] in Pointer to the Source data
  140. \param[out] out Pointer to the Result data
  141. \param[in] size the Source data size
  142. \return error code \ref uint32_t
  143. */
  144. uint32_t sc_tdes_ecb_encrypt(sc_des_t *des, void *in, void *out, uint32_t size);
  145. /**
  146. \brief TDes ecb decrypt
  147. \param[in] des handle to operate
  148. \param[in] in Pointer to the Source data
  149. \param[out] out Pointer to the Result data
  150. \param[in] size the Source data size
  151. \return error code \ref uint32_t
  152. */
  153. uint32_t sc_tdes_ecb_decrypt(sc_des_t *des, void *in, void *out, uint32_t size);
  154. /**
  155. \brief TDes cbc encrypt
  156. \param[in] des handle to operate
  157. \param[in] in Pointer to the Source data
  158. \param[out] out Pointer to the Result data
  159. \param[in] size the Source data size
  160. \param[in] iv init vector
  161. \return error code \ref uint32_t
  162. */
  163. uint32_t sc_tdes_cbc_encrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv);
  164. /**
  165. \brief TDes cbc decrypt
  166. \param[in] des handle to operate
  167. \param[in] in Pointer to the Source data
  168. \param[out] out Pointer to the Result data
  169. \param[in] size the Source data size
  170. \param[in] iv init vector
  171. \return error code \ref uint32_t
  172. */
  173. uint32_t sc_tdes_cbc_decrypt(sc_des_t *des, void *in, void *out, uint32_t size, void *iv);
  174. /**
  175. \brief Config DES mode dma or slave
  176. \param[in] mode \ref sc_des_trans_mode_t
  177. \return None
  178. */
  179. void sc_des_trans_config(sc_des_t *des, sc_des_trans_mode_t mode) ;
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif /* _SC_DES_H_ */