| 1 | /* Jitter RNG: Keccak / SHA-3 / SHAKE / XDRBG Implementation |
| 2 | * |
| 3 | * Copyright (C) 2021 - 2026, Stephan Mueller <[email protected]> |
| 4 | * |
| 5 | * License: see LICENSE file in root directory |
| 6 | * |
| 7 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 8 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 9 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 10 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 11 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 12 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 13 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 14 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 15 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 16 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 17 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 18 | * DAMAGE. |
| 19 | */ |
| 20 | |
| 21 | #include "jitterentropy-sha3.h" |
| 22 | #include "jitterentropy-internal.h" |
| 23 | |
| 24 | /*************************************************************************** |
| 25 | * Message Digest Implementation |
| 26 | ***************************************************************************/ |
| 27 | |
| 28 | /* |
| 29 | * Conversion of Little-Endian representations in byte streams - the data |
| 30 | * representation in the integer values is the host representation. |
| 31 | */ |
| 32 | static inline uint32_t ptr_to_le32(const uint8_t *p) |
| 33 | { |
| 34 | return (uint32_t)p[0] | (uint32_t)p[1] << 8 | |
| 35 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; |
| 36 | } |
| 37 | |
| 38 | static inline uint64_t ptr_to_le64(const uint8_t *p) |
| 39 | { |
| 40 | return (uint64_t)ptr_to_le32(p) | (uint64_t)ptr_to_le32(p + 4) << 32; |
| 41 | } |
| 42 | |
| 43 | static inline void le32_to_ptr(uint8_t *p, const uint32_t value) |
| 44 | { |
| 45 | p[0] = (uint8_t)(value); |
| 46 | p[1] = (uint8_t)(value >> 8); |
| 47 | p[2] = (uint8_t)(value >> 16); |
| 48 | p[3] = (uint8_t)(value >> 24); |
| 49 | } |
| 50 | |
| 51 | static inline void le64_to_ptr(uint8_t *p, const uint64_t value) |
| 52 | { |
| 53 | le32_to_ptr(p + 4, (uint32_t)(value >> 32)); |
| 54 | le32_to_ptr(p, (uint32_t)(value)); |
| 55 | } |
| 56 | |
| 57 | /*********************************** Keccak ***********************************/ |
| 58 | /* state[x + y*5] */ |
| 59 | #define A(x, y) (x + 5 * y) |
| 60 | |
| 61 | static inline void jent_keccakp_theta(uint64_t s[25]) |
| 62 | { |
| 63 | uint64_t C[5], D[5]; |
| 64 | |
| 65 | /* Step 1 */ |
| 66 | C[0] = s[A(0, 0)] ^ s[A(0, 1)] ^ s[A(0, 2)] ^ s[A(0, 3)] ^ s[A(0, 4)]; |
| 67 | C[1] = s[A(1, 0)] ^ s[A(1, 1)] ^ s[A(1, 2)] ^ s[A(1, 3)] ^ s[A(1, 4)]; |
| 68 | C[2] = s[A(2, 0)] ^ s[A(2, 1)] ^ s[A(2, 2)] ^ s[A(2, 3)] ^ s[A(2, 4)]; |
| 69 | C[3] = s[A(3, 0)] ^ s[A(3, 1)] ^ s[A(3, 2)] ^ s[A(3, 3)] ^ s[A(3, 4)]; |
| 70 | C[4] = s[A(4, 0)] ^ s[A(4, 1)] ^ s[A(4, 2)] ^ s[A(4, 3)] ^ s[A(4, 4)]; |
| 71 | |
| 72 | /* Step 2 */ |
| 73 | D[0] = C[4] ^ rol64(C[1], 1); |
| 74 | D[1] = C[0] ^ rol64(C[2], 1); |
| 75 | D[2] = C[1] ^ rol64(C[3], 1); |
| 76 | D[3] = C[2] ^ rol64(C[4], 1); |
| 77 | D[4] = C[3] ^ rol64(C[0], 1); |
| 78 | |
| 79 | /* Step 3 */ |
| 80 | s[A(0, 0)] ^= D[0]; |
| 81 | s[A(1, 0)] ^= D[1]; |
| 82 | s[A(2, 0)] ^= D[2]; |
| 83 | s[A(3, 0)] ^= D[3]; |
| 84 | s[A(4, 0)] ^= D[4]; |
| 85 | |
| 86 | s[A(0, 1)] ^= D[0]; |
| 87 | s[A(1, 1)] ^= D[1]; |
| 88 | s[A(2, 1)] ^= D[2]; |
| 89 | s[A(3, 1)] ^= D[3]; |
| 90 | s[A(4, 1)] ^= D[4]; |
| 91 | |
| 92 | s[A(0, 2)] ^= D[0]; |
| 93 | s[A(1, 2)] ^= D[1]; |
| 94 | s[A(2, 2)] ^= D[2]; |
| 95 | s[A(3, 2)] ^= D[3]; |
| 96 | s[A(4, 2)] ^= D[4]; |
| 97 | |
| 98 | s[A(0, 3)] ^= D[0]; |
| 99 | s[A(1, 3)] ^= D[1]; |
| 100 | s[A(2, 3)] ^= D[2]; |
| 101 | s[A(3, 3)] ^= D[3]; |
| 102 | s[A(4, 3)] ^= D[4]; |
| 103 | |
| 104 | s[A(0, 4)] ^= D[0]; |
| 105 | s[A(1, 4)] ^= D[1]; |
| 106 | s[A(2, 4)] ^= D[2]; |
| 107 | s[A(3, 4)] ^= D[3]; |
| 108 | s[A(4, 4)] ^= D[4]; |
| 109 | } |
| 110 | |
| 111 | static inline void jent_keccakp_rho(uint64_t s[25]) |
| 112 | { |
| 113 | /* Step 1 */ |
| 114 | /* s[A(0, 0)] = s[A(0, 0)]; */ |
| 115 | |
| 116 | #define RHO_ROL(t) (((t + 1) * (t + 2) / 2) % 64) |
| 117 | /* Step 3 */ |
| 118 | s[A(1, 0)] = rol64(s[A(1, 0)], RHO_ROL(0)); |
| 119 | s[A(0, 2)] = rol64(s[A(0, 2)], RHO_ROL(1)); |
| 120 | s[A(2, 1)] = rol64(s[A(2, 1)], RHO_ROL(2)); |
| 121 | s[A(1, 2)] = rol64(s[A(1, 2)], RHO_ROL(3)); |
| 122 | s[A(2, 3)] = rol64(s[A(2, 3)], RHO_ROL(4)); |
| 123 | s[A(3, 3)] = rol64(s[A(3, 3)], RHO_ROL(5)); |
| 124 | s[A(3, 0)] = rol64(s[A(3, 0)], RHO_ROL(6)); |
| 125 | s[A(0, 1)] = rol64(s[A(0, 1)], RHO_ROL(7)); |
| 126 | s[A(1, 3)] = rol64(s[A(1, 3)], RHO_ROL(8)); |
| 127 | s[A(3, 1)] = rol64(s[A(3, 1)], RHO_ROL(9)); |
| 128 | s[A(1, 4)] = rol64(s[A(1, 4)], RHO_ROL(10)); |
| 129 | s[A(4, 4)] = rol64(s[A(4, 4)], RHO_ROL(11)); |
| 130 | s[A(4, 0)] = rol64(s[A(4, 0)], RHO_ROL(12)); |
| 131 | s[A(0, 3)] = rol64(s[A(0, 3)], RHO_ROL(13)); |
| 132 | s[A(3, 4)] = rol64(s[A(3, 4)], RHO_ROL(14)); |
| 133 | s[A(4, 3)] = rol64(s[A(4, 3)], RHO_ROL(15)); |
| 134 | s[A(3, 2)] = rol64(s[A(3, 2)], RHO_ROL(16)); |
| 135 | s[A(2, 2)] = rol64(s[A(2, 2)], RHO_ROL(17)); |
| 136 | s[A(2, 0)] = rol64(s[A(2, 0)], RHO_ROL(18)); |
| 137 | s[A(0, 4)] = rol64(s[A(0, 4)], RHO_ROL(19)); |
| 138 | s[A(4, 2)] = rol64(s[A(4, 2)], RHO_ROL(20)); |
| 139 | s[A(2, 4)] = rol64(s[A(2, 4)], RHO_ROL(21)); |
| 140 | s[A(4, 1)] = rol64(s[A(4, 1)], RHO_ROL(22)); |
| 141 | s[A(1, 1)] = rol64(s[A(1, 1)], RHO_ROL(23)); |
| 142 | } |
| 143 | |
| 144 | static inline void jent_keccakp_pi(uint64_t s[25]) |
| 145 | { |
| 146 | uint64_t t = s[A(4, 4)]; |
| 147 | |
| 148 | /* Step 1 */ |
| 149 | /* s[A(0, 0)] = s[A(0, 0)]; */ |
| 150 | s[A(4, 4)] = s[A(1, 4)]; |
| 151 | s[A(1, 4)] = s[A(3, 1)]; |
| 152 | s[A(3, 1)] = s[A(1, 3)]; |
| 153 | s[A(1, 3)] = s[A(0, 1)]; |
| 154 | s[A(0, 1)] = s[A(3, 0)]; |
| 155 | s[A(3, 0)] = s[A(3, 3)]; |
| 156 | s[A(3, 3)] = s[A(2, 3)]; |
| 157 | s[A(2, 3)] = s[A(1, 2)]; |
| 158 | s[A(1, 2)] = s[A(2, 1)]; |
| 159 | s[A(2, 1)] = s[A(0, 2)]; |
| 160 | s[A(0, 2)] = s[A(1, 0)]; |
| 161 | s[A(1, 0)] = s[A(1, 1)]; |
| 162 | s[A(1, 1)] = s[A(4, 1)]; |
| 163 | s[A(4, 1)] = s[A(2, 4)]; |
| 164 | s[A(2, 4)] = s[A(4, 2)]; |
| 165 | s[A(4, 2)] = s[A(0, 4)]; |
| 166 | s[A(0, 4)] = s[A(2, 0)]; |
| 167 | s[A(2, 0)] = s[A(2, 2)]; |
| 168 | s[A(2, 2)] = s[A(3, 2)]; |
| 169 | s[A(3, 2)] = s[A(4, 3)]; |
| 170 | s[A(4, 3)] = s[A(3, 4)]; |
| 171 | s[A(3, 4)] = s[A(0, 3)]; |
| 172 | s[A(0, 3)] = s[A(4, 0)]; |
| 173 | s[A(4, 0)] = t; |
| 174 | } |
| 175 | |
| 176 | static inline void jent_keccakp_chi(uint64_t s[25]) |
| 177 | { |
| 178 | uint64_t t0[5], t1[5]; |
| 179 | |
| 180 | t0[0] = s[A(0, 0)]; |
| 181 | t0[1] = s[A(0, 1)]; |
| 182 | t0[2] = s[A(0, 2)]; |
| 183 | t0[3] = s[A(0, 3)]; |
| 184 | t0[4] = s[A(0, 4)]; |
| 185 | |
| 186 | t1[0] = s[A(1, 0)]; |
| 187 | t1[1] = s[A(1, 1)]; |
| 188 | t1[2] = s[A(1, 2)]; |
| 189 | t1[3] = s[A(1, 3)]; |
| 190 | t1[4] = s[A(1, 4)]; |
| 191 | |
| 192 | s[A(0, 0)] ^= ~s[A(1, 0)] & s[A(2, 0)]; |
| 193 | s[A(0, 1)] ^= ~s[A(1, 1)] & s[A(2, 1)]; |
| 194 | s[A(0, 2)] ^= ~s[A(1, 2)] & s[A(2, 2)]; |
| 195 | s[A(0, 3)] ^= ~s[A(1, 3)] & s[A(2, 3)]; |
| 196 | s[A(0, 4)] ^= ~s[A(1, 4)] & s[A(2, 4)]; |
| 197 | |
| 198 | s[A(1, 0)] ^= ~s[A(2, 0)] & s[A(3, 0)]; |
| 199 | s[A(1, 1)] ^= ~s[A(2, 1)] & s[A(3, 1)]; |
| 200 | s[A(1, 2)] ^= ~s[A(2, 2)] & s[A(3, 2)]; |
| 201 | s[A(1, 3)] ^= ~s[A(2, 3)] & s[A(3, 3)]; |
| 202 | s[A(1, 4)] ^= ~s[A(2, 4)] & s[A(3, 4)]; |
| 203 | |
| 204 | s[A(2, 0)] ^= ~s[A(3, 0)] & s[A(4, 0)]; |
| 205 | s[A(2, 1)] ^= ~s[A(3, 1)] & s[A(4, 1)]; |
| 206 | s[A(2, 2)] ^= ~s[A(3, 2)] & s[A(4, 2)]; |
| 207 | s[A(2, 3)] ^= ~s[A(3, 3)] & s[A(4, 3)]; |
| 208 | s[A(2, 4)] ^= ~s[A(3, 4)] & s[A(4, 4)]; |
| 209 | |
| 210 | s[A(3, 0)] ^= ~s[A(4, 0)] & t0[0]; |
| 211 | s[A(3, 1)] ^= ~s[A(4, 1)] & t0[1]; |
| 212 | s[A(3, 2)] ^= ~s[A(4, 2)] & t0[2]; |
| 213 | s[A(3, 3)] ^= ~s[A(4, 3)] & t0[3]; |
| 214 | s[A(3, 4)] ^= ~s[A(4, 4)] & t0[4]; |
| 215 | |
| 216 | s[A(4, 0)] ^= ~t0[0] & t1[0]; |
| 217 | s[A(4, 1)] ^= ~t0[1] & t1[1]; |
| 218 | s[A(4, 2)] ^= ~t0[2] & t1[2]; |
| 219 | s[A(4, 3)] ^= ~t0[3] & t1[3]; |
| 220 | s[A(4, 4)] ^= ~t0[4] & t1[4]; |
| 221 | } |
| 222 | |
| 223 | static const uint64_t jent_keccakp_iota_vals[] = { |
| 224 | 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL, |
| 225 | 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL, |
| 226 | 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL, |
| 227 | 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL, |
| 228 | 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, |
| 229 | 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL, |
| 230 | 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL, |
| 231 | 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL |
| 232 | }; |
| 233 | |
| 234 | static inline void jent_keccakp_iota(uint64_t s[25], unsigned int round) |
| 235 | { |
| 236 | s[0] ^= jent_keccakp_iota_vals[round]; |
| 237 | } |
| 238 | |
| 239 | static inline void jent_keccakp_1600(uint64_t s[25]) |
| 240 | { |
| 241 | unsigned int round; |
| 242 | |
| 243 | for (round = 0; round < 24; round++) { |
| 244 | jent_keccakp_theta(s); |
| 245 | jent_keccakp_rho(s); |
| 246 | jent_keccakp_pi(s); |
| 247 | jent_keccakp_chi(s); |
| 248 | jent_keccakp_iota(s, round); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /*********************************** SHA-3 ************************************/ |
| 253 | |
| 254 | static inline void jent_sha3_init(struct jent_sha_ctx *ctx) |
| 255 | { |
| 256 | unsigned int i; |
| 257 | |
| 258 | for (i = 0; i < 25; i++) |
| 259 | ctx->state[i] = 0; |
| 260 | ctx->msg_len = 0; |
| 261 | } |
| 262 | |
| 263 | void jent_sha3_256_init(struct jent_sha_ctx *ctx) |
| 264 | { |
| 265 | jent_sha3_init(ctx); |
| 266 | |
| 267 | ctx->r = JENT_SHA3_256_SIZE_BLOCK; |
| 268 | ctx->rword = JENT_SHA3_256_SIZE_BLOCK / sizeof(uint64_t); |
| 269 | ctx->digestsize = JENT_SHA3_256_SIZE_DIGEST; |
| 270 | ctx->padding = 0x06; |
| 271 | } |
| 272 | |
| 273 | void jent_shake256_init(struct jent_sha_ctx *ctx) |
| 274 | { |
| 275 | jent_sha3_init(ctx); |
| 276 | |
| 277 | ctx->r = JENT_SHA3_256_SIZE_BLOCK; |
| 278 | ctx->rword = JENT_SHA3_256_SIZE_BLOCK / sizeof(uint64_t); |
| 279 | ctx->digestsize = 0; |
| 280 | ctx->padding = 0x1f; |
| 281 | ctx->initially_seeded = 0; |
| 282 | } |
| 283 | |
| 284 | static inline void jent_sha3_fill_state(struct jent_sha_ctx *ctx, |
| 285 | const uint8_t *in) |
| 286 | { |
| 287 | unsigned int i; |
| 288 | |
| 289 | for (i = 0; i < ctx->rword; i++) { |
| 290 | ctx->state[i] ^= ptr_to_le64(in); |
| 291 | in += 8; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void jent_sha3_update(struct jent_sha_ctx *ctx, const uint8_t *in, size_t inlen) |
| 296 | { |
| 297 | size_t partial = ctx->msg_len % ctx->r; |
| 298 | |
| 299 | ctx->msg_len += inlen; |
| 300 | |
| 301 | /* Sponge absorbing phase */ |
| 302 | |
| 303 | /* Check if we have a partial block stored */ |
| 304 | if (partial) { |
| 305 | size_t todo = ctx->r - partial; |
| 306 | |
| 307 | /* |
| 308 | * If the provided data is small enough to fit in the partial |
| 309 | * buffer, copy it and leave it unprocessed. |
| 310 | */ |
| 311 | if (inlen < todo) { |
| 312 | memcpy(ctx->partial + partial, in, inlen); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * The input data is large enough to fill the entire partial |
| 318 | * block buffer. Thus, we fill it and transform it. |
| 319 | */ |
| 320 | memcpy(ctx->partial + partial, in, todo); |
| 321 | inlen -= todo; |
| 322 | in += todo; |
| 323 | |
| 324 | jent_sha3_fill_state(ctx, ctx->partial); |
| 325 | jent_keccakp_1600(ctx->state); |
| 326 | } |
| 327 | |
| 328 | /* Perform a transformation of full block-size messages */ |
| 329 | for (; inlen >= ctx->r; inlen -= ctx->r, in += ctx->r) { |
| 330 | jent_sha3_fill_state(ctx, in); |
| 331 | jent_keccakp_1600(ctx->state); |
| 332 | } |
| 333 | |
| 334 | /* If we have data left, copy it into the partial block buffer */ |
| 335 | memcpy(ctx->partial, in, inlen); |
| 336 | } |
| 337 | |
| 338 | void jent_sha3_final(struct jent_sha_ctx *ctx, uint8_t *digest) |
| 339 | { |
| 340 | size_t partial = ctx->msg_len % ctx->r; |
| 341 | unsigned int i; |
| 342 | |
| 343 | /* Final round in sponge absorbing phase */ |
| 344 | |
| 345 | /* Fill the unused part of the partial buffer with zeros */ |
| 346 | memset(ctx->partial + partial, 0, ctx->r - partial); |
| 347 | |
| 348 | /* |
| 349 | * Add the leading and trailing bit as well as the 01 bits for the |
| 350 | * SHA-3 suffix. |
| 351 | */ |
| 352 | ctx->partial[partial] = ctx->padding; |
| 353 | ctx->partial[ctx->r - 1] |= 0x80; |
| 354 | |
| 355 | /* Final transformation */ |
| 356 | jent_sha3_fill_state(ctx, ctx->partial); |
| 357 | jent_keccakp_1600(ctx->state); |
| 358 | |
| 359 | /* |
| 360 | * Sponge squeeze phase - This squeeze implementation is deliberately |
| 361 | * short for the use in XDRBG-like constructions as it has the following |
| 362 | * caveats compared to a general-purpose squeeze: |
| 363 | * |
| 364 | * * the digest size is always smaller than the rate size r as we do not |
| 365 | * have a loop around the jent_keccakp_1600 / copy out loop below |
| 366 | * |
| 367 | * * the requested digest size must be multiples of uint64_t |
| 368 | */ |
| 369 | for (i = 0; i < ctx->digestsize / 8; i++, digest += 8) |
| 370 | le64_to_ptr(digest, ctx->state[i]); |
| 371 | |
| 372 | memset(ctx->partial, 0, ctx->r); |
| 373 | jent_sha3_init(ctx); |
| 374 | } |
| 375 | |
| 376 | int jent_sha3_alloc(void **hash_state) |
| 377 | { |
| 378 | struct sha_ctx *tmp; |
| 379 | |
| 380 | tmp = jent_zalloc(JENT_SHA_MAX_CTX_SIZE); |
| 381 | if (!tmp) |
| 382 | return 1; |
| 383 | |
| 384 | *hash_state = tmp; |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | void jent_sha3_dealloc(void *hash_state) |
| 390 | { |
| 391 | struct sha_ctx *ctx = (struct sha_ctx *)hash_state; |
| 392 | |
| 393 | jent_zfree(ctx, JENT_SHA_MAX_CTX_SIZE); |
| 394 | } |
| 395 | |
| 396 | /*********************************** XDRBG ************************************/ |
| 397 | |
| 398 | #define JENT_XDRBG_DRNG_ENCODE_N(x) ((x) * 85) |
| 399 | |
| 400 | /* |
| 401 | * This operation implements XDRBG-256 as defined in [1]. |
| 402 | * |
| 403 | * The output size is [0:256] bits. |
| 404 | * |
| 405 | * [1] https://leancrypto.org/papers/xdrbg.pdf |
| 406 | */ |
| 407 | static void jent_xdrbg256_generate_block(struct jent_sha_ctx *ctx, uint8_t *dst, |
| 408 | size_t dst_len) |
| 409 | { |
| 410 | /* |
| 411 | * XDRBG: |
| 412 | * 512 Bit for next state (internal memory) || 256 Bit output for user |
| 413 | */ |
| 414 | uint8_t jent_block_next_state[JENT_XDRBG_SIZE_STATE + |
| 415 | JENT_SHA3_256_SIZE_DIGEST]; |
| 416 | uint8_t encode; |
| 417 | |
| 418 | /* Checking the output size */ |
| 419 | BUILD_BUG_ON(JENT_SHA3_256_SIZE_DIGEST != ((DATA_SIZE_BITS / 8))); |
| 420 | /* |
| 421 | * The XOF implementation only allows the generation of up to one |
| 422 | * rate-size block. See the comments in the squeeze operation for |
| 423 | * details |
| 424 | */ |
| 425 | BUILD_BUG_ON(JENT_SHA3_256_SIZE_BLOCK < sizeof(jent_block_next_state)); |
| 426 | /* |
| 427 | * The squeeze operation is limited to return multiples of uint64_t - |
| 428 | * verify all set_digestsize values. |
| 429 | */ |
| 430 | BUILD_BUG_ON(JENT_XDRBG_SIZE_STATE % sizeof(uint64_t)); |
| 431 | BUILD_BUG_ON(sizeof(jent_block_next_state) % sizeof(uint64_t)); |
| 432 | |
| 433 | /* The final operation automatically re-initializes the ->hash_state */ |
| 434 | |
| 435 | /* |
| 436 | * XDRBG: finalize seeding |
| 437 | * |
| 438 | * seed is inserted with SHAKE-256 update |
| 439 | * |
| 440 | * initial seeding: |
| 441 | * V ← XOF( encode(( seed ), α, 0), |V| ) |
| 442 | * |
| 443 | * reseeding: |
| 444 | * V ← XOF( encode(( V' || seed ), α, 1), |V| ) |
| 445 | * |
| 446 | * The insertion of the V' is done at the end of this function for the |
| 447 | * next finalization of the reseeding. α is defined to be empty. |
| 448 | */ |
| 449 | encode = JENT_XDRBG_DRNG_ENCODE_N(ctx->initially_seeded); |
| 450 | ctx->initially_seeded = 1; |
| 451 | jent_sha3_update(ctx, &encode, 1); |
| 452 | jent_shake256_set_digestsize(ctx, JENT_XDRBG_SIZE_STATE); |
| 453 | jent_sha3_final(ctx, jent_block_next_state); |
| 454 | |
| 455 | /* |
| 456 | * XDRBG: generate |
| 457 | * |
| 458 | * ℓ = dst_len which is at maximum 256 bits |
| 459 | * T ← XOF( encode(V', α, 2), ℓ + |V| ) |
| 460 | * V ← first |V| bits of T |
| 461 | * Σ ← last ℓ bits of T |
| 462 | */ |
| 463 | jent_sha3_update(ctx, jent_block_next_state, JENT_XDRBG_SIZE_STATE); |
| 464 | encode = JENT_XDRBG_DRNG_ENCODE_N(2); |
| 465 | jent_sha3_update(ctx, &encode, 1); |
| 466 | /* |
| 467 | * Request a full block irrespective of the output size due to |
| 468 | * Keccak squeeze implementation limitation. |
| 469 | */ |
| 470 | jent_shake256_set_digestsize(ctx, sizeof(jent_block_next_state)); |
| 471 | jent_sha3_final(ctx, jent_block_next_state); |
| 472 | |
| 473 | /* Return Σ to the caller truncated to the requested size */ |
| 474 | if (dst_len) { |
| 475 | /* Safety measure to not overflow the generated buffer */ |
| 476 | if (dst_len > JENT_SHA3_256_SIZE_DIGEST) |
| 477 | dst_len = JENT_SHA3_256_SIZE_DIGEST; |
| 478 | |
| 479 | memcpy(dst, jent_block_next_state + JENT_XDRBG_SIZE_STATE, |
| 480 | dst_len); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * XDRBG: reseed |
| 485 | * Set the V into the state. |
| 486 | */ |
| 487 | jent_sha3_update(ctx, jent_block_next_state, JENT_XDRBG_SIZE_STATE); |
| 488 | jent_memset_secure(jent_block_next_state, |
| 489 | sizeof(jent_block_next_state)); |
| 490 | } |
| 491 | |
| 492 | void jent_drbg_generate_block(struct jent_sha_ctx *ctx, uint8_t *dst, |
| 493 | size_t dst_len) |
| 494 | { |
| 495 | jent_xdrbg256_generate_block(ctx, dst, dst_len); |
| 496 | } |
| 497 | |
| 498 | /********************************** Selftest **********************************/ |
| 499 | |
| 500 | /* |
| 501 | * The SHAKE-256 support is only needed to support the XDRBG. Therefore, it is |
| 502 | * implicitly self-tested with the XDRBG-256 self test. Yet, this self-test |
| 503 | * code for SHAKE-256 is left in here to allow implementors to activate it at |
| 504 | * their discretion. Furthermore it provides an example how to invoke the |
| 505 | * Keccak operation as a SHAKE-256 for testing and analysis. |
| 506 | */ |
| 507 | #if 0 |
| 508 | static int jent_shake256_tester(void) |
| 509 | { |
| 510 | HASH_CTX_ON_STACK(ctx); |
| 511 | static const uint8_t msg[] = { 0x6C, 0x9E, 0xC8, 0x5C, 0xBA, 0xBA, 0x62, |
| 512 | 0xF5, 0xBC, 0xFE, 0xA1, 0x9E, 0xB9, 0xC9, |
| 513 | 0x20, 0x52, 0xD8, 0xFF, 0x18, 0x81, 0x52, |
| 514 | 0xE9, 0x61, 0xC1, 0xEC, 0x5C, 0x75, 0xBF, |
| 515 | 0xC3, 0xC9, 0x1C, 0x8D }; |
| 516 | static const uint8_t exp[] = { 0x7d, 0x6a, 0x09, 0x6e, 0x13, 0x66, 0x1d, |
| 517 | 0x9d, 0x0e, 0xca, 0xf5, 0x38, 0x30, 0xa1, |
| 518 | 0x92, 0x87, 0xe0, 0xb3, 0x6e, 0xce, 0x48, |
| 519 | 0x82, 0xeb, 0x58, 0x0b, 0x78, 0x5c, 0x1d, |
| 520 | 0xef, 0x2d, 0xe5, 0xaa, 0x6c }; |
| 521 | uint8_t act[sizeof(exp)] = { 0 }; |
| 522 | unsigned int i; |
| 523 | |
| 524 | jent_shake256_init(&ctx); |
| 525 | jent_sha3_update(&ctx, msg, sizeof(msg)); |
| 526 | jent_shake256_set_digestsize(&ctx, sizeof(exp)); |
| 527 | jent_sha3_final(&ctx, act); |
| 528 | |
| 529 | for (i = 0; i < sizeof(exp); i++) { |
| 530 | if (exp[i] != act[i]) |
| 531 | return 1; |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | #endif |
| 537 | |
| 538 | static int jent_xdrbg256_tester(void) |
| 539 | { |
| 540 | HASH_CTX_ON_STACK(ctx); |
| 541 | /* |
| 542 | * Test vectors are generated using the leancrypto XDRBG implementation. |
| 543 | */ |
| 544 | static const uint8_t seed[] = { |
| 545 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 546 | }; |
| 547 | static const uint8_t exp[] = { |
| 548 | 0x51, 0xe4, 0x3c, 0xf6, 0x4b, 0xa2, 0x80, 0x77, |
| 549 | 0x33, 0x1a, 0x47, 0xe3, 0xf8, 0xb4, 0x1a, 0x42, |
| 550 | 0xad, 0xd3, 0xa0, 0xf2, 0x53, 0x97, 0x10, 0xdd, |
| 551 | 0x6e, 0xa1, 0x16, 0x1d, 0x37, 0x8a, 0x6f, 0xb6 |
| 552 | }; |
| 553 | uint8_t act[sizeof(exp)] = { 0 }; |
| 554 | unsigned int i; |
| 555 | |
| 556 | BUILD_BUG_ON(JENT_SHA3_256_SIZE_DIGEST != sizeof(exp)); |
| 557 | |
| 558 | jent_shake256_init(&ctx); |
| 559 | /* Initial seed */ |
| 560 | jent_sha3_update(&ctx, seed, sizeof(seed)); |
| 561 | jent_xdrbg256_generate_block(&ctx, act, sizeof(act)); |
| 562 | /* Reseeding */ |
| 563 | jent_sha3_update(&ctx, seed, sizeof(seed)); |
| 564 | jent_xdrbg256_generate_block(&ctx, act, sizeof(act)); |
| 565 | |
| 566 | for (i = 0; i < sizeof(exp); i++) { |
| 567 | if (exp[i] != act[i]) |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static int jent_sha3_256_tester(void) |
| 575 | { |
| 576 | HASH_CTX_ON_STACK(ctx); |
| 577 | static const uint8_t msg[] = { 0x5E, 0x5E, 0xD6 }; |
| 578 | static const uint8_t exp[] = { |
| 579 | 0xF1, 0x6E, 0x66, 0xC0, 0x43, 0x72, 0xB4, 0xA3, 0xE1, 0xE3, |
| 580 | 0x2E, 0x07, 0xC4, 0x1C, 0x03, 0x40, 0x8A, 0xD5, 0x43, 0x86, |
| 581 | 0x8C, 0xC4, 0x0E, 0xC5, 0x5E, 0x00, 0xBB, 0xBB, 0xBD, 0xF5, |
| 582 | 0x91, 0x1E |
| 583 | }; |
| 584 | uint8_t act[sizeof(exp)] = { 0 }; |
| 585 | unsigned int i; |
| 586 | |
| 587 | jent_sha3_256_init(&ctx); |
| 588 | jent_sha3_update(&ctx, msg, sizeof(msg)); |
| 589 | jent_sha3_final(&ctx, act); |
| 590 | |
| 591 | for (i = 0; i < sizeof(exp); i++) { |
| 592 | if (exp[i] != act[i]) |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | int jent_sha3_tester(void) |
| 600 | { |
| 601 | if (jent_sha3_256_tester()) |
| 602 | return 1; |
| 603 | return jent_xdrbg256_tester(); |
| 604 | } |