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