18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 5d8571d3eSHans Petter Selasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * All rights reserved. 78d59ecb2SHans Petter Selasky * 88d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 98d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 108d59ecb2SHans Petter Selasky * are met: 118d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 128d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 138d59ecb2SHans Petter Selasky * disclaimer. 148d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 158d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 168d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 178d59ecb2SHans Petter Selasky * 188d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288d59ecb2SHans Petter Selasky * 298d59ecb2SHans Petter Selasky * $FreeBSD$ 308d59ecb2SHans Petter Selasky */ 31bf4e2e5bSMark Johnston 328d59ecb2SHans Petter Selasky #ifndef _LINUX_RANDOM_H_ 338d59ecb2SHans Petter Selasky #define _LINUX_RANDOM_H_ 348d59ecb2SHans Petter Selasky 35408c514fSNeel Chauhan #include <linux/types.h> 368d59ecb2SHans Petter Selasky #include <sys/random.h> 37d8571d3eSHans Petter Selasky #include <sys/libkern.h> 388d59ecb2SHans Petter Selasky 39d109700cSJohannes Lundberg #define get_random_u32() get_random_int() 40d109700cSJohannes Lundberg 418d59ecb2SHans Petter Selasky static inline void 428d59ecb2SHans Petter Selasky get_random_bytes(void *buf, int nbytes) 438d59ecb2SHans Petter Selasky { 44bf4e2e5bSMark Johnston 45a8a16c71SConrad Meyer arc4random_buf(buf, nbytes); 468d59ecb2SHans Petter Selasky } 478d59ecb2SHans Petter Selasky 48bf4e2e5bSMark Johnston static inline u_int 49bf4e2e5bSMark Johnston get_random_int(void) 50bf4e2e5bSMark Johnston { 51bf4e2e5bSMark Johnston u_int val; 52bf4e2e5bSMark Johnston 53bf4e2e5bSMark Johnston get_random_bytes(&val, sizeof(val)); 54bf4e2e5bSMark Johnston return (val); 55bf4e2e5bSMark Johnston } 56bf4e2e5bSMark Johnston 57bf4e2e5bSMark Johnston static inline u_long 58bf4e2e5bSMark Johnston get_random_long(void) 59bf4e2e5bSMark Johnston { 60bf4e2e5bSMark Johnston u_long val; 61bf4e2e5bSMark Johnston 62bf4e2e5bSMark Johnston get_random_bytes(&val, sizeof(val)); 63bf4e2e5bSMark Johnston return (val); 64bf4e2e5bSMark Johnston } 65bf4e2e5bSMark Johnston 66*10096cb6SBjoern A. Zeeb static __inline uint32_t 67*10096cb6SBjoern A. Zeeb prandom_u32(void) 68*10096cb6SBjoern A. Zeeb { 69*10096cb6SBjoern A. Zeeb uint32_t val; 70*10096cb6SBjoern A. Zeeb 71*10096cb6SBjoern A. Zeeb get_random_bytes(&val, sizeof(val)); 72*10096cb6SBjoern A. Zeeb return (val); 73*10096cb6SBjoern A. Zeeb } 74*10096cb6SBjoern A. Zeeb 75af300929SEmmanuel Vadot static inline u32 76af300929SEmmanuel Vadot prandom_u32_max(u32 max) 77af300929SEmmanuel Vadot { 78af300929SEmmanuel Vadot return (arc4random_uniform(max)); 79af300929SEmmanuel Vadot } 80af300929SEmmanuel Vadot 818d59ecb2SHans Petter Selasky #endif /* _LINUX_RANDOM_H_ */ 82