summaryrefslogtreecommitdiff
path: root/toxencryptsave/toxencryptsave.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-10-17 16:06:00 -0400
committerirungentoo <irungentoo@gmail.com>2014-10-17 16:06:00 -0400
commit0fb3062f08b99cfbc3871ca1365d1b6a5b737d68 (patch)
tree4baf63c53117f8dae5a2017e3400c4a001867a94 /toxencryptsave/toxencryptsave.c
parent418bb2b19430866b3e1f44036907ae9d119e1d62 (diff)
parentff1d4faa744138946d12f98d21fcc10f33db10bd (diff)
Merge branch 'master' of https://github.com/dubslow/toxcore
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 31d2be9b..6567eb96 100644
--- a/toxencryptsave/toxencryptsave.c
+++ b/toxencryptsave/toxencryptsave.c
@@ -181,6 +181,28 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3
181 return tox_pass_encrypt(temp_data, temp_size, passphrase, pplength, data); 181 return tox_pass_encrypt(temp_data, temp_size, passphrase, pplength, data);
182} 182}
183 183
184/* Save the messenger data encrypted with the given key from tox_derive_key.
185 * data must be at least tox_encrypted_size().
186 *
187 * returns 0 on success
188 * returns -1 on failure
189 */
190int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key)
191{
192 /* first get plain save data */
193 uint32_t temp_size = tox_size(tox);
194 uint8_t temp_data[temp_size];
195 tox_save(tox, temp_data);
196
197 /* the output data consists of, in order: magic number, enc_data */
198 /* first add the magic number */
199 memcpy(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH);
200 data += TOX_ENC_SAVE_MAGIC_LENGTH;
201
202 /* encrypt */
203 return tox_pass_key_encrypt(temp_data, temp_size, key, data);
204}
205
184/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by 206/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
185 * tox_derive_key_from_pass. 207 * tox_derive_key_from_pass.
186 * 208 *
@@ -266,6 +288,29 @@ int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *
266 return tox_load(tox, temp_data, decrypt_length); 288 return tox_load(tox, temp_data, decrypt_length);
267} 289}
268 290
291/* Load the messenger from encrypted data of size length, with key from tox_derive_key.
292 *
293 * returns 0 on success
294 * returns -1 on failure
295 */
296int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *key)
297{
298 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0)
299 return -1;
300
301 data += TOX_ENC_SAVE_MAGIC_LENGTH;
302 length -= TOX_ENC_SAVE_MAGIC_LENGTH;
303
304 uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
305 uint8_t temp_data[decrypt_length];
306
307 if (tox_pass_key_decrypt(data, length, key, temp_data)
308 != decrypt_length)
309 return -1;
310
311 return tox_load(tox, temp_data, decrypt_length);
312}
313
269/* Determines whether or not the given data is encrypted (by checking the magic number) 314/* Determines whether or not the given data is encrypted (by checking the magic number)
270 * 315 *
271 * returns 1 if it is encrypted 316 * returns 1 if it is encrypted