diff options
author | dubslow <bunslow@gmail.com> | 2014-10-17 06:02:15 -0500 |
---|---|---|
committer | dubslow <bunslow@gmail.com> | 2014-10-17 06:02:15 -0500 |
commit | ffb13e4716e002c0e532afec6723d90ded72d451 (patch) | |
tree | 3735f4feab020d6bdd5e017a396aa0e5945a07e9 /toxencryptsave | |
parent | 6114bd7f3ef1fda71e45e81f259074cf4f8e58eb (diff) |
add load/save from key instead of pw
Diffstat (limited to 'toxencryptsave')
-rw-r--r-- | toxencryptsave/toxencryptsave.c | 45 | ||||
-rw-r--r-- | toxencryptsave/toxencryptsave.h | 15 |
2 files changed, 60 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 | */ | ||
184 | int 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 | */ | ||
290 | int 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 | */ |
96 | int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength); | 96 | int 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 | */ | ||
104 | int 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 | */ |
122 | int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength); | 130 | int 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 | */ | ||
137 | int 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 |