randstr.c

Go to the documentation of this file.
00001 /* randstr.c  --  Functions for getting random data
00002  *
00003  *  GPLv2 only - Copyright (C) 2009 - 2010
00004  *               David Sommerseth <dazo@users.sourceforge.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; version 2
00009  *  of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *
00020  */
00021 
00031 #include <stdio.h>
00032 #include <unistd.h>
00033 #include <openssl/rand.h>
00034 
00035 #include <eurephia_nullsafe.h>
00036 #include <eurephia_context.h>
00037 #include <eurephia_log.h>
00038 
00042 static int rand_init = 0;
00043 
00054 int eurephia_randstring(eurephiaCTX *ctx, void *rndstr, size_t len) {
00055         int attempts = 0;
00056         do {
00057                 if( !rand_init ) {
00058                         if( !RAND_load_file("/dev/urandom", 64) ) {
00059                                 eurephia_log(ctx, LOG_FATAL, 0, "Could not load random data from /dev/urandom");
00060                                 return 0;
00061                         }
00062                         rand_init = 1;
00063                 }
00064 
00065                 if( RAND_pseudo_bytes((unsigned char *) rndstr, len) ) {
00066                         return 1;
00067                 }
00068                 sleep(1);
00069                 rand_init = 0;
00070         } while( attempts++ < 11 );
00071         eurephia_log(ctx, LOG_FATAL, 0, "RAND_pseudo_bytes() could not generate enough random data");
00072         return 0;
00073 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines