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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2016-03-29 19:02:26 +00:00
|
|
|
#include "crypto.h"
|
|
|
|
#include "crypto-utils.h"
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
#include "libtransmission-test.h"
|
|
|
|
|
2015-01-01 14:23:47 +00:00
|
|
|
#include "crypto-test-ref.h"
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
test_torrent_hash (void)
|
|
|
|
{
|
|
|
|
tr_crypto a;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
2016-03-29 03:04:54 +00:00
|
|
|
uint8_t i;
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-01-01 14:23:47 +00:00
|
|
|
tr_crypto a;
|
|
|
|
tr_crypto_ b;
|
2014-01-19 04:41:20 +00:00
|
|
|
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)
|
2016-03-29 03:04:54 +00:00
|
|
|
hash[i] = (uint8_t)i;
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
tr_cryptoConstruct (&a, hash, false);
|
2015-01-01 14:23:47 +00:00
|
|
|
tr_cryptoConstruct_ (&b, hash, true);
|
|
|
|
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);
|
2015-01-01 14:23:47 +00:00
|
|
|
tr_cryptoDecryptInit_ (&b);
|
|
|
|
tr_cryptoDecrypt_ (&b, sizeof (test1), buf11, buf12);
|
2014-01-19 04:41:20 +00:00
|
|
|
check_streq (test1, buf12);
|
|
|
|
|
2015-01-01 14:23:47 +00:00
|
|
|
tr_cryptoEncryptInit_ (&b);
|
|
|
|
tr_cryptoEncrypt_ (&b, sizeof (test2), test2, buf21);
|
2014-01-19 04:41:20 +00:00
|
|
|
tr_cryptoDecryptInit (&a);
|
|
|
|
tr_cryptoDecrypt (&a, sizeof (test2), buf21, buf22);
|
|
|
|
check_streq (test2, buf22);
|
|
|
|
|
2015-01-01 14:23:47 +00:00
|
|
|
tr_cryptoDestruct_ (&b);
|
2014-01-19 04:41:20 +00:00
|
|
|
tr_cryptoDestruct (&a);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
test_sha1 (void)
|
|
|
|
{
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
2015-01-01 14:23:47 +00:00
|
|
|
uint8_t hash_[SHA_DIGEST_LENGTH];
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
check (tr_sha1 (hash, "test", 4, NULL));
|
2015-01-01 14:23:47 +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);
|
2015-01-01 14:23:47 +00:00
|
|
|
check (memcmp (hash, hash_, SHA_DIGEST_LENGTH) == 0);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2014-12-04 12:13:59 +00:00
|
|
|
check (tr_sha1 (hash, "1", 1, "22", 2, "333", 3, NULL));
|
2015-01-01 14:23:47 +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);
|
2015-01-01 14:23:47 +00:00
|
|
|
check (memcmp (hash, hash_, SHA_DIGEST_LENGTH) == 0);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-01-01 14:23:47 +00:00
|
|
|
hashes[j] = j % 2 == 0 ? tr_ssha1 (phrase) : tr_ssha1_ (phrase);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
check (hashes[j] != NULL);
|
|
|
|
|
|
|
|
/* phrase matches each of generated hashes */
|
|
|
|
check (tr_ssha1_matches (hashes[j], phrase));
|
2015-01-01 14:23:47 +00:00
|
|
|
check (tr_ssha1_matches_ (hashes[j], phrase));
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-01-01 14:23:47 +00:00
|
|
|
{
|
|
|
|
/* changed phrase doesn't match the hashes */
|
|
|
|
check (!tr_ssha1_matches (hashes[j], phrase));
|
|
|
|
check (!tr_ssha1_matches_ (hashes[j], phrase));
|
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-03 21:35:20 +00:00
|
|
|
static bool
|
|
|
|
base64_eq (const char * a,
|
|
|
|
const char * b)
|
|
|
|
{
|
|
|
|
for (; ; ++a, ++b)
|
|
|
|
{
|
|
|
|
while (*a == '\r' || *a == '\n')
|
|
|
|
++a;
|
|
|
|
while (*b == '\r' || *b == '\n')
|
|
|
|
++b;
|
|
|
|
if (*a == '\0' || *b == '\0' || *a != *b)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *a == *b;
|
|
|
|
}
|
|
|
|
|
2014-12-04 19:58:34 +00:00
|
|
|
static int
|
|
|
|
test_base64 (void)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char * in, * out;
|
2016-03-29 03:04:54 +00:00
|
|
|
size_t i;
|
2014-12-04 19:58:34 +00:00
|
|
|
|
|
|
|
out = tr_base64_encode_str ("YOYO!", &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (strlen (out), len);
|
2015-01-03 21:35:20 +00:00
|
|
|
check (base64_eq ("WU9ZTyE=", out));
|
2014-12-04 19:58:34 +00:00
|
|
|
in = tr_base64_decode_str (out, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (5, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
check_streq ("YOYO!", in);
|
|
|
|
tr_free (in);
|
|
|
|
tr_free (out);
|
|
|
|
|
|
|
|
out = tr_base64_encode ("", 0, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (0, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
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);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (0, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
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);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (0, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
check (out == NULL);
|
|
|
|
out = tr_base64_decode (NULL, 0, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (0, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
check (out == NULL);
|
|
|
|
|
|
|
|
#define MAX_BUF_SIZE 1024
|
|
|
|
|
|
|
|
for (i = 1; i <= MAX_BUF_SIZE; ++i)
|
|
|
|
{
|
2016-03-29 03:04:54 +00:00
|
|
|
size_t j;
|
2014-12-04 19:58:34 +00:00
|
|
|
char buf[MAX_BUF_SIZE + 1];
|
|
|
|
|
|
|
|
for (j = 0; j < i; ++j)
|
2016-03-29 03:04:54 +00:00
|
|
|
buf[j] = (char) tr_rand_int_weak (256);
|
2014-12-04 19:58:34 +00:00
|
|
|
|
|
|
|
out = tr_base64_encode (buf, j, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (strlen (out), len);
|
2014-12-04 19:58:34 +00:00
|
|
|
in = tr_base64_decode (out, len, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (j, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
check (memcmp (in, buf, len) == 0);
|
|
|
|
tr_free (in);
|
|
|
|
tr_free (out);
|
|
|
|
|
|
|
|
for (j = 0; j < i; ++j)
|
2016-03-29 03:04:54 +00:00
|
|
|
buf[j] = (char)(1 + tr_rand_int_weak (255));
|
2014-12-04 19:58:34 +00:00
|
|
|
buf[j] = '\0';
|
|
|
|
|
|
|
|
out = tr_base64_encode_str (buf, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (strlen (out), len);
|
2014-12-04 19:58:34 +00:00
|
|
|
in = tr_base64_decode_str (out, &len);
|
2016-03-29 03:04:54 +00:00
|
|
|
check_uint_eq (j, len);
|
2014-12-04 19:58:34 +00:00
|
|
|
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));
|
|
|
|
}
|