summaryrefslogtreecommitdiff
path: root/nacl/randombytes
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
commite2967396ac73cb7410787886cdaf072a184ffc49 (patch)
tree527a74d25a4a0705fc641994fd35bfab22662034 /nacl/randombytes
parent8928c817df345f29aa0b194743595aa11bd6a8ba (diff)
Added NaCl crypto library.
Diffstat (limited to 'nacl/randombytes')
-rw-r--r--nacl/randombytes/devurandom.c34
-rw-r--r--nacl/randombytes/devurandom.h24
-rw-r--r--nacl/randombytes/do43
-rw-r--r--nacl/randombytes/test.c15
4 files changed, 116 insertions, 0 deletions
diff --git a/nacl/randombytes/devurandom.c b/nacl/randombytes/devurandom.c
new file mode 100644
index 00000000..f3b8d418
--- /dev/null
+++ b/nacl/randombytes/devurandom.c
@@ -0,0 +1,34 @@
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <fcntl.h>
4#include <unistd.h>
5
6/* it's really stupid that there isn't a syscall for this */
7
8static int fd = -1;
9
10void randombytes(unsigned char *x,unsigned long long xlen)
11{
12 int i;
13
14 if (fd == -1) {
15 for (;;) {
16 fd = open("/dev/urandom",O_RDONLY);
17 if (fd != -1) break;
18 sleep(1);
19 }
20 }
21
22 while (xlen > 0) {
23 if (xlen < 1048576) i = xlen; else i = 1048576;
24
25 i = read(fd,x,i);
26 if (i < 1) {
27 sleep(1);
28 continue;
29 }
30
31 x += i;
32 xlen -= i;
33 }
34}
diff --git a/nacl/randombytes/devurandom.h b/nacl/randombytes/devurandom.h
new file mode 100644
index 00000000..2e0caf8a
--- /dev/null
+++ b/nacl/randombytes/devurandom.h
@@ -0,0 +1,24 @@
1/*
2randombytes/devurandom.h version 20080713
3D. J. Bernstein
4Public domain.
5*/
6
7#ifndef randombytes_devurandom_H
8#define randombytes_devurandom_H
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14extern void randombytes(unsigned char *,unsigned long long);
15
16#ifdef __cplusplus
17}
18#endif
19
20#ifndef randombytes_implementation
21#define randombytes_implementation "devurandom"
22#endif
23
24#endif
diff --git a/nacl/randombytes/do b/nacl/randombytes/do
new file mode 100644
index 00000000..42586282
--- /dev/null
+++ b/nacl/randombytes/do
@@ -0,0 +1,43 @@
1#!/bin/sh -e
2
3okabi | (
4 while read abi
5 do
6
7 rm -f randombytes.o randombytes.h
8
9 (
10 echo devurandom
11 ) | (
12 while read n
13 do
14 okc-$abi | (
15 while read c
16 do
17 echo "=== `date` === Trying $n.c with $c..." >&2
18 rm -f test randombytes-impl.o randombytes-impl.h randombytes-impl.c
19 cp $n.c randombytes-impl.c || continue
20 cp $n.h randombytes-impl.h || continue
21 $c -c randombytes-impl.c || continue
22 $c -o test test.c randombytes-impl.o || continue
23 ./test || continue
24 echo "=== `date` === Success. Using $n.c." >&2
25 mkdir -p lib/$abi
26 mv randombytes-impl.o lib/$abi/randombytes.o
27 mkdir -p include/$abi
28 mv randombytes-impl.h include/$abi/randombytes.h
29 exit 0
30 done
31 exit 111
32 ) && exit 0
33 done
34 exit 111
35 ) || (
36 echo ===== Giving up. >&2
37 rm -f test randombytes-impl.o randombytes-impl.h randombytes-impl.c
38 exit 111
39 ) || exit 111
40
41 done
42 exit 0
43) || exit 111
diff --git a/nacl/randombytes/test.c b/nacl/randombytes/test.c
new file mode 100644
index 00000000..646811ca
--- /dev/null
+++ b/nacl/randombytes/test.c
@@ -0,0 +1,15 @@
1#include "randombytes-impl.h"
2
3unsigned char x[65536];
4unsigned long long freq[256];
5
6int main()
7{
8 unsigned long long i;
9
10 randombytes(x,sizeof x);
11 for (i = 0;i < 256;++i) freq[i] = 0;
12 for (i = 0;i < sizeof x;++i) ++freq[255 & (int) x[i]];
13 for (i = 0;i < 256;++i) if (!freq[i]) return 111;
14 return 0;
15}