cast.c
上传用户:market2
上传日期:2018-11-18
资源大小:18786k
文件大小:7k
源码类别:

外挂编程

开发平台:

Windows_Unix

  1. /*
  2.  * CAST-128 in C
  3.  * Written by Steve Reid <sreid@sea-to-sky.net>
  4.  * 100% Public Domain - no warranty
  5.  * Released 1997.10.11
  6.  */
  7. #include <string.h>
  8. #include "../typedefs.h"
  9. #include "cast.h"
  10. #include "cast_sboxes.h"
  11. // Macros to access 8-bit bytes out of a 32-bit word
  12. #define U8a(x) ( (byte) (x>>24) )
  13. #define U8b(x) ( (byte) ((x>>16)&255) )
  14. #define U8c(x) ( (byte) ((x>>8)&255) )
  15. #define U8d(x) ( (byte) ((x)&255) )
  16. // Circular left shift
  17. #define ROL(x, n) ( ((x)<<(n)) | ((x)>>(32-(n))) )
  18. // CAST-128 uses three different round functions
  19. #define F1(l, r, i) 
  20. t = ROL(key->xkey[i] + r, key->xkey[i+16]); 
  21. l ^= ((cast_sbox1[U8a(t)] ^ cast_sbox2[U8b(t)]) - 
  22.  cast_sbox3[U8c(t)]) + cast_sbox4[U8d(t)];
  23. #define F2(l, r, i) 
  24. t = ROL(key->xkey[i] ^ r, key->xkey[i+16]); 
  25. l ^= ((cast_sbox1[U8a(t)] - cast_sbox2[U8b(t)]) + 
  26.  cast_sbox3[U8c(t)]) ^ cast_sbox4[U8d(t)];
  27. #define F3(l, r, i) 
  28. t = ROL(key->xkey[i] - r, key->xkey[i+16]); 
  29. l ^= ((cast_sbox1[U8a(t)] + cast_sbox2[U8b(t)]) ^ 
  30.  cast_sbox3[U8c(t)]) - cast_sbox4[U8d(t)];
  31. /***** Encryption Function *****/
  32. void cast_encrypt(cast_key* key, byte* inblock, byte* outblock)
  33. {
  34. dword t, l, r;
  35. // Get inblock into l,r
  36. l = ((dword)inblock[0] << 24) | ((dword)inblock[1] << 16) |
  37. ((dword)inblock[2] << 8) | (dword)inblock[3];
  38. r = ((dword)inblock[4] << 24) | ((dword)inblock[5] << 16) |
  39. ((dword)inblock[6] << 8) | (dword)inblock[7];
  40. // Do the work
  41. F1(l, r,  0);
  42. F2(r, l,  1);
  43. F3(l, r,  2);
  44. F1(r, l,  3);
  45. F2(l, r,  4);
  46. F3(r, l,  5);
  47. F1(l, r,  6);
  48. F2(r, l,  7);
  49. F3(l, r,  8);
  50. F1(r, l,  9);
  51. F2(l, r, 10);
  52. F3(r, l, 11);
  53. // Only do full 16 rounds if key length > 80 bits
  54. if (key->rounds > 12) {
  55. F1(l, r, 12);
  56. F2(r, l, 13);
  57. F3(l, r, 14);
  58. F1(r, l, 15);
  59. }
  60. // Put l,r into outblock
  61. outblock[0] = U8a(r);
  62. outblock[1] = U8b(r);
  63. outblock[2] = U8c(r);
  64. outblock[3] = U8d(r);
  65. outblock[4] = U8a(l);
  66. outblock[5] = U8b(l);
  67. outblock[6] = U8c(l);
  68. outblock[7] = U8d(l);
  69. // Wipe clean
  70. t = l = r = 0;
  71. }
  72. /***** Decryption Function *****/
  73. void cast_decrypt(cast_key* key, byte* inblock, byte* outblock)
  74. {
  75. dword t, l, r;
  76. // Get inblock into l,r
  77. r = ((dword)inblock[0] << 24) | ((dword)inblock[1] << 16) |
  78.  ((dword)inblock[2] << 8) | (dword)inblock[3];
  79. l = ((dword)inblock[4] << 24) | ((dword)inblock[5] << 16) |
  80.  ((dword)inblock[6] << 8) | (dword)inblock[7];
  81.  
  82. // Do the work
  83. // Only do full 16 rounds if key length > 80 bits
  84. if (key->rounds > 12) {
  85. F1(r, l, 15);
  86. F3(l, r, 14);
  87. F2(r, l, 13);
  88. F1(l, r, 12);
  89. }
  90. F3(r, l, 11);
  91. F2(l, r, 10);
  92. F1(r, l,  9);
  93. F3(l, r,  8);
  94. F2(r, l,  7);
  95. F1(l, r,  6);
  96. F3(r, l,  5);
  97. F2(l, r,  4);
  98. F1(r, l,  3);
  99. F3(l, r,  2);
  100. F2(r, l,  1);
  101. F1(l, r,  0);
  102. // Put l,r into outblock
  103. outblock[0] = U8a(l);
  104. outblock[1] = U8b(l);
  105. outblock[2] = U8c(l);
  106. outblock[3] = U8d(l);
  107. outblock[4] = U8a(r);
  108. outblock[5] = U8b(r);
  109. outblock[6] = U8c(r);
  110. outblock[7] = U8d(r);
  111. // Wipe clean
  112. t = l = r = 0;
  113. }
  114. /***** Key Schedual *****/
  115. void cast_setkey(cast_key* key, byte* rawkey, int keybytes)
  116. {
  117. dword t[4], z[4], x[4];
  118. int i;
  119. memset(t, 0, sizeof(t)); memset(z, 0, sizeof(z)); memset(x, 0, sizeof(x));
  120. // Set number of rounds to 12 or 16, depending on key length
  121. key->rounds = (keybytes <= 10 ? 12 : 16);
  122. // Copy key to workspace x
  123. for (i = 0; i < 4; i++) {
  124. x[i] = 0;
  125. if ((i*4+0) < keybytes) x[i] = (dword)rawkey[i*4+0] << 24;
  126. if ((i*4+1) < keybytes) x[i] |= (dword)rawkey[i*4+1] << 16;
  127. if ((i*4+2) < keybytes) x[i] |= (dword)rawkey[i*4+2] << 8;
  128. if ((i*4+3) < keybytes) x[i] |= (dword)rawkey[i*4+3];
  129. }
  130. // Generate 32 subkeys, four at a time
  131. for (i = 0; i < 32; i+=4) {
  132. switch (i & 4) {
  133.  case 0:
  134. t[0] = z[0] = x[0] ^ cast_sbox5[U8b(x[3])] ^
  135.  cast_sbox6[U8d(x[3])] ^ cast_sbox7[U8a(x[3])] ^
  136.  cast_sbox8[U8c(x[3])] ^ cast_sbox7[U8a(x[2])];
  137. t[1] = z[1] = x[2] ^ cast_sbox5[U8a(z[0])] ^
  138.  cast_sbox6[U8c(z[0])] ^ cast_sbox7[U8b(z[0])] ^
  139.  cast_sbox8[U8d(z[0])] ^ cast_sbox8[U8c(x[2])];
  140. t[2] = z[2] = x[3] ^ cast_sbox5[U8d(z[1])] ^
  141.  cast_sbox6[U8c(z[1])] ^ cast_sbox7[U8b(z[1])] ^
  142.  cast_sbox8[U8a(z[1])] ^ cast_sbox5[U8b(x[2])];
  143. t[3] = z[3] = x[1] ^ cast_sbox5[U8c(z[2])] ^
  144.  cast_sbox6[U8b(z[2])] ^ cast_sbox7[U8d(z[2])] ^
  145.  cast_sbox8[U8a(z[2])] ^ cast_sbox6[U8d(x[2])];
  146. break;
  147.  case 4:
  148. t[0] = x[0] = z[2] ^ cast_sbox5[U8b(z[1])] ^
  149.  cast_sbox6[U8d(z[1])] ^ cast_sbox7[U8a(z[1])] ^
  150.  cast_sbox8[U8c(z[1])] ^ cast_sbox7[U8a(z[0])];
  151. t[1] = x[1] = z[0] ^ cast_sbox5[U8a(x[0])] ^
  152.  cast_sbox6[U8c(x[0])] ^ cast_sbox7[U8b(x[0])] ^
  153.  cast_sbox8[U8d(x[0])] ^ cast_sbox8[U8c(z[0])];
  154. t[2] = x[2] = z[1] ^ cast_sbox5[U8d(x[1])] ^
  155.  cast_sbox6[U8c(x[1])] ^ cast_sbox7[U8b(x[1])] ^
  156.  cast_sbox8[U8a(x[1])] ^ cast_sbox5[U8b(z[0])];
  157. t[3] = x[3] = z[3] ^ cast_sbox5[U8c(x[2])] ^
  158.  cast_sbox6[U8b(x[2])] ^ cast_sbox7[U8d(x[2])] ^
  159.  cast_sbox8[U8a(x[2])] ^ cast_sbox6[U8d(z[0])];
  160. break;
  161. }
  162. switch (i & 12) {
  163.  case 0:
  164.  case 12:
  165. key->xkey[i+0] = cast_sbox5[U8a(t[2])] ^ cast_sbox6[U8b(t[2])] ^
  166.  cast_sbox7[U8d(t[1])] ^ cast_sbox8[U8c(t[1])];
  167. key->xkey[i+1] = cast_sbox5[U8c(t[2])] ^ cast_sbox6[U8d(t[2])] ^
  168.  cast_sbox7[U8b(t[1])] ^ cast_sbox8[U8a(t[1])];
  169. key->xkey[i+2] = cast_sbox5[U8a(t[3])] ^ cast_sbox6[U8b(t[3])] ^
  170.  cast_sbox7[U8d(t[0])] ^ cast_sbox8[U8c(t[0])];
  171. key->xkey[i+3] = cast_sbox5[U8c(t[3])] ^ cast_sbox6[U8d(t[3])] ^
  172.  cast_sbox7[U8b(t[0])] ^ cast_sbox8[U8a(t[0])];
  173. break;
  174.  case 4:
  175.  case 8:
  176. key->xkey[i+0] = cast_sbox5[U8d(t[0])] ^ cast_sbox6[U8c(t[0])] ^
  177.  cast_sbox7[U8a(t[3])] ^ cast_sbox8[U8b(t[3])];
  178. key->xkey[i+1] = cast_sbox5[U8b(t[0])] ^ cast_sbox6[U8a(t[0])] ^
  179.  cast_sbox7[U8c(t[3])] ^ cast_sbox8[U8d(t[3])];
  180. key->xkey[i+2] = cast_sbox5[U8d(t[1])] ^ cast_sbox6[U8c(t[1])] ^
  181.  cast_sbox7[U8a(t[2])] ^ cast_sbox8[U8b(t[2])];
  182. key->xkey[i+3] = cast_sbox5[U8b(t[1])] ^ cast_sbox6[U8a(t[1])] ^
  183.  cast_sbox7[U8c(t[2])] ^ cast_sbox8[U8d(t[2])];
  184. break;
  185. }
  186. switch (i & 12) {
  187.  case 0:
  188. key->xkey[i+0] ^= cast_sbox5[U8c(z[0])];
  189. key->xkey[i+1] ^= cast_sbox6[U8c(z[1])];
  190. key->xkey[i+2] ^= cast_sbox7[U8b(z[2])];
  191. key->xkey[i+3] ^= cast_sbox8[U8a(z[3])];
  192. break;
  193.  case 4:
  194. key->xkey[i+0] ^= cast_sbox5[U8a(x[2])];
  195. key->xkey[i+1] ^= cast_sbox6[U8b(x[3])];
  196. key->xkey[i+2] ^= cast_sbox7[U8d(x[0])];
  197. key->xkey[i+3] ^= cast_sbox8[U8d(x[1])];
  198. break;
  199.  case 8:
  200. key->xkey[i+0] ^= cast_sbox5[U8b(z[2])];
  201. key->xkey[i+1] ^= cast_sbox6[U8a(z[3])];
  202. key->xkey[i+2] ^= cast_sbox7[U8c(z[0])];
  203. key->xkey[i+3] ^= cast_sbox8[U8c(z[1])];
  204. break;
  205.  case 12:
  206. key->xkey[i+0] ^= cast_sbox5[U8d(x[0])];
  207. key->xkey[i+1] ^= cast_sbox6[U8d(x[1])];
  208. key->xkey[i+2] ^= cast_sbox7[U8a(x[2])];
  209. key->xkey[i+3] ^= cast_sbox8[U8b(x[3])];
  210. break;
  211. }
  212. if (i >= 16) {
  213. key->xkey[i+0] &= 31;
  214. key->xkey[i+1] &= 31;
  215. key->xkey[i+2] &= 31;
  216. key->xkey[i+3] &= 31;
  217. }
  218. }
  219. // Wipe clean
  220. for (i = 0; i < 4; i++) {
  221. t[i] = x[i] = z[i] = 0;
  222. }
  223. }