1 /* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 
24 
25 /*
26  Functions to handle the encode() and decode() functions
27  The strongness of this crypt is large based on how good the random
28  generator is.	It should be ok for short strings, but for communication one
29  needs something like 'ssh'.
30 
31  WARNING: This class is deprecated and will be removed in the next
32  server version. Please use AES encrypt/decrypt instead
33 */
34 
35 #include "sql_crypt.h"
36 #include "password.h"
37 
init(ulong * rand_nr)38 void SQL_CRYPT::init(ulong *rand_nr)
39 {
40   uint i;
41   randominit(&rand,rand_nr[0],rand_nr[1]);
42 
43   for (i=0 ; i<=255; i++)
44    decode_buff[i]= (char) i;
45 
46   for (i=0 ; i<= 255 ; i++)
47   {
48     int idx= (uint) (my_rnd(&rand)*255.0);
49     char a= decode_buff[idx];
50     decode_buff[idx]= decode_buff[i];
51     decode_buff[+i]=a;
52   }
53   for (i=0 ; i <= 255 ; i++)
54    encode_buff[(uchar) decode_buff[i]]=i;
55   org_rand=rand;
56   shift=0;
57 }
58 
59 
encode(char * str,size_t length)60 void SQL_CRYPT::encode(char *str, size_t length)
61 {
62   for (size_t i=0; i < length; i++)
63   {
64     shift^=(uint) (my_rnd(&rand)*255.0);
65     uint idx= (uint) (uchar) str[0];
66     *str++ = (char) ((uchar) encode_buff[idx] ^ shift);
67     shift^= idx;
68   }
69 }
70 
71 
decode(char * str,size_t length)72 void SQL_CRYPT::decode(char *str, size_t length)
73 {
74   for (size_t i=0; i < length; i++)
75   {
76     shift^=(uint) (my_rnd(&rand)*255.0);
77     uint idx= (uint) ((uchar) str[0] ^ shift);
78     *str = decode_buff[idx];
79     shift^= (uint) (uchar) *str++;
80   }
81 }
82