sober128.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. #include "mycrypt.h"
  12. /* Implementation of SOBER-128 by Tom St Denis.
  13. * Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM.
  14. */
  15. #ifdef SOBER128
  16. #include "sober128tab.c"
  17. const struct _prng_descriptor sober128_desc =
  18. {
  19. "sober128", 64,
  20. &sober128_start,
  21. &sober128_add_entropy,
  22. &sober128_ready,
  23. &sober128_read,
  24. &sober128_done,
  25. &sober128_export,
  26. &sober128_import,
  27. &sober128_test
  28. };
  29. /* don't change these... */
  30. #define N 17
  31. #define FOLD N /* how many iterations of folding to do */
  32. #define INITKONST 0x6996c53a /* value of KONST to use during key loading */
  33. #define KEYP 15 /* where to insert key words */
  34. #define FOLDP 4 /* where to insert non-linear feedback */
  35. #define B(x,i) ((unsigned char)(((x) >> (8*i)) & 0xFF))
  36. static ulong32 BYTE2WORD(unsigned char *b)
  37. {
  38. ulong32 t;
  39. LOAD32L(t, b);
  40. return t;
  41. }
  42. #define WORD2BYTE(w, b) STORE32L(b, w)
  43. static void XORWORD(ulong32 w, unsigned char *b)
  44. {
  45. ulong32 t;
  46. LOAD32L(t, b);
  47. t ^= w;
  48. STORE32L(t, b);
  49. }
  50. /* give correct offset for the current position of the register,
  51. * where logically R[0] is at position "zero".
  52. */
  53. #define OFF(zero, i) (((zero)+(i)) % N)
  54. /* step the LFSR */
  55. /* After stepping, "zero" moves right one place */
  56. #define STEP(R,z) \
  57. R[OFF(z,0)] = R[OFF(z,15)] ^ R[OFF(z,4)] ^ (R[OFF(z,0)] << 8) ^ Multab[(R[OFF(z,0)] >> 24) & 0xFF];
  58. static void cycle(ulong32 *R)
  59. {
  60. ulong32 t;
  61. int i;
  62. STEP(R,0);
  63. t = R[0];
  64. for (i = 1; i < N; ++i) {
  65. R[i-1] = R[i];
  66. }
  67. R[N-1] = t;
  68. }
  69. /* Return a non-linear function of some parts of the register.
  70. */
  71. #define NLFUNC(c,z) \
  72. { \
  73. t = c->R[OFF(z,0)] + c->R[OFF(z,16)]; \
  74. t ^= Sbox[(t >> 24) & 0xFF]; \
  75. t = ROR(t, 8); \
  76. t = ((t + c->R[OFF(z,1)]) ^ c->konst) + c->R[OFF(z,6)]; \
  77. t ^= Sbox[(t >> 24) & 0xFF]; \
  78. t = t + c->R[OFF(z,13)]; \
  79. }
  80. static ulong32 nltap(struct sober128_prng *c)
  81. {
  82. ulong32 t;
  83. NLFUNC(c, 0);
  84. return t;
  85. }
  86. /* initialise to known state
  87. */
  88. int sober128_start(prng_state *prng)
  89. {
  90. int i;
  91. struct sober128_prng *c;
  92. _ARGCHK(prng != NULL);
  93. c = &(prng->sober128);
  94. /* Register initialised to Fibonacci numbers */
  95. c->R[0] = 1;
  96. c->R[1] = 1;
  97. for (i = 2; i < N; ++i) {
  98. c->R[i] = c->R[i-1] + c->R[i-2];
  99. }
  100. c->konst = INITKONST;
  101. /* next add_entropy will be the key */
  102. c->flag = 1;
  103. c->set = 0;
  104. return CRYPT_OK;
  105. }
  106. /* Save the current register state
  107. */
  108. static void s128_savestate(struct sober128_prng *c)
  109. {
  110. int i;
  111. for (i = 0; i < N; ++i) {
  112. c->initR[i] = c->R[i];
  113. }
  114. }
  115. /* initialise to previously saved register state
  116. */
  117. static void s128_reloadstate(struct sober128_prng *c)
  118. {
  119. int i;
  120. for (i = 0; i < N; ++i) {
  121. c->R[i] = c->initR[i];
  122. }
  123. }
  124. /* Initialise "konst"
  125. */
  126. static void s128_genkonst(struct sober128_prng *c)
  127. {
  128. ulong32 newkonst;
  129. do {
  130. cycle(c->R);
  131. newkonst = nltap(c);
  132. } while ((newkonst & 0xFF000000) == 0);
  133. c->konst = newkonst;
  134. }
  135. /* Load key material into the register
  136. */
  137. #define ADDKEY(k) \
  138. c->R[KEYP] += (k);
  139. #define XORNL(nl) \
  140. c->R[FOLDP] ^= (nl);
  141. /* nonlinear diffusion of register for key */
  142. #define DROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); c->R[OFF((z+1),FOLDP)] ^= t;
  143. static void s128_diffuse(struct sober128_prng *c)
  144. {
  145. ulong32 t;
  146. /* relies on FOLD == N == 17! */
  147. DROUND(0);
  148. DROUND(1);
  149. DROUND(2);
  150. DROUND(3);
  151. DROUND(4);
  152. DROUND(5);
  153. DROUND(6);
  154. DROUND(7);
  155. DROUND(8);
  156. DROUND(9);
  157. DROUND(10);
  158. DROUND(11);
  159. DROUND(12);
  160. DROUND(13);
  161. DROUND(14);
  162. DROUND(15);
  163. DROUND(16);
  164. }
  165. int sober128_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
  166. {
  167. struct sober128_prng *c;
  168. ulong32 i, k;
  169. _ARGCHK(buf != NULL);
  170. _ARGCHK(prng != NULL);
  171. c = &(prng->sober128);
  172. if (c->flag == 1) {
  173. /* this is the first call to the add_entropy so this input is the key */
  174. /* len must be multiple of 4 bytes */
  175. if ((len & 3) != 0) {
  176. return CRYPT_INVALID_KEYSIZE;
  177. }
  178. for (i = 0; i < len; i += 4) {
  179. k = BYTE2WORD((unsigned char *)&buf[i]);
  180. ADDKEY(k);
  181. cycle(c->R);
  182. XORNL(nltap(c));
  183. }
  184. /* also fold in the length of the key */
  185. ADDKEY(len);
  186. /* now diffuse */
  187. s128_diffuse(c);
  188. s128_genkonst(c);
  189. s128_savestate(c);
  190. c->nbuf = 0;
  191. c->flag = 0;
  192. c->set = 1;
  193. } else {
  194. /* ok we are adding an IV then... */
  195. s128_reloadstate(c);
  196. /* len must be multiple of 4 bytes */
  197. if ((len & 3) != 0) {
  198. return CRYPT_INVALID_KEYSIZE;
  199. }
  200. for (i = 0; i < len; i += 4) {
  201. k = BYTE2WORD((unsigned char *)&buf[i]);
  202. ADDKEY(k);
  203. cycle(c->R);
  204. XORNL(nltap(c));
  205. }
  206. /* also fold in the length of the key */
  207. ADDKEY(len);
  208. /* now diffuse */
  209. s128_diffuse(c);
  210. c->nbuf = 0;
  211. }
  212. return CRYPT_OK;
  213. }
  214. int sober128_ready(prng_state *prng)
  215. {
  216. return prng->sober128.set == 1 ? CRYPT_OK : CRYPT_ERROR;
  217. }
  218. /* XOR pseudo-random bytes into buffer
  219. */
  220. #define SROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); XORWORD(t, buf+(z*4));
  221. unsigned long sober128_read(unsigned char *buf, unsigned long nbytes, prng_state *prng)
  222. {
  223. struct sober128_prng *c;
  224. ulong32 t, tlen;
  225. _ARGCHK(buf != NULL);
  226. _ARGCHK(prng != NULL);
  227. c = &(prng->sober128);
  228. t = 0;
  229. tlen = nbytes;
  230. /* handle any previously buffered bytes */
  231. while (c->nbuf != 0 && nbytes != 0) {
  232. *buf++ ^= c->sbuf & 0xFF;
  233. c->sbuf >>= 8;
  234. c->nbuf -= 8;
  235. --nbytes;
  236. }
  237. #ifndef SMALL_CODE
  238. /* do lots at a time, if there's enough to do */
  239. while (nbytes >= N*4) {
  240. SROUND(0);
  241. SROUND(1);
  242. SROUND(2);
  243. SROUND(3);
  244. SROUND(4);
  245. SROUND(5);
  246. SROUND(6);
  247. SROUND(7);
  248. SROUND(8);
  249. SROUND(9);
  250. SROUND(10);
  251. SROUND(11);
  252. SROUND(12);
  253. SROUND(13);
  254. SROUND(14);
  255. SROUND(15);
  256. SROUND(16);
  257. buf += 4*N;
  258. nbytes -= 4*N;
  259. }
  260. #endif
  261. /* do small or odd size buffers the slow way */
  262. while (4 <= nbytes) {
  263. cycle(c->R);
  264. t = nltap(c);
  265. XORWORD(t, buf);
  266. buf += 4;
  267. nbytes -= 4;
  268. }
  269. /* handle any trailing bytes */
  270. if (nbytes != 0) {
  271. cycle(c->R);
  272. c->sbuf = nltap(c);
  273. c->nbuf = 32;
  274. while (c->nbuf != 0 && nbytes != 0) {
  275. *buf++ ^= c->sbuf & 0xFF;
  276. c->sbuf >>= 8;
  277. c->nbuf -= 8;
  278. --nbytes;
  279. }
  280. }
  281. return tlen;
  282. }
  283. int sober128_done(prng_state *prng)
  284. {
  285. _ARGCHK(prng != NULL);
  286. return CRYPT_OK;
  287. }
  288. int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
  289. {
  290. _ARGCHK(outlen != NULL);
  291. _ARGCHK(out != NULL);
  292. _ARGCHK(prng != NULL);
  293. if (*outlen < 64) {
  294. return CRYPT_BUFFER_OVERFLOW;
  295. }
  296. if (sober128_read(out, 64, prng) != 64) {
  297. return CRYPT_ERROR_READPRNG;
  298. }
  299. *outlen = 64;
  300. return CRYPT_OK;
  301. }
  302. int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
  303. {
  304. int err;
  305. _ARGCHK(in != NULL);
  306. _ARGCHK(prng != NULL);
  307. if (inlen != 64) {
  308. return CRYPT_INVALID_ARG;
  309. }
  310. if ((err = sober128_start(prng)) != CRYPT_OK) {
  311. return err;
  312. }
  313. if ((err = sober128_add_entropy(in, 64, prng)) != CRYPT_OK) {
  314. return err;
  315. }
  316. return sober128_ready(prng);
  317. }
  318. int sober128_test(void)
  319. {
  320. #ifndef LTC_TEST
  321. return CRYPT_NOP;
  322. #else
  323. static const struct {
  324. int keylen, ivlen, len;
  325. unsigned char key[16], iv[4], out[20];
  326. } tests[] = {
  327. {
  328. 16, 4, 20,
  329. /* key */
  330. { 't', 'e', 's', 't', ' ', 'k', 'e', 'y',
  331. ' ', '1', '2', '8', 'b', 'i', 't', 's' },
  332. /* IV */
  333. { 0x00, 0x00, 0x00, 0x0 },
  334. /* expected output */
  335. { 0x43, 0x50, 0x0c, 0xcf, 0x89, 0x91, 0x9f, 0x1d,
  336. 0xaa, 0x37, 0x74, 0x95, 0xf4, 0xb4, 0x58, 0xc2,
  337. 0x40, 0x37, 0x8b, 0xbb }
  338. }
  339. };
  340. prng_state prng;
  341. unsigned char dst[20];
  342. int err, x;
  343. for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
  344. if ((err = sober128_start(&prng)) != CRYPT_OK) {
  345. return err;
  346. }
  347. if ((err = sober128_add_entropy(tests[x].key, tests[x].keylen, &prng)) != CRYPT_OK) {
  348. return err;
  349. }
  350. /* add IV */
  351. if ((err = sober128_add_entropy(tests[x].iv, tests[x].ivlen, &prng)) != CRYPT_OK) {
  352. return err;
  353. }
  354. /* ready up */
  355. if ((err = sober128_ready(&prng)) != CRYPT_OK) {
  356. return err;
  357. }
  358. memset(dst, 0, tests[x].len);
  359. if (sober128_read(dst, tests[x].len, &prng) != (unsigned long)tests[x].len) {
  360. return CRYPT_ERROR_READPRNG;
  361. }
  362. sober128_done(&prng);
  363. if (memcmp(dst, tests[x].out, tests[x].len)) {
  364. #if 0
  365. printf("\n\nSOBER128 failed, I got:\n");
  366. for (y = 0; y < tests[x].len; y++) printf("%02x ", dst[y]);
  367. printf("\n");
  368. #endif
  369. return CRYPT_FAIL_TESTVECTOR;
  370. }
  371. }
  372. return CRYPT_OK;
  373. #endif
  374. }
  375. #endif