Jan Kundrát | 6b05a1d | 2019-03-06 17:01:28 +0100 | [diff] [blame] | 1 | /* rndaddentropy, an RNDADDENTROPY ioctl wrapper |
| 2 | * Copyright (C) 2012 Ryan Finnie <ryan@finnie.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/fcntl.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <linux/random.h> |
| 25 | |
| 26 | int main(int argc, char *argv[]) { |
| 27 | struct { |
| 28 | int entropy_count; |
| 29 | int buf_size; |
| 30 | char buf[8192]; |
| 31 | } entropy; |
| 32 | |
| 33 | int i; |
| 34 | for(i=1; i < argc; i++) { |
| 35 | if(strcmp(argv[i], "--help") == 0) { |
| 36 | fprintf(stderr, "rndaddentropy, an RNDADDENTROPY ioctl wrapper\n"); |
| 37 | fprintf(stderr, "Copyright (C) 2012 Ryan Finnie <ryan@finnie.org>\n"); |
| 38 | fprintf(stderr, "\n"); |
| 39 | fprintf(stderr, "Usage: $ENTROPY_GENERATOR | rndaddentropy\n"); |
| 40 | fprintf(stderr, "\n"); |
| 41 | fprintf(stderr, "WARNING! This program is dangerous, and relies on your entropy\n"); |
| 42 | fprintf(stderr, "generator producing adequate output. Inadequate entropy generation\n"); |
| 43 | fprintf(stderr, "fed to the primary pool is a security risk to the system.\n"); |
| 44 | return(1); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | int randfd; |
| 49 | if((randfd = open("/dev/random", O_WRONLY)) < 0) { |
| 50 | perror("/dev/random"); |
| 51 | return(1); |
| 52 | } |
| 53 | |
| 54 | int count; |
| 55 | while((count = fread(entropy.buf, 1, sizeof(entropy.buf), stdin)) > 0) { |
| 56 | // Jan Kundrat: be more conservative -- one bit of entropy per 16 bytes of randomness |
| 57 | entropy.entropy_count = count / 16; |
| 58 | if (entropy.entropy_count < 1) { |
| 59 | entropy.entropy_count = 1; |
| 60 | } |
| 61 | entropy.buf_size = count; |
| 62 | if(ioctl(randfd, RNDADDENTROPY, &entropy) < 0) { |
| 63 | perror("RNDADDENTROPY"); |
| 64 | return(1); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return(0); |
| 69 | } |
| 70 | |