summaryrefslogtreecommitdiff
path: root/toxencryptsave/toxencryptsave.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxencryptsave/toxencryptsave.c')
-rw-r--r--toxencryptsave/toxencryptsave.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c
index 9b202f49..c086ddb7 100644
--- a/toxencryptsave/toxencryptsave.c
+++ b/toxencryptsave/toxencryptsave.c
@@ -28,7 +28,6 @@
28#include "toxencryptsave.h" 28#include "toxencryptsave.h"
29#include "defines.h" 29#include "defines.h"
30#include "../toxcore/crypto_core.h" 30#include "../toxcore/crypto_core.h"
31#include "../toxcore/tox.h"
32#define SET_ERROR_PARAMETER(param, x) {if(param) {*param = x;}} 31#define SET_ERROR_PARAMETER(param, x) {if(param) {*param = x;}}
33 32
34#ifdef VANILLA_NACL 33#ifdef VANILLA_NACL
@@ -91,7 +90,7 @@ bool tox_derive_key_from_pass(uint8_t *passphrase, size_t pplength, uint8_t *out
91} 90}
92 91
93/* Same as above, except with use the given salt for deterministic key derivation. 92/* Same as above, except with use the given salt for deterministic key derivation.
94 * The salt must be tox_salt_length() bytes in length. 93 * The salt must be TOX_PASS_SALT_LENGTH bytes in length.
95 */ 94 */
96bool tox_derive_key_with_salt(uint8_t *passphrase, size_t pplength, uint8_t *salt, uint8_t *out_key, 95bool tox_derive_key_with_salt(uint8_t *passphrase, size_t pplength, uint8_t *salt, uint8_t *out_key,
97 TOX_ERR_KEY_DERIVATION *error) 96 TOX_ERR_KEY_DERIVATION *error)
@@ -254,32 +253,31 @@ bool tox_pass_key_decrypt(const uint8_t *data, size_t length, const uint8_t *key
254bool tox_pass_decrypt(const uint8_t *data, size_t length, uint8_t *passphrase, size_t pplength, uint8_t *out, 253bool tox_pass_decrypt(const uint8_t *data, size_t length, uint8_t *passphrase, size_t pplength, uint8_t *out,
255 TOX_ERR_DECRYPTION *error) 254 TOX_ERR_DECRYPTION *error)
256{ 255{
256 if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH || pplength == 0) {
257 SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
258 return 0;
259 }
260 if (!data || !passphrase || !out) {
261 SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL);
262 return 0;
263 }
257 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) { 264 if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
258 SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT); 265 SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT);
259 return 0; 266 return 0;
260 } 267 }
261 268
262 uint8_t passkey[crypto_hash_sha256_BYTES];
263 crypto_hash_sha256(passkey, passphrase, pplength);
264
265 uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES]; 269 uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
266 memcpy(salt, data + TOX_ENC_SAVE_MAGIC_LENGTH, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); 270 memcpy(salt, data + TOX_ENC_SAVE_MAGIC_LENGTH, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
267 271
268 /* derive the key */ 272 /* derive the key */
269 uint8_t key[crypto_box_KEYBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES]; 273 uint8_t key[crypto_box_KEYBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
270 274
271 if (crypto_pwhash_scryptsalsa208sha256( 275 if (!tox_derive_key_with_salt(passphrase, pplength, salt, key, NULL)) {
272 key + crypto_pwhash_scryptsalsa208sha256_SALTBYTES,
273 crypto_box_KEYBYTES, (char *)passkey, sizeof(passkey), salt,
274 crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 2, /* slightly stronger */
275 crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE) != 0) {
276 /* out of memory most likely */ 276 /* out of memory most likely */
277 SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED); 277 SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED);
278 return 0; 278 return 0;
279 } 279 }
280 280
281 sodium_memzero(passkey, crypto_hash_sha256_BYTES); /* wipe plaintext pw */
282
283 return tox_pass_key_decrypt(data, length, key, out, error); 281 return tox_pass_key_decrypt(data, length, key, out, error);
284} 282}
285 283