1 /*
2    Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    Without limiting anything contained in the foregoing, this file,
16    which is part of C Driver for MySQL (Connector/C), is also subject to the
17    Universal FOSS Exception, version 1.0, a copy of which can be found at
18    http://oss.oracle.com/licenses/universal-foss-exception.
19 
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License, version 2.0, for more details.
24 
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
28 
29 #include <my_rnd.h>
30 
31 #if defined(HAVE_OPENSSL)
32 #include <openssl/rand.h>
33 #include <openssl/err.h>
34 #endif /* HAVE_OPENSSL */
35 
36 
37 /*
38   A wrapper to use OpenSSL PRNGs.
39 */
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46   Generate random number.
47 
48   @param rand_st [INOUT] Structure used for number generation.
49 
50   @retval                Generated pseudo random number.
51 */
52 
my_rnd(struct rand_struct * rand_st)53 double my_rnd(struct rand_struct *rand_st)
54 {
55   rand_st->seed1= (rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
56   rand_st->seed2= (rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
57   return (((double) rand_st->seed1) / rand_st->max_value_dbl);
58 }
59 
60 
61 
62 /**
63 Fill a buffer with random bytes using the SSL library routines
64 
65 @param buffer       [OUT]   Buffer to receive the random data
66 @param buffer_size  [IN]    sizeof the the buffer
67 
68 @retval      1  error ocurred.
69 @retval      0  OK
70 */
71 int
my_rand_buffer(unsigned char * buffer,size_t buffer_size)72 my_rand_buffer(unsigned char *buffer, size_t buffer_size)
73 {
74   int rc;
75 #if defined(HAVE_OPENSSL)
76   rc= RAND_bytes(buffer, buffer_size);
77 
78   if (!rc)
79   {
80     ERR_clear_error();
81     return 1;
82   }
83 #else /* no SSL */
84 #error not using an SSL library not supported
85 #endif
86   return 0;
87 }
88 
89 
90 /**
91   Generate a random number using the OpenSSL supplied
92   random number generator if available.
93 
94   @param rand_st [INOUT] Structure used for number generation
95                          only if none of the SSL libraries are
96                          available.
97 
98   @retval                Generated random number.
99 */
100 
my_rnd_ssl(struct rand_struct * rand_st)101 double my_rnd_ssl(struct rand_struct *rand_st)
102 {
103   unsigned int res;
104 
105   if (my_rand_buffer((unsigned char *) &res, sizeof(res)))
106     return my_rnd(rand_st);
107 
108   return (double)res / (double)UINT_MAX;
109 }
110 
111 
112 #ifdef __cplusplus
113 }
114 #endif
115