summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/net/rng.c
blob: 414a6fa7e2b6667cda519205a6b6141cf87b2fc5 (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
32
33
34
35
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "eos.h"
#include "event.h"
#include "dev/net.h"

int getentropy(unsigned char *b, size_t sz) {
    unsigned char type;
    EOSMessage msg;
    uint16_t len;
    int rv;

    type = EOS_NET_MTYPE_RNG;
    len = sizeof(uint16_t);

    eos_net_alloc(&msg);

    if ((msg.size < len) || (msg.size < sz)) {
        eos_net_free(&msg, 1);
        return -1;
    }

    msg.buffer[0] = sz >> 8;
    msg.buffer[1] = sz;

    rv = eos_net_xchg(&type, &msg, &len);
    if (rv || (len != sz)) rv = -1;

    if (!rv) memcpy(b, msg.buffer, sz);
    eos_net_free(&msg, 1);

    return rv;
}