crypto.c 46 KB

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