Subversion Repositories planix.SVN

Rev

Rev 33 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include "os.h"
2
#include <libsec.h>
3
 
4
static void
33 7u83 5
init(Chachastate *cs)
2 - 6
{
33 7u83 7
	ulong seed[11];
8
	int i;
2 - 9
 
33 7u83 10
	for(i=0; i<nelem(seed); i++)
11
		seed[i] = truerand();
2 - 12
 
33 7u83 13
	setupChachastate(cs, (uchar*)&seed[0], 32, (uchar*)&seed[8], 12, 20);
14
	memset(seed, 0, sizeof(seed));
2 - 15
}
16
 
17
static void
33 7u83 18
fill(Chachastate *cs, uchar *p, int n)
2 - 19
{
33 7u83 20
	Chachastate c;
2 - 21
 
33 7u83 22
	c = *cs;
23
	chacha_encrypt((uchar*)&cs->input[4], 32, &c);
24
	if(++cs->input[13] == 0)
25
		if(++cs->input[14] == 0)
26
			++cs->input[15];
27
 
28
	chacha_encrypt(p, n, &c);
29
	memset(&c, 0, sizeof(c));
2 - 30
}
31
 
32
void
33
genrandom(uchar *p, int n)
34
{
33 7u83 35
	static QLock lk;
36
	static Chachastate cs;
37
 
38
	qlock(&lk);
39
	if(cs.rounds == 0)
40
		init(&cs);
41
	cs.input[4] ^= getpid();	/* fork protection */
42
	fill(&cs, p, n);
43
	qunlock(&lk);
2 - 44
}