Changeset 6517


Ignore:
Timestamp:
Aug 14, 2008, 11:11:25 AM (13 years ago)
Author:
muks
Message:

Replace random number generation code

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/tr-prefs.c

    r6491 r6517  
    4141                        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    4242                        "1234567890";
     43    GRand *rand;
    4344
    4445    cf_check_older_configs( );
     
    113114    pref_string_set_default ( PREF_KEY_RPC_ACL, TR_DEFAULT_RPC_ACL );
    114115
     116    rand = g_rand_new ();
    115117    for( i=0; i<16; ++i )
    116         pw[i] = pool[ tr_rand( strlen( pool ) ) ];
     118        pw[i] = pool[ g_rand_int_range (rand, 0, strlen(pool))];
     119    g_rand_free (rand);
     120
    117121    pw[16] = '\0';
    118122    pref_string_set_default( PREF_KEY_RPC_USERNAME, "transmission" );
  • trunk/libtransmission/crypto.c

    r6480 r6517  
    1111 */
    1212
     13#include <stdlib.h> /* for abs() */
     14#include <values.h> /* for INT_MAX */
    1315#include <sys/types.h> /* for event.h, as well as netinet/in.h on some platforms */
    1416#include <assert.h>
     
    2123#include <openssl/rc4.h>
    2224#include <openssl/sha.h>
     25#include <openssl/rand.h>
    2326
    2427#include <event.h>
     
    265268    return crypto->torrentHashIsSet ? 1 : 0;
    266269}
     270
     271int tr_cryptoRandInt( int sup )
     272{
     273    int r;
     274
     275    RAND_pseudo_bytes ((unsigned char *) &r, sizeof r);
     276
     277    return ((int) (sup * (abs(r) / (INT_MAX + 1.0))));
     278}
     279
     280void tr_cryptoRandBuf ( unsigned char *buf, size_t len )
     281{
     282    RAND_pseudo_bytes ( buf, len );
     283}
     284
  • trunk/libtransmission/crypto.h

    r4404 r6517  
    7676                             ... );
    7777
     78
     79/** Returns a random number in the range of [0...n) */
     80int tr_cryptoRandInt ( int n );
     81
     82/** Fills a buffer with random bytes */
     83void tr_cryptoRandBuf ( unsigned char *buf, size_t len );
     84
    7885#endif
  • trunk/libtransmission/handshake.c

    r6496 r6517  
    278278sendYa( tr_handshake * handshake )
    279279{
    280     int i;
    281280    int len;
    282281    const uint8_t * public_key;
     
    291290
    292291    /* add some bullshit padding */
    293     len = tr_rand( PadA_MAXLEN );
    294     for( i=0; i<len; ++i )
    295         pad_a[i] = tr_rand( UCHAR_MAX );
     292    len = tr_cryptoRandInt( PadA_MAXLEN );
     293    tr_cryptoRandBuf(pad_a, len);
    296294    evbuffer_add( outbuf, pad_a, len );
    297295
     
    705703    tr_sha1( handshake->myReq1, "req1", 4, secret, KEY_LEN, NULL );
    706704
    707 dbgmsg( handshake, "sending B->A: Diffie Hellman Yb, PadB" );
     705    dbgmsg( handshake, "sending B->A: Diffie Hellman Yb, PadB" );
    708706    /* send our public key to the peer */
    709707    walk = outbuf;
     
    711709    memcpy( walk, myKey, len );
    712710    walk += len;
    713     len = tr_rand( PadB_MAXLEN );
    714     while( len-- )
    715         *walk++ = tr_rand( UCHAR_MAX );
     711    len = tr_cryptoRandInt( PadB_MAXLEN );
     712    tr_cryptoRandBuf( walk, len );
     713    walk += len;
    716714
    717715    setReadState( handshake, AWAITING_PAD_A );
  • trunk/libtransmission/peer-mgr.c

    r6468 r6517  
    601601            setme->priority = inf->pieces[piece].priority;
    602602            setme->peerCount = 0;
    603             setme->random = tr_rand( UINT16_MAX );
     603            setme->random = tr_cryptoRandInt( UINT16_MAX );
    604604            setme->missingBlockCount = tr_cpMissingBlocksInPiece( tor->completion, piece );
    605605
     
    643643    if( retCount ) {
    644644        tr_peer ** tmp = tr_new( tr_peer*, retCount );
    645         i = tr_rand( retCount );
     645        i = tr_cryptoRandInt( retCount );
    646646        memcpy( tmp, ret, sizeof(tr_peer*) * retCount );
    647647        memcpy( ret, tmp+i, sizeof(tr_peer*) * (retCount-i) );
     
    16311631        if(( n = tr_ptrArraySize( randPool )))
    16321632        {
    1633             c = tr_ptrArrayNth( randPool, tr_rand( n ));
     1633            c = tr_ptrArrayNth( randPool, tr_cryptoRandInt( n ));
    16341634            c->doUnchoke = 1;
    16351635            t->optimistic = c->peer;
  • trunk/libtransmission/session.c

    r6496 r6517  
    3838#include "utils.h"
    3939#include "web.h"
     40#include "crypto.h"
    4041
    4142/* Generate a peer id : "-TRxyzb-" + 12 random alphanumeric
     
    5657
    5758    for( i=8; i<19; ++i ) {
    58         val = tr_rand( base );
     59        val = tr_cryptoRandInt( base );
    5960        total += val;
    6061        buf[i] = pool[val];
  • trunk/libtransmission/torrent.c

    r6468 r6517  
    380380    for( i=0; i<n; ++i ) {
    381381        r[i].tracker = info->trackers[i];
    382         r[i].random_value = tr_rand( INT_MAX );
     382        r[i].random_value = tr_cryptoRandInt( INT_MAX );
    383383    }
    384384    qsort( r, n, sizeof( struct RandomTracker ), compareRandomTracker );
  • trunk/libtransmission/tracker.c

    r6490 r6517  
    1919#include "transmission.h"
    2020#include "bencode.h"
     21#include "crypto.h"
    2122#include "completion.h"
    2223#include "net.h"
     
    827828    const int poolSize = strlen( pool );
    828829    for( i=0; i<len; ++i )
    829         *msg++ = pool[tr_rand(poolSize)];
     830        *msg++ = pool[tr_cryptoRandInt(poolSize)];
    830831    *msg = '\0';
    831832}
     
    872873    t->manualAnnounceAllowedAt  = ~(time_t)0;
    873874    t->name = tr_strdup( info->name );
    874     t->randOffset = tr_rand( 30 );
     875    t->randOffset = tr_cryptoRandInt( 30 );
    875876    memcpy( t->hash, info->hash, SHA_DIGEST_LENGTH );
    876877    escape( t->escaped, info->hash, SHA_DIGEST_LENGTH );
  • trunk/libtransmission/utils.c

    r6507 r6517  
    266266}
    267267
    268 int tr_rand( int sup )
    269 {
    270     static int init = 0;
    271 
    272     assert( sup > 0 );
    273 
    274     if( !init )
    275     {
    276         srand( tr_date() );
    277         init = 1;
    278     }
    279     return rand() % sup;
    280 }
    281 
    282268/***
    283269****
  • trunk/libtransmission/utils.h

    r6400 r6517  
    117117
    118118char* tr_getLogTimeStr( char * buf, int buflen );
    119 
    120 /** Returns a random number in the range of [0...n) */
    121 int tr_rand ( int n );
    122119
    123120/**
Note: See TracChangeset for help on using the changeset viewer.