cry1.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * shift register length = 65
  3. * tap at position 18
  4. * shuffle-buffer size = 4096
  5. */
  6. #ifdef TIME_TEST
  7. #include <stdio.h>
  8. #include <time.h>
  9. #define N 10000000 /* parameters for time test */
  10. #define NSTARTS 4000
  11. #define NTINY 50000000
  12. #define KEY "Arthurs's sample key"
  13. typedef unsigned int unsigned32;
  14. #endif /* TIME_TEST */
  15. static unsigned32 lf[65], mix[4096];
  16. #define R(x) ((x) >> 20)
  17. #define S(x) ((x) >> 9)
  18. #define T(x) ((x) << 13)
  19. static unsigned char byte_order_test[] = {1, 0, 0, 0};
  20. #define CRYPT_BLOCK_SIZE 128
  21. void crypt_get_block(unsigned char block[CRYPT_BLOCK_SIZE])
  22. {
  23. unsigned32 *b = (unsigned32 *)block;
  24. int n;
  25. lf[0] -= lf[18]; lf[1] ^= lf[19];
  26. lf[2] -= lf[20]; lf[3] += lf[21];
  27. lf[4] += lf[22]; lf[5] -= lf[23];
  28. lf[6] ^= lf[24]; lf[7] -= lf[25];
  29. lf[8] += lf[26]; lf[9] ^= lf[27];
  30. lf[10] -= lf[28]; lf[11] -= lf[29];
  31. lf[12] += lf[30]; lf[13] += lf[31];
  32. lf[14] -= lf[32]; lf[15] ^= lf[33];
  33. lf[16] -= lf[34]; lf[17] += lf[35];
  34. lf[18] += lf[36]; lf[19] += lf[37];
  35. lf[20] -= lf[38]; lf[21] -= lf[39];
  36. lf[22] ^= lf[40]; lf[23] += lf[41];
  37. lf[24] -= lf[42]; lf[25] -= lf[43];
  38. lf[26] += lf[44]; lf[27] += lf[45];
  39. lf[28] -= lf[46]; lf[29] ^= lf[47];
  40. lf[30] -= lf[48]; lf[31] += lf[49];
  41. lf[32] -= lf[50]; lf[33] ^= lf[51];
  42. lf[34] -= lf[52]; lf[35] ^= lf[53];
  43. lf[36] += lf[54]; lf[37] += lf[55];
  44. lf[38] ^= lf[56]; lf[39] ^= lf[57];
  45. lf[40] += lf[58]; lf[41] -= lf[59];
  46. lf[42] ^= lf[60]; lf[43] += lf[61];
  47. lf[44] += lf[62]; lf[45] ^= lf[63];
  48. lf[46] ^= lf[64]; lf[47] -= lf[0];
  49. lf[48] ^= lf[1]; lf[49] ^= lf[2];
  50. lf[50] ^= lf[3]; lf[51] ^= lf[4];
  51. lf[52] ^= lf[5]; lf[53] ^= lf[6];
  52. lf[54] += lf[7]; lf[55] -= lf[8];
  53. lf[56] -= lf[9]; lf[57] ^= lf[10];
  54. lf[58] -= lf[11]; lf[59] -= lf[12];
  55. lf[60] ^= lf[13]; lf[61] += lf[14];
  56. lf[62] ^= lf[15]; lf[63] -= lf[16];
  57. lf[64] -= lf[17];
  58. n = R(lf[0]); b[0] = mix[n]; mix[n] = (lf[54] + S(lf[29])) ^ T(lf[5]);
  59. n = R(lf[1]); b[1] = mix[n]; mix[n] = (lf[39] + S(lf[47])) + T(lf[15]);
  60. n = R(lf[2]); b[2] = mix[n]; mix[n] = (lf[25] + S(lf[14])) + T(lf[38]);
  61. n = R(lf[4]); b[3] = mix[n]; mix[n] = (lf[48] - S(lf[40])) ^ T(lf[10]);
  62. n = R(lf[5]); b[4] = mix[n]; mix[n] = (lf[44] - S(lf[55])) - T(lf[49]);
  63. n = R(lf[6]); b[5] = mix[n]; mix[n] = (lf[9] ^ S(lf[37])) + T(lf[50]);
  64. n = R(lf[8]); b[6] = mix[n]; mix[n] = (lf[64] ^ S(lf[51])) + T(lf[8]);
  65. n = R(lf[9]); b[7] = mix[n]; mix[n] = (lf[11] - S(lf[35])) - T(lf[21]);
  66. n = R(lf[10]); b[8] = mix[n]; mix[n] = (lf[20] ^ S(lf[21])) ^ T(lf[3]);
  67. n = R(lf[12]); b[9] = mix[n]; mix[n] = (lf[6] ^ S(lf[31])) - T(lf[61]);
  68. n = R(lf[13]); b[10] = mix[n]; mix[n] = (lf[3] - S(lf[16])) ^ T(lf[16]);
  69. n = R(lf[14]); b[11] = mix[n]; mix[n] = (lf[17] - S(lf[53])) - T(lf[2]);
  70. n = R(lf[16]); b[12] = mix[n]; mix[n] = (lf[27] + S(lf[42])) - T(lf[33]);
  71. n = R(lf[17]); b[13] = mix[n]; mix[n] = (lf[28] + S(lf[63])) - T(lf[46]);
  72. n = R(lf[18]); b[14] = mix[n]; mix[n] = (lf[10] - S(lf[46])) + T(lf[35]);
  73. n = R(lf[20]); b[15] = mix[n]; mix[n] = (lf[53] - S(lf[10])) - T(lf[27]);
  74. n = R(lf[21]); b[16] = mix[n]; mix[n] = (lf[4] + S(lf[18])) - T(lf[7]);
  75. n = R(lf[22]); b[17] = mix[n]; mix[n] = (lf[43] + S(lf[64])) ^ T(lf[45]);
  76. n = R(lf[24]); b[18] = mix[n]; mix[n] = (lf[14] + S(lf[26])) + T(lf[44]);
  77. n = R(lf[25]); b[19] = mix[n]; mix[n] = (lf[23] ^ S(lf[38])) + T(lf[58]);
  78. n = R(lf[26]); b[20] = mix[n]; mix[n] = (lf[47] + S(lf[59])) ^ T(lf[47]);
  79. n = R(lf[28]); b[21] = mix[n]; mix[n] = (lf[63] - S(lf[36])) - T(lf[57]);
  80. n = R(lf[29]); b[22] = mix[n]; mix[n] = (lf[56] + S(lf[4])) + T(lf[19]);
  81. n = R(lf[30]); b[23] = mix[n]; mix[n] = (lf[42] - S(lf[52])) - T(lf[56]);
  82. n = R(lf[32]); b[24] = mix[n]; mix[n] = (lf[37] + S(lf[3])) - T(lf[63]);
  83. n = R(lf[33]); b[25] = mix[n]; mix[n] = (lf[32] + S(lf[1])) - T(lf[12]);
  84. n = R(lf[34]); b[26] = mix[n]; mix[n] = (lf[62] - S(lf[39])) - T(lf[31]);
  85. n = R(lf[36]); b[27] = mix[n]; mix[n] = (lf[2] ^ S(lf[44])) ^ T(lf[18]);
  86. n = R(lf[37]); b[28] = mix[n]; mix[n] = (lf[24] ^ S(lf[50])) ^ T(lf[55]);
  87. n = R(lf[38]); b[29] = mix[n]; mix[n] = (lf[22] + S(lf[27])) - T(lf[32]);
  88. n = R(lf[40]); b[30] = mix[n]; mix[n] = (lf[51] + S(lf[33])) + T(lf[0]);
  89. n = R(lf[41]); b[31] = mix[n]; mix[n] = (lf[52] ^ S(lf[19])) - T(lf[26]);
  90. n = R(lf[42]); mix[n] = (lf[5] ^ S(lf[41])) + T(lf[28]);
  91. n = R(lf[44]); mix[n] = (lf[30] ^ S(lf[15])) - T(lf[30]);
  92. n = R(lf[45]); mix[n] = (lf[45] + S(lf[24])) ^ T(lf[51]);
  93. n = R(lf[46]); mix[n] = (lf[13] + S(lf[49])) - T(lf[11]);
  94. n = R(lf[48]); mix[n] = (lf[16] + S(lf[11])) - T(lf[39]);
  95. n = R(lf[49]); mix[n] = (lf[57] - S(lf[43])) - T(lf[60]);
  96. n = R(lf[50]); mix[n] = (lf[49] + S(lf[48])) ^ T(lf[25]);
  97. n = R(lf[52]); mix[n] = (lf[34] - S(lf[22])) ^ T(lf[23]);
  98. n = R(lf[53]); mix[n] = (lf[18] + S(lf[6])) + T(lf[1]);
  99. n = R(lf[54]); mix[n] = (lf[29] + S(lf[61])) - T(lf[64]);
  100. n = R(lf[56]); mix[n] = (lf[59] ^ S(lf[45])) - T(lf[41]);
  101. n = R(lf[57]); mix[n] = (lf[36] - S(lf[32])) + T(lf[37]);
  102. n = R(lf[58]); mix[n] = (lf[40] + S(lf[60])) + T(lf[14]);
  103. n = R(lf[60]); mix[n] = (lf[1] + S(lf[56])) ^ T(lf[36]);
  104. n = R(lf[61]); mix[n] = (lf[8] ^ S(lf[5])) ^ T(lf[17]);
  105. n = R(lf[62]); mix[n] = (lf[31] ^ S(lf[17])) ^ T(lf[52]);
  106. /* The test this way around favours Intel etc byte order */
  107. if (((unsigned32 *)byte_order_test)[0] != 1)
  108. { int i;
  109. for (i=0; i<32; i++)
  110. { unsigned32 w = b[i];
  111. unsigned32 b0, b1, b2, b3;
  112. b0 = (w >> 24) & 0xffU;
  113. b1 = (w >> 8) & 0xff00U;
  114. b2 = (w << 8) & 0xff0000U;
  115. b3 = (w << 24) & 0xff000000U;
  116. b[i] = b0 | b1 | b2 | b3;
  117. }
  118. }
  119. return;
  120. }
  121. void crypt_init(char *key)
  122. {
  123. char *pk = key;
  124. unsigned char junk[128];
  125. int i, j;
  126. unsigned32 w = 0;
  127. for (i=0; i<260; i++)
  128. { int k = *pk++;
  129. if (k == 0) pk = key; /* Cycle key (inc. terminating 0) */
  130. w = (w << 8) | (k & 0xff);
  131. if ((i % 4) == 3) lf[i/4] = w;
  132. }
  133. for (i=0; i<4096; i++) mix[i] = 0;
  134. for (i=0; i<8; i++)
  135. { for (j=0; j<65; j++)
  136. lf[j] = (lf[j] << 10) | (lf[j] >> 22);
  137. lf[0] |= 1;
  138. for (j=0; j<64; j++)
  139. crypt_get_block(junk);
  140. }
  141. for (i=0; i<4096;)
  142. { int j;
  143. crypt_get_block(junk);
  144. for (j=0; j<32; j++)
  145. { unsigned32 r = junk[4*j];
  146. r = (r << 8) | junk[4*j+1];
  147. r = (r << 8) | junk[4*j+2];
  148. r = (r << 8) | junk[4*j+3];
  149. if (r == 0) continue;
  150. mix[i++] ^= junk[j];
  151. if (i == 4096) break;
  152. }
  153. }
  154. for (i=0; i<192; i++)
  155. crypt_get_block(junk);
  156. return;
  157. }
  158. #ifdef TIME_TEST
  159. /*
  160. * The main program here does not do anything of real interest. It
  161. * runs both the key-setup and the main loop lots of times and reports
  162. * how long it all takes.
  163. *
  164. * Here is some sample output from a Pentium-II 400Mhz system
  165. *
  166. * 1.23 milliseconds to startup
  167. * rate = 106.43 megabytes per second
  168. * 9a cb fe 7f 5b 10 0b ce f6 49 89 b2 b7 17 f1 c7
  169. * 29 39 70 f0 ff 4b ec 8a 98 af 41 38 52 85 c9 88
  170. * 91 c7 18 46 68 3c 92 04 b2 21 ed 5e 30 6e e0 d9
  171. * cd ba d9 a6 86 9a 65 35 5c 65 61 e6 45 00 ac 88
  172. * 41 8a 5e 76 cb 2c 0c fb 62 3b 1b 31 37 cf 1f 97
  173. * 81 6c 53 73 3a fe 4f df b6 a4 00 45 59 ab 58 48
  174. * ce e7 08 81 67 17 07 f3 d3 88 34 4b 8a ec 8c 43
  175. * e9 4e 65 f5 f2 21 1e a9 44 6d a1 ad ac d5 f9 ac
  176. *
  177. */
  178. int main(int argc, char *argv[])
  179. {
  180. clock_t c0, c1;
  181. unsigned char r[CRYPT_BLOCK_SIZE];
  182. int i, j = 0;
  183. double rate;
  184. c0 = clock();
  185. for (i=0; i<(NTINY+1); i++) j ^= i;
  186. c1 = clock();
  187. printf("[%.8x] %.2f nanoseconds to do tiny loop\n", j,
  188. 1.0e9*(double)(c1-c0)/((double)CLOCKS_PER_SEC*(double)(NTINY+1)));
  189. c0 = clock();
  190. for (i=0; i<NSTARTS; i++) crypt_init(KEY);
  191. c1 = clock();
  192. printf("%.2f milliseconds to startup\n",
  193. 1000.0*(double)(c1-c0)/((double)CLOCKS_PER_SEC*(double)NSTARTS));
  194. c0 = clock();
  195. for (i=0; i<N; i++) crypt_get_block(r);
  196. c1 = clock();
  197. rate = (double)N*(double)CRYPT_BLOCK_SIZE*(double)CLOCKS_PER_SEC/
  198. ((double)(c1-c0)*1.0e6);
  199. printf("rate = %.2f megabytes per second\n", rate);
  200. for (i=0; i<128; i++)
  201. { printf("%.2x ", r[i]);
  202. if ((i % 16) == 15) printf("\n");
  203. }
  204. return 0;
  205. }
  206. #endif /* TIME_TEST */
  207. #undef R
  208. #undef S
  209. #undef T