des.c 31 KB

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