sshargon2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. * Implementation of the Argon2 password hash function.
  3. *
  4. * My sources for the algorithm description and test vectors (the latter in
  5. * test/cryptsuite.py) were the reference implementation on Github, and also
  6. * the Internet-Draft description:
  7. *
  8. * https://github.com/P-H-C/phc-winner-argon2
  9. * https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-argon2-13
  10. */
  11. #include <assert.h>
  12. #include "putty.h"
  13. #include "ssh.h"
  14. #include "marshal.h"
  15. /* ----------------------------------------------------------------------
  16. * Argon2 uses data marshalling rules similar to SSH but with 32-bit integers
  17. * stored little-endian. Start with some local BinarySink routines for storing
  18. * a uint32 and a string in that fashion.
  19. */
  20. static void BinarySink_put_uint32_le(BinarySink *bs, unsigned long val)
  21. {
  22. unsigned char data[4];
  23. PUT_32BIT_LSB_FIRST(data, val);
  24. bs->write(bs, data, sizeof(data));
  25. }
  26. static void BinarySink_put_stringpl_le(BinarySink *bs, ptrlen pl)
  27. {
  28. /* Check that the string length fits in a uint32, without doing a
  29. * potentially implementation-defined shift of more than 31 bits */
  30. assert((pl.len >> 31) < 2);
  31. BinarySink_put_uint32_le(bs, pl.len);
  32. bs->write(bs, pl.ptr, pl.len);
  33. }
  34. #define put_uint32_le(bs, val) \
  35. BinarySink_put_uint32_le(BinarySink_UPCAST(bs), val)
  36. #define put_stringpl_le(bs, val) \
  37. BinarySink_put_stringpl_le(BinarySink_UPCAST(bs), val)
  38. /* ----------------------------------------------------------------------
  39. * Argon2 defines a hash-function family that's an extension of BLAKE2b to
  40. * generate longer output digests, by repeatedly outputting half of a BLAKE2
  41. * hash output and then re-hashing the whole thing until there are 64 or fewer
  42. * bytes left to output. The spec calls this H' (a variant of the original
  43. * hash it calls H, which is the unmodified BLAKE2b).
  44. */
  45. static ssh_hash *hprime_new(unsigned length)
  46. {
  47. ssh_hash *h = blake2b_new_general(length > 64 ? 64 : length);
  48. put_uint32_le(h, length);
  49. return h;
  50. }
  51. static void hprime_final(ssh_hash *h, unsigned length, void *vout)
  52. {
  53. uint8_t *out = (uint8_t *)vout;
  54. while (length > 64) {
  55. uint8_t hashbuf[64];
  56. ssh_hash_final(h, hashbuf);
  57. memcpy(out, hashbuf, 32);
  58. out += 32;
  59. length -= 32;
  60. h = blake2b_new_general(length > 64 ? 64 : length);
  61. put_data(h, hashbuf, 64);
  62. smemclr(hashbuf, sizeof(hashbuf));
  63. }
  64. ssh_hash_final(h, out);
  65. }
  66. /* Externally visible entry point for the long hash function. This is only
  67. * used by testcrypt, so it would be overkill to set it up like a proper
  68. * ssh_hash. */
  69. strbuf *argon2_long_hash(unsigned length, ptrlen data)
  70. {
  71. ssh_hash *h = hprime_new(length);
  72. put_datapl(h, data);
  73. { // WINSCP
  74. strbuf *out = strbuf_new();
  75. hprime_final(h, length, strbuf_append(out, length));
  76. return out;
  77. } // WINSCP
  78. }
  79. /* ----------------------------------------------------------------------
  80. * Argon2's own mixing function G, which operates on 1Kb blocks of data.
  81. *
  82. * The definition of G in the spec takes two 1Kb blocks as input and produces
  83. * a 1Kb output block. The first thing that happens to the input blocks is
  84. * that they get XORed together, and then only the XOR output is used, so you
  85. * could perfectly well regard G as a 1Kb->1Kb function.
  86. */
  87. static inline uint64_t ror(uint64_t x, unsigned rotation)
  88. {
  89. #pragma option push -w-ngu // WINSCP
  90. unsigned lshift = 63 & -rotation, rshift = 63 & rotation;
  91. #pragma option pop // WINSCP
  92. return (x << lshift) | (x >> rshift);
  93. }
  94. static inline uint64_t trunc32(uint64_t x)
  95. {
  96. return x & 0xFFFFFFFF;
  97. }
  98. /* Internal function similar to the BLAKE2b round, which mixes up four 64-bit
  99. * words */
  100. static inline void GB(uint64_t *a, uint64_t *b, uint64_t *c, uint64_t *d)
  101. {
  102. *a += *b + 2 * trunc32(*a) * trunc32(*b);
  103. *d = ror(*d ^ *a, 32);
  104. *c += *d + 2 * trunc32(*c) * trunc32(*d);
  105. *b = ror(*b ^ *c, 24);
  106. *a += *b + 2 * trunc32(*a) * trunc32(*b);
  107. *d = ror(*d ^ *a, 16);
  108. *c += *d + 2 * trunc32(*c) * trunc32(*d);
  109. *b = ror(*b ^ *c, 63);
  110. }
  111. /* Higher-level internal function which mixes up sixteen 64-bit words. This is
  112. * applied to different subsets of the 128 words in a kilobyte block, and the
  113. * API here is designed to make it easy to apply in the circumstances the spec
  114. * requires. In every call, the sixteen words form eight pairs adjacent in
  115. * memory, whose addresses are in arithmetic progression. So the 16 input
  116. * words are in[0], in[1], in[instep], in[instep+1], ..., in[7*instep],
  117. * in[7*instep+1], and the 16 output words similarly. */
  118. static inline void P(uint64_t *out, unsigned outstep,
  119. uint64_t *in, unsigned instep)
  120. {
  121. unsigned i; // WINSCP
  122. for (i = 0; i < 8; i++) {
  123. out[i*outstep] = in[i*instep];
  124. out[i*outstep+1] = in[i*instep+1];
  125. }
  126. GB(out+0*outstep+0, out+2*outstep+0, out+4*outstep+0, out+6*outstep+0);
  127. GB(out+0*outstep+1, out+2*outstep+1, out+4*outstep+1, out+6*outstep+1);
  128. GB(out+1*outstep+0, out+3*outstep+0, out+5*outstep+0, out+7*outstep+0);
  129. GB(out+1*outstep+1, out+3*outstep+1, out+5*outstep+1, out+7*outstep+1);
  130. GB(out+0*outstep+0, out+2*outstep+1, out+5*outstep+0, out+7*outstep+1);
  131. GB(out+0*outstep+1, out+3*outstep+0, out+5*outstep+1, out+6*outstep+0);
  132. GB(out+1*outstep+0, out+3*outstep+1, out+4*outstep+0, out+6*outstep+1);
  133. GB(out+1*outstep+1, out+2*outstep+0, out+4*outstep+1, out+7*outstep+0);
  134. }
  135. /* The full G function, taking input blocks X and Y. The result of G is most
  136. * often XORed into an existing output block, so this API is designed with
  137. * that in mind: the mixing function's output is always XORed into whatever
  138. * 1Kb of data is already at 'out'. */
  139. static void G_xor(uint8_t *out, const uint8_t *X, const uint8_t *Y)
  140. {
  141. uint64_t R[128], Q[128], Z[128];
  142. unsigned i; // WINSCP
  143. for (i = 0; i < 128; i++)
  144. R[i] = GET_64BIT_LSB_FIRST(X + 8*i) ^ GET_64BIT_LSB_FIRST(Y + 8*i);
  145. for (i = 0; i < 8; i++) // WINSCP
  146. P(Q+16*i, 2, R+16*i, 2);
  147. for (i = 0; i < 8; i++) // WINSCP
  148. P(Z+2*i, 16, Q+2*i, 16);
  149. for (i = 0; i < 128; i++) // WINSCP
  150. PUT_64BIT_LSB_FIRST(out + 8*i,
  151. GET_64BIT_LSB_FIRST(out + 8*i) ^ R[i] ^ Z[i]);
  152. smemclr(R, sizeof(R));
  153. smemclr(Q, sizeof(Q));
  154. smemclr(Z, sizeof(Z));
  155. }
  156. /* ----------------------------------------------------------------------
  157. * The main Argon2 function.
  158. */
  159. static void argon2_internal(uint32_t p, uint32_t T, uint32_t m, uint32_t t,
  160. uint32_t y, ptrlen P, ptrlen S, ptrlen K, ptrlen X,
  161. uint8_t *out)
  162. {
  163. /*
  164. * Start by hashing all the input data together: the four string arguments
  165. * (password P, salt S, optional secret key K, optional associated data
  166. * X), plus all the parameters for the function's memory and time usage.
  167. *
  168. * The output of this hash is the sole input to the subsequent mixing
  169. * step: Argon2 does not preserve any more entropy from the inputs, it
  170. * just makes it extra painful to get the final answer.
  171. */
  172. uint8_t h0[64];
  173. {
  174. ssh_hash *h = blake2b_new_general(64);
  175. put_uint32_le(h, p);
  176. put_uint32_le(h, T);
  177. put_uint32_le(h, m);
  178. put_uint32_le(h, t);
  179. put_uint32_le(h, 0x13); /* hash function version number */
  180. put_uint32_le(h, y);
  181. put_stringpl_le(h, P);
  182. put_stringpl_le(h, S);
  183. put_stringpl_le(h, K);
  184. put_stringpl_le(h, X);
  185. ssh_hash_final(h, h0);
  186. }
  187. { // WINSCP
  188. struct blk { uint8_t data[1024]; };
  189. /*
  190. * Array of 1Kb blocks. The total size is (approximately) m, the
  191. * caller-specified parameter for how much memory to use; the blocks are
  192. * regarded as a rectangular array of p rows ('lanes') by q columns, where
  193. * p is the 'parallelism' input parameter (the lanes can be processed
  194. * concurrently up to a point) and q is whatever makes the product pq come
  195. * to m.
  196. *
  197. * Additionally, each row is divided into four equal 'segments', which are
  198. * important to the way the algorithm decides which blocks to use as input
  199. * to each step of the function.
  200. *
  201. * The term 'slice' refers to a whole set of vertically aligned segments,
  202. * i.e. slice 0 is the whole left quarter of the array, and slice 3 the
  203. * whole right quarter.
  204. */
  205. size_t SL = m / (4*p); /* segment length: # of 1Kb blocks in a segment */
  206. size_t q = 4 * SL; /* width of the array: 4 segments times SL */
  207. size_t mprime = q * p; /* total size of the array, approximately m */
  208. /* Allocate the memory. */
  209. struct blk *B = snewn(mprime, struct blk);
  210. memset(B, 0, mprime * sizeof(struct blk));
  211. /*
  212. * Initial setup: fill the first two full columns of the array with data
  213. * expanded from the starting hash h0. Each block is the result of using
  214. * the long-output hash function H' to hash h0 itself plus the block's
  215. * coordinates in the array.
  216. */
  217. { // WINSCP
  218. size_t i; // WINSCP
  219. for (i = 0; i < p; i++) {
  220. ssh_hash *h = hprime_new(1024);
  221. put_data(h, h0, 64);
  222. put_uint32_le(h, 0);
  223. put_uint32_le(h, i);
  224. hprime_final(h, 1024, B[i].data);
  225. }
  226. for (i = 0; i < p; i++) { // WINSCP
  227. ssh_hash *h = hprime_new(1024);
  228. put_data(h, h0, 64);
  229. put_uint32_le(h, 1);
  230. put_uint32_le(h, i);
  231. hprime_final(h, 1024, B[i+p].data);
  232. }
  233. /*
  234. * Declarations for the main loop.
  235. *
  236. * The basic structure of the main loop is going to involve processing the
  237. * array one whole slice (vertically divided quarter) at a time. Usually
  238. * we'll write a new value into every single block in the slice, except
  239. * that in the initial slice on the first pass, we've already written
  240. * values into the first two columns during the initial setup above. So
  241. * 'jstart' indicates the starting index in each segment we process; it
  242. * starts off as 2 so that we don't overwrite the inital setup, and then
  243. * after the first slice is done, we set it to 0, and it stays there.
  244. *
  245. * d_mode indicates whether we're being data-dependent (true) or
  246. * data-independent (false). In the hybrid Argon2id mode, we start off
  247. * independent, and then once we've mixed things up enough, switch over to
  248. * dependent mode to force long serial chains of computation.
  249. */
  250. { // WINSCP
  251. size_t jstart = 2;
  252. bool d_mode = (y == 0);
  253. struct blk out2i, tmp2i, in2i;
  254. /* Outermost loop: t whole passes from left to right over the array */
  255. size_t pass; // WINSCP
  256. for (pass = 0; pass < t; pass++) {
  257. /* Within that, we process the array in its four main slices */
  258. unsigned slice; // WINSCP
  259. for (slice = 0; slice < 4; slice++) {
  260. /* In Argon2id mode, if we're half way through the first pass,
  261. * this is the moment to switch d_mode from false to true */
  262. if (pass == 0 && slice == 2 && y == 2)
  263. d_mode = true;
  264. /* Loop over every segment in the slice (i.e. every row). So i is
  265. * the y-coordinate of each block we process. */
  266. { // WINSCP
  267. size_t i; // WINSCP
  268. for (i = 0; i < p; i++) {
  269. /* And within that segment, process the blocks from left to
  270. * right, starting at 'jstart' (usually 0, but 2 in the first
  271. * slice). */
  272. size_t jpre; // WINSCP
  273. for (jpre = jstart; jpre < SL; jpre++) {
  274. /* j is the x-coordinate of each block we process, made up
  275. * of the slice number and the index 'jpre' within the
  276. * segment. */
  277. size_t j = slice * SL + jpre;
  278. /* jm1 is j-1 (mod q) */
  279. uint32_t jm1 = (j == 0 ? q-1 : j-1);
  280. /*
  281. * Construct two 32-bit pseudorandom integers J1 and J2.
  282. * This is the part of the algorithm that varies between
  283. * the data-dependent and independent modes.
  284. */
  285. uint32_t J1, J2;
  286. if (d_mode) {
  287. /*
  288. * Data-dependent: grab the first 64 bits of the block
  289. * to the left of this one.
  290. */
  291. J1 = GET_32BIT_LSB_FIRST(B[i + p * jm1].data);
  292. J2 = GET_32BIT_LSB_FIRST(B[i + p * jm1].data + 4);
  293. } else {
  294. /*
  295. * Data-independent: generate pseudorandom data by
  296. * hashing a sequence of preimage blocks that include
  297. * all our input parameters, plus the coordinates of
  298. * this point in the algorithm (array position and
  299. * pass number) to make all the hash outputs distinct.
  300. *
  301. * The hash we use is G itself, applied twice. So we
  302. * generate 1Kb of data at a time, which is enough for
  303. * 128 (J1,J2) pairs. Hence we only need to do the
  304. * hashing if our index within the segment is a
  305. * multiple of 128, or if we're at the very start of
  306. * the algorithm (in which case we started at 2 rather
  307. * than 0). After that we can just keep picking data
  308. * out of our most recent hash output.
  309. */
  310. if (jpre == jstart || jpre % 128 == 0) {
  311. /*
  312. * Hash preimage is mostly zeroes, with a
  313. * collection of assorted integer values we had
  314. * anyway.
  315. */
  316. memset(in2i.data, 0, sizeof(in2i.data));
  317. PUT_64BIT_LSB_FIRST(in2i.data + 0, pass);
  318. PUT_64BIT_LSB_FIRST(in2i.data + 8, i);
  319. PUT_64BIT_LSB_FIRST(in2i.data + 16, slice);
  320. PUT_64BIT_LSB_FIRST(in2i.data + 24, mprime);
  321. PUT_64BIT_LSB_FIRST(in2i.data + 32, t);
  322. PUT_64BIT_LSB_FIRST(in2i.data + 40, y);
  323. PUT_64BIT_LSB_FIRST(in2i.data + 48, jpre / 128 + 1);
  324. /*
  325. * Now apply G twice to generate the hash output
  326. * in out2i.
  327. */
  328. memset(tmp2i.data, 0, sizeof(tmp2i.data));
  329. G_xor(tmp2i.data, tmp2i.data, in2i.data);
  330. memset(out2i.data, 0, sizeof(out2i.data));
  331. G_xor(out2i.data, out2i.data, tmp2i.data);
  332. }
  333. /*
  334. * Extract J1 and J2 from the most recent hash output
  335. * (whether we've just computed it or not).
  336. */
  337. J1 = GET_32BIT_LSB_FIRST(
  338. out2i.data + 8 * (jpre % 128));
  339. J2 = GET_32BIT_LSB_FIRST(
  340. out2i.data + 8 * (jpre % 128) + 4);
  341. }
  342. /*
  343. * Now convert J1 and J2 into the index of an existing
  344. * block of the array to use as input to this step. This
  345. * is fairly fiddly.
  346. *
  347. * The easy part: the y-coordinate of the input block is
  348. * obtained by reducing J2 mod p, except that at the very
  349. * start of the algorithm (processing the first slice on
  350. * the first pass) we simply use the same y-coordinate as
  351. * our output block.
  352. *
  353. * Note that it's safe to use the ordinary % operator
  354. * here, without any concern for timing side channels: in
  355. * data-independent mode J2 is not correlated to any
  356. * secrets, and in data-dependent mode we're going to be
  357. * giving away side-channel data _anyway_ when we use it
  358. * as an array index (and by assumption we don't care,
  359. * because it's already massively randomised from the real
  360. * inputs).
  361. */
  362. { // WINSCP
  363. uint32_t index_l = (pass == 0 && slice == 0) ? i : J2 % p;
  364. /*
  365. * The hard part: which block in this array row do we use?
  366. *
  367. * First, we decide what the possible candidates are. This
  368. * requires some case analysis, and depends on whether the
  369. * array row is the same one we're writing into or not.
  370. *
  371. * If it's not the same row: we can't use any block from
  372. * the current slice (because the segments within a slice
  373. * have to be processable in parallel, so in a concurrent
  374. * implementation those blocks are potentially in the
  375. * process of being overwritten by other threads). But the
  376. * other three slices are fair game, except that in the
  377. * first pass, slices to the right of us won't have had
  378. * any values written into them yet at all.
  379. *
  380. * If it is the same row, we _are_ allowed to use blocks
  381. * from the current slice, but only the ones before our
  382. * current position.
  383. *
  384. * In both cases, we also exclude the individual _column_
  385. * just to the left of the current one. (The block
  386. * immediately to our left is going to be the _other_
  387. * input to G, but the spec also says that we avoid that
  388. * column even in a different row.)
  389. *
  390. * All of this means that we end up choosing from a
  391. * cyclically contiguous interval of blocks within this
  392. * lane, but the start and end points require some thought
  393. * to get them right.
  394. */
  395. /* Start position is the beginning of the _next_ slice
  396. * (containing data from the previous pass), unless we're
  397. * on pass 0, where the start position has to be 0. */
  398. uint32_t Wstart = (pass == 0 ? 0 : (slice + 1) % 4 * SL);
  399. /* End position splits up by cases. */
  400. uint32_t Wend;
  401. if (index_l == i) {
  402. /* Same lane as output: we can use anything up to (but
  403. * not including) the block immediately left of us. */
  404. Wend = jm1;
  405. } else {
  406. /* Different lane from output: we can use anything up
  407. * to the previous slice boundary, or one less than
  408. * that if we're at the very left edge of our slice
  409. * right now. */
  410. Wend = SL * slice;
  411. if (jpre == 0)
  412. Wend = (Wend + q-1) % q;
  413. }
  414. /* Total number of blocks available to choose from */
  415. { // WINSCP
  416. uint32_t Wsize = (Wend + q - Wstart) % q;
  417. /* Fiddly computation from the spec that chooses from the
  418. * available blocks, in a deliberately non-uniform
  419. * fashion, using J1 as pseudorandom input data. Output is
  420. * zz which is the index within our contiguous interval. */
  421. uint32_t x = ((uint64_t)J1 * J1) >> 32;
  422. uint32_t y = ((uint64_t)Wsize * x) >> 32;
  423. uint32_t zz = Wsize - 1 - y;
  424. /* And index_z is the actual x coordinate of the block we
  425. * want. */
  426. uint32_t index_z = (Wstart + zz) % q;
  427. /* Phew! Combine that block with the one immediately to
  428. * our left, and XOR over the top of whatever is already
  429. * in our current output block. */
  430. G_xor(B[i + p * j].data, B[i + p * jm1].data,
  431. B[index_l + p * index_z].data);
  432. } // WINSCP
  433. } // WINSCP
  434. }
  435. }
  436. /* We've finished processing a slice. Reset jstart to 0. It will
  437. * onily _not_ have been 0 if this was pass 0 slice 0, in which
  438. * case it still had its initial value of 2 to avoid the starting
  439. * data. */
  440. jstart = 0;
  441. } // WINSCP
  442. }
  443. }
  444. /*
  445. * The main output is all done. Final output works by taking the XOR of
  446. * all the blocks in the rightmost column of the array, and then using
  447. * that as input to our long hash H'. The output of _that_ is what we
  448. * deliver to the caller.
  449. */
  450. { // WINSCP
  451. struct blk C = B[p * (q-1)];
  452. size_t i; // WINSCP
  453. for (i = 1; i < p; i++)
  454. memxor(C.data, C.data, B[i + p * (q-1)].data, 1024);
  455. {
  456. ssh_hash *h = hprime_new(T);
  457. put_data(h, C.data, 1024);
  458. hprime_final(h, T, out);
  459. }
  460. /*
  461. * Clean up.
  462. */
  463. smemclr(out2i.data, sizeof(out2i.data));
  464. smemclr(tmp2i.data, sizeof(tmp2i.data));
  465. smemclr(in2i.data, sizeof(in2i.data));
  466. smemclr(C.data, sizeof(C.data));
  467. smemclr(B, mprime * sizeof(struct blk));
  468. sfree(B);
  469. } // WINSCP
  470. } // WINSCP
  471. } // WINSCP
  472. } // WINSCP
  473. }
  474. /*
  475. * Wrapper function that appends to a strbuf (which sshpubk.c will want).
  476. */
  477. void argon2(Argon2Flavour flavour, uint32_t mem, uint32_t passes,
  478. uint32_t parallel, uint32_t taglen,
  479. ptrlen P, ptrlen S, ptrlen K, ptrlen X, strbuf *out)
  480. {
  481. argon2_internal(parallel, taglen, mem, passes, flavour,
  482. P, S, K, X, strbuf_append(out, taglen));
  483. }
  484. /*
  485. * Wrapper function which dynamically chooses the number of passes to run in
  486. * order to hit an approximate total amount of CPU time. Writes the result
  487. * into 'passes'.
  488. */
  489. void argon2_choose_passes(
  490. Argon2Flavour flavour, uint32_t mem,
  491. uint32_t milliseconds, uint32_t *passes,
  492. uint32_t parallel, uint32_t taglen,
  493. ptrlen P, ptrlen S, ptrlen K, ptrlen X,
  494. strbuf *out)
  495. {
  496. unsigned long desired_time = (TICKSPERSEC * milliseconds) / 1000;
  497. /*
  498. * We only need the time taken to be approximately right, so we
  499. * scale up the number of passes geometrically, which avoids
  500. * taking O(t^2) time to find a pass count taking time t.
  501. *
  502. * Using the Fibonacci numbers is slightly nicer than the obvious
  503. * approach of powers of 2, because it's still very easy to
  504. * compute, and grows less fast (powers of 1.6 instead of 2), so
  505. * you get just a touch more precision.
  506. */
  507. uint32_t a = 1, b = 1;
  508. while (true) {
  509. unsigned long start_time = GETTICKCOUNT();
  510. argon2(flavour, mem, b, parallel, taglen, P, S, K, X, out);
  511. { // WINSCP
  512. unsigned long ticks = GETTICKCOUNT() - start_time;
  513. /* But just in case computers get _too_ fast, we have to cap
  514. * the growth before it gets past the uint32_t upper bound! So
  515. * if computing a+b would overflow, stop here. */
  516. if (ticks >= desired_time || a > (uint32_t)~b) {
  517. *passes = b;
  518. return;
  519. } else {
  520. strbuf_clear(out);
  521. /* Next Fibonacci number: replace (a, b) with (b, a+b) */
  522. b += a;
  523. a = b - a;
  524. }
  525. } // WINSCP
  526. }
  527. }