const uint8_t* tr_cryptoComputeSecret( tr_crypto * crypto, const uint8_t * peerPublicKey ) { int len; uint8_t secret[KEY_LEN]; BIGNUM * bn = BN_bin2bn( peerPublicKey, KEY_LEN, NULL ); DH * dh; ensureKeyExists( crypto ); dh = crypto->dh; assert( DH_size( dh ) == KEY_LEN ); len = DH_compute_key( secret, bn, dh ); if( len == -1 ) logErrorFromSSL( ); else { int offset; assert( len <= KEY_LEN ); offset = KEY_LEN - len; memset( crypto->mySecret, 0, offset ); memcpy( crypto->mySecret + offset, secret, len ); crypto->mySecretIsSet = 1; } BN_free( bn ); return crypto->mySecret; }
const uint8_t* tr_cryptoGetMyPublicKey (const tr_crypto * crypto, int * setme_len) { ensureKeyExists ((tr_crypto *) crypto); *setme_len = KEY_LEN; return crypto->myPublicKey; }
bool tr_cryptoComputeSecret (tr_crypto * crypto, const uint8_t * peerPublicKey) { ensureKeyExists (crypto); crypto->mySecret = tr_dh_agree (crypto->dh, peerPublicKey, KEY_LEN); return crypto->mySecret != NULL; }