change to VodafoneUSB Modem interface
Fork of MySQLClient by
LPC1768/services/mysql/encrypt.c@6:cb3bd3012d79, 2013-11-22 (annotated)
- Committer:
- loc_tran17
- Date:
- Fri Nov 22 07:38:25 2013 +0000
- Revision:
- 6:cb3bd3012d79
trial with MySQLClient
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
loc_tran17 | 6:cb3bd3012d79 | 1 | #include <stdio.h> |
loc_tran17 | 6:cb3bd3012d79 | 2 | #include <string.h> |
loc_tran17 | 6:cb3bd3012d79 | 3 | #include <math.h> |
loc_tran17 | 6:cb3bd3012d79 | 4 | #include "mycrypt.h" |
loc_tran17 | 6:cb3bd3012d79 | 5 | #define SCRAMBLE_LENGTH_323 8 |
loc_tran17 | 6:cb3bd3012d79 | 6 | |
loc_tran17 | 6:cb3bd3012d79 | 7 | typedef unsigned long ulong; |
loc_tran17 | 6:cb3bd3012d79 | 8 | typedef unsigned int uint; |
loc_tran17 | 6:cb3bd3012d79 | 9 | typedef unsigned char uchar; |
loc_tran17 | 6:cb3bd3012d79 | 10 | |
loc_tran17 | 6:cb3bd3012d79 | 11 | struct rand_struct { |
loc_tran17 | 6:cb3bd3012d79 | 12 | unsigned long seed1,seed2,max_value; |
loc_tran17 | 6:cb3bd3012d79 | 13 | double max_value_dbl; |
loc_tran17 | 6:cb3bd3012d79 | 14 | }; |
loc_tran17 | 6:cb3bd3012d79 | 15 | |
loc_tran17 | 6:cb3bd3012d79 | 16 | void hash_password(ulong *result, const char *password, uint password_len) |
loc_tran17 | 6:cb3bd3012d79 | 17 | { |
loc_tran17 | 6:cb3bd3012d79 | 18 | register ulong nr=1345345333L, add=7, nr2=0x12345671L; |
loc_tran17 | 6:cb3bd3012d79 | 19 | ulong tmp; |
loc_tran17 | 6:cb3bd3012d79 | 20 | const char *password_end= password + password_len; |
loc_tran17 | 6:cb3bd3012d79 | 21 | for (; password < password_end; password++) |
loc_tran17 | 6:cb3bd3012d79 | 22 | { |
loc_tran17 | 6:cb3bd3012d79 | 23 | if (*password == ' ' || *password == '\t') |
loc_tran17 | 6:cb3bd3012d79 | 24 | continue; /* skip space in password */ |
loc_tran17 | 6:cb3bd3012d79 | 25 | tmp= (ulong) (uchar) *password; |
loc_tran17 | 6:cb3bd3012d79 | 26 | nr^= (((nr & 63)+add)*tmp)+ (nr << 8); |
loc_tran17 | 6:cb3bd3012d79 | 27 | nr2+=(nr2 << 8) ^ nr; |
loc_tran17 | 6:cb3bd3012d79 | 28 | add+=tmp; |
loc_tran17 | 6:cb3bd3012d79 | 29 | } |
loc_tran17 | 6:cb3bd3012d79 | 30 | result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */; |
loc_tran17 | 6:cb3bd3012d79 | 31 | result[1]=nr2 & (((ulong) 1L << 31) -1L); |
loc_tran17 | 6:cb3bd3012d79 | 32 | } |
loc_tran17 | 6:cb3bd3012d79 | 33 | |
loc_tran17 | 6:cb3bd3012d79 | 34 | void randominit(struct rand_struct *rand_st, ulong seed1, ulong seed2) |
loc_tran17 | 6:cb3bd3012d79 | 35 | { /* For mysql 3.21.# */ |
loc_tran17 | 6:cb3bd3012d79 | 36 | #ifdef HAVE_purify |
loc_tran17 | 6:cb3bd3012d79 | 37 | bzero((char*) rand_st,sizeof(*rand_st)); /* Avoid UMC varnings */ |
loc_tran17 | 6:cb3bd3012d79 | 38 | #endif |
loc_tran17 | 6:cb3bd3012d79 | 39 | rand_st->max_value= 0x3FFFFFFFL; |
loc_tran17 | 6:cb3bd3012d79 | 40 | rand_st->max_value_dbl=(double) rand_st->max_value; |
loc_tran17 | 6:cb3bd3012d79 | 41 | rand_st->seed1=seed1%rand_st->max_value ; |
loc_tran17 | 6:cb3bd3012d79 | 42 | rand_st->seed2=seed2%rand_st->max_value; |
loc_tran17 | 6:cb3bd3012d79 | 43 | } |
loc_tran17 | 6:cb3bd3012d79 | 44 | |
loc_tran17 | 6:cb3bd3012d79 | 45 | double my_rnd(struct rand_struct *rand_st) |
loc_tran17 | 6:cb3bd3012d79 | 46 | { |
loc_tran17 | 6:cb3bd3012d79 | 47 | rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value; |
loc_tran17 | 6:cb3bd3012d79 | 48 | rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value; |
loc_tran17 | 6:cb3bd3012d79 | 49 | return (((double) rand_st->seed1)/rand_st->max_value_dbl); |
loc_tran17 | 6:cb3bd3012d79 | 50 | } |
loc_tran17 | 6:cb3bd3012d79 | 51 | |
loc_tran17 | 6:cb3bd3012d79 | 52 | void scramble_323(char *to, const char *message, const char *password) |
loc_tran17 | 6:cb3bd3012d79 | 53 | { |
loc_tran17 | 6:cb3bd3012d79 | 54 | struct rand_struct rand_st; |
loc_tran17 | 6:cb3bd3012d79 | 55 | ulong hash_pass[2], hash_message[2]; |
loc_tran17 | 6:cb3bd3012d79 | 56 | |
loc_tran17 | 6:cb3bd3012d79 | 57 | if (password && password[0]) |
loc_tran17 | 6:cb3bd3012d79 | 58 | { |
loc_tran17 | 6:cb3bd3012d79 | 59 | char extra, *to_start=to; |
loc_tran17 | 6:cb3bd3012d79 | 60 | const char *message_end= message + SCRAMBLE_LENGTH_323; |
loc_tran17 | 6:cb3bd3012d79 | 61 | hash_password(hash_pass,password, (uint) strlen(password)); |
loc_tran17 | 6:cb3bd3012d79 | 62 | hash_password(hash_message, message, SCRAMBLE_LENGTH_323); |
loc_tran17 | 6:cb3bd3012d79 | 63 | randominit(&rand_st,hash_pass[0] ^ hash_message[0], |
loc_tran17 | 6:cb3bd3012d79 | 64 | hash_pass[1] ^ hash_message[1]); |
loc_tran17 | 6:cb3bd3012d79 | 65 | for (; message < message_end; message++) |
loc_tran17 | 6:cb3bd3012d79 | 66 | *to++= (char) (floor(my_rnd(&rand_st)*31)+64); |
loc_tran17 | 6:cb3bd3012d79 | 67 | extra=(char) (floor(my_rnd(&rand_st)*31)); |
loc_tran17 | 6:cb3bd3012d79 | 68 | while (to_start != to) |
loc_tran17 | 6:cb3bd3012d79 | 69 | *(to_start++)^=extra; |
loc_tran17 | 6:cb3bd3012d79 | 70 | } |
loc_tran17 | 6:cb3bd3012d79 | 71 | *to= 0; |
loc_tran17 | 6:cb3bd3012d79 | 72 | } |