2007-09-20 16:32:01 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2007-09-20 16:32:01 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-09-20 16:32:01 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TR_ENCRYPTION_H
|
|
|
|
#define TR_ENCRYPTION_H
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
|
|
|
#error only libtransmission should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2008-10-22 17:19:22 +00:00
|
|
|
#include "utils.h" /* TR_GNUC_NULL_TERMINATED */
|
2008-10-22 17:14:50 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
2010-01-19 19:37:00 +00:00
|
|
|
*** @addtogroup peers
|
|
|
|
*** @{
|
|
|
|
**/
|
2009-05-29 19:17:12 +00:00
|
|
|
|
2011-04-17 05:22:50 +00:00
|
|
|
#include <openssl/dh.h> /* RC4_KEY */
|
|
|
|
#include <openssl/rc4.h> /* DH */
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
KEY_LEN = 96
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Holds state information for encrypted peer communications */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
RC4_KEY dec_key;
|
|
|
|
RC4_KEY enc_key;
|
|
|
|
DH * dh;
|
|
|
|
uint8_t myPublicKey[KEY_LEN];
|
|
|
|
uint8_t mySecret[KEY_LEN];
|
|
|
|
uint8_t torrentHash[SHA_DIGEST_LENGTH];
|
|
|
|
bool isIncoming;
|
|
|
|
bool torrentHashIsSet;
|
|
|
|
bool mySecretIsSet;
|
|
|
|
}
|
|
|
|
tr_crypto;
|
|
|
|
|
|
|
|
/** @brief construct a new tr_crypto object */
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoConstruct (tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming);
|
2011-04-17 05:22:50 +00:00
|
|
|
|
|
|
|
/** @brief destruct an existing tr_crypto object */
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoDestruct (tr_crypto * crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoSetTorrentHash (tr_crypto * crypto, const uint8_t * torrentHash);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
const uint8_t* tr_cryptoGetTorrentHash (const tr_crypto * crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_cryptoHasTorrentHash (const tr_crypto * crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
const uint8_t* tr_cryptoComputeSecret (tr_crypto * crypto,
|
|
|
|
const uint8_t * peerPublicKey);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
const uint8_t* tr_cryptoGetMyPublicKey (const tr_crypto * crypto,
|
|
|
|
int * setme_len);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoDecryptInit (tr_crypto * crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoDecrypt (tr_crypto * crypto,
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t buflen,
|
|
|
|
const void * buf_in,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * buf_out);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoEncryptInit (tr_crypto * crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoEncrypt (tr_crypto * crypto,
|
2008-09-23 19:11:04 +00:00
|
|
|
size_t buflen,
|
|
|
|
const void * buf_in,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * buf_out);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/* @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
*** @addtogroup utils Utilities
|
|
|
|
*** @{
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief generate a SHA1 hash from one or more chunks of memory */
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_sha1 (uint8_t * setme,
|
2010-01-19 19:37:00 +00:00
|
|
|
const void * content1,
|
|
|
|
int content1_len,
|
2012-12-05 17:29:46 +00:00
|
|
|
...) TR_GNUC_NULL_TERMINATED;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2008-08-14 11:11:25 +00:00
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief returns a random number in the range of [0...n) */
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_cryptoRandInt (int n);
|
2008-08-14 11:11:25 +00:00
|
|
|
|
2010-01-06 00:18:33 +00:00
|
|
|
/**
|
2010-01-19 19:37:00 +00:00
|
|
|
* @brief returns a pseudorandom number in the range of [0...n)
|
2010-01-06 00:18:33 +00:00
|
|
|
*
|
2012-12-05 17:29:46 +00:00
|
|
|
* This is faster, BUT WEAKER, than tr_cryptoRandInt () and never
|
2010-01-06 00:18:33 +00:00
|
|
|
* be used in sensitive cases.
|
2012-12-05 17:29:46 +00:00
|
|
|
* @see tr_cryptoRandInt ()
|
2010-01-06 00:18:33 +00:00
|
|
|
*/
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_cryptoWeakRandInt (int n);
|
2008-08-27 18:50:21 +00:00
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief fill a buffer with random bytes */
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_cryptoRandBuf (void * buf, size_t len);
|
2008-08-14 11:11:25 +00:00
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief generate a SSHA password from its plaintext source */
|
2012-12-05 17:29:46 +00:00
|
|
|
char* tr_ssha1 (const void * plaintext);
|
2009-03-17 21:50:20 +00:00
|
|
|
|
2010-01-19 19:37:00 +00:00
|
|
|
/** @brief Validate a test password against the a ssha1 password */
|
2012-12-05 17:29:46 +00:00
|
|
|
bool tr_ssha1_matches (const char * ssha1, const char * pass);
|
2009-03-17 21:50:20 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#endif
|