2 * Copyright IBM Corp. 2006,2007
3 * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
4 * Driver for the s390 pseudo random number generator
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/miscdevice.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/random.h>
13 #include <asm/debug.h>
14 #include <asm/uaccess.h>
16 #include "crypt_s390.h"
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Jan Glauber <jan.glauber@de.ibm.com>");
20 MODULE_DESCRIPTION("s390 PRNG interface");
22 static int prng_chunk_size = 256;
23 module_param(prng_chunk_size, int, S_IRUSR | S_IRGRP | S_IROTH);
24 MODULE_PARM_DESC(prng_chunk_size, "PRNG read chunk size in bytes");
26 static int prng_entropy_limit = 4096;
27 module_param(prng_entropy_limit, int, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
28 MODULE_PARM_DESC(prng_entropy_limit,
29 "PRNG add entropy after that much bytes were produced");
32 * Any one who considers arithmetical methods of producing random digits is,
33 * of course, in a state of sin. -- John von Neumann
36 struct s390_prng_data {
37 unsigned long count; /* how many bytes were produced */
41 static struct s390_prng_data *p;
43 /* copied from libica, use a non-zero initial parameter block */
44 static unsigned char parm_block[32] = {
45 0x0F,0x2B,0x8E,0x63,0x8C,0x8E,0xD2,0x52,0x64,0xB7,0xA0,0x7B,0x75,0x28,0xB8,0xF4,
46 0x75,0x5F,0xD2,0xA6,0x8D,0x97,0x11,0xFF,0x49,0xD8,0x23,0xF3,0x7E,0x21,0xEC,0xA0,
49 static int prng_open(struct inode *inode, struct file *file)
51 return nonseekable_open(inode, file);
54 static void prng_add_entropy(void)
60 for (i = 0; i < 16; i++) {
61 ret = crypt_s390_kmc(KMC_PRNG, parm_block, (char *)entropy,
62 (char *)entropy, sizeof(entropy));
63 BUG_ON(ret < 0 || ret != sizeof(entropy));
64 memcpy(parm_block, entropy, sizeof(entropy));
68 static void prng_seed(int nbytes)
74 get_random_bytes(buf, nbytes);
78 *((__u64 *)parm_block) ^= *((__u64 *)buf+i*8);
86 static ssize_t prng_read(struct file *file, char __user *ubuf, size_t nbytes,
93 /* nbytes can be arbitrary length, we split it into chunks */
95 /* same as in extract_entropy_user in random.c */
97 if (signal_pending(current)) {
106 * we lose some random bytes if an attacker issues
107 * reads < 8 bytes, but we don't care
109 chunk = min_t(int, nbytes, prng_chunk_size);
111 /* PRNG only likes multiples of 8 bytes */
112 n = (chunk + 7) & -8;
114 if (p->count > prng_entropy_limit)
117 /* if the CPU supports PRNG stckf is present too */
118 asm volatile(".insn s,0xb27c0000,%0"
119 : "=m" (*((unsigned long long *)p->buf)) : : "cc");
122 * Beside the STCKF the input for the TDES-EDE is the output
123 * of the last operation. We differ here from X9.17 since we
124 * only store one timestamp into the buffer. Padding the whole
125 * buffer with timestamps does not improve security, since
126 * successive stckf have nearly constant offsets.
127 * If an attacker knows the first timestamp it would be
128 * trivial to guess the additional values. One timestamp
129 * is therefore enough and still guarantees unique input values.
131 * Note: you can still get strict X9.17 conformity by setting
132 * prng_chunk_size to 8 bytes.
134 tmp = crypt_s390_kmc(KMC_PRNG, parm_block, p->buf, p->buf, n);
135 BUG_ON((tmp < 0) || (tmp != n));
139 if (copy_to_user(ubuf, p->buf, chunk))
149 static const struct file_operations prng_fops = {
150 .owner = THIS_MODULE,
156 static struct miscdevice prng_dev = {
158 .minor = MISC_DYNAMIC_MINOR,
162 static int __init prng_init(void)
166 /* check if the CPU has a PRNG */
167 if (!crypt_s390_func_available(KMC_PRNG))
170 if (prng_chunk_size < 8)
173 p = kmalloc(sizeof(struct s390_prng_data), GFP_KERNEL);
178 p->buf = kmalloc(prng_chunk_size, GFP_KERNEL);
184 /* initialize the PRNG, add 128 bits of entropy */
187 ret = misc_register(&prng_dev);
190 "Could not register misc device for PRNG.\n");
202 static void __exit prng_exit(void)
205 memset(p->buf, 0, prng_chunk_size);
209 misc_deregister(&prng_dev);
212 module_init(prng_init);
213 module_exit(prng_exit);