des.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 i;
  548. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  549. unsigned char temp[8];
  550. if (length % 8) {
  551. return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH;
  552. }
  553. if (mode == MBEDTLS_DES_ENCRYPT) {
  554. while (length > 0) {
  555. for (i = 0; i < 8; i++) {
  556. output[i] = (unsigned char) (input[i] ^ iv[i]);
  557. }
  558. ret = mbedtls_des_crypt_ecb(ctx, output, output);
  559. if (ret != 0) {
  560. goto exit;
  561. }
  562. memcpy(iv, output, 8);
  563. input += 8;
  564. output += 8;
  565. length -= 8;
  566. }
  567. } else { /* MBEDTLS_DES_DECRYPT */
  568. while (length > 0) {
  569. memcpy(temp, input, 8);
  570. ret = mbedtls_des_crypt_ecb(ctx, input, output);
  571. if (ret != 0) {
  572. goto exit;
  573. }
  574. for (i = 0; i < 8; i++) {
  575. output[i] = (unsigned char) (output[i] ^ iv[i]);
  576. }
  577. memcpy(iv, temp, 8);
  578. input += 8;
  579. output += 8;
  580. length -= 8;
  581. }
  582. }
  583. ret = 0;
  584. exit:
  585. return ret;
  586. }
  587. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  588. /*
  589. * 3DES-ECB block encryption/decryption
  590. */
  591. #if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
  592. int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
  593. const unsigned char input[8],
  594. unsigned char output[8])
  595. {
  596. int i;
  597. uint32_t X, Y, T, *SK;
  598. SK = ctx->sk;
  599. X = MBEDTLS_GET_UINT32_BE(input, 0);
  600. Y = MBEDTLS_GET_UINT32_BE(input, 4);
  601. DES_IP(X, Y);
  602. for (i = 0; i < 8; i++) {
  603. DES_ROUND(Y, X);
  604. DES_ROUND(X, Y);
  605. }
  606. for (i = 0; i < 8; i++) {
  607. DES_ROUND(X, Y);
  608. DES_ROUND(Y, X);
  609. }
  610. for (i = 0; i < 8; i++) {
  611. DES_ROUND(Y, X);
  612. DES_ROUND(X, Y);
  613. }
  614. DES_FP(Y, X);
  615. MBEDTLS_PUT_UINT32_BE(Y, output, 0);
  616. MBEDTLS_PUT_UINT32_BE(X, output, 4);
  617. return 0;
  618. }
  619. #endif /* !MBEDTLS_DES3_CRYPT_ECB_ALT */
  620. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  621. /*
  622. * 3DES-CBC buffer encryption/decryption
  623. */
  624. int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
  625. int mode,
  626. size_t length,
  627. unsigned char iv[8],
  628. const unsigned char *input,
  629. unsigned char *output)
  630. {
  631. int i;
  632. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  633. unsigned char temp[8];
  634. if (length % 8) {
  635. return MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH;
  636. }
  637. if (mode == MBEDTLS_DES_ENCRYPT) {
  638. while (length > 0) {
  639. for (i = 0; i < 8; i++) {
  640. output[i] = (unsigned char) (input[i] ^ iv[i]);
  641. }
  642. ret = mbedtls_des3_crypt_ecb(ctx, output, output);
  643. if (ret != 0) {
  644. goto exit;
  645. }
  646. memcpy(iv, output, 8);
  647. input += 8;
  648. output += 8;
  649. length -= 8;
  650. }
  651. } else { /* MBEDTLS_DES_DECRYPT */
  652. while (length > 0) {
  653. memcpy(temp, input, 8);
  654. ret = mbedtls_des3_crypt_ecb(ctx, input, output);
  655. if (ret != 0) {
  656. goto exit;
  657. }
  658. for (i = 0; i < 8; i++) {
  659. output[i] = (unsigned char) (output[i] ^ iv[i]);
  660. }
  661. memcpy(iv, temp, 8);
  662. input += 8;
  663. output += 8;
  664. length -= 8;
  665. }
  666. }
  667. ret = 0;
  668. exit:
  669. return ret;
  670. }
  671. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  672. #endif /* !MBEDTLS_DES_ALT */
  673. #if defined(MBEDTLS_SELF_TEST)
  674. /*
  675. * DES and 3DES test vectors from:
  676. *
  677. * http://csrc.nist.gov/groups/STM/cavp/documents/des/tripledes-vectors.zip
  678. */
  679. static const unsigned char des3_test_keys[24] =
  680. {
  681. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  682. 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
  683. 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23
  684. };
  685. static const unsigned char des3_test_buf[8] =
  686. {
  687. 0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
  688. };
  689. static const unsigned char des3_test_ecb_dec[3][8] =
  690. {
  691. { 0x37, 0x2B, 0x98, 0xBF, 0x52, 0x65, 0xB0, 0x59 },
  692. { 0xC2, 0x10, 0x19, 0x9C, 0x38, 0x5A, 0x65, 0xA1 },
  693. { 0xA2, 0x70, 0x56, 0x68, 0x69, 0xE5, 0x15, 0x1D }
  694. };
  695. static const unsigned char des3_test_ecb_enc[3][8] =
  696. {
  697. { 0x1C, 0xD5, 0x97, 0xEA, 0x84, 0x26, 0x73, 0xFB },
  698. { 0xB3, 0x92, 0x4D, 0xF3, 0xC5, 0xB5, 0x42, 0x93 },
  699. { 0xDA, 0x37, 0x64, 0x41, 0xBA, 0x6F, 0x62, 0x6F }
  700. };
  701. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  702. static const unsigned char des3_test_iv[8] =
  703. {
  704. 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
  705. };
  706. static const unsigned char des3_test_cbc_dec[3][8] =
  707. {
  708. { 0x58, 0xD9, 0x48, 0xEF, 0x85, 0x14, 0x65, 0x9A },
  709. { 0x5F, 0xC8, 0x78, 0xD4, 0xD7, 0x92, 0xD9, 0x54 },
  710. { 0x25, 0xF9, 0x75, 0x85, 0xA8, 0x1E, 0x48, 0xBF }
  711. };
  712. static const unsigned char des3_test_cbc_enc[3][8] =
  713. {
  714. { 0x91, 0x1C, 0x6D, 0xCF, 0x48, 0xA7, 0xC3, 0x4D },
  715. { 0x60, 0x1A, 0x76, 0x8F, 0xA1, 0xF9, 0x66, 0xF1 },
  716. { 0xA1, 0x50, 0x0F, 0x99, 0xB2, 0xCD, 0x64, 0x76 }
  717. };
  718. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  719. /*
  720. * Checkup routine
  721. */
  722. int mbedtls_des_self_test(int verbose)
  723. {
  724. int i, j, u, v, ret = 0;
  725. mbedtls_des_context ctx;
  726. mbedtls_des3_context ctx3;
  727. unsigned char buf[8];
  728. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  729. unsigned char prv[8];
  730. unsigned char iv[8];
  731. #endif
  732. mbedtls_des_init(&ctx);
  733. mbedtls_des3_init(&ctx3);
  734. /*
  735. * ECB mode
  736. */
  737. for (i = 0; i < 6; i++) {
  738. u = i >> 1;
  739. v = i & 1;
  740. if (verbose != 0) {
  741. mbedtls_printf(" DES%c-ECB-%3d (%s): ",
  742. (u == 0) ? ' ' : '3', 56 + u * 56,
  743. (v == MBEDTLS_DES_DECRYPT) ? "dec" : "enc");
  744. }
  745. memcpy(buf, des3_test_buf, 8);
  746. switch (i) {
  747. case 0:
  748. ret = mbedtls_des_setkey_dec(&ctx, des3_test_keys);
  749. break;
  750. case 1:
  751. ret = mbedtls_des_setkey_enc(&ctx, des3_test_keys);
  752. break;
  753. case 2:
  754. ret = mbedtls_des3_set2key_dec(&ctx3, des3_test_keys);
  755. break;
  756. case 3:
  757. ret = mbedtls_des3_set2key_enc(&ctx3, des3_test_keys);
  758. break;
  759. case 4:
  760. ret = mbedtls_des3_set3key_dec(&ctx3, des3_test_keys);
  761. break;
  762. case 5:
  763. ret = mbedtls_des3_set3key_enc(&ctx3, des3_test_keys);
  764. break;
  765. default:
  766. return 1;
  767. }
  768. if (ret != 0) {
  769. goto exit;
  770. }
  771. for (j = 0; j < 100; j++) {
  772. if (u == 0) {
  773. ret = mbedtls_des_crypt_ecb(&ctx, buf, buf);
  774. } else {
  775. ret = mbedtls_des3_crypt_ecb(&ctx3, buf, buf);
  776. }
  777. if (ret != 0) {
  778. goto exit;
  779. }
  780. }
  781. if ((v == MBEDTLS_DES_DECRYPT &&
  782. memcmp(buf, des3_test_ecb_dec[u], 8) != 0) ||
  783. (v != MBEDTLS_DES_DECRYPT &&
  784. memcmp(buf, des3_test_ecb_enc[u], 8) != 0)) {
  785. if (verbose != 0) {
  786. mbedtls_printf("failed\n");
  787. }
  788. ret = 1;
  789. goto exit;
  790. }
  791. if (verbose != 0) {
  792. mbedtls_printf("passed\n");
  793. }
  794. }
  795. if (verbose != 0) {
  796. mbedtls_printf("\n");
  797. }
  798. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  799. /*
  800. * CBC mode
  801. */
  802. for (i = 0; i < 6; i++) {
  803. u = i >> 1;
  804. v = i & 1;
  805. if (verbose != 0) {
  806. mbedtls_printf(" DES%c-CBC-%3d (%s): ",
  807. (u == 0) ? ' ' : '3', 56 + u * 56,
  808. (v == MBEDTLS_DES_DECRYPT) ? "dec" : "enc");
  809. }
  810. memcpy(iv, des3_test_iv, 8);
  811. memcpy(prv, des3_test_iv, 8);
  812. memcpy(buf, des3_test_buf, 8);
  813. switch (i) {
  814. case 0:
  815. ret = mbedtls_des_setkey_dec(&ctx, des3_test_keys);
  816. break;
  817. case 1:
  818. ret = mbedtls_des_setkey_enc(&ctx, des3_test_keys);
  819. break;
  820. case 2:
  821. ret = mbedtls_des3_set2key_dec(&ctx3, des3_test_keys);
  822. break;
  823. case 3:
  824. ret = mbedtls_des3_set2key_enc(&ctx3, des3_test_keys);
  825. break;
  826. case 4:
  827. ret = mbedtls_des3_set3key_dec(&ctx3, des3_test_keys);
  828. break;
  829. case 5:
  830. ret = mbedtls_des3_set3key_enc(&ctx3, des3_test_keys);
  831. break;
  832. default:
  833. return 1;
  834. }
  835. if (ret != 0) {
  836. goto exit;
  837. }
  838. if (v == MBEDTLS_DES_DECRYPT) {
  839. for (j = 0; j < 100; j++) {
  840. if (u == 0) {
  841. ret = mbedtls_des_crypt_cbc(&ctx, v, 8, iv, buf, buf);
  842. } else {
  843. ret = mbedtls_des3_crypt_cbc(&ctx3, v, 8, iv, buf, buf);
  844. }
  845. if (ret != 0) {
  846. goto exit;
  847. }
  848. }
  849. } else {
  850. for (j = 0; j < 100; j++) {
  851. unsigned char tmp[8];
  852. if (u == 0) {
  853. ret = mbedtls_des_crypt_cbc(&ctx, v, 8, iv, buf, buf);
  854. } else {
  855. ret = mbedtls_des3_crypt_cbc(&ctx3, v, 8, iv, buf, buf);
  856. }
  857. if (ret != 0) {
  858. goto exit;
  859. }
  860. memcpy(tmp, prv, 8);
  861. memcpy(prv, buf, 8);
  862. memcpy(buf, tmp, 8);
  863. }
  864. memcpy(buf, prv, 8);
  865. }
  866. if ((v == MBEDTLS_DES_DECRYPT &&
  867. memcmp(buf, des3_test_cbc_dec[u], 8) != 0) ||
  868. (v != MBEDTLS_DES_DECRYPT &&
  869. memcmp(buf, des3_test_cbc_enc[u], 8) != 0)) {
  870. if (verbose != 0) {
  871. mbedtls_printf("failed\n");
  872. }
  873. ret = 1;
  874. goto exit;
  875. }
  876. if (verbose != 0) {
  877. mbedtls_printf("passed\n");
  878. }
  879. }
  880. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  881. if (verbose != 0) {
  882. mbedtls_printf("\n");
  883. }
  884. exit:
  885. mbedtls_des_free(&ctx);
  886. mbedtls_des3_free(&ctx3);
  887. if (ret != 0) {
  888. ret = 1;
  889. }
  890. return ret;
  891. }
  892. #endif /* MBEDTLS_SELF_TEST */
  893. #endif /* MBEDTLS_DES_C */