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