1/* 2 * This file Copyright (C) Mnemosyne LLC 3 * 4 * This file is licensed by the GPL version 2. Works owned by the 5 * Transmission project are granted a special exemption to clause 2(b) 6 * so that the bulk of its code can remain under the MIT license. 7 * This exemption does not extend to derived works not owned by 8 * the Transmission project. 9 * 10 * $Id: crypto.h 12365 2011-04-17 05:22:50Z jordan $ 11 */ 12 13#ifndef TR_ENCRYPTION_H 14#define TR_ENCRYPTION_H 15 16#ifndef __TRANSMISSION__ 17#error only libtransmission should #include this header. 18#endif 19 20#include <inttypes.h> 21 22#include "utils.h" /* TR_GNUC_NULL_TERMINATED */ 23 24/** 25*** @addtogroup peers 26*** @{ 27**/ 28 29#include <openssl/dh.h> /* RC4_KEY */ 30#include <openssl/rc4.h> /* DH */ 31 32enum 33{ 34 KEY_LEN = 96 35}; 36 37/** @brief Holds state information for encrypted peer communications */ 38typedef struct 39{ 40 RC4_KEY dec_key; 41 RC4_KEY enc_key; 42 DH * dh; 43 uint8_t myPublicKey[KEY_LEN]; 44 uint8_t mySecret[KEY_LEN]; 45 uint8_t torrentHash[SHA_DIGEST_LENGTH]; 46 bool isIncoming; 47 bool torrentHashIsSet; 48 bool mySecretIsSet; 49} 50tr_crypto; 51 52/** @brief construct a new tr_crypto object */ 53void tr_cryptoConstruct( tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming ); 54 55/** @brief destruct an existing tr_crypto object */ 56void tr_cryptoDestruct( tr_crypto * crypto ); 57 58 59void tr_cryptoSetTorrentHash( tr_crypto * crypto, const uint8_t * torrentHash ); 60 61const uint8_t* tr_cryptoGetTorrentHash( const tr_crypto * crypto ); 62 63int tr_cryptoHasTorrentHash( const tr_crypto * crypto ); 64 65const uint8_t* tr_cryptoComputeSecret( tr_crypto * crypto, 66 const uint8_t * peerPublicKey ); 67 68const uint8_t* tr_cryptoGetMyPublicKey( const tr_crypto * crypto, 69 int * setme_len ); 70 71void tr_cryptoDecryptInit( tr_crypto * crypto ); 72 73void tr_cryptoDecrypt( tr_crypto * crypto, 74 size_t buflen, 75 const void * buf_in, 76 void * buf_out ); 77 78void tr_cryptoEncryptInit( tr_crypto * crypto ); 79 80void tr_cryptoEncrypt( tr_crypto * crypto, 81 size_t buflen, 82 const void * buf_in, 83 void * buf_out ); 84 85/* @} */ 86 87/** 88*** @addtogroup utils Utilities 89*** @{ 90**/ 91 92 93/** @brief generate a SHA1 hash from one or more chunks of memory */ 94void tr_sha1( uint8_t * setme, 95 const void * content1, 96 int content1_len, 97 ... ) TR_GNUC_NULL_TERMINATED; 98 99 100/** @brief returns a random number in the range of [0...n) */ 101int tr_cryptoRandInt( int n ); 102 103/** 104 * @brief returns a pseudorandom number in the range of [0...n) 105 * 106 * This is faster, BUT WEAKER, than tr_cryptoRandInt() and never 107 * be used in sensitive cases. 108 * @see tr_cryptoRandInt() 109 */ 110int tr_cryptoWeakRandInt( int n ); 111 112/** @brief fill a buffer with random bytes */ 113void tr_cryptoRandBuf( void * buf, size_t len ); 114 115/** @brief generate a SSHA password from its plaintext source */ 116char* tr_ssha1( const void * plaintext ); 117 118/** @brief Validate a test password against the a ssha1 password */ 119bool tr_ssha1_matches( const char * ssha1, const char * pass ); 120 121/* @} */ 122 123#endif 124