| 1 | #include <string.h> |
| 2 | |
| 3 | #include "transmission.h" |
| 4 | #include "crypto.h" |
| 5 | |
| 6 | #include "libtransmission-test.h" |
| 7 | |
| 8 | #define SHA_DIGEST_LENGTH 20 |
| 9 | |
| 10 | static int |
| 11 | test_torrent_hash (void) |
| 12 | { |
| 13 | tr_crypto a; |
| 14 | uint8_t hash[SHA_DIGEST_LENGTH]; |
| 15 | int i; |
| 16 | |
| 17 | for (i = 0; i < SHA_DIGEST_LENGTH; ++i) |
| 18 | hash[i] = i; |
| 19 | |
| 20 | tr_cryptoConstruct (&a, NULL, true); |
| 21 | |
| 22 | check (!tr_cryptoHasTorrentHash (&a)); |
| 23 | #ifdef NDEBUG |
| 24 | check (tr_cryptoGetTorrentHash (&a) == NULL); |
| 25 | #endif |
| 26 | |
| 27 | tr_cryptoSetTorrentHash (&a, hash); |
| 28 | check (tr_cryptoHasTorrentHash (&a)); |
| 29 | check (tr_cryptoGetTorrentHash (&a) != NULL); |
| 30 | check (memcmp (tr_cryptoGetTorrentHash (&a), hash, SHA_DIGEST_LENGTH) == 0); |
| 31 | |
| 32 | tr_cryptoDestruct (&a); |
| 33 | |
| 34 | for (i = 0; i < SHA_DIGEST_LENGTH; ++i) |
| 35 | hash[i] = i + 1; |
| 36 | |
| 37 | tr_cryptoConstruct (&a, hash, false); |
| 38 | |
| 39 | check (tr_cryptoHasTorrentHash (&a)); |
| 40 | check (tr_cryptoGetTorrentHash (&a) != NULL); |
| 41 | check (memcmp (tr_cryptoGetTorrentHash (&a), hash, SHA_DIGEST_LENGTH) == 0); |
| 42 | |
| 43 | tr_cryptoSetTorrentHash (&a, NULL); |
| 44 | check (!tr_cryptoHasTorrentHash (&a)); |
| 45 | #ifdef NDEBUG |
| 46 | check (tr_cryptoGetTorrentHash (&a) == NULL); |
| 47 | #endif |
| 48 | |
| 49 | tr_cryptoDestruct (&a); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | test_encrypt_decrypt (void) |
| 56 | { |
| 57 | tr_crypto a, b; |
| 58 | uint8_t hash[SHA_DIGEST_LENGTH]; |
| 59 | const char test1[] = { "test1" }; |
| 60 | char buf11[sizeof (test1)], buf12[sizeof (test1)]; |
| 61 | const char test2[] = { "@#)C$@)#(*%bvkdjfhwbc039bc4603756VB3)" }; |
| 62 | char buf21[sizeof (test2)], buf22[sizeof (test2)]; |
| 63 | int i; |
| 64 | |
| 65 | for (i = 0; i < SHA_DIGEST_LENGTH; ++i) |
| 66 | hash[i] = i; |
| 67 | |
| 68 | tr_cryptoConstruct (&a, hash, false); |
| 69 | tr_cryptoConstruct (&b, hash, true); |
| 70 | tr_cryptoComputeSecret (&a, tr_cryptoGetMyPublicKey (&b, &i)); |
| 71 | tr_cryptoComputeSecret (&b, tr_cryptoGetMyPublicKey (&a, &i)); |
| 72 | |
| 73 | tr_cryptoEncryptInit (&a); |
| 74 | tr_cryptoEncrypt (&a, sizeof (test1), test1, buf11); |
| 75 | tr_cryptoDecryptInit (&b); |
| 76 | tr_cryptoDecrypt (&b, sizeof (test1), buf11, buf12); |
| 77 | check_streq (test1, buf12); |
| 78 | |
| 79 | tr_cryptoEncryptInit (&b); |
| 80 | tr_cryptoEncrypt (&b, sizeof (test2), test2, buf21); |
| 81 | tr_cryptoDecryptInit (&a); |
| 82 | tr_cryptoDecrypt (&a, sizeof (test2), buf21, buf22); |
| 83 | check_streq (test2, buf22); |
| 84 | |
| 85 | tr_cryptoDestruct (&b); |
| 86 | tr_cryptoDestruct (&a); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int |
| 92 | test_sha1 (void) |
| 93 | { |
| 94 | uint8_t hash[SHA_DIGEST_LENGTH]; |
| 95 | |
| 96 | tr_sha1 (hash, "test", 4, NULL); |
| 97 | 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); |
| 98 | |
| 99 | tr_sha1 (hash, "1", 1, "22", 2, "333", 3, NULL); |
| 100 | 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); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int |
| 106 | test_ssha1 (void) |
| 107 | { |
| 108 | const char * const test_data[] = |
| 109 | { |
| 110 | "test", |
| 111 | "QNY)(*#$B)!_X$B !_B#($^!)*&$%CV!#)&$C!@$(P*)" |
| 112 | }; |
| 113 | |
| 114 | size_t i; |
| 115 | |
| 116 | #define HASH_COUNT (16 * 1024) |
| 117 | |
| 118 | for (i = 0; i < sizeof (test_data) / sizeof (*test_data); ++i) |
| 119 | { |
| 120 | char * const phrase = tr_strdup (test_data[i]); |
| 121 | char ** hashes = tr_new (char *, HASH_COUNT); |
| 122 | size_t j; |
| 123 | |
| 124 | for (j = 0; j < HASH_COUNT; ++j) |
| 125 | { |
| 126 | hashes[j] = tr_ssha1 (phrase); |
| 127 | |
| 128 | check (hashes[j] != NULL); |
| 129 | |
| 130 | /* phrase matches each of generated hashes */ |
| 131 | check (tr_ssha1_matches (hashes[j], phrase)); |
| 132 | } |
| 133 | |
| 134 | for (j = 0; j < HASH_COUNT; ++j) |
| 135 | { |
| 136 | size_t k; |
| 137 | |
| 138 | /* all hashes are different */ |
| 139 | for (k = 0; k < HASH_COUNT; ++k) |
| 140 | check (k == j || strcmp (hashes[j], hashes[k]) != 0); |
| 141 | } |
| 142 | |
| 143 | /* exchange two first chars */ |
| 144 | phrase[0] ^= phrase[1]; |
| 145 | phrase[1] ^= phrase[0]; |
| 146 | phrase[0] ^= phrase[1]; |
| 147 | |
| 148 | for (j = 0; j < HASH_COUNT; ++j) |
| 149 | /* changed phrase doesn't match the hashes */ |
| 150 | check (!tr_ssha1_matches (hashes[j], phrase)); |
| 151 | |
| 152 | for (j = 0; j < HASH_COUNT; ++j) |
| 153 | tr_free (hashes[j]); |
| 154 | |
| 155 | tr_free (hashes); |
| 156 | tr_free (phrase); |
| 157 | } |
| 158 | |
| 159 | #undef HASH_COUNT |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int |
| 165 | main (void) |
| 166 | { |
| 167 | const testFunc tests[] = { test_torrent_hash, |
| 168 | test_encrypt_decrypt, |
| 169 | test_sha1, |
| 170 | test_ssha1 }; |
| 171 | |
| 172 | return runTests (tests, NUM_TESTS (tests)); |
| 173 | } |