2011-01-19 13:48:47 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2007-09-20 16:32:01 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2007-09-20 16:32:01 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <inttypes.h> /* uint8_t */
|
2011-03-24 21:49:42 +00:00
|
|
|
#include <limits.h> /* INT_MAX */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <stdarg.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdlib.h> /* abs () */
|
|
|
|
#include <string.h> /* memcpy (), memset (), strcmp () */
|
2007-10-25 13:59:46 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/dh.h>
|
2009-02-10 15:54:47 +00:00
|
|
|
#include <openssl/err.h>
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <openssl/rc4.h>
|
|
|
|
#include <openssl/sha.h>
|
2008-08-14 11:11:25 +00:00
|
|
|
#include <openssl/rand.h>
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2010-04-23 16:36:16 +00:00
|
|
|
#include "transmission.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "crypto.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2009-02-10 15:54:47 +00:00
|
|
|
#define MY_NAME "tr_crypto"
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_sha1 (uint8_t * setme, const void * content1, int content1_len, ...)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
va_list vl;
|
|
|
|
SHA_CTX sha;
|
|
|
|
const void * content;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
SHA1_Init (&sha);
|
|
|
|
SHA1_Update (&sha, content1, content1_len);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
va_start (vl, content1_len);
|
|
|
|
while ((content = va_arg (vl, const void*)))
|
|
|
|
SHA1_Update (&sha, content, va_arg (vl, int));
|
|
|
|
va_end (vl);
|
2011-01-19 21:50:51 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
SHA1_Final (setme, &sha);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define KEY_LEN 96
|
|
|
|
|
|
|
|
#define PRIME_LEN 96
|
|
|
|
|
2009-12-12 03:51:36 +00:00
|
|
|
#define DH_PRIVKEY_LEN_MIN 16
|
|
|
|
#define DH_PRIVKEY_LEN 20
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static const uint8_t dh_P[PRIME_LEN] =
|
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
|
|
|
|
0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
|
|
|
|
0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
|
|
|
|
0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
|
|
|
|
0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
|
|
|
|
0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
|
|
|
|
0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
|
|
|
|
0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63,
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t dh_G[] = { 2 };
|
|
|
|
|
2007-10-13 23:15:43 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define logErrorFromSSL(...) \
|
2013-01-24 23:59:52 +00:00
|
|
|
do { \
|
2013-01-25 23:34:20 +00:00
|
|
|
if (tr_logLevelIsActive (TR_LOG_ERROR)) { \
|
2013-01-24 23:59:52 +00:00
|
|
|
char buf[512]; \
|
|
|
|
ERR_error_string_n (ERR_get_error (), buf, sizeof (buf)); \
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddMessage (__FILE__, __LINE__, TR_LOG_ERROR, MY_NAME, "%s", buf); \
|
2013-01-24 23:59:52 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
2009-02-10 15:54:47 +00:00
|
|
|
|
2009-12-12 03:51:36 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
ensureKeyExists (tr_crypto * crypto)
|
2007-10-11 02:22:17 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
if (crypto->dh == NULL)
|
2007-10-11 02:22:17 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
int len, offset;
|
|
|
|
DH * dh = DH_new ();
|
2009-02-10 15:54:47 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
dh->p = BN_bin2bn (dh_P, sizeof (dh_P), NULL);
|
|
|
|
if (dh->p == NULL)
|
|
|
|
logErrorFromSSL ();
|
2009-02-10 15:54:47 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
dh->g = BN_bin2bn (dh_G, sizeof (dh_G), NULL);
|
|
|
|
if (dh->g == NULL)
|
|
|
|
logErrorFromSSL ();
|
2009-12-12 03:51:36 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* private DH value: strong random BN of DH_PRIVKEY_LEN*8 bits */
|
|
|
|
dh->priv_key = BN_new ();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (BN_rand (dh->priv_key, DH_PRIVKEY_LEN * 8, -1, 0) != 1)
|
2012-12-05 17:29:46 +00:00
|
|
|
logErrorFromSSL ();
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
|
|
|
while (BN_num_bits (dh->priv_key) < DH_PRIVKEY_LEN_MIN * 8);
|
|
|
|
|
|
|
|
if (!DH_generate_key (dh))
|
|
|
|
logErrorFromSSL ();
|
2007-10-11 02:22:17 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* DH can generate key sizes that are smaller than the size of
|
|
|
|
P with exponentially decreasing probability, in which case
|
|
|
|
the msb's of myPublicKey need to be zeroed appropriately. */
|
|
|
|
len = BN_num_bytes (dh->pub_key);
|
|
|
|
offset = KEY_LEN - len;
|
|
|
|
assert (len <= KEY_LEN);
|
|
|
|
memset (crypto->myPublicKey, 0, offset);
|
|
|
|
BN_bn2bin (dh->pub_key, crypto->myPublicKey + offset);
|
2009-12-12 03:51:36 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
crypto->dh = dh;
|
2009-12-12 03:51:36 +00:00
|
|
|
}
|
2007-10-13 23:15:43 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2011-04-17 05:22:50 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoConstruct (tr_crypto * crypto, const uint8_t * torrentHash, bool isIncoming)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
memset (crypto, 0, sizeof (tr_crypto));
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
crypto->dh = NULL;
|
|
|
|
crypto->isIncoming = isIncoming;
|
|
|
|
tr_cryptoSetTorrentHash (crypto, torrentHash);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoDestruct (tr_crypto * crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
if (crypto->dh != NULL)
|
|
|
|
DH_free (crypto->dh);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
const uint8_t*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoComputeSecret (tr_crypto * crypto,
|
|
|
|
const uint8_t * peerPublicKey)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
DH * dh;
|
|
|
|
int len;
|
|
|
|
uint8_t secret[KEY_LEN];
|
|
|
|
BIGNUM * bn = BN_bin2bn (peerPublicKey, KEY_LEN, NULL);
|
2009-12-12 03:51:36 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
ensureKeyExists (crypto);
|
|
|
|
dh = crypto->dh;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
assert (DH_size (dh) == KEY_LEN);
|
2007-10-11 02:22:17 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
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;
|
2009-02-10 15:54:47 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
BN_free (bn);
|
|
|
|
return crypto->mySecret;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoGetMyPublicKey (const tr_crypto * crypto,
|
2013-01-24 23:59:52 +00:00
|
|
|
int * setme_len)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
ensureKeyExists ((tr_crypto *) crypto);
|
|
|
|
*setme_len = KEY_LEN;
|
|
|
|
return crypto->myPublicKey;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
2013-01-24 23:59:52 +00:00
|
|
|
initRC4 (tr_crypto * crypto,
|
|
|
|
RC4_KEY * setme,
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * key)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
SHA_CTX sha;
|
|
|
|
uint8_t buf[SHA_DIGEST_LENGTH];
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
assert (crypto->torrentHashIsSet);
|
|
|
|
assert (crypto->mySecretIsSet);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (SHA1_Init (&sha)
|
|
|
|
&& SHA1_Update (&sha, key, 4)
|
|
|
|
&& SHA1_Update (&sha, crypto->mySecret, KEY_LEN)
|
|
|
|
&& SHA1_Update (&sha, crypto->torrentHash, SHA_DIGEST_LENGTH)
|
|
|
|
&& SHA1_Final (buf, &sha))
|
2009-02-10 15:54:47 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
RC4_set_key (setme, SHA_DIGEST_LENGTH, buf);
|
2009-02-10 15:54:47 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
else
|
2009-02-10 15:54:47 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
logErrorFromSSL ();
|
2009-02-10 15:54:47 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoDecryptInit (tr_crypto * crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
unsigned char discard[1024];
|
|
|
|
const char * txt = crypto->isIncoming ? "keyA" : "keyB";
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
initRC4 (crypto, &crypto->dec_key, txt);
|
|
|
|
RC4 (&crypto->dec_key, sizeof (discard), discard, discard);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-24 23:59:52 +00:00
|
|
|
tr_cryptoDecrypt (tr_crypto * crypto,
|
2007-09-20 16:32:01 +00:00
|
|
|
size_t buf_len,
|
|
|
|
const void * buf_in,
|
2013-01-24 23:59:52 +00:00
|
|
|
void * buf_out)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
RC4 (&crypto->dec_key, buf_len,
|
2012-12-05 17:29:46 +00:00
|
|
|
(const unsigned char*)buf_in,
|
|
|
|
(unsigned char*)buf_out);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoEncryptInit (tr_crypto * crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
unsigned char discard[1024];
|
|
|
|
const char * txt = crypto->isIncoming ? "keyB" : "keyA";
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
initRC4 (crypto, &crypto->enc_key, txt);
|
|
|
|
RC4 (&crypto->enc_key, sizeof (discard), discard, discard);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-24 23:59:52 +00:00
|
|
|
tr_cryptoEncrypt (tr_crypto * crypto,
|
2007-09-20 16:32:01 +00:00
|
|
|
size_t buf_len,
|
|
|
|
const void * buf_in,
|
2013-01-24 23:59:52 +00:00
|
|
|
void * buf_out)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
RC4 (&crypto->enc_key, buf_len,
|
2012-12-05 17:29:46 +00:00
|
|
|
(const unsigned char*)buf_in,
|
|
|
|
(unsigned char*)buf_out);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
2013-01-24 23:59:52 +00:00
|
|
|
tr_cryptoSetTorrentHash (tr_crypto * crypto,
|
2012-12-05 17:29:46 +00:00
|
|
|
const uint8_t * hash)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
crypto->torrentHashIsSet = hash ? 1 : 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (hash)
|
|
|
|
memcpy (crypto->torrentHash, hash, SHA_DIGEST_LENGTH);
|
|
|
|
else
|
|
|
|
memset (crypto->torrentHash, 0, SHA_DIGEST_LENGTH);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoGetTorrentHash (const tr_crypto * crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
assert (crypto);
|
|
|
|
assert (crypto->torrentHashIsSet);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
return crypto->torrentHash;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoHasTorrentHash (const tr_crypto * crypto)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
assert (crypto);
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
return crypto->torrentHashIsSet ? 1 : 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-08-14 11:11:25 +00:00
|
|
|
|
2008-08-27 18:50:21 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoRandInt (int upperBound)
|
2008-08-14 11:11:25 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
int noise;
|
|
|
|
int val;
|
2008-08-14 11:11:25 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
assert (upperBound > 0);
|
2009-05-19 20:52:24 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (RAND_pseudo_bytes ((unsigned char *) &noise, sizeof noise) >= 0)
|
2009-02-10 21:43:08 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
val = abs (noise) % upperBound;
|
2009-02-10 21:43:08 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
else /* fall back to a weaker implementation... */
|
2009-02-10 21:43:08 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
val = tr_cryptoWeakRandInt (upperBound);
|
2009-02-10 21:43:08 +00:00
|
|
|
}
|
2008-08-14 11:11:25 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
return val;
|
2008-08-14 11:11:25 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 18:50:21 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoWeakRandInt (int upperBound)
|
2008-08-27 18:50:21 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
static bool init = false;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
assert (upperBound > 0);
|
2008-08-27 18:50:21 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (!init)
|
2008-08-27 18:50:21 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
srand (tr_time_msec ());
|
|
|
|
init = true;
|
2008-08-27 18:50:21 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
return rand () % upperBound;
|
2008-08-27 18:50:21 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_cryptoRandBuf (void * buf, size_t len)
|
2008-08-14 11:11:25 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
if (RAND_pseudo_bytes ((unsigned char*)buf, len) != 1)
|
|
|
|
logErrorFromSSL ();
|
2008-08-14 11:11:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-17 21:50:20 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
char*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ssha1 (const void * plaintext)
|
2009-03-17 21:50:20 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
enum { saltval_len = 8,
|
|
|
|
salter_len = 64 };
|
|
|
|
static const char * salter = "0123456789"
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
"./";
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
unsigned char salt[saltval_len];
|
|
|
|
uint8_t sha[SHA_DIGEST_LENGTH];
|
|
|
|
char buf[2*SHA_DIGEST_LENGTH + saltval_len + 2];
|
|
|
|
|
|
|
|
tr_cryptoRandBuf (salt, saltval_len);
|
|
|
|
for (i=0; i<saltval_len; ++i)
|
|
|
|
salt[i] = salter[ salt[i] % salter_len ];
|
|
|
|
|
|
|
|
tr_sha1 (sha, plaintext, strlen (plaintext), salt, saltval_len, NULL);
|
|
|
|
tr_sha1_to_hex (&buf[1], sha);
|
|
|
|
memcpy (&buf[1+2*SHA_DIGEST_LENGTH], &salt, saltval_len);
|
|
|
|
buf[1+2*SHA_DIGEST_LENGTH + saltval_len] = '\0';
|
|
|
|
buf[0] = '{'; /* signal that this is a hash. this makes saving/restoring easier */
|
|
|
|
|
|
|
|
return tr_strdup (&buf);
|
2009-03-24 01:39:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ssha1_matches (const char * source, const char * pass)
|
2009-03-24 01:39:06 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
char * salt;
|
|
|
|
size_t saltlen;
|
|
|
|
char * hashed;
|
|
|
|
uint8_t buf[SHA_DIGEST_LENGTH];
|
|
|
|
bool result;
|
|
|
|
const size_t sourcelen = strlen (source);
|
|
|
|
|
|
|
|
/* extract the salt */
|
|
|
|
if (sourcelen < 2*SHA_DIGEST_LENGTH-1)
|
|
|
|
return false;
|
|
|
|
saltlen = sourcelen - 2*SHA_DIGEST_LENGTH-1;
|
|
|
|
salt = tr_malloc (saltlen);
|
|
|
|
memcpy (salt, source + 2*SHA_DIGEST_LENGTH+1, saltlen);
|
|
|
|
|
|
|
|
/* hash pass + salt */
|
|
|
|
hashed = tr_malloc (2*SHA_DIGEST_LENGTH + saltlen + 2);
|
|
|
|
tr_sha1 (buf, pass, strlen (pass), salt, saltlen, NULL);
|
|
|
|
tr_sha1_to_hex (&hashed[1], buf);
|
|
|
|
memcpy (hashed + 1+2*SHA_DIGEST_LENGTH, salt, saltlen);
|
|
|
|
hashed[1+2*SHA_DIGEST_LENGTH + saltlen] = '\0';
|
|
|
|
hashed[0] = '{';
|
|
|
|
|
|
|
|
result = strcmp (source, hashed) == 0 ? true : false;
|
|
|
|
|
|
|
|
tr_free (hashed);
|
|
|
|
tr_free (salt);
|
|
|
|
|
|
|
|
return result;
|
2009-03-17 21:50:20 +00:00
|
|
|
}
|