From 45f8bb8c40fd1405b88b6ed403fe130f6344942d Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Thu, 1 Jan 2015 14:23:47 +0000 Subject: [PATCH] #4400, #5462: Add possibility to test one crypto backend against another (reference) backend --- libtransmission/CMakeLists.txt | 4 +- libtransmission/Makefile.am | 1 + libtransmission/crypto-test-ref.h | 174 ++++++++++++++++++++++++++++++ libtransmission/crypto-test.c | 79 +++++++++++--- 4 files changed, 244 insertions(+), 14 deletions(-) create mode 100644 libtransmission/crypto-test-ref.h diff --git a/libtransmission/CMakeLists.txt b/libtransmission/CMakeLists.txt index bf69cff7a..959e75bed 100644 --- a/libtransmission/CMakeLists.txt +++ b/libtransmission/CMakeLists.txt @@ -218,9 +218,11 @@ if(ENABLE_TESTS) target_link_libraries(${TR_NAME}-test ${TR_NAME}) + set(crypto-test_ADD_SOURCES crypto-test-ref.h) + foreach(T bitfield blocklist clients crypto error file history json magnet metainfo move peer-msgs quark rename rpc session tr-getopt utils variant) set(TP ${TR_NAME}-test-${T}) - add_executable(${TP} ${T}-test.c) + add_executable(${TP} ${T}-test.c ${${T}-test_ADD_SOURCES}) target_link_libraries(${TP} ${TR_NAME} ${TR_NAME}-test) add_test(${T} ${TP}) endforeach() diff --git a/libtransmission/Makefile.am b/libtransmission/Makefile.am index 75f1a4eb8..425d774f0 100644 --- a/libtransmission/Makefile.am +++ b/libtransmission/Makefile.am @@ -196,6 +196,7 @@ clients_test_LDADD = ${apps_ldadd} clients_test_LDFLAGS = ${apps_ldflags} crypto_test_SOURCES = crypto-test.c $(TEST_SOURCES) +crypto_test_HEADERS = crypto-test-ref.h crypto_test_LDADD = ${apps_ldadd} crypto_test_LDFLAGS = ${apps_ldflags} diff --git a/libtransmission/crypto-test-ref.h b/libtransmission/crypto-test-ref.h new file mode 100644 index 000000000..843f3fbc3 --- /dev/null +++ b/libtransmission/crypto-test-ref.h @@ -0,0 +1,174 @@ +/* + * This file Copyright (C) 2015 Mnemosyne LLC + * + * It may be used under the GNU GPL versions 2 or 3 + * or any future license endorsed by Mnemosyne LLC. + * + * $Id$ + */ + +#ifndef TR_CRYPTO_TEST_REF_H +#define TR_CRYPTO_TEST_REF_H + +/* #define CRYPTO_REFERENCE_CHECK */ + +#ifdef CRYPTO_REFERENCE_CHECK + +#define KEY_LEN KEY_LEN_ + +#define tr_sha1_ctx_t tr_sha1_ctx_t_ +#define tr_rc4_ctx_t tr_rc4_ctx_t_ +#define tr_dh_ctx_t tr_dh_ctx_t_ +#define tr_dh_secret_t tr_dh_secret_t_ +#define tr_crypto tr_crypto_ +#define tr_cryptoConstruct tr_cryptoConstruct_ +#define tr_cryptoDestruct tr_cryptoDestruct_ +#define tr_cryptoSetTorrentHash tr_cryptoSetTorrentHash_ +#define tr_cryptoGetTorrentHash tr_cryptoGetTorrentHash_ +#define tr_cryptoHasTorrentHash tr_cryptoHasTorrentHash_ +#define tr_cryptoComputeSecret tr_cryptoComputeSecret_ +#define tr_cryptoGetMyPublicKey tr_cryptoGetMyPublicKey_ +#define tr_cryptoDecryptInit tr_cryptoDecryptInit_ +#define tr_cryptoDecrypt tr_cryptoDecrypt_ +#define tr_cryptoEncryptInit tr_cryptoEncryptInit_ +#define tr_cryptoEncrypt tr_cryptoEncrypt_ +#define tr_cryptoSecretKeySha1 tr_cryptoSecretKeySha1_ +#define tr_sha1 tr_sha1_ +#define tr_sha1_init tr_sha1_init_ +#define tr_sha1_update tr_sha1_update_ +#define tr_sha1_final tr_sha1_final_ +#define tr_rc4_new tr_rc4_new_ +#define tr_rc4_free tr_rc4_free_ +#define tr_rc4_set_key tr_rc4_set_key_ +#define tr_rc4_process tr_rc4_process_ +#define tr_dh_new tr_dh_new_ +#define tr_dh_free tr_dh_free_ +#define tr_dh_make_key tr_dh_make_key_ +#define tr_dh_agree tr_dh_agree_ +#define tr_dh_secret_derive tr_dh_secret_derive_ +#define tr_dh_secret_free tr_dh_secret_free_ +#define tr_dh_align_key tr_dh_align_key_ +#define tr_rand_int tr_rand_int_ +#define tr_rand_int_weak tr_rand_int_weak_ +#define tr_rand_buffer tr_rand_buffer_ +#define tr_ssha1 tr_ssha1_ +#define tr_ssha1_matches tr_ssha1_matches_ +#define tr_base64_encode tr_base64_encode_ +#define tr_base64_encode_str tr_base64_encode_str_ +#define tr_base64_encode_impl tr_base64_encode_impl_ +#define tr_base64_decode tr_base64_decode_ +#define tr_base64_decode_str tr_base64_decode_str_ +#define tr_base64_decode_impl tr_base64_decode_impl_ +#define tr_sha1_to_hex tr_sha1_to_hex_ +#define tr_hex_to_sha1 tr_hex_to_sha1_ + +#undef TR_ENCRYPTION_H +#undef TR_CRYPTO_UTILS_H + +#include "crypto.h" +#include "crypto-utils.h" +#include "crypto.c" +#include "crypto-utils.c" +#include "crypto-utils-openssl.c" + +#undef KEY_LEN_ + +#undef tr_sha1_ctx_t +#undef tr_rc4_ctx_t +#undef tr_dh_ctx_t +#undef tr_dh_secret_t +#undef tr_crypto +#undef tr_cryptoConstruct +#undef tr_cryptoDestruct +#undef tr_cryptoSetTorrentHash +#undef tr_cryptoGetTorrentHash +#undef tr_cryptoHasTorrentHash +#undef tr_cryptoComputeSecret +#undef tr_cryptoGetMyPublicKey +#undef tr_cryptoDecryptInit +#undef tr_cryptoDecrypt +#undef tr_cryptoEncryptInit +#undef tr_cryptoEncrypt +#undef tr_cryptoSecretKeySha1 +#undef tr_sha1 +#undef tr_sha1_init +#undef tr_sha1_update +#undef tr_sha1_final +#undef tr_rc4_new +#undef tr_rc4_free +#undef tr_rc4_set_key +#undef tr_rc4_process +#undef tr_dh_new +#undef tr_dh_free +#undef tr_dh_make_key +#undef tr_dh_agree +#undef tr_dh_secret_derive +#undef tr_dh_secret_free +#undef tr_dh_align_key +#undef tr_rand_int +#undef tr_rand_int_weak +#undef tr_rand_buffer +#undef tr_ssha1 +#undef tr_ssha1_matches +#undef tr_base64_encode +#undef tr_base64_encode_str +#undef tr_base64_encode_impl +#undef tr_base64_decode +#undef tr_base64_decode_str +#undef tr_base64_decode_impl +#undef tr_sha1_to_hex +#undef tr_hex_to_sha1 + +#else /* CRYPTO_REFERENCE_CHECK */ + +#define KEY_LEN_ KEY_LEN + +#define tr_sha1_ctx_t_ tr_sha1_ctx_t +#define tr_rc4_ctx_t_ tr_rc4_ctx_t +#define tr_dh_ctx_t_ tr_dh_ctx_t +#define tr_dh_secret_t_ tr_dh_secret_t +#define tr_crypto_ tr_crypto +#define tr_cryptoConstruct_ tr_cryptoConstruct +#define tr_cryptoDestruct_ tr_cryptoDestruct +#define tr_cryptoSetTorrentHash_ tr_cryptoSetTorrentHash +#define tr_cryptoGetTorrentHash_ tr_cryptoGetTorrentHash +#define tr_cryptoHasTorrentHash_ tr_cryptoHasTorrentHash +#define tr_cryptoComputeSecret_ tr_cryptoComputeSecret +#define tr_cryptoGetMyPublicKey_ tr_cryptoGetMyPublicKey +#define tr_cryptoDecryptInit_ tr_cryptoDecryptInit +#define tr_cryptoDecrypt_ tr_cryptoDecrypt +#define tr_cryptoEncryptInit_ tr_cryptoEncryptInit +#define tr_cryptoEncrypt_ tr_cryptoEncrypt +#define tr_cryptoSecretKeySha1_ tr_cryptoSecretKeySha1 +#define tr_sha1_ tr_sha1 +#define tr_sha1_init_ tr_sha1_init +#define tr_sha1_update_ tr_sha1_update +#define tr_sha1_final_ tr_sha1_final +#define tr_rc4_new_ tr_rc4_new +#define tr_rc4_free_ tr_rc4_free +#define tr_rc4_set_key_ tr_rc4_set_key +#define tr_rc4_process_ tr_rc4_process +#define tr_dh_new_ tr_dh_new +#define tr_dh_free_ tr_dh_free +#define tr_dh_make_key_ tr_dh_make_key +#define tr_dh_agree_ tr_dh_agree +#define tr_dh_secret_derive_ tr_dh_secret_derive +#define tr_dh_secret_free_ tr_dh_secret_free +#define tr_dh_align_key_ tr_dh_align_key +#define tr_rand_int_ tr_rand_int +#define tr_rand_int_weak_ tr_rand_int_weak +#define tr_rand_buffer_ tr_rand_buffer +#define tr_ssha1_ tr_ssha1 +#define tr_ssha1_matches_ tr_ssha1_matches +#define tr_base64_encode_ tr_base64_encode +#define tr_base64_encode_str_ tr_base64_encode_str +#define tr_base64_encode_impl_ tr_base64_encode_impl +#define tr_base64_decode_ tr_base64_decode +#define tr_base64_decode_str_ tr_base64_decode_str +#define tr_base64_decode_impl_ tr_base64_decode_impl +#define tr_sha1_to_hex_ tr_sha1_to_hex +#define tr_hex_to_sha1_ tr_hex_to_sha1 + +#endif /* CRYPTO_REFERENCE_CHECK */ + +#endif /* TR_CRYPTO_TEST_REF_H */ diff --git a/libtransmission/crypto-test.c b/libtransmission/crypto-test.c index 6474907cf..90eeb4b4c 100644 --- a/libtransmission/crypto-test.c +++ b/libtransmission/crypto-test.c @@ -15,7 +15,7 @@ #include "libtransmission-test.h" -#define SHA_DIGEST_LENGTH 20 +#include "crypto-test-ref.h" static int test_torrent_hash (void) @@ -60,7 +60,8 @@ test_torrent_hash (void) static int test_encrypt_decrypt (void) { - tr_crypto a, b; + tr_crypto a; + tr_crypto_ b; uint8_t hash[SHA_DIGEST_LENGTH]; const char test1[] = { "test1" }; char buf11[sizeof (test1)], buf12[sizeof (test1)]; @@ -72,23 +73,23 @@ test_encrypt_decrypt (void) hash[i] = i; tr_cryptoConstruct (&a, hash, false); - tr_cryptoConstruct (&b, hash, true); - check (tr_cryptoComputeSecret (&a, tr_cryptoGetMyPublicKey (&b, &i))); - check (tr_cryptoComputeSecret (&b, tr_cryptoGetMyPublicKey (&a, &i))); + tr_cryptoConstruct_ (&b, hash, true); + check (tr_cryptoComputeSecret (&a, tr_cryptoGetMyPublicKey_ (&b, &i))); + check (tr_cryptoComputeSecret_ (&b, tr_cryptoGetMyPublicKey (&a, &i))); tr_cryptoEncryptInit (&a); tr_cryptoEncrypt (&a, sizeof (test1), test1, buf11); - tr_cryptoDecryptInit (&b); - tr_cryptoDecrypt (&b, sizeof (test1), buf11, buf12); + 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_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_ (&b); tr_cryptoDestruct (&a); return 0; @@ -98,12 +99,17 @@ static int test_sha1 (void) { 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; } @@ -129,12 +135,13 @@ test_ssha1 (void) for (j = 0; j < HASH_COUNT; ++j) { - hashes[j] = tr_ssha1 (phrase); + hashes[j] = j % 2 == 0 ? tr_ssha1 (phrase) : tr_ssha1_ (phrase); check (hashes[j] != NULL); /* phrase matches each of generated hashes */ check (tr_ssha1_matches (hashes[j], phrase)); + check (tr_ssha1_matches_ (hashes[j], phrase)); } for (j = 0; j < HASH_COUNT; ++j) @@ -152,8 +159,11 @@ test_ssha1 (void) 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)); + { + /* changed phrase doesn't match the hashes */ + check (!tr_ssha1_matches (hashes[j], phrase)); + check (!tr_ssha1_matches_ (hashes[j], phrase)); + } for (j = 0; j < HASH_COUNT; ++j) tr_free (hashes[j]); @@ -193,6 +203,15 @@ test_base64 (void) 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_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); @@ -203,6 +222,15 @@ test_base64 (void) check_int_eq (0, len); check_streq ("", out); tr_free (out); + out = tr_base64_decode_ ("", 0, &len); + check_int_eq (0, len); + check_streq ("", out); + tr_free (out); + + out = tr_base64_encode_ ("", 0, &len); + check_int_eq (0, len); + check_streq ("", out); + tr_free (out); out = tr_base64_decode ("", 0, &len); check_int_eq (0, len); check_streq ("", out); @@ -212,6 +240,15 @@ test_base64 (void) check_int_eq (0, len); check (out == NULL); tr_free (out); + out = tr_base64_decode_ (NULL, 0, &len); + check_int_eq (0, len); + check (out == NULL); + tr_free (out); + + out = tr_base64_encode_ (NULL, 0, &len); + check_int_eq (0, len); + check (out == NULL); + tr_free (out); out = tr_base64_decode (NULL, 0, &len); check_int_eq (0, len); check (out == NULL); @@ -229,6 +266,14 @@ test_base64 (void) 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); + + 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); @@ -241,6 +286,14 @@ test_base64 (void) 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); + + 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); -- 2.40.0