des.c 32 KB

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