summaryrefslogtreecommitdiff
path: root/toxencryptsave/toxencryptsave.c
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 /toxencryptsave/toxencryptsave.c
parent6114bd7f3ef1fda71e45e81f259074cf4f8e58eb (diff)
add load/save from key instead of pw
Diffstat (limited to 'toxencryptsave/toxencryptsave.c')
-rw-r--r--toxencryptsave/toxencryptsave.c45
1 files changed, 45 insertions, 0 deletions
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