-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_arc4rand.c
More file actions
44 lines (31 loc) · 750 Bytes
/
Copy patht_arc4rand.c
File metadata and controls
44 lines (31 loc) · 750 Bytes
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
40
41
42
/*
* Simple test harness and benchmark for chacha20 based random
* generator.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include "cryptorand.h"
extern void error(int doexit, int err, const char* fmt, ...);
int
main(int argc, const char *argv[])
{
crypto_rand_state st;
int r = crypto_rand_init(&st, CRYPTO_RAND_CHACHA20, getentropy);
assert(r == 0);
int n = 1024;
if (argc > 1) {
int z = atoi(argv[1]);
if (z <= 0) error(1, 0, "invalid size %s", argv[1]);
n = z;
}
uint8_t *buf = malloc(n);
assert(buf);
crypto_rand_buf(&st, buf, n);
fwrite(buf, 1, n, stdout);
free(buf);
return 0;
}