From bde05c9a0b4bbc902a889d82eb6f9913a1181117 Mon Sep 17 00:00:00 2001 From: "Thing-han, Lim" <15379156+potsrevennil@users.noreply.github.com> Date: Thu, 28 Mar 2024 14:56:20 +0800 Subject: [PATCH] extract notrandombytes into a separate file Signed-off-by: Thing-han, Lim <15379156+potsrevennil@users.noreply.github.com> --- hal/notrandombytes.c | 86 ++++++++++++++++++++++++++++++++++++++++ hal/randombytes.c | 94 -------------------------------------------- mk/mps2-an386.mk | 2 +- 3 files changed, 87 insertions(+), 95 deletions(-) create mode 100644 hal/notrandombytes.c diff --git a/hal/notrandombytes.c b/hal/notrandombytes.c new file mode 100644 index 0000000..b2843ac --- /dev/null +++ b/hal/notrandombytes.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: Apache-2.0 +#include "randombytes.h" +#include + +static uint32_t seed[32] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, + 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5 + }; +static uint32_t in[12]; +static uint8_t out_buf[sizeof(uint32_t) * 16]; +static int32_t outleft = 0; + +#define ROTATE(x, b) (((x) << (b)) | ((x) >> (32 - (b)))) +#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x, b)); + +static void surf(uint32_t out[8]) { + uint32_t t[12]; + uint32_t x; + uint32_t sum = 0; + int32_t r; + int32_t i; + int32_t loop; + + for (i = 0; i < 12; ++i) { + t[i] = in[i] ^ seed[12 + i]; + } + for (i = 0; i < 8; ++i) { + out[i] = seed[24 + i]; + } + x = t[11]; + for (loop = 0; loop < 2; ++loop) { + for (r = 0; r < 16; ++r) { + sum += 0x9e3779b9; + MUSH(0, 5) + MUSH(1, 7) + MUSH(2, 9) + MUSH(3, 13) + MUSH(4, 5) + MUSH(5, 7) + MUSH(6, 9) + MUSH(7, 13) + MUSH(8, 5) + MUSH(9, 7) + MUSH(10, 9) + MUSH(11, 13) + } + for (i = 0; i < 8; ++i) { + out[i] ^= t[i + 4]; + } + } +} + +void randombytes_regen(void); +void randombytes_regen(void) { + uint32_t out[8]; + if (!++in[0]) { + if (!++in[1]) { + if (!++in[2]) { + ++in[3]; + } + } + } + surf(out); + memcpy(out_buf, out, sizeof(out)); + if (!++in[0]) { + if (!++in[1]) { + if (!++in[2]) { + ++in[3]; + } + } + } + surf(out); + memcpy(out_buf + sizeof(out), out, sizeof(out)); + outleft = sizeof(out_buf); +} + +int randombytes(uint8_t *buf, size_t xlen) { + while (xlen > 0) { + if (!outleft) { + randombytes_regen(); + } + *buf = out_buf[--outleft]; + ++buf; + --xlen; + } + return 0; +} diff --git a/hal/randombytes.c b/hal/randombytes.c index 2f67a84..0a42512 100644 --- a/hal/randombytes.c +++ b/hal/randombytes.c @@ -1,9 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 #include "randombytes.h" -#if defined(STM32F2) || defined(STM32F4) || \ -defined(STM32L4R5ZI) && !defined(MPS2_AN386) - #include int randombytes(uint8_t *obuf, size_t len) { @@ -28,94 +25,3 @@ int randombytes(uint8_t *obuf, size_t len) { return 0; } - -#else - -#warning Using a non-random randombytes - -#include - -static uint32_t seed[32] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, - 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5 - }; -static uint32_t in[12]; -static uint8_t out_buf[sizeof(uint32_t) * 16]; -static int32_t outleft = 0; - -#define ROTATE(x, b) (((x) << (b)) | ((x) >> (32 - (b)))) -#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x, b)); - -static void surf(uint32_t out[8]) { - uint32_t t[12]; - uint32_t x; - uint32_t sum = 0; - int32_t r; - int32_t i; - int32_t loop; - - for (i = 0; i < 12; ++i) { - t[i] = in[i] ^ seed[12 + i]; - } - for (i = 0; i < 8; ++i) { - out[i] = seed[24 + i]; - } - x = t[11]; - for (loop = 0; loop < 2; ++loop) { - for (r = 0; r < 16; ++r) { - sum += 0x9e3779b9; - MUSH(0, 5) - MUSH(1, 7) - MUSH(2, 9) - MUSH(3, 13) - MUSH(4, 5) - MUSH(5, 7) - MUSH(6, 9) - MUSH(7, 13) - MUSH(8, 5) - MUSH(9, 7) - MUSH(10, 9) - MUSH(11, 13) - } - for (i = 0; i < 8; ++i) { - out[i] ^= t[i + 4]; - } - } -} - -void randombytes_regen(void); -void randombytes_regen(void) { - uint32_t out[8]; - if (!++in[0]) { - if (!++in[1]) { - if (!++in[2]) { - ++in[3]; - } - } - } - surf(out); - memcpy(out_buf, out, sizeof(out)); - if (!++in[0]) { - if (!++in[1]) { - if (!++in[2]) { - ++in[3]; - } - } - } - surf(out); - memcpy(out_buf + sizeof(out), out, sizeof(out)); - outleft = sizeof(out_buf); -} - -int randombytes(uint8_t *buf, size_t xlen) { - while (xlen > 0) { - if (!outleft) { - randombytes_regen(); - } - *buf = out_buf[--outleft]; - ++buf; - --xlen; - } - return 0; -} - -#endif diff --git a/mk/mps2-an386.mk b/mk/mps2-an386.mk index e00ba78..7513b2b 100644 --- a/mk/mps2-an386.mk +++ b/mk/mps2-an386.mk @@ -39,7 +39,7 @@ LDFLAGS += \ LIBHAL_SRC := \ hal/mps2/startup_MPS2.S \ hal/hal-mps2.c \ - hal/randombytes.c + hal/notrandombytes.c obj/libpqm4hal.a: $(call objs,$(LIBHAL_SRC)) obj/libpqm4hal.a: CPPFLAGS += -Ihal/mps2