des.h 11 KB

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