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