*** empty log message ***
[gnutls.git] / lib / gnutls_random.c
blob6c64c87aac945ef3ea0425ea4ff6c088bd9e72b5
1 /*
2 * Copyright (C) 2001 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <gnutls_int.h>
23 #include <gnutls_random.h>
24 #include <gnutls_errors.h>
25 #ifndef USE_GCRYPT
26 # ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 # endif
29 # include <sys/types.h>
30 # include <sys/stat.h>
31 # include <fcntl.h>
32 #endif
34 /* fills the buffer 'res' with random bytes of 'bytes' long.
35 * level is WEAK, STRONG, or VERY_STRONG (libgcrypt)
37 int _gnutls_get_random(opaque * res, int bytes, int level)
39 #ifndef USE_GCRYPT
40 int fd;
41 char *device;
43 device = "dev/urandom";
45 fd = open(device, O_RDONLY);
46 if (device == NULL || fd < 0) {
47 _gnutls_log( "Could not open random device\n");
48 return GNUTLS_E_FILE_ERROR;
49 } else {
50 read(fd, res, bytes);
51 close(fd);
53 return 0;
54 #else /* using gcrypt */
55 gcry_randomize( res, bytes, level);
57 return 0;
58 #endif