Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 6b997c5 | 2011-08-08 12:55:25 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011 University of California, Los Angeles |
Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 6b997c5 | 2011-08-08 12:55:25 -0700 | [diff] [blame] | 5 | * 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 Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 8 | * |
Alexander Afanasyev | 6b997c5 | 2011-08-08 12:55:25 -0700 | [diff] [blame] | 9 | * 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 Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 19 | */ |
| 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 | */ |
| 31 | void |
| 32 | ccn_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 | */ |
| 48 | void |
| 49 | ccn_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 | } |