crypto.c 46 KB

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