- Timestamp:
- 2006-06-28T14:47:05Z (18 years ago)
- Branches:
- master
- Children:
- 5c9512f
- Parents:
- b3c467b
- Location:
- lib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/misc.c
rb3c467b r1719464 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * 4 * Copyright 2002-200 4Wilmer van der Gaast and others *4 * Copyright 2002-2006 Wilmer van der Gaast and others * 5 5 \********************************************************************/ 6 6 … … 11 11 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> 12 12 * (and possibly other members of the Gaim team) 13 * Copyright 2002-200 5Wilmer van der Gaast <wilmer@gaast.net>13 * Copyright 2002-2006 Wilmer van der Gaast <wilmer@gaast.net> 14 14 */ 15 15 … … 422 422 } 423 423 424 char *set_eval_charset( irc_t *irc, set_t *set, char *value ) 425 { 426 GIConv cd; 427 428 if ( g_strncasecmp( value, "none", 4 ) == 0 ) 429 return( value ); 430 431 cd = g_iconv_open( "UTF-8", value ); 432 if( cd == (GIConv) -1 ) 433 return( NULL ); 434 435 g_iconv_close( cd ); 436 return( value ); 437 } 424 /* A pretty reliable random number generator. Tries to use the /dev/random 425 devices first, and falls back to the random number generator from libc 426 when it fails. Opens randomizer devices with O_NONBLOCK to make sure a 427 lack of entropy won't halt BitlBee. */ 428 void random_bytes( unsigned char *buf, int count ) 429 { 430 static int use_dev = -1; 431 432 /* Actually this probing code isn't really necessary, is it? */ 433 if( use_dev == -1 ) 434 { 435 if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) 436 use_dev = 1; 437 else 438 { 439 use_dev = 0; 440 srand( ( getpid() << 16 ) ^ time( NULL ) ); 441 } 442 } 443 444 if( use_dev ) 445 { 446 int fd; 447 448 /* At least on Linux, /dev/random can block if there's not 449 enough entropy. We really don't want that, so if it can't 450 give anything, use /dev/urandom instead. */ 451 if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) 452 if( read( fd, buf, count ) == count ) 453 { 454 close( fd ); 455 return; 456 } 457 close( fd ); 458 459 /* urandom isn't supposed to block at all, but just to be 460 sure. If it blocks, we'll disable use_dev and use the libc 461 randomizer instead. */ 462 if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) 463 if( read( fd, buf, count ) == count ) 464 { 465 close( fd ); 466 return; 467 } 468 close( fd ); 469 470 /* If /dev/random blocks once, we'll still try to use it 471 again next time. If /dev/urandom also fails for some 472 reason, stick with libc during this session. */ 473 474 use_dev = 0; 475 srand( ( getpid() << 16 ) ^ time( NULL ) ); 476 } 477 478 if( !use_dev ) 479 { 480 int i; 481 482 /* Possibly the LSB of rand() isn't very random on some 483 platforms. Seems okay on at least Linux and OSX though. */ 484 for( i = 0; i < count; i ++ ) 485 buf[i] = rand() & 0xff; 486 } 487 } -
lib/misc.h
rb3c467b r1719464 24 24 */ 25 25 26 #ifndef _UTIL_H 27 #define _UTIL_H 26 #ifndef _MISC_H 27 #define _MISC_H 28 29 #include <gmodule.h> 30 #include <time.h> 28 31 29 32 G_MODULE_EXPORT void strip_linefeed( gchar *text ); … … 45 48 46 49 G_MODULE_EXPORT signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ); 47 char *set_eval_charset( irc_t *irc, set_t *set, char *value ); 50 51 G_MODULE_EXPORT void random_bytes( unsigned char *buf, int count ); 48 52 49 53 #endif -
lib/rc4.c
rb3c467b r1719464 39 39 40 40 #include <glib.h> 41 #include <gmodule.h> 41 42 #include <stdlib.h> 42 43 #include <string.h> 44 #include "misc.h" 43 45 #include "rc4.h" 44 46 … … 134 136 key = g_malloc( key_len ); 135 137 strcpy( (char*) key, password ); 136 for( i = 0; i < RC4_IV_LEN; i ++ ) 137 key[key_len-RC4_IV_LEN+i] = crypt[0][i] = rand() & 0xff; 138 139 /* Add the salt. Save it for later (when decrypting) and, of course, 140 add it to the encryption key. */ 141 random_bytes( crypt[0], RC4_IV_LEN ); 142 memcpy( key + key_len - RC4_IV_LEN, crypt[0], RC4_IV_LEN ); 138 143 139 144 /* Generate the initial S[] from the IVed key. */
Note: See TracChangeset
for help on using the changeset viewer.