des.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* des.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file des.c
  18. * \author Daniel Otte
  19. * \email daniel.otte@rub.de
  20. * \date 2007-06-16
  21. * \brief DES and EDE-DES implementation
  22. * \license GPLv3 or later
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. const uint8_t sbox[256] = {
  28. /* S-box 1 */
  29. 0xE4, 0xD1, 0x2F, 0xB8, 0x3A, 0x6C, 0x59, 0x07,
  30. 0x0F, 0x74, 0xE2, 0xD1, 0xA6, 0xCB, 0x95, 0x38,
  31. 0x41, 0xE8, 0xD6, 0x2B, 0xFC, 0x97, 0x3A, 0x50,
  32. 0xFC, 0x82, 0x49, 0x17, 0x5B, 0x3E, 0xA0, 0x6D,
  33. /* S-box 2 */
  34. 0xF1, 0x8E, 0x6B, 0x34, 0x97, 0x2D, 0xC0, 0x5A,
  35. 0x3D, 0x47, 0xF2, 0x8E, 0xC0, 0x1A, 0x69, 0xB5,
  36. 0x0E, 0x7B, 0xA4, 0xD1, 0x58, 0xC6, 0x93, 0x2F,
  37. 0xD8, 0xA1, 0x3F, 0x42, 0xB6, 0x7C, 0x05, 0xE9,
  38. /* S-box 3 */
  39. 0xA0, 0x9E, 0x63, 0xF5, 0x1D, 0xC7, 0xB4, 0x28,
  40. 0xD7, 0x09, 0x34, 0x6A, 0x28, 0x5E, 0xCB, 0xF1,
  41. 0xD6, 0x49, 0x8F, 0x30, 0xB1, 0x2C, 0x5A, 0xE7,
  42. 0x1A, 0xD0, 0x69, 0x87, 0x4F, 0xE3, 0xB5, 0x2C,
  43. /* S-box 4 */
  44. 0x7D, 0xE3, 0x06, 0x9A, 0x12, 0x85, 0xBC, 0x4F,
  45. 0xD8, 0xB5, 0x6F, 0x03, 0x47, 0x2C, 0x1A, 0xE9,
  46. 0xA6, 0x90, 0xCB, 0x7D, 0xF1, 0x3E, 0x52, 0x84,
  47. 0x3F, 0x06, 0xA1, 0xD8, 0x94, 0x5B, 0xC7, 0x2E,
  48. /* S-box 5 */
  49. 0x2C, 0x41, 0x7A, 0xB6, 0x85, 0x3F, 0xD0, 0xE9,
  50. 0xEB, 0x2C, 0x47, 0xD1, 0x50, 0xFA, 0x39, 0x86,
  51. 0x42, 0x1B, 0xAD, 0x78, 0xF9, 0xC5, 0x63, 0x0E,
  52. 0xB8, 0xC7, 0x1E, 0x2D, 0x6F, 0x09, 0xA4, 0x53,
  53. /* S-box 6 */
  54. 0xC1, 0xAF, 0x92, 0x68, 0x0D, 0x34, 0xE7, 0x5B,
  55. 0xAF, 0x42, 0x7C, 0x95, 0x61, 0xDE, 0x0B, 0x38,
  56. 0x9E, 0xF5, 0x28, 0xC3, 0x70, 0x4A, 0x1D, 0xB6,
  57. 0x43, 0x2C, 0x95, 0xFA, 0xBE, 0x17, 0x60, 0x8D,
  58. /* S-box 7 */
  59. 0x4B, 0x2E, 0xF0, 0x8D, 0x3C, 0x97, 0x5A, 0x61,
  60. 0xD0, 0xB7, 0x49, 0x1A, 0xE3, 0x5C, 0x2F, 0x86,
  61. 0x14, 0xBD, 0xC3, 0x7E, 0xAF, 0x68, 0x05, 0x92,
  62. 0x6B, 0xD8, 0x14, 0xA7, 0x95, 0x0F, 0xE2, 0x3C,
  63. /* S-box 8 */
  64. 0xD2, 0x84, 0x6F, 0xB1, 0xA9, 0x3E, 0x50, 0xC7,
  65. 0x1F, 0xD8, 0xA3, 0x74, 0xC5, 0x6B, 0x0E, 0x92,
  66. 0x7B, 0x41, 0x9C, 0xE2, 0x06, 0xAD, 0xF3, 0x58,
  67. 0x21, 0xE7, 0x4A, 0x8D, 0xFC, 0x90, 0x35, 0x6B
  68. };
  69. const uint8_t e_permtab[] ={
  70. 4, 6, /* 4 bytes in 6 bytes out*/
  71. 32, 1, 2, 3, 4, 5,
  72. 4, 5, 6, 7, 8, 9,
  73. 8, 9, 10, 11, 12, 13,
  74. 12, 13, 14, 15, 16, 17,
  75. 16, 17, 18, 19, 20, 21,
  76. 20, 21, 22, 23, 24, 25,
  77. 24, 25, 26, 27, 28, 29,
  78. 28, 29, 30, 31, 32, 1
  79. };
  80. const uint8_t p_permtab[] ={
  81. 4, 4, /* 32 bit -> 32 bit */
  82. 16, 7, 20, 21,
  83. 29, 12, 28, 17,
  84. 1, 15, 23, 26,
  85. 5, 18, 31, 10,
  86. 2, 8, 24, 14,
  87. 32, 27, 3, 9,
  88. 19, 13, 30, 6,
  89. 22, 11, 4, 25
  90. };
  91. const uint8_t ip_permtab[] ={
  92. 8, 8, /* 64 bit -> 64 bit */
  93. 58, 50, 42, 34, 26, 18, 10, 2,
  94. 60, 52, 44, 36, 28, 20, 12, 4,
  95. 62, 54, 46, 38, 30, 22, 14, 6,
  96. 64, 56, 48, 40, 32, 24, 16, 8,
  97. 57, 49, 41, 33, 25, 17, 9, 1,
  98. 59, 51, 43, 35, 27, 19, 11, 3,
  99. 61, 53, 45, 37, 29, 21, 13, 5,
  100. 63, 55, 47, 39, 31, 23, 15, 7
  101. };
  102. const uint8_t inv_ip_permtab[] ={
  103. 8, 8, /* 64 bit -> 64 bit */
  104. 40, 8, 48, 16, 56, 24, 64, 32,
  105. 39, 7, 47, 15, 55, 23, 63, 31,
  106. 38, 6, 46, 14, 54, 22, 62, 30,
  107. 37, 5, 45, 13, 53, 21, 61, 29,
  108. 36, 4, 44, 12, 52, 20, 60, 28,
  109. 35, 3, 43, 11, 51, 19, 59, 27,
  110. 34, 2, 42, 10, 50, 18, 58, 26,
  111. 33, 1, 41, 9, 49, 17, 57, 25
  112. };
  113. const uint8_t pc1_permtab[] ={
  114. 8, 7, /* 64 bit -> 56 bit*/
  115. 57, 49, 41, 33, 25, 17, 9,
  116. 1, 58, 50, 42, 34, 26, 18,
  117. 10, 2, 59, 51, 43, 35, 27,
  118. 19, 11, 3, 60, 52, 44, 36,
  119. 63, 55, 47, 39, 31, 23, 15,
  120. 7, 62, 54, 46, 38, 30, 22,
  121. 14, 6, 61, 53, 45, 37, 29,
  122. 21, 13, 5, 28, 20, 12, 4
  123. };
  124. const uint8_t pc2_permtab[] ={
  125. 7, 6, /* 56 bit -> 48 bit */
  126. 14, 17, 11, 24, 1, 5,
  127. 3, 28, 15, 6, 21, 10,
  128. 23, 19, 12, 4, 26, 8,
  129. 16, 7, 27, 20, 13, 2,
  130. 41, 52, 31, 37, 47, 55,
  131. 30, 40, 51, 45, 33, 48,
  132. 44, 49, 39, 56, 34, 53,
  133. 46, 42, 50, 36, 29, 32
  134. };
  135. const uint8_t splitin6bitword_permtab[] = {
  136. 8, 8, /* 64 bit -> 64 bit */
  137. 64, 64, 1, 6, 2, 3, 4, 5,
  138. 64, 64, 7, 12, 8, 9, 10, 11,
  139. 64, 64, 13, 18, 14, 15, 16, 17,
  140. 64, 64, 19, 24, 20, 21, 22, 23,
  141. 64, 64, 25, 30, 26, 27, 28, 29,
  142. 64, 64, 31, 36, 32, 33, 34, 35,
  143. 64, 64, 37, 42, 38, 39, 40, 41,
  144. 64, 64, 43, 48, 44, 45, 46, 47
  145. };
  146. const uint8_t shiftkey_permtab[] = {
  147. 7, 7, /* 56 bit -> 56 bit */
  148. 2, 3, 4, 5, 6, 7, 8, 9,
  149. 10, 11, 12, 13, 14, 15, 16, 17,
  150. 18, 19, 20, 21, 22, 23, 24, 25,
  151. 26, 27, 28, 1,
  152. 30, 31, 32, 33, 34, 35, 36, 37,
  153. 38, 39, 40, 41, 42, 43, 44, 45,
  154. 46, 47, 48, 49, 50, 51, 52, 53,
  155. 54, 55, 56, 29
  156. };
  157. const uint8_t shiftkeyinv_permtab[] = {
  158. 7, 7,
  159. 28, 1, 2, 3, 4, 5, 6, 7,
  160. 8, 9, 10, 11, 12, 13, 14, 15,
  161. 16, 17, 18, 19, 20, 21, 22, 23,
  162. 24, 25, 26, 27,
  163. 56, 29, 30, 31, 32, 33, 34, 35,
  164. 36, 37, 38, 39, 40, 41, 42, 43,
  165. 44, 45, 46, 47, 48, 49, 50, 51,
  166. 52, 53, 54, 55
  167. };
  168. /*
  169. 1 0
  170. 1 0
  171. 2 1
  172. 2 1
  173. 2 1
  174. 2 1
  175. 2 1
  176. 2 1
  177. ----
  178. 1 0
  179. 2 1
  180. 2 1
  181. 2 1
  182. 2 1
  183. 2 1
  184. 2 1
  185. 1 0
  186. */
  187. #define ROTTABLE 0x7EFC
  188. #define ROTTABLE_INV 0x3F7E
  189. /******************************************************************************/
  190. void permute(const uint8_t *ptable, const uint8_t *in, uint8_t *out){
  191. uint8_t ob; /* in-bytes and out-bytes */
  192. uint8_t byte, bit; /* counter for bit and byte */
  193. ob = ptable[1];
  194. ptable = &(ptable[2]);
  195. for(byte=0; byte<ob; ++byte){
  196. uint8_t x,t=0;
  197. for(bit=0; bit<8; ++bit){
  198. x=*ptable++ -1 ;
  199. t<<=1;
  200. if((in[x/8]) & (0x80>>(x%8)) ){
  201. t|=0x01;
  202. }
  203. }
  204. out[byte]=t;
  205. }
  206. }
  207. /******************************************************************************/
  208. void changeendian32(uint32_t * a){
  209. *a = (*a & 0x000000FF) << 24 |
  210. (*a & 0x0000FF00) << 8 |
  211. (*a & 0x00FF0000) >> 8 |
  212. (*a & 0xFF000000) >> 24;
  213. }
  214. /******************************************************************************/
  215. static inline
  216. void shiftkey(uint8_t *key){
  217. uint8_t k[7];
  218. memcpy(k, key, 7);
  219. permute((uint8_t*)shiftkey_permtab, k, key);
  220. }
  221. /******************************************************************************/
  222. static inline
  223. void shiftkey_inv(uint8_t *key){
  224. uint8_t k[7];
  225. memcpy(k, key, 7);
  226. permute((uint8_t*)shiftkeyinv_permtab, k, key);
  227. }
  228. /******************************************************************************/
  229. static inline
  230. uint64_t splitin6bitwords(uint64_t a){
  231. uint64_t ret=0;
  232. a &= 0x0000ffffffffffffLL;
  233. permute((uint8_t*)splitin6bitword_permtab, (uint8_t*)&a, (uint8_t*)&ret);
  234. return ret;
  235. }
  236. /******************************************************************************/
  237. static inline
  238. uint8_t substitute(uint8_t a, uint8_t * sbp){
  239. uint8_t x;
  240. x = sbp[a>>1];
  241. x = (a&1)?x&0x0F:x>>4;
  242. return x;
  243. }
  244. /******************************************************************************/
  245. uint32_t des_f(uint32_t r, uint8_t* kr){
  246. uint8_t i;
  247. uint32_t t=0,ret;
  248. uint64_t data;
  249. uint8_t *sbp; /* sboxpointer */
  250. permute((uint8_t*)e_permtab, (uint8_t*)&r, (uint8_t*)&data);
  251. for(i=0; i<7; ++i)
  252. ((uint8_t*)&data)[i] ^= kr[i];
  253. /* Sbox substitution */
  254. data = splitin6bitwords(data);
  255. sbp=(uint8_t*)sbox;
  256. for(i=0; i<8; ++i){
  257. uint8_t x;
  258. x = substitute(((uint8_t*)&data)[i], sbp);
  259. t<<=4;
  260. t |= x;
  261. sbp += 32;
  262. }
  263. changeendian32(&t);
  264. permute((uint8_t*)p_permtab,(uint8_t*)&t, (uint8_t*)&ret);
  265. return ret;
  266. }
  267. /******************************************************************************/
  268. void des_enc(void* out, const void* in, const void* key){
  269. #define R (data.v32[1])
  270. #define L (data.v32[0])
  271. uint8_t kr[6],k[7];
  272. uint8_t i;
  273. union {
  274. uint8_t v8[8];
  275. uint32_t v32[2];
  276. } data;
  277. permute((uint8_t*)ip_permtab, (uint8_t*)in, data.v8);
  278. permute((uint8_t*)pc1_permtab, (const uint8_t*)key, k);
  279. for(i=0; i<8; ++i){
  280. shiftkey(k);
  281. if(ROTTABLE&((1<<((i<<1)+0))) )
  282. shiftkey(k);
  283. permute((uint8_t*)pc2_permtab, k, kr);
  284. L ^= des_f(R, kr);
  285. shiftkey(k);
  286. if(ROTTABLE&((1<<((i<<1)+1))) )
  287. shiftkey(k);
  288. permute((uint8_t*)pc2_permtab, k, kr);
  289. R ^= des_f(L, kr);
  290. }
  291. /* L <-> R*/
  292. R ^= L;
  293. L ^= R;
  294. R ^= L;
  295. permute((uint8_t*)inv_ip_permtab, data.v8, (uint8_t*)out);
  296. }
  297. /******************************************************************************/
  298. void des_dec(void* out, const void* in, const uint8_t* key){
  299. uint8_t kr[6],k[7];
  300. union {
  301. uint8_t v8[8];
  302. uint32_t v32[2];
  303. } data;
  304. int8_t i;
  305. permute((uint8_t*)ip_permtab, (uint8_t*)in, data.v8);
  306. permute((uint8_t*)pc1_permtab, (const uint8_t*)key, k);
  307. for(i=7; i>=0; --i){
  308. permute((uint8_t*)pc2_permtab, k, kr);
  309. L ^= des_f(R, kr);
  310. shiftkey_inv(k);
  311. if(ROTTABLE&((1<<((i<<1)+1))) ){
  312. shiftkey_inv(k);
  313. }
  314. permute((uint8_t*)pc2_permtab, k, kr);
  315. R ^= des_f(L, kr);
  316. shiftkey_inv(k);
  317. if(ROTTABLE&((1<<((i<<1)+0))) ){
  318. shiftkey_inv(k);
  319. }
  320. }
  321. /* L <-> R*/
  322. R ^= L;
  323. L ^= R;
  324. R ^= L;
  325. permute((uint8_t*)inv_ip_permtab, data.v8, (uint8_t*)out);
  326. }
  327. /******************************************************************************/
  328. void tdes_enc(void* out, void* in, const void* key){
  329. des_enc(out, in, (uint8_t*)key + 0);
  330. des_dec(out, out, (uint8_t*)key + 8);
  331. des_enc(out, out, (uint8_t*)key +16);
  332. }
  333. /******************************************************************************/
  334. void tdes_dec(void* out, void* in, const uint8_t* key){
  335. des_dec(out, in, (uint8_t*)key +16);
  336. des_enc(out, out, (uint8_t*)key + 8);
  337. des_dec(out, out, (uint8_t*)key + 0);
  338. }
  339. /******************************************************************************/