Ilya Moiseenko | c115fba | 2011-08-01 10:53:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ccn_random.cc |
| 3 | * Abstraction |
| 4 | * |
| 5 | * Created by Ilya on 7/29/11. |
| 6 | * Copyright 2011 __MyCompanyName__. All rights reserved. |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include "ccn_random.h" |
| 11 | #include <openssl/rand.h> |
| 12 | //#include <openssl/rand.c> |
| 13 | |
| 14 | /** |
| 15 | * Generate pseudo-random bytes. |
| 16 | * |
| 17 | * @param buf is the destination buffer |
| 18 | * @param size is in bytes |
| 19 | */ |
| 20 | void |
| 21 | ccn_random_bytes(unsigned char *buf, size_t size) |
| 22 | { |
| 23 | int num = size; |
| 24 | |
| 25 | if (num < 0 || num != (int)size) |
| 26 | abort(); |
| 27 | RAND_bytes(buf, num); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Feed some entropy to the random number generator. |
| 32 | * |
| 33 | * @param buf is the source buffer |
| 34 | * @param size is in bytes |
| 35 | * @param bits_of_entropy is an estimate; use 0 to make me guess |
| 36 | */ |
| 37 | void |
| 38 | ccn_add_entropy(const void *buf, size_t size, int bits_of_entropy) |
| 39 | { |
| 40 | int num = size; |
| 41 | |
| 42 | if (num < 0 || num != (int)size) |
| 43 | abort(); |
| 44 | /* Supply a hopefully conservative estimate of entropy. */ |
| 45 | if (bits_of_entropy <= 0) |
| 46 | bits_of_entropy = (num < 32) ? 1 : num / 32; |
| 47 | RAND_add((unsigned char *)buf, num, bits_of_entropy * 0.125); |
| 48 | } |