cleanup: Enable most cppcheck warnings as errors.

Cleaned up some of the warnings it gives. Disabled the ones that are
less useful for us at this time.
This commit is contained in:
iphydf 2022-02-21 13:24:00 +00:00
parent 86a528a37a
commit e7bc611dac
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
18 changed files with 136 additions and 118 deletions

View File

@ -235,7 +235,7 @@ static void autotox_add_friend(AutoTox *autotoxes, uint32_t adding, uint32_t add
static void initialise_friend_graph(Graph_Type graph, uint32_t num_toxes, AutoTox *autotoxes)
{
if (graph == GRAPH_LINEAR) {
printf("toxes #%u-#%u each add adjacent toxes as friends\n", 0, num_toxes - 1);
printf("toxes #%d-#%u each add adjacent toxes as friends\n", 0, num_toxes - 1);
for (uint32_t i = 0; i < num_toxes; ++i) {
for (uint32_t j = i - 1; j != i + 3; j += 2) {
@ -245,7 +245,7 @@ static void initialise_friend_graph(Graph_Type graph, uint32_t num_toxes, AutoTo
}
}
} else if (graph == GRAPH_COMPLETE) {
printf("toxes #%u-#%u add each other as friends\n", 0, num_toxes - 1);
printf("toxes #%d-#%u add each other as friends\n", 0, num_toxes - 1);
for (uint32_t i = 0; i < num_toxes; ++i) {
for (uint32_t j = 0; j < num_toxes; ++j) {

View File

@ -21,17 +21,17 @@ typedef struct AutoTox {
void *state;
} AutoTox;
bool all_connected(AutoTox *toxes, uint32_t tox_count);
bool all_connected(AutoTox *autotoxes, uint32_t tox_count);
bool all_friends_connected(AutoTox *toxes, uint32_t tox_count);
bool all_friends_connected(AutoTox *autotoxes, uint32_t tox_count);
void iterate_all_wait(AutoTox *toxes, uint32_t tox_count, uint32_t wait);
void iterate_all_wait(AutoTox *autotoxes, uint32_t tox_count, uint32_t wait);
void save_autotox(AutoTox *autotox);
void kill_autotox(AutoTox *autotox);
void reload(AutoTox *autotox);
void set_mono_time_callback(AutoTox *tox);
void set_mono_time_callback(AutoTox *autotox);
typedef enum Graph_Type {
GRAPH_COMPLETE = 0,

View File

@ -8,7 +8,6 @@ CPPFLAGS+=("-Iother/bootstrap_daemon/src")
CPPFLAGS+=("-Iother/fun")
CPPFLAGS+=("-Itesting")
CPPFLAGS+=("-Itesting/fuzzing")
CPPFLAGS+=("-Itesting/groupchats")
CPPFLAGS+=("-Itoxcore")
CPPFLAGS+=("-Itoxcore/events")
CPPFLAGS+=("-Itoxav")

View File

@ -6,9 +6,29 @@ SKIP_GTEST=1
set -e
CPPCHECK="--enable=all"
CPPCHECK+=("--inconclusive")
CPPCHECK+=("--error-exitcode=1")
# Used for VLA.
CPPCHECK+=("--suppress=allocaCalled")
# We actually write C code.
CPPCHECK+=("--suppress=cstyleCast")
# Used to enable/disable parts using if with constant condition.
CPPCHECK+=("--suppress=knownConditionTrueFalse")
# Cppcheck does not need standard library headers to get proper results.
CPPCHECK+=("--suppress=missingIncludeSystem")
# Range-for is fine.
CPPCHECK+=("--suppress=useStlAlgorithm")
# TODO(iphydf): Maybe fix?
CPPCHECK+=("--suppress=shadowArgument")
CPPCHECK+=("--suppress=shadowFunction")
CPPCHECK+=("--suppress=signConversion")
# TODO(iphydf): Fixed in the toxav refactor PR.
CPPCHECK+=("--suppress=redundantAssignment")
run() {
echo "Running cppcheck in variant '$*'"
cppcheck amalgamation.cc "${CPPFLAGS[@]}" "$@"
cppcheck "${CPPCHECK[@]}" amalgamation.cc "${CPPFLAGS[@]}" "$@"
}
. other/analysis/variants.sh

View File

@ -43,12 +43,12 @@ run() {
-Wfloat-equal \
-Wformat=2 \
-Wframe-address \
-Wframe-larger-than=133168 \
-Wignored-qualifiers \
-Wframe-larger-than=12400 \
-Wignored-attributes \
-Wignored-qualifiers \
-Winit-self \
-Winline \
-Wlarger-than=133120 \
-Wlarger-than=530000 \
-Wmaybe-uninitialized \
-Wmemset-transposed-args \
-Wmisleading-indentation \

View File

@ -1 +1 @@
dc97c6a50cf2e3d06dc24216194569b06445922a5182362af2124114653d7f71 /usr/local/bin/tox-bootstrapd
e60be1422bb4ba3bf080faca429174afb4804211d5d5fe1ba365ed65f505b361 /usr/local/bin/tox-bootstrapd

View File

@ -60,7 +60,7 @@ static void handle_args(const int argc, const char *const argv[], const char *co
crypto_scalarmult_base(public_key, secret_key);
} else if (argc == 2) {
printf("Error: Secret key must be a %d character hex string.\n", crypto_box_SECRETKEYBYTES * 2);
printf("Error: Secret key must be a %u character hex string.\n", crypto_box_SECRETKEYBYTES * 2);
exit(1);
} else {
print_usage(argv[0], does_what);

View File

@ -638,7 +638,7 @@ bool toxav_call_control(ToxAV *av, uint32_t friend_number, Toxav_Call_Control co
return rc == TOXAV_ERR_CALL_CONTROL_OK;
}
bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate,
bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_rate,
Toxav_Err_Bit_Rate_Set *error)
{
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
@ -649,7 +649,7 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_
goto RETURN;
}
if (audio_bit_rate > 0 && audio_bit_rate_invalid(audio_bit_rate)) {
if (bit_rate > 0 && audio_bit_rate_invalid(bit_rate)) {
rc = TOXAV_ERR_BIT_RATE_SET_INVALID_BIT_RATE;
goto RETURN;
}
@ -663,11 +663,11 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_
goto RETURN;
}
LOGGER_DEBUG(av->m->log, "Setting new audio bitrate to: %d", audio_bit_rate);
LOGGER_DEBUG(av->m->log, "Setting new audio bitrate to: %d", bit_rate);
if (call->audio_bit_rate == audio_bit_rate) {
LOGGER_DEBUG(av->m->log, "Audio bitrate already set to: %d", audio_bit_rate);
} else if (audio_bit_rate == 0) {
if (call->audio_bit_rate == bit_rate) {
LOGGER_DEBUG(av->m->log, "Audio bitrate already set to: %d", bit_rate);
} else if (bit_rate == 0) {
LOGGER_DEBUG(av->m->log, "Turned off audio sending");
if (msi_change_capabilities(call->msi_call, call->msi_call->
@ -694,10 +694,10 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_
goto RETURN;
}
} else {
LOGGER_DEBUG(av->m->log, "Set new audio bit rate %d", audio_bit_rate);
LOGGER_DEBUG(av->m->log, "Set new audio bit rate %d", bit_rate);
}
call->audio_bit_rate = audio_bit_rate;
call->audio_bit_rate = bit_rate;
pthread_mutex_unlock(call->toxav_call_mutex);
}
@ -710,7 +710,7 @@ RETURN:
return rc == TOXAV_ERR_BIT_RATE_SET_OK;
}
bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_bit_rate,
bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_rate,
Toxav_Err_Bit_Rate_Set *error)
{
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
@ -721,7 +721,7 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_
goto RETURN;
}
if (video_bit_rate > 0 && video_bit_rate_invalid(video_bit_rate)) {
if (bit_rate > 0 && video_bit_rate_invalid(bit_rate)) {
rc = TOXAV_ERR_BIT_RATE_SET_INVALID_BIT_RATE;
goto RETURN;
}
@ -735,11 +735,11 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_
goto RETURN;
}
LOGGER_DEBUG(av->m->log, "Setting new video bitrate to: %d", video_bit_rate);
LOGGER_DEBUG(av->m->log, "Setting new video bitrate to: %d", bit_rate);
if (call->video_bit_rate == video_bit_rate) {
LOGGER_DEBUG(av->m->log, "Video bitrate already set to: %d", video_bit_rate);
} else if (video_bit_rate == 0) {
if (call->video_bit_rate == bit_rate) {
LOGGER_DEBUG(av->m->log, "Video bitrate already set to: %d", bit_rate);
} else if (bit_rate == 0) {
LOGGER_DEBUG(av->m->log, "Turned off video sending");
/* Video sending is turned off; notify peer */
@ -766,10 +766,10 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_
goto RETURN;
}
} else {
LOGGER_DEBUG(av->m->log, "Set new video bit rate %d", video_bit_rate);
LOGGER_DEBUG(av->m->log, "Set new video bit rate %d", bit_rate);
}
call->video_bit_rate = video_bit_rate;
call->video_bit_rate = bit_rate;
pthread_mutex_unlock(call->toxav_call_mutex);
}

View File

@ -36,7 +36,7 @@ Broadcast_Info *lan_discovery_init(DHT *dht);
* Clear packet handlers.
*/
non_null()
void lan_discovery_kill(DHT *dht, Broadcast_Info *info);
void lan_discovery_kill(DHT *dht, Broadcast_Info *broadcast);
/**
* Is IP a local ip or not.

View File

@ -1617,15 +1617,12 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdat
*/
static void break_files(const Messenger *m, int32_t friendnumber)
{
Friend *const f = &m->friendlist[friendnumber];
// TODO(irungentoo): Inform the client which file transfers get killed with a callback?
for (uint32_t i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
if (m->friendlist[friendnumber].file_sending[i].status != FILESTATUS_NONE) {
m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_NONE;
}
if (m->friendlist[friendnumber].file_receiving[i].status != FILESTATUS_NONE) {
m->friendlist[friendnumber].file_receiving[i].status = FILESTATUS_NONE;
}
f->file_sending[i].status = FILESTATUS_NONE;
f->file_receiving[i].status = FILESTATUS_NONE;
}
}

View File

@ -14,7 +14,7 @@ non_null() bool bin_unpack_bool(bool *val, const msgpack_object *obj);
non_null() bool bin_unpack_u16(uint16_t *val, const msgpack_object *obj);
non_null() bool bin_unpack_u32(uint32_t *val, const msgpack_object *obj);
non_null() bool bin_unpack_u64(uint64_t *val, const msgpack_object *obj);
non_null() bool bin_unpack_bytes(uint8_t **data, size_t *data_length, const msgpack_object *obj);
non_null() bool bin_unpack_bytes(uint8_t **data_ptr, size_t *data_length_ptr, const msgpack_object *obj);
non_null() bool bin_unpack_bytes_fixed(uint8_t *data, uint32_t data_length, const msgpack_object *obj);
#endif // C_TOXCORE_TOXCORE_BIN_UNPACK_H

View File

@ -190,10 +190,10 @@ int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key,
return crypto_box_beforenm(shared_key, public_key, secret_key);
}
int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
const uint8_t *plain, size_t length, uint8_t *encrypted)
{
if (length == 0 || secret_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr) {
if (length == 0 || shared_key == nullptr || nonce == nullptr || plain == nullptr || encrypted == nullptr) {
return -1;
}
@ -226,7 +226,7 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);
if (crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce,
secret_key) != 0) {
shared_key) != 0) {
crypto_free(temp_plain, size_temp_plain);
crypto_free(temp_encrypted, size_temp_encrypted);
return -1;
@ -241,10 +241,10 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
return length + crypto_box_MACBYTES;
}
int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
const uint8_t *encrypted, size_t length, uint8_t *plain)
{
if (length <= crypto_box_BOXZEROBYTES || secret_key == nullptr || nonce == nullptr || encrypted == nullptr
if (length <= crypto_box_BOXZEROBYTES || shared_key == nullptr || nonce == nullptr || encrypted == nullptr
|| plain == nullptr) {
return -1;
}
@ -276,7 +276,7 @@ int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);
if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, nonce,
secret_key) != 0) {
shared_key) != 0) {
crypto_free(temp_plain, size_temp_plain);
crypto_free(temp_encrypted, size_temp_encrypted);
return -1;
@ -396,11 +396,11 @@ void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length)
crypto_hash_sha512(hash, data, length);
}
void random_bytes(uint8_t *data, size_t length)
void random_bytes(uint8_t *bytes, size_t length)
{
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
fuzz_random_bytes(data, length);
fuzz_random_bytes(bytes, length);
#else
randombytes(data, length);
randombytes(bytes, length);
#endif
}

