des.c 32 KB

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