4
0

crypto.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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, tomstdenis@iahu.ca, http://libtomcrypt.org
  10. */
  11. #include <assert.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/poll.h>
  18. #if defined(OPENAIS_BSD)
  19. #include <sys/endian.h>
  20. #endif
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include "crypto.h"
  24. #define CONST64(n) n ## ULL
  25. typedef unsigned long ulong32;
  26. typedef unsigned long long ulong64;
  27. #if __BYTE_ORDER == __LITTLE_ENDIAN
  28. #define ENDIAN_LITTLE
  29. #elif __BYTE_ORDER == __BIG_ENDIAN
  30. #define ENDIAN_BIG
  31. #elif _BYTE_ORDER == _LITTLE_ENDIAN
  32. #define ENDIAN_LITTLE
  33. #elif _BYTE_ORDER == _BIG_ENDIAN
  34. #define ENDIAN_BIG
  35. #elif
  36. #warning "cannot detect byte order"
  37. #endif
  38. #if defined(OPENAIS_LINUX)
  39. #if __WORDIZE == 64
  40. #define ENDIAN_64BITWORD
  41. #endif
  42. #if __WORDIZE == 32
  43. #define ENDIAN_32BITWORD
  44. #endif
  45. #else
  46. /* XXX need to find a better default
  47. */
  48. #define ENDIAN_32BITWORD
  49. #endif
  50. /* ---- HELPER MACROS ---- */
  51. #ifdef ENDIAN_NEUTRAL
  52. #define STORE32L(x, y) \
  53. { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  54. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  55. #define LOAD32L(x, y) \
  56. { x = ((unsigned long)((y)[3] & 255)<<24) | \
  57. ((unsigned long)((y)[2] & 255)<<16) | \
  58. ((unsigned long)((y)[1] & 255)<<8) | \
  59. ((unsigned long)((y)[0] & 255)); }
  60. #define STORE64L(x, y) \
  61. { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
  62. (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
  63. (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  64. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  65. #define LOAD64L(x, y) \
  66. { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
  67. (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
  68. (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
  69. (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
  70. #define STORE32H(x, y) \
  71. { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
  72. (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
  73. #define LOAD32H(x, y) \
  74. { x = ((unsigned long)((y)[0] & 255)<<24) | \
  75. ((unsigned long)((y)[1] & 255)<<16) | \
  76. ((unsigned long)((y)[2] & 255)<<8) | \
  77. ((unsigned long)((y)[3] & 255)); }
  78. #define STORE64H(x, y) \
  79. { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
  80. (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
  81. (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
  82. (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
  83. #define LOAD64H(x, y) \
  84. { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
  85. (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
  86. (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
  87. (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
  88. #endif /* ENDIAN_NEUTRAL */
  89. #ifdef ENDIAN_LITTLE
  90. #define STORE32H(x, y) \
  91. { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
  92. (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
  93. #define LOAD32H(x, y) \
  94. { x = ((unsigned long)((y)[0] & 255)<<24) | \
  95. ((unsigned long)((y)[1] & 255)<<16) | \
  96. ((unsigned long)((y)[2] & 255)<<8) | \
  97. ((unsigned long)((y)[3] & 255)); }
  98. #define STORE64H(x, y) \
  99. { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
  100. (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
  101. (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
  102. (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
  103. #define LOAD64H(x, y) \
  104. { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
  105. (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
  106. (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
  107. (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
  108. #ifdef ENDIAN_32BITWORD
  109. #define STORE32L(x, y) \
  110. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  111. #define LOAD32L(x, y) \
  112. memcpy(&(x), y, 4);
  113. #define STORE64L(x, y) \
  114. { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
  115. (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
  116. (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  117. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  118. #define LOAD64L(x, y) \
  119. { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
  120. (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
  121. (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
  122. (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
  123. #else /* 64-bit words then */
  124. #define STORE32L(x, y) \
  125. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  126. #define LOAD32L(x, y) \
  127. { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
  128. #define STORE64L(x, y) \
  129. { ulong64 __t = (x); memcpy(y, &__t, 8); }
  130. #define LOAD64L(x, y) \
  131. { memcpy(&(x), y, 8); }
  132. #endif /* ENDIAN_64BITWORD */
  133. #endif /* ENDIAN_LITTLE */
  134. #ifdef ENDIAN_BIG
  135. #define STORE32L(x, y) \
  136. { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  137. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  138. #define LOAD32L(x, y) \
  139. { x = ((unsigned long)((y)[3] & 255)<<24) | \
  140. ((unsigned long)((y)[2] & 255)<<16) | \
  141. ((unsigned long)((y)[1] & 255)<<8) | \
  142. ((unsigned long)((y)[0] & 255)); }
  143. #define STORE64L(x, y) \
  144. { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
  145. (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
  146. (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  147. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  148. #define LOAD64L(x, y) \
  149. { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
  150. (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
  151. (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
  152. (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
  153. #ifdef ENDIAN_32BITWORD
  154. #define STORE32H(x, y) \
  155. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  156. #define LOAD32H(x, y) \
  157. memcpy(&(x), y, 4);
  158. #define STORE64H(x, y) \
  159. { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
  160. (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
  161. (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
  162. (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
  163. #define LOAD64H(x, y) \
  164. { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
  165. (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
  166. (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
  167. (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
  168. #else /* 64-bit words then */
  169. #define STORE32H(x, y) \
  170. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  171. #define LOAD32H(x, y) \
  172. { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
  173. #define STORE64H(x, y) \
  174. { ulong64 __t = (x); memcpy(y, &__t, 8); }
  175. #define LOAD64H(x, y) \
  176. { memcpy(&(x), y, 8); }
  177. #endif /* ENDIAN_64BITWORD */
  178. #endif /* ENDIAN_BIG */
  179. #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
  180. ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
  181. #if defined(__GNUC__) && defined(__i386__) && !defined(INTEL_CC)
  182. static inline unsigned long ROL(unsigned long word, int i)
  183. {
  184. __asm__("roll %%cl,%0"
  185. :"=r" (word)
  186. :"0" (word),"c" (i));
  187. return word;
  188. }
  189. static inline unsigned long ROR(unsigned long word, int i)
  190. {
  191. __asm__("rorl %%cl,%0"
  192. :"=r" (word)
  193. :"0" (word),"c" (i));
  194. return word;
  195. }
  196. #else
  197. /* rotates the hard way */
  198. #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
  199. #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
  200. #endif
  201. #define ROL64(x, y) \
  202. ( (((x)<<((ulong64)(y)&63)) | \
  203. (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
  204. #define ROR64(x, y) \
  205. ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
  206. ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
  207. #undef MAX
  208. #undef MIN
  209. #define MAX(x, y) ( ((x)>(y))?(x):(y) )
  210. #define MIN(x, y) ( ((x)<(y))?(x):(y) )
  211. /* extract a byte portably */
  212. #define byte(x, n) (((x) >> (8 * (n))) & 255)
  213. #define CONST64(n) n ## ULL
  214. /* a simple macro for making hash "process" functions */
  215. #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
  216. int func_name (hash_state * md, const unsigned char *buf, unsigned long len) \
  217. { \
  218. unsigned long n; \
  219. if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
  220. return CRYPT_INVALID_ARG; \
  221. } \
  222. while (len > 0) { \
  223. if (md-> state_var .curlen == 0 && len >= block_size) { \
  224. compress_name (md, (unsigned char *)buf); \
  225. md-> state_var .length += block_size * 8; \
  226. buf += block_size; \
  227. len -= block_size; \
  228. } else { \
  229. n = MIN(len, (block_size - md-> state_var .curlen)); \
  230. memcpy(md-> state_var .buf + md-> state_var.curlen, buf, (size_t)n); \
  231. md-> state_var .curlen += n; \
  232. buf += n; \
  233. len -= n; \
  234. if (md-> state_var .curlen == block_size) { \
  235. compress_name (md, md-> state_var .buf); \
  236. md-> state_var .length += 8*block_size; \
  237. md-> state_var .curlen = 0; \
  238. } \
  239. } \
  240. } \
  241. return CRYPT_OK; \
  242. }
  243. #define MAXBLOCKSIZE 128
  244. /*
  245. * The mycrypt_macros.h file
  246. */
  247. /* ---- HELPER MACROS ---- */
  248. #ifdef ENDIAN_NEUTRAL
  249. #define STORE32L(x, y) \
  250. { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  251. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  252. #define LOAD32L(x, y) \
  253. { x = ((unsigned long)((y)[3] & 255)<<24) | \
  254. ((unsigned long)((y)[2] & 255)<<16) | \
  255. ((unsigned long)((y)[1] & 255)<<8) | \
  256. ((unsigned long)((y)[0] & 255)); }
  257. #define STORE64L(x, y) \
  258. { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
  259. (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
  260. (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  261. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  262. #define LOAD64L(x, y) \
  263. { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
  264. (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
  265. (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
  266. (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
  267. #define STORE32H(x, y) \
  268. { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
  269. (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
  270. #define LOAD32H(x, y) \
  271. { x = ((unsigned long)((y)[0] & 255)<<24) | \
  272. ((unsigned long)((y)[1] & 255)<<16) | \
  273. ((unsigned long)((y)[2] & 255)<<8) | \
  274. ((unsigned long)((y)[3] & 255)); }
  275. #define STORE64H(x, y) \
  276. { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
  277. (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
  278. (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
  279. (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
  280. #define LOAD64H(x, y) \
  281. { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
  282. (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
  283. (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
  284. (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
  285. #endif /* ENDIAN_NEUTRAL */
  286. #ifdef ENDIAN_LITTLE
  287. #define STORE32H(x, y) \
  288. { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
  289. (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
  290. #define LOAD32H(x, y) \
  291. { x = ((unsigned long)((y)[0] & 255)<<24) | \
  292. ((unsigned long)((y)[1] & 255)<<16) | \
  293. ((unsigned long)((y)[2] & 255)<<8) | \
  294. ((unsigned long)((y)[3] & 255)); }
  295. #define STORE64H(x, y) \
  296. { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
  297. (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
  298. (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
  299. (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
  300. #define LOAD64H(x, y) \
  301. { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
  302. (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
  303. (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
  304. (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
  305. #ifdef ENDIAN_32BITWORD
  306. #define STORE32L(x, y) \
  307. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  308. #define LOAD32L(x, y) \
  309. memcpy(&(x), y, 4);
  310. #define STORE64L(x, y) \
  311. { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
  312. (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
  313. (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  314. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  315. #define LOAD64L(x, y) \
  316. { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
  317. (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
  318. (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
  319. (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
  320. #else /* 64-bit words then */
  321. #define STORE32L(x, y) \
  322. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  323. #define LOAD32L(x, y) \
  324. { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
  325. #define STORE64L(x, y) \
  326. { ulong64 __t = (x); memcpy(y, &__t, 8); }
  327. #define LOAD64L(x, y) \
  328. { memcpy(&(x), y, 8); }
  329. #endif /* ENDIAN_64BITWORD */
  330. #endif /* ENDIAN_LITTLE */
  331. #ifdef ENDIAN_BIG
  332. #define STORE32L(x, y) \
  333. { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  334. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  335. #define LOAD32L(x, y) \
  336. { x = ((unsigned long)((y)[3] & 255)<<24) | \
  337. ((unsigned long)((y)[2] & 255)<<16) | \
  338. ((unsigned long)((y)[1] & 255)<<8) | \
  339. ((unsigned long)((y)[0] & 255)); }
  340. #define STORE64L(x, y) \
  341. { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
  342. (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
  343. (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
  344. (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  345. #define LOAD64L(x, y) \
  346. { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
  347. (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
  348. (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
  349. (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
  350. #ifdef ENDIAN_32BITWORD
  351. #define STORE32H(x, y) \
  352. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  353. #define LOAD32H(x, y) \
  354. memcpy(&(x), y, 4);
  355. #define STORE64H(x, y) \
  356. { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
  357. (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
  358. (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
  359. (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
  360. #define LOAD64H(x, y) \
  361. { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
  362. (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
  363. (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
  364. (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
  365. #else /* 64-bit words then */
  366. #define STORE32H(x, y) \
  367. { unsigned long __t = (x); memcpy(y, &__t, 4); }
  368. #define LOAD32H(x, y) \
  369. { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
  370. #define STORE64H(x, y) \
  371. { ulong64 __t = (x); memcpy(y, &__t, 8); }
  372. #define LOAD64H(x, y) \
  373. { memcpy(&(x), y, 8); }
  374. #endif /* ENDIAN_64BITWORD */
  375. #endif /* ENDIAN_BIG */
  376. #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
  377. ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
  378. #define ROL64(x, y) \
  379. ( (((x)<<((ulong64)(y)&63)) | \
  380. (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
  381. #define ROR64(x, y) \
  382. ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
  383. ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
  384. #undef MAX
  385. #undef MIN
  386. #define MAX(x, y) ( ((x)>(y))?(x):(y) )
  387. #define MIN(x, y) ( ((x)<(y))?(x):(y) )
  388. /* extract a byte portably */
  389. #define byte(x, n) (((x) >> (8 * (n))) & 255)
  390. /* $Id: s128multab.h 213 2003-12-16 04:27:12Z ggr $ */
  391. /* @(#)TuringMultab.h 1.3 (QUALCOMM) 02/09/03 */
  392. /* Multiplication table for Turing using 0xD02B4367 */
  393. static const ulong32 Multab[256] = {
  394. 0x00000000, 0xD02B4367, 0xED5686CE, 0x3D7DC5A9,
  395. 0x97AC41D1, 0x478702B6, 0x7AFAC71F, 0xAAD18478,
  396. 0x631582EF, 0xB33EC188, 0x8E430421, 0x5E684746,
  397. 0xF4B9C33E, 0x24928059, 0x19EF45F0, 0xC9C40697,
  398. 0xC62A4993, 0x16010AF4, 0x2B7CCF5D, 0xFB578C3A,
  399. 0x51860842, 0x81AD4B25, 0xBCD08E8C, 0x6CFBCDEB,
  400. 0xA53FCB7C, 0x7514881B, 0x48694DB2, 0x98420ED5,
  401. 0x32938AAD, 0xE2B8C9CA, 0xDFC50C63, 0x0FEE4F04,
  402. 0xC154926B, 0x117FD10C, 0x2C0214A5, 0xFC2957C2,
  403. 0x56F8D3BA, 0x86D390DD, 0xBBAE5574, 0x6B851613,
  404. 0xA2411084, 0x726A53E3, 0x4F17964A, 0x9F3CD52D,
  405. 0x35ED5155, 0xE5C61232, 0xD8BBD79B, 0x089094FC,
  406. 0x077EDBF8, 0xD755989F, 0xEA285D36, 0x3A031E51,
  407. 0x90D29A29, 0x40F9D94E, 0x7D841CE7, 0xADAF5F80,
  408. 0x646B5917, 0xB4401A70, 0x893DDFD9, 0x59169CBE,
  409. 0xF3C718C6, 0x23EC5BA1, 0x1E919E08, 0xCEBADD6F,
  410. 0xCFA869D6, 0x1F832AB1, 0x22FEEF18, 0xF2D5AC7F,
  411. 0x58042807, 0x882F6B60, 0xB552AEC9, 0x6579EDAE,
  412. 0xACBDEB39, 0x7C96A85E, 0x41EB6DF7, 0x91C02E90,
  413. 0x3B11AAE8, 0xEB3AE98F, 0xD6472C26, 0x066C6F41,
  414. 0x09822045, 0xD9A96322, 0xE4D4A68B, 0x34FFE5EC,
  415. 0x9E2E6194, 0x4E0522F3, 0x7378E75A, 0xA353A43D,
  416. 0x6A97A2AA, 0xBABCE1CD, 0x87C12464, 0x57EA6703,
  417. 0xFD3BE37B, 0x2D10A01C, 0x106D65B5, 0xC04626D2,
  418. 0x0EFCFBBD, 0xDED7B8DA, 0xE3AA7D73, 0x33813E14,
  419. 0x9950BA6C, 0x497BF90B, 0x74063CA2, 0xA42D7FC5,
  420. 0x6DE97952, 0xBDC23A35, 0x80BFFF9C, 0x5094BCFB,
  421. 0xFA453883, 0x2A6E7BE4, 0x1713BE4D, 0xC738FD2A,
  422. 0xC8D6B22E, 0x18FDF149, 0x258034E0, 0xF5AB7787,
  423. 0x5F7AF3FF, 0x8F51B098, 0xB22C7531, 0x62073656,
  424. 0xABC330C1, 0x7BE873A6, 0x4695B60F, 0x96BEF568,
  425. 0x3C6F7110, 0xEC443277, 0xD139F7DE, 0x0112B4B9,
  426. 0xD31DD2E1, 0x03369186, 0x3E4B542F, 0xEE601748,
  427. 0x44B19330, 0x949AD057, 0xA9E715FE, 0x79CC5699,
  428. 0xB008500E, 0x60231369, 0x5D5ED6C0, 0x8D7595A7,
  429. 0x27A411DF, 0xF78F52B8, 0xCAF29711, 0x1AD9D476,
  430. 0x15379B72, 0xC51CD815, 0xF8611DBC, 0x284A5EDB,
  431. 0x829BDAA3, 0x52B099C4, 0x6FCD5C6D, 0xBFE61F0A,
  432. 0x7622199D, 0xA6095AFA, 0x9B749F53, 0x4B5FDC34,
  433. 0xE18E584C, 0x31A51B2B, 0x0CD8DE82, 0xDCF39DE5,
  434. 0x1249408A, 0xC26203ED, 0xFF1FC644, 0x2F348523,
  435. 0x85E5015B, 0x55CE423C, 0x68B38795, 0xB898C4F2,
  436. 0x715CC265, 0xA1778102, 0x9C0A44AB, 0x4C2107CC,
  437. 0xE6F083B4, 0x36DBC0D3, 0x0BA6057A, 0xDB8D461D,
  438. 0xD4630919, 0x04484A7E, 0x39358FD7, 0xE91ECCB0,
  439. 0x43CF48C8, 0x93E40BAF, 0xAE99CE06, 0x7EB28D61,
  440. 0xB7768BF6, 0x675DC891, 0x5A200D38, 0x8A0B4E5F,
  441. 0x20DACA27, 0xF0F18940, 0xCD8C4CE9, 0x1DA70F8E,
  442. 0x1CB5BB37, 0xCC9EF850, 0xF1E33DF9, 0x21C87E9E,
  443. 0x8B19FAE6, 0x5B32B981, 0x664F7C28, 0xB6643F4F,
  444. 0x7FA039D8, 0xAF8B7ABF, 0x92F6BF16, 0x42DDFC71,
  445. 0xE80C7809, 0x38273B6E, 0x055AFEC7, 0xD571BDA0,
  446. 0xDA9FF2A4, 0x0AB4B1C3, 0x37C9746A, 0xE7E2370D,
  447. 0x4D33B375, 0x9D18F012, 0xA06535BB, 0x704E76DC,
  448. 0xB98A704B, 0x69A1332C, 0x54DCF685, 0x84F7B5E2,
  449. 0x2E26319A, 0xFE0D72FD, 0xC370B754, 0x135BF433,
  450. 0xDDE1295C, 0x0DCA6A3B, 0x30B7AF92, 0xE09CECF5,
  451. 0x4A4D688D, 0x9A662BEA, 0xA71BEE43, 0x7730AD24,
  452. 0xBEF4ABB3, 0x6EDFE8D4, 0x53A22D7D, 0x83896E1A,
  453. 0x2958EA62, 0xF973A905, 0xC40E6CAC, 0x14252FCB,
  454. 0x1BCB60CF, 0xCBE023A8, 0xF69DE601, 0x26B6A566,
  455. 0x8C67211E, 0x5C4C6279, 0x6131A7D0, 0xB11AE4B7,
  456. 0x78DEE220, 0xA8F5A147, 0x958864EE, 0x45A32789,
  457. 0xEF72A3F1, 0x3F59E096, 0x0224253F, 0xD20F6658,
  458. };
  459. /* $Id: s128sbox.h 213 2003-12-16 04:27:12Z ggr $ */
  460. /* Sbox for SOBER-128 */
  461. /*
  462. * This is really the combination of two SBoxes; the least significant
  463. * 24 bits comes from:
  464. * 8->32 Sbox generated by Millan et. al. at Queensland University of
  465. * Technology. See: E. Dawson, W. Millan, L. Burnett, G. Carter,
  466. * "On the Design of 8*32 S-boxes". Unpublished report, by the
  467. * Information Systems Research Centre,
  468. * Queensland University of Technology, 1999.
  469. *
  470. * The most significant 8 bits are the Skipjack "F table", which can be
  471. * found at http://csrc.nist.gov/CryptoToolkit/skipjack/skipjack.pdf .
  472. * In this optimised table, though, the intent is to XOR the word from
  473. * the table selected by the high byte with the input word. Thus, the
  474. * high byte is actually the Skipjack F-table entry XORED with its
  475. * table index.
  476. */
  477. static const ulong32 Sbox[256] = {
  478. 0xa3aa1887, 0xd65e435c, 0x0b65c042, 0x800e6ef4,
  479. 0xfc57ee20, 0x4d84fed3, 0xf066c502, 0xf354e8ae,
  480. 0xbb2ee9d9, 0x281f38d4, 0x1f829b5d, 0x735cdf3c,
  481. 0x95864249, 0xbc2e3963, 0xa1f4429f, 0xf6432c35,
  482. 0xf7f40325, 0x3cc0dd70, 0x5f973ded, 0x9902dc5e,
  483. 0xda175b42, 0x590012bf, 0xdc94d78c, 0x39aab26b,
  484. 0x4ac11b9a, 0x8c168146, 0xc3ea8ec5, 0x058ac28f,
  485. 0x52ed5c0f, 0x25b4101c, 0x5a2db082, 0x370929e1,
  486. 0x2a1843de, 0xfe8299fc, 0x202fbc4b, 0x833915dd,
  487. 0x33a803fa, 0xd446b2de, 0x46233342, 0x4fcee7c3,
  488. 0x3ad607ef, 0x9e97ebab, 0x507f859b, 0xe81f2e2f,
  489. 0xc55b71da, 0xd7e2269a, 0x1339c3d1, 0x7ca56b36,
  490. 0xa6c9def2, 0xb5c9fc5f, 0x5927b3a3, 0x89a56ddf,
  491. 0xc625b510, 0x560f85a7, 0xace82e71, 0x2ecb8816,
  492. 0x44951e2a, 0x97f5f6af, 0xdfcbc2b3, 0xce4ff55d,
  493. 0xcb6b6214, 0x2b0b83e3, 0x549ea6f5, 0x9de041af,
  494. 0x792f1f17, 0xf73b99ee, 0x39a65ec0, 0x4c7016c6,
  495. 0x857709a4, 0xd6326e01, 0xc7b280d9, 0x5cfb1418,
  496. 0xa6aff227, 0xfd548203, 0x506b9d96, 0xa117a8c0,
  497. 0x9cd5bf6e, 0xdcee7888, 0x61fcfe64, 0xf7a193cd,
  498. 0x050d0184, 0xe8ae4930, 0x88014f36, 0xd6a87088,
  499. 0x6bad6c2a, 0x1422c678, 0xe9204de7, 0xb7c2e759,
  500. 0x0200248e, 0x013b446b, 0xda0d9fc2, 0x0414a895,
  501. 0x3a6cc3a1, 0x56fef170, 0x86c19155, 0xcf7b8a66,
  502. 0x551b5e69, 0xb4a8623e, 0xa2bdfa35, 0xc4f068cc,
  503. 0x573a6acd, 0x6355e936, 0x03602db9, 0x0edf13c1,
  504. 0x2d0bb16d, 0x6980b83c, 0xfeb23763, 0x3dd8a911,
  505. 0x01b6bc13, 0xf55579d7, 0xf55c2fa8, 0x19f4196e,
  506. 0xe7db5476, 0x8d64a866, 0xc06e16ad, 0xb17fc515,
  507. 0xc46feb3c, 0x8bc8a306, 0xad6799d9, 0x571a9133,
  508. 0x992466dd, 0x92eb5dcd, 0xac118f50, 0x9fafb226,
  509. 0xa1b9cef3, 0x3ab36189, 0x347a19b1, 0x62c73084,
  510. 0xc27ded5c, 0x6c8bc58f, 0x1cdde421, 0xed1e47fb,
  511. 0xcdcc715e, 0xb9c0ff99, 0x4b122f0f, 0xc4d25184,
  512. 0xaf7a5e6c, 0x5bbf18bc, 0x8dd7c6e0, 0x5fb7e420,
  513. 0x521f523f, 0x4ad9b8a2, 0xe9da1a6b, 0x97888c02,
  514. 0x19d1e354, 0x5aba7d79, 0xa2cc7753, 0x8c2d9655,
  515. 0x19829da1, 0x531590a7, 0x19c1c149, 0x3d537f1c,
  516. 0x50779b69, 0xed71f2b7, 0x463c58fa, 0x52dc4418,
  517. 0xc18c8c76, 0xc120d9f0, 0xafa80d4d, 0x3b74c473,
  518. 0xd09410e9, 0x290e4211, 0xc3c8082b, 0x8f6b334a,
  519. 0x3bf68ed2, 0xa843cc1b, 0x8d3c0ff3, 0x20e564a0,
  520. 0xf8f55a4f, 0x2b40f8e7, 0xfea7f15f, 0xcf00fe21,
  521. 0x8a6d37d6, 0xd0d506f1, 0xade00973, 0xefbbde36,
  522. 0x84670fa8, 0xfa31ab9e, 0xaedab618, 0xc01f52f5,
  523. 0x6558eb4f, 0x71b9e343, 0x4b8d77dd, 0x8cb93da6,
  524. 0x740fd52d, 0x425412f8, 0xc5a63360, 0x10e53ad0,
  525. 0x5a700f1c, 0x8324ed0b, 0xe53dc1ec, 0x1a366795,
  526. 0x6d549d15, 0xc5ce46d7, 0xe17abe76, 0x5f48e0a0,
  527. 0xd0f07c02, 0x941249b7, 0xe49ed6ba, 0x37a47f78,
  528. 0xe1cfffbd, 0xb007ca84, 0xbb65f4da, 0xb59f35da,
  529. 0x33d2aa44, 0x417452ac, 0xc0d674a7, 0x2d61a46a,
  530. 0xdc63152a, 0x3e12b7aa, 0x6e615927, 0xa14fb118,
  531. 0xa151758d, 0xba81687b, 0xe152f0b3, 0x764254ed,
  532. 0x34c77271, 0x0a31acab, 0x54f94aec, 0xb9e994cd,
  533. 0x574d9e81, 0x5b623730, 0xce8a21e8, 0x37917f0b,
  534. 0xe8a9b5d6, 0x9697adf8, 0xf3d30431, 0x5dcac921,
  535. 0x76b35d46, 0xaa430a36, 0xc2194022, 0x22bca65e,
  536. 0xdaec70ba, 0xdfaea8cc, 0x777bae8b, 0x242924d5,
  537. 0x1f098a5a, 0x4b396b81, 0x55de2522, 0x435c1cb8,
  538. 0xaeb8fe1d, 0x9db3c697, 0x5b164f83, 0xe0c16376,
  539. 0xa319224c, 0xd0203b35, 0x433ac0fe, 0x1466a19a,
  540. 0x45f0b24f, 0x51fda998, 0xc0d52d71, 0xfa0896a8,
  541. 0xf9e6053f, 0xa4b0d300, 0xd499cbcc, 0xb95e3d40,
  542. };
  543. /* Implementation of SOBER-128 by Tom St Denis.
  544. * Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM.
  545. */
  546. const struct _prng_descriptor sober128_desc =
  547. {
  548. "sober128", 64,
  549. &sober128_start,
  550. &sober128_add_entropy,
  551. &sober128_ready,
  552. &sober128_read,
  553. };
  554. const struct _prng_descriptor *prng_descriptor[] = {
  555. &sober128_desc
  556. };
  557. /* don't change these... */
  558. #define N 17
  559. #define FOLD N /* how many iterations of folding to do */
  560. #define INITKONST 0x6996c53a /* value of KONST to use during key loading */
  561. #define KEYP 15 /* where to insert key words */
  562. #define FOLDP 4 /* where to insert non-linear feedback */
  563. #define B(x,i) ((unsigned char)(((x) >> (8*i)) & 0xFF))
  564. static ulong32 BYTE2WORD(unsigned char *b)
  565. {
  566. ulong32 t;
  567. LOAD32L(t, b);
  568. return t;
  569. }
  570. #define WORD2BYTE(w, b) STORE32L(b, w)
  571. static void XORWORD(ulong32 w, unsigned char *b)
  572. {
  573. ulong32 t;
  574. LOAD32L(t, b);
  575. t ^= w;
  576. STORE32L(t, b);
  577. }
  578. /* give correct offset for the current position of the register,
  579. * where logically R[0] is at position "zero".
  580. */
  581. #define OFF(zero, i) (((zero)+(i)) % N)
  582. /* step the LFSR */
  583. /* After stepping, "zero" moves right one place */
  584. #define STEP(R,z) \
  585. R[OFF(z,0)] = R[OFF(z,15)] ^ R[OFF(z,4)] ^ (R[OFF(z,0)] << 8) ^ Multab[(R[OFF(z,0)] >> 24) & 0xFF];
  586. static void cycle(ulong32 *R)
  587. {
  588. ulong32 t;
  589. int i;
  590. STEP(R,0);
  591. t = R[0];
  592. for (i = 1; i < N; ++i) {
  593. R[i-1] = R[i];
  594. }
  595. R[N-1] = t;
  596. }
  597. /* Return a non-linear function of some parts of the register.
  598. */
  599. #define NLFUNC(c,z) \
  600. { \
  601. t = c->R[OFF(z,0)] + c->R[OFF(z,16)]; \
  602. t ^= Sbox[(t >> 24) & 0xFF]; \
  603. t = ROR(t, 8); \
  604. t = ((t + c->R[OFF(z,1)]) ^ c->konst) + c->R[OFF(z,6)]; \
  605. t ^= Sbox[(t >> 24) & 0xFF]; \
  606. t = t + c->R[OFF(z,13)]; \
  607. }
  608. static ulong32 nltap(struct sober128_prng *c)
  609. {
  610. ulong32 t;
  611. NLFUNC(c, 0);
  612. return t;
  613. }
  614. /* initialise to known state
  615. */
  616. int sober128_start(prng_state *prng)
  617. {
  618. int i;
  619. struct sober128_prng *c;
  620. c = &(prng->sober128);
  621. /* Register initialised to Fibonacci numbers */
  622. c->R[0] = 1;
  623. c->R[1] = 1;
  624. for (i = 2; i < N; ++i) {
  625. c->R[i] = c->R[i-1] + c->R[i-2];
  626. }
  627. c->konst = INITKONST;
  628. /* next add_entropy will be the key */
  629. c->flag = 1;
  630. c->set = 0;
  631. return CRYPT_OK;
  632. }
  633. /* Save the current register state
  634. */
  635. static void s128_savestate(struct sober128_prng *c)
  636. {
  637. int i;
  638. for (i = 0; i < N; ++i) {
  639. c->initR[i] = c->R[i];
  640. }
  641. }
  642. /* initialise to previously saved register state
  643. */
  644. static void s128_reloadstate(struct sober128_prng *c)
  645. {
  646. int i;
  647. for (i = 0; i < N; ++i) {
  648. c->R[i] = c->initR[i];
  649. }
  650. }
  651. /* Initialise "konst"
  652. */
  653. static void s128_genkonst(struct sober128_prng *c)
  654. {
  655. ulong32 newkonst;
  656. do {
  657. cycle(c->R);
  658. newkonst = nltap(c);
  659. } while ((newkonst & 0xFF000000) == 0);
  660. c->konst = newkonst;
  661. }
  662. /* Load key material into the register
  663. */
  664. #define ADDKEY(k) \
  665. c->R[KEYP] += (k);
  666. #define XORNL(nl) \
  667. c->R[FOLDP] ^= (nl);
  668. /* nonlinear diffusion of register for key */
  669. #define DROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); c->R[OFF((z+1),FOLDP)] ^= t;
  670. static void s128_diffuse(struct sober128_prng *c)
  671. {
  672. ulong32 t;
  673. /* relies on FOLD == N == 17! */
  674. DROUND(0);
  675. DROUND(1);
  676. DROUND(2);
  677. DROUND(3);
  678. DROUND(4);
  679. DROUND(5);
  680. DROUND(6);
  681. DROUND(7);
  682. DROUND(8);
  683. DROUND(9);
  684. DROUND(10);
  685. DROUND(11);
  686. DROUND(12);
  687. DROUND(13);
  688. DROUND(14);
  689. DROUND(15);
  690. DROUND(16);
  691. }
  692. int sober128_add_entropy(const unsigned char *buf, unsigned long len, prng_state *prng)
  693. {
  694. struct sober128_prng *c;
  695. ulong32 i, k;
  696. c = &(prng->sober128);
  697. if (c->flag == 1) {
  698. /* this is the first call to the add_entropy so this input is the key */
  699. /* len must be multiple of 4 bytes */
  700. assert ((len & 3) == 0);
  701. for (i = 0; i < len; i += 4) {
  702. k = BYTE2WORD((unsigned char *)&buf[i]);
  703. ADDKEY(k);
  704. cycle(c->R);
  705. XORNL(nltap(c));
  706. }
  707. /* also fold in the length of the key */
  708. ADDKEY(len);
  709. /* now diffuse */
  710. s128_diffuse(c);
  711. s128_genkonst(c);
  712. s128_savestate(c);
  713. c->nbuf = 0;
  714. c->flag = 0;
  715. c->set = 1;
  716. } else {
  717. /* ok we are adding an IV then... */
  718. s128_reloadstate(c);
  719. /* len must be multiple of 4 bytes */
  720. assert ((len & 3) == 0);
  721. for (i = 0; i < len; i += 4) {
  722. k = BYTE2WORD((unsigned char *)&buf[i]);
  723. ADDKEY(k);
  724. cycle(c->R);
  725. XORNL(nltap(c));
  726. }
  727. /* also fold in the length of the key */
  728. ADDKEY(len);
  729. /* now diffuse */
  730. s128_diffuse(c);
  731. c->nbuf = 0;
  732. }
  733. return CRYPT_OK;
  734. }
  735. int sober128_ready(prng_state *prng)
  736. {
  737. return prng->sober128.set == 1 ? CRYPT_OK : CRYPT_ERROR;
  738. }
  739. /* XOR pseudo-random bytes into buffer
  740. */
  741. #define SROUND(z) STEP(c->R,z); NLFUNC(c,(z+1)); XORWORD(t, buf+(z*4));
  742. unsigned long sober128_read(unsigned char *buf, unsigned long nbytes, prng_state *prng)
  743. {
  744. struct sober128_prng *c;
  745. ulong32 t, tlen;
  746. c = &(prng->sober128);
  747. t = 0;
  748. tlen = nbytes;
  749. /* handle any previously buffered bytes */
  750. while (c->nbuf != 0 && nbytes != 0) {
  751. *buf++ ^= c->sbuf & 0xFF;
  752. c->sbuf >>= 8;
  753. c->nbuf -= 8;
  754. --nbytes;
  755. }
  756. #ifndef SMALL_CODE
  757. /* do lots at a time, if there's enough to do */
  758. while (nbytes >= N*4) {
  759. SROUND(0);
  760. SROUND(1);
  761. SROUND(2);
  762. SROUND(3);
  763. SROUND(4);
  764. SROUND(5);
  765. SROUND(6);
  766. SROUND(7);
  767. SROUND(8);
  768. SROUND(9);
  769. SROUND(10);
  770. SROUND(11);
  771. SROUND(12);
  772. SROUND(13);
  773. SROUND(14);
  774. SROUND(15);
  775. SROUND(16);
  776. buf += 4*N;
  777. nbytes -= 4*N;
  778. }
  779. #endif
  780. /* do small or odd size buffers the slow way */
  781. while (4 <= nbytes) {
  782. cycle(c->R);
  783. t = nltap(c);
  784. XORWORD(t, buf);
  785. buf += 4;
  786. nbytes -= 4;
  787. }
  788. /* handle any trailing bytes */
  789. if (nbytes != 0) {
  790. cycle(c->R);
  791. c->sbuf = nltap(c);
  792. c->nbuf = 32;
  793. while (c->nbuf != 0 && nbytes != 0) {
  794. *buf++ ^= c->sbuf & 0xFF;
  795. c->sbuf >>= 8;
  796. c->nbuf -= 8;
  797. --nbytes;
  798. }
  799. }
  800. return tlen;
  801. }
  802. /* SHA1 code by Tom St Denis */
  803. const struct _hash_descriptor sha1_desc =
  804. {
  805. "sha1",
  806. 2,
  807. 20,
  808. 64,
  809. /* DER identifier */
  810. { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E,
  811. 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 },
  812. 15,
  813. &sha1_init,
  814. &sha1_process,
  815. &sha1_done,
  816. };
  817. #define F0(x,y,z) (z ^ (x & (y ^ z)))
  818. #define F1(x,y,z) (x ^ y ^ z)
  819. #define F2(x,y,z) ((x & y) | (z & (x | y)))
  820. #define F3(x,y,z) (x ^ y ^ z)
  821. static void sha1_compress(hash_state *md, unsigned char *buf)
  822. {
  823. ulong32 a,b,c,d,e,W[80],i;
  824. /* copy the state into 512-bits into W[0..15] */
  825. for (i = 0; i < 16; i++) {
  826. LOAD32H(W[i], buf + (4*i));
  827. }
  828. /* copy state */
  829. a = md->sha1.state[0];
  830. b = md->sha1.state[1];
  831. c = md->sha1.state[2];
  832. d = md->sha1.state[3];
  833. e = md->sha1.state[4];
  834. /* expand it */
  835. for (i = 16; i < 80; i++) {
  836. W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
  837. }
  838. /* compress */
  839. /* round one */
  840. #define FF0(a,b,c,d,e,i) e = (ROL(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROL(b, 30);
  841. #define FF1(a,b,c,d,e,i) e = (ROL(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROL(b, 30);
  842. #define FF2(a,b,c,d,e,i) e = (ROL(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROL(b, 30);
  843. #define FF3(a,b,c,d,e,i) e = (ROL(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROL(b, 30);
  844. for (i = 0; i < 20; ) {
  845. FF0(a,b,c,d,e,i++);
  846. FF0(e,a,b,c,d,i++);
  847. FF0(d,e,a,b,c,i++);
  848. FF0(c,d,e,a,b,i++);
  849. FF0(b,c,d,e,a,i++);
  850. }
  851. /* round two */
  852. for (; i < 40; ) {
  853. FF1(a,b,c,d,e,i++);
  854. FF1(e,a,b,c,d,i++);
  855. FF1(d,e,a,b,c,i++);
  856. FF1(c,d,e,a,b,i++);
  857. FF1(b,c,d,e,a,i++);
  858. }
  859. /* round three */
  860. for (; i < 60; ) {
  861. FF2(a,b,c,d,e,i++);
  862. FF2(e,a,b,c,d,i++);
  863. FF2(d,e,a,b,c,i++);
  864. FF2(c,d,e,a,b,i++);
  865. FF2(b,c,d,e,a,i++);
  866. }
  867. /* round four */
  868. for (; i < 80; ) {
  869. FF3(a,b,c,d,e,i++);
  870. FF3(e,a,b,c,d,i++);
  871. FF3(d,e,a,b,c,i++);
  872. FF3(c,d,e,a,b,i++);
  873. FF3(b,c,d,e,a,i++);
  874. }
  875. #undef FF0
  876. #undef FF1
  877. #undef FF2
  878. #undef FF3
  879. /* store */
  880. md->sha1.state[0] = md->sha1.state[0] + a;
  881. md->sha1.state[1] = md->sha1.state[1] + b;
  882. md->sha1.state[2] = md->sha1.state[2] + c;
  883. md->sha1.state[3] = md->sha1.state[3] + d;
  884. md->sha1.state[4] = md->sha1.state[4] + e;
  885. }
  886. void sha1_init(hash_state * md)
  887. {
  888. md->sha1.state[0] = 0x67452301UL;
  889. md->sha1.state[1] = 0xefcdab89UL;
  890. md->sha1.state[2] = 0x98badcfeUL;
  891. md->sha1.state[3] = 0x10325476UL;
  892. md->sha1.state[4] = 0xc3d2e1f0UL;
  893. md->sha1.curlen = 0;
  894. md->sha1.length = 0;
  895. }
  896. HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
  897. int sha1_done(hash_state * md, unsigned char *hash)
  898. {
  899. int i;
  900. /*
  901. * Assert there isn't an invalid argument
  902. */
  903. assert (md->sha1.curlen < sizeof (md->sha1.buf));
  904. /* increase the length of the message */
  905. md->sha1.length += md->sha1.curlen * 8;
  906. /* append the '1' bit */
  907. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
  908. /* if the length is currently above 56 bytes we append zeros
  909. * then compress. Then we can fall back to padding zeros and length
  910. * encoding like normal.
  911. */
  912. if (md->sha1.curlen > 56) {
  913. while (md->sha1.curlen < 64) {
  914. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
  915. }
  916. sha1_compress(md, md->sha1.buf);
  917. md->sha1.curlen = 0;
  918. }
  919. /* pad upto 56 bytes of zeroes */
  920. while (md->sha1.curlen < 56) {
  921. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
  922. }
  923. /* store length */
  924. STORE64H(md->sha1.length, md->sha1.buf+56);
  925. sha1_compress(md, md->sha1.buf);
  926. /* copy output */
  927. for (i = 0; i < 5; i++) {
  928. STORE32H(md->sha1.state[i], hash+(4*i));
  929. }
  930. return CRYPT_OK;
  931. }
  932. /* Submited by Dobes Vandermeer (dobes@smartt.com) */
  933. /*
  934. (1) append zeros to the end of K to create a B byte string
  935. (e.g., if K is of length 20 bytes and B=64, then K will be
  936. appended with 44 zero bytes 0x00)
  937. (2) XOR (bitwise exclusive-OR) the B byte string computed in step
  938. (1) with ipad (ipad = the byte 0x36 repeated B times)
  939. (3) append the stream of data 'text' to the B byte string resulting
  940. from step (2)
  941. (4) apply H to the stream generated in step (3)
  942. (5) XOR (bitwise exclusive-OR) the B byte string computed in
  943. step (1) with opad (opad = the byte 0x5C repeated B times.)
  944. (6) append the H result from step (4) to the B byte string
  945. resulting from step (5)
  946. (7) apply H to the stream generated in step (6) and output
  947. the result
  948. */
  949. int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen)
  950. {
  951. unsigned char buf[128];
  952. unsigned long hashsize;
  953. unsigned long i;
  954. int err;
  955. hmac->hash = hash;
  956. hashsize = hash_descriptor[hash]->hashsize;
  957. /* valid key length? */
  958. assert (keylen > 0);
  959. assert (keylen <= hash_descriptor[hash]->blocksize);
  960. memcpy(hmac->key, key, (size_t)keylen);
  961. if(keylen < hash_descriptor[hash]->blocksize) {
  962. memset((hmac->key) + keylen, 0, (size_t)(hash_descriptor[hash]->blocksize - keylen));
  963. }
  964. // Create the initial vector for step (3)
  965. for(i=0; i < hash_descriptor[hash]->blocksize; i++) {
  966. buf[i] = hmac->key[i] ^ 0x36;
  967. }
  968. // Pre-pend that to the hash data
  969. hash_descriptor[hash]->init(&hmac->md);
  970. err = hash_descriptor[hash]->process(&hmac->md, buf, hash_descriptor[hash]->blocksize);
  971. return err;
  972. }
  973. int hmac_process(hmac_state *hmac, const unsigned char *buf, unsigned long len)
  974. {
  975. return hash_descriptor[hmac->hash]->process(&hmac->md, buf, len);
  976. }
  977. /* Submited by Dobes Vandermeer (dobes@smartt.com) */
  978. /*
  979. (1) append zeros to the end of K to create a B byte string
  980. (e.g., if K is of length 20 bytes and B=64, then K will be
  981. appended with 44 zero bytes 0x00)
  982. (2) XOR (bitwise exclusive-OR) the B byte string computed in step
  983. (1) with ipad (ipad = the byte 0x36 repeated B times)
  984. (3) append the stream of data 'text' to the B byte string resulting
  985. from step (2)
  986. (4) apply H to the stream generated in step (3)
  987. (5) XOR (bitwise exclusive-OR) the B byte string computed in
  988. step (1) with opad (opad = the byte 0x5C repeated B times.)
  989. (6) append the H result from step (4) to the B byte string
  990. resulting from step (5)
  991. (7) apply H to the stream generated in step (6) and output
  992. the result
  993. */
  994. int hmac_done(hmac_state *hmac, unsigned char *hashOut, unsigned long *outlen)
  995. {
  996. unsigned char buf[128];
  997. unsigned char isha[256];
  998. unsigned long hashsize, i;
  999. int hash, err;
  1000. /* test hash */
  1001. hash = hmac->hash;
  1002. /* get the hash message digest size */
  1003. hashsize = hash_descriptor[hash]->hashsize;
  1004. // Get the hash of the first HMAC vector plus the data
  1005. if ((err = hash_descriptor[hash]->done(&hmac->md, isha)) != CRYPT_OK) {
  1006. goto __ERR;
  1007. }
  1008. // Create the second HMAC vector vector for step (3)
  1009. for(i=0; i < hash_descriptor[hash]->blocksize; i++) {
  1010. buf[i] = hmac->key[i] ^ 0x5C;
  1011. }
  1012. // Now calculate the "outer" hash for step (5), (6), and (7)
  1013. hash_descriptor[hash]->init(&hmac->md);
  1014. if ((err = hash_descriptor[hash]->process(&hmac->md, buf, hash_descriptor[hash]->blocksize)) != CRYPT_OK) {
  1015. goto __ERR;
  1016. }
  1017. if ((err = hash_descriptor[hash]->process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
  1018. goto __ERR;
  1019. }
  1020. if ((err = hash_descriptor[hash]->done(&hmac->md, buf)) != CRYPT_OK) {
  1021. goto __ERR;
  1022. }
  1023. // copy to output
  1024. for (i = 0; i < hashsize && i < *outlen; i++) {
  1025. hashOut[i] = buf[i];
  1026. }
  1027. *outlen = i;
  1028. err = CRYPT_OK;
  1029. __ERR:
  1030. return err;
  1031. }
  1032. const struct _hash_descriptor *hash_descriptor[] =
  1033. {
  1034. &sha1_desc
  1035. };
  1036. /* portable way to get secure random bits to feed a PRNG */
  1037. /* on *NIX read /dev/random */
  1038. static unsigned long rng_nix(unsigned char *buf, unsigned long len,
  1039. void (*callback)(void))
  1040. {
  1041. int fd;
  1042. int rb;
  1043. fd = open ("/dev/urandom", O_RDONLY);
  1044. rb = read (fd, buf, len);
  1045. close (fd);
  1046. return (rb);
  1047. }
  1048. /* on ANSI C platforms with 100 < CLOCKS_PER_SEC < 10000 */
  1049. #if defined(XCLOCKS_PER_SEC)
  1050. #define ANSI_RNG
  1051. static unsigned long rng_ansic(unsigned char *buf, unsigned long len,
  1052. void (*callback)(void))
  1053. {
  1054. clock_t t1;
  1055. int l, acc, bits, a, b;
  1056. if (XCLOCKS_PER_SEC < 100 || XCLOCKS_PER_SEC > 10000) {
  1057. return 0;
  1058. }
  1059. l = len;
  1060. bits = 8;
  1061. acc = a = b = 0;
  1062. while (len--) {
  1063. if (callback != NULL) callback();
  1064. while (bits--) {
  1065. do {
  1066. t1 = XCLOCK(); while (t1 == XCLOCK()) a ^= 1;
  1067. t1 = XCLOCK(); while (t1 == XCLOCK()) b ^= 1;
  1068. } while (a == b);
  1069. acc = (acc << 1) | a;
  1070. }
  1071. *buf++ = acc;
  1072. acc = 0;
  1073. bits = 8;
  1074. }
  1075. acc = bits = a = b = 0;
  1076. return l;
  1077. }
  1078. #endif
  1079. unsigned long rng_get_bytes(unsigned char *buf, unsigned long len,
  1080. void (*callback)(void))
  1081. {
  1082. unsigned long x;
  1083. x = rng_nix(buf, len, callback); if (x != 0) { return x; }
  1084. #ifdef ANSI_RNG
  1085. x = rng_ansic(buf, len, callback); if (x != 0) { return x; }
  1086. #endif
  1087. return 0;
  1088. }
  1089. int rng_make_prng(int bits, int wprng, prng_state *prng,
  1090. void (*callback)(void))
  1091. {
  1092. unsigned char buf[256];
  1093. int err;
  1094. if (bits < 64 || bits > 1024) {
  1095. return CRYPT_INVALID_PRNGSIZE;
  1096. }
  1097. if ((err = prng_descriptor[wprng]->start(prng)) != CRYPT_OK) {
  1098. return err;
  1099. }
  1100. bits = ((bits/8)+((bits&7)!=0?1:0)) * 2;
  1101. if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) {
  1102. return CRYPT_ERROR_READPRNG;
  1103. }
  1104. if ((err = prng_descriptor[wprng]->add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) {
  1105. return err;
  1106. }
  1107. if ((err = prng_descriptor[wprng]->ready(prng)) != CRYPT_OK) {
  1108. return err;
  1109. }
  1110. return CRYPT_OK;
  1111. }