| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* SPDX-License-Identifier: LicenseRef-PD-hp OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/* References |
| 7 |
* ========== |
| 8 |
* |
| 9 |
* - [surf] |
| 10 |
* SURF: Simple Unpredictable Random Function |
| 11 |
* Daniel J. Bernstein |
| 12 |
* https://cr.yp.to/papers.html#surf |
| 13 |
*/ |
| 14 |
|
| 15 |
/* * Based on @[surf]*/ |
| 16 |
|
| 17 |
/** |
| 18 |
* WARNING |
| 19 |
* |
| 20 |
* The randombytes() implementation in this file is for TESTING ONLY. |
| 21 |
* You MUST NOT use this implementation outside of testing. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <stdint.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include "notrandombytes.h" |
| 29 |
|
| 30 |
#ifdef ENABLE_CT_TESTING |
| 31 |
#include <valgrind/memcheck.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
static uint32_t seed[32] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, |
| 35 |
2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5}; |
| 36 |
static uint32_t in[12]; |
| 37 |
static uint32_t out[8]; |
| 38 |
static int32_t outleft = 0; |
| 39 |
|
| 40 |
void randombytes_reset(void) |
| 41 |
{ |
| 42 |
memset(in, 0, sizeof(in)); |
| 43 |
memset(out, 0, sizeof(out)); |
| 44 |
outleft = 0; |
| 45 |
} |
| 46 |
|
| 47 |
#define ROTATE(x, b) (((x) << (b)) | ((x) >> (32 - (b)))) |
| 48 |
#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x, b)); |
| 49 |
|
| 50 |
static void surf(void) |
| 51 |
{ |
| 52 |
uint32_t t[12]; |
| 53 |
uint32_t x; |
| 54 |
uint32_t sum = 0; |
| 55 |
int32_t r; |
| 56 |
int32_t i; |
| 57 |
int32_t loop; |
| 58 |
|
| 59 |
for (i = 0; i < 12; ++i) |
| 60 |
{ |
| 61 |
t[i] = in[i] ^ seed[12 + i]; |
| 62 |
} |
| 63 |
for (i = 0; i < 8; ++i) |
| 64 |
{ |
| 65 |
out[i] = seed[24 + i]; |
| 66 |
} |
| 67 |
x = t[11]; |
| 68 |
for (loop = 0; loop < 2; ++loop) |
| 69 |
{ |
| 70 |
for (r = 0; r < 16; ++r) |
| 71 |
{ |
| 72 |
sum += 0x9e3779b9; |
| 73 |
MUSH(0, 5) |
| 74 |
MUSH(1, 7) |
| 75 |
MUSH(2, 9) |
| 76 |
MUSH(3, 13) |
| 77 |
MUSH(4, 5) |
| 78 |
MUSH(5, 7) |
| 79 |
MUSH(6, 9) |
| 80 |
MUSH(7, 13) |
| 81 |
MUSH(8, 5) |
| 82 |
MUSH(9, 7) |
| 83 |
MUSH(10, 9) |
| 84 |
MUSH(11, 13) |
| 85 |
} |
| 86 |
for (i = 0; i < 8; ++i) |
| 87 |
{ |
| 88 |
out[i] ^= t[i + 4]; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
int randombytes(uint8_t *buf, size_t n) |
| 94 |
{ |
| 95 |
#ifdef ENABLE_CT_TESTING |
| 96 |
uint8_t *buf_orig = buf; |
| 97 |
size_t n_orig = n; |
| 98 |
#endif |
| 99 |
|
| 100 |
while (n > 0) |
| 101 |
{ |
| 102 |
if (!outleft) |
| 103 |
{ |
| 104 |
if (!++in[0]) |
| 105 |
{ |
| 106 |
if (!++in[1]) |
| 107 |
{ |
| 108 |
if (!++in[2]) |
| 109 |
{ |
| 110 |
++in[3]; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
surf(); |
| 115 |
outleft = 8; |
| 116 |
} |
| 117 |
*buf = (uint8_t)out[--outleft]; |
| 118 |
++buf; |
| 119 |
--n; |
| 120 |
} |
| 121 |
|
| 122 |
#ifdef ENABLE_CT_TESTING |
| 123 |
/* |
| 124 |
* Mark all randombytes output as secret (undefined). |
| 125 |
* Valgrind will propagate this to everything derived from it. |
| 126 |
*/ |
| 127 |
VALGRIND_MAKE_MEM_UNDEFINED(buf_orig, n_orig); |
| 128 |
#endif /* ENABLE_CT_TESTING */ |
| 129 |
return 0; |
| 130 |
} |