des.c 31 KB

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