Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 1998-1999 Brian Bruns
3 : * Copyright (C) 2005-2015 Frediano Ziglio
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Library General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2 of the License, or (at your option) any later version.
9 : *
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Library General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Library General Public
16 : * License along with this library; if not, write to the
17 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 : * Boston, MA 02111-1307, USA.
19 : */
20 :
21 : #include <config.h>
22 :
23 : #if HAVE_STDLIB_H
24 : #include <stdlib.h>
25 : #endif /* HAVE_STDLIB_H */
26 :
27 : #include <freetds/tds.h>
28 :
29 : #ifdef HAVE_GNUTLS
30 : #include <gnutls/gnutls.h>
31 : #include <gnutls/crypto.h>
32 : #ifndef HAVE_GNUTLS_RND
33 : #include <gcrypt.h>
34 : #endif
35 : #elif defined(HAVE_OPENSSL)
36 : #include <openssl/rand.h>
37 : #endif
38 :
39 : void
40 714 : tds_random_buffer(unsigned char *out, int len)
41 : {
42 : int i;
43 :
44 : #if defined(HAVE_GNUTLS) && defined(HAVE_GNUTLS_RND)
45 357 : if (gnutls_rnd(GNUTLS_RND_RANDOM, out, len) >= 0)
46 : return;
47 0 : if (gnutls_rnd(GNUTLS_RND_NONCE, out, len) >= 0)
48 : return;
49 : #elif defined(HAVE_GNUTLS)
50 : void *p = gcry_random_bytes(len, GCRY_STRONG_RANDOM);
51 : if (p) {
52 : memcpy(out, p, len);
53 : free(p);
54 : return;
55 : }
56 : #elif defined(HAVE_OPENSSL)
57 357 : if (RAND_bytes(out, len) == 1)
58 : return;
59 : #endif
60 :
61 : /* TODO find a better random... */
62 0 : for (i = 0; i < len; ++i)
63 0 : out[i] = rand() / (RAND_MAX / 256);
64 : }
|