Rik Van Dyck / Mbed 2 deprecated Project_Embedded_C

Dependencies:   mbed DS1307 Servo TextLCD

Committer:
rikvandyck
Date:
Thu Dec 04 10:44:07 2014 +0000
Revision:
0:e1edd52b1ee2
test

Who changed what in which revision?

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