NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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