Mercurial > dropbear
comparison libtommath/bn_s_mp_rand_jenkins.c @ 1739:13d834efc376 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 15 Oct 2020 19:55:15 +0800 |
parents | 1051e4eea25a |
children |
comparison
equal
deleted
inserted
replaced
1562:768ebf737aa0 | 1739:13d834efc376 |
---|---|
1 #include "tommath_private.h" | |
2 #ifdef BN_S_MP_RAND_JENKINS_C | |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */ | |
4 /* SPDX-License-Identifier: Unlicense */ | |
5 | |
6 /* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */ | |
7 /* Chosen for speed and a good "mix" */ | |
8 typedef struct { | |
9 uint64_t a; | |
10 uint64_t b; | |
11 uint64_t c; | |
12 uint64_t d; | |
13 } ranctx; | |
14 | |
15 static ranctx jenkins_x; | |
16 | |
17 #define rot(x,k) (((x)<<(k))|((x)>>(64-(k)))) | |
18 static uint64_t s_rand_jenkins_val(void) | |
19 { | |
20 uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7); | |
21 jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13); | |
22 jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37); | |
23 jenkins_x.c = jenkins_x.d + e; | |
24 jenkins_x.d = e + jenkins_x.a; | |
25 return jenkins_x.d; | |
26 } | |
27 | |
28 void s_mp_rand_jenkins_init(uint64_t seed) | |
29 { | |
30 uint64_t i; | |
31 jenkins_x.a = 0xf1ea5eedULL; | |
32 jenkins_x.b = jenkins_x.c = jenkins_x.d = seed; | |
33 for (i = 0uLL; i < 20uLL; ++i) { | |
34 (void)s_rand_jenkins_val(); | |
35 } | |
36 } | |
37 | |
38 mp_err s_mp_rand_jenkins(void *p, size_t n) | |
39 { | |
40 char *q = (char *)p; | |
41 while (n > 0u) { | |
42 int i; | |
43 uint64_t x = s_rand_jenkins_val(); | |
44 for (i = 0; (i < 8) && (n > 0u); ++i, --n) { | |
45 *q++ = (char)(x & 0xFFuLL); | |
46 x >>= 8; | |
47 } | |
48 } | |
49 return MP_OKAY; | |
50 } | |
51 | |
52 #endif |