des.c 33 KB

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