des.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * DES encryption/decryption
  3. * Copyright (c) 2007 Reimar Doeffinger
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <inttypes.h>
  22. #include "avutil.h"
  23. #include "common.h"
  24. #include "intreadwrite.h"
  25. #include "des.h"
  26. typedef struct AVDES AVDES;
  27. #define T(a, b, c, d, e, f, g, h) 64-a,64-b,64-c,64-d,64-e,64-f,64-g,64-h
  28. static const uint8_t IP_shuffle[] = {
  29. T(58, 50, 42, 34, 26, 18, 10, 2),
  30. T(60, 52, 44, 36, 28, 20, 12, 4),
  31. T(62, 54, 46, 38, 30, 22, 14, 6),
  32. T(64, 56, 48, 40, 32, 24, 16, 8),
  33. T(57, 49, 41, 33, 25, 17, 9, 1),
  34. T(59, 51, 43, 35, 27, 19, 11, 3),
  35. T(61, 53, 45, 37, 29, 21, 13, 5),
  36. T(63, 55, 47, 39, 31, 23, 15, 7)
  37. };
  38. #undef T
  39. #define T(a, b, c, d) 32-a,32-b,32-c,32-d
  40. static const uint8_t P_shuffle[] = {
  41. T(16, 7, 20, 21),
  42. T(29, 12, 28, 17),
  43. T( 1, 15, 23, 26),
  44. T( 5, 18, 31, 10),
  45. T( 2, 8, 24, 14),
  46. T(32, 27, 3, 9),
  47. T(19, 13, 30, 6),
  48. T(22, 11, 4, 25)
  49. };
  50. #undef T
  51. #define T(a, b, c, d, e, f, g) 64-a,64-b,64-c,64-d,64-e,64-f,64-g
  52. static const uint8_t PC1_shuffle[] = {
  53. T(57, 49, 41, 33, 25, 17, 9),
  54. T( 1, 58, 50, 42, 34, 26, 18),
  55. T(10, 2, 59, 51, 43, 35, 27),
  56. T(19, 11, 3, 60, 52, 44, 36),
  57. T(63, 55, 47, 39, 31, 23, 15),
  58. T( 7, 62, 54, 46, 38, 30, 22),
  59. T(14, 6, 61, 53, 45, 37, 29),
  60. T(21, 13, 5, 28, 20, 12, 4)
  61. };
  62. #undef T
  63. #define T(a, b, c, d, e, f) 56-a,56-b,56-c,56-d,56-e,56-f
  64. static const uint8_t PC2_shuffle[] = {
  65. T(14, 17, 11, 24, 1, 5),
  66. T( 3, 28, 15, 6, 21, 10),
  67. T(23, 19, 12, 4, 26, 8),
  68. T(16, 7, 27, 20, 13, 2),
  69. T(41, 52, 31, 37, 47, 55),
  70. T(30, 40, 51, 45, 33, 48),
  71. T(44, 49, 39, 56, 34, 53),
  72. T(46, 42, 50, 36, 29, 32)
  73. };
  74. #undef T
  75. #if CONFIG_SMALL
  76. static const uint8_t S_boxes[8][32] = {
  77. {
  78. 0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18, 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
  79. 0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b, 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0,
  80. }, {
  81. 0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4, 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
  82. 0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21, 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f,
  83. }, {
  84. 0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5, 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
  85. 0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70, 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7,
  86. }, {
  87. 0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a, 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
  88. 0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d, 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4,
  89. }, {
  90. 0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16, 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
  91. 0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8, 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e,
  92. }, {
  93. 0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58, 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
  94. 0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3, 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6,
  95. }, {
  96. 0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad, 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
  97. 0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e, 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2,
  98. }, {
  99. 0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41, 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
  100. 0x27, 0x1b, 0xe4, 0x71, 0x49, 0xac, 0x8e, 0xd2, 0xf0, 0xc6, 0x9a, 0x0d, 0x3f, 0x53, 0x65, 0xb8,
  101. }
  102. };
  103. #else
  104. /**
  105. * This table contains the results of applying both the S-box and P-shuffle.
  106. * It can be regenerated by compiling this file with -DCONFIG_SMALL -DTEST -DGENTABLES
  107. */
  108. static const uint32_t S_boxes_P_shuffle[8][64] = {
  109. {
  110. 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000,
  111. 0x00000200, 0x00808200, 0x00808202, 0x00000200, 0x00800202, 0x00808002, 0x00800000, 0x00000002,
  112. 0x00000202, 0x00800200, 0x00800200, 0x00008200, 0x00008200, 0x00808000, 0x00808000, 0x00800202,
  113. 0x00008002, 0x00800002, 0x00800002, 0x00008002, 0x00000000, 0x00000202, 0x00008202, 0x00800000,
  114. 0x00008000, 0x00808202, 0x00000002, 0x00808000, 0x00808200, 0x00800000, 0x00800000, 0x00000200,
  115. 0x00808002, 0x00008000, 0x00008200, 0x00800002, 0x00000200, 0x00000002, 0x00800202, 0x00008202,
  116. 0x00808202, 0x00008002, 0x00808000, 0x00800202, 0x00800002, 0x00000202, 0x00008202, 0x00808200,
  117. 0x00000202, 0x00800200, 0x00800200, 0x00000000, 0x00008002, 0x00008200, 0x00000000, 0x00808002,
  118. },
  119. {
  120. 0x40084010, 0x40004000, 0x00004000, 0x00084010, 0x00080000, 0x00000010, 0x40080010, 0x40004010,
  121. 0x40000010, 0x40084010, 0x40084000, 0x40000000, 0x40004000, 0x00080000, 0x00000010, 0x40080010,
  122. 0x00084000, 0x00080010, 0x40004010, 0x00000000, 0x40000000, 0x00004000, 0x00084010, 0x40080000,
  123. 0x00080010, 0x40000010, 0x00000000, 0x00084000, 0x00004010, 0x40084000, 0x40080000, 0x00004010,
  124. 0x00000000, 0x00084010, 0x40080010, 0x00080000, 0x40004010, 0x40080000, 0x40084000, 0x00004000,
  125. 0x40080000, 0x40004000, 0x00000010, 0x40084010, 0x00084010, 0x00000010, 0x00004000, 0x40000000,
  126. 0x00004010, 0x40084000, 0x00080000, 0x40000010, 0x00080010, 0x40004010, 0x40000010, 0x00080010,
  127. 0x00084000, 0x00000000, 0x40004000, 0x00004010, 0x40000000, 0x40080010, 0x40084010, 0x00084000,
  128. },
  129. {
  130. 0x00000104, 0x04010100, 0x00000000, 0x04010004, 0x04000100, 0x00000000, 0x00010104, 0x04000100,
  131. 0x00010004, 0x04000004, 0x04000004, 0x00010000, 0x04010104, 0x00010004, 0x04010000, 0x00000104,
  132. 0x04000000, 0x00000004, 0x04010100, 0x00000100, 0x00010100, 0x04010000, 0x04010004, 0x00010104,
  133. 0x04000104, 0x00010100, 0x00010000, 0x04000104, 0x00000004, 0x04010104, 0x00000100, 0x04000000,
  134. 0x04010100, 0x04000000, 0x00010004, 0x00000104, 0x00010000, 0x04010100, 0x04000100, 0x00000000,
  135. 0x00000100, 0x00010004, 0x04010104, 0x04000100, 0x04000004, 0x00000100, 0x00000000, 0x04010004,
  136. 0x04000104, 0x00010000, 0x04000000, 0x04010104, 0x00000004, 0x00010104, 0x00010100, 0x04000004,
  137. 0x04010000, 0x04000104, 0x00000104, 0x04010000, 0x00010104, 0x00000004, 0x04010004, 0x00010100,
  138. },
  139. {
  140. 0x80401000, 0x80001040, 0x80001040, 0x00000040, 0x00401040, 0x80400040, 0x80400000, 0x80001000,
  141. 0x00000000, 0x00401000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00400040, 0x80400000,
  142. 0x80000000, 0x00001000, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x80001000, 0x00001040,
  143. 0x80400040, 0x80000000, 0x00001040, 0x00400040, 0x00001000, 0x00401040, 0x80401040, 0x80000040,
  144. 0x00400040, 0x80400000, 0x00401000, 0x80401040, 0x80000040, 0x00000000, 0x00000000, 0x00401000,
  145. 0x00001040, 0x00400040, 0x80400040, 0x80000000, 0x80401000, 0x80001040, 0x80001040, 0x00000040,
  146. 0x80401040, 0x80000040, 0x80000000, 0x00001000, 0x80400000, 0x80001000, 0x00401040, 0x80400040,
  147. 0x80001000, 0x00001040, 0x00400000, 0x80401000, 0x00000040, 0x00400000, 0x00001000, 0x00401040,
  148. },
  149. {
  150. 0x00000080, 0x01040080, 0x01040000, 0x21000080, 0x00040000, 0x00000080, 0x20000000, 0x01040000,
  151. 0x20040080, 0x00040000, 0x01000080, 0x20040080, 0x21000080, 0x21040000, 0x00040080, 0x20000000,
  152. 0x01000000, 0x20040000, 0x20040000, 0x00000000, 0x20000080, 0x21040080, 0x21040080, 0x01000080,
  153. 0x21040000, 0x20000080, 0x00000000, 0x21000000, 0x01040080, 0x01000000, 0x21000000, 0x00040080,
  154. 0x00040000, 0x21000080, 0x00000080, 0x01000000, 0x20000000, 0x01040000, 0x21000080, 0x20040080,
  155. 0x01000080, 0x20000000, 0x21040000, 0x01040080, 0x20040080, 0x00000080, 0x01000000, 0x21040000,
  156. 0x21040080, 0x00040080, 0x21000000, 0x21040080, 0x01040000, 0x00000000, 0x20040000, 0x21000000,
  157. 0x00040080, 0x01000080, 0x20000080, 0x00040000, 0x00000000, 0x20040000, 0x01040080, 0x20000080,
  158. },
  159. {
  160. 0x10000008, 0x10200000, 0x00002000, 0x10202008, 0x10200000, 0x00000008, 0x10202008, 0x00200000,
  161. 0x10002000, 0x00202008, 0x00200000, 0x10000008, 0x00200008, 0x10002000, 0x10000000, 0x00002008,
  162. 0x00000000, 0x00200008, 0x10002008, 0x00002000, 0x00202000, 0x10002008, 0x00000008, 0x10200008,
  163. 0x10200008, 0x00000000, 0x00202008, 0x10202000, 0x00002008, 0x00202000, 0x10202000, 0x10000000,
  164. 0x10002000, 0x00000008, 0x10200008, 0x00202000, 0x10202008, 0x00200000, 0x00002008, 0x10000008,
  165. 0x00200000, 0x10002000, 0x10000000, 0x00002008, 0x10000008, 0x10202008, 0x00202000, 0x10200000,
  166. 0x00202008, 0x10202000, 0x00000000, 0x10200008, 0x00000008, 0x00002000, 0x10200000, 0x00202008,
  167. 0x00002000, 0x00200008, 0x10002008, 0x00000000, 0x10202000, 0x10000000, 0x00200008, 0x10002008,
  168. },
  169. {
  170. 0x00100000, 0x02100001, 0x02000401, 0x00000000, 0x00000400, 0x02000401, 0x00100401, 0x02100400,
  171. 0x02100401, 0x00100000, 0x00000000, 0x02000001, 0x00000001, 0x02000000, 0x02100001, 0x00000401,
  172. 0x02000400, 0x00100401, 0x00100001, 0x02000400, 0x02000001, 0x02100000, 0x02100400, 0x00100001,
  173. 0x02100000, 0x00000400, 0x00000401, 0x02100401, 0x00100400, 0x00000001, 0x02000000, 0x00100400,
  174. 0x02000000, 0x00100400, 0x00100000, 0x02000401, 0x02000401, 0x02100001, 0x02100001, 0x00000001,
  175. 0x00100001, 0x02000000, 0x02000400, 0x00100000, 0x02100400, 0x00000401, 0x00100401, 0x02100400,
  176. 0x00000401, 0x02000001, 0x02100401, 0x02100000, 0x00100400, 0x00000000, 0x00000001, 0x02100401,
  177. 0x00000000, 0x00100401, 0x02100000, 0x00000400, 0x02000001, 0x02000400, 0x00000400, 0x00100001,
  178. },
  179. {
  180. 0x08000820, 0x00000800, 0x00020000, 0x08020820, 0x08000000, 0x08000820, 0x00000020, 0x08000000,
  181. 0x00020020, 0x08020000, 0x08020820, 0x00020800, 0x08020800, 0x00020820, 0x00000800, 0x00000020,
  182. 0x08020000, 0x08000020, 0x08000800, 0x00000820, 0x00020800, 0x00020020, 0x08020020, 0x08020800,
  183. 0x00000820, 0x00000000, 0x00000000, 0x08020020, 0x08000020, 0x08000800, 0x00020820, 0x00020000,
  184. 0x00020820, 0x00020000, 0x08020800, 0x00000800, 0x00000020, 0x08020020, 0x00000800, 0x00020820,
  185. 0x08000800, 0x00000020, 0x08000020, 0x08020000, 0x08020020, 0x08000000, 0x00020000, 0x08000820,
  186. 0x00000000, 0x08020820, 0x00020020, 0x08000020, 0x08020000, 0x08000800, 0x08000820, 0x00000000,
  187. 0x08020820, 0x00020800, 0x00020800, 0x00000820, 0x00000820, 0x00020020, 0x08000000, 0x08020800,
  188. },
  189. };
  190. #endif
  191. static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
  192. int i;
  193. uint64_t res = 0;
  194. for (i = 0; i < shuffle_len; i++)
  195. res += res + ((in >> *shuffle++) & 1);
  196. return res;
  197. }
  198. static uint64_t shuffle_inv(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
  199. int i;
  200. uint64_t res = 0;
  201. shuffle += shuffle_len - 1;
  202. for (i = 0; i < shuffle_len; i++) {
  203. res |= (in & 1) << *shuffle--;
  204. in >>= 1;
  205. }
  206. return res;
  207. }
  208. static uint32_t f_func(uint32_t r, uint64_t k) {
  209. int i;
  210. uint32_t out = 0;
  211. // rotate to get first part of E-shuffle in the lowest 6 bits
  212. r = (r << 1) | (r >> 31);
  213. // apply S-boxes, those compress the data again from 8 * 6 to 8 * 4 bits
  214. for (i = 7; i >= 0; i--) {
  215. uint8_t tmp = (r ^ k) & 0x3f;
  216. #if CONFIG_SMALL
  217. uint8_t v = S_boxes[i][tmp >> 1];
  218. if (tmp & 1) v >>= 4;
  219. out = (out >> 4) | (v << 28);
  220. #else
  221. out |= S_boxes_P_shuffle[i][tmp];
  222. #endif
  223. // get next 6 bits of E-shuffle and round key k into the lowest bits
  224. r = (r >> 4) | (r << 28);
  225. k >>= 6;
  226. }
  227. #if CONFIG_SMALL
  228. out = shuffle(out, P_shuffle, sizeof(P_shuffle));
  229. #endif
  230. return out;
  231. }
  232. /**
  233. * \brief rotate the two halves of the expanded 56 bit key each 1 bit left
  234. *
  235. * Note: the specification calls this "shift", so I kept it although
  236. * it is confusing.
  237. */
  238. static uint64_t key_shift_left(uint64_t CDn) {
  239. uint64_t carries = (CDn >> 27) & 0x10000001;
  240. CDn <<= 1;
  241. CDn &= ~0x10000001;
  242. CDn |= carries;
  243. return CDn;
  244. }
  245. static void gen_roundkeys(uint64_t K[16], uint64_t key) {
  246. int i;
  247. // discard parity bits from key and shuffle it into C and D parts
  248. uint64_t CDn = shuffle(key, PC1_shuffle, sizeof(PC1_shuffle));
  249. // generate round keys
  250. for (i = 0; i < 16; i++) {
  251. CDn = key_shift_left(CDn);
  252. if (i > 1 && i != 8 && i != 15)
  253. CDn = key_shift_left(CDn);
  254. K[i] = shuffle(CDn, PC2_shuffle, sizeof(PC2_shuffle));
  255. }
  256. }
  257. static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt) {
  258. int i;
  259. // used to apply round keys in reverse order for decryption
  260. decrypt = decrypt ? 15 : 0;
  261. // shuffle irrelevant to security but to ease hardware implementations
  262. in = shuffle(in, IP_shuffle, sizeof(IP_shuffle));
  263. for (i = 0; i < 16; i++) {
  264. uint32_t f_res;
  265. f_res = f_func(in, K[decrypt ^ i]);
  266. in = (in << 32) | (in >> 32);
  267. in ^= f_res;
  268. }
  269. in = (in << 32) | (in >> 32);
  270. // reverse shuffle used to ease hardware implementations
  271. in = shuffle_inv(in, IP_shuffle, sizeof(IP_shuffle));
  272. return in;
  273. }
  274. int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
  275. if (key_bits != 64 && key_bits != 192)
  276. return -1;
  277. d->triple_des = key_bits > 64;
  278. gen_roundkeys(d->round_keys[0], AV_RB64(key));
  279. if (d->triple_des) {
  280. gen_roundkeys(d->round_keys[1], AV_RB64(key + 8));
  281. gen_roundkeys(d->round_keys[2], AV_RB64(key + 16));
  282. }
  283. return 0;
  284. }
  285. void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
  286. uint64_t iv_val = iv ? av_be2ne64(*(uint64_t *)iv) : 0;
  287. while (count-- > 0) {
  288. uint64_t dst_val;
  289. uint64_t src_val = src ? av_be2ne64(*(const uint64_t *)src) : 0;
  290. if (decrypt) {
  291. uint64_t tmp = src_val;
  292. if (d->triple_des) {
  293. src_val = des_encdec(src_val, d->round_keys[2], 1);
  294. src_val = des_encdec(src_val, d->round_keys[1], 0);
  295. }
  296. dst_val = des_encdec(src_val, d->round_keys[0], 1) ^ iv_val;
  297. iv_val = iv ? tmp : 0;
  298. } else {
  299. dst_val = des_encdec(src_val ^ iv_val, d->round_keys[0], 0);
  300. if (d->triple_des) {
  301. dst_val = des_encdec(dst_val, d->round_keys[1], 1);
  302. dst_val = des_encdec(dst_val, d->round_keys[2], 0);
  303. }
  304. iv_val = iv ? dst_val : 0;
  305. }
  306. *(uint64_t *)dst = av_be2ne64(dst_val);
  307. src += 8;
  308. dst += 8;
  309. }
  310. if (iv)
  311. *(uint64_t *)iv = av_be2ne64(iv_val);
  312. }
  313. #ifdef TEST
  314. #undef printf
  315. #undef rand
  316. #undef srand
  317. #include <stdlib.h>
  318. #include <stdio.h>
  319. #include <sys/time.h>
  320. static uint64_t rand64(void) {
  321. uint64_t r = rand();
  322. r = (r << 32) | rand();
  323. return r;
  324. }
  325. static const uint8_t test_key[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  326. static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
  327. static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = {0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18};
  328. static DECLARE_ALIGNED(8, uint8_t, tmp)[8];
  329. static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8];
  330. static const uint8_t cbc_key[] = {
  331. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  332. 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01,
  333. 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23
  334. };
  335. static int run_test(int cbc, int decrypt) {
  336. AVDES d;
  337. int delay = cbc && !decrypt ? 2 : 1;
  338. uint64_t res;
  339. AV_WB64(large_buffer[0], 0x4e6f772069732074ULL);
  340. AV_WB64(large_buffer[1], 0x1234567890abcdefULL);
  341. AV_WB64(tmp, 0x1234567890abcdefULL);
  342. av_des_init(&d, cbc_key, 192, decrypt);
  343. av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt);
  344. res = AV_RB64(large_buffer[9999 + delay]);
  345. if (cbc) {
  346. if (decrypt)
  347. return res == 0xc5cecf63ecec514cULL;
  348. else
  349. return res == 0xcb191f85d1ed8439ULL;
  350. } else {
  351. if (decrypt)
  352. return res == 0x8325397644091a0aULL;
  353. else
  354. return res == 0xdd17e8b8b437d232ULL;
  355. }
  356. }
  357. int main(void) {
  358. AVDES d;
  359. int i;
  360. #ifdef GENTABLES
  361. int j;
  362. #endif
  363. struct timeval tv;
  364. uint64_t key[3];
  365. uint64_t data;
  366. uint64_t ct;
  367. uint64_t roundkeys[16];
  368. gettimeofday(&tv, NULL);
  369. srand(tv.tv_sec * 1000 * 1000 + tv.tv_usec);
  370. key[0] = AV_RB64(test_key);
  371. data = AV_RB64(plain);
  372. gen_roundkeys(roundkeys, key[0]);
  373. if (des_encdec(data, roundkeys, 0) != AV_RB64(crypt)) {
  374. printf("Test 1 failed\n");
  375. return 1;
  376. }
  377. av_des_init(&d, test_key, 64, 0);
  378. av_des_crypt(&d, tmp, plain, 1, NULL, 0);
  379. if (memcmp(tmp, crypt, sizeof(crypt))) {
  380. printf("Public API decryption failed\n");
  381. return 1;
  382. }
  383. if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) {
  384. printf("Partial Monte-Carlo test failed\n");
  385. return 1;
  386. }
  387. for (i = 0; i < 1000000; i++) {
  388. key[0] = rand64(); key[1] = rand64(); key[2] = rand64();
  389. data = rand64();
  390. av_des_init(&d, key, 192, 0);
  391. av_des_crypt(&d, &ct, &data, 1, NULL, 0);
  392. av_des_init(&d, key, 192, 1);
  393. av_des_crypt(&d, &ct, &ct, 1, NULL, 1);
  394. if (ct != data) {
  395. printf("Test 2 failed\n");
  396. return 1;
  397. }
  398. }
  399. #ifdef GENTABLES
  400. printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n");
  401. for (i = 0; i < 8; i++) {
  402. printf(" {");
  403. for (j = 0; j < 64; j++) {
  404. uint32_t v = S_boxes[i][j >> 1];
  405. v = j & 1 ? v >> 4 : v & 0xf;
  406. v <<= 28 - 4 * i;
  407. v = shuffle(v, P_shuffle, sizeof(P_shuffle));
  408. printf((j & 7) == 0 ? "\n " : " ");
  409. printf("0x%08X,", v);
  410. }
  411. printf("\n },\n");
  412. }
  413. printf("};\n");
  414. #endif
  415. return 0;
  416. }
  417. #endif