summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordubslow <bunslow@gmail.com>2014-10-17 06:02:15 -0500
committerdubslow <bunslow@gmail.com>2014-10-17 06:02:15 -0500
commitffb13e4716e002c0e532afec6723d90ded72d451 (patch)
tree3735f4feab020d6bdd5e017a396aa0e5945a07e9
parent6114bd7f3ef1fda71e45e81f259074cf4f8e58eb (diff)
add load/save from key instead of pw
-rw-r--r--auto_tests/encryptsave_test.c40
-rw-r--r--toxencryptsave/toxencryptsave.c45
-rw-r--r--toxencryptsave/toxencryptsave.h15
3 files changed, 100 insertions, 0 deletions
diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c
index 752f906f..b335cbe1 100644
--- a/auto_tests/encryptsave_test.c
+++ b/auto_tests/encryptsave_test.c
@@ -25,6 +25,9 @@ unsigned char known_key[crypto_box_BEFORENMBYTES] = {0x29, 0x36, 0x1c, 0x9e, 0x6
25char* pw = "hunter2"; 25char* pw = "hunter2";
26unsigned int pwlen = 7; 26unsigned int pwlen = 7;
27 27
28unsigned char known_key2[crypto_box_BEFORENMBYTES] = {0x7a, 0xfa, 0x95, 0x45, 0x36, 0x8a, 0xa2, 0x5c, 0x40, 0xfd, 0xc0, 0xe2, 0x35, 0x8, 0x7, 0x88, 0xfa, 0xf9, 0x37, 0x86, 0xeb, 0xff, 0x50, 0x4f, 0x3, 0xe2, 0xf6, 0xd9, 0xef, 0x9, 0x17, 0x1};
29// same as above, except standard opslimit instead of extra ops limit for test_known_kdf, and hash pw before kdf for compat
30
28/* cause I'm shameless */ 31/* cause I'm shameless */
29void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) 32void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
30{ 33{
@@ -61,11 +64,13 @@ START_TEST(test_save_friend)
61 tox_get_address(tox2, address); 64 tox_get_address(tox2, address);
62 int test = tox_add_friend(tox1, address, (uint8_t *)"Gentoo", 7); 65 int test = tox_add_friend(tox1, address, (uint8_t *)"Gentoo", 7);
63 ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); 66 ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
67
64 uint32_t size = tox_encrypted_size(tox1); 68 uint32_t size = tox_encrypted_size(tox1);
65 uint8_t data[size]; 69 uint8_t data[size];
66 test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25); 70 test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25);
67 ck_assert_msg(test == 0, "failed to encrypted save"); 71 ck_assert_msg(test == 0, "failed to encrypted save");
68 ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing"); 72 ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing");
73
69 Tox *tox3 = tox_new(0); 74 Tox *tox3 = tox_new(0);
70 test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25); 75 test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25);
71 ck_assert_msg(test == 0, "failed to encrypted load"); 76 ck_assert_msg(test == 0, "failed to encrypted load");
@@ -73,6 +78,41 @@ START_TEST(test_save_friend)
73 test = tox_get_client_id(tox3, 0, address2); 78 test = tox_get_client_id(tox3, 0, address2);
74 ck_assert_msg(test == 0, "no friends!"); 79 ck_assert_msg(test == 0, "no friends!");
75 ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match!"); 80 ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match!");
81
82 size = tox_encrypted_size(tox3);
83 uint8_t data2[size];
84 uint8_t key[32 + crypto_box_BEFORENMBYTES];
85 memcpy(key, salt, 32); memcpy(key+32, known_key2, crypto_box_BEFORENMBYTES);
86 test = tox_encrypted_key_save(tox3, data2, key);
87 ck_assert_msg(test == 0, "failed to encrypted save the second");
88 ck_assert_msg(tox_is_save_encrypted(data2) == 1, "magic number the second missing");
89
90 // first test tox_encrypted_key_load
91 Tox* tox4 = tox_new(0);
92 test = tox_encrypted_key_load(tox4, data2, size, key);
93 ck_assert_msg(test == 0, "failed to encrypted load the second");
94 uint8_t address4[TOX_CLIENT_ID_SIZE];
95 test = tox_get_client_id(tox4, 0, address4);
96 ck_assert_msg(test == 0, "no friends! the second");
97 ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match! the second");
98
99 // now test compaitibilty with tox_encrypted_load, first manually...
100 uint8_t out1[size], out2[size];
101 printf("Trying to decrypt from pw:\n");
102 uint32_t sz1 = tox_pass_decrypt(data2+TOX_ENC_SAVE_MAGIC_LENGTH, size-TOX_ENC_SAVE_MAGIC_LENGTH, pw, pwlen, out1);
103 uint32_t sz2 = tox_pass_key_decrypt(data2+TOX_ENC_SAVE_MAGIC_LENGTH, size-TOX_ENC_SAVE_MAGIC_LENGTH, key, out2);
104 ck_assert_msg(sz1 == sz2, "differing output sizes");
105 ck_assert_msg(memcmp(out1, out2, sz1) == 0, "differing output data");
106
107 // and now with the code in use (I only bothered with manually to debug this, and it seems a waste
108 // to remove the manual check now that it's there)
109 Tox* tox5 = tox_new(0);
110 test = tox_encrypted_load(tox5, data2, size, pw, pwlen);
111 ck_assert_msg(test == 0, "failed to encrypted load the third");
112 uint8_t address5[TOX_CLIENT_ID_SIZE];
113 test = tox_get_client_id(tox4, 0, address5);
114 ck_assert_msg(test == 0, "no friends! the third");
115 ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match! the third");
76} 116}
77END_TEST 117END_TEST
78 118
diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c
index 7492f06b..0d63a0aa 100644
--- a/toxencryptsave/toxencryptsave.c
+++ b/toxencryptsave/toxencryptsave.c
@@ -175,6 +175,28 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3
175 return tox_pass_encrypt(temp_data, temp_size, passphrase, pplength, data); 175 return tox_pass_encrypt(temp_data, temp_size, passphrase, pplength, data);
176} 176}
177 177
178/* Save the messenger data encrypted with the given key from tox_derive_key.
179 * data must be at least tox_encrypted_size().
180 *
181 * returns 0 on success
182 * returns -1 on failure
183 */
184int tox_encrypted_key_save(const Tox* tox, uint8_t* data, uint8_t* key)
185{
186 /* first get plain save data */
187 uint32_t temp_size = tox_size(tox);
188 uint8_t temp_data[temp_size];
189 tox_save(tox, temp_data);
190
191 /* the output data consists of, in order: magic number, enc_data */
192 /* first add the magic number */
193 memcpy(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH);
194 data += TOX_ENC_SAVE_MAGIC_LENGTH;
195
196 /* encrypt */
197 return tox_pass_key_encrypt(temp_data, temp_size, key, data);
198}
199
178/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by 200/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
179 * tox_derive_key_from_pass. 201 * tox_derive_key_from_pass.
180 * 202 *
@@ -260,6 +282,29 @@ int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *
260 return tox_load(tox, temp_data, decrypt_length); 282 return tox_load(tox, temp_data, decrypt_length);
261} 283}
262 284
285/* Load the messenger from encrypted data of size length, with key from tox_derive_key.
286 *
287 * returns 0 on success
288 * returns -1 on failure
289 */
290int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t* key)
291{
292 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0)
293 return -1;
294
295 data += TOX_ENC_SAVE_MAGIC_LENGTH;
296 length -= TOX_ENC_SAVE_MAGIC_LENGTH;
297
298 uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
299 uint8_t temp_data[decrypt_length];
300
301 if (tox_pass_key_decrypt(data, length, key, temp_data)
302 != decrypt_length)
303 return -1;
304 printf("tox key load: decryption passed\n");
305 return tox_load(tox, temp_data, decrypt_length);
306}
307
263/* Determines whether or not the given data is encrypted (by checking the magic number) 308/* Determines whether or not the given data is encrypted (by checking the magic number)
264 * 309 *
265 * returns 1 if it is encrypted 310 * returns 1 if it is encrypted
diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h
index d805cf97..ddfce485 100644
--- a/toxencryptsave/toxencryptsave.h
+++ b/toxencryptsave/toxencryptsave.h
@@ -95,6 +95,14 @@ int tox_pass_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *passphrase
95 */ 95 */
96int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength); 96int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength);
97 97
98/* Save the messenger data encrypted with the given key from tox_derive_key.
99 * data must be at least tox_encrypted_size().
100 *
101 * returns 0 on success
102 * returns -1 on failure
103 */
104int tox_encrypted_key_save(const Tox* tox, uint8_t* data, uint8_t* key);
105
98/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by 106/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
99 * tox_derive_key_from_pass. 107 * tox_derive_key_from_pass.
100 * 108 *
@@ -121,6 +129,13 @@ int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase,
121 */ 129 */
122int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength); 130int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength);
123 131
132/* Load the messenger from encrypted data of size length, with key from tox_derive_key.
133 *
134 * returns 0 on success
135 * returns -1 on failure
136 */
137int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t* key);
138
124/* Determines whether or not the given data is encrypted (by checking the magic number) 139/* Determines whether or not the given data is encrypted (by checking the magic number)
125 * 140 *
126 * returns 1 if it is encrypted 141 * returns 1 if it is encrypted