From e632ef8a478ebb964b855c641e2ba14b279c78e1 Mon Sep 17 00:00:00 2001 From: Dubslow Date: Fri, 27 Feb 2015 17:42:36 -0600 Subject: [PATCH] Realign toxencryptsave with new API --- toxcore/tox.h | 8 ++++++++ toxencryptsave/toxencryptsave.c | 36 ++++++++++++++++----------------- toxencryptsave/toxencryptsave.h | 25 +++++++++++++++-------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/toxcore/tox.h b/toxcore/tox.h index 2acc70ea..abae26e6 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -443,6 +443,14 @@ enum TOX_ERR_NEW { * The byte array to be loaded contained an encrypted save. */ TOX_ERR_NEW_LOAD_ENCRYPTED, + /** + * The encrypted byte array could not be decrypted. Either the data was + * corrupt or the password/key was incorrect. + * + * NOTE: This error code is only set by tox_encrypted_new() and + * tox_encrypted_key_new(), in the toxencryptsave module. + */ + TOX_ERR_NEW_LOAD_DECRYPTION_FAILED, /** * The data format was invalid. This can happen when loading data that was * saved by an older version of Tox, or when the data has been corrupted. diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c index 13a34dea..b801e1ba 100644 --- a/toxencryptsave/toxencryptsave.c +++ b/toxencryptsave/toxencryptsave.c @@ -29,6 +29,7 @@ #include "defines.h" #include "../toxcore/crypto_core.h" #include "../toxcore/tox.h" +#define SET_ERROR_PARAMETER(param, x) {if(param) {*param = x;}} #ifdef VANILLA_NACL #include "crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h" @@ -293,38 +294,42 @@ int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, return tox_pass_key_decrypt(data, length, key, out); } -/* Load the messenger from encrypted data of size length. +/* Load the new messenger from encrypted data of size length. + * All other arguments are like toxcore/tox_new(). * - * returns 0 on success - * returns -1 on failure + * returns NULL on failure; see the documentation in toxcore/tox.h. */ -int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength) +Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase, size_t pplength, TOX_ERR_NEW *error) { uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; uint8_t temp_data[decrypt_length]; if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data) - != decrypt_length) - return -1; + != decrypt_length) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_DECRYPTION_FAILED); + return NULL; + } - return tox_load(tox, temp_data, decrypt_length); + return tox_new(options, temp_data, decrypt_length, error); } /* Load the messenger from encrypted data of size length, with key from tox_derive_key. + * All other arguments are like toxcore/tox_new(). * - * returns 0 on success - * returns -1 on failure + * returns NULL on failure; see the documentation in toxcore/tox.h. */ -int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *key) +Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key, TOX_ERR_NEW *error) { uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; uint8_t temp_data[decrypt_length]; if (tox_pass_key_decrypt(data, length, key, temp_data) - != decrypt_length) - return -1; + != decrypt_length) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_DECRYPTION_FAILED); + return NULL; + } - return tox_load(tox, temp_data, decrypt_length); + return tox_new(options, temp_data, decrypt_length, error); } /* Determines whether or not the given data is encrypted (by checking the magic number) @@ -339,8 +344,3 @@ int tox_is_data_encrypted(const uint8_t *data) else return 0; } - -int tox_is_save_encrypted(const uint8_t *data) -{ - return tox_is_data_encrypted(data); -} diff --git a/toxencryptsave/toxencryptsave.h b/toxencryptsave/toxencryptsave.h index da13f312..ea5f4eab 100644 --- a/toxencryptsave/toxencryptsave.h +++ b/toxencryptsave/toxencryptsave.h @@ -29,10 +29,13 @@ extern "C" { #endif #include +#include #ifndef TOX_DEFINED #define TOX_DEFINED typedef struct Tox Tox; +struct Tox_Options; +typedef uint8_t TOX_ERR_NEW; #endif // these functions provide access to these defines in toxencryptsave.c, which @@ -88,6 +91,9 @@ int tox_pass_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *passphrase /* Save the messenger data encrypted with the given password. * data must be at least tox_encrypted_size(). * + * NOTE: Unlike tox_save(), this function may fail. Be sure to check its return + * value. + * * returns 0 on success * returns -1 on failure */ @@ -104,12 +110,12 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3 */ int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength, uint8_t *out); -/* Load the messenger from encrypted data of size length. +/* Load the new messenger from encrypted data of size length. + * All other arguments are like toxcore/tox_new(). * - * returns 0 on success - * returns -1 on failure + * returns NULL on failure; see the documentation in toxcore/tox.h. */ -int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength); +Tox *tox_encrypted_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *passphrase, size_t pplength, TOX_ERR_NEW *error); /******************************* BEGIN PART 1 ******************************* @@ -161,6 +167,9 @@ int tox_pass_key_encrypt(const uint8_t *data, uint32_t data_len, const uint8_t * /* Save the messenger data encrypted with the given key from tox_derive_key. * data must be at least tox_encrypted_size(). * + * NOTE: Unlike tox_save(), this function may fail. Be sure to check its return + * value. + * * returns 0 on success * returns -1 on failure */ @@ -175,11 +184,12 @@ int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key); int tox_pass_key_decrypt(const uint8_t *data, uint32_t length, const uint8_t *key, uint8_t *out); /* Load the messenger from encrypted data of size length, with key from tox_derive_key. + * All other arguments are like toxcore/tox_new(). * - * returns 0 on success - * returns -1 on failure + * returns NULL on failure; see the documentation in toxcore/tox.h. */ -int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *key); +Tox *tox_encrypted_key_new(const struct Tox_Options *options, const uint8_t *data, size_t length, uint8_t *key, TOX_ERR_NEW *error); + /* Determines whether or not the given data is encrypted (by checking the magic number) * @@ -187,7 +197,6 @@ int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8 * returns 0 otherwise */ int tox_is_data_encrypted(const uint8_t *data); -int tox_is_save_encrypted(const uint8_t *data); // poorly-named alias for backwards compat (oh irony...) #ifdef __cplusplus }