1*640eb22bSagc /*-
2*640eb22bSagc * Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
3*640eb22bSagc * All rights reserved.
4*640eb22bSagc *
5*640eb22bSagc * Redistribution and use in source and binary forms, with or without
6*640eb22bSagc * modification, are permitted provided that the following conditions
7*640eb22bSagc * are met:
8*640eb22bSagc * 1. Redistributions of source code must retain the above copyright
9*640eb22bSagc * notice, this list of conditions and the following disclaimer.
10*640eb22bSagc * 2. Redistributions in binary form must reproduce the above copyright
11*640eb22bSagc * notice, this list of conditions and the following disclaimer in the
12*640eb22bSagc * documentation and/or other materials provided with the distribution.
13*640eb22bSagc *
14*640eb22bSagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*640eb22bSagc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*640eb22bSagc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*640eb22bSagc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*640eb22bSagc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*640eb22bSagc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*640eb22bSagc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*640eb22bSagc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*640eb22bSagc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*640eb22bSagc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*640eb22bSagc */
25*640eb22bSagc #include <sys/types.h>
26*640eb22bSagc #include <sys/param.h>
27*640eb22bSagc
28*640eb22bSagc #ifdef _KERNEL
29*640eb22bSagc # include <sys/kmem.h>
30*640eb22bSagc #else
31*640eb22bSagc # include <arpa/inet.h>
32*640eb22bSagc # include <ctype.h>
33*640eb22bSagc # include <inttypes.h>
34*640eb22bSagc # include <stdarg.h>
35*640eb22bSagc # include <stdio.h>
36*640eb22bSagc # include <stdlib.h>
37*640eb22bSagc # include <string.h>
38*640eb22bSagc # include <time.h>
39*640eb22bSagc # include <unistd.h>
40*640eb22bSagc #endif
41*640eb22bSagc
42*640eb22bSagc #include "rand.h"
43*640eb22bSagc
44*640eb22bSagc int
RAND_bytes(unsigned char * buf,int num)45*640eb22bSagc RAND_bytes(unsigned char *buf, int num)
46*640eb22bSagc {
47*640eb22bSagc uint32_t r;
48*640eb22bSagc size_t cc;
49*640eb22bSagc int i;
50*640eb22bSagc
51*640eb22bSagc if (buf == NULL || num < 0) {
52*640eb22bSagc return 0;
53*640eb22bSagc }
54*640eb22bSagc for (i = 0 ; i < num ; i += sizeof(r)) {
55*640eb22bSagc r = arc4random();
56*640eb22bSagc cc = MIN(sizeof(r), (size_t)(num - i));
57*640eb22bSagc (void) memcpy(&buf[i], &r, cc);
58*640eb22bSagc }
59*640eb22bSagc return 1;
60*640eb22bSagc }
61