summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/rng.c
blob: 3927df13ddd1767fc6f9204f9acf6551cbf26d06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <esp_system.h>
#include <esp_log.h>
#include <esp_err.h>

#include "net.h"
#include "rng.h"

static const char *TAG = "EOS RNG";

static void rng_handler(unsigned char _mtype, 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_SIZE_BUF) {
        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");
}