Skip to content

Commit 226b23b

Browse files
committed
cleanup: Add explicit array sizes to toxencryptsave.
Also mark arrays as `[]` instead of `*`.
1 parent ef33cb4 commit 226b23b

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

toxencryptsave/toxencryptsave.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ void tox_pass_key_free(Tox_Pass_Key *key)
7979
*
8080
* @return true on success.
8181
*/
82-
bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *error)
82+
bool tox_get_salt(
83+
const uint8_t ciphertext[TOX_PASS_ENCRYPTION_EXTRA_LENGTH],
84+
uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Get_Salt *error)
8385
{
8486
if (ciphertext == nullptr || salt == nullptr) {
8587
SET_ERROR_PARAMETER(error, TOX_ERR_GET_SALT_NULL);
@@ -112,8 +114,9 @@ bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *er
112114
*
113115
* @return new symmetric key on success, NULL on failure.
114116
*/
115-
Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_len,
116-
Tox_Err_Key_Derivation *error)
117+
Tox_Pass_Key *tox_pass_key_derive(
118+
const uint8_t passphrase[], size_t passphrase_len,
119+
Tox_Err_Key_Derivation *error)
117120
{
118121
const Random *rng = system_random();
119122

@@ -136,8 +139,9 @@ Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_l
136139
*
137140
* @return new symmetric key on success, NULL on failure.
138141
*/
139-
Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t passphrase_len,
140-
const uint8_t *salt, Tox_Err_Key_Derivation *error)
142+
Tox_Pass_Key *tox_pass_key_derive_with_salt(
143+
const uint8_t passphrase[], size_t passphrase_len,
144+
const uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Key_Derivation *error)
141145
{
142146
if (salt == nullptr || (passphrase == nullptr && passphrase_len != 0)) {
143147
SET_ERROR_PARAMETER(error, TOX_ERR_KEY_DERIVATION_NULL);
@@ -189,8 +193,8 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pa
189193
*
190194
* @return true on success.
191195
*/
192-
bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, size_t plaintext_len,
193-
uint8_t *ciphertext, Tox_Err_Encryption *error)
196+
bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], size_t plaintext_len,
197+
uint8_t ciphertext[], Tox_Err_Encryption *error)
194198
{
195199
const Random *rng = system_random();
196200

@@ -250,8 +254,8 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz
250254
*
251255
* @return true on success.
252256
*/
253-
bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint8_t *passphrase, size_t passphrase_len,
254-
uint8_t *ciphertext, Tox_Err_Encryption *error)
257+
bool tox_pass_encrypt(const uint8_t plaintext[], size_t plaintext_len, const uint8_t passphrase[], size_t passphrase_len,
258+
uint8_t ciphertext[], Tox_Err_Encryption *error)
255259
{
256260
Tox_Err_Key_Derivation err;
257261
Tox_Pass_Key *key = tox_pass_key_derive(passphrase, passphrase_len, &err);
@@ -281,8 +285,8 @@ bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint
281285
*
282286
* @return true on success.
283287
*/
284-
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, size_t ciphertext_len,
285-
uint8_t *plaintext, Tox_Err_Decryption *error)
288+
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], size_t ciphertext_len,
289+
uint8_t plaintext[], Tox_Err_Decryption *error)
286290
{
287291
if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
288292
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
@@ -333,8 +337,8 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, si
333337
*
334338
* @return true on success.
335339
*/
336-
bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const uint8_t *passphrase,
337-
size_t passphrase_len, uint8_t *plaintext, Tox_Err_Decryption *error)
340+
bool tox_pass_decrypt(const uint8_t ciphertext[], size_t ciphertext_len, const uint8_t passphrase[],
341+
size_t passphrase_len, uint8_t plaintext[], Tox_Err_Decryption *error)
338342
{
339343
if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
340344
SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH);
@@ -382,7 +386,7 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui
382386
*
383387
* @return true if the data is encrypted by this module.
384388
*/
385-
bool tox_is_data_encrypted(const uint8_t *data)
389+
bool tox_is_data_encrypted(const uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH])
386390
{
387391
return memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0;
388392
}

