des.c 31 KB

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