2 * PRNG: Pseudo Random Number Generator
3 * Based on NIST Recommended PRNG From ANSI X9.31 Appendix A.2.4 using
4 * AES 128 cipher in RFC3686 ctr mode
6 * (C) Neil Horman <nhorman@tuxdriver.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <linux/scatterlist.h>
23 #include <linux/string.h>
24 #include <linux/crypto.h>
25 #include <linux/highmem.h>
26 #include <linux/moduleparam.h>
27 #include <linux/jiffies.h>
28 #include <linux/timex.h>
29 #include <linux/interrupt.h>
30 #include <linux/miscdevice.h>
33 #define TEST_PRNG_ON_START 0
35 #define DEFAULT_PRNG_KEY "0123456789abcdef1011"
36 #define DEFAULT_PRNG_KSZ 20
37 #define DEFAULT_PRNG_IV "defaultv"
38 #define DEFAULT_PRNG_IVSZ 8
39 #define DEFAULT_BLK_SZ 16
40 #define DEFAULT_V_SEED "zaybxcwdveuftgsh"
43 * Flags for the prng_context flags field
46 #define PRNG_FIXED_SIZE 0x1
47 #define PRNG_NEED_RESET 0x2
50 * Note: DT is our counter value
51 * I is our intermediate value
52 * V is our seed vector
53 * See http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
54 * for implementation details
62 unsigned char rand_data[DEFAULT_BLK_SZ];
63 unsigned char last_rand_data[DEFAULT_BLK_SZ];
64 unsigned char DT[DEFAULT_BLK_SZ];
65 unsigned char I[DEFAULT_BLK_SZ];
66 unsigned char V[DEFAULT_BLK_SZ];
68 struct crypto_blkcipher *tfm;
74 static void hexdump(char *note, unsigned char *buf, unsigned int len)
77 printk(KERN_CRIT "%s", note);
78 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
84 #define dbgprint(format, args...) do {if(dbg) printk(format, ##args);} while(0)
86 static void xor_vectors(unsigned char *in1, unsigned char *in2,
87 unsigned char *out, unsigned int size)
92 out[i] = in1[i] ^ in2[i];
96 * Returns DEFAULT_BLK_SZ bytes of random data per call
97 * returns 0 if generation succeded, <0 if something went wrong
99 static int _get_more_prng_bytes(struct prng_context *ctx)
102 struct blkcipher_desc desc;
103 struct scatterlist sg_in, sg_out;
105 unsigned char tmp[DEFAULT_BLK_SZ];
111 dbgprint(KERN_CRIT "Calling _get_more_prng_bytes for context %p\n",ctx);
113 hexdump("Input DT: ", ctx->DT, DEFAULT_BLK_SZ);
114 hexdump("Input I: ", ctx->I, DEFAULT_BLK_SZ);
115 hexdump("Input V: ", ctx->V, DEFAULT_BLK_SZ);
118 * This algorithm is a 3 stage state machine
127 * Start by encrypting the counter value
128 * This gives us an intermediate value I
130 memcpy(tmp, ctx->DT, DEFAULT_BLK_SZ);
131 sg_init_one(&sg_out, &ctx->I[0], DEFAULT_BLK_SZ);
132 hexdump("tmp stage 0: ", tmp, DEFAULT_BLK_SZ);
137 * Next xor I with our secret vector V
138 * encrypt that result to obtain our
139 * pseudo random data which we output
141 xor_vectors(ctx->I, ctx->V, tmp, DEFAULT_BLK_SZ);
142 sg_init_one(&sg_out, &ctx->rand_data[0], DEFAULT_BLK_SZ);
143 hexdump("tmp stage 1: ", tmp, DEFAULT_BLK_SZ);
147 * First check that we didn't produce the same random data
148 * that we did last time around through this
150 if (!memcmp(ctx->rand_data, ctx->last_rand_data, DEFAULT_BLK_SZ)) {
151 printk(KERN_ERR "ctx %p Failed repetition check!\n",
153 ctx->flags |= PRNG_NEED_RESET;
156 memcpy(ctx->last_rand_data, ctx->rand_data, DEFAULT_BLK_SZ);
159 * Lastly xor the random data with I
160 * and encrypt that to obtain a new secret vector V
162 xor_vectors(ctx->rand_data, ctx->I, tmp, DEFAULT_BLK_SZ);
163 sg_init_one(&sg_out, &ctx->V[0], DEFAULT_BLK_SZ);
164 hexdump("tmp stage 2: ", tmp, DEFAULT_BLK_SZ);
168 /* Initialize our input buffer */
169 sg_init_one(&sg_in, &tmp[0], DEFAULT_BLK_SZ);
171 /* do the encryption */
172 ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, DEFAULT_BLK_SZ);
174 /* And check the result */
176 dbgprint(KERN_CRIT "Encryption of new block failed for context %p\n",ctx);
177 ctx->rand_data_valid = DEFAULT_BLK_SZ;
184 * Now update our DT value
186 for (i=DEFAULT_BLK_SZ-1;i>0;i--) {
187 ctx->DT[i] = ctx->DT[i-1];
191 dbgprint("Returning new block for context %p\n",ctx);
192 ctx->rand_data_valid = 0;
194 hexdump("Output DT: ", ctx->DT, DEFAULT_BLK_SZ);
195 hexdump("Output I: ", ctx->I, DEFAULT_BLK_SZ);
196 hexdump("Output V: ", ctx->V, DEFAULT_BLK_SZ);
197 hexdump("New Random Data: ", ctx->rand_data, DEFAULT_BLK_SZ);
202 /* Our exported functions */
203 int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx)
206 unsigned char *ptr = buf;
207 unsigned int byte_count = (unsigned int)nbytes;
214 spin_lock_irqsave(&ctx->prng_lock, flags);
217 if (ctx->flags & PRNG_NEED_RESET)
221 * If the FIXED_SIZE flag is on, only return whole blocks of
225 if (ctx->flags & PRNG_FIXED_SIZE) {
226 if (nbytes < DEFAULT_BLK_SZ)
228 byte_count = DEFAULT_BLK_SZ;
233 dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",byte_count, ctx);
237 if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
238 if (_get_more_prng_bytes(ctx) < 0) {
239 memset(buf, 0, nbytes);
246 * Copy up to the next whole block size
248 if (byte_count < DEFAULT_BLK_SZ) {
249 for (;ctx->rand_data_valid < DEFAULT_BLK_SZ; ctx->rand_data_valid++) {
250 *ptr = ctx->rand_data[ctx->rand_data_valid];
259 * Now copy whole blocks
261 for(;byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
262 if (_get_more_prng_bytes(ctx) < 0) {
263 memset(buf, 0, nbytes);
267 memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
268 ctx->rand_data_valid += DEFAULT_BLK_SZ;
269 ptr += DEFAULT_BLK_SZ;
273 * Now copy any extra partial data
279 spin_unlock_irqrestore(&ctx->prng_lock, flags);
280 dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",err, ctx);
283 EXPORT_SYMBOL_GPL(get_prng_bytes);
285 struct prng_context *alloc_prng_context(void)
287 struct prng_context *ctx=kzalloc(sizeof(struct prng_context), GFP_KERNEL);
289 spin_lock_init(&ctx->prng_lock);
291 if (reset_prng_context(ctx, NULL, NULL, NULL, NULL)) {
296 dbgprint(KERN_CRIT "returning context %p\n",ctx);
300 EXPORT_SYMBOL_GPL(alloc_prng_context);
302 void free_prng_context(struct prng_context *ctx)
304 crypto_free_blkcipher(ctx->tfm);
307 EXPORT_SYMBOL_GPL(free_prng_context);
309 int reset_prng_context(struct prng_context *ctx,
310 unsigned char *key, unsigned char *iv,
311 unsigned char *V, unsigned char *DT)
317 spin_lock(&ctx->prng_lock);
318 ctx->flags |= PRNG_NEED_RESET;
321 memcpy(ctx->prng_key,key,strlen(ctx->prng_key));
323 ctx->prng_key = DEFAULT_PRNG_KEY;
326 memcpy(ctx->prng_iv,iv, strlen(ctx->prng_iv));
328 ctx->prng_iv = DEFAULT_PRNG_IV;
331 memcpy(ctx->V,V,DEFAULT_BLK_SZ);
333 memcpy(ctx->V,DEFAULT_V_SEED,DEFAULT_BLK_SZ);
336 memcpy(ctx->DT, DT, DEFAULT_BLK_SZ);
338 memset(ctx->DT, 0, DEFAULT_BLK_SZ);
340 memset(ctx->rand_data,0,DEFAULT_BLK_SZ);
341 memset(ctx->last_rand_data,0,DEFAULT_BLK_SZ);
344 crypto_free_blkcipher(ctx->tfm);
346 ctx->tfm = crypto_alloc_blkcipher("rfc3686(ctr(aes))",0,0);
348 dbgprint(KERN_CRIT "Failed to alloc crypto tfm for context %p\n",ctx->tfm);
352 ctx->rand_data_valid = DEFAULT_BLK_SZ;
354 ret = crypto_blkcipher_setkey(ctx->tfm, ctx->prng_key, strlen(ctx->prng_key));
356 dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
357 crypto_blkcipher_get_flags(ctx->tfm));
358 crypto_free_blkcipher(ctx->tfm);
362 iv_len = crypto_blkcipher_ivsize(ctx->tfm);
364 crypto_blkcipher_set_iv(ctx->tfm, ctx->prng_iv, iv_len);
367 ctx->flags &= ~PRNG_NEED_RESET;
369 spin_unlock(&ctx->prng_lock);
374 EXPORT_SYMBOL_GPL(reset_prng_context);
376 /* Module initalization */
377 static int __init prng_mod_init(void)
380 #ifdef TEST_PRNG_ON_START
382 unsigned char tmpbuf[DEFAULT_BLK_SZ];
384 struct prng_context *ctx = alloc_prng_context();
388 if (get_prng_bytes(tmpbuf, DEFAULT_BLK_SZ, ctx) < 0) {
389 free_prng_context(ctx);
393 free_prng_context(ctx);
399 static void __exit prng_mod_fini(void)
404 MODULE_LICENSE("GPL");
405 MODULE_DESCRIPTION("Software Pseudo Random Number Generator");
406 MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
407 module_param(dbg, int, 0);
408 MODULE_PARM_DESC(dbg, "Boolean to enable debugging (0/1 == off/on)");
409 module_init(prng_mod_init);
410 module_exit(prng_mod_fini);