summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/rng.c
blob: 6f53a0b1b82304b092944c9a654bc98fec87c3a0 (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
#include <esp_random.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, EOSMessage *msg, uint16_t len) {
    unsigned char *buffer = msg->buffer;
    uint16_t rng_len = 0;

    if (!(eos_msg_flags(msg) & EOS_MSG_FLAG_RPLY_REQ)) return;
    if (len < sizeof(uint16_t)) return;

    rng_len  = (uint16_t)buffer[0] << 8;
    rng_len |= (uint16_t)buffer[1];
    if (rng_len > msg->size) return;

    esp_fill_random(buffer, rng_len);
    eos_net_reply(EOS_NET_MTYPE_RNG, msg, rng_len);
}

void eos_rng_init(void) {
    eos_net_set_handler(EOS_NET_MTYPE_RNG, rng_handler);

    ESP_LOGI(TAG, "INIT");
}