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