forked from Green-Sky/tomato
Merge commit '596ea37298252ce239cfb6fd9c0f459574bd54e3'
This commit is contained in:
@@ -20,6 +20,8 @@ cc_library(
|
||||
":defines",
|
||||
"//c-toxcore/toxcore:ccompat",
|
||||
"//c-toxcore/toxcore:crypto_core",
|
||||
"//c-toxcore/toxcore:os_memory",
|
||||
"//c-toxcore/toxcore:os_random",
|
||||
"@libsodium",
|
||||
],
|
||||
)
|
||||
@@ -34,6 +36,8 @@ cc_library(
|
||||
deps = [
|
||||
"//c-toxcore/toxcore:ccompat",
|
||||
"//c-toxcore/toxcore:crypto_core",
|
||||
"//c-toxcore/toxcore:os_memory",
|
||||
"//c-toxcore/toxcore:os_random",
|
||||
"@libsodium",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
|
||||
#include "../toxcore/ccompat.h"
|
||||
#include "../toxcore/crypto_core.h"
|
||||
#include "../toxcore/mem.h"
|
||||
#include "../toxcore/os_memory.h"
|
||||
#include "../toxcore/os_random.h"
|
||||
#include "defines.h"
|
||||
|
||||
static_assert(TOX_PASS_SALT_LENGTH == crypto_pwhash_scryptsalsa208sha256_SALTBYTES,
|
||||
@@ -52,6 +55,13 @@ struct Tox_Pass_Key {
|
||||
|
||||
void tox_pass_key_free(Tox_Pass_Key *key)
|
||||
{
|
||||
if (key == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* wipe sensitive state */
|
||||
crypto_memzero(key->salt, TOX_PASS_SALT_LENGTH);
|
||||
crypto_memzero(key->key, TOX_PASS_KEY_LENGTH);
|
||||
free(key);
|
||||
}
|
||||
|
||||
@@ -198,9 +208,10 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(
|
||||
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)
|
||||
{
|
||||
const Memory *mem = os_memory();
|
||||
const Random *rng = os_random();
|
||||
|
||||
if (rng == nullptr) {
|
||||
if (mem == nullptr || rng == nullptr) {
|
||||
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED);
|
||||
return false;
|
||||
}
|
||||
@@ -231,7 +242,7 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], si
|
||||
ciphertext += crypto_box_NONCEBYTES;
|
||||
|
||||
/* now encrypt */
|
||||
const int32_t encrypted_len = encrypt_data_symmetric(os_memory(), key->key, nonce, plaintext, plaintext_len, ciphertext);
|
||||
const int32_t encrypted_len = encrypt_data_symmetric(mem, key->key, nonce, plaintext, plaintext_len, ciphertext);
|
||||
if (encrypted_len < 0 || (size_t)encrypted_len != plaintext_len + crypto_box_MACBYTES) {
|
||||
SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED);
|
||||
return false;
|
||||
@@ -291,6 +302,13 @@ bool tox_pass_encrypt(const uint8_t plaintext[], size_t plaintext_len, const uin
|
||||
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], size_t ciphertext_len,
|
||||
uint8_t plaintext[], Tox_Err_Decryption *error)
|
||||
{
|
||||
const Memory *mem = os_memory();
|
||||
|
||||
if (mem == nullptr) {
|
||||
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
|
||||
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
|
||||
return false;
|
||||
@@ -316,7 +334,7 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], s
|
||||
ciphertext += crypto_box_NONCEBYTES;
|
||||
|
||||
/* decrypt the ciphertext */
|
||||
const int32_t decrypted_len = decrypt_data_symmetric(os_memory(), key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext);
|
||||
const int32_t decrypted_len = decrypt_data_symmetric(mem, key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext);
|
||||
if (decrypted_len < 0 || (size_t)decrypted_len != decrypt_length) {
|
||||
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED);
|
||||
return false;
|
||||
|
||||
@@ -226,10 +226,7 @@ bool tox_pass_decrypt(const uint8_t ciphertext[], size_t ciphertext_len, const u
|
||||
* using tox_pass_key_derive or tox_pass_key_derive_with_salt and must be
|
||||
* deallocated using tox_pass_key_free.
|
||||
*/
|
||||
#ifndef TOX_PASS_KEY_DEFINED
|
||||
#define TOX_PASS_KEY_DEFINED
|
||||
typedef struct Tox_Pass_Key Tox_Pass_Key;
|
||||
#endif /* TOX_PASS_KEY_DEFINED */
|
||||
|
||||
/**
|
||||
* Deallocate a Tox_Pass_Key. This function behaves like `free()`, so NULL is an
|
||||
|
||||
Reference in New Issue
Block a user