View File

@ -25,7 +25,7 @@
struct Tox_Event_Friend_Status {
uint32_t friend_number;
Tox_User_Status connection_status;
Tox_User_Status status;
};
non_null()
@ -55,16 +55,16 @@ uint32_t tox_event_friend_status_get_friend_number(const Tox_Event_Friend_Status
}
non_null()
static void tox_event_friend_status_set_connection_status(Tox_Event_Friend_Status *friend_status,
Tox_User_Status connection_status)
static void tox_event_friend_status_set_status(Tox_Event_Friend_Status *friend_status,
Tox_User_Status status)
{
assert(friend_status != nullptr);
friend_status->connection_status = connection_status;
friend_status->status = status;
}
Tox_User_Status tox_event_friend_status_get_connection_status(const Tox_Event_Friend_Status *friend_status)
Tox_User_Status tox_event_friend_status_get_status(const Tox_Event_Friend_Status *friend_status)
{
assert(friend_status != nullptr);
return friend_status->connection_status;
return friend_status->status;
}
non_null()
@ -76,7 +76,7 @@ static void tox_event_friend_status_pack(
bin_pack_u32(mp, TOX_EVENT_FRIEND_STATUS);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_u32(mp, event->connection_status);
bin_pack_u32(mp, event->status);
}
non_null()
@ -90,7 +90,7 @@ static bool tox_event_friend_status_unpack(
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& tox_unpack_user_status(&event->connection_status, &obj->via.array.ptr[1]);
&& tox_unpack_user_status(&event->status, &obj->via.array.ptr[1]);
}
@ -187,7 +187,7 @@ bool tox_events_unpack_friend_status(Tox_Events *events, const msgpack_object *o
*****************************************************/
void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_Status connection_status,
void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_Status status,
void *user_data)
{
Tox_Events_State *state = tox_events_alloc(user_data);
@ -201,5 +201,5 @@ void tox_events_handle_friend_status(Tox *tox, uint32_t friend_number, Tox_User_
}
tox_event_friend_status_set_friend_number(friend_status, friend_number);
tox_event_friend_status_set_connection_status(friend_status, connection_status);
tox_event_friend_status_set_status(friend_status, status);
}

