Mercurial > hg > quicktun
annotate src/randombytes.c @ 64:fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
author | Ivo Smits <Ivo@UFO-Net.nl> |
---|---|
date | Sat, 07 Jan 2017 18:07:27 +0100 |
parents | |
children |
rev | line source |
---|---|
64
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
1 /* |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
2 randombytes/devurandom.h version 20080713 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
3 D. J. Bernstein |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
4 Public domain. |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
5 */ |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
6 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
7 #include <sys/types.h> |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
8 #include <sys/stat.h> |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
9 #include <fcntl.h> |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
10 #include <unistd.h> |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
11 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
12 static int fd = -1; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
13 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
14 void randombytes(unsigned char *x,unsigned long long xlen) { |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
15 int i; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
16 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
17 if (fd == -1) { |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
18 for (;;) { |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
19 fd = open("/dev/urandom",O_RDONLY); |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
20 if (fd != -1) break; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
21 sleep(1); |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
22 } |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
23 } |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
24 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
25 while (xlen > 0) { |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
26 if (xlen < 1048576) i = xlen; else i = 1048576; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
27 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
28 i = read(fd,x,i); |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
29 if (i < 1) { |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
30 sleep(1); |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
31 continue; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
32 } |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
33 |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
34 x += i; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
35 xlen -= i; |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
36 } |
fa53d1c54886
Use default RNG for key generation, added options to generate public key from private key, use bundled tweetnacl as fallback instead of nacl download
Ivo Smits <Ivo@UFO-Net.nl>
parents:
diff
changeset
|
37 } |