des.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * \file des.h
  3. *
  4. * \brief DES block cipher
  5. *
  6. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  7. * security risk. We recommend considering stronger ciphers
  8. * instead.
  9. */
  10. /*
  11. * Copyright The Mbed TLS Contributors
  12. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  13. *
  14. */
  15. #ifndef MBEDTLS_DES_H
  16. #define MBEDTLS_DES_H
  17. #include "mbedtls/private_access.h"
  18. #include "mbedtls/build_info.h"
  19. #include "mbedtls/platform_util.h"
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #define MBEDTLS_DES_ENCRYPT 1
  23. #define MBEDTLS_DES_DECRYPT 0
  24. /** The data input has an invalid length. */
  25. #define MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH -0x0032
  26. #define MBEDTLS_DES_KEY_SIZE 8
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #if !defined(MBEDTLS_DES_ALT)
  31. // Regular implementation
  32. //
  33. /**
  34. * \brief DES context structure
  35. *
  36. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  37. * security risk. We recommend considering stronger ciphers
  38. * instead.
  39. */
  40. typedef struct mbedtls_des_context {
  41. uint32_t MBEDTLS_PRIVATE(sk)[32]; /*!< DES subkeys */
  42. }
  43. mbedtls_des_context;
  44. /**
  45. * \brief Triple-DES context structure
  46. *
  47. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  48. * security risk. We recommend considering stronger ciphers
  49. * instead.
  50. */
  51. typedef struct mbedtls_des3_context {
  52. uint32_t MBEDTLS_PRIVATE(sk)[96]; /*!< 3DES subkeys */
  53. }
  54. mbedtls_des3_context;
  55. #else /* MBEDTLS_DES_ALT */
  56. #include "des_alt.h"
  57. #endif /* MBEDTLS_DES_ALT */
  58. /**
  59. * \brief Initialize DES context
  60. *
  61. * \param ctx DES context to be initialized
  62. *
  63. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  64. * security risk. We recommend considering stronger ciphers
  65. * instead.
  66. */
  67. void mbedtls_des_init(mbedtls_des_context *ctx);
  68. /**
  69. * \brief Clear DES context
  70. *
  71. * \param ctx DES context to be cleared
  72. *
  73. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  74. * security risk. We recommend considering stronger ciphers
  75. * instead.
  76. */
  77. void mbedtls_des_free(mbedtls_des_context *ctx);
  78. /**
  79. * \brief Initialize Triple-DES context
  80. *
  81. * \param ctx DES3 context to be initialized
  82. *
  83. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  84. * security risk. We recommend considering stronger ciphers
  85. * instead.
  86. */
  87. void mbedtls_des3_init(mbedtls_des3_context *ctx);
  88. /**
  89. * \brief Clear Triple-DES context
  90. *
  91. * \param ctx DES3 context to be cleared
  92. *
  93. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  94. * security risk. We recommend considering stronger ciphers
  95. * instead.
  96. */
  97. void mbedtls_des3_free(mbedtls_des3_context *ctx);
  98. /**
  99. * \brief Set key parity on the given key to odd.
  100. *
  101. * DES keys are 56 bits long, but each byte is padded with
  102. * a parity bit to allow verification.
  103. *
  104. * \param key 8-byte secret key
  105. *
  106. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  107. * security risk. We recommend considering stronger ciphers
  108. * instead.
  109. */
  110. void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]);
  111. /**
  112. * \brief Check that key parity on the given key is odd.
  113. *
  114. * DES keys are 56 bits long, but each byte is padded with
  115. * a parity bit to allow verification.
  116. *
  117. * \param key 8-byte secret key
  118. *
  119. * \return 0 is parity was ok, 1 if parity was not correct.
  120. *
  121. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  122. * security risk. We recommend considering stronger ciphers
  123. * instead.
  124. */
  125. MBEDTLS_CHECK_RETURN_TYPICAL
  126. int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
  127. /**
  128. * \brief Check that key is not a weak or semi-weak DES key
  129. *
  130. * \param key 8-byte secret key
  131. *
  132. * \return 0 if no weak key was found, 1 if a weak key was identified.
  133. *
  134. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  135. * security risk. We recommend considering stronger ciphers
  136. * instead.
  137. */
  138. MBEDTLS_CHECK_RETURN_TYPICAL
  139. int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
  140. /**
  141. * \brief DES key schedule (56-bit, encryption)
  142. *
  143. * \param ctx DES context to be initialized
  144. * \param key 8-byte secret key
  145. *
  146. * \return 0
  147. *
  148. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  149. * security risk. We recommend considering stronger ciphers
  150. * instead.
  151. */
  152. MBEDTLS_CHECK_RETURN_TYPICAL
  153. int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
  154. /**
  155. * \brief DES key schedule (56-bit, decryption)
  156. *
  157. * \param ctx DES context to be initialized
  158. * \param key 8-byte secret key
  159. *
  160. * \return 0
  161. *
  162. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  163. * security risk. We recommend considering stronger ciphers
  164. * instead.
  165. */
  166. MBEDTLS_CHECK_RETURN_TYPICAL
  167. int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
  168. /**
  169. * \brief Triple-DES key schedule (112-bit, encryption)
  170. *
  171. * \param ctx 3DES context to be initialized
  172. * \param key 16-byte secret key
  173. *
  174. * \return 0
  175. *
  176. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  177. * security risk. We recommend considering stronger ciphers
  178. * instead.
  179. */
  180. MBEDTLS_CHECK_RETURN_TYPICAL
  181. int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
  182. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
  183. /**
  184. * \brief Triple-DES key schedule (112-bit, decryption)
  185. *
  186. * \param ctx 3DES context to be initialized
  187. * \param key 16-byte secret key
  188. *
  189. * \return 0
  190. *
  191. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  192. * security risk. We recommend considering stronger ciphers
  193. * instead.
  194. */
  195. MBEDTLS_CHECK_RETURN_TYPICAL
  196. int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
  197. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]);
  198. /**
  199. * \brief Triple-DES key schedule (168-bit, encryption)
  200. *
  201. * \param ctx 3DES context to be initialized
  202. * \param key 24-byte secret key
  203. *
  204. * \return 0
  205. *
  206. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  207. * security risk. We recommend considering stronger ciphers
  208. * instead.
  209. */
  210. MBEDTLS_CHECK_RETURN_TYPICAL
  211. int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
  212. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
  213. /**
  214. * \brief Triple-DES key schedule (168-bit, decryption)
  215. *
  216. * \param ctx 3DES context to be initialized
  217. * \param key 24-byte secret key
  218. *
  219. * \return 0
  220. *
  221. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  222. * security risk. We recommend considering stronger ciphers
  223. * instead.
  224. */
  225. MBEDTLS_CHECK_RETURN_TYPICAL
  226. int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
  227. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]);
  228. /**
  229. * \brief DES-ECB block encryption/decryption
  230. *
  231. * \param ctx DES context
  232. * \param input 64-bit input block
  233. * \param output 64-bit output block
  234. *
  235. * \return 0 if successful
  236. *
  237. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  238. * security risk. We recommend considering stronger ciphers
  239. * instead.
  240. */
  241. MBEDTLS_CHECK_RETURN_TYPICAL
  242. int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
  243. const unsigned char input[8],
  244. unsigned char output[8]);
  245. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  246. /**
  247. * \brief DES-CBC buffer encryption/decryption
  248. *
  249. * \note Upon exit, the content of the IV is updated so that you can
  250. * call the function same function again on the following
  251. * block(s) of data and get the same result as if it was
  252. * encrypted in one call. This allows a "streaming" usage.
  253. * If on the other hand you need to retain the contents of the
  254. * IV, you should either save it manually or use the cipher
  255. * module instead.
  256. *
  257. * \param ctx DES context
  258. * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
  259. * \param length length of the input data
  260. * \param iv initialization vector (updated after use)
  261. * \param input buffer holding the input data
  262. * \param output buffer holding the output data
  263. *
  264. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  265. * security risk. We recommend considering stronger ciphers
  266. * instead.
  267. */
  268. MBEDTLS_CHECK_RETURN_TYPICAL
  269. int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
  270. int mode,
  271. size_t length,
  272. unsigned char iv[8],
  273. const unsigned char *input,
  274. unsigned char *output);
  275. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  276. /**
  277. * \brief 3DES-ECB block encryption/decryption
  278. *
  279. * \param ctx 3DES context
  280. * \param input 64-bit input block
  281. * \param output 64-bit output block
  282. *
  283. * \return 0 if successful
  284. *
  285. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  286. * security risk. We recommend considering stronger ciphers
  287. * instead.
  288. */
  289. MBEDTLS_CHECK_RETURN_TYPICAL
  290. int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
  291. const unsigned char input[8],
  292. unsigned char output[8]);
  293. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  294. /**
  295. * \brief 3DES-CBC buffer encryption/decryption
  296. *
  297. * \note Upon exit, the content of the IV is updated so that you can
  298. * call the function same function again on the following
  299. * block(s) of data and get the same result as if it was
  300. * encrypted in one call. This allows a "streaming" usage.
  301. * If on the other hand you need to retain the contents of the
  302. * IV, you should either save it manually or use the cipher
  303. * module instead.
  304. *
  305. * \param ctx 3DES context
  306. * \param mode MBEDTLS_DES_ENCRYPT or MBEDTLS_DES_DECRYPT
  307. * \param length length of the input data
  308. * \param iv initialization vector (updated after use)
  309. * \param input buffer holding the input data
  310. * \param output buffer holding the output data
  311. *
  312. * \return 0 if successful, or MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH
  313. *
  314. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  315. * security risk. We recommend considering stronger ciphers
  316. * instead.
  317. */
  318. MBEDTLS_CHECK_RETURN_TYPICAL
  319. int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
  320. int mode,
  321. size_t length,
  322. unsigned char iv[8],
  323. const unsigned char *input,
  324. unsigned char *output);
  325. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  326. /**
  327. * \brief Internal function for key expansion.
  328. * (Only exposed to allow overriding it,
  329. * see MBEDTLS_DES_SETKEY_ALT)
  330. *
  331. * \param SK Round keys
  332. * \param key Base key
  333. *
  334. * \warning DES/3DES are considered weak ciphers and their use constitutes a
  335. * security risk. We recommend considering stronger ciphers
  336. * instead.
  337. */
  338. void mbedtls_des_setkey(uint32_t SK[32],
  339. const unsigned char key[MBEDTLS_DES_KEY_SIZE]);
  340. #if defined(MBEDTLS_SELF_TEST)
  341. /**
  342. * \brief Checkup routine
  343. *
  344. * \return 0 if successful, or 1 if the test failed
  345. */
  346. MBEDTLS_CHECK_RETURN_CRITICAL
  347. int mbedtls_des_self_test(int verbose);
  348. #endif /* MBEDTLS_SELF_TEST */
  349. #ifdef __cplusplus
  350. }
  351. #endif
  352. #endif /* des.h */