blob: 6e2dd0c7ec0c80f0bbbf9d95435ff820ed6cf16e [file] [log] [blame]
Alexander Afanasyev6b997c52011-08-08 12:55:25 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -07002/*
Alexander Afanasyev6b997c52011-08-08 12:55:25 -07003 * Copyright (c) 2011 University of California, Los Angeles
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -07004 *
Alexander Afanasyev6b997c52011-08-08 12:55:25 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -07008 *
Alexander Afanasyev6b997c52011-08-08 12:55:25 -07009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
Ilya Moiseenkoc115fba2011-08-01 10:53:18 -070019 */
20
21#include "ccn_random.h"
22#include <openssl/rand.h>
23//#include <openssl/rand.c>
24
25/**
26 * Generate pseudo-random bytes.
27 *
28 * @param buf is the destination buffer
29 * @param size is in bytes
30 */
31void
32ccn_random_bytes(unsigned char *buf, size_t size)
33{
34 int num = size;
35
36 if (num < 0 || num != (int)size)
37 abort();
38 RAND_bytes(buf, num);
39}
40
41/**
42 * Feed some entropy to the random number generator.
43 *
44 * @param buf is the source buffer
45 * @param size is in bytes
46 * @param bits_of_entropy is an estimate; use 0 to make me guess
47 */
48void
49ccn_add_entropy(const void *buf, size_t size, int bits_of_entropy)
50{
51 int num = size;
52
53 if (num < 0 || num != (int)size)
54 abort();
55 /* Supply a hopefully conservative estimate of entropy. */
56 if (bits_of_entropy <= 0)
57 bits_of_entropy = (num < 32) ? 1 : num / 32;
58 RAND_add((unsigned char *)buf, num, bits_of_entropy * 0.125);
59}