]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/chanservcrypto.c
More work on challenge.
[irc/quakenet/newserv.git] / chanserv / chanservcrypto.c
1 #include <stdio.h>
2
3 #include "chanserv.h"
4 #include "../core/error.h"
5 #include "../lib/prng.h"
6
7 prngctx rng;
8
9 void chanservcryptoinit(void) {
10 int ret;
11
12 FILE *e = fopen(ENTROPYSOURCE, "rb");
13 if(!e) {
14 Error("chanserv",ERR_STOP,"Unable to open entropy source.");
15 /* shouldn't be running now... */
16 }
17
18 ret = fread(rng.randrsl, 1, sizeof(rng.randrsl), e);
19 fclose(e);
20
21 if(ret != sizeof(rng.randrsl)) {
22 Error("chanserv",ERR_STOP,"Unable to read entropy.");
23 /* shouldn't be running now... */
24 }
25
26 prnginit(&rng, 1);
27 }
28
29 ub4 cs_getrandint(void) {
30 return prng(&rng);
31 }
32
33 void cs_getrandbytes(unsigned char *buf, size_t bytes) {
34 ub4 b;
35
36 for(;bytes>0;bytes-=4,buf+=4) {
37 ub4 b = cs_getrandint();
38 memcpy(buf, &b, 4);
39 }
40 }