backports: add support for prandom_bytes
authorEmmanuel Grumbach <emmanuel.grumbach@intel.com>
Tue, 3 Dec 2013 14:10:52 +0000 (15:10 +0100)
committerLuis R. Rodriguez <mcgrof@do-not-panic.com>
Fri, 6 Dec 2013 16:20:43 +0000 (17:20 +0100)
This was added in 3.8 and not used until now.
Since it it being used by the wireless stack, backport it.

Signed-off-by: Emmanuel Grumbach <emmanuel.grumbach@intel.com>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
[mcgrof: fixed renamed prandom_bytes_state to prandom_bytes on symbol]
Signed-off-by: Luis R. Rodriguez <mcgrof@do-not-panic.com>
backport/backport-include/linux/random.h
backport/compat/compat-3.8.c

index 812ce7fa75262eaccf8659f2f4cebc48c72d363e..e91510609774759865260f8109c88a05125e61fc 100644 (file)
@@ -8,6 +8,9 @@
 #define prandom_seed(_seed)            srandom32(_seed)
 #define prandom_u32()                  random32()
 #define prandom_u32_state(_state)      prandom32(_state)
+/* backport 6582c665d6b882dad8329e05749fbcf119f1ab88 */
+#define prandom_bytes LINUX_BACKPORT(prandom_bytes)
+void prandom_bytes(void *buf, int bytes);
 #endif
 
 #endif /* __BACKPORT_RANDOM_H */
index d43d3862888b289c733ef3a0140269adbf30a65e..a018ea9707c5abc7957ee4aef3e7d4795e3f6f63 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/module.h>
 #include "hid-ids.h"
 #include <linux/netdevice.h>
+#include <linux/random.h>
 
 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,7,8))
 void netdev_set_default_ethtool_ops(struct net_device *dev,
@@ -419,6 +420,37 @@ int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long
 }
 EXPORT_SYMBOL_GPL(vm_iomap_memory);
 
+/**
+ *     prandom_bytes - get the requested number of pseudo-random bytes
+ *     @buf: where to copy the pseudo-random bytes to
+ *     @bytes: the requested number of bytes
+ */
+void prandom_bytes(void *buf, int bytes)
+{
+       unsigned char *p = buf;
+       int i;
+
+       for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
+               u32 random = random32();
+               int j;
+
+               for (j = 0; j < sizeof(u32); j++) {
+                       p[i + j] = random;
+                       random >>= BITS_PER_BYTE;
+               }
+       }
+
+       if (i < bytes) {
+               u32 random = random32();
+
+               for (; i < bytes; i++) {
+                       p[i] = random;
+                       random >>= BITS_PER_BYTE;
+               }
+       }
+}
+EXPORT_SYMBOL_GPL(prandom_bytes);
+
 #ifdef CONFIG_OF
 /**
  * of_find_property_value_of_size