|
| 1 | +/* Acharacter device which returns bytes from a PRNG in the xorshiro family. */ |
| 2 | + |
| 3 | +#include <linux/device.h> |
| 4 | +#include <linux/fs.h> |
| 5 | +#include <linux/init.h> |
| 6 | +#include <linux/kernel.h> |
| 7 | +#include <linux/module.h> |
| 8 | +#include <linux/mutex.h> |
| 9 | +#include <linux/uaccess.h> |
| 10 | + |
| 11 | +#define DEVICE_NAME "xoro" |
| 12 | +#define CLASS_NAME "xoro" |
| 13 | + |
| 14 | +MODULE_LICENSE("Dual MIT/GPL"); |
| 15 | +MODULE_AUTHOR("National Cheng Kung University, Taiwan"); |
| 16 | +MODULE_DESCRIPTION("Xoroshiro128p PRNG"); |
| 17 | +MODULE_VERSION("0.1"); |
| 18 | + |
| 19 | +static int major_number; |
| 20 | +static struct class *dev_class = NULL; |
| 21 | +static struct device *dev_device = NULL; |
| 22 | + |
| 23 | +static int n_opens = 0; /* Count the number of times device is opened. */ |
| 24 | + |
| 25 | +/* Mutex to allow only one userspace program to read at once. */ |
| 26 | +static DEFINE_MUTEX(xoro_mutex); |
| 27 | + |
| 28 | +/* This is xoroshiro128+ 1.0, written by David Blackman and Sebastiano Vigna |
| 29 | + * ([email protected]). It passes all tests we are aware of except for the four |
| 30 | + * lower bits, which might fail linearity tests (and just those), so if |
| 31 | + * low linear complexity is not considered an issue (as it is usually the |
| 32 | + * case) it can be used to generate 64-bit outputs, too; moreover, this |
| 33 | + * generator has a very mild Hamming-weight dependency making our test |
| 34 | + * (https://xoshiro.di.unimi.it/hwd.php) fail after 5 TB of output; we believe |
| 35 | + * this slight bias cannot affect any application. If you are concerned, |
| 36 | + * use xoroshiro128++, xoroshiro128** or xoshiro256+. |
| 37 | + * |
| 38 | + * We suggest to use a sign test to extract a random Boolean value, and |
| 39 | + * right shifts to extract subsets of bits. |
| 40 | + * |
| 41 | + * The state must be seeded so that it is not everywhere zero. If you have |
| 42 | + * a 64-bit seed, we suggest to seed a splitmix64 generator and use its |
| 43 | + * output to fill s. |
| 44 | + * |
| 45 | + * NOTE: the parameters (a=24, b=16, b=37) of this version give slightly |
| 46 | + * better results in our test than the 2016 version (a=55, b=14, c=36). |
| 47 | + */ |
| 48 | + |
| 49 | +static inline uint64_t rotl(const uint64_t x, int k) |
| 50 | +{ |
| 51 | + return (x << k) | (x >> (64 - k)); |
| 52 | +} |
| 53 | + |
| 54 | +static uint64_t s[2]; |
| 55 | + |
| 56 | +static void seed(uint64_t s0, uint64_t s1) |
| 57 | +{ |
| 58 | + s[0] = s0; |
| 59 | + s[1] = s1; |
| 60 | + return; |
| 61 | +} |
| 62 | + |
| 63 | +static uint64_t next(void) |
| 64 | +{ |
| 65 | + const uint64_t s0 = s[0]; |
| 66 | + uint64_t s1 = s[1]; |
| 67 | + const uint64_t result = s0 + s1; |
| 68 | + |
| 69 | + s1 ^= s0; |
| 70 | + s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); /* a, b */ |
| 71 | + s[1] = rotl(s1, 37); /* c */ |
| 72 | + |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +/* This is the jump function for the generator. It is equivalent to 2^64 calls |
| 77 | + * to next(); it can be used to generate 2^64 non-overlapping subsequences for |
| 78 | + * parallel computations. |
| 79 | + */ |
| 80 | +static void jump(void) |
| 81 | +{ |
| 82 | + static const uint64_t JUMP[] = {0xdf900294d8f554a5, 0x170865df4b3201fc}; |
| 83 | + |
| 84 | + uint64_t s0 = 0; |
| 85 | + uint64_t s1 = 0; |
| 86 | + int i, b; |
| 87 | + for (i = 0; i < sizeof JUMP / sizeof *JUMP; i++) |
| 88 | + for (b = 0; b < 64; b++) { |
| 89 | + if (JUMP[i] & (uint64_t) (1) << b) { |
| 90 | + s0 ^= s[0]; |
| 91 | + s1 ^= s[1]; |
| 92 | + } |
| 93 | + next(); |
| 94 | + } |
| 95 | + |
| 96 | + s[0] = s0; |
| 97 | + s[1] = s1; |
| 98 | +} |
| 99 | + |
| 100 | +static int dev_open(struct inode *, struct file *); |
| 101 | +static int dev_release(struct inode *, struct file *); |
| 102 | +static ssize_t dev_read(struct file *, char *, size_t, loff_t *); |
| 103 | +static struct file_operations fops = { |
| 104 | + .open = dev_open, |
| 105 | + .read = dev_read, |
| 106 | + .release = dev_release, |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Initialize /dev/xoro. |
| 111 | + * Returns 0 if successful. |
| 112 | + */ |
| 113 | +static int __init xoro_init(void) |
| 114 | +{ |
| 115 | + printk(KERN_INFO "XORO: Initializing...\n"); |
| 116 | + |
| 117 | + major_number = register_chrdev(0, DEVICE_NAME, &fops); |
| 118 | + if (0 > major_number) { |
| 119 | + printk(KERN_ALERT "XORO: Failed to register major_number\n"); |
| 120 | + return major_number; |
| 121 | + } |
| 122 | + printk(KERN_INFO "XORO: major_number=%d\n", major_number); |
| 123 | + |
| 124 | + dev_class = class_create(THIS_MODULE, CLASS_NAME); |
| 125 | + if (IS_ERR(dev_class)) { |
| 126 | + unregister_chrdev(major_number, DEVICE_NAME); // backout |
| 127 | + printk(KERN_ALERT "XORO: Failed to create dev_class\n"); |
| 128 | + return PTR_ERR(dev_class); |
| 129 | + } |
| 130 | + printk(KERN_INFO "XORO: dev_class[name]=%s\n", CLASS_NAME); |
| 131 | + |
| 132 | + // Register the device driver |
| 133 | + dev_device = device_create(dev_class, NULL, MKDEV(major_number, 0), NULL, |
| 134 | + DEVICE_NAME); |
| 135 | + if (IS_ERR(dev_device)) { |
| 136 | + class_destroy(dev_class); // backout |
| 137 | + unregister_chrdev(major_number, DEVICE_NAME); // backout |
| 138 | + printk(KERN_ALERT "XORO: Failed to create dev_device\n"); |
| 139 | + return PTR_ERR(dev_device); |
| 140 | + } |
| 141 | + printk(KERN_INFO "XORO: dev_device[name]=%s\n", DEVICE_NAME); |
| 142 | + |
| 143 | + mutex_init(&xoro_mutex); |
| 144 | + |
| 145 | + seed(314159265, 1618033989); // Initialize PRNG with pi and phi. |
| 146 | + |
| 147 | + printk(KERN_INFO "XORO: Initialized\n"); |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Free all module resources. |
| 153 | + * Not used if part of a built-in driver rather than a LKM. |
| 154 | + */ |
| 155 | +static void __exit xoro_exit(void) |
| 156 | +{ |
| 157 | + mutex_destroy(&xoro_mutex); |
| 158 | + |
| 159 | + device_destroy(dev_class, MKDEV(major_number, 0)); |
| 160 | + |
| 161 | + class_unregister(dev_class); |
| 162 | + class_destroy(dev_class); |
| 163 | + |
| 164 | + unregister_chrdev(major_number, DEVICE_NAME); |
| 165 | + |
| 166 | + printk(KERN_INFO "XORO: Exit\n"); |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * open() syscall. |
| 171 | + * Increment counter, perform another jump to effectively give each |
| 172 | + * reader a separate PRNG. |
| 173 | + * @inodep Pointer to an inode object (defined in linux/fs.h) |
| 174 | + * @filep Pointer to a file object (defined in linux/fs.h) |
| 175 | + */ |
| 176 | +static int dev_open(struct inode *inodep, struct file *filep) |
| 177 | +{ |
| 178 | + /* Try to acquire the mutex (returns 0 on fail) */ |
| 179 | + if (!mutex_trylock(&xoro_mutex)) { |
| 180 | + printk(KERN_INFO "XORO: %s busy\n", DEVICE_NAME); |
| 181 | + return -EBUSY; |
| 182 | + } |
| 183 | + |
| 184 | + jump(); // xorolus.c |
| 185 | + |
| 186 | + printk(KERN_INFO "XORO: %s opened. n_opens=%d\n", DEVICE_NAME, n_opens++); |
| 187 | + |
| 188 | + return 0; |
| 189 | +} |
| 190 | + |
| 191 | +/** |
| 192 | + * Called whenever device is read from user space. |
| 193 | + * @filep Pointer to a file object (defined in linux/fs.h). |
| 194 | + * @buffer Pointer to the buffer to which this function may write data. |
| 195 | + * @len Number of bytes requested. |
| 196 | + * @offset Unused. |
| 197 | + * Returns number of bytes successfully read. Negative on error. |
| 198 | + */ |
| 199 | +static ssize_t dev_read(struct file *filep, |
| 200 | + char *buffer, |
| 201 | + size_t len, |
| 202 | + loff_t *offset) |
| 203 | +{ |
| 204 | + // Give at most 8 bytes per read. |
| 205 | + size_t len_ = (len > 8) ? 8 : len; |
| 206 | + |
| 207 | + uint64_t value = next(); // xorolus.c |
| 208 | + |
| 209 | + // copy_to_user has the format ( * to, *from, size) and returns 0 on success |
| 210 | + int n_notcopied = copy_to_user(buffer, (char *) (&value), len_); |
| 211 | + |
| 212 | + if (0 != n_notcopied) { |
| 213 | + printk(KERN_ALERT "XORO: Failed to read %d/%ld bytes\n", n_notcopied, |
| 214 | + len_); |
| 215 | + return -EFAULT; |
| 216 | + } else { |
| 217 | + printk(KERN_INFO "XORO: read %ld bytes\n", len_); |
| 218 | + return len_; |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * Called when the userspace program calls close(). |
| 224 | + * @inodep A pointer to an inode object (defined in linux/fs.h) |
| 225 | + * @filep A pointer to a file object (defined in linux/fs.h) |
| 226 | + */ |
| 227 | +static int dev_release(struct inode *inodep, struct file *filep) |
| 228 | +{ |
| 229 | + mutex_unlock(&xoro_mutex); |
| 230 | + printk(KERN_INFO "XORO: %s closed\n", DEVICE_NAME); |
| 231 | + return 0; |
| 232 | +} |
| 233 | + |
| 234 | +module_init(xoro_init); |
| 235 | +module_exit(xoro_exit); |
0 commit comments