Removed empty man page for starter.
[strongswan.git] / src / libfreeswan / prng.c
1 /*
2 * crypto-class pseudorandom number generator
3 * currently uses same algorithm as RC4(TM), from Schneier 2nd ed p397
4 * Copyright (C) 2002 Henry Spencer.
5 *
6 * This library is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Library General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 * License for more details.
15 */
16 #include "internal.h"
17 #include "freeswan.h"
18
19 /*
20 - prng_init - initialize PRNG from a key
21 */
22 void
23 prng_init(prng, key, keylen)
24 struct prng *prng;
25 const unsigned char *key;
26 size_t keylen;
27 {
28 unsigned char k[256];
29 int i, j;
30 unsigned const char *p;
31 unsigned const char *keyend = key + keylen;
32 unsigned char t;
33
34 for (i = 0; i <= 255; i++)
35 prng->sbox[i] = i;
36 p = key;
37 for (i = 0; i <= 255; i++) {
38 k[i] = *p++;
39 if (p >= keyend)
40 p = key;
41 }
42 j = 0;
43 for (i = 0; i <= 255; i++) {
44 j = (j + prng->sbox[i] + k[i]) & 0xff;
45 t = prng->sbox[i];
46 prng->sbox[i] = prng->sbox[j];
47 prng->sbox[j] = t;
48 k[i] = 0; /* clear out key memory */
49 }
50 prng->i = 0;
51 prng->j = 0;
52 prng->count = 0;
53 }
54
55 /*
56 - prng_bytes - get some pseudorandom bytes from PRNG
57 */
58 void
59 prng_bytes(prng, dst, dstlen)
60 struct prng *prng;
61 unsigned char *dst;
62 size_t dstlen;
63 {
64 int i, j, t;
65 unsigned char *p = dst;
66 size_t remain = dstlen;
67 # define MAX 4000000000ul
68
69 while (remain > 0) {
70 i = (prng->i + 1) & 0xff;
71 prng->i = i;
72 j = (prng->j + prng->sbox[i]) & 0xff;
73 prng->j = j;
74 t = prng->sbox[i];
75 prng->sbox[i] = prng->sbox[j];
76 prng->sbox[j] = t;
77 t = (t + prng->sbox[i]) & 0xff;
78 *p++ = prng->sbox[t];
79 remain--;
80 }
81 if (prng->count < MAX - dstlen)
82 prng->count += dstlen;
83 else
84 prng->count = MAX;
85 }
86
87 /*
88 - prnt_count - how many bytes have been extracted from PRNG so far?
89 */
90 unsigned long
91 prng_count(prng)
92 struct prng *prng;
93 {
94 return prng->count;
95 }
96
97 /*
98 - prng_final - clear out PRNG to ensure nothing left in memory
99 */
100 void
101 prng_final(prng)
102 struct prng *prng;
103 {
104 int i;
105
106 for (i = 0; i <= 255; i++)
107 prng->sbox[i] = 0;
108 prng->i = 0;
109 prng->j = 0;
110 prng->count = 0; /* just for good measure */
111 }
112
113
114
115 #ifdef PRNG_MAIN
116
117 #include <stdio.h>
118
119 void regress();
120
121 int
122 main(argc, argv)
123 int argc;
124 char *argv[];
125 {
126 struct prng pr;
127 unsigned char buf[100];
128 unsigned char *p;
129 size_t n;
130
131 if (argc < 2) {
132 fprintf(stderr, "Usage: %s {key|-r}\n", argv[0]);
133 exit(2);
134 }
135
136 if (strcmp(argv[1], "-r") == 0) {
137 regress();
138 fprintf(stderr, "regress() returned?!?\n");
139 exit(1);
140 }
141
142 prng_init(&pr, argv[1], strlen(argv[1]));
143 prng_bytes(&pr, buf, 32);
144 printf("0x");
145 for (p = buf, n = 32; n > 0; p++, n--)
146 printf("%02x", *p);
147 printf("\n%lu bytes\n", prng_count(&pr));
148 prng_final(&pr);
149 exit(0);
150 }
151
152 void
153 regress()
154 {
155 struct prng pr;
156 unsigned char buf[100];
157 unsigned char *p;
158 size_t n;
159 /* somewhat non-random sample key */
160 unsigned char key[] = "here we go gathering nuts in May";
161 /* first thirty bytes of output from that key */
162 unsigned char good[] = "\x3f\x02\x8e\x4a\x2a\xea\x23\x18\x92\x7c"
163 "\x09\x52\x83\x61\xaa\x26\xce\xbb\x9d\x71"
164 "\x71\xe5\x10\x22\xaf\x60\x54\x8d\x5b\x28";
165 int nzero, none;
166 int show = 0;
167
168 prng_init(&pr, key, strlen(key));
169 prng_bytes(&pr, buf, sizeof(buf));
170 for (p = buf, n = sizeof(buf); n > 0; p++, n--) {
171 if (*p == 0)
172 nzero++;
173 if (*p == 255)
174 none++;
175 }
176 if (nzero > 3 || none > 3) {
177 fprintf(stderr, "suspiciously non-random output!\n");
178 show = 1;
179 }
180 if (memcmp(buf, good, strlen(good)) != 0) {
181 fprintf(stderr, "incorrect output!\n");
182 show = 1;
183 }
184 if (show) {
185 fprintf(stderr, "0x");
186 for (p = buf, n = sizeof(buf); n > 0; p++, n--)
187 fprintf(stderr, "%02x", *p);
188 fprintf(stderr, "\n");
189 exit(1);
190 }
191 if (prng_count(&pr) != sizeof(buf)) {
192 fprintf(stderr, "got %u bytes, but count is %lu\n",
193 sizeof(buf), prng_count(&pr));
194 exit(1);
195 }
196 prng_final(&pr);
197 exit(0);
198 }
199
200 #endif /* PRNG_MAIN */