cry1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * cry1.c Copyright Codemist Ltd. 2002
  3. *
  4. *
  5. * This is an experimental and not properly validated cipher. The idea
  6. * is that of a feedback shift register. It observes that in any single bit
  7. * position and for so long as one ignored carries, ADD, SUBTRACT and XOR
  8. * all behave the same. So it runs a fibonacci-style generator but with the
  9. * processing written out in-line so it can use these three operations
  10. * in turn. The pattern of their use was derived from bits of the value
  11. * of pi or some such and is "random". Next it make output words by combining
  12. * (again using mixtures of arithmetic and logical operators) fields from
  13. * within the raw shift register. It discards some values or bits from the
  14. * main shift register. It then shuffles the resulting output words in
  15. * 4K word buffer so it will be harder to work out which came from where.
  16. * This HAS NOT been subject to serious crypto-analysis so should at most
  17. * be used in places where confusion is enough...
  18. */
  19. /*
  20. * This code may be used and modified, and redistributed in binary
  21. * or source form, subject to the "CCL Public License", which should
  22. * accompany it. This license is a variant on the BSD license, and thus
  23. * permits use of code derived from this in either open and commercial
  24. * projects: but it does require that updates to this code be made
  25. * available back to the originators of the package.
  26. * Before merging other code in with this or linking this code
  27. * with other packages or libraries please check that the license terms
  28. * of the other material are compatible with those of this.
  29. */
  30. /*
  31. * shift register length = 65
  32. * tap at position 18
  33. * shuffle-buffer size = 4096
  34. */
  35. /* Signature: 71cbef8a 21-Apr-2002 */
  36. #ifdef TIME_TEST
  37. #include <stdio.h>
  38. #include <time.h>
  39. #define N 10000000 /* parameters for time test */
  40. #define NSTARTS 4000
  41. #define NTINY 50000000
  42. #define KEY "Arthurs's sample key"
  43. typedef unsigned int unsigned32;
  44. #endif /* TIME_TEST */
  45. static unsigned32 lf[65], mix[4096];
  46. #define R(x) ((x) >> 20)
  47. #define S(x) ((x) >> 9)
  48. #define T(x) ((x) << 13)
  49. static unsigned char byte_order_test[] = {1, 0, 0, 0};
  50. #define CRYPT_BLOCK_SIZE 128
  51. void crypt_get_block(unsigned char block[CRYPT_BLOCK_SIZE])
  52. {
  53. unsigned32 *b = (unsigned32 *)block;
  54. int n;
  55. lf[0] -= lf[18]; lf[1] ^= lf[19];
  56. lf[2] -= lf[20]; lf[3] += lf[21];
  57. lf[4] += lf[22]; lf[5] -= lf[23];
  58. lf[6] ^= lf[24]; lf[7] -= lf[25];
  59. lf[8] += lf[26]; lf[9] ^= lf[27];
  60. lf[10] -= lf[28]; lf[11] -= lf[29];
  61. lf[12] += lf[30]; lf[13] += lf[31];
  62. lf[14] -= lf[32]; lf[15] ^= lf[33];
  63. lf[16] -= lf[34]; lf[17] += lf[35];
  64. lf[18] += lf[36]; lf[19] += lf[37];
  65. lf[20] -= lf[38]; lf[21] -= lf[39];
  66. lf[22] ^= lf[40]; lf[23] += lf[41];
  67. lf[24] -= lf[42]; lf[25] -= lf[43];
  68. lf[26] += lf[44]; lf[27] += lf[45];
  69. lf[28] -= lf[46]; lf[29] ^= lf[47];
  70. lf[30] -= lf[48]; lf[31] += lf[49];
  71. lf[32] -= lf[50]; lf[33] ^= lf[51];
  72. lf[34] -= lf[52]; lf[35] ^= lf[53];
  73. lf[36] += lf[54]; lf[37] += lf[55];
  74. lf[38] ^= lf[56]; lf[39] ^= lf[57];
  75. lf[40] += lf[58]; lf[41] -= lf[59];
  76. lf[42] ^= lf[60]; lf[43] += lf[61];
  77. lf[44] += lf[62]; lf[45] ^= lf[63];
  78. lf[46] ^= lf[64]; lf[47] -= lf[0];
  79. lf[48] ^= lf[1]; lf[49] ^= lf[2];
  80. lf[50] ^= lf[3]; lf[51] ^= lf[4];
  81. lf[52] ^= lf[5]; lf[53] ^= lf[6];
  82. lf[54] += lf[7]; lf[55] -= lf[8];
  83. lf[56] -= lf[9]; lf[57] ^= lf[10];
  84. lf[58] -= lf[11]; lf[59] -= lf[12];
  85. lf[60] ^= lf[13]; lf[61] += lf[14];
  86. lf[62] ^= lf[15]; lf[63] -= lf[16];
  87. lf[64] -= lf[17];
  88. n = R(lf[0]); b[0] = mix[n]; mix[n] = (lf[54] + S(lf[29])) ^ T(lf[5]);
  89. n = R(lf[1]); b[1] = mix[n]; mix[n] = (lf[39] + S(lf[47])) + T(lf[15]);
  90. n = R(lf[2]); b[2] = mix[n]; mix[n] = (lf[25] + S(lf[14])) + T(lf[38]);
  91. n = R(lf[4]); b[3] = mix[n]; mix[n] = (lf[48] - S(lf[40])) ^ T(lf[10]);
  92. n = R(lf[5]); b[4] = mix[n]; mix[n] = (lf[44] - S(lf[55])) - T(lf[49]);
  93. n = R(lf[6]); b[5] = mix[n]; mix[n] = (lf[9] ^ S(lf[37])) + T(lf[50]);
  94. n = R(lf[8]); b[6] = mix[n]; mix[n] = (lf[64] ^ S(lf[51])) + T(lf[8]);
  95. n = R(lf[9]); b[7] = mix[n]; mix[n] = (lf[11] - S(lf[35])) - T(lf[21]);
  96. n = R(lf[10]); b[8] = mix[n]; mix[n] = (lf[20] ^ S(lf[21])) ^ T(lf[3]);
  97. n = R(lf[12]); b[9] = mix[n]; mix[n] = (lf[6] ^ S(lf[31])) - T(lf[61]);
  98. n = R(lf[13]); b[10] = mix[n]; mix[n] = (lf[3] - S(lf[16])) ^ T(lf[16]);
  99. n = R(lf[14]); b[11] = mix[n]; mix[n] = (lf[17] - S(lf[53])) - T(lf[2]);
  100. n = R(lf[16]); b[12] = mix[n]; mix[n] = (lf[27] + S(lf[42])) - T(lf[33]);
  101. n = R(lf[17]); b[13] = mix[n]; mix[n] = (lf[28] + S(lf[63])) - T(lf[46]);
  102. n = R(lf[18]); b[14] = mix[n]; mix[n] = (lf[10] - S(lf[46])) + T(lf[35]);
  103. n = R(lf[20]); b[15] = mix[n]; mix[n] = (lf[53] - S(lf[10])) - T(lf[27]);
  104. n = R(lf[21]); b[16] = mix[n]; mix[n] = (lf[4] + S(lf[18])) - T(lf[7]);
  105. n = R(lf[22]); b[17] = mix[n]; mix[n] = (lf[43] + S(lf[64])) ^ T(lf[45]);
  106. n = R(lf[24]); b[18] = mix[n]; mix[n] = (lf[14] + S(lf[26])) + T(lf[44]);
  107. n = R(lf[25]); b[19] = mix[n]; mix[n] = (lf[23] ^ S(lf[38])) + T(lf[58]);
  108. n = R(lf[26]); b[20] = mix[n]; mix[n] = (lf[47] + S(lf[59])) ^ T(lf[47]);
  109. n = R(lf[28]); b[21] = mix[n]; mix[n] = (lf[63] - S(lf[36])) - T(lf[57]);
  110. n = R(lf[29]); b[22] = mix[n]; mix[n] = (lf[56] + S(lf[4])) + T(lf[19]);
  111. n = R(lf[30]); b[23] = mix[n]; mix[n] = (lf[42] - S(lf[52])) - T(lf[56]);
  112. n = R(lf[32]); b[24] = mix[n]; mix[n] = (lf[37] + S(lf[3])) - T(lf[63]);
  113. n = R(lf[33]); b[25] = mix[n]; mix[n] = (lf[32] + S(lf[1])) - T(lf[12]);
  114. n = R(lf[34]); b[26] = mix[n]; mix[n] = (lf[62] - S(lf[39])) - T(lf[31]);
  115. n = R(lf[36]); b[27] = mix[n]; mix[n] = (lf[2] ^ S(lf[44])) ^ T(lf[18]);
  116. n = R(lf[37]); b[28] = mix[n]; mix[n] = (lf[24] ^ S(lf[50])) ^ T(lf[55]);
  117. n = R(lf[38]); b[29] = mix[n]; mix[n] = (lf[22] + S(lf[27])) - T(lf[32]);
  118. n = R(lf[40]); b[30] = mix[n]; mix[n] = (lf[51] + S(lf[33])) + T(lf[0]);
  119. n = R(lf[41]); b[31] = mix[n]; mix[n] = (lf[52] ^ S(lf[19])) - T(lf[26]);
  120. n = R(lf[42]); mix[n] = (lf[5] ^ S(lf[41])) + T(lf[28]);
  121. n = R(lf[44]); mix[n] = (lf[30] ^ S(lf[15])) - T(lf[30]);
  122. n = R(lf[45]); mix[n] = (lf[45] + S(lf[24])) ^ T(lf[51]);
  123. n = R(lf[46]); mix[n] = (lf[13] + S(lf[49])) - T(lf[11]);
  124. n = R(lf[48]); mix[n] = (lf[16] + S(lf[11])) - T(lf[39]);
  125. n = R(lf[49]); mix[n] = (lf[57] - S(lf[43])) - T(lf[60]);
  126. n = R(lf[50]); mix[n] = (lf[49] + S(lf[48])) ^ T(lf[25]);
  127. n = R(lf[52]); mix[n] = (lf[34] - S(lf[22])) ^ T(lf[23]);
  128. n = R(lf[53]); mix[n] = (lf[18] + S(lf[6])) + T(lf[1]);
  129. n = R(lf[54]); mix[n] = (lf[29] + S(lf[61])) - T(lf[64]);
  130. n = R(lf[56]); mix[n] = (lf[59] ^ S(lf[45])) - T(lf[41]);
  131. n = R(lf[57]); mix[n] = (lf[36] - S(lf[32])) + T(lf[37]);
  132. n = R(lf[58]); mix[n] = (lf[40] + S(lf[60])) + T(lf[14]);
  133. n = R(lf[60]); mix[n] = (lf[1] + S(lf[56])) ^ T(lf[36]);
  134. n = R(lf[61]); mix[n] = (lf[8] ^ S(lf[5])) ^ T(lf[17]);
  135. n = R(lf[62]); mix[n] = (lf[31] ^ S(lf[17])) ^ T(lf[52]);
  136. /* The test this way around favours Intel etc byte order */
  137. if (((unsigned32 *)byte_order_test)[0] != 1)
  138. { int i;
  139. for (i=0; i<32; i++)
  140. { unsigned32 w = b[i];
  141. unsigned32 b0, b1, b2, b3;
  142. b0 = (w >> 24) & 0xffU;
  143. b1 = (w >> 8) & 0xff00U;
  144. b2 = (w << 8) & 0xff0000U;
  145. b3 = (w << 24) & 0xff000000U;
  146. b[i] = b0 | b1 | b2 | b3;
  147. }
  148. }
  149. return;
  150. }
  151. void crypt_init(char *key)
  152. {
  153. char *pk = key;
  154. unsigned char junk[128];
  155. int i, j;
  156. unsigned32 w = 0;
  157. for (i=0; i<260; i++)
  158. { int k = *pk++;
  159. if (k == 0) pk = key; /* Cycle key (inc. terminating 0) */
  160. w = (w << 8) | (k & 0xff);
  161. if ((i % 4) == 3) lf[i/4] = w;
  162. }
  163. for (i=0; i<4096; i++) mix[i] = 0;
  164. for (i=0; i<8; i++)
  165. { for (j=0; j<65; j++)
  166. lf[j] = (lf[j] << 10) | (lf[j] >> 22);
  167. lf[0] |= 1;
  168. for (j=0; j<64; j++)
  169. crypt_get_block(junk);
  170. }
  171. for (i=0; i<4096;)
  172. { int j;
  173. crypt_get_block(junk);
  174. for (j=0; j<32; j++)
  175. { unsigned32 r = junk[4*j];
  176. r = (r << 8) | junk[4*j+1];
  177. r = (r << 8) | junk[4*j+2];
  178. r = (r << 8) | junk[4*j+3];
  179. if (r == 0) continue;
  180. mix[i++] ^= junk[j];
  181. if (i == 4096) break;
  182. }
  183. }
  184. for (i=0; i<192; i++)
  185. crypt_get_block(junk);
  186. return;
  187. }
  188. #ifdef TIME_TEST
  189. /*
  190. * The main program here does not do anything of real interest. It
  191. * runs both the key-setup and the main loop lots of times and reports
  192. * how long it all takes.
  193. *
  194. * Here is some sample output from a Pentium-II 400Mhz system
  195. *
  196. * 1.23 milliseconds to startup
  197. * rate = 106.43 megabytes per second
  198. * 9a cb fe 7f 5b 10 0b ce f6 49 89 b2 b7 17 f1 c7
  199. * 29 39 70 f0 ff 4b ec 8a 98 af 41 38 52 85 c9 88
  200. * 91 c7 18 46 68 3c 92 04 b2 21 ed 5e 30 6e e0 d9
  201. * cd ba d9 a6 86 9a 65 35 5c 65 61 e6 45 00 ac 88
  202. * 41 8a 5e 76 cb 2c 0c fb 62 3b 1b 31 37 cf 1f 97
  203. * 81 6c 53 73 3a fe 4f df b6 a4 00 45 59 ab 58 48
  204. * ce e7 08 81 67 17 07 f3 d3 88 34 4b 8a ec 8c 43
  205. * e9 4e 65 f5 f2 21 1e a9 44 6d a1 ad ac d5 f9 ac
  206. *
  207. */
  208. int main(int argc, char *argv[])
  209. {
  210. clock_t c0, c1;
  211. unsigned char r[CRYPT_BLOCK_SIZE];
  212. int i, j = 0;
  213. double rate;
  214. c0 = clock();
  215. for (i=0; i<(NTINY+1); i++) j ^= i;
  216. c1 = clock();
  217. printf("[%.8x] %.2f nanoseconds to do tiny loop\n", j,
  218. 1.0e9*(double)(c1-c0)/((double)CLOCKS_PER_SEC*(double)(NTINY+1)));
  219. c0 = clock();
  220. for (i=0; i<NSTARTS; i++) crypt_init(KEY);
  221. c1 = clock();
  222. printf("%.2f milliseconds to startup\n",
  223. 1000.0*(double)(c1-c0)/((double)CLOCKS_PER_SEC*(double)NSTARTS));
  224. c0 = clock();
  225. for (i=0; i<N; i++) crypt_get_block(r);
  226. c1 = clock();
  227. rate = (double)N*(double)CRYPT_BLOCK_SIZE*(double)CLOCKS_PER_SEC/
  228. ((double)(c1-c0)*1.0e6);
  229. printf("rate = %.2f megabytes per second\n", rate);
  230. for (i=0; i<128; i++)
  231. { printf("%.2x ", r[i]);
  232. if ((i % 16) == 15) printf("\n");
  233. }
  234. return 0;
  235. }
  236. #endif /* TIME_TEST */
  237. #undef R
  238. #undef S
  239. #undef T