1 /*
2 * This file Copyright (C) 2007-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 */
8
9 #include <string.h> /* memcpy(), memmove(), memset() */
10
11 #include "transmission.h"
12 #include "crypto.h"
13 #include "crypto-utils.h"
14 #include "tr-assert.h"
15 #include "utils.h"
16
17 /**
18 ***
19 **/
20
21 #define PRIME_LEN 96
22 #define DH_PRIVKEY_LEN 20
23
24 static uint8_t const dh_P[PRIME_LEN] =
25 {
26 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
27 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
28 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
29 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
30 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
31 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
32 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
33 0xA6, 0x3A, 0x36, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x05, 0x63,
34 };
35
36 static uint8_t const dh_G[] = { 2 };
37
38 /**
39 ***
40 **/
41
ensureKeyExists(tr_crypto * crypto)42 static void ensureKeyExists(tr_crypto* crypto)
43 {
44 if (crypto->dh == NULL)
45 {
46 size_t public_key_length;
47
48 crypto->dh = tr_dh_new(dh_P, sizeof(dh_P), dh_G, sizeof(dh_G));
49 tr_dh_make_key(crypto->dh, DH_PRIVKEY_LEN, crypto->myPublicKey, &public_key_length);
50
51 TR_ASSERT(public_key_length == KEY_LEN);
52 }
53 }
54
tr_cryptoConstruct(tr_crypto * crypto,uint8_t const * torrentHash,bool isIncoming)55 void tr_cryptoConstruct(tr_crypto* crypto, uint8_t const* torrentHash, bool isIncoming)
56 {
57 memset(crypto, 0, sizeof(tr_crypto));
58
59 crypto->isIncoming = isIncoming;
60 tr_cryptoSetTorrentHash(crypto, torrentHash);
61 }
62
tr_cryptoDestruct(tr_crypto * crypto)63 void tr_cryptoDestruct(tr_crypto* crypto)
64 {
65 tr_dh_secret_free(crypto->mySecret);
66 tr_dh_free(crypto->dh);
67 tr_rc4_free(crypto->enc_key);
68 tr_rc4_free(crypto->dec_key);
69 }
70
71 /**
72 ***
73 **/
74
tr_cryptoComputeSecret(tr_crypto * crypto,uint8_t const * peerPublicKey)75 bool tr_cryptoComputeSecret(tr_crypto* crypto, uint8_t const* peerPublicKey)
76 {
77 ensureKeyExists(crypto);
78 crypto->mySecret = tr_dh_agree(crypto->dh, peerPublicKey, KEY_LEN);
79 return crypto->mySecret != NULL;
80 }
81
tr_cryptoGetMyPublicKey(tr_crypto const * crypto,int * setme_len)82 uint8_t const* tr_cryptoGetMyPublicKey(tr_crypto const* crypto, int* setme_len)
83 {
84 ensureKeyExists((tr_crypto*)crypto);
85 *setme_len = KEY_LEN;
86 return crypto->myPublicKey;
87 }
88
89 /**
90 ***
91 **/
92
initRC4(tr_crypto * crypto,tr_rc4_ctx_t * setme,char const * key)93 static void initRC4(tr_crypto* crypto, tr_rc4_ctx_t* setme, char const* key)
94 {
95 TR_ASSERT(crypto->torrentHashIsSet);
96
97 if (*setme == NULL)
98 {
99 *setme = tr_rc4_new();
100 }
101
102 uint8_t buf[SHA_DIGEST_LENGTH];
103
104 if (tr_cryptoSecretKeySha1(crypto, key, 4, crypto->torrentHash, SHA_DIGEST_LENGTH, buf))
105 {
106 tr_rc4_set_key(*setme, buf, SHA_DIGEST_LENGTH);
107 }
108 }
109
tr_cryptoDecryptInit(tr_crypto * crypto)110 void tr_cryptoDecryptInit(tr_crypto* crypto)
111 {
112 uint8_t discard[1024];
113 char const* txt = crypto->isIncoming ? "keyA" : "keyB";
114
115 initRC4(crypto, &crypto->dec_key, txt);
116 tr_rc4_process(crypto->dec_key, discard, discard, sizeof(discard));
117 }
118
tr_cryptoDecrypt(tr_crypto * crypto,size_t buf_len,void const * buf_in,void * buf_out)119 void tr_cryptoDecrypt(tr_crypto* crypto, size_t buf_len, void const* buf_in, void* buf_out)
120 {
121 /* FIXME: someone calls this function with uninitialized key */
122 if (crypto->dec_key == NULL)
123 {
124 if (buf_in != buf_out)
125 {
126 memmove(buf_out, buf_in, buf_len);
127 }
128
129 return;
130 }
131
132 tr_rc4_process(crypto->dec_key, buf_in, buf_out, buf_len);
133 }
134
tr_cryptoEncryptInit(tr_crypto * crypto)135 void tr_cryptoEncryptInit(tr_crypto* crypto)
136 {
137 uint8_t discard[1024];
138 char const* txt = crypto->isIncoming ? "keyB" : "keyA";
139
140 initRC4(crypto, &crypto->enc_key, txt);
141 tr_rc4_process(crypto->enc_key, discard, discard, sizeof(discard));
142 }
143
tr_cryptoEncrypt(tr_crypto * crypto,size_t buf_len,void const * buf_in,void * buf_out)144 void tr_cryptoEncrypt(tr_crypto* crypto, size_t buf_len, void const* buf_in, void* buf_out)
145 {
146 /* FIXME: someone calls this function with uninitialized key */
147 if (crypto->enc_key == NULL)
148 {
149 if (buf_in != buf_out)
150 {
151 memmove(buf_out, buf_in, buf_len);
152 }
153
154 return;
155 }
156
157 tr_rc4_process(crypto->enc_key, buf_in, buf_out, buf_len);
158 }
159
tr_cryptoSecretKeySha1(tr_crypto const * crypto,void const * prepend_data,size_t prepend_data_size,void const * append_data,size_t append_data_size,uint8_t * hash)160 bool tr_cryptoSecretKeySha1(tr_crypto const* crypto, void const* prepend_data, size_t prepend_data_size,
161 void const* append_data, size_t append_data_size, uint8_t* hash)
162 {
163 TR_ASSERT(crypto != NULL);
164 TR_ASSERT(crypto->mySecret != NULL);
165
166 return tr_dh_secret_derive(crypto->mySecret, prepend_data, prepend_data_size, append_data, append_data_size, hash);
167 }
168
169 /**
170 ***
171 **/
172
tr_cryptoSetTorrentHash(tr_crypto * crypto,uint8_t const * hash)173 void tr_cryptoSetTorrentHash(tr_crypto* crypto, uint8_t const* hash)
174 {
175 crypto->torrentHashIsSet = hash != NULL;
176
177 if (hash != NULL)
178 {
179 memcpy(crypto->torrentHash, hash, SHA_DIGEST_LENGTH);
180 }
181 else
182 {
183 memset(crypto->torrentHash, 0, SHA_DIGEST_LENGTH);
184 }
185 }
186
tr_cryptoGetTorrentHash(tr_crypto const * crypto)187 uint8_t const* tr_cryptoGetTorrentHash(tr_crypto const* crypto)
188 {
189 TR_ASSERT(crypto != NULL);
190
191 return crypto->torrentHashIsSet ? crypto->torrentHash : NULL;
192 }
193
tr_cryptoHasTorrentHash(tr_crypto const * crypto)194 bool tr_cryptoHasTorrentHash(tr_crypto const* crypto)
195 {
196 TR_ASSERT(crypto != NULL);
197
198 return crypto->torrentHashIsSet;
199 }
200