View File

@ -24,8 +24,8 @@
struct Tox_Event_Friend_Status_Message {
uint32_t friend_number;
uint8_t *status_message;
size_t status_message_length;
uint8_t *message;
size_t message_length;
};
non_null()
@ -38,7 +38,7 @@ static void tox_event_friend_status_message_construct(Tox_Event_Friend_Status_Me
non_null()
static void tox_event_friend_status_message_destruct(Tox_Event_Friend_Status_Message *friend_status_message)
{
free(friend_status_message->status_message);
free(friend_status_message->message);
}
non_null()
@ -55,38 +55,38 @@ uint32_t tox_event_friend_status_message_get_friend_number(const Tox_Event_Frien
}
non_null()
static bool tox_event_friend_status_message_set_status_message(Tox_Event_Friend_Status_Message *friend_status_message,
const uint8_t *status_message, size_t status_message_length)
static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_Message *friend_status_message,
const uint8_t *message, size_t message_length)
{
assert(friend_status_message != nullptr);
if (friend_status_message->status_message != nullptr) {
free(friend_status_message->status_message);
friend_status_message->status_message = nullptr;
friend_status_message->status_message_length = 0;
if (friend_status_message->message != nullptr) {
free(friend_status_message->message);
friend_status_message->message = nullptr;
friend_status_message->message_length = 0;
}
friend_status_message->status_message = (uint8_t *)malloc(status_message_length);
friend_status_message->message = (uint8_t *)malloc(message_length);
if (friend_status_message->status_message == nullptr) {
if (friend_status_message->message == nullptr) {
return false;
}
memcpy(friend_status_message->status_message, status_message, status_message_length);
friend_status_message->status_message_length = status_message_length;
memcpy(friend_status_message->message, message, message_length);
friend_status_message->message_length = message_length;
return true;
}
size_t tox_event_friend_status_message_get_status_message_length(const Tox_Event_Friend_Status_Message
size_t tox_event_friend_status_message_get_message_length(const Tox_Event_Friend_Status_Message
*friend_status_message)
{
assert(friend_status_message != nullptr);
return friend_status_message->status_message_length;
return friend_status_message->message_length;
}
const uint8_t *tox_event_friend_status_message_get_status_message(const Tox_Event_Friend_Status_Message
const uint8_t *tox_event_friend_status_message_get_message(const Tox_Event_Friend_Status_Message
*friend_status_message)
{
assert(friend_status_message != nullptr);
return friend_status_message->status_message;
return friend_status_message->message;
}
non_null()
@ -98,7 +98,7 @@ static void tox_event_friend_status_message_pack(
bin_pack_u32(mp, TOX_EVENT_FRIEND_STATUS_MESSAGE);
bin_pack_array(mp, 2);
bin_pack_u32(mp, event->friend_number);
bin_pack_bytes(mp, event->status_message, event->status_message_length);
bin_pack_bytes(mp, event->message, event->message_length);
}
non_null()
@ -112,7 +112,7 @@ static bool tox_event_friend_status_message_unpack(
}
return bin_unpack_u32(&event->friend_number, &obj->via.array.ptr[0])
&& bin_unpack_bytes(&event->status_message, &event->status_message_length, &obj->via.array.ptr[1]);
&& bin_unpack_bytes(&event->message, &event->message_length, &obj->via.array.ptr[1]);
}
@ -210,7 +210,7 @@ bool tox_events_unpack_friend_status_message(Tox_Events *events, const msgpack_o
*****************************************************/
void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *status_message,
void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, const uint8_t *message,
size_t length, void *user_data)
{
Tox_Events_State *state = tox_events_alloc(user_data);
@ -224,5 +224,5 @@ void tox_events_handle_friend_status_message(Tox *tox, uint32_t friend_number, c
}
tox_event_friend_status_message_set_friend_number(friend_status_message, friend_number);
tox_event_friend_status_message_set_status_message(friend_status_message, status_message, length);
tox_event_friend_status_message_set_message(friend_status_message, message, length);
}

View File

@ -122,6 +122,8 @@ typedef struct Crypto_Connection {
uint32_t dht_pk_callback_number;
} Crypto_Connection;
static const Crypto_Connection empty_crypto_connection = {{0}};
struct Net_Crypto {
const Logger *log;
Mono_Time *mono_time;
@ -1856,7 +1858,7 @@ static int create_crypto_connection(Net_Crypto *c)
if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == 0) {
id = c->crypto_connections_length;
++c->crypto_connections_length;
memset(&c->crypto_connections[id], 0, sizeof(Crypto_Connection));
c->crypto_connections[id] = empty_crypto_connection;
}
}

View File

@ -728,7 +728,7 @@ void tox_options_set_log_user_data(struct Tox_Options *options, void *user_data)
bool tox_options_get_experimental_thread_safety(const struct Tox_Options *options);
void tox_options_set_experimental_thread_safety(struct Tox_Options *options, bool thread_safety);
void tox_options_set_experimental_thread_safety(struct Tox_Options *options, bool experimental_thread_safety);
/**
* @brief Initialises a Tox_Options object with the default options.
@ -1652,10 +1652,10 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien
/**
* @param friend_number The friend number of the friend who started or stopped
* typing.
* @param is_typing The result of calling tox_friend_get_typing on the passed
* @param typing The result of calling tox_friend_get_typing on the passed
* friend_number.
*/
typedef void tox_friend_typing_cb(Tox *tox, uint32_t friend_number, bool is_typing, void *user_data);
typedef void tox_friend_typing_cb(Tox *tox, uint32_t friend_number, bool typing, void *user_data);
/**

View File

@ -160,15 +160,15 @@ size_t tox_event_friend_request_get_message_length(
const Tox_Event_Friend_Request *friend_request);
typedef struct Tox_Event_Friend_Status Tox_Event_Friend_Status;
Tox_User_Status tox_event_friend_status_get_connection_status(
Tox_User_Status tox_event_friend_status_get_status(
const Tox_Event_Friend_Status *friend_status);
uint32_t tox_event_friend_status_get_friend_number(
const Tox_Event_Friend_Status *friend_status);
typedef struct Tox_Event_Friend_Status_Message Tox_Event_Friend_Status_Message;
const uint8_t *tox_event_friend_status_message_get_status_message(
const uint8_t *tox_event_friend_status_message_get_message(
const Tox_Event_Friend_Status_Message *friend_status_message);
size_t tox_event_friend_status_message_get_status_message_length(
size_t tox_event_friend_status_message_get_message_length(
const Tox_Event_Friend_Status_Message *friend_status_message);
uint32_t tox_event_friend_status_message_get_friend_number(
const Tox_Event_Friend_Status_Message *friend_status_message);

View File

@ -50,9 +50,9 @@ struct Tox_Pass_Key {
uint8_t key[TOX_PASS_KEY_LENGTH];
};
void tox_pass_key_free(Tox_Pass_Key *pass_key)
void tox_pass_key_free(Tox_Pass_Key *key)
{
free(pass_key);
free(key);
}
/* Clients should consider alerting their users that, unlike plain data, if even one bit
@ -68,20 +68,20 @@ void tox_pass_key_free(Tox_Pass_Key *pass_key)
* success does not say anything about the validity of the data, only that data of
* the appropriate size was copied
*/
bool tox_get_salt(const uint8_t *data, uint8_t *salt, Tox_Err_Get_Salt *error)
bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *error)
{
if (data == nullptr || salt == nullptr) {
if (ciphertext == nullptr || salt == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_GET_SALT_NULL);
return false;
}
if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_GET_SALT_BAD_FORMAT);
return false;
}
data += TOX_ENC_SAVE_MAGIC_LENGTH;
memcpy(salt, data, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
ciphertext += TOX_ENC_SAVE_MAGIC_LENGTH;
memcpy(salt, ciphertext, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
SET_ERROR_PARAMETER(error, TOX_ERR_GET_SALT_OK);
return true;
}
@ -97,27 +97,27 @@ bool tox_get_salt(const uint8_t *data, uint8_t *salt, Tox_Err_Get_Salt *error)
*
* returns true on success
*/
Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t pplength,
Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_len,
Tox_Err_Key_Derivation *error)
{
uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
random_bytes(salt, sizeof(salt));
return tox_pass_key_derive_with_salt(passphrase, pplength, salt, error);
return tox_pass_key_derive_with_salt(passphrase, passphrase_len, salt, error);
}
/* Same as above, except with use the given salt for deterministic key derivation.
* The salt must be TOX_PASS_SALT_LENGTH bytes in length.
*/
Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pplength,
Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t passphrase_len,
const uint8_t *salt, Tox_Err_Key_Derivation *error)
{
if (salt == nullptr || (passphrase == nullptr && pplength != 0)) {
if (salt == nullptr || (passphrase == nullptr && passphrase_len != 0)) {
SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_NULL);
return nullptr;
}
uint8_t passkey[crypto_hash_sha256_BYTES];
crypto_hash_sha256(passkey, passphrase, pplength);
crypto_hash_sha256(passkey, passphrase, passphrase_len);
uint8_t key[CRYPTO_SHARED_KEY_SIZE];
@ -206,11 +206,11 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz
*
* returns true on success
*/
bool tox_pass_encrypt(const uint8_t *data, size_t data_len, const uint8_t *passphrase, size_t pplength, uint8_t *out,
Tox_Err_Encryption *error)
bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint8_t *passphrase, size_t passphrase_len,
uint8_t *ciphertext, Tox_Err_Encryption *error)
{
Tox_Err_Key_Derivation err;
Tox_Pass_Key *key = tox_pass_key_derive(passphrase, pplength, &err);
Tox_Pass_Key *key = tox_pass_key_derive(passphrase, passphrase_len, &err);
if (key == nullptr) {
if (err == TOX_ERR_KEY_DERIVATION_NULL) {
@ -222,7 +222,7 @@ bool tox_pass_encrypt(const uint8_t *data, size_t data_len, const uint8_t *passp
return 0;
}
bool result = tox_pass_key_encrypt(key, data, data_len, out, error);
bool result = tox_pass_key_encrypt(key, plaintext, plaintext_len, ciphertext, error);
tox_pass_key_free(key);
return result;
}
@ -234,7 +234,7 @@ bool tox_pass_encrypt(const uint8_t *data, size_t data_len, const uint8_t *passp
*
* returns true on success
*/
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t length, uint8_t *out,
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, size_t length, uint8_t *plaintext,
Tox_Err_Decryption *error)
{
if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
@ -242,27 +242,27 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t l
return 0;
}
if (data == nullptr || key == nullptr || out == nullptr) {
if (ciphertext == nullptr || key == nullptr || plaintext == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL);
return 0;
}
if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT);
return 0;
}
data += TOX_ENC_SAVE_MAGIC_LENGTH;
data += crypto_pwhash_scryptsalsa208sha256_SALTBYTES; // salt only affects key derivation
ciphertext += TOX_ENC_SAVE_MAGIC_LENGTH;
ciphertext += crypto_pwhash_scryptsalsa208sha256_SALTBYTES; // salt only affects key derivation
size_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t nonce[crypto_box_NONCEBYTES];
memcpy(nonce, data, crypto_box_NONCEBYTES);
data += crypto_box_NONCEBYTES;
memcpy(nonce, ciphertext, crypto_box_NONCEBYTES);
ciphertext += crypto_box_NONCEBYTES;
/* decrypt the data */
if (decrypt_data_symmetric(key->key, nonce, data, decrypt_length + crypto_box_MACBYTES, out)
/* decrypt the ciphertext */
if (decrypt_data_symmetric(key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext)
!= decrypt_length) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED);
return 0;
@ -280,29 +280,29 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *data, size_t l
*
* returns true on success
*/
bool tox_pass_decrypt(const uint8_t *data, size_t length, const uint8_t *passphrase, size_t pplength, uint8_t *out,
Tox_Err_Decryption *error)
bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const uint8_t *passphrase,
size_t passphrase_len, uint8_t *plaintext, Tox_Err_Decryption *error)
{
if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
return 0;
}
if (data == nullptr || passphrase == nullptr || out == nullptr) {
if (ciphertext == nullptr || passphrase == nullptr || plaintext == nullptr) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL);
return 0;
}
if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) {
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT);
return 0;
}
uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
memcpy(salt, data + TOX_ENC_SAVE_MAGIC_LENGTH, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
memcpy(salt, ciphertext + TOX_ENC_SAVE_MAGIC_LENGTH, crypto_pwhash_scryptsalsa208sha256_SALTBYTES);
/* derive the key */
Tox_Pass_Key *key = tox_pass_key_derive_with_salt(passphrase, pplength, salt, nullptr);
Tox_Pass_Key *key = tox_pass_key_derive_with_salt(passphrase, passphrase_len, salt, nullptr);
if (key == nullptr) {
/* out of memory most likely */
@ -310,7 +310,7 @@ bool tox_pass_decrypt(const uint8_t *data, size_t length, const uint8_t *passphr
return 0;
}
bool result = tox_pass_key_decrypt(key, data, length, out, error);
bool result = tox_pass_key_decrypt(key, ciphertext, ciphertext_len, plaintext, error);
tox_pass_key_free(key);
return result;
}