summaryrefslogtreecommitdiff
path: root/toxencryptsave
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
parent418bb2b19430866b3e1f44036907ae9d119e1d62 (diff)
parentff1d4faa744138946d12f98d21fcc10f33db10bd (diff)
Merge branch 'master' of https://github.com/dubslow/toxcore
Diffstat (limited to 'toxencryptsave')
-rw-r--r--toxencryptsave/toxencryptsave.c45
-rw-r--r--toxencryptsave/toxencryptsave.h15
2 files changed, 60 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
diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h
index b00570e0..14334ea7 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