From 4e2c7b2b070699b9f0a9cfe75de8316866cc5ed1 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Thu, 31 Mar 2022 13:17:48 +0200 Subject: random numbers for fe310 --- ecp/src/platform/fe310/features.mk | 3 +++ ecp/src/platform/posix/features.mk | 7 ++++++ ext/libressl/crypto/compat/arc4random_fe310.h | 4 +++- fw/esp32/components/eos/include/rng.h | 1 + fw/esp32/components/eos/rng.c | 31 +++++++++++++++++++++++++++ fw/fe310/eos/rng.c | 27 +++++++++++++++++++++++ fw/fe310/eos/rng.h | 5 +++++ 7 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 ecp/src/platform/fe310/features.mk create mode 100644 ecp/src/platform/posix/features.mk create mode 100644 fw/esp32/components/eos/include/rng.h create mode 100644 fw/esp32/components/eos/rng.c create mode 100644 fw/fe310/eos/rng.c create mode 100644 fw/fe310/eos/rng.h diff --git a/ecp/src/platform/fe310/features.mk b/ecp/src/platform/fe310/features.mk new file mode 100644 index 0000000..39f95cf --- /dev/null +++ b/ecp/src/platform/fe310/features.mk @@ -0,0 +1,3 @@ +with_vconn = yes +with_dir = yes +with_debug = yes diff --git a/ecp/src/platform/posix/features.mk b/ecp/src/platform/posix/features.mk new file mode 100644 index 0000000..2ce8220 --- /dev/null +++ b/ecp/src/platform/posix/features.mk @@ -0,0 +1,7 @@ +with_pthread = yes +with_htable = yes +with_vconn = yes +with_dirsrv = yes +with_rbuf = yes +with_msgq = yes +with_debug = yes diff --git a/ext/libressl/crypto/compat/arc4random_fe310.h b/ext/libressl/crypto/compat/arc4random_fe310.h index 131b0d3..ddfa1ab 100644 --- a/ext/libressl/crypto/compat/arc4random_fe310.h +++ b/ext/libressl/crypto/compat/arc4random_fe310.h @@ -20,8 +20,10 @@ _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) return arc4random_alloc((void **)rsp, sizeof(**rsp), (void **)rsxp, sizeof(**rsxp)); } -void arc4random_close(void) +void arc4random_close(void **rsp, void **rsxp) { + *rsp = rs; + *rsxp = rsx; rs = NULL; rsx = NULL; } \ No newline at end of file diff --git a/fw/esp32/components/eos/include/rng.h b/fw/esp32/components/eos/include/rng.h new file mode 100644 index 0000000..0690973 --- /dev/null +++ b/fw/esp32/components/eos/include/rng.h @@ -0,0 +1 @@ +void eos_rng_init(void); diff --git a/fw/esp32/components/eos/rng.c b/fw/esp32/components/eos/rng.c new file mode 100644 index 0000000..79eca21 --- /dev/null +++ b/fw/esp32/components/eos/rng.c @@ -0,0 +1,31 @@ +#include +#include +#include + +#include "net.h" +#include "rng.h" + +static const char *TAG = "EOS RNG"; + +static void rng_handler(unsigned char type, unsigned char *buffer, uint16_t buf_len) { + uint16_t rng_len = 0; + + if (buf_len < sizeof(uint16_t)) goto rng_handler_fin; + + rng_len = (uint16_t)buffer[0] << 8; + rng_len |= (uint16_t)buffer[1]; + if (rng_len > EOS_NET_MTU) { + rng_len = 0; + goto rng_handler_fin; + } + esp_fill_random(buffer, rng_len); + +rng_handler_fin: + eos_net_reply(EOS_NET_MTYPE_RNG, buffer, rng_len); +} + +void eos_rng_init(void) { + eos_net_set_handler(EOS_NET_MTYPE_RNG, rng_handler); + ESP_LOGI(TAG, "INIT"); +} + diff --git a/fw/fe310/eos/rng.c b/fw/fe310/eos/rng.c new file mode 100644 index 0000000..317941d --- /dev/null +++ b/fw/fe310/eos/rng.c @@ -0,0 +1,27 @@ +#include +#include +#include + +#include "net.h" + +int getentropy(unsigned char *b, size_t sz) { + unsigned char type; + unsigned char *buffer; + uint16_t len; + int rv; + + buffer = eos_net_alloc(); + + type = EOS_NET_MTYPE_RNG; + len = sizeof(uint16_t); + buffer[0] = sz >> 8; + buffer[1] = sz; + + rv = eos_net_xchg(&type, buffer, &len); + if (rv || (len != sz)) rv = -1; + + if (!rv) memcpy(b, buffer, sz); + eos_net_free(buffer, 1); + + return rv; +} diff --git a/fw/fe310/eos/rng.h b/fw/fe310/eos/rng.h new file mode 100644 index 0000000..27bbf74 --- /dev/null +++ b/fw/fe310/eos/rng.h @@ -0,0 +1,5 @@ +#include + +int getentropy(unsigned char *b, size_t sz); +int arc4random_alloc(void **rsp, size_t rsp_size, void **rsxp, size_t rsxp_size); +void arc4random_close(void **rsp, void **rsxp); \ No newline at end of file -- cgit v1.2.3