1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
15 
16 
17 
18 /*
19  Functions to handle the encode() and decode() functions
20  The strongness of this crypt is large based on how good the random
21  generator is.	It should be ok for short strings, but for communication one
22  needs something like 'ssh'.
23 */
24 
25 #ifdef USE_PRAGMA_IMPLEMENTATION
26 #pragma implementation				// gcc: Class implementation
27 #endif
28 
29 #include "mariadb.h"
30 #include "sql_priv.h"
31 #include "sql_crypt.h"
32 #include "password.h"
33 
34 void SQL_CRYPT::init(ulong *rand_nr)
35 {
36   uint i;
37   my_rnd_init(&rand,rand_nr[0],rand_nr[1]);
38 
39   for (i=0 ; i<=255; i++)
40    decode_buff[i]= (char) i;
41 
42   for (i=0 ; i<= 255 ; i++)
43   {
44     int idx= (uint) (my_rnd(&rand)*255.0);
45     char a= decode_buff[idx];
46     decode_buff[idx]= decode_buff[i];
47     decode_buff[+i]=a;
48   }
49   for (i=0 ; i <= 255 ; i++)
50    encode_buff[(uchar) decode_buff[i]]=i;
51   org_rand=rand;
52   shift=0;
53 }
54 
55 
56 void SQL_CRYPT::encode(char *str,uint length)
57 {
58   for (uint i=0; i < length; i++)
59   {
60     shift^=(uint) (my_rnd(&rand)*255.0);
61     uint idx= (uint) (uchar) str[0];
62     *str++ = (char) ((uchar) encode_buff[idx] ^ shift);
63     shift^= idx;
64   }
65 }
66 
67 
68 void SQL_CRYPT::decode(char *str,uint length)
69 {
70   for (uint i=0; i < length; i++)
71   {
72     shift^=(uint) (my_rnd(&rand)*255.0);
73     uint idx= (uint) ((uchar) str[0] ^ shift);
74     *str = decode_buff[idx];
75     shift^= (uint) (uchar) *str++;
76   }
77 }
78