File: randombytes.c

package info (click to toggle)
librandombytes 0~20240318-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 344 kB
  • sloc: ansic: 411; python: 340; sh: 137; makefile: 28
file content (39 lines) | stat: -rw-r--r-- 644 bytes parent folder | download | duplicates (2)
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
36
37
38
39
// version 20230126
// public domain
// djb

// automatic-alternatives 1 -lcrypto

#include <unistd.h>
#include <openssl/rand.h>
#include "randombytes.h"

__attribute__((constructor))
static void startup(void)
{
  unsigned char x;
  while (RAND_bytes(&x,1) != 1)
    sleep(1);
}

void randombytes(void *x,long long xbytes)
{
  while (xbytes > 0) {
    // RAND_bytes takes int length
    // assume int is at least 32 bits

    int todo = 1048576;
    if (xbytes < 1048576) todo = xbytes;

    while (RAND_bytes(x,todo) != 1)
      sleep(1);

    x += todo;
    xbytes -= todo;
  }
}

const char *randombytes_source(void)
{
  return "openssl";
}