crypto.c 48 KB

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