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)
|
||||
{
|
||||
TOX_ERR_NEW err = TOX_ERR_NEW_OK;
|
||||
Tox *tox1 = tox_new(0);
|
||||
Tox *tox2 = tox_new(0);
|
||||
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(tox_is_save_encrypted(data) == 1, "magic number missing");
|
||||
|
||||
Tox *tox3 = tox_new(0);
|
||||
test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25);
|
||||
ck_assert_msg(test == 0, "failed to encrypted load");
|
||||
Tox *tox3 = tox_encrypted_new(0, data, size, "correcthorsebatterystaple", 25, &err);
|
||||
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new");
|
||||
uint8_t address2[TOX_CLIENT_ID_SIZE];
|
||||
test = tox_get_client_id(tox3, 0, address2);
|
||||
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(tox_is_save_encrypted(data2) == 1, "magic number the second missing");
|
||||
|
||||
// first test tox_encrypted_key_load
|
||||
Tox *tox4 = tox_new(0);
|
||||
test = tox_encrypted_key_load(tox4, data2, size, key);
|
||||
ck_assert_msg(test == 0, "failed to encrypted load the second");
|
||||
// first test tox_encrypted_key_new
|
||||
Tox *tox4 = tox_encrypted_key_new(0, data2, size, key, &err);
|
||||
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new the second");
|
||||
uint8_t address4[TOX_CLIENT_ID_SIZE];
|
||||
test = tox_get_client_id(tox4, 0, address4);
|
||||
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");
|
||||
|
||||
// now test compaitibilty with tox_encrypted_load, first manually...
|
||||
// now test compaitibilty with tox_encrypted_new, first manually...
|
||||
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 sz2 = tox_pass_key_decrypt(data2, size, key, out2);
|
||||
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
|
||||
// to remove the manual check now that it's there)
|
||||
Tox *tox5 = tox_new(0);
|
||||
test = tox_encrypted_load(tox5, data2, size, pw, pwlen);
|
||||
ck_assert_msg(test == 0, "failed to encrypted load the third");
|
||||
Tox *tox5 = tox_encrypted_new(0, data2, size, pw, pwlen, &err);
|
||||
ck_assert_msg(err == TOX_ERR_NEW_OK, "failed to encrypted new the third");
|
||||
uint8_t address5[TOX_CLIENT_ID_SIZE];
|
||||
test = tox_get_client_id(tox4, 0, address5);
|
||||
ck_assert_msg(test == 0, "no friends! the third");
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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,44 @@ 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 +346,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);
|
||||
}
|
||||
|
|
|
@ -29,10 +29,13 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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,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);
|
||||
|
||||
/* 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 +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.
|
||||
* 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 +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);
|
||||
|
||||
/* 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 +199,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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user