des1.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef DES_H
  2. #define DES_H
  3. #include <stddef.h>
  4. //#include <stdint.h>
  5. typedef unsigned int uint32_t;
  6. #define DES_ENCRYPT 1
  7. #define DES_DECRYPT 0
  8. #define ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
  9. #define DES_KEY_SIZE 8
  10. #if !defined(DES_ALT)
  11. // Regular implementation
  12. //
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. // DES上下文结构体;
  17. typedef struct
  18. {
  19. uint32_t sk[32]; // DES subkeys;
  20. }
  21. des_context;
  22. // 3层(Triple)DES上下文结构体;
  23. typedef struct
  24. {
  25. uint32_t sk[96]; // 3DES subkeys;
  26. }
  27. des3_context;
  28. /**
  29. * \brief Initialize DES context
  30. *
  31. * \param ctx DES context to be initialized
  32. */
  33. void des_init( des_context *ctx );
  34. /**
  35. * \brief Clear DES context
  36. *
  37. * \param ctx DES context to be cleared
  38. */
  39. void des_free( des_context *ctx );
  40. /**
  41. * \brief Initialize Triple-DES context
  42. *
  43. * \param ctx DES3 context to be initialized
  44. */
  45. void des3_init( des3_context *ctx );
  46. /**
  47. * \brief Clear Triple-DES context
  48. *
  49. * \param ctx DES3 context to be cleared
  50. */
  51. void des3_free( des3_context *ctx );
  52. /**
  53. * \brief Set key parity on the given key to odd.
  54. *
  55. * DES keys are 56 bits long, but each byte is padded with
  56. * a parity bit to allow verification.
  57. *
  58. * \param key 8-byte secret key
  59. */
  60. void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
  61. /**
  62. * \brief Check that key parity on the given key is odd.
  63. *
  64. * DES keys are 56 bits long, but each byte is padded with
  65. * a parity bit to allow verification.
  66. *
  67. * \param key 8-byte secret key
  68. *
  69. * \return 0 is parity was ok, 1 if parity was not correct.
  70. */
  71. int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
  72. /**
  73. * \brief Check that key is not a weak or semi-weak DES key
  74. *
  75. * \param key 8-byte secret key
  76. *
  77. * \return 0 if no weak key was found, 1 if a weak key was identified.
  78. */
  79. int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
  80. /**
  81. * \brief DES key schedule (56-bit, encryption)
  82. *
  83. * \param ctx DES context to be initialized
  84. * \param key 8-byte secret key
  85. *
  86. * \return 0
  87. */
  88. int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
  89. /**
  90. * \brief DES key schedule (56-bit, decryption)
  91. *
  92. * \param ctx DES context to be initialized
  93. * \param key 8-byte secret key
  94. *
  95. * \return 0
  96. */
  97. int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
  98. /**
  99. * \brief Triple-DES key schedule (112-bit, encryption)
  100. *
  101. * \param ctx 3DES context to be initialized
  102. * \param key 16-byte secret key
  103. *
  104. * \return 0
  105. */
  106. int des3_set2key_enc( des3_context *ctx,
  107. const unsigned char key[DES_KEY_SIZE * 2] );
  108. /**
  109. * \brief Triple-DES key schedule (112-bit, decryption)
  110. *
  111. * \param ctx 3DES context to be initialized
  112. * \param key 16-byte secret key
  113. *
  114. * \return 0
  115. */
  116. int des3_set2key_dec( des3_context *ctx,
  117. const unsigned char key[DES_KEY_SIZE * 2] );
  118. /**
  119. * \brief Triple-DES key schedule (168-bit, encryption)
  120. *
  121. * \param ctx 3DES context to be initialized
  122. * \param key 24-byte secret key
  123. *
  124. * \return 0
  125. */
  126. int des3_set3key_enc( des3_context *ctx,
  127. const unsigned char key[DES_KEY_SIZE * 3] );
  128. /**
  129. * \brief Triple-DES key schedule (168-bit, decryption)
  130. *
  131. * \param ctx 3DES context to be initialized
  132. * \param key 24-byte secret key
  133. *
  134. * \return 0
  135. */
  136. int des3_set3key_dec( des3_context *ctx,
  137. const unsigned char key[DES_KEY_SIZE * 3] );
  138. /**
  139. * \brief DES-ECB block encryption/decryption
  140. *
  141. * \param ctx DES context
  142. * \param input 64-bit input block
  143. * \param output 64-bit output block
  144. *
  145. * \return 0 if successful
  146. */
  147. int des_crypt_ecb( des_context *ctx,
  148. const unsigned char input[8],
  149. unsigned char output[8] );
  150. #if defined(CIPHER_MODE_CBC)
  151. /**
  152. * \brief DES-CBC buffer encryption/decryption
  153. *
  154. * \note Upon exit, the content of the IV is updated so that you can
  155. * call the function same function again on the following
  156. * block(s) of data and get the same result as if it was
  157. * encrypted in one call. This allows a "streaming" usage.
  158. * If on the other hand you need to retain the contents of the
  159. * IV, you should either save it manually or use the cipher
  160. * module instead.
  161. *
  162. * \param ctx DES context
  163. * \param mode DES_ENCRYPT or DES_DECRYPT
  164. * \param length length of the input data
  165. * \param iv initialization vector (updated after use)
  166. * \param input buffer holding the input data
  167. * \param output buffer holding the output data
  168. */
  169. int des_crypt_cbc( des_context *ctx,
  170. int mode,
  171. size_t length,
  172. unsigned char iv[8],
  173. const unsigned char *input,
  174. unsigned char *output );
  175. #endif /* CIPHER_MODE_CBC */
  176. /**
  177. * \brief 3DES-ECB block encryption/decryption
  178. *
  179. * \param ctx 3DES context
  180. * \param input 64-bit input block
  181. * \param output 64-bit output block
  182. *
  183. * \return 0 if successful
  184. */
  185. int des3_crypt_ecb( des3_context *ctx,
  186. const unsigned char input[8],
  187. unsigned char output[8] );
  188. #if defined(CIPHER_MODE_CBC)
  189. /**
  190. * \brief 3DES-CBC buffer encryption/decryption
  191. *
  192. * \note Upon exit, the content of the IV is updated so that you can
  193. * call the function same function again on the following
  194. * block(s) of data and get the same result as if it was
  195. * encrypted in one call. This allows a "streaming" usage.
  196. * If on the other hand you need to retain the contents of the
  197. * IV, you should either save it manually or use the cipher
  198. * module instead.
  199. *
  200. * \param ctx 3DES context
  201. * \param mode DES_ENCRYPT or DES_DECRYPT
  202. * \param length length of the input data
  203. * \param iv initialization vector (updated after use)
  204. * \param input buffer holding the input data
  205. * \param output buffer holding the output data
  206. *
  207. * \return 0 if successful, or ERR_DES_INVALID_INPUT_LENGTH
  208. */
  209. int des3_crypt_cbc( des3_context *ctx,
  210. int mode,
  211. size_t length,
  212. unsigned char iv[8],
  213. const unsigned char *input,
  214. unsigned char *output );
  215. #endif /* CIPHER_MODE_CBC */
  216. /**
  217. * \brief Internal function for key expansion.
  218. * (Only exposed to allow overriding it,
  219. * see DES_SETKEY_ALT)
  220. *
  221. * \param SK Round keys
  222. * \param key Base key
  223. */
  224. void des_setkey( uint32_t SK[32],
  225. const unsigned char key[DES_KEY_SIZE] );
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #else /* DES_ALT */
  230. #include "des_alt.h"
  231. #endif /* DES_ALT */
  232. #ifdef __cplusplus
  233. extern "C" {
  234. #endif
  235. /**
  236. * \brief Checkup routine
  237. *
  238. * \return 0 if successful, or 1 if the test failed
  239. */
  240. int des_self_test( int verbose );
  241. #ifdef __cplusplus
  242. }
  243. #endif
  244. #endif /* des.h */