crypto.c 46 KB

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