diff options
Diffstat (limited to 'protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c')
-rw-r--r-- | protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c | 71 |
1 files changed, 34 insertions, 37 deletions
diff --git a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c index be4bcaf26c..1d464534d0 100644 --- a/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c +++ b/protocols/Tox/libtox/src/toxencryptsave/toxencryptsave.c @@ -3,32 +3,20 @@ * Copyright © 2013 Tox project. */ -/* +/** * Batch encryption functions. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "../toxcore/ccompat.h" -#include "../toxcore/crypto_core.h" -#include "defines.h" #include "toxencryptsave.h" -#define SET_ERROR_PARAMETER(param, x) do { if (param) { *param = x; } } while (0) - -#ifdef VANILLA_NACL -#include <crypto_box.h> -#include <crypto_hash_sha256.h> -#include "crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h" -#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) -#else + #include <sodium.h> -#endif #include <stdlib.h> #include <string.h> -//!TOKSTYLE- +#include "../toxcore/ccompat.h" +#include "../toxcore/crypto_core.h" +#include "defines.h" + static_assert(TOX_PASS_SALT_LENGTH == crypto_pwhash_scryptsalsa208sha256_SALTBYTES, "TOX_PASS_SALT_LENGTH is assumed to be equal to crypto_pwhash_scryptsalsa208sha256_SALTBYTES"); static_assert(TOX_PASS_KEY_LENGTH == CRYPTO_SHARED_KEY_SIZE, @@ -36,7 +24,13 @@ static_assert(TOX_PASS_KEY_LENGTH == CRYPTO_SHARED_KEY_SIZE, static_assert(TOX_PASS_ENCRYPTION_EXTRA_LENGTH == (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH), "TOX_PASS_ENCRYPTION_EXTRA_LENGTH is assumed to be equal to (crypto_box_MACBYTES + crypto_box_NONCEBYTES + crypto_pwhash_scryptsalsa208sha256_SALTBYTES + TOX_ENC_SAVE_MAGIC_LENGTH)"); -//!TOKSTYLE+ + +#define SET_ERROR_PARAMETER(param, x) \ + do { \ + if (param) { \ + *param = x; \ + } \ + } while (0) uint32_t tox_pass_salt_length(void) { @@ -142,7 +136,7 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp crypto_memzero(passkey, crypto_hash_sha256_BYTES); /* wipe plaintext pw */ - Tox_Pass_Key *out_key = (Tox_Pass_Key *)malloc(sizeof(Tox_Pass_Key)); + Tox_Pass_Key *out_key = (Tox_Pass_Key *)calloc(1, sizeof(Tox_Pass_Key)); if (!out_key) { SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_FAILED); @@ -156,18 +150,21 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pp } /** - * Encrypt arbitrary with a key produced by `tox_derive_key_*`. The output - * array must be at least data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes long. - * key must be TOX_PASS_KEY_LENGTH bytes. - * If you already have a symmetric key from somewhere besides this module, simply - * call encrypt_data_symmetric in toxcore/crypto_core directly. + * Encrypt a plain text with a key produced by tox_pass_key_derive or tox_pass_key_derive_with_salt. * - * returns true on success + * The output array must be at least `plaintext_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH` + * bytes long. + * + * @param plaintext A byte array of length `plaintext_len`. + * @param plaintext_len The length of the plain text array. Bigger than 0. + * @param ciphertext The cipher text array to write the encrypted data to. + * + * @return true on success. */ -bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t data_len, uint8_t *out, - Tox_Err_Encryption *error) +bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, size_t plaintext_len, + uint8_t *ciphertext, Tox_Err_Encryption *error) { - if (data_len == 0 || !data || !key || !out) { + if (plaintext_len == 0 || !plaintext || !key || !ciphertext) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_NULL); return 0; } @@ -180,21 +177,21 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t d // need them to decrypt the data /* first add the magic number */ - memcpy(out, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH); - out += TOX_ENC_SAVE_MAGIC_LENGTH; + memcpy(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH); + ciphertext += TOX_ENC_SAVE_MAGIC_LENGTH; /* then add the rest prefix */ - memcpy(out, key->salt, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); - out += crypto_pwhash_scryptsalsa208sha256_SALTBYTES; + memcpy(ciphertext, key->salt, crypto_pwhash_scryptsalsa208sha256_SALTBYTES); + ciphertext += crypto_pwhash_scryptsalsa208sha256_SALTBYTES; uint8_t nonce[crypto_box_NONCEBYTES]; random_nonce(nonce); - memcpy(out, nonce, crypto_box_NONCEBYTES); - out += crypto_box_NONCEBYTES; + memcpy(ciphertext, nonce, crypto_box_NONCEBYTES); + ciphertext += crypto_box_NONCEBYTES; /* now encrypt */ - if (encrypt_data_symmetric(key->key, nonce, data, data_len, out) - != data_len + crypto_box_MACBYTES) { + if (encrypt_data_symmetric(key->key, nonce, plaintext, plaintext_len, ciphertext) + != plaintext_len + crypto_box_MACBYTES) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED); return 0; } |