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 /**
30   @file mysys/my_rnd.cc
31 */
32 
33 #include "my_rnd.h"
34 #include <mysql_com.h>
35 #include <openssl/err.h>
36 #include <openssl/rand.h>
37 
38 /*
39   A wrapper to use OpenSSL PRNGs.
40 */
41 
42 /**
43   Generate random number.
44 
45   @param [in,out] rand_st Structure used for number generation.
46 
47   @retval                Generated pseudo random number.
48 */
49 
my_rnd(struct rand_struct * rand_st)50 double my_rnd(struct rand_struct *rand_st) {
51   rand_st->seed1 = (rand_st->seed1 * 3 + rand_st->seed2) % rand_st->max_value;
52   rand_st->seed2 = (rand_st->seed1 + rand_st->seed2 + 33) % rand_st->max_value;
53   return (((double)rand_st->seed1) / rand_st->max_value_dbl);
54 }
55 
56 /**
57 Fill a buffer with random bytes using the SSL library routines
58 
59 @param [out] buffer          Buffer to receive the random data
60 @param [in] buffer_size      sizeof the the buffer
61 
62 @retval      1  error occurred.
63 @retval      0  OK
64 */
my_rand_buffer(unsigned char * buffer,size_t buffer_size)65 int my_rand_buffer(unsigned char *buffer, size_t buffer_size) {
66   int rc;
67   rc = RAND_bytes(buffer, (int)buffer_size);
68 
69   if (!rc) {
70     ERR_clear_error();
71     return 1;
72   }
73   return 0;
74 }
75 
76 /**
77   Generate a random number using the OpenSSL supplied
78   random number generator if available.
79 
80   @param [out] failed set to TRUE if the method failed. FALSE if OK.
81 
82   @retval                Generated random number or 0 if failed is set.
83 */
84 
my_rnd_ssl(bool * failed)85 double my_rnd_ssl(bool *failed) {
86   unsigned int res;
87 
88   if (my_rand_buffer((unsigned char *)&res, sizeof(res))) {
89     *failed = true;
90     return 0;
91   } else
92     *failed = false;
93   return (double)res / (double)UINT_MAX;
94 }
95