Dirk-Willem van Gulik (NXP/mbed) / Mbed 2 deprecated Bonjour

Dependencies:   mbed

Committer:
dirkx
Date:
Wed Jul 21 19:25:56 2010 +0000
Revision:
0:355018f44c9f

        

Who changed what in which revision?

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