DESCryptoServiceProvider.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. //
  2. // System.Security.Cryptography.DESCryptoServiceProvider
  3. //
  4. // Authors:
  5. // Sergey Chaban ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  9. //
  10. using System;
  11. namespace System.Security.Cryptography {
  12. // References:
  13. // a. FIPS PUB 46-3: Data Encryption Standard
  14. // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  15. internal class DESTransform : SymmetricTransform {
  16. internal static readonly int KEY_BIT_SIZE = 64;
  17. internal static readonly int KEY_BYTE_SIZE = KEY_BIT_SIZE / 8;
  18. internal static readonly int BLOCK_BIT_SIZE = 64;
  19. internal static readonly int BLOCK_BYTE_SIZE = BLOCK_BIT_SIZE / 8;
  20. private byte [] keySchedule;
  21. private byte [] byteBuff;
  22. private uint [] dwordBuff;
  23. // S-boxes from FIPS 46-3, Appendix 1, page 17
  24. private static byte [] sBoxes = {
  25. /* S1 */
  26. 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
  27. 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
  28. 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
  29. 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
  30. /* S2 */
  31. 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
  32. 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
  33. 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
  34. 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
  35. /* S3 */
  36. 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
  37. 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
  38. 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
  39. 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
  40. /* S4 */
  41. 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
  42. 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
  43. 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
  44. 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
  45. /* S5 */
  46. 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
  47. 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
  48. 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
  49. 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
  50. /* S6 */
  51. 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
  52. 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
  53. 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
  54. 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
  55. /* S7 */
  56. 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
  57. 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
  58. 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
  59. 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
  60. /* S8 */
  61. 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
  62. 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
  63. 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
  64. 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
  65. };
  66. // P table from page 15, also in Appendix 1, page 18
  67. private static byte [] pTab = {
  68. 16-1, 7-1, 20-1, 21-1,
  69. 29-1, 12-1, 28-1, 17-1,
  70. 1-1, 15-1, 23-1, 26-1,
  71. 5-1, 18-1, 31-1, 10-1,
  72. 2-1, 8-1, 24-1, 14-1,
  73. 32-1, 27-1, 3-1, 9-1,
  74. 19-1, 13-1, 30-1, 6-1,
  75. 22-1, 11-1, 4-1, 25-1
  76. };
  77. // Permuted choice 1 table, PC-1, page 19
  78. // Translated to zero-based format.
  79. private static byte [] PC1 = {
  80. 57-1, 49-1, 41-1, 33-1, 25-1, 17-1, 9-1,
  81. 1-1, 58-1, 50-1, 42-1, 34-1, 26-1, 18-1,
  82. 10-1, 2-1, 59-1, 51-1, 43-1, 35-1, 27-1,
  83. 19-1, 11-1, 3-1, 60-1, 52-1, 44-1, 36-1,
  84. 63-1, 55-1, 47-1, 39-1, 31-1, 23-1, 15-1,
  85. 7-1, 62-1, 54-1, 46-1, 38-1, 30-1, 22-1,
  86. 14-1, 6-1, 61-1, 53-1, 45-1, 37-1, 29-1,
  87. 21-1, 13-1, 5-1, 28-1, 20-1, 12-1, 4-1
  88. };
  89. private static byte [] leftRot = {
  90. 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
  91. };
  92. private static byte [] leftRotTotal;
  93. // Permuted choice 2 table, PC-2, page 21
  94. // Translated to zero-based format.
  95. private static byte [] PC2 = {
  96. 14-1, 17-1, 11-1, 24-1, 1-1, 5-1,
  97. 3-1, 28-1, 15-1, 6-1, 21-1, 10-1,
  98. 23-1, 19-1, 12-1, 4-1, 26-1, 8-1,
  99. 16-1, 7-1, 27-1, 20-1, 13-1, 2-1,
  100. 41-1, 52-1, 31-1, 37-1, 47-1, 55-1,
  101. 30-1, 40-1, 51-1, 45-1, 33-1, 48-1,
  102. 44-1, 49-1, 39-1, 56-1, 34-1, 53-1,
  103. 46-1, 42-1, 50-1, 36-1, 29-1, 32-1
  104. };
  105. // Initial permutation IP, page 10.
  106. // Transposed to 0-based format.
  107. private static byte [] ipBits = {
  108. 58-1, 50-1, 42-1, 34-1, 26-1, 18-1, 10-1, 2-1,
  109. 60-1, 52-1, 44-1, 36-1, 28-1, 20-1, 12-1, 4-1,
  110. 62-1, 54-1, 46-1, 38-1, 30-1, 22-1, 14-1, 6-1,
  111. 64-1, 56-1, 48-1, 40-1, 32-1, 24-1, 16-1, 8-1,
  112. 57-1, 49-1, 41-1, 33-1, 25-1, 17-1, 9-1, 1-1,
  113. 59-1, 51-1, 43-1, 35-1, 27-1, 19-1, 11-1, 3-1,
  114. 61-1, 53-1, 45-1, 37-1, 29-1, 21-1, 13-1, 5-1,
  115. 63-1, 55-1, 47-1, 39-1, 31-1, 23-1, 15-1, 7-1
  116. };
  117. // Final permutation FP = IP^(-1), page 10.
  118. // Transposed to 0-based format.
  119. private static byte [] fpBits = {
  120. 40-1, 8-1, 48-1, 16-1, 56-1, 24-1, 64-1, 32-1,
  121. 39-1, 7-1, 47-1, 15-1, 55-1, 23-1, 63-1, 31-1,
  122. 38-1, 6-1, 46-1, 14-1, 54-1, 22-1, 62-1, 30-1,
  123. 37-1, 5-1, 45-1, 13-1, 53-1, 21-1, 61-1, 29-1,
  124. 36-1, 4-1, 44-1, 12-1, 52-1, 20-1, 60-1, 28-1,
  125. 35-1, 3-1, 43-1, 11-1, 51-1, 19-1, 59-1, 27-1,
  126. 34-1, 2-1, 42-1, 10-1, 50-1, 18-1, 58-1, 26-1,
  127. 33-1, 1-1, 41-1, 9-1, 49-1, 17-1, 57-1, 25-1
  128. };
  129. private static uint [] spBoxes;
  130. private static int [] ipTab;
  131. private static int [] fpTab;
  132. static DESTransform ()
  133. {
  134. spBoxes = new uint [64 * 8];
  135. int [] pBox = new int [32];
  136. for (int p = 0; p < 32; p++) {
  137. for (int i = 0; i < 32; i++) {
  138. if (p == pTab [i]) {
  139. pBox [p] = i;
  140. break;
  141. }
  142. }
  143. }
  144. for (int s = 0; s < 8; s++) { // for each S-box
  145. int sOff = s << 6;
  146. for (int i = 0; i < 64; i++) { // inputs
  147. uint sp=0;
  148. int indx = (i & 0x20) | ((i & 1) << 4) | ((i >> 1) & 0xF);
  149. for (int j = 0; j < 4; j++) { // for each bit in the output
  150. if ((sBoxes [sOff + indx] & (8 >> j)) != 0) {
  151. sp |= (uint) (1 << (31 - pBox [(s << 2) + j]));
  152. }
  153. }
  154. spBoxes [sOff + i] = sp;
  155. }
  156. }
  157. leftRotTotal = new byte [leftRot.Length];
  158. for (int i = 0; i < leftRot.Length; i++) {
  159. int r = 0;
  160. for (int j = 0; j <= i; r += leftRot [j++]);
  161. leftRotTotal [i] = (byte) r;
  162. }
  163. InitPermutationTable (ipBits, out ipTab);
  164. InitPermutationTable (fpBits, out fpTab);
  165. } // class constructor
  166. // Default constructor.
  167. internal DESTransform (SymmetricAlgorithm symmAlgo, bool encryption, byte[] key, byte[] iv) : base (symmAlgo, encryption, iv)
  168. {
  169. keySchedule = new byte [KEY_BYTE_SIZE * 16];
  170. byteBuff = new byte [BLOCK_BYTE_SIZE];
  171. dwordBuff = new uint [BLOCK_BYTE_SIZE / 4];
  172. SetKey (key);
  173. }
  174. private static void InitPermutationTable (byte [] pBits, out int [] permTab)
  175. {
  176. permTab = new int [8*2 * 8*2 * (64/32)];
  177. for (int i = 0; i < 16; i++) {
  178. for (int j = 0; j < 16; j++) {
  179. int offs = (i << 5) + (j << 1);
  180. for (int n = 0; n < 64; n++) {
  181. int bitNum = (int) pBits [n];
  182. if ((bitNum >> 2 == i) &&
  183. 0 != (j & (8 >> (bitNum & 3)))) {
  184. permTab [offs + (n >> (3+2))] |= (int) ((0x80808080 & (0xFF << (n & (3 << 3)))) >> (n & 7));
  185. }
  186. }
  187. }
  188. }
  189. }
  190. private uint CipherFunct(uint r, int n)
  191. {
  192. uint res = 0;
  193. byte [] subkey = keySchedule;
  194. int i = n << 3;
  195. uint rt = (r >> 1) | (r << 31); // ROR32(r)
  196. res |= spBoxes [0*64 + (((rt >> 26) ^ subkey [i++]) & 0x3F)];
  197. res |= spBoxes [1*64 + (((rt >> 22) ^ subkey [i++]) & 0x3F)];
  198. res |= spBoxes [2*64 + (((rt >> 18) ^ subkey [i++]) & 0x3F)];
  199. res |= spBoxes [3*64 + (((rt >> 14) ^ subkey [i++]) & 0x3F)];
  200. res |= spBoxes [4*64 + (((rt >> 10) ^ subkey [i++]) & 0x3F)];
  201. res |= spBoxes [5*64 + (((rt >> 6) ^ subkey [i++]) & 0x3F)];
  202. res |= spBoxes [6*64 + (((rt >> 2) ^ subkey [i++]) & 0x3F)];
  203. rt = (r << 1) | (r >> 31); // ROL32(r)
  204. res |= spBoxes [7*64 + ((rt ^ subkey [i]) & 0x3F)];
  205. return res;
  206. }
  207. private static void Permutation (byte [] input, byte [] _output, int [] permTab, bool preSwap)
  208. {
  209. if (preSwap) BSwap (input);
  210. byte [] output = _output;
  211. int offs1 = (((int)(input [0]) >> 4)) << 1;
  212. int offs2 = (1 << 5) + ((((int)input [0]) & 0xF) << 1);
  213. int d1 = permTab [offs1++] | permTab [offs2++];
  214. int d2 = permTab [offs1] | permTab [offs2];
  215. int max = BLOCK_BYTE_SIZE << 1;
  216. for (int i = 2, indx = 1; i < max; i += 2, indx++) {
  217. int ii = (int) input [indx];
  218. offs1 = (i << 5) + ((ii >> 4) << 1);
  219. offs2 = ((i + 1) << 5) + ((ii & 0xF) << 1);
  220. d1 |= permTab [offs1++] | permTab [offs2++];
  221. d2 |= permTab [offs1] | permTab [offs2];
  222. }
  223. if (preSwap) {
  224. output [0] = (byte) (d1);
  225. output [1] = (byte) (d1 >> 8);
  226. output [2] = (byte) (d1 >> 16);
  227. output [3] = (byte) (d1 >> 24);
  228. output [4] = (byte) (d2);
  229. output [5] = (byte) (d2 >> 8);
  230. output [6] = (byte) (d2 >> 16);
  231. output [7] = (byte) (d2 >> 24);
  232. } else {
  233. output [0] = (byte) (d1 >> 24);
  234. output [1] = (byte) (d1 >> 16);
  235. output [2] = (byte) (d1 >> 8);
  236. output [3] = (byte) (d1);
  237. output [4] = (byte) (d2 >> 24);
  238. output [5] = (byte) (d2 >> 16);
  239. output [6] = (byte) (d2 >> 8);
  240. output [7] = (byte) (d2);
  241. }
  242. }
  243. private static void BSwap (byte [] byteBuff)
  244. {
  245. byte t;
  246. t = byteBuff [0];
  247. byteBuff [0] = byteBuff [3];
  248. byteBuff [3] = t;
  249. t = byteBuff [1];
  250. byteBuff [1] = byteBuff [2];
  251. byteBuff [2] = t;
  252. t = byteBuff [4];
  253. byteBuff [4] = byteBuff [7];
  254. byteBuff [7] = t;
  255. t = byteBuff [5];
  256. byteBuff [5] = byteBuff [6];
  257. byteBuff [6] = t;
  258. }
  259. internal void SetKey (byte[] key)
  260. {
  261. // NOTE: see Fig. 3, Key schedule calculation, at page 20.
  262. Array.Clear (keySchedule, 0, keySchedule.Length);
  263. int keyBitSize = PC1.Length;
  264. byte [] keyPC1 = new byte [keyBitSize]; // PC1-permuted key
  265. byte [] keyRot = new byte [keyBitSize]; // PC1 & rotated
  266. int indx = 0;
  267. foreach (byte bitPos in PC1) {
  268. keyPC1 [indx++] = (byte)((key [(int)bitPos >> 3] >> (7 ^ (bitPos & 7))) & 1);
  269. }
  270. int j;
  271. for (int i = 0; i < KEY_BYTE_SIZE*2; i++) {
  272. int b = keyBitSize >> 1;
  273. for (j = 0; j < b; j++) {
  274. int s = j + (int) leftRotTotal [i];
  275. keyRot [j] = keyPC1 [s < b ? s : s - b];
  276. }
  277. for (j = b; j < keyBitSize; j++) {
  278. int s = j + (int) leftRotTotal [i];
  279. keyRot [j] = keyPC1 [s < keyBitSize ? s : s - b];
  280. }
  281. int keyOffs = i * KEY_BYTE_SIZE;
  282. j = 0;
  283. foreach (byte bitPos in PC2) {
  284. if (keyRot [(int)bitPos] != 0) {
  285. keySchedule [keyOffs + (j/6)] |= (byte) (0x80 >> ((j % 6) + 2));
  286. }
  287. j++;
  288. }
  289. }
  290. }
  291. // public helper for TripleDES
  292. public void ProcessBlock (byte[] input, byte[] output)
  293. {
  294. ECB (input, output);
  295. }
  296. protected override void ECB (byte[] input, byte[] output)
  297. {
  298. byte [] byteBuff = this.byteBuff;
  299. uint [] dwordBuff = this.dwordBuff;
  300. Permutation (input, byteBuff, ipTab, false);
  301. Buffer.BlockCopy (byteBuff, 0, dwordBuff, 0, BLOCK_BYTE_SIZE);
  302. if (encrypt) {
  303. uint d0 = dwordBuff[0];
  304. uint d1 = dwordBuff[1];
  305. // 16 rounds
  306. d0 ^= CipherFunct (d1, 0);
  307. d1 ^= CipherFunct (d0, 1);
  308. d0 ^= CipherFunct (d1, 2);
  309. d1 ^= CipherFunct (d0, 3);
  310. d0 ^= CipherFunct (d1, 4);
  311. d1 ^= CipherFunct (d0, 5);
  312. d0 ^= CipherFunct (d1, 6);
  313. d1 ^= CipherFunct (d0, 7);
  314. d0 ^= CipherFunct (d1, 8);
  315. d1 ^= CipherFunct (d0, 9);
  316. d0 ^= CipherFunct (d1, 10);
  317. d1 ^= CipherFunct (d0, 11);
  318. d0 ^= CipherFunct (d1, 12);
  319. d1 ^= CipherFunct (d0, 13);
  320. d0 ^= CipherFunct (d1, 14);
  321. d1 ^= CipherFunct (d0, 15);
  322. dwordBuff [0] = d1;
  323. dwordBuff [1] = d0;
  324. }
  325. else {
  326. uint d1 = dwordBuff [0];
  327. uint d0 = dwordBuff [1];
  328. // 16 rounds in reverse order
  329. d1 ^= CipherFunct (d0, 15);
  330. d0 ^= CipherFunct (d1, 14);
  331. d1 ^= CipherFunct (d0, 13);
  332. d0 ^= CipherFunct (d1, 12);
  333. d1 ^= CipherFunct (d0, 11);
  334. d0 ^= CipherFunct (d1, 10);
  335. d1 ^= CipherFunct (d0, 9);
  336. d0 ^= CipherFunct (d1, 8);
  337. d1 ^= CipherFunct (d0, 7);
  338. d0 ^= CipherFunct (d1, 6);
  339. d1 ^= CipherFunct (d0, 5);
  340. d0 ^= CipherFunct (d1, 4);
  341. d1 ^= CipherFunct (d0, 3);
  342. d0 ^= CipherFunct (d1, 2);
  343. d1 ^= CipherFunct (d0, 1);
  344. d0 ^= CipherFunct (d1, 0);
  345. dwordBuff [0] = d0;
  346. dwordBuff [1] = d1;
  347. }
  348. Buffer.BlockCopy (dwordBuff, 0, byteBuff, 0, BLOCK_BYTE_SIZE);
  349. Permutation (byteBuff, output, fpTab, true);
  350. }
  351. }
  352. public sealed class DESCryptoServiceProvider : DES {
  353. public DESCryptoServiceProvider () : base () {}
  354. public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
  355. {
  356. Key = rgbKey;
  357. IV = rgbIV;
  358. return new DESTransform (this, false, rgbKey, rgbIV);
  359. }
  360. public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
  361. {
  362. Key = rgbKey;
  363. IV = rgbIV;
  364. return new DESTransform (this, true, rgbKey, rgbIV);
  365. }
  366. public override void GenerateIV ()
  367. {
  368. IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
  369. }
  370. public override void GenerateKey ()
  371. {
  372. KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
  373. while (IsWeakKey (KeyValue) || IsSemiWeakKey (KeyValue))
  374. KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
  375. }
  376. } // DESCryptoServiceProvider
  377. } // System.Security.Cryptography