des.c 31 KB

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