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"
|
2017-05-13 22:38:31 +00:00
|
|
|
#include "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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_torrent_hash(void)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_crypto a;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (uint8_t i = 0; i < SHA_DIGEST_LENGTH; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
hash[i] = i;
|
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoConstruct(&a, NULL, true);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
check(!tr_cryptoHasTorrentHash(&a));
|
|
|
|
check(tr_cryptoGetTorrentHash(&a) == NULL);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoSetTorrentHash(&a, hash);
|
|
|
|
check(tr_cryptoHasTorrentHash(&a));
|
|
|
|
check(tr_cryptoGetTorrentHash(&a) != NULL);
|
|
|
|
check(memcmp(tr_cryptoGetTorrentHash(&a), hash, SHA_DIGEST_LENGTH) == 0);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoDestruct(&a);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (uint8_t i = 0; i < SHA_DIGEST_LENGTH; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
hash[i] = i + 1;
|
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoConstruct(&a, hash, false);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_cryptoHasTorrentHash(&a));
|
|
|
|
check(tr_cryptoGetTorrentHash(&a) != NULL);
|
|
|
|
check(memcmp(tr_cryptoGetTorrentHash(&a), hash, SHA_DIGEST_LENGTH) == 0);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoSetTorrentHash(&a, NULL);
|
|
|
|
check(!tr_cryptoHasTorrentHash(&a));
|
|
|
|
check(tr_cryptoGetTorrentHash(&a) == NULL);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoDestruct(&a);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_encrypt_decrypt(void)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_crypto a;
|
|
|
|
tr_crypto_ b;
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
2017-04-20 16:02:19 +00:00
|
|
|
char const test1[] = { "test1" };
|
2017-05-01 15:46:41 +00:00
|
|
|
char buf11[sizeof(test1)];
|
|
|
|
char buf12[sizeof(test1)];
|
2017-04-20 16:02:19 +00:00
|
|
|
char const test2[] = { "@#)C$@)#(*%bvkdjfhwbc039bc4603756VB3)" };
|
2017-05-01 15:46:41 +00:00
|
|
|
char buf21[sizeof(test2)];
|
|
|
|
char buf22[sizeof(test2)];
|
2017-05-13 22:38:31 +00:00
|
|
|
int public_key_length;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < SHA_DIGEST_LENGTH; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
hash[i] = (uint8_t)i;
|
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoConstruct(&a, hash, false);
|
|
|
|
tr_cryptoConstruct_(&b, hash, true);
|
2017-05-13 22:38:31 +00:00
|
|
|
check(tr_cryptoComputeSecret(&a, tr_cryptoGetMyPublicKey_(&b, &public_key_length)));
|
|
|
|
check(tr_cryptoComputeSecret_(&b, tr_cryptoGetMyPublicKey(&a, &public_key_length)));
|
2017-04-19 12:04:45 +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);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoEncryptInit_(&b);
|
|
|
|
tr_cryptoEncrypt_(&b, sizeof(test2), test2, buf21);
|
|
|
|
tr_cryptoDecryptInit(&a);
|
|
|
|
tr_cryptoDecrypt(&a, sizeof(test2), buf21, buf22);
|
|
|
|
check_streq(test2, buf22);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_cryptoDestruct_(&b);
|
|
|
|
tr_cryptoDestruct(&a);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_sha1(void)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
uint8_t hash_[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
check(tr_sha1(hash, "test", 4, NULL));
|
|
|
|
check(tr_sha1_(hash_, "test", 4, NULL));
|
|
|
|
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);
|
|
|
|
check(memcmp(hash, hash_, SHA_DIGEST_LENGTH) == 0);
|
|
|
|
|
|
|
|
check(tr_sha1(hash, "1", 1, "22", 2, "333", 3, NULL));
|
|
|
|
check(tr_sha1_(hash_, "1", 1, "22", 2, "333", 3, NULL));
|
|
|
|
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);
|
|
|
|
check(memcmp(hash, hash_, SHA_DIGEST_LENGTH) == 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_ssha1(void)
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const plain_text;
|
|
|
|
char const* const ssha1;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
test_data[] =
|
|
|
|
{
|
|
|
|
{ "test", "{15ad0621b259a84d24dcd4e75b09004e98a3627bAMbyRHJy" },
|
|
|
|
{ "QNY)(*#$B)!_X$B !_B#($^!)*&$%CV!#)&$C!@$(P*)", "{10e2d7acbb104d970514a147cd16d51dfa40fb3c0OSwJtOL" }
|
|
|
|
};
|
|
|
|
|
2017-01-09 11:12:17 +00:00
|
|
|
#define HASH_COUNT (4 * 1024)
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 0; i < TR_N_ELEMENTS(test_data); ++i)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* const phrase = tr_strdup(test_data[i].plain_text);
|
|
|
|
char** hashes = tr_new(char*, HASH_COUNT);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
check(tr_ssha1_matches(test_data[i].ssha1, phrase));
|
|
|
|
check(tr_ssha1_matches_(test_data[i].ssha1, phrase));
|
2017-01-09 11:12:17 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t j = 0; j < HASH_COUNT; ++j)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
hashes[j] = j % 2 == 0 ? tr_ssha1(phrase) : tr_ssha1_(phrase);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
check(hashes[j] != NULL);
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* phrase matches each of generated hashes */
|
|
|
|
check(tr_ssha1_matches(hashes[j], phrase));
|
|
|
|
check(tr_ssha1_matches_(hashes[j], phrase));
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t j = 0; j < HASH_COUNT; ++j)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* all hashes are different */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t k = 0; k < HASH_COUNT; ++k)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
check(k == j || strcmp(hashes[j], hashes[k]) != 0);
|
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* exchange two first chars */
|
|
|
|
phrase[0] ^= phrase[1];
|
|
|
|
phrase[1] ^= phrase[0];
|
|
|
|
phrase[0] ^= phrase[1];
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t j = 0; j < HASH_COUNT; ++j)
|
2015-01-01 14:23:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* changed phrase doesn't match the hashes */
|
|
|
|
check(!tr_ssha1_matches(hashes[j], phrase));
|
|
|
|
check(!tr_ssha1_matches_(hashes[j], phrase));
|
2015-01-01 14:23:47 +00:00
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t j = 0; j < HASH_COUNT; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(hashes[j]);
|
|
|
|
}
|
2014-01-19 04:41:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(hashes);
|
|
|
|
tr_free(phrase);
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef HASH_COUNT
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* should work with different salt lengths as well */
|
|
|
|
check(tr_ssha1_matches("{a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", "test"));
|
|
|
|
check(tr_ssha1_matches("{d209a21d3bc4f8fc4f8faf347e69f3def597eb170pySy4ai1ZPMjeU1", "test"));
|
2017-01-09 11:12:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_random(void)
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
/* test that tr_rand_int() stays in-bounds */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < 100000; ++i)
|
2014-12-04 11:27:38 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const val = tr_rand_int(100);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(val >= 0);
|
|
|
|
check(val < 100);
|
2014-12-04 11:27:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2014-12-04 11:27:38 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool base64_eq(char const* a, char const* b)
|
2015-01-03 21:35:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;; ++a, ++b)
|
2015-01-03 21:35:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
while (*a == '\r' || *a == '\n')
|
|
|
|
{
|
|
|
|
++a;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*b == '\r' || *b == '\n')
|
|
|
|
{
|
|
|
|
++b;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*a == '\0' || *b == '\0' || *a != *b)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-01-03 21:35:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return *a == *b;
|
2015-01-03 21:35:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int test_base64(void)
|
2014-12-04 19:58:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-05-01 15:46:41 +00:00
|
|
|
char* in;
|
|
|
|
char* out;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
out = tr_base64_encode_str("YOYO!", &len);
|
|
|
|
check_uint_eq(strlen(out), len);
|
|
|
|
check(base64_eq("WU9ZTyE=", out));
|
|
|
|
in = tr_base64_decode_str(out, &len);
|
|
|
|
check_uint_eq(5, len);
|
|
|
|
check_streq("YOYO!", in);
|
|
|
|
tr_free(in);
|
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
out = tr_base64_encode("", 0, &len);
|
|
|
|
check_uint_eq(0, len);
|
|
|
|
check_streq("", out);
|
|
|
|
tr_free(out);
|
|
|
|
out = tr_base64_decode("", 0, &len);
|
|
|
|
check_uint_eq(0, len);
|
|
|
|
check_streq("", out);
|
|
|
|
tr_free(out);
|
|
|
|
|
|
|
|
out = tr_base64_encode(NULL, 0, &len);
|
|
|
|
check_uint_eq(0, len);
|
|
|
|
check(out == NULL);
|
|
|
|
out = tr_base64_decode(NULL, 0, &len);
|
|
|
|
check_uint_eq(0, len);
|
|
|
|
check(out == NULL);
|
2014-12-04 19:58:34 +00:00
|
|
|
|
|
|
|
#define MAX_BUF_SIZE 1024
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t i = 1; i <= MAX_BUF_SIZE; ++i)
|
2014-12-04 19:58:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[MAX_BUF_SIZE + 1];
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t j = 0; j < i; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
buf[j] = (char)tr_rand_int_weak(256);
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
out = tr_base64_encode(buf, i, &len);
|
2017-04-19 12:04:45 +00:00
|
|
|
check_uint_eq(strlen(out), len);
|
|
|
|
in = tr_base64_decode(out, len, &len);
|
2017-05-13 22:38:31 +00:00
|
|
|
check_uint_eq(i, len);
|
2017-04-19 12:04:45 +00:00
|
|
|
check(memcmp(in, buf, len) == 0);
|
|
|
|
tr_free(in);
|
|
|
|
tr_free(out);
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (size_t j = 0; j < i; ++j)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
buf[j] = (char)(1 + tr_rand_int_weak(255));
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
buf[i] = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
out = tr_base64_encode_str(buf, &len);
|
|
|
|
check_uint_eq(strlen(out), len);
|
|
|
|
in = tr_base64_decode_str(out, &len);
|
2017-05-13 22:38:31 +00:00
|
|
|
check_uint_eq(i, len);
|
2017-04-19 12:04:45 +00:00
|
|
|
check_streq(in, buf);
|
|
|
|
tr_free(in);
|
|
|
|
tr_free(out);
|
2014-12-04 19:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef MAX_BUF_SIZE
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2014-12-04 19:58:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int main(void)
|
2014-01-19 04:41:20 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
testFunc const tests[] =
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
test_torrent_hash,
|
|
|
|
test_encrypt_decrypt,
|
|
|
|
test_sha1,
|
|
|
|
test_ssha1,
|
|
|
|
test_random,
|
|
|
|
test_base64
|
|
|
|
};
|
|
|
|
|
|
|
|
return runTests(tests, NUM_TESTS(tests));
|
2014-01-19 04:41:20 +00:00
|
|
|
}
|