des.h 8.7 KB

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