2014-01-19 04:41:20 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2013-2014 Mnemosyne LLC
|
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 04:41:20 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "crypto.h"
|
2014-12-04 11:27:38 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
|
|
|
#define SHA_DIGEST_LENGTH 20
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_torrent_hash (void)
|
|
|
|
{
|
|
|
|
tr_crypto a;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
|
|
|
|
hash[i] = i;
|
|
|
|
|
|
|
|
tr_cryptoConstruct (&a, NULL, true);
|
|
|
|
|
|
|
|
check (!tr_cryptoHasTorrentHash (&a));
|
|
|
|
check (tr_cryptoGetTorrentHash (&a) == NULL);
|
|
|
|
|
|
|
|
tr_cryptoSetTorrentHash (&a, hash);
|
|
|
|
check (tr_cryptoHasTorrentHash (&a));
|
|
|
|
check (tr_cryptoGetTorrentHash (&a) != NULL);
|
|
|
|
check (memcmp (tr_cryptoGetTorrentHash (&a), hash, SHA_DIGEST_LENGTH) == 0);
|
|
|
|
|
|
|
|
tr_cryptoDestruct (&a);
|
|
|
|
|
|
|
|
for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
|
|
|
|
hash[i] = i + 1;
|
|
|
|
|
|
|
|
tr_cryptoConstruct (&a, hash, false);
|
|
|
|
|
|
|
|
check (tr_cryptoHasTorrentHash (&a));
|
|
|
|
check (tr_cryptoGetTorrentHash (&a) != NULL);
|
|
|
|
check (memcmp (tr_cryptoGetTorrentHash (&a), hash, SHA_DIGEST_LENGTH) == 0);
|
|
|
|
|
|
|
|
tr_cryptoSetTorrentHash (&a, NULL);
|
|
|
|
check (!tr_cryptoHasTorrentHash (&a));
|
|
|
|
check (tr_cryptoGetTorrentHash (&a) == NULL);
|
|
|
|
|
|
|
|
tr_cryptoDestruct (&a);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_encrypt_decrypt (void)
|
|
|
|
{
|
|
|
|
tr_crypto a, b;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
const char test1[] = { "test1" };
|
|
|
|
char buf11[sizeof (test1)], buf12[sizeof (test1)];
|
|
|
|
const char test2[] = { "@#)C$@)#(*%bvkdjfhwbc039bc4603756VB3)" };
|
|
|
|
char buf21[sizeof (test2)], buf22[sizeof (test2)];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < SHA_DIGEST_LENGTH; ++i)
|
|
|
|
hash[i] = i;
|
|
|
|
|
|
|
|
tr_cryptoConstruct (&a, hash, false);
|
|
|
|
tr_cryptoConstruct (&b, hash, true);
|
#4400, #5462: Move DH helpers to crypto-utils
On a way to factoring out OpenSSL support to a standalone file to ease
addition of other crypto libraries support in the future, move helpers
providing DH key exchange to crypto-utils.{c,h}. OpenSSL-related
functionality (DH context management) is moved to crypto-utils-openssl.c.
Since we know in advance that DH secret key management code will be the
same for most of backends, implement common functionality in separate
crypto-utils-fallback.c.
Add new tr_dh_ctx_t and tr_dh_secret_t types and functions to be
implemented by crypto backends:
* tr_dh_new - allocate DH context,
* tr_dh_free - free the context,
* tr_dh_make_key - generate private/public keypair,
* tr_dh_agree - perform DH key exchange and generate secret key,
* tr_dh_secret_derive - calculate secret key hash,
* tr_dh_secret_free - free the secret key,
* tr_dh_align_key - align some DH key in the buffer allocated for it.
Make DH secret key not accessible in plain form outside the crypto
backend. This allows for implementations where the key is managed by
the underlying library and is not even exposed to our backend.
2014-12-04 19:18:08 +00:00
|
|
|
check (tr_cryptoComputeSecret (&a, tr_cryptoGetMyPublicKey (&b, &i)));
|
|
|
|
check (tr_cryptoComputeSecret (&b, tr_cryptoGetMyPublicKey (&a, &i)));
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
tr_cryptoEncryptInit (&a);
|
|
|
|
tr_cryptoEncrypt (&a, sizeof (test1), test1, buf11);
|
|
|
|
tr_cryptoDecryptInit (&b);
|
|
|
|
tr_cryptoDecrypt (&b, sizeof (test1), buf11, buf12);
|
|
|
|
check_streq (test1, buf12);
|
|
|
|
|
|
|
|
tr_cryptoEncryptInit (&b);
|
|
|
|
tr_cryptoEncrypt (&b, sizeof (test2), test2, buf21);
|
|
|
|
tr_cryptoDecryptInit (&a);
|
|
|
|
tr_cryptoDecrypt (&a, sizeof (test2), buf21, buf22);
|
|
|
|
check_streq (test2, buf22);
|
|
|
|
|
|
|
|
tr_cryptoDestruct (&b);
|
|
|
|
tr_cryptoDestruct (&a);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_sha1 (void)
|
|
|
|
{
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
check (tr_sha1 (hash, "test", 4, NULL));
|
2014-01-19 04:41:20 +00:00
|
|
|
check (memcmp (hash, "\xa9\x4a\x8f\xe5\xcc\xb1\x9b\xa6\x1c\x4c\x08\x73\xd3\x91\xe9\x87\x98\x2f\xbb\xd3", SHA_DIGEST_LENGTH) == 0);
|
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
check (tr_sha1 (hash, "1", 1, "22", 2, "333", 3, NULL));
|
2014-01-19 04:41:20 +00:00
|
|
|
check (memcmp (hash, "\x1f\x74\x64\x8e\x50\xa6\xa6\x70\x8e\xc5\x4a\xb3\x27\xa1\x63\xd5\x53\x6b\x7c\xed", SHA_DIGEST_LENGTH) == 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_ssha1 (void)
|
|
|
|
{
|
|
|
|
const char * const test_data[] =
|
|
|
|
{
|
|
|
|
"test",
|
|
|
|
"QNY)(*#$B)!_X$B !_B#($^!)*&$%CV!#)&$C!@$(P*)"
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
#define HASH_COUNT (16 * 1024)
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (test_data) / sizeof (*test_data); ++i)
|
|
|
|
{
|
|
|
|
char * const phrase = tr_strdup (test_data[i]);
|
|
|
|
char ** hashes = tr_new (char *, HASH_COUNT);
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
for (j = 0; j < HASH_COUNT; ++j)
|
|
|
|
{
|
|
|
|
hashes[j] = tr_ssha1 (phrase);
|
|
|
|
|
|
|
|
check (hashes[j] != NULL);
|
|
|
|
|
|
|
|
/* phrase matches each of generated hashes */
|
|
|
|
check (tr_ssha1_matches (hashes[j], phrase));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < HASH_COUNT; ++j)
|
|
|
|
{
|
|
|
|
size_t k;
|
|
|
|
|
|
|
|
/* all hashes are different */
|
|
|
|
for (k = 0; k < HASH_COUNT; ++k)
|
|
|
|
check (k == j || strcmp (hashes[j], hashes[k]) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exchange two first chars */
|
|
|
|
phrase[0] ^= phrase[1];
|
|
|
|
phrase[1] ^= phrase[0];
|
|
|
|
phrase[0] ^= phrase[1];
|
|
|
|
|
|
|
|
for (j = 0; j < HASH_COUNT; ++j)
|
|
|
|
/* changed phrase doesn't match the hashes */
|
|
|
|
check (!tr_ssha1_matches (hashes[j], phrase));
|
|
|
|
|
|
|
|
for (j = 0; j < HASH_COUNT; ++j)
|
|
|
|
tr_free (hashes[j]);
|
|
|
|
|
|
|
|
tr_free (hashes);
|
|
|
|
tr_free (phrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef HASH_COUNT
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-04 11:27:38 +00:00
|
|
|
static int
|
|
|
|
test_random (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* test that tr_rand_int () stays in-bounds */
|
|
|
|
for (i = 0; i < 100000; ++i)
|
|
|
|
{
|
|
|
|
const int val = tr_rand_int (100);
|
|
|
|
check (val >= 0);
|
|
|
|
check (val < 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-04 19:58:34 +00:00
|
|
|
static int
|
|
|
|
test_base64 (void)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char * in, * out;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
out = tr_base64_encode_str ("YOYO!", &len);
|
|
|
|
check_int_eq (8, len);
|
|
|
|
check_streq ("WU9ZTyE=", out);
|
|
|
|
in = tr_base64_decode_str (out, &len);
|
|
|
|
check_int_eq (5, len);
|
|
|
|
check_streq ("YOYO!", in);
|
|
|
|
tr_free (in);
|
|
|
|
tr_free (out);
|
|
|
|
|
|
|
|
out = tr_base64_encode ("", 0, &len);
|
|
|
|
check_int_eq (0, len);
|
|
|
|
check_streq ("", out);
|
2014-12-07 10:42:12 +00:00
|
|
|
tr_free (out);
|
2014-12-04 19:58:34 +00:00
|
|
|
out = tr_base64_decode ("", 0, &len);
|
|
|
|
check_int_eq (0, len);
|
|
|
|
check_streq ("", out);
|
2014-12-07 10:42:12 +00:00
|
|
|
tr_free (out);
|
2014-12-04 19:58:34 +00:00
|
|
|
|
|
|
|
out = tr_base64_encode (NULL, 0, &len);
|
|
|
|
check_int_eq (0, len);
|
|
|
|
check (out == NULL);
|
2014-12-07 10:42:12 +00:00
|
|
|
tr_free (out);
|
2014-12-04 19:58:34 +00:00
|
|
|
out = tr_base64_decode (NULL, 0, &len);
|
|
|
|
check_int_eq (0, len);
|
|
|
|
check (out == NULL);
|
2014-12-07 10:42:12 +00:00
|
|
|
tr_free (out);
|
2014-12-04 19:58:34 +00:00
|
|
|
|
|
|
|
#define MAX_BUF_SIZE 1024
|
|
|
|
|
|
|
|
for (i = 1; i <= MAX_BUF_SIZE; ++i)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
char buf[MAX_BUF_SIZE + 1];
|
|
|
|
|
|
|
|
for (j = 0; j < i; ++j)
|
|
|
|
buf[j] = tr_rand_int_weak (256);
|
|
|
|
|
|
|
|
out = tr_base64_encode (buf, j, &len);
|
|
|
|
check_int_eq ((j + 2) / 3 * 4, len);
|
|
|
|
in = tr_base64_decode (out, len, &len);
|
|
|
|
check_int_eq (j, len);
|
|
|
|
check (memcmp (in, buf, len) == 0);
|
|
|
|
tr_free (in);
|
|
|
|
tr_free (out);
|
|
|
|
|
|
|
|
for (j = 0; j < i; ++j)
|
|
|
|
buf[j] = 1 + tr_rand_int_weak (255);
|
|
|
|
buf[j] = '\0';
|
|
|
|
|
|
|
|
out = tr_base64_encode_str (buf, &len);
|
|
|
|
check_int_eq ((j + 2) / 3 * 4, len);
|
|
|
|
in = tr_base64_decode_str (out, &len);
|
|
|
|
check_int_eq (j, len);
|
|
|
|
check_streq (in, buf);
|
|
|
|
tr_free (in);
|
|
|
|
tr_free (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAX_BUF_SIZE
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-19 04:41:20 +00:00
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
|
|
|
const testFunc tests[] = { test_torrent_hash,
|
|
|
|
test_encrypt_decrypt,
|
|
|
|
test_sha1,
|
2014-12-04 11:27:38 +00:00
|
|
|
test_ssha1,
|
2014-12-04 19:58:34 +00:00
|
|
|
test_random,
|
|
|
|
test_base64 };
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
return runTests (tests, NUM_TESTS (tests));
|
|
|
|
}
|