des.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /**
  2. * @file des.c
  3. * @brief DES (Data Encryption Standard)
  4. *
  5. * @section License
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * Copyright (C) 2010-2023 Oryx Embedded SARL. All rights reserved.
  10. *
  11. * This file is part of CycloneCRYPTO Open.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. *
  27. * @section Description
  28. *
  29. * DES is an encryption algorithm designed to encipher and decipher blocks of
  30. * 64 bits under control of a 64-bit key. Refer to FIPS 46-3 for more details
  31. *
  32. * @author Oryx Embedded SARL (www.oryx-embedded.com)
  33. * @version 2.2.4
  34. **/
  35. //Switch to the appropriate trace level
  36. #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
  37. //Dependencies
  38. #include "core/crypto.h"
  39. #include "cipher/des.h"
  40. //Check crypto library configuration
  41. #if (DES_SUPPORT == ENABLED || DES3_SUPPORT == ENABLED)
  42. //Rotate left operation
  43. #define ROL28(a, n) ((((a) << (n)) | ((a) >> (28 - (n)))) & 0x0FFFFFFF)
  44. //Permutation of bit fields between words (Eric Young's technique)
  45. #define SWAPMOVE(a, b, n, m) \
  46. { \
  47. t = ((a >> n) ^ b) & m; \
  48. b ^= t; \
  49. a ^= t << n; \
  50. }
  51. //Initial permutation
  52. #define IP(l, r) \
  53. { \
  54. SWAPMOVE(l, r, 4, 0x0F0F0F0F); \
  55. SWAPMOVE(l, r, 16, 0x0000FFFF); \
  56. SWAPMOVE(r, l, 2, 0x33333333); \
  57. SWAPMOVE(r, l, 8, 0x00FF00FF); \
  58. SWAPMOVE(l, r, 1, 0x55555555); \
  59. l = ROL32(l, 1); \
  60. r = ROL32(r, 1); \
  61. }
  62. //Inverse of initial permutation
  63. #define IP_INV(l, r) \
  64. { \
  65. l = ROR32(l, 1); \
  66. r = ROR32(r, 1); \
  67. SWAPMOVE(l, r, 1, 0x55555555); \
  68. SWAPMOVE(r, l, 8, 0x00FF00FF); \
  69. SWAPMOVE(r, l, 2, 0x33333333); \
  70. SWAPMOVE(l, r, 16, 0x0000FFFF); \
  71. SWAPMOVE(l, r, 4, 0x0F0F0F0F); \
  72. }
  73. //Permuted choice 1
  74. #define PC1(c, d) \
  75. { \
  76. SWAPMOVE(c, d, 4, 0x0F0F0F0F); \
  77. SWAPMOVE(c, d, 16, 0x0000FFFF); \
  78. SWAPMOVE(d, c, 2, 0x33333333); \
  79. SWAPMOVE(d, c, 8, 0x00FF00FF); \
  80. SWAPMOVE(c, d, 1, 0x55555555); \
  81. SWAPMOVE(d, c, 8, 0x00FF00FF); \
  82. SWAPMOVE(c, d, 16, 0x0000FFFF); \
  83. t = (c << 4) & 0x0FFFFFF0; \
  84. t |= (d >> 24) & 0x0000000F; \
  85. c = (d << 20) & 0x0FF00000; \
  86. c |= (d << 4) & 0x000FF000; \
  87. c |= (d >> 12) & 0x00000FF0; \
  88. c |= (d >> 28) & 0x0000000F; \
  89. d = t; \
  90. }
  91. //Permuted choice 2 (first half)
  92. #define PC2_L(c, d) \
  93. (((c << 4) & 0x24000000) | \
  94. ((c << 28) & 0x10000000) | \
  95. ((c << 14) & 0x08000000) | \
  96. ((c << 18) & 0x02080000) | \
  97. ((c << 6) & 0x01000000) | \
  98. ((c << 9) & 0x00200000) | \
  99. ((c >> 1) & 0x00100000) | \
  100. ((c << 10) & 0x00040000) | \
  101. ((c << 2) & 0x00020000) | \
  102. ((c >> 10) & 0x00010000) | \
  103. ((d >> 13) & 0x00002000) | \
  104. ((d >> 4) & 0x00001000) | \
  105. ((d << 6) & 0x00000800) | \
  106. ((d >> 1) & 0x00000400) | \
  107. ((d >> 14) & 0x00000200) | \
  108. ((d >> 0) & 0x00000100) | \
  109. ((d >> 5) & 0x00000020) | \
  110. ((d >> 10) & 0x00000010) | \
  111. ((d >> 3) & 0x00000008) | \
  112. ((d >> 18) & 0x00000004) | \
  113. ((d >> 26) & 0x00000002) | \
  114. ((d >> 24) & 0x00000001))
  115. //Permuted choice 2 (second half)
  116. #define PC2_R(c, d) \
  117. (((c << 15) & 0x20000000) | \
  118. ((c << 17) & 0x10000000) | \
  119. ((c << 10) & 0x08000000) | \
  120. ((c << 22) & 0x04000000) | \
  121. ((c >> 2) & 0x02000000) | \
  122. ((c << 1) & 0x01000000) | \
  123. ((c << 16) & 0x00200000) | \
  124. ((c << 11) & 0x00100000) | \
  125. ((c << 3) & 0x00080000) | \
  126. ((c >> 6) & 0x00040000) | \
  127. ((c << 15) & 0x00020000) | \
  128. ((c >> 4) & 0x00010000) | \
  129. ((d >> 2) & 0x00002000) | \
  130. ((d << 8) & 0x00001000) | \
  131. ((d >> 14) & 0x00000808) | \
  132. ((d >> 9) & 0x00000400) | \
  133. ((d >> 0) & 0x00000200) | \
  134. ((d << 7) & 0x00000100) | \
  135. ((d >> 7) & 0x00000020) | \
  136. ((d >> 3) & 0x00000011) | \
  137. ((d << 2) & 0x00000004) | \
  138. ((d >> 21) & 0x00000002))
  139. //Round function
  140. #define ROUND(l, r, k1, k2) \
  141. { \
  142. t = r ^ k1; \
  143. l ^= sp2[(t >> 24) & 0x3F]; \
  144. l ^= sp4[(t >> 16) & 0x3F]; \
  145. l ^= sp6[(t >> 8) & 0x3F]; \
  146. l ^= sp8[t & 0x3F]; \
  147. t = ROR32(r, 4) ^ k2; \
  148. l ^= sp1[(t >> 24) & 0x3F]; \
  149. l ^= sp3[(t >> 16) & 0x3F]; \
  150. l ^= sp5[(t >> 8) & 0x3F]; \
  151. l ^= sp7[t & 0x3F]; \
  152. }
  153. //Selection function 1
  154. static const uint32_t sp1[64] =
  155. {
  156. 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004, 0x00010000,
  157. 0x00000400, 0x01010400, 0x01010404, 0x00000400, 0x01000404, 0x01010004, 0x01000000, 0x00000004,
  158. 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
  159. 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404, 0x00010404, 0x01000000,
  160. 0x00010000, 0x01010404, 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000, 0x00000400,
  161. 0x01010004, 0x00010000, 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
  162. 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404, 0x00010404, 0x01010400,
  163. 0x00000404, 0x01000400, 0x01000400, 0x00000000, 0x00010004, 0x00010400, 0x00000000, 0x01010004
  164. };
  165. //Selection function 2
  166. static const uint32_t sp2[64] =
  167. {
  168. 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020, 0x80008020,
  169. 0x80000020, 0x80108020, 0x80108000, 0x80000000, 0x80008000, 0x00100000, 0x00000020, 0x80100020,
  170. 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
  171. 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000, 0x80100000, 0x00008020,
  172. 0x00000000, 0x00108020, 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000, 0x00008000,
  173. 0x80100000, 0x80008000, 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
  174. 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020, 0x80000020, 0x00100020,
  175. 0x00108000, 0x00000000, 0x80008000, 0x00008020, 0x80000000, 0x80100020, 0x80108020, 0x00108000
  176. };
  177. //Selection function 3
  178. static const uint32_t sp3[64] =
  179. {
  180. 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208, 0x08000200,
  181. 0x00020008, 0x08000008, 0x08000008, 0x00020000, 0x08020208, 0x00020008, 0x08020000, 0x00000208,
  182. 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
  183. 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208, 0x00000200, 0x08000000,
  184. 0x08020200, 0x08000000, 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200, 0x00000000,
  185. 0x00000200, 0x00020008, 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
  186. 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208, 0x00020200, 0x08000008,
  187. 0x08020000, 0x08000208, 0x00000208, 0x08020000, 0x00020208, 0x00000008, 0x08020008, 0x00020200
  188. };
  189. //Selection function 4
  190. static const uint32_t sp4[64] =
  191. {
  192. 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001, 0x00002001,
  193. 0x00000000, 0x00802000, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00800080, 0x00800001,
  194. 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
  195. 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080, 0x00802081, 0x00000081,
  196. 0x00800080, 0x00800001, 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000, 0x00802000,
  197. 0x00002080, 0x00800080, 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
  198. 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001, 0x00802080, 0x00800081,
  199. 0x00002001, 0x00002080, 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002000, 0x00802080
  200. };
  201. //Selection function 5
  202. static const uint32_t sp5[64] =
  203. {
  204. 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000, 0x02080000,
  205. 0x40080100, 0x00080000, 0x02000100, 0x40080100, 0x42000100, 0x42080000, 0x00080100, 0x40000000,
  206. 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
  207. 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000, 0x42000000, 0x00080100,
  208. 0x00080000, 0x42000100, 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100, 0x40080100,
  209. 0x02000100, 0x40000000, 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
  210. 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000, 0x40080000, 0x42000000,
  211. 0x00080100, 0x02000100, 0x40000100, 0x00080000, 0x00000000, 0x40080000, 0x02080100, 0x40000100
  212. };
  213. //Selection function 6
  214. static const uint32_t sp6[64] =
  215. {
  216. 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010, 0x00400000,
  217. 0x20004000, 0x00404010, 0x00400000, 0x20000010, 0x00400010, 0x20004000, 0x20000000, 0x00004010,
  218. 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
  219. 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000, 0x20404000, 0x20000000,
  220. 0x20004000, 0x00000010, 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010, 0x20000010,
  221. 0x00400000, 0x20004000, 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
  222. 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000, 0x20400000, 0x00404010,
  223. 0x00004000, 0x00400010, 0x20004010, 0x00000000, 0x20404000, 0x20000000, 0x00400010, 0x20004010
  224. };
  225. //Selection function 7
  226. static const uint32_t sp7[64] =
  227. {
  228. 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802, 0x04200800,
  229. 0x04200802, 0x00200000, 0x00000000, 0x04000002, 0x00000002, 0x04000000, 0x04200002, 0x00000802,
  230. 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
  231. 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002, 0x04000000, 0x00200800,
  232. 0x04000000, 0x00200800, 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002, 0x00000002,
  233. 0x00200002, 0x04000000, 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
  234. 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000, 0x00000002, 0x04200802,
  235. 0x00000000, 0x00200802, 0x04200000, 0x00000800, 0x04000002, 0x04000800, 0x00000800, 0x00200002
  236. };
  237. //Selection function 8
  238. static const uint32_t sp8[64] =
  239. {
  240. 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040, 0x10000000,
  241. 0x00040040, 0x10040000, 0x10041040, 0x00041000, 0x10041000, 0x00041040, 0x00001000, 0x00000040,
  242. 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
  243. 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000, 0x00041040, 0x00040000,
  244. 0x00041040, 0x00040000, 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000, 0x00041040,
  245. 0x10001000, 0x00000040, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
  246. 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0x00000000,
  247. 0x10041040, 0x00041000, 0x00041000, 0x00001040, 0x00001040, 0x00040040, 0x10000000, 0x10041000
  248. };
  249. //Common interface for encryption algorithms
  250. const CipherAlgo desCipherAlgo =
  251. {
  252. "DES",
  253. sizeof(DesContext),
  254. CIPHER_ALGO_TYPE_BLOCK,
  255. DES_BLOCK_SIZE,
  256. (CipherAlgoInit) desInit,
  257. NULL,
  258. NULL,
  259. (CipherAlgoEncryptBlock) desEncryptBlock,
  260. (CipherAlgoDecryptBlock) desDecryptBlock,
  261. (CipherAlgoDeinit) desDeinit
  262. };
  263. /**
  264. * @brief Initialize a DES context using the supplied key
  265. * @param[in] context Pointer to the DES context to initialize
  266. * @param[in] key Pointer to the key
  267. * @param[in] keyLen Length of the key (must be set to 8)
  268. * @return Error code
  269. **/
  270. error_t desInit(DesContext *context, const uint8_t *key,
  271. size_t keyLen)
  272. {
  273. uint_t i;
  274. uint32_t c;
  275. uint32_t d;
  276. uint32_t t;
  277. //Check parameters
  278. if(context == NULL || key == NULL)
  279. return WINDOWS_ERROR_INVALID_PARAMETER;
  280. //Invalid key length?
  281. if(keyLen != 8)
  282. return ERROR_INVALID_KEY_LENGTH;
  283. //Copy the key
  284. c = LOAD32BE(key + 0);
  285. d = LOAD32BE(key + 4);
  286. //Permuted choice 1
  287. PC1(c, d);
  288. //Generate the key schedule
  289. for(i = 0; i < 16; i++)
  290. {
  291. //Individual blocks are shifted left
  292. if(i == 0 || i == 1 || i == 8 || i == 15)
  293. {
  294. c = ROL28(c, 1);
  295. d = ROL28(d, 1);
  296. }
  297. else
  298. {
  299. c = ROL28(c, 2);
  300. d = ROL28(d, 2);
  301. }
  302. //Permuted choice 2
  303. context->ks[2 * i] = PC2_L(c, d);
  304. context->ks[2 * i + 1] = PC2_R(c, d);
  305. }
  306. //No error to report
  307. return WINDOWS_NO_ERROR;
  308. }
  309. /**
  310. * @brief Encrypt a 8-byte block using DES algorithm
  311. * @param[in] context Pointer to the DES context
  312. * @param[in] input Plaintext block to encrypt
  313. * @param[out] output Ciphertext block resulting from encryption
  314. **/
  315. void desEncryptBlock(DesContext *context, const uint8_t *input,
  316. uint8_t *output)
  317. {
  318. uint_t i;
  319. uint32_t l;
  320. uint32_t r;
  321. uint32_t t;
  322. //Copy the plaintext from the input buffer
  323. l = LOAD32BE(input + 0);
  324. r = LOAD32BE(input + 4);
  325. //Initial permutation
  326. IP(l, r);
  327. //16 rounds of computation are needed
  328. for(i = 0; i < 32; i += 4)
  329. {
  330. //Apply odd round function
  331. ROUND(l, r, context->ks[i], context->ks[i + 1]);
  332. //Apply even round function
  333. ROUND(r, l, context->ks[i + 2], context->ks[i + 3]);
  334. }
  335. //Inverse of initial permutation
  336. IP_INV(r, l);
  337. //Copy the resulting ciphertext
  338. STORE32BE(r, output + 0);
  339. STORE32BE(l, output + 4);
  340. }
  341. /**
  342. * @brief Decrypt a 8-byte block using DES algorithm
  343. * @param[in] context Pointer to the DES context
  344. * @param[in] input Ciphertext block to decrypt
  345. * @param[out] output Plaintext block resulting from decryption
  346. **/
  347. void desDecryptBlock(DesContext *context, const uint8_t *input,
  348. uint8_t *output)
  349. {
  350. uint_t i;
  351. uint32_t l;
  352. uint32_t r;
  353. uint32_t t;
  354. //Copy the ciphertext from the input buffer
  355. r = LOAD32BE(input + 0);
  356. l = LOAD32BE(input + 4);
  357. //Initial permutation
  358. IP(r, l);
  359. //For decryption, keys in the key schedule must be applied in reverse order
  360. for(i = 32; i > 0; i -= 4)
  361. {
  362. //Apply even round function
  363. ROUND(r, l, context->ks[i - 2], context->ks[i - 1]);
  364. //Apply odd round function
  365. ROUND(l, r, context->ks[i - 4], context->ks[i - 3]);
  366. }
  367. //Inverse of initial permutation
  368. IP_INV(l, r);
  369. //Copy the resulting plaintext
  370. STORE32BE(l, output + 0);
  371. STORE32BE(r, output + 4);
  372. }
  373. /**
  374. * @brief Release DES context
  375. * @param[in] context Pointer to the DES context
  376. **/
  377. void desDeinit(DesContext *context)
  378. {
  379. //Clear DES context
  380. osMemset(context, 0, sizeof(DesContext));
  381. }
  382. #endif