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