| 1 |
#ifdef __cplusplus |
| 2 |
extern "C" { |
| 3 |
#endif |
| 4 |
|
| 5 |
#include "ulib/chacha.h" |
| 6 |
#include "ulib/splitmix.h" |
| 7 |
#include "ulib/xoshiro.h" |
| 8 |
|
| 9 |
#ifdef __cplusplus |
| 10 |
} |
| 11 |
#endif |
| 12 |
|
| 13 |
/* perl versions broken on some platforms */ |
| 14 |
#undef U8TO16_LE |
| 15 |
#define U8TO16_LE(p) ( \ |
| 16 |
((U16)((p)[0]) ) | \ |
| 17 |
((U16)((p)[1]) << 8) \ |
| 18 |
) |
| 19 |
#undef U8TO32_LE |
| 20 |
#define U8TO32_LE(p) ( \ |
| 21 |
((U32)((p)[0]) ) | \ |
| 22 |
((U32)((p)[1]) << 8) | \ |
| 23 |
((U32)((p)[2]) << 16) | \ |
| 24 |
((U32)((p)[3]) << 24) \ |
| 25 |
) |
| 26 |
#undef U8TO64_LE |
| 27 |
#define U8TO64_LE(p) ( \ |
| 28 |
((U64)((p)[0]) ) | \ |
| 29 |
((U64)((p)[1]) << 8) | \ |
| 30 |
((U64)((p)[2]) << 16) | \ |
| 31 |
((U64)((p)[3]) << 24) | \ |
| 32 |
((U64)((p)[4]) << 32) | \ |
| 33 |
((U64)((p)[5]) << 40) | \ |
| 34 |
((U64)((p)[6]) << 48) | \ |
| 35 |
((U64)((p)[7]) << 56) \ |
| 36 |
) |
| 37 |
#undef U32TO8_LE |
| 38 |
#define U32TO8_LE(p, v) do { \ |
| 39 |
U32 _v = v; \ |
| 40 |
(p)[0] = (((_v) ) & 0xFFU); \ |
| 41 |
(p)[1] = (((_v) >> 8) & 0xFFU); \ |
| 42 |
(p)[2] = (((_v) >> 16) & 0xFFU); \ |
| 43 |
(p)[3] = (((_v) >> 24) & 0xFFU); \ |
| 44 |
} while (0) |
| 45 |
|
| 46 |
/* perls ROTL32 broken too */ |
| 47 |
#define rotl32(x,r) ((((U32)(x)) << (r)) | (((U32)(x)) >> (32 - (r)))) |
| 48 |
|
| 49 |
#define QROUND(a,b,c,d) \ |
| 50 |
a += b; d = rotl32(d ^ a, 16); \ |
| 51 |
c += d; b = rotl32(b ^ c, 12); \ |
| 52 |
a += b; d = rotl32(d ^ a, 8); \ |
| 53 |
c += d; b = rotl32(b ^ c, 7); |
| 54 |
|
| 55 |
|
| 56 |
static void chacha_init(pUCXT, const UCHAR *seed, IV init_buffer) { |
| 57 |
chacha_t *cc = &SMEM->cc; |
| 58 |
U32 *x = (U32*)&cc->state; |
| 59 |
|
| 60 |
x[ 0] = 0x61707865; |
| 61 |
x[ 1] = 0x3320646e; |
| 62 |
x[ 2] = 0x79622d32; |
| 63 |
x[ 3] = 0x6b206574; |
| 64 |
x[ 4] = U8TO32_LE(seed + 0); |
| 65 |
x[ 5] = U8TO32_LE(seed + 4); |
| 66 |
x[ 6] = U8TO32_LE(seed + 8); |
| 67 |
x[ 7] = U8TO32_LE(seed + 12); |
| 68 |
x[ 8] = U8TO32_LE(seed + 16); |
| 69 |
x[ 9] = U8TO32_LE(seed + 20); |
| 70 |
x[10] = U8TO32_LE(seed + 24); |
| 71 |
x[11] = U8TO32_LE(seed + 28); |
| 72 |
x[12] = 0; |
| 73 |
x[13] = 0; |
| 74 |
x[14] = U8TO32_LE(seed + 32); |
| 75 |
x[15] = U8TO32_LE(seed + 36); |
| 76 |
|
| 77 |
if (init_buffer) { |
| 78 |
memset(cc->buf, 0, CC_BUFSZ); |
| 79 |
cc->have = 0; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static void chacha_core(pUCXT, UCHAR* buf) { |
| 84 |
chacha_t *cc = &SMEM->cc; |
| 85 |
U32 *s = cc->state; |
| 86 |
U32 i, x[16]; |
| 87 |
|
| 88 |
memcpy(x, s, 16*sizeof(U32)); |
| 89 |
|
| 90 |
for (i = 0; i<CC_ROUNDS; i+=2) { |
| 91 |
QROUND( x[ 0], x[ 4], x[ 8], x[12] ); |
| 92 |
QROUND( x[ 1], x[ 5], x[ 9], x[13] ); |
| 93 |
QROUND( x[ 2], x[ 6], x[10], x[14] ); |
| 94 |
QROUND( x[ 3], x[ 7], x[11], x[15] ); |
| 95 |
QROUND( x[ 0], x[ 5], x[10], x[15] ); |
| 96 |
QROUND( x[ 1], x[ 6], x[11], x[12] ); |
| 97 |
QROUND( x[ 2], x[ 7], x[ 8], x[13] ); |
| 98 |
QROUND( x[ 3], x[ 4], x[ 9], x[14] ); |
| 99 |
} |
| 100 |
|
| 101 |
for (i = 0; i < 16; i++) |
| 102 |
x[i] += s[i]; |
| 103 |
|
| 104 |
for (i = 0; i < 16; i++) |
| 105 |
U32TO8_LE( buf+4*i, x[i] ); |
| 106 |
|
| 107 |
/* inc counter */ |
| 108 |
if (++s[12] == 0) s[13]++; |
| 109 |
} |
| 110 |
|
| 111 |
static U16 chacha_stream(pUCXT, UCHAR* buf, U16 n) { |
| 112 |
U16 r = n; |
| 113 |
UCHAR sbuf[CC_CORESZ]; |
| 114 |
|
| 115 |
while (r >= CC_CORESZ) { |
| 116 |
chacha_core(aUCXT, buf); |
| 117 |
buf += CC_CORESZ; |
| 118 |
r -= CC_CORESZ; |
| 119 |
} |
| 120 |
if (r > 0) { |
| 121 |
chacha_core(aUCXT, sbuf); |
| 122 |
memcpy(buf, sbuf, r); |
| 123 |
} |
| 124 |
return n; |
| 125 |
} |
| 126 |
|
| 127 |
static U32 chacha_refill(pUCXT) { |
| 128 |
chacha_t *cc = &SMEM->cc; |
| 129 |
U64 *cp; |
| 130 |
|
| 131 |
/* refill buffer */ |
| 132 |
cc->have = chacha_stream(aUCXT, (UCHAR*)&cc->buf, CC_BUFSZ); |
| 133 |
|
| 134 |
/* reseed with KEYSZ bytes from buffer, then zero */ |
| 135 |
/* |
| 136 |
chacha_init(cc.buf, 0); |
| 137 |
memset(cc.buf, 0, KEYSZ); |
| 138 |
cc.have = BUFSZ - KEYSZ; |
| 139 |
return cc.have; |
| 140 |
*/ |
| 141 |
|
| 142 |
/* create new key */ |
| 143 |
/* |
| 144 |
UCHAR seed[40]; |
| 145 |
cp = (U64*)&seed; |
| 146 |
*cp++ = uu_xoshiro_rand(); |
| 147 |
*cp++ = uu_xoshiro_rand(); |
| 148 |
*cp++ = uu_xoshiro_rand(); |
| 149 |
*cp++ = uu_xoshiro_rand(); |
| 150 |
*cp++ = uu_xoshiro_rand(); |
| 151 |
chacha_init((UCHAR*)&seed, 0); |
| 152 |
return cc.have; |
| 153 |
*/ |
| 154 |
|
| 155 |
/* salt the state */ |
| 156 |
/* |
| 157 |
cp = (U64*)&cc.state; |
| 158 |
while (cp < (U64*)&cc.buf) |
| 159 |
*cp++ ^= (U32)uu_xoshiro_rand(); |
| 160 |
return cc.have; |
| 161 |
*/ |
| 162 |
|
| 163 |
/* salt the buffer */ |
| 164 |
cp = (U64*)&cc->buf; |
| 165 |
while (cp < (U64*)&cc->have) |
| 166 |
*cp++ ^= uu_xoshiro_rand(aUCXT); |
| 167 |
return cc->have; |
| 168 |
} |
| 169 |
|
| 170 |
void uu_chacha_srand(pUCXT) { |
| 171 |
U64 d, n, *cp; |
| 172 |
UCHAR data[40]; |
| 173 |
|
| 174 |
uu_splitmix_srand(aUCXT); |
| 175 |
uu_xoshiro_srand(aUCXT); |
| 176 |
|
| 177 |
cp = (U64*)&data; |
| 178 |
|
| 179 |
*cp++ = uu_xoshiro_rand(aUCXT); |
| 180 |
*cp++ = uu_xoshiro_rand(aUCXT); |
| 181 |
*cp++ = uu_xoshiro_rand(aUCXT); |
| 182 |
*cp++ = uu_xoshiro_rand(aUCXT); |
| 183 |
*cp++ = uu_xoshiro_rand(aUCXT); |
| 184 |
|
| 185 |
chacha_init(aUCXT, data, 1); |
| 186 |
|
| 187 |
/* stir 8 - 39 times */ |
| 188 |
uu_chacha_rand64(aUCXT, &d); |
| 189 |
n = 8 + (d >> 59); |
| 190 |
|
| 191 |
while (n-- > 0) |
| 192 |
uu_chacha_rand64(aUCXT, &d); |
| 193 |
} |
| 194 |
|
| 195 |
/* API */ |
| 196 |
|
| 197 |
void uu_chacha_rand16(pUCXT, U16 *out) { |
| 198 |
chacha_t *cc = &SMEM->cc; |
| 199 |
UCHAR *ptr; |
| 200 |
|
| 201 |
if (cc->have < 2) chacha_refill(aUCXT); |
| 202 |
ptr = cc->buf + CC_BUFSZ - cc->have; |
| 203 |
cc->have -= 2; |
| 204 |
|
| 205 |
*out = U8TO16_LE(ptr); |
| 206 |
} |
| 207 |
|
| 208 |
void uu_chacha_rand32(pUCXT, U32 *out) { |
| 209 |
chacha_t *cc = &SMEM->cc; |
| 210 |
UCHAR *ptr; |
| 211 |
|
| 212 |
if (cc->have < 4) chacha_refill(aUCXT); |
| 213 |
ptr = cc->buf + CC_BUFSZ - cc->have; |
| 214 |
cc->have -= 4; |
| 215 |
|
| 216 |
*out = U8TO32_LE(ptr); |
| 217 |
} |
| 218 |
|
| 219 |
void uu_chacha_rand64(pUCXT, U64 *out) { |
| 220 |
chacha_t *cc = &SMEM->cc; |
| 221 |
UCHAR *ptr; |
| 222 |
|
| 223 |
if (cc->have < 8) chacha_refill(aUCXT); |
| 224 |
ptr = cc->buf + CC_BUFSZ - cc->have; |
| 225 |
cc->have -= 8; |
| 226 |
|
| 227 |
*out = U8TO64_LE(ptr); |
| 228 |
} |
| 229 |
|
| 230 |
void uu_chacha_rand128(pUCXT, void *out) { |
| 231 |
chacha_t *cc = &SMEM->cc; |
| 232 |
U64 a, b; |
| 233 |
UCHAR *ptr; |
| 234 |
|
| 235 |
if (cc->have < 16) chacha_refill(aUCXT); |
| 236 |
ptr = cc->buf + CC_BUFSZ - cc->have; |
| 237 |
cc->have -= 16; |
| 238 |
|
| 239 |
a = U8TO64_LE(ptr); |
| 240 |
b = U8TO64_LE(ptr); |
| 241 |
*((U64*)out) = a; |
| 242 |
*(((U64*)out)+8) = b; |
| 243 |
} |
| 244 |
|
| 245 |
/* ex:set ts=2 sw=2 itab=spaces: */ |