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