Changeset 3405


Ignore:
Timestamp:
Oct 13, 2007, 11:15:43 PM (15 years ago)
Author:
charles
Message:

Try sharing a DH so that we don't spend so many cpu cycles making new ones when we're working through the peers trying to find good ones.

Location:
trunk/libtransmission
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/crypto.c

    r3360 r3405  
    7777struct tr_crypto
    7878{
    79     DH * dh;
    8079    RC4_KEY dec_key;
    8180    RC4_KEY enc_key;
     
    8685    uint8_t myPublicKey[KEY_LEN];
    8786    uint8_t mySecret[KEY_LEN];
    88 
    8987};
    9088
    91 static void
    92 ensureKeyExists( tr_crypto * crypto )
    93 {
    94     if( crypto->dh == NULL )
     89/**
     90***
     91**/
     92
     93static DH*
     94getSharedDH( void )
     95{
     96    static DH * dh = NULL;
     97
     98    if( dh == NULL )
    9599    {
    96         int len, offset;
    97 
    98         crypto->dh = DH_new( );
    99         crypto->dh->p = BN_bin2bn( dh_P, sizeof(dh_P), NULL );
    100         crypto->dh->g = BN_bin2bn( dh_G, sizeof(dh_G), NULL );
    101         DH_generate_key( crypto->dh );
    102 
    103         /* DH can generate key sizes that are smaller than the size of
    104            P with exponentially decreasing probability, in which case
    105            the msb's of myPublicKey need to be zeroed appropriately. */
    106         len = DH_size( crypto->dh );
    107         offset = KEY_LEN - len;
    108         assert( len <= KEY_LEN );
    109         memset( crypto->myPublicKey, 0, offset );
    110         BN_bn2bin( crypto->dh->pub_key, crypto->myPublicKey + offset );
     100        dh = DH_new( );
     101        dh->p = BN_bin2bn( dh_P, sizeof(dh_P), NULL );
     102        dh->g = BN_bin2bn( dh_G, sizeof(dh_G), NULL );
     103        DH_generate_key( dh );
    111104    }
    112 }
    113 
    114 /**
    115 ***
    116 **/
     105
     106    return dh;
     107}
    117108
    118109tr_crypto *
     
    120111              int             isIncoming )
    121112{
     113    int len, offset;
    122114    tr_crypto * crypto;
     115    DH * dh = getSharedDH( );
    123116
    124117    crypto = tr_new0( tr_crypto, 1 );
    125118    crypto->isIncoming = isIncoming ? 1 : 0;
    126     crypto->dh = NULL;
    127119    tr_cryptoSetTorrentHash( crypto, torrentHash );
     120
     121    /* DH can generate key sizes that are smaller than the size of
     122       P with exponentially decreasing probability, in which case
     123       the msb's of myPublicKey need to be zeroed appropriately. */
     124    len = DH_size( dh );
     125    offset = KEY_LEN - len;
     126    assert( len <= KEY_LEN );
     127    memset( crypto->myPublicKey, 0, offset );
     128    BN_bn2bin( dh->pub_key, crypto->myPublicKey + offset );
     129
    128130    return crypto;
    129131}
     
    132134tr_cryptoFree( tr_crypto * crypto )
    133135{
    134     assert( crypto != NULL );
    135     if( crypto->dh != NULL )
    136         DH_free( crypto->dh );
    137136    tr_free( crypto );
    138137}
     
    149148    uint8_t secret[KEY_LEN];
    150149    BIGNUM * bn = BN_bin2bn( peerPublicKey, KEY_LEN, NULL );
    151 
    152     ensureKeyExists( crypto );
    153     assert( DH_size( crypto->dh ) == KEY_LEN );
    154 
    155     len = DH_compute_key( secret, bn, crypto->dh );
     150    DH * dh = getSharedDH( );
     151    assert( DH_size(dh) == KEY_LEN );
     152
     153    len = DH_compute_key( secret, bn, dh );
    156154    assert( len <= KEY_LEN );
    157155    offset = KEY_LEN - len;
     
    168166tr_cryptoGetMyPublicKey( const tr_crypto * crypto, int * setme_len )
    169167{
    170     ensureKeyExists( (tr_crypto*) crypto );
    171168    *setme_len = KEY_LEN;
    172169    return crypto->myPublicKey;
  • trunk/libtransmission/peer-msgs.c

    r3393 r3405  
    7070    KEEPALIVE_INTERVAL_SECS = 90,          /* idle seconds before we send a keepalive */
    7171    PEX_INTERVAL            = (60 * 1000), /* msec between calls to sendPex() */
    72     PEER_PULSE_INTERVAL     = (66),        /* msec between calls to pulse() */
     72    PEER_PULSE_INTERVAL     = (20),        /* msec between calls to pulse() */
    7373    RATE_PULSE_INTERVAL     = (333),       /* msec between calls to ratePulse() */
    7474};
Note: See TracChangeset for help on using the changeset viewer.