des.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * \file des.h
  3. *
  4. * \brief DES block cipher
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_DES_H
  26. #define MBEDTLS_DES_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #define MBEDTLS_DES_ENCRYPT 1
  35. #define MBEDTLS_DES_DECRYPT 0
  36. #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */
  37. #define MBEDTLS_DES_KEY_SIZE 8
  38. #if !defined(MBEDTLS_DES_ALT)
  39. // Regular implementation
  40. //
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /**
  45. * \brief DES context structure
  46. */
  47. typedef struct
  48. {
  49. uint32_t sk[32]; /*!< DES subkeys */
  50. }
  51. mbedtls_des_context;
  52. /**
  53. * \brief Triple-DES context structure
  54. */
  55. typedef struct
  56. {
  57. uint32_t sk[96]; /*!< 3DES subkeys */
  58. }
  59. mbedtls_des3_context;
  60. /**
  61. * \brief Initialize DES context
  62. *
  63. * \param ctx DES context to be initialized
  64. */
  65. void mbedtls_des_init( mbedtls_des_context *ctx );
  66. /**
  67. * \brief Clear DES context
  68. *
  69. * \param ctx DES context to be cleared
  70. */
  71. void mbedtls_des_free( mbedtls_des_context *ctx );
  72. /**
  73. * \brief Initialize Triple-DES context
  74. *
  75. * \param ctx DES3 context to be initialized
  76. */
  77. void mbedtls_des3_init( mbedtls_des3_context *ctx );
  78. /**
  79. * \brief Clear Triple-DES context
  80. *
  81. * \param ctx DES3 context to be cleared
  82. */
  83. void mbedtls_des3_free( mbedtls_des3_context *ctx );
  84. /**
  85. * \brief Set key parity on the given key to odd.
  86. *
  87. * DES keys are 56 bits long, but each byte is padded with
  88. * a parity bit to allow verification.
  89. *
  90. * \param key 8-byte secret key
  91. */
  92. void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  93. /**
  94. * \brief Check that key parity on the given key is odd.
  95. *
  96. * DES keys are 56 bits long, but each byte is padded with
  97. * a parity bit to allow verification.
  98. *
  99. * \param key 8-byte secret key
  100. *
  101. * \return 0 is parity was ok, 1 if parity was not correct.
  102. */
  103. int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  104. /**
  105. * \brief Check that key is not a weak or semi-weak DES key
  106. *
  107. * \param key 8-byte secret key
  108. *
  109. * \return 0 if no weak key was found, 1 if a weak key was identified.
  110. */
  111. int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  112. /**
  113. * \brief DES key schedule (56-bit, encryption)
  114. *
  115. * \param ctx DES context to be initialized
  116. * \param key 8-byte secret key
  117. *
  118. * \return 0
  119. */
  120. int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  121. /**
  122. * \brief DES key schedule (56-bit, decryption)
  123. *
  124. * \param ctx DES context to be initialized
  125. * \param key 8-byte secret key
  126. *
  127. * \return 0
  128. */
  129. int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  130. /**
  131. * \brief Triple-DES key schedule (112-bit, encryption)
  132. *
  133. * \param ctx 3DES context to be initialized
  134. * \param key 16-byte secret key
  135. *
  136. * \return 0
  137. */
  138. int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  139. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
  140. /**
  141. * \brief Triple-DES key schedule (112-bit, decryption)
  142. *
  143. * \param ctx 3DES context to be initialized
  144. * \param key 16-byte secret key
  145. *
  146. * \return 0
  147. */
  148. int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  149. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] );
  150. /**
  151. * \brief Triple-DES key schedule (168-bit, encryption)
  152. *
  153. * \param ctx 3DES context to be initialized
  154. * \param key 24-byte secret key
  155. *
  156. * \return 0
  157. */
  158. int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  159. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
  160. /**
  161. * \brief Triple-DES key schedule (168-bit, decryption)
  162. *
  163. * \param ctx 3DES context to be initialized
  164. * \param key 24-byte secret key
  165. *
  166. * \return 0
  167. */
  168. int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  169. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] );
  170. /**
  171. * \brief DES-ECB block encryption/decryption
  172. *
  173. * \param ctx DES context
  174. * \param input 64-bit input block
  175. * \param output 64-bit output block
  176. *
  177. * \return 0 if successful
  178. */
  179. int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  180. const unsigned char input[8],
  181. unsigned char output[8] );
  182. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  183. /**
  184. * \brief DES-CBC buffer encryption/decryption
  185. *
  186. * \note Upon exit, the content of the IV is updated so that you can
  187. * call the function same function again on the following
  188. * block(s) of data and get the same result as if it was
  189. * encrypted in one call. This allows a "streaming" usage.
  190. * If on the other hand you need to retain the contents of the
  191. * IV, you should either save it manually or use the cipher
  192. * module instead.
  193. *
  194. * \param ctx DES context
  195. * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
  196. * \param length length of the input data
  197. * \param iv initialization vector (updated after use)
  198. * \param input buffer holding the input data
  199. * \param output buffer holding the output data
  200. */
  201. int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  202. int mode,
  203. size_t length,
  204. unsigned char iv[8],
  205. const unsigned char *input,
  206. unsigned char *output );
  207. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  208. /**
  209. * \brief 3DES-ECB block encryption/decryption
  210. *
  211. * \param ctx 3DES context
  212. * \param input 64-bit input block
  213. * \param output 64-bit output block
  214. *
  215. * \return 0 if successful
  216. */
  217. int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  218. const unsigned char input[8],
  219. unsigned char output[8] );
  220. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  221. /**
  222. * \brief 3DES-CBC buffer encryption/decryption
  223. *
  224. * \note Upon exit, the content of the IV is updated so that you can
  225. * call the function same function again on the following
  226. * block(s) of data and get the same result as if it was
  227. * encrypted in one call. This allows a "streaming" usage.
  228. * If on the other hand you need to retain the contents of the
  229. * IV, you should either save it manually or use the cipher
  230. * module instead.
  231. *
  232. * \param ctx 3DES context
  233. * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
  234. * \param length length of the input data
  235. * \param iv initialization vector (updated after use)
  236. * \param input buffer holding the input data
  237. * \param output buffer holding the output data
  238. *
  239. * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
  240. */
  241. int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  242. int mode,
  243. size_t length,
  244. unsigned char iv[8],
  245. const unsigned char *input,
  246. unsigned char *output );
  247. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  248. /**
  249. * \brief Internal function for key expansion.
  250. * (Only exposed to allow overriding it,
  251. * see MBEDTLS_DES_SETKEY_ALT)
  252. *
  253. * \param SK Round keys
  254. * \param key Base key
  255. */
  256. void mbedtls_des_setkey( uint32_t SK[32],
  257. const unsigned char key[MBEDTLS_DES_KEY_SIZE] );
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #else /* MBEDTLS_DES_ALT */
  262. #include "des_alt.h"
  263. #endif /* MBEDTLS_DES_ALT */
  264. #ifdef __cplusplus
  265. extern "C" {
  266. #endif
  267. /**
  268. * \brief Checkup routine
  269. *
  270. * \return 0 if successful, or 1 if the test failed
  271. */
  272. int mbedtls_des_self_test( int verbose );
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276. #endif /* des.h */