crypto.c 46 KB

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