63 #include <my_global.h>
69 #include "crypt_genhash_impl.h"
82 void randominit(
struct rand_struct *rand_st, ulong seed1, ulong seed2)
85 memset(rand_st, 0,
sizeof(*rand_st));
87 rand_st->max_value= 0x3FFFFFFFL;
88 rand_st->max_value_dbl=(double) rand_st->max_value;
89 rand_st->seed1=seed1%rand_st->max_value ;
90 rand_st->seed2=seed2%rand_st->max_value;
103 void hash_password(ulong *result,
const char *password, uint password_len)
105 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
107 const char *password_end= password + password_len;
108 for (; password < password_end; password++)
110 if (*password ==
' ' || *password ==
'\t')
112 tmp= (ulong) (uchar) *password;
113 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
114 nr2+=(nr2 << 8) ^ nr;
117 result[0]=nr & (((ulong) 1L << 31) -1L); ;
118 result[1]=nr2 & (((ulong) 1L << 31) -1L);
132 void my_make_scrambled_password_323(
char *
to,
const char *password,
136 hash_password(hash_res, password, (uint) pass_len);
137 sprintf(to,
"%08lx%08lx", hash_res[0], hash_res[1]);
152 void make_scrambled_password_323(
char *to,
const char *password)
154 my_make_scrambled_password_323(to, password, strlen(password));
170 void scramble_323(
char *to,
const char *
message,
const char *password)
173 ulong hash_pass[2], hash_message[2];
175 if (password && password[0])
178 const char *message_end= message + SCRAMBLE_LENGTH_323;
179 hash_password(hash_pass,password, (uint) strlen(password));
180 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
181 randominit(&rand_st,hash_pass[0] ^ hash_message[0],
182 hash_pass[1] ^ hash_message[1]);
183 for (; message < message_end; message++)
184 *to++= (
char) (floor(my_rnd(&rand_st)*31)+64);
185 extra=(char) (floor(my_rnd(&rand_st)*31));
186 while (to_start != to)
187 *(to_start++)^=extra;
206 check_scramble_323(
const unsigned char *scrambled,
const char *message,
210 ulong hash_message[2];
212 uchar buff[16], scrambled_buff[SCRAMBLE_LENGTH_323 + 1];
217 memcpy(scrambled_buff, scrambled, SCRAMBLE_LENGTH_323);
218 scrambled_buff[SCRAMBLE_LENGTH_323]=
'\0';
219 scrambled= scrambled_buff;
221 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
222 randominit(&rand_st,hash_pass[0] ^ hash_message[0],
223 hash_pass[1] ^ hash_message[1]);
225 DBUG_ASSERT(
sizeof(buff) > SCRAMBLE_LENGTH_323);
226 for (pos=scrambled ; *pos && to < buff+
sizeof(buff) ; pos++)
227 *to++=(
char) (floor(my_rnd(&rand_st)*31)+64);
228 if (pos-scrambled != SCRAMBLE_LENGTH_323)
230 extra=(char) (floor(my_rnd(&rand_st)*31));
234 if (*scrambled++ != (uchar) (*to++ ^ extra))
240 static inline uint8 char_val(uint8 X)
242 return (uint) (X >=
'0' && X <=
'9' ? X-
'0' :
243 X >=
'A' && X <=
'Z' ? X-
'A'+10 : X-
'a'+10);
258 void get_salt_from_password_323(ulong *res,
const char *password)
267 for (i=0 ; i < 8 ; i++)
268 val=(val << 4)+char_val(*password++);
283 void make_password_from_salt_323(
char *to,
const ulong *salt)
285 sprintf(to,
"%08lx%08lx", salt[0], salt[1]);
308 void create_random_string(
char *to, uint length,
struct rand_struct *rand_st)
310 char *end= to + length;
315 for (; to < end; to++)
316 *to= (
char) (my_rnd(rand_st) * 94 + 33);
323 #define PVERSION41_CHAR '*'
338 char *octet2hex(
char *to,
const char *str, uint len)
340 const char *str_end= str + len;
341 for (; str != str_end; ++str)
343 *to++= _dig_vec_upper[((uchar) *str) >> 4];
344 *to++= _dig_vec_upper[((uchar) *str) & 0x0F];
362 hex2octet(uint8 *to,
const char *str, uint len)
364 const char *str_end= str + len;
365 while (str < str_end)
367 register char tmp= char_val(*str++);
368 *to++= (tmp << 4) | char_val(*str++);
386 my_crypt(
char *to,
const uchar *s1,
const uchar *s2, uint len)
388 const uint8 *s1_end= s1 + len;
390 *to++= *s1++ ^ *s2++;
393 #if defined(HAVE_OPENSSL)
394 void my_make_scrambled_password(
char *to,
const char *password,
398 char salt[CRYPT_SALT_LENGTH + 1];
400 generate_user_salt(salt, CRYPT_SALT_LENGTH + 1);
402 CRYPT_MAX_PASSWORD_SIZE,
423 void compute_two_stage_sha1_hash(
const char *password,
size_t pass_len,
424 uint8 *hash_stage1, uint8 *hash_stage2)
447 void my_make_scrambled_password_sha1(
char *to,
const char *password,
450 uint8 hash_stage2[SHA1_HASH_SIZE];
453 compute_two_stage_sha1_hash(password, pass_len, (uint8 *) to, hash_stage2);
456 *to++= PVERSION41_CHAR;
457 octet2hex(to, (
const char*) hash_stage2, SHA1_HASH_SIZE);
472 void make_scrambled_password(
char *to,
const char *password)
474 my_make_scrambled_password_sha1(to, password, strlen(password));
496 scramble(
char *to,
const char *message,
const char *password)
498 uint8 hash_stage1[SHA1_HASH_SIZE];
499 uint8 hash_stage2[SHA1_HASH_SIZE];
502 compute_two_stage_sha1_hash(password, strlen(password), hash_stage1,
507 (
const char *) hash_stage2, SHA1_HASH_SIZE);
508 my_crypt(to, (
const uchar *) to, hash_stage1, SCRAMBLE_LENGTH);
533 check_scramble_sha1(
const uchar *scramble_arg,
const char *message,
534 const uint8 *hash_stage2)
536 uint8
buf[SHA1_HASH_SIZE];
537 uint8 hash_stage2_reassured[SHA1_HASH_SIZE];
541 (
const char *) hash_stage2, SHA1_HASH_SIZE);
543 my_crypt((
char *) buf, buf, scramble_arg, SCRAMBLE_LENGTH);
548 return test(memcmp(hash_stage2, hash_stage2_reassured, SHA1_HASH_SIZE));
552 check_scramble(
const uchar *scramble_arg,
const char *message,
553 const uint8 *hash_stage2)
555 return check_scramble_sha1(scramble_arg, message, hash_stage2);
568 void get_salt_from_password(uint8 *hash_stage2,
const char *password)
570 hex2octet(hash_stage2, password+1 , SHA1_HASH_SIZE * 2);
581 void make_password_from_salt(
char *to,
const uint8 *hash_stage2)
583 *to++= PVERSION41_CHAR;
584 octet2hex(to, (
const char*) hash_stage2, SHA1_HASH_SIZE);