Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

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