SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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