des.c 31 KB

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