des.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. * refrenced from https://github.com/fffaraz/cppDES
  11. */
  12. #ifndef __ASIO2_DES_IMPL_HPP__
  13. #define __ASIO2_DES_IMPL_HPP__
  14. #include <cassert>
  15. #include <cstdint>
  16. #include <cstring>
  17. #include <string>
  18. #include <sstream>
  19. namespace asio2
  20. {
  21. class des
  22. {
  23. public:
  24. des(uint64_t key)
  25. {
  26. keygen(key);
  27. }
  28. /*
  29. * key.size() should be 8,if less than 8,key will be padded with '\0',
  30. * if greater than 8,key will be truncate to 8
  31. */
  32. des(std::string key)
  33. {
  34. uint64_t k = 0;
  35. key.resize(sizeof(k));
  36. std::memcpy((void*)&k, (const void*)key.data(), (std::min)(key.size(), sizeof(k)));
  37. keygen(k);
  38. }
  39. ~des()
  40. {
  41. }
  42. des(const des & other)
  43. {
  44. std::memcpy((void *)(this->sub_key), (const void *)(other.sub_key), sizeof(this->sub_key));
  45. }
  46. des & operator=(const des & other)
  47. {
  48. std::memcpy((void *)(this->sub_key), (const void *)(other.sub_key), sizeof(this->sub_key));
  49. return (*this);
  50. }
  51. des(des && other)
  52. {
  53. std::memcpy((void *)(this->sub_key), (const void *)(other.sub_key), sizeof(this->sub_key));
  54. }
  55. des & operator=(des && other)
  56. {
  57. std::memcpy((void *)(this->sub_key), (const void *)(other.sub_key), sizeof(this->sub_key));
  58. return (*this);
  59. }
  60. uint64_t encrypt(uint64_t block)
  61. {
  62. return do_des(block, false);
  63. }
  64. uint64_t decrypt(uint64_t block)
  65. {
  66. return do_des(block, true);
  67. }
  68. /*
  69. * note : if msg contains '\0',there may be a wrong result when decrypt
  70. */
  71. std::string encrypt(std::string msg)
  72. {
  73. if (msg.empty())
  74. return std::string{};
  75. // Amount of padding needed
  76. uint8_t padding = uint8_t(0);
  77. if ((msg.size() % 8) != 0)
  78. {
  79. padding = uint8_t(8 - (msg.size() % 8));
  80. msg.resize(msg.size() + padding);
  81. }
  82. uint64_t block = uint64_t(0);
  83. uint8_t * buf = (uint8_t*)msg.data();
  84. for (std::size_t i = 0; i < msg.size();)
  85. {
  86. i += 8;
  87. memcpy(&block, buf, 8);
  88. if (!(i < msg.size()))
  89. {
  90. // Pad block with a 1 followed by 0s
  91. uint8_t shift = static_cast<uint8_t>(padding * uint8_t(8));
  92. block <<= shift;
  93. //buffer |= uint64_t(0x0000000000000001) << (shift - uint8_t(1));
  94. }
  95. block = encrypt(block);
  96. memcpy(buf, &block, 8);
  97. buf += 8;
  98. }
  99. return msg;
  100. }
  101. std::string decrypt(std::string msg)
  102. {
  103. if (msg.empty() || (msg.size() % 8) != 0)
  104. return std::string();
  105. uint64_t block = uint64_t(0);
  106. uint8_t * buf = (uint8_t*)msg.data();
  107. for (std::size_t i = 0; i < msg.size();)
  108. {
  109. i += 8;
  110. memcpy(&block, buf, 8);
  111. block = decrypt(block);
  112. if (!(i < msg.size()))
  113. {
  114. // Amount of padding on file
  115. [[maybe_unused]] uint8_t padding = uint8_t(0);
  116. // Check for and record padding on end
  117. while (!(block & uint64_t(0x00000000000000ff)))
  118. {
  119. block >>= 8;
  120. ++padding;
  121. }
  122. }
  123. memcpy(buf, &block, 8);
  124. buf += 8;
  125. }
  126. while (!msg.empty() && msg.back() == '\0')
  127. msg.erase(msg.size() - 1);
  128. return msg;
  129. }
  130. protected:
  131. uint64_t do_des(uint64_t block, bool mode)
  132. {
  133. // applying initial permutation
  134. block = ip(block);
  135. // dividing T' into two 32-bit parts
  136. uint32_t L = static_cast<uint32_t>((block >> 32) & L64_MASK);
  137. uint32_t R = static_cast<uint32_t>(block & L64_MASK);
  138. // 16 rounds
  139. for (uint8_t i = 0; i < 16; ++i)
  140. {
  141. uint32_t F = mode ? f(R, sub_key[15 - i]) : f(R, sub_key[i]);
  142. feistel(L, R, F);
  143. }
  144. // swapping the two parts
  145. block = (((uint64_t)R) << 32) | (uint64_t)L;
  146. // applying final permutation
  147. return fp(block);
  148. }
  149. void keygen(uint64_t key)
  150. {
  151. // initial key schedule calculation
  152. uint64_t permuted_choice_1 = 0; // 56 bits
  153. for (uint8_t i = 0; i < 56; ++i)
  154. {
  155. permuted_choice_1 <<= 1;
  156. permuted_choice_1 |= (key >> (64 - PC1[i])) & LB64_MASK;
  157. }
  158. // 28 bits
  159. uint32_t C = (uint32_t)((permuted_choice_1 >> 28) & 0x000000000fffffff);
  160. uint32_t D = (uint32_t)(permuted_choice_1 & 0x000000000fffffff);
  161. // Calculation of the 16 keys
  162. for (uint8_t i = 0; i < 16; ++i)
  163. {
  164. // key schedule, shifting Ci and Di
  165. for (uint8_t j = 0; j < ITERATION_SHIFT[i]; ++j)
  166. {
  167. C = (0x0fffffff & (C << 1)) | (0x00000001 & (C >> 27));
  168. D = (0x0fffffff & (D << 1)) | (0x00000001 & (D >> 27));
  169. }
  170. uint64_t permuted_choice_2 = (((uint64_t)C) << 28) | (uint64_t)D;
  171. sub_key[i] = 0; // 48 bits (2*24)
  172. for (uint8_t j = 0; j < 48; ++j)
  173. {
  174. sub_key[i] <<= 1;
  175. sub_key[i] |= (permuted_choice_2 >> (56 - PC2[j])) & LB64_MASK;
  176. }
  177. }
  178. }
  179. uint64_t ip(uint64_t block)
  180. {
  181. // initial permutation
  182. uint64_t result = 0;
  183. for (uint8_t i = 0; i < 64; ++i)
  184. {
  185. result <<= 1;
  186. result |= (block >> (64 - IP[i])) & LB64_MASK;
  187. }
  188. return result;
  189. }
  190. uint64_t fp(uint64_t block)
  191. {
  192. // inverse initial permutation
  193. uint64_t result = 0;
  194. for (uint8_t i = 0; i < 64; ++i)
  195. {
  196. result <<= 1;
  197. result |= (block >> (64 - FP[i])) & LB64_MASK;
  198. }
  199. return result;
  200. }
  201. void feistel(uint32_t &L, uint32_t &R, uint32_t F)
  202. {
  203. uint32_t temp = R;
  204. R = L ^ F;
  205. L = temp;
  206. }
  207. uint32_t f(uint32_t R, uint64_t k) // f(R,k) function
  208. {
  209. // applying expansion permutation and returning 48-bit data
  210. uint64_t s_input = 0;
  211. for (uint8_t i = 0; i < 48; ++i)
  212. {
  213. s_input <<= 1;
  214. s_input |= (uint64_t)((R >> (32 - EXPANSION[i])) & LB32_MASK);
  215. }
  216. // XORing expanded Ri with Ki, the round key
  217. s_input = s_input ^ k;
  218. // applying S-Boxes function and returning 32-bit data
  219. uint32_t s_output = 0;
  220. for (uint8_t i = 0; i < 8; ++i)
  221. {
  222. // Outer bits
  223. char row = static_cast<char>((s_input & (0x0000840000000000 >> 6 * i)) >> (42 - 6 * i));
  224. row = static_cast<char>((row >> 4) | (row & 0x01));
  225. // Middle 4 bits of input
  226. char column = (char)((s_input & (0x0000780000000000 >> 6 * i)) >> (43 - 6 * i));
  227. s_output <<= 4;
  228. s_output |= (uint32_t)(SBOX[i][16 * row + column] & 0x0f);
  229. }
  230. // applying the round permutation
  231. uint32_t f_result = 0;
  232. for (uint8_t i = 0; i < 32; ++i)
  233. {
  234. f_result <<= 1;
  235. f_result |= (s_output >> (32 - PBOX[i])) & LB32_MASK;
  236. }
  237. return f_result;
  238. }
  239. private:
  240. uint64_t sub_key[16] = { 0 }; // 48 bits each
  241. // Permuted Choice 1 Table [7*8]
  242. const char PC1[7 * 8] =
  243. {
  244. 57, 49, 41, 33, 25, 17, 9,
  245. 1, 58, 50, 42, 34, 26, 18,
  246. 10, 2, 59, 51, 43, 35, 27,
  247. 19, 11, 3, 60, 52, 44, 36,
  248. 63, 55, 47, 39, 31, 23, 15,
  249. 7, 62, 54, 46, 38, 30, 22,
  250. 14, 6, 61, 53, 45, 37, 29,
  251. 21, 13, 5, 28, 20, 12, 4
  252. };
  253. // Permuted Choice 2 Table [6*8]
  254. const char PC2[6 * 8] =
  255. {
  256. 14, 17, 11, 24, 1, 5,
  257. 3, 28, 15, 6, 21, 10,
  258. 23, 19, 12, 4, 26, 8,
  259. 16, 7, 27, 20, 13, 2,
  260. 41, 52, 31, 37, 47, 55,
  261. 30, 40, 51, 45, 33, 48,
  262. 44, 49, 39, 56, 34, 53,
  263. 46, 42, 50, 36, 29, 32
  264. };
  265. // Iteration Shift Array
  266. const char ITERATION_SHIFT[16] =
  267. {
  268. // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  269. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  270. };
  271. const uint32_t LB32_MASK = 0x00000001;
  272. const uint64_t LB64_MASK = 0x0000000000000001;
  273. const uint64_t L64_MASK = 0x00000000ffffffff;
  274. // Initial Permutation Table [8*8]
  275. const char IP[8 * 8] =
  276. {
  277. 58, 50, 42, 34, 26, 18, 10, 2,
  278. 60, 52, 44, 36, 28, 20, 12, 4,
  279. 62, 54, 46, 38, 30, 22, 14, 6,
  280. 64, 56, 48, 40, 32, 24, 16, 8,
  281. 57, 49, 41, 33, 25, 17, 9, 1,
  282. 59, 51, 43, 35, 27, 19, 11, 3,
  283. 61, 53, 45, 37, 29, 21, 13, 5,
  284. 63, 55, 47, 39, 31, 23, 15, 7
  285. };
  286. // Inverse Initial Permutation Table [8*8]
  287. const char FP[8 * 8] =
  288. {
  289. 40, 8, 48, 16, 56, 24, 64, 32,
  290. 39, 7, 47, 15, 55, 23, 63, 31,
  291. 38, 6, 46, 14, 54, 22, 62, 30,
  292. 37, 5, 45, 13, 53, 21, 61, 29,
  293. 36, 4, 44, 12, 52, 20, 60, 28,
  294. 35, 3, 43, 11, 51, 19, 59, 27,
  295. 34, 2, 42, 10, 50, 18, 58, 26,
  296. 33, 1, 41, 9, 49, 17, 57, 25
  297. };
  298. // Expansion table [6*8]
  299. const char EXPANSION[6 * 8] =
  300. {
  301. 32, 1, 2, 3, 4, 5,
  302. 4, 5, 6, 7, 8, 9,
  303. 8, 9, 10, 11, 12, 13,
  304. 12, 13, 14, 15, 16, 17,
  305. 16, 17, 18, 19, 20, 21,
  306. 20, 21, 22, 23, 24, 25,
  307. 24, 25, 26, 27, 28, 29,
  308. 28, 29, 30, 31, 32, 1
  309. };
  310. // The S-Box tables [8*16*4]
  311. const char SBOX[8][64] =
  312. {
  313. {
  314. // S1
  315. 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  316. 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  317. 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  318. 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
  319. },
  320. {
  321. // S2
  322. 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  323. 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  324. 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  325. 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
  326. },
  327. {
  328. // S3
  329. 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  330. 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  331. 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  332. 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
  333. },
  334. {
  335. // S4
  336. 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  337. 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  338. 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  339. 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
  340. },
  341. {
  342. // S5
  343. 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  344. 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  345. 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  346. 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
  347. },
  348. {
  349. // S6
  350. 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  351. 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  352. 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  353. 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
  354. },
  355. {
  356. // S7
  357. 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  358. 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  359. 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  360. 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
  361. },
  362. {
  363. // S8
  364. 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  365. 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  366. 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  367. 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
  368. }
  369. };
  370. // Post S-Box permutation [4*8]
  371. const char PBOX[4 * 8] =
  372. {
  373. 16, 7, 20, 21,
  374. 29, 12, 28, 17,
  375. 1, 15, 23, 26,
  376. 5, 18, 31, 10,
  377. 2, 8, 24, 14,
  378. 32, 27, 3, 9,
  379. 19, 13, 30, 6,
  380. 22, 11, 4, 25
  381. };
  382. };
  383. }
  384. #endif // !__ASIO2_DES_IMPL_HPP__