des.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * \file des.h
  3. *
  4. * \brief DES block cipher
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. /* Note: This file has been modified by ST's MCD Application Team, to support
  28. the hardware crypto engine embedded in STM32F417xx */
  29. #ifndef POLARSSL_DES_H
  30. #define POLARSSL_DES_H
  31. #include <string.h>
  32. #define DES_ENCRYPT 1
  33. #define DES_DECRYPT 0
  34. #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
  35. #define DES_KEY_SIZE 8
  36. /**
  37. * \brief DES context structure
  38. */
  39. typedef struct
  40. {
  41. int mode; /*!< encrypt/decrypt */
  42. unsigned long sk[32]; /*!< DES subkeys */
  43. }
  44. des_context;
  45. /**
  46. * \brief Triple-DES context structure
  47. */
  48. typedef struct
  49. {
  50. int mode; /*!< encrypt/decrypt */
  51. unsigned long sk[96]; /*!< 3DES subkeys */
  52. #ifdef USE_STM32F4XX_HW_CRYPTO
  53. unsigned char tdes_enc_key[24]; /* Encryption key */
  54. unsigned char tdes_dec_key[24]; /* Decryption key */
  55. #endif /* USE_STM32F4XX_HW_CRYPTO */
  56. }
  57. des3_context;
  58. #ifdef __cplusplus
  59. extern "C" {
  60. #endif
  61. /**
  62. * \brief Set key parity on the given key to 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. void des_key_set_parity( unsigned char key[DES_KEY_SIZE] );
  70. /**
  71. * \brief Check that key parity on the given key is odd.
  72. *
  73. * DES keys are 56 bits long, but each byte is padded with
  74. * a parity bit to allow verification.
  75. *
  76. * \param key 8-byte secret key
  77. *
  78. * \return 0 is parity was ok, 1 if parity was not correct.
  79. */
  80. int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] );
  81. /**
  82. * \brief Check that key is not a weak or semi-weak DES key
  83. *
  84. * \param key 8-byte secret key
  85. *
  86. * \resurn 0 if no weak key was found, 1 if a weak key was identified.
  87. */
  88. int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] );
  89. /**
  90. * \brief DES key schedule (56-bit, encryption)
  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_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
  98. /**
  99. * \brief DES key schedule (56-bit, decryption)
  100. *
  101. * \param ctx DES context to be initialized
  102. * \param key 8-byte secret key
  103. *
  104. * \return 0
  105. */
  106. int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] );
  107. /**
  108. * \brief Triple-DES key schedule (112-bit, encryption)
  109. *
  110. * \param ctx 3DES context to be initialized
  111. * \param key 16-byte secret key
  112. *
  113. * \return 0
  114. */
  115. int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
  116. /**
  117. * \brief Triple-DES key schedule (112-bit, decryption)
  118. *
  119. * \param ctx 3DES context to be initialized
  120. * \param key 16-byte secret key
  121. *
  122. * \return 0
  123. */
  124. int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] );
  125. /**
  126. * \brief Triple-DES key schedule (168-bit, encryption)
  127. *
  128. * \param ctx 3DES context to be initialized
  129. * \param key 24-byte secret key
  130. *
  131. * \return 0
  132. */
  133. int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
  134. /**
  135. * \brief Triple-DES key schedule (168-bit, decryption)
  136. *
  137. * \param ctx 3DES context to be initialized
  138. * \param key 24-byte secret key
  139. *
  140. * \return 0
  141. */
  142. int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] );
  143. /**
  144. * \brief DES-ECB block encryption/decryption
  145. *
  146. * \param ctx DES context
  147. * \param input 64-bit input block
  148. * \param output 64-bit output block
  149. *
  150. * \return 0 if successful
  151. */
  152. int des_crypt_ecb( des_context *ctx,
  153. const unsigned char input[8],
  154. unsigned char output[8] );
  155. /**
  156. * \brief DES-CBC buffer encryption/decryption
  157. *
  158. * \param ctx DES context
  159. * \param mode DES_ENCRYPT or DES_DECRYPT
  160. * \param length length of the input data
  161. * \param iv initialization vector (updated after use)
  162. * \param input buffer holding the input data
  163. * \param output buffer holding the output data
  164. */
  165. int des_crypt_cbc( des_context *ctx,
  166. int mode,
  167. size_t length,
  168. unsigned char iv[8],
  169. const unsigned char *input,
  170. unsigned char *output );
  171. /**
  172. * \brief 3DES-ECB block encryption/decryption
  173. *
  174. * \param ctx 3DES context
  175. * \param input 64-bit input block
  176. * \param output 64-bit output block
  177. *
  178. * \return 0 if successful
  179. */
  180. int des3_crypt_ecb( des3_context *ctx,
  181. const unsigned char input[8],
  182. unsigned char output[8] );
  183. /**
  184. * \brief 3DES-CBC buffer encryption/decryption
  185. *
  186. * \param ctx 3DES context
  187. * \param mode DES_ENCRYPT or DES_DECRYPT
  188. * \param length length of the input data
  189. * \param iv initialization vector (updated after use)
  190. * \param input buffer holding the input data
  191. * \param output buffer holding the output data
  192. *
  193. * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH
  194. */
  195. int des3_crypt_cbc( des3_context *ctx,
  196. int mode,
  197. size_t length,
  198. unsigned char iv[8],
  199. const unsigned char *input,
  200. unsigned char *output );
  201. /*
  202. * \brief Checkup routine
  203. *
  204. * \return 0 if successful, or 1 if the test failed
  205. */
  206. int des_self_test( int verbose );
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif /* des.h */