mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge branch 'new_api' of https://github.com/dubslow/toxcore into new_api
This commit is contained in:
commit
680c7c2ecd
|
@ -55,6 +55,7 @@ END_TEST
|
||||||
|
|
||||||
START_TEST(test_save_friend)
|
START_TEST(test_save_friend)
|
||||||
{
|
{
|
||||||
|
TOX_ERR_NEW err = TOX_ERR_NEW_OK;
|
||||||
Tox *tox1 = tox_new(0);
|
Tox *tox1 = tox_new(0);
|
||||||
Tox *tox2 = tox_new(0);
|
Tox *tox2 = tox_new(0);
|
||||||
ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
|
ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
|
||||||
|
@ -71,9 +72,8 @@ START_TEST(test_save_friend)
|
||||||
ck_assert_msg(test == 0, "failed to encrypted save");
|
ck_assert_msg(test == 0, "failed to encrypted save");
|
||||||
ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing");
|
ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing");
|
||||||
|
|
||||||
Tox *tox3 = tox_new(0);
|
Tox *tox3 = tox_encrypted_new(0, data, size, "correcthorsebatterystaple", 25, &err);
|
||||||
test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25);
|
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new");
|
||||||
ck_assert_msg(test == 0, "failed to encrypted load");
|
|
||||||
uint8_t address2[TOX_CLIENT_ID_SIZE];
|
uint8_t address2[TOX_CLIENT_ID_SIZE];
|
||||||
test = tox_get_client_id(tox3, 0, address2);
|
test = tox_get_client_id(tox3, 0, address2);
|
||||||
ck_assert_msg(test == 0, "no friends!");
|
ck_assert_msg(test == 0, "no friends!");
|
||||||
|
@ -88,18 +88,17 @@ START_TEST(test_save_friend)
|
||||||
ck_assert_msg(test == 0, "failed to encrypted save the second");
|
ck_assert_msg(test == 0, "failed to encrypted save the second");
|
||||||
ck_assert_msg(tox_is_save_encrypted(data2) == 1, "magic number the second missing");
|
ck_assert_msg(tox_is_save_encrypted(data2) == 1, "magic number the second missing");
|
||||||
|
|
||||||
// first test tox_encrypted_key_load
|
// first test tox_encrypted_key_new
|
||||||
Tox *tox4 = tox_new(0);
|
Tox *tox4 = tox_encrypted_key_new(0, data2, size, key, &err);
|
||||||
test = tox_encrypted_key_load(tox4, data2, size, key);
|
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new the second");
|
||||||
ck_assert_msg(test == 0, "failed to encrypted load the second");
|
|
||||||
uint8_t address4[TOX_CLIENT_ID_SIZE];
|
uint8_t address4[TOX_CLIENT_ID_SIZE];
|
||||||
test = tox_get_client_id(tox4, 0, address4);
|
test = tox_get_client_id(tox4, 0, address4);
|
||||||
ck_assert_msg(test == 0, "no friends! the second");
|
ck_assert_msg(test == 0, "no friends! the second");
|
||||||
ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match! the second");
|
ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match! the second");
|
||||||
|
|
||||||
// now test compaitibilty with tox_encrypted_load, first manually...
|
// now test compaitibilty with tox_encrypted_new, first manually...
|
||||||
uint8_t out1[size], out2[size];
|
uint8_t out1[size], out2[size];
|
||||||
printf("Trying to decrypt from pw:\n");
|
//printf("Trying to decrypt from pw:\n");
|
||||||
uint32_t sz1 = tox_pass_decrypt(data2, size, pw, pwlen, out1);
|
uint32_t sz1 = tox_pass_decrypt(data2, size, pw, pwlen, out1);
|
||||||
uint32_t sz2 = tox_pass_key_decrypt(data2, size, key, out2);
|
uint32_t sz2 = tox_pass_key_decrypt(data2, size, key, out2);
|
||||||
ck_assert_msg(sz1 == sz2, "differing output sizes");
|
ck_assert_msg(sz1 == sz2, "differing output sizes");
|
||||||
|
@ -107,9 +106,8 @@ START_TEST(test_save_friend)
|
||||||
|
|
||||||
// and now with the code in use (I only bothered with manually to debug this, and it seems a waste
|
// and now with the code in use (I only bothered with manually to debug this, and it seems a waste
|
||||||
// to remove the manual check now that it's there)
|
// to remove the manual check now that it's there)
|
||||||
Tox *tox5 = tox_new(0);
|
Tox *tox5 = tox_encrypted_new(0, data2, size, pw, pwlen, &err);
|
||||||
test = tox_encrypted_load(tox5, data2, size, pw, pwlen);
|
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new the third");
|
||||||
ck_assert_msg(test == 0, "failed to encrypted load the third");
|
|
||||||
uint8_t address5[TOX_CLIENT_ID_SIZE];
|
uint8_t address5[TOX_CLIENT_ID_SIZE];
|
||||||
test = tox_get_client_id(tox4, 0, address5);
|
test = tox_get_client_id(tox4, 0, address5);
|
||||||
ck_assert_msg(test == 0, "no friends! the third");
|
ck_assert_msg(test == 0, "no friends! the third");
|
||||||
|
|
|
@ -1003,7 +1003,7 @@ void callback_file_data(Messenger *m, void (*function)(Messenger *m, uint32_t, u
|
||||||
* return 0 on failure
|
* return 0 on failure
|
||||||
*/
|
*/
|
||||||
static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize,
|
static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize,
|
||||||
const uint8_t *filename, uint16_t filename_length)
|
const uint8_t *filename, uint16_t filename_length)
|
||||||
{
|
{
|
||||||
if (friend_not_valid(m, friendnumber))
|
if (friend_not_valid(m, friendnumber))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -443,6 +443,14 @@ enum TOX_ERR_NEW {
|
||||||
* The byte array to be loaded contained an encrypted save.
|
* The byte array to be loaded contained an encrypted save.
|
||||||
*/
|
*/
|
||||||
TOX_ERR_NEW_LOAD_ENCRYPTED,
|
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
|
* 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.
|
* saved by an older version of Tox, or when the data has been corrupted.
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "../toxcore/crypto_core.h"
|
#include "../toxcore/crypto_core.h"
|
||||||
#include "../toxcore/tox.h"
|
#include "../toxcore/tox.h"
|
||||||
|
#define SET_ERROR_PARAMETER(param, x) {if(param) {*param = x;}}
|
||||||
|
|
||||||
#ifdef VANILLA_NACL
|
#ifdef VANILLA_NACL
|
||||||
#include "crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h"
|
#include "crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h"
|
||||||
|
@ -293,38 +294,44 @@ int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase,
|
||||||
return tox_pass_key_decrypt(data, length, key, out);
|
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 NULL on failure; see the documentation in toxcore/tox.h.
|
||||||
* returns -1 on failure
|
|
||||||
*/
|
*/
|
||||||
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;
|
uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
||||||
uint8_t temp_data[decrypt_length];
|
uint8_t temp_data[decrypt_length];
|
||||||
|
|
||||||
if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data)
|
if (tox_pass_decrypt(data, length, passphrase, pplength, temp_data)
|
||||||
!= decrypt_length)
|
!= decrypt_length) {
|
||||||
return -1;
|
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.
|
/* 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 NULL on failure; see the documentation in toxcore/tox.h.
|
||||||
* returns -1 on failure
|
|
||||||
*/
|
*/
|
||||||
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;
|
uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
||||||
uint8_t temp_data[decrypt_length];
|
uint8_t temp_data[decrypt_length];
|
||||||
|
|
||||||
if (tox_pass_key_decrypt(data, length, key, temp_data)
|
if (tox_pass_key_decrypt(data, length, key, temp_data)
|
||||||
!= decrypt_length)
|
!= decrypt_length) {
|
||||||
return -1;
|
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)
|
/* Determines whether or not the given data is encrypted (by checking the magic number)
|
||||||
|
@ -339,8 +346,3 @@ int tox_is_data_encrypted(const uint8_t *data)
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int tox_is_save_encrypted(const uint8_t *data)
|
|
||||||
{
|
|
||||||
return tox_is_data_encrypted(data);
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,10 +29,13 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#ifndef TOX_DEFINED
|
#ifndef TOX_DEFINED
|
||||||
#define TOX_DEFINED
|
#define TOX_DEFINED
|
||||||
typedef struct Tox Tox;
|
typedef struct Tox Tox;
|
||||||
|
struct Tox_Options;
|
||||||
|
typedef uint8_t TOX_ERR_NEW;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// these functions provide access to these defines in toxencryptsave.c, which
|
// 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.
|
/* Save the messenger data encrypted with the given password.
|
||||||
* data must be at least tox_encrypted_size().
|
* 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 0 on success
|
||||||
* returns -1 on failure
|
* returns -1 on failure
|
||||||
*/
|
*/
|
||||||
|
@ -104,12 +110,13 @@ 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);
|
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 NULL on failure; see the documentation in toxcore/tox.h.
|
||||||
* returns -1 on failure
|
|
||||||
*/
|
*/
|
||||||
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 *******************************
|
/******************************* BEGIN PART 1 *******************************
|
||||||
|
@ -161,6 +168,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.
|
/* Save the messenger data encrypted with the given key from tox_derive_key.
|
||||||
* data must be at least tox_encrypted_size().
|
* 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 0 on success
|
||||||
* returns -1 on failure
|
* returns -1 on failure
|
||||||
*/
|
*/
|
||||||
|
@ -175,11 +185,13 @@ 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);
|
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.
|
/* 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 NULL on failure; see the documentation in toxcore/tox.h.
|
||||||
* returns -1 on failure
|
|
||||||
*/
|
*/
|
||||||
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)
|
/* Determines whether or not the given data is encrypted (by checking the magic number)
|
||||||
*
|
*
|
||||||
|
@ -187,7 +199,6 @@ int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8
|
||||||
* returns 0 otherwise
|
* returns 0 otherwise
|
||||||
*/
|
*/
|
||||||
int tox_is_data_encrypted(const uint8_t *data);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user