toxencryptsave/toxencryptsave.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ typedef enum Tox_Err_Decryption {
186186
*
187187
* @return true on success.
188188
*/
189-
bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint8_t *passphrase, size_t passphrase_len,
190-
uint8_t *ciphertext, Tox_Err_Encryption *error);
189+
bool tox_pass_encrypt(const uint8_t plaintext[], size_t plaintext_len, const uint8_t passphrase[], size_t passphrase_len,
190+
uint8_t ciphertext[], Tox_Err_Encryption *error);
191191

192192
/**
193193
* Decrypts the given data with the given passphrase.
@@ -203,8 +203,8 @@ bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint
203203
*
204204
* @return true on success.
205205
*/
206-
bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const uint8_t *passphrase,
207-
size_t passphrase_len, uint8_t *plaintext, Tox_Err_Decryption *error);
206+
bool tox_pass_decrypt(const uint8_t ciphertext[], size_t ciphertext_len, const uint8_t passphrase[],
207+
size_t passphrase_len, uint8_t plaintext[], Tox_Err_Decryption *error);
208208

209209

210210
/*******************************************************************************
@@ -255,7 +255,8 @@ void tox_pass_key_free(Tox_Pass_Key *key);
255255
*
256256
* @return new symmetric key on success, NULL on failure.
257257
*/
258-
Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_len,
258+
Tox_Pass_Key *tox_pass_key_derive(
259+
const uint8_t passphrase[], size_t passphrase_len,
259260
Tox_Err_Key_Derivation *error);
260261

261262
/**
@@ -267,8 +268,9 @@ Tox_Pass_Key *tox_pass_key_derive(const uint8_t *passphrase, size_t passphrase_l
267268
*
268269
* @return new symmetric key on success, NULL on failure.
269270
*/
270-
Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t passphrase_len,
271-
const uint8_t *salt, Tox_Err_Key_Derivation *error);
271+
Tox_Pass_Key *tox_pass_key_derive_with_salt(
272+
const uint8_t passphrase[], size_t passphrase_len,
273+
const uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Key_Derivation *error);
272274

273275
/**
274276
* Encrypt a plain text with a key produced by tox_pass_key_derive or tox_pass_key_derive_with_salt.
@@ -282,8 +284,8 @@ Tox_Pass_Key *tox_pass_key_derive_with_salt(const uint8_t *passphrase, size_t pa
282284
*
283285
* @return true on success.
284286
*/
285-
bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, size_t plaintext_len,
286-
uint8_t *ciphertext, Tox_Err_Encryption *error);
287+
bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], size_t plaintext_len,
288+
uint8_t ciphertext[], Tox_Err_Encryption *error);
287289

288290
/**
289291
* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
@@ -295,8 +297,8 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz
295297
*
296298
* @return true on success.
297299
*/
298-
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, size_t ciphertext_len,
299-
uint8_t *plaintext, Tox_Err_Decryption *error);
300+
bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], size_t ciphertext_len,
301+
uint8_t plaintext[], Tox_Err_Decryption *error);
300302

301303
typedef enum Tox_Err_Get_Salt {
302304

@@ -338,7 +340,9 @@ typedef enum Tox_Err_Get_Salt {
338340
*
339341
* @return true on success.
340342
*/
341-
bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *error);
343+
bool tox_get_salt(
344+
const uint8_t ciphertext[TOX_PASS_ENCRYPTION_EXTRA_LENGTH],
345+
uint8_t salt[TOX_PASS_SALT_LENGTH], Tox_Err_Get_Salt *error);
342346

343347
/**
344348
* Determines whether or not the given data is encrypted by this module.
@@ -354,7 +358,7 @@ bool tox_get_salt(const uint8_t *ciphertext, uint8_t *salt, Tox_Err_Get_Salt *er
354358
*
355359
* @return true if the data is encrypted by this module.
356360
*/
357-
bool tox_is_data_encrypted(const uint8_t *data);
361+
bool tox_is_data_encrypted(const uint8_t data[TOX_PASS_ENCRYPTION_EXTRA_LENGTH]);
358362

359363

360364
#ifdef __cplusplus

0 commit comments

Comments
 (0)