Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ICTFBI 0:4edb816d21e1 1 /* Copyright (C) 2007 MySQL AB & Michael Widenius
ICTFBI 0:4edb816d21e1 2
ICTFBI 0:4edb816d21e1 3 This program is free software; you can redistribute it and/or modify
ICTFBI 0:4edb816d21e1 4 it under the terms of the GNU General Public License as published by
ICTFBI 0:4edb816d21e1 5 the Free Software Foundation; version 2 of the License.
ICTFBI 0:4edb816d21e1 6
ICTFBI 0:4edb816d21e1 7 This program is distributed in the hope that it will be useful,
ICTFBI 0:4edb816d21e1 8 but WITHOUT ANY WARRANTY; without even the implied warranty of
ICTFBI 0:4edb816d21e1 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ICTFBI 0:4edb816d21e1 10 GNU General Public License for more details.
ICTFBI 0:4edb816d21e1 11
ICTFBI 0:4edb816d21e1 12 You should have received a copy of the GNU General Public License
ICTFBI 0:4edb816d21e1 13 along with this program; if not, write to the Free Software
ICTFBI 0:4edb816d21e1 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
ICTFBI 0:4edb816d21e1 15
ICTFBI 0:4edb816d21e1 16 #define SCRAMBLE_LENGTH_323 8
ICTFBI 0:4edb816d21e1 17
ICTFBI 0:4edb816d21e1 18 #include <string.h>
ICTFBI 0:4edb816d21e1 19 #include <math.h>
ICTFBI 0:4edb816d21e1 20
ICTFBI 0:4edb816d21e1 21 typedef unsigned int uint;
ICTFBI 0:4edb816d21e1 22 typedef unsigned long ulong;
ICTFBI 0:4edb816d21e1 23 typedef unsigned char uchar;
ICTFBI 0:4edb816d21e1 24
ICTFBI 0:4edb816d21e1 25 struct my_rnd_struct {
ICTFBI 0:4edb816d21e1 26 unsigned long seed1,seed2,max_value;
ICTFBI 0:4edb816d21e1 27 double max_value_dbl;
ICTFBI 0:4edb816d21e1 28 };
ICTFBI 0:4edb816d21e1 29
ICTFBI 0:4edb816d21e1 30 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2);
ICTFBI 0:4edb816d21e1 31 static double my_rnd(struct my_rnd_struct *rand_st);
ICTFBI 0:4edb816d21e1 32 static void hash_password(ulong *result, const char *password, uint password_len);
ICTFBI 0:4edb816d21e1 33
ICTFBI 0:4edb816d21e1 34 /*
ICTFBI 0:4edb816d21e1 35 Initialize random generator
ICTFBI 0:4edb816d21e1 36
ICTFBI 0:4edb816d21e1 37 NOTES
ICTFBI 0:4edb816d21e1 38 MySQL's password checks depends on this, so don't do any changes
ICTFBI 0:4edb816d21e1 39 that changes the random numbers that are generated!
ICTFBI 0:4edb816d21e1 40 */
ICTFBI 0:4edb816d21e1 41
ICTFBI 0:4edb816d21e1 42 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
ICTFBI 0:4edb816d21e1 43 {
ICTFBI 0:4edb816d21e1 44 rand_st->max_value= 0x3FFFFFFFL;
ICTFBI 0:4edb816d21e1 45 rand_st->max_value_dbl=(double) rand_st->max_value;
ICTFBI 0:4edb816d21e1 46 rand_st->seed1=seed1%rand_st->max_value ;
ICTFBI 0:4edb816d21e1 47 rand_st->seed2=seed2%rand_st->max_value;
ICTFBI 0:4edb816d21e1 48 }
ICTFBI 0:4edb816d21e1 49
ICTFBI 0:4edb816d21e1 50 /*
ICTFBI 0:4edb816d21e1 51 Generate random number.
ICTFBI 0:4edb816d21e1 52
ICTFBI 0:4edb816d21e1 53 SYNOPSIS
ICTFBI 0:4edb816d21e1 54 my_rnd()
ICTFBI 0:4edb816d21e1 55 rand_st INOUT Structure used for number generation
ICTFBI 0:4edb816d21e1 56
ICTFBI 0:4edb816d21e1 57 RETURN VALUE
ICTFBI 0:4edb816d21e1 58 generated pseudo random number
ICTFBI 0:4edb816d21e1 59 */
ICTFBI 0:4edb816d21e1 60
ICTFBI 0:4edb816d21e1 61 static double my_rnd(struct my_rnd_struct *rand_st)
ICTFBI 0:4edb816d21e1 62 {
ICTFBI 0:4edb816d21e1 63 rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
ICTFBI 0:4edb816d21e1 64 rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
ICTFBI 0:4edb816d21e1 65 return (((double) rand_st->seed1)/rand_st->max_value_dbl);
ICTFBI 0:4edb816d21e1 66 }
ICTFBI 0:4edb816d21e1 67
ICTFBI 0:4edb816d21e1 68 /*
ICTFBI 0:4edb816d21e1 69 Generate binary hash from raw text string
ICTFBI 0:4edb816d21e1 70 Used for Pre-4.1 password handling
ICTFBI 0:4edb816d21e1 71 SYNOPSIS
ICTFBI 0:4edb816d21e1 72 hash_password()
ICTFBI 0:4edb816d21e1 73 result OUT store hash in this location
ICTFBI 0:4edb816d21e1 74 password IN plain text password to build hash
ICTFBI 0:4edb816d21e1 75 password_len IN password length (password may be not null-terminated)
ICTFBI 0:4edb816d21e1 76 */
ICTFBI 0:4edb816d21e1 77
ICTFBI 0:4edb816d21e1 78 static void hash_password(ulong *result, const char *password, uint password_len)
ICTFBI 0:4edb816d21e1 79 {
ICTFBI 0:4edb816d21e1 80 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
ICTFBI 0:4edb816d21e1 81 ulong tmp;
ICTFBI 0:4edb816d21e1 82 const char *password_end= password + password_len;
ICTFBI 0:4edb816d21e1 83 for (; password < password_end; password++)
ICTFBI 0:4edb816d21e1 84 {
ICTFBI 0:4edb816d21e1 85 if (*password == ' ' || *password == '\t')
ICTFBI 0:4edb816d21e1 86 continue; /* skip space in password */
ICTFBI 0:4edb816d21e1 87 tmp= (ulong) (uchar) *password;
ICTFBI 0:4edb816d21e1 88 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
ICTFBI 0:4edb816d21e1 89 nr2+=(nr2 << 8) ^ nr;
ICTFBI 0:4edb816d21e1 90 add+=tmp;
ICTFBI 0:4edb816d21e1 91 }
ICTFBI 0:4edb816d21e1 92 result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
ICTFBI 0:4edb816d21e1 93 result[1]=nr2 & (((ulong) 1L << 31) -1L);
ICTFBI 0:4edb816d21e1 94 }
ICTFBI 0:4edb816d21e1 95
ICTFBI 0:4edb816d21e1 96
ICTFBI 0:4edb816d21e1 97
ICTFBI 0:4edb816d21e1 98 /*
ICTFBI 0:4edb816d21e1 99 Scramble string with password.
ICTFBI 0:4edb816d21e1 100 Used in pre 4.1 authentication phase.
ICTFBI 0:4edb816d21e1 101 SYNOPSIS
ICTFBI 0:4edb816d21e1 102 scramble_323()
ICTFBI 0:4edb816d21e1 103 to OUT Store scrambled message here. Buffer must be at least
ICTFBI 0:4edb816d21e1 104 SCRAMBLE_LENGTH_323+1 bytes long
ICTFBI 0:4edb816d21e1 105 message IN Message to scramble. Message must be at least
ICTFBI 0:4edb816d21e1 106 SRAMBLE_LENGTH_323 bytes long.
ICTFBI 0:4edb816d21e1 107 password IN Password to use while scrambling
ICTFBI 0:4edb816d21e1 108 */
ICTFBI 0:4edb816d21e1 109
ICTFBI 0:4edb816d21e1 110 void scramble_323(char *to, const char *message, const char *password)
ICTFBI 0:4edb816d21e1 111 {
ICTFBI 0:4edb816d21e1 112 struct my_rnd_struct rand_st;
ICTFBI 0:4edb816d21e1 113 ulong hash_pass[2], hash_message[2];
ICTFBI 0:4edb816d21e1 114
ICTFBI 0:4edb816d21e1 115 if (password && password[0])
ICTFBI 0:4edb816d21e1 116 {
ICTFBI 0:4edb816d21e1 117 char extra, *to_start=to;
ICTFBI 0:4edb816d21e1 118 const char *message_end= message + SCRAMBLE_LENGTH_323;
ICTFBI 0:4edb816d21e1 119 hash_password(hash_pass,password, (uint) strlen(password));
ICTFBI 0:4edb816d21e1 120 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
ICTFBI 0:4edb816d21e1 121 my_rnd_init(&rand_st,hash_pass[0] ^ hash_message[0],
ICTFBI 0:4edb816d21e1 122 hash_pass[1] ^ hash_message[1]);
ICTFBI 0:4edb816d21e1 123 for (; message < message_end; message++)
ICTFBI 0:4edb816d21e1 124 *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
ICTFBI 0:4edb816d21e1 125 extra=(char) (floor(my_rnd(&rand_st)*31));
ICTFBI 0:4edb816d21e1 126 while (to_start != to)
ICTFBI 0:4edb816d21e1 127 *(to_start++)^=extra;
ICTFBI 0:4edb816d21e1 128 }
ICTFBI 0:4edb816d21e1 129 *to= 0;
ICTFBI 0:4edb816d21e1 130 }
ICTFBI 0:4edb816d21e1 131