des.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * FIPS-46-3 compliant Triple-DES implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * DES, on which TDES is based, was originally designed by Horst Feistel
  21. * at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
  22. *
  23. * http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  24. */
  25. #include "common.h"
  26. #if defined(MBEDTLS_DES_C)
  27. #include "mbedtls/des.h"
  28. #include "mbedtls/error.h"
  29. #include "mbedtls/platform_util.h"
  30. #include <string.h>
  31. #if defined(MBEDTLS_SELF_TEST)
  32. #if defined(MBEDTLS_PLATFORM_C)
  33. #include "mbedtls/platform.h"
  34. #else
  35. #include <stdio.h>
  36. #define mbedtls_printf printf
  37. #endif /* MBEDTLS_PLATFORM_C */
  38. #endif /* MBEDTLS_SELF_TEST */
  39. #if !defined(MBEDTLS_DES_ALT)
  40. /*
  41. * Expanded DES S-boxes
  42. */
  43. static const uint32_t SB1[64] =
  44. {
  45. 0x01010400, 0x00000000, 0x00010000, 0x01010404,
  46. 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  47. 0x00000400, 0x01010400, 0x01010404, 0x00000400,
  48. 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  49. 0x00000404, 0x01000400, 0x01000400, 0x00010400,
  50. 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  51. 0x00010004, 0x01000004, 0x01000004, 0x00010004,
  52. 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  53. 0x00010000, 0x01010404, 0x00000004, 0x01010000,
  54. 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  55. 0x01010004, 0x00010000, 0x00010400, 0x01000004,
  56. 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  57. 0x01010404, 0x00010004, 0x01010000, 0x01000404,
  58. 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  59. 0x00000404, 0x01000400, 0x01000400, 0x00000000,
  60. 0x00010004, 0x00010400, 0x00000000, 0x01010004
  61. };
  62. static const uint32_t SB2[64] =
  63. {
  64. 0x80108020, 0x80008000, 0x00008000, 0x00108020,
  65. 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  66. 0x80000020, 0x80108020, 0x80108000, 0x80000000,
  67. 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  68. 0x00108000, 0x00100020, 0x80008020, 0x00000000,
  69. 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  70. 0x00100020, 0x80000020, 0x00000000, 0x00108000,
  71. 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  72. 0x00000000, 0x00108020, 0x80100020, 0x00100000,
  73. 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  74. 0x80100000, 0x80008000, 0x00000020, 0x80108020,
  75. 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  76. 0x00008020, 0x80108000, 0x00100000, 0x80000020,
  77. 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  78. 0x00108000, 0x00000000, 0x80008000, 0x00008020,
  79. 0x80000000, 0x80100020, 0x80108020, 0x00108000
  80. };
  81. static const uint32_t SB3[64] =
  82. {
  83. 0x00000208, 0x08020200, 0x00000000, 0x08020008,
  84. 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  85. 0x00020008, 0x08000008, 0x08000008, 0x00020000,
  86. 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  87. 0x08000000, 0x00000008, 0x08020200, 0x00000200,
  88. 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  89. 0x08000208, 0x00020200, 0x00020000, 0x08000208,
  90. 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  91. 0x08020200, 0x08000000, 0x00020008, 0x00000208,
  92. 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  93. 0x00000200, 0x00020008, 0x08020208, 0x08000200,
  94. 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  95. 0x08000208, 0x00020000, 0x08000000, 0x08020208,
  96. 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  97. 0x08020000, 0x08000208, 0x00000208, 0x08020000,
  98. 0x00020208, 0x00000008, 0x08020008, 0x00020200
  99. };
  100. static const uint32_t SB4[64] =
  101. {
  102. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  103. 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  104. 0x00000000, 0x00802000, 0x00802000, 0x00802081,
  105. 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  106. 0x00000001, 0x00002000, 0x00800000, 0x00802001,
  107. 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  108. 0x00800081, 0x00000001, 0x00002080, 0x00800080,
  109. 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  110. 0x00800080, 0x00800001, 0x00802000, 0x00802081,
  111. 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  112. 0x00002080, 0x00800080, 0x00800081, 0x00000001,
  113. 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  114. 0x00802081, 0x00000081, 0x00000001, 0x00002000,
  115. 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  116. 0x00002001, 0x00002080, 0x00800000, 0x00802001,
  117. 0x00000080, 0x00800000, 0x00002000, 0x00802080
  118. };
  119. static const uint32_t SB5[64] =
  120. {
  121. 0x00000100, 0x02080100, 0x02080000, 0x42000100,
  122. 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  123. 0x40080100, 0x00080000, 0x02000100, 0x40080100,
  124. 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  125. 0x02000000, 0x40080000, 0x40080000, 0x00000000,
  126. 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  127. 0x42080000, 0x40000100, 0x00000000, 0x42000000,
  128. 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  129. 0x00080000, 0x42000100, 0x00000100, 0x02000000,
  130. 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  131. 0x02000100, 0x40000000, 0x42080000, 0x02080100,
  132. 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  133. 0x42080100, 0x00080100, 0x42000000, 0x42080100,
  134. 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  135. 0x00080100, 0x02000100, 0x40000100, 0x00080000,
  136. 0x00000000, 0x40080000, 0x02080100, 0x40000100
  137. };
  138. static const uint32_t SB6[64] =
  139. {
  140. 0x20000010, 0x20400000, 0x00004000, 0x20404010,
  141. 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  142. 0x20004000, 0x00404010, 0x00400000, 0x20000010,
  143. 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  144. 0x00000000, 0x00400010, 0x20004010, 0x00004000,
  145. 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  146. 0x20400010, 0x00000000, 0x00404010, 0x20404000,
  147. 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  148. 0x20004000, 0x00000010, 0x20400010, 0x00404000,
  149. 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  150. 0x00400000, 0x20004000, 0x20000000, 0x00004010,
  151. 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  152. 0x00404010, 0x20404000, 0x00000000, 0x20400010,
  153. 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  154. 0x00004000, 0x00400010, 0x20004010, 0x00000000,
  155. 0x20404000, 0x20000000, 0x00400010, 0x20004010
  156. };
  157. static const uint32_t SB7[64] =
  158. {
  159. 0x00200000, 0x04200002, 0x04000802, 0x00000000,
  160. 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  161. 0x04200802, 0x00200000, 0x00000000, 0x04000002,
  162. 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  163. 0x04000800, 0x00200802, 0x00200002, 0x04000800,
  164. 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  165. 0x04200000, 0x00000800, 0x00000802, 0x04200802,
  166. 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  167. 0x04000000, 0x00200800, 0x00200000, 0x04000802,
  168. 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  169. 0x00200002, 0x04000000, 0x04000800, 0x00200000,
  170. 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  171. 0x00000802, 0x04000002, 0x04200802, 0x04200000,
  172. 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  173. 0x00000000, 0x00200802, 0x04200000, 0x00000800,
  174. 0x04000002, 0x04000800, 0x00000800, 0x00200002
  175. };
  176. static const uint32_t SB8[64] =
  177. {
  178. 0x10001040, 0x00001000, 0x00040000, 0x10041040,
  179. 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  180. 0x00040040, 0x10040000, 0x10041040, 0x00041000,
  181. 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  182. 0x10040000, 0x10000040, 0x10001000, 0x00001040,
  183. 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  184. 0x00001040, 0x00000000, 0x00000000, 0x10040040,
  185. 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  186. 0x00041040, 0x00040000, 0x10041000, 0x00001000,
  187. 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  188. 0x10001000, 0x00000040, 0x10000040, 0x10040000,
  189. 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  190. 0x00000000, 0x10041040, 0x00040040, 0x10000040,
  191. 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  192. 0x10041040, 0x00041000, 0x00041000, 0x00001040,
  193. 0x00001040, 0x00040040, 0x10000000, 0x10041000
  194. };
  195. /*
  196. * PC1: left and right halves bit-swap
  197. */
  198. static const uint32_t LHs[16] =
  199. {
  200. 0x00000000, 0x00000001, 0x00000100, 0x00000101,
  201. 0x00010000, 0x00010001, 0x00010100, 0x00010101,
  202. 0x01000000, 0x01000001, 0x01000100, 0x01000101,
  203. 0x01010000, 0x01010001, 0x01010100, 0x01010101
  204. };
  205. static const uint32_t RHs[16] =
  206. {
  207. 0x00000000, 0x01000000, 0x00010000, 0x01010000,
  208. 0x00000100, 0x01000100, 0x00010100, 0x01010100,
  209. 0x00000001, 0x01000001, 0x00010001, 0x01010001,
  210. 0x00000101, 0x01000101, 0x00010101, 0x01010101,
  211. };
  212. /*
  213. * Initial Permutation macro
  214. */
  215. #define DES_IP(X,Y) \
  216. do \
  217. { \
  218. T = (((X) >> 4) ^ (Y)) & 0x0F0F0F0F; (Y) ^= T; (X) ^= (T << 4); \
  219. T = (((X) >> 16) ^ (Y)) & 0x0000FFFF; (Y) ^= T; (X) ^= (T << 16); \
  220. T = (((Y) >> 2) ^ (X)) & 0x33333333; (X) ^= T; (Y) ^= (T << 2); \
  221. T = (((Y) >> 8) ^ (X)) & 0x00FF00FF; (X) ^= T; (Y) ^= (T << 8); \
  222. (Y) = (((Y) << 1) | ((Y) >> 31)) & 0xFFFFFFFF; \
  223. T = ((X) ^ (Y)) & 0xAAAAAAAA; (Y) ^= T; (X) ^= T; \
  224. (X) = (((X) << 1) | ((X) >> 31)) & 0xFFFFFFFF; \
  225. } while( 0 )
  226. /*
  227. * Final Permutation macro
  228. */
  229. #define DES_FP(X,Y) \
  230. do \
  231. { \
  232. (X) = (((X) << 31) | ((X) >> 1)) & 0xFFFFFFFF; \
  233. T = ((X) ^ (Y)) & 0xAAAAAAAA; (X) ^= T; (Y) ^= T; \
  234. (Y) = (((Y) << 31) | ((Y) >> 1)) & 0xFFFFFFFF; \
  235. T = (((Y) >> 8) ^ (X)) & 0x00FF00FF; (X) ^= T; (Y) ^= (T << 8); \
  236. T = (((Y) >> 2) ^ (X)) & 0x33333333; (X) ^= T; (Y) ^= (T << 2); \
  237. T = (((X) >> 16) ^ (Y)) & 0x0000FFFF; (Y) ^= T; (X) ^= (T << 16); \
  238. T = (((X) >> 4) ^ (Y)) & 0x0F0F0F0F; (Y) ^= T; (X) ^= (T << 4); \
  239. } while( 0 )
  240. /*
  241. * DES round macro
  242. */
  243. #define DES_ROUND(X,Y) \
  244. do \
  245. { \
  246. T = *SK++ ^ (X); \
  247. (Y) ^= SB8[ (T ) & 0x3F ] ^ \
  248. SB6[ (T >> 8) & 0x3F ] ^ \
  249. SB4[ (T >> 16) & 0x3F ] ^ \
  250. SB2[ (T >> 24) & 0x3F ]; \
  251. \
  252. T = *SK++ ^ (((X) << 28) | ((X) >> 4)); \
  253. (Y) ^= SB7[ (T ) & 0x3F ] ^ \
  254. SB5[ (T >> 8) & 0x3F ] ^ \
  255. SB3[ (T >> 16) & 0x3F ] ^ \
  256. SB1[ (T >> 24) & 0x3F ]; \
  257. } while( 0 )
  258. #define SWAP(a,b) \
  259. do \
  260. { \
  261. uint32_t t = (a); (a) = (b); (b) = t; t = 0; \
  262. } while( 0 )
  263. void mbedtls_des_init( mbedtls_des_context *ctx )
  264. {
  265. memset( ctx, 0, sizeof( mbedtls_des_context ) );
  266. }
  267. void mbedtls_des_free( mbedtls_des_context *ctx )
  268. {
  269. if( ctx == NULL )
  270. return;
  271. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_des_context ) );
  272. }
  273. void mbedtls_des3_init( mbedtls_des3_context *ctx )
  274. {
  275. memset( ctx, 0, sizeof( mbedtls_des3_context ) );
  276. }
  277. void mbedtls_des3_free( mbedtls_des3_context *ctx )
  278. {
  279. if( ctx == NULL )
  280. return;
  281. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_des3_context ) );
  282. }
  283. static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
  284. 11, 13, 14, 16, 19, 21, 22, 25, 26, 28, 31, 32, 35, 37, 38, 41, 42, 44,
  285. 47, 49, 50, 52, 55, 56, 59, 61, 62, 64, 67, 69, 70, 73, 74, 76, 79, 81,
  286. 82, 84, 87, 88, 91, 93, 94, 97, 98, 100, 103, 104, 107, 109, 110, 112,
  287. 115, 117, 118, 121, 122, 124, 127, 128, 131, 133, 134, 137, 138, 140,
  288. 143, 145, 146, 148, 151, 152, 155, 157, 158, 161, 162, 164, 167, 168,
  289. 171, 173, 174, 176, 179, 181, 182, 185, 186, 188, 191, 193, 194, 196,
  290. 199, 200, 203, 205, 206, 208, 211, 213, 214, 217, 218, 220, 223, 224,
  291. 227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
  292. 254 };
  293. void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  294. {
  295. int i;
  296. for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  297. key[i] = odd_parity_table[key[i] / 2];
  298. }
  299. /*
  300. * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
  301. */
  302. int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  303. {
  304. int i;
  305. for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
  306. if( key[i] != odd_parity_table[key[i] / 2] )
  307. return( 1 );
  308. return( 0 );
  309. }
  310. /*
  311. * Table of weak and semi-weak keys
  312. *
  313. * Source: http://en.wikipedia.org/wiki/Weak_key
  314. *
  315. * Weak:
  316. * Alternating ones + zeros (0x0101010101010101)
  317. * Alternating 'F' + 'E' (0xFEFEFEFEFEFEFEFE)
  318. * '0xE0E0E0E0F1F1F1F1'
  319. * '0x1F1F1F1F0E0E0E0E'
  320. *
  321. * Semi-weak:
  322. * 0x011F011F010E010E and 0x1F011F010E010E01
  323. * 0x01E001E001F101F1 and 0xE001E001F101F101
  324. * 0x01FE01FE01FE01FE and 0xFE01FE01FE01FE01
  325. * 0x1FE01FE00EF10EF1 and 0xE01FE01FF10EF10E
  326. * 0x1FFE1FFE0EFE0EFE and 0xFE1FFE1FFE0EFE0E
  327. * 0xE0FEE0FEF1FEF1FE and 0xFEE0FEE0FEF1FEF1
  328. *
  329. */
  330. #define WEAK_KEY_COUNT 16
  331. static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
  332. {
  333. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  334. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  335. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
  336. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  337. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  338. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  339. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  340. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  341. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  342. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  343. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  344. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  345. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  346. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  347. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  348. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  349. };
  350. int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  351. {
  352. int i;
  353. for( i = 0; i < WEAK_KEY_COUNT; i++ )
  354. if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
  355. return( 1 );
  356. return( 0 );
  357. }
  358. #if !defined(MBEDTLS_DES_SETKEY_ALT)
  359. void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  360. {
  361. int i;
  362. uint32_t X, Y, T;
  363. X = MBEDTLS_GET_UINT32_BE( key, 0 );
  364. Y = MBEDTLS_GET_UINT32_BE( key, 4 );
  365. /*
  366. * Permuted Choice 1
  367. */
  368. T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
  369. T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
  370. X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
  371. | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
  372. | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
  373. | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
  374. Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
  375. | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
  376. | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
  377. | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
  378. X &= 0x0FFFFFFF;
  379. Y &= 0x0FFFFFFF;
  380. /*
  381. * calculate subkeys
  382. */
  383. for( i = 0; i < 16; i++ )
  384. {
  385. if( i < 2 || i == 8 || i == 15 )
  386. {
  387. X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
  388. Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
  389. }
  390. else
  391. {
  392. X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
  393. Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
  394. }
  395. *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
  396. | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
  397. | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
  398. | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
  399. | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
  400. | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
  401. | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
  402. | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
  403. | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
  404. | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
  405. | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
  406. *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
  407. | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
  408. | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
  409. | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
  410. | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
  411. | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
  412. | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
  413. | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
  414. | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
  415. | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
  416. | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
  417. }
  418. }
  419. #endif /* !MBEDTLS_DES_SETKEY_ALT */
  420. /*
  421. * DES key schedule (56-bit, encryption)
  422. */
  423. int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  424. {
  425. mbedtls_des_setkey( ctx->sk, key );
  426. return( 0 );
  427. }
  428. /*
  429. * DES key schedule (56-bit, decryption)
  430. */
  431. int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
  432. {
  433. int i;
  434. mbedtls_des_setkey( ctx->sk, key );
  435. for( i = 0; i < 16; i += 2 )
  436. {
  437. SWAP( ctx->sk[i ], ctx->sk[30 - i] );
  438. SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
  439. }
  440. return( 0 );
  441. }
  442. static void des3_set2key( uint32_t esk[96],
  443. uint32_t dsk[96],
  444. const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] )
  445. {
  446. int i;
  447. mbedtls_des_setkey( esk, key );
  448. mbedtls_des_setkey( dsk + 32, key + 8 );
  449. for( i = 0; i < 32; i += 2 )
  450. {
  451. dsk[i ] = esk[30 - i];
  452. dsk[i + 1] = esk[31 - i];
  453. esk[i + 32] = dsk[62 - i];
  454. esk[i + 33] = dsk[63 - i];
  455. esk[i + 64] = esk[i ];
  456. esk[i + 65] = esk[i + 1];
  457. dsk[i + 64] = dsk[i ];
  458. dsk[i + 65] = dsk[i + 1];
  459. }
  460. }
  461. /*
  462. * Triple-DES key schedule (112-bit, encryption)
  463. */
  464. int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
  465. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  466. {
  467. uint32_t sk[96];
  468. des3_set2key( ctx->sk, sk, key );
  469. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  470. return( 0 );
  471. }
  472. /*
  473. * Triple-DES key schedule (112-bit, decryption)
  474. */
  475. int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
  476. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
  477. {
  478. uint32_t sk[96];
  479. des3_set2key( sk, ctx->sk, key );
  480. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  481. return( 0 );
  482. }
  483. static void des3_set3key( uint32_t esk[96],
  484. uint32_t dsk[96],
  485. const unsigned char key[24] )
  486. {
  487. int i;
  488. mbedtls_des_setkey( esk, key );
  489. mbedtls_des_setkey( dsk + 32, key + 8 );
  490. mbedtls_des_setkey( esk + 64, key + 16 );
  491. for( i = 0; i < 32; i += 2 )
  492. {
  493. dsk[i ] = esk[94 - i];
  494. dsk[i + 1] = esk[95 - i];
  495. esk[i + 32] = dsk[62 - i];
  496. esk[i + 33] = dsk[63 - i];
  497. dsk[i + 64] = esk[30 - i];
  498. dsk[i + 65] = esk[31 - i];
  499. }
  500. }
  501. /*
  502. * Triple-DES key schedule (168-bit, encryption)
  503. */
  504. int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
  505. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  506. {
  507. uint32_t sk[96];
  508. des3_set3key( ctx->sk, sk, key );
  509. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  510. return( 0 );
  511. }
  512. /*
  513. * Triple-DES key schedule (168-bit, decryption)
  514. */
  515. int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
  516. const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
  517. {
  518. uint32_t sk[96];
  519. des3_set3key( sk, ctx->sk, key );
  520. mbedtls_platform_zeroize( sk, sizeof( sk ) );
  521. return( 0 );
  522. }
  523. /*
  524. * DES-ECB block encryption/decryption
  525. */
  526. #if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
  527. int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
  528. const unsigned char input[8],
  529. unsigned char output[8] )
  530. {
  531. int i;
  532. uint32_t X, Y, T, *SK;
  533. SK = ctx->sk;
  534. X = MBEDTLS_GET_UINT32_BE( input, 0 );
  535. Y = MBEDTLS_GET_UINT32_BE( input, 4 );
  536. DES_IP( X, Y );
  537. for( i = 0; i < 8; i++ )
  538. {
  539. DES_ROUND( Y, X );
  540. DES_ROUND( X, Y );
  541. }
  542. DES_FP( Y, X );
  543. MBEDTLS_PUT_UINT32_BE( Y, output, 0 );
  544. MBEDTLS_PUT_UINT32_BE( X, output, 4 );
  545. return( 0 );
  546. }
  547. #endif /* !MBEDTLS_DES_CRYPT_ECB_ALT */
  548. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  549. /*
  550. * DES-CBC buffer encryption/decryption
  551. */
  552. int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
  553. int mode,
  554. size_t length,
  555. unsigned char iv[8],
  556. const unsigned char *input,
  557. unsigned char *output )
  558. {
  559. int i;
  560. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  561. unsigned char temp[8];
  562. if( length % 8 )
  563. return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  564. if( mode == MBEDTLS_DES_ENCRYPT )
  565. {
  566. while( length > 0 )
  567. {
  568. for( i = 0; i < 8; i++ )
  569. output[i] = (unsigned char)( input[i] ^ iv[i] );
  570. ret = mbedtls_des_crypt_ecb( ctx, output, output );
  571. if( ret != 0 )
  572. goto exit;
  573. memcpy( iv, output, 8 );
  574. input += 8;
  575. output += 8;
  576. length -= 8;
  577. }
  578. }
  579. else /* MBEDTLS_DES_DECRYPT */
  580. {
  581. while( length > 0 )
  582. {
  583. memcpy( temp, input, 8 );
  584. ret = mbedtls_des_crypt_ecb( ctx, input, output );
  585. if( ret != 0 )
  586. goto exit;
  587. for( i = 0; i < 8; i++ )
  588. output[i] = (unsigned char)( output[i] ^ iv[i] );
  589. memcpy( iv, temp, 8 );
  590. input += 8;
  591. output += 8;
  592. length -= 8;
  593. }
  594. }
  595. ret = 0;
  596. exit:
  597. return( ret );
  598. }
  599. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  600. /*
  601. * 3DES-ECB block encryption/decryption
  602. */
  603. #if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
  604. int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
  605. const unsigned char input[8],
  606. unsigned char output[8] )
  607. {
  608. int i;
  609. uint32_t X, Y, T, *SK;
  610. SK = ctx->sk;
  611. X = MBEDTLS_GET_UINT32_BE( input, 0 );
  612. Y = MBEDTLS_GET_UINT32_BE( input, 4 );
  613. DES_IP( X, Y );
  614. for( i = 0; i < 8; i++ )
  615. {
  616. DES_ROUND( Y, X );
  617. DES_ROUND( X, Y );
  618. }
  619. for( i = 0; i < 8; i++ )
  620. {
  621. DES_ROUND( X, Y );
  622. DES_ROUND( Y, X );
  623. }
  624. for( i = 0; i < 8; i++ )
  625. {
  626. DES_ROUND( Y, X );
  627. DES_ROUND( X, Y );
  628. }
  629. DES_FP( Y, X );
  630. MBEDTLS_PUT_UINT32_BE( Y, output, 0 );
  631. MBEDTLS_PUT_UINT32_BE( X, output, 4 );
  632. return( 0 );
  633. }
  634. #endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */
  635. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  636. /*
  637. * 3DES-CBC buffer encryption/decryption
  638. */
  639. int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
  640. int mode,
  641. size_t length,
  642. unsigned char iv[8],
  643. const unsigned char *input,
  644. unsigned char *output )
  645. {
  646. int i;
  647. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  648. unsigned char temp[8];
  649. if( length % 8 )
  650. return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
  651. if( mode == MBEDTLS_DES_ENCRYPT )
  652. {
  653. while( length > 0 )
  654. {
  655. for( i = 0; i < 8; i++ )
  656. output[i] = (unsigned char)( input[i] ^ iv[i] );
  657. ret = mbedtls_des3_crypt_ecb( ctx, output, output );
  658. if( ret != 0 )
  659. goto exit;
  660. memcpy( iv, output, 8 );
  661. input += 8;
  662. output += 8;
  663. length -= 8;
  664. }
  665. }
  666. else /* MBEDTLS_DES_DECRYPT */
  667. {
  668. while( length > 0 )
  669. {
  670. memcpy( temp, input, 8 );
  671. ret = mbedtls_des3_crypt_ecb( ctx, input, output );
  672. if( ret != 0 )
  673. goto exit;
  674. for( i = 0; i < 8; i++ )
  675. output[i] = (unsigned char)( output[i] ^ iv[i] );
  676. memcpy( iv, temp, 8 );
  677. input += 8;
  678. output += 8;
  679. length -= 8;
  680. }
  681. }
  682. ret = 0;
  683. exit:
  684. return( ret );
  685. }
  686. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  687. #endif /* !MBEDTLS_DES_ALT */
  688. #if defined(MBEDTLS_SELF_TEST)
  689. /*
  690. * DES and 3DES test vectors from:
  691. *
  692. * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
  693. */
  694. static const unsigned char des3_test_keys[24] =
  695. {
  696. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  697. 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
  698. 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
  699. };
  700. static const unsigned char des3_test_buf[8] =
  701. {
  702. 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
  703. };
  704. static const unsigned char des3_test_ecb_dec[3][8] =
  705. {
  706. { 0x37, 0x2B, 0x98, 0xBF, 0x52, 0x65, 0xB0, 0x59 },
  707. { 0xC2, 0x10, 0x19, 0x9C, 0x38, 0x5A, 0x65, 0xA1 },
  708. { 0xA2, 0x70, 0x56, 0x68, 0x69, 0xE5, 0x15, 0x1D }
  709. };
  710. static const unsigned char des3_test_ecb_enc[3][8] =
  711. {
  712. { 0x1C, 0xD5, 0x97, 0xEA, 0x84, 0x26, 0x73, 0xFB },
  713. { 0xB3, 0x92, 0x4D, 0xF3, 0xC5, 0xB5, 0x42, 0x93 },
  714. { 0xDA, 0x37, 0x64, 0x41, 0xBA, 0x6F, 0x62, 0x6F }
  715. };
  716. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  717. static const unsigned char des3_test_iv[8] =
  718. {
  719. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
  720. };
  721. static const unsigned char des3_test_cbc_dec[3][8] =
  722. {
  723. { 0x58, 0xD9, 0x48, 0xEF, 0x85, 0x14, 0x65, 0x9A },
  724. { 0x5F, 0xC8, 0x78, 0xD4, 0xD7, 0x92, 0xD9, 0x54 },
  725. { 0x25, 0xF9, 0x75, 0x85, 0xA8, 0x1E, 0x48, 0xBF }
  726. };
  727. static const unsigned char des3_test_cbc_enc[3][8] =
  728. {
  729. { 0x91, 0x1C, 0x6D, 0xCF, 0x48, 0xA7, 0xC3, 0x4D },
  730. { 0x60, 0x1A, 0x76, 0x8F, 0xA1, 0xF9, 0x66, 0xF1 },
  731. { 0xA1, 0x50, 0x0F, 0x99, 0xB2, 0xCD, 0x64, 0x76 }
  732. };
  733. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  734. /*
  735. * Checkup routine
  736. */
  737. int mbedtls_des_self_test( int verbose )
  738. {
  739. int i, j, u, v, ret = 0;
  740. mbedtls_des_context ctx;
  741. mbedtls_des3_context ctx3;
  742. unsigned char buf[8];
  743. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  744. unsigned char prv[8];
  745. unsigned char iv[8];
  746. #endif
  747. mbedtls_des_init( &ctx );
  748. mbedtls_des3_init( &ctx3 );
  749. /*
  750. * ECB mode
  751. */
  752. for( i = 0; i < 6; i++ )
  753. {
  754. u = i >> 1;
  755. v = i & 1;
  756. if( verbose != 0 )
  757. mbedtls_printf( " DES%c-ECB-%3d (%s): ",
  758. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  759. ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  760. memcpy( buf, des3_test_buf, 8 );
  761. switch( i )
  762. {
  763. case 0:
  764. ret = mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  765. break;
  766. case 1:
  767. ret = mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  768. break;
  769. case 2:
  770. ret = mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  771. break;
  772. case 3:
  773. ret = mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  774. break;
  775. case 4:
  776. ret = mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  777. break;
  778. case 5:
  779. ret = mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  780. break;
  781. default:
  782. return( 1 );
  783. }
  784. if( ret != 0 )
  785. goto exit;
  786. for( j = 0; j < 100; j++ )
  787. {
  788. if( u == 0 )
  789. ret = mbedtls_des_crypt_ecb( &ctx, buf, buf );
  790. else
  791. ret = mbedtls_des3_crypt_ecb( &ctx3, buf, buf );
  792. if( ret != 0 )
  793. goto exit;
  794. }
  795. if( ( v == MBEDTLS_DES_DECRYPT &&
  796. memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
  797. ( v != MBEDTLS_DES_DECRYPT &&
  798. memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
  799. {
  800. if( verbose != 0 )
  801. mbedtls_printf( "failed\n" );
  802. ret = 1;
  803. goto exit;
  804. }
  805. if( verbose != 0 )
  806. mbedtls_printf( "passed\n" );
  807. }
  808. if( verbose != 0 )
  809. mbedtls_printf( "\n" );
  810. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  811. /*
  812. * CBC mode
  813. */
  814. for( i = 0; i < 6; i++ )
  815. {
  816. u = i >> 1;
  817. v = i & 1;
  818. if( verbose != 0 )
  819. mbedtls_printf( " DES%c-CBC-%3d (%s): ",
  820. ( u == 0 ) ? ' ' : '3', 56 + u * 56,
  821. ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
  822. memcpy( iv, des3_test_iv, 8 );
  823. memcpy( prv, des3_test_iv, 8 );
  824. memcpy( buf, des3_test_buf, 8 );
  825. switch( i )
  826. {
  827. case 0:
  828. ret = mbedtls_des_setkey_dec( &ctx, des3_test_keys );
  829. break;
  830. case 1:
  831. ret = mbedtls_des_setkey_enc( &ctx, des3_test_keys );
  832. break;
  833. case 2:
  834. ret = mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
  835. break;
  836. case 3:
  837. ret = mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
  838. break;
  839. case 4:
  840. ret = mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
  841. break;
  842. case 5:
  843. ret = mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
  844. break;
  845. default:
  846. return( 1 );
  847. }
  848. if( ret != 0 )
  849. goto exit;
  850. if( v == MBEDTLS_DES_DECRYPT )
  851. {
  852. for( j = 0; j < 100; j++ )
  853. {
  854. if( u == 0 )
  855. ret = mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  856. else
  857. ret = mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  858. if( ret != 0 )
  859. goto exit;
  860. }
  861. }
  862. else
  863. {
  864. for( j = 0; j < 100; j++ )
  865. {
  866. unsigned char tmp[8];
  867. if( u == 0 )
  868. ret = mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
  869. else
  870. ret = mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
  871. if( ret != 0 )
  872. goto exit;
  873. memcpy( tmp, prv, 8 );
  874. memcpy( prv, buf, 8 );
  875. memcpy( buf, tmp, 8 );
  876. }
  877. memcpy( buf, prv, 8 );
  878. }
  879. if( ( v == MBEDTLS_DES_DECRYPT &&
  880. memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
  881. ( v != MBEDTLS_DES_DECRYPT &&
  882. memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
  883. {
  884. if( verbose != 0 )
  885. mbedtls_printf( "failed\n" );
  886. ret = 1;
  887. goto exit;
  888. }
  889. if( verbose != 0 )
  890. mbedtls_printf( "passed\n" );
  891. }
  892. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  893. if( verbose != 0 )
  894. mbedtls_printf( "\n" );
  895. exit:
  896. mbedtls_des_free( &ctx );
  897. mbedtls_des3_free( &ctx3 );
  898. if( ret != 0 )
  899. ret = 1;
  900. return( ret );
  901. }
  902. #endif /* MBEDTLS_SELF_TEST */
  903. #endif /* MBEDTLS_DES_C */