mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Format crypto_core.c.
Changes: * 100 columns maximum (not strict, can be a bit more sometimes). * No space after cast.
This commit is contained in:
parent
473cde24d8
commit
acc19a202f
|
@ -50,6 +50,7 @@ if grep '<unresolved>' */*.h; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CC_SOURCES=`find . '(' -name '*.cc' ')'`
|
CC_SOURCES=`find . '(' -name '*.cc' ')'`
|
||||||
|
CC_SOURCES="$CC_SOURCES toxcore/crypto_core.c"
|
||||||
CC_SOURCES="$CC_SOURCES toxcore/ping_array.c"
|
CC_SOURCES="$CC_SOURCES toxcore/ping_array.c"
|
||||||
|
|
||||||
for bin in clang-format-6.0 clang-format-5.0 clang-format; do
|
for bin in clang-format-6.0 clang-format-5.0 clang-format; do
|
||||||
|
|
|
@ -129,13 +129,14 @@ bool public_key_valid(const uint8_t *public_key)
|
||||||
* encrypt/decrypt operation.
|
* encrypt/decrypt operation.
|
||||||
* shared_key has to be crypto_box_BEFORENMBYTES bytes long.
|
* shared_key has to be crypto_box_BEFORENMBYTES bytes long.
|
||||||
*/
|
*/
|
||||||
int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, uint8_t *shared_key)
|
int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key,
|
||||||
|
uint8_t *shared_key)
|
||||||
{
|
{
|
||||||
return crypto_box_beforenm(shared_key, public_key, 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, const uint8_t *plain, size_t length,
|
int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
|
||||||
uint8_t *encrypted)
|
const uint8_t *plain, size_t length, uint8_t *encrypted)
|
||||||
{
|
{
|
||||||
if (length == 0 || !secret_key || !nonce || !plain || !encrypted) {
|
if (length == 0 || !secret_key || !nonce || !plain || !encrypted) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -145,19 +146,21 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
|
||||||
VLA(uint8_t, temp_encrypted, length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES);
|
VLA(uint8_t, temp_encrypted, length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES);
|
||||||
|
|
||||||
memset(temp_plain, 0, crypto_box_ZEROBYTES);
|
memset(temp_plain, 0, crypto_box_ZEROBYTES);
|
||||||
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes.
|
// Pad the message with 32 0 bytes.
|
||||||
|
memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);
|
||||||
|
|
||||||
if (crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, secret_key) != 0) {
|
if (crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce,
|
||||||
|
secret_key) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unpad the encrypted message. */
|
// Unpad the encrypted message.
|
||||||
memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES);
|
memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES);
|
||||||
return length + crypto_box_MACBYTES;
|
return length + crypto_box_MACBYTES;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *encrypted, size_t length,
|
int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
|
||||||
uint8_t *plain)
|
const uint8_t *encrypted, size_t length, uint8_t *plain)
|
||||||
{
|
{
|
||||||
if (length <= crypto_box_BOXZEROBYTES || !secret_key || !nonce || !encrypted || !plain) {
|
if (length <= crypto_box_BOXZEROBYTES || !secret_key || !nonce || !encrypted || !plain) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -167,9 +170,11 @@ int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
|
||||||
VLA(uint8_t, temp_encrypted, length + crypto_box_BOXZEROBYTES);
|
VLA(uint8_t, temp_encrypted, length + crypto_box_BOXZEROBYTES);
|
||||||
|
|
||||||
memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES);
|
memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES);
|
||||||
memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes.
|
// Pad the message with 16 0 bytes.
|
||||||
|
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) {
|
if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, nonce,
|
||||||
|
secret_key) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,12 +210,13 @@ int32_t decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Increment the given nonce by 1. */
|
/* Increment the given nonce by 1. */
|
||||||
void increment_nonce(uint8_t *nonce)
|
void increment_nonce(uint8_t *nonce)
|
||||||
{
|
{
|
||||||
/* TODO(irungentoo): use increment_nonce_number(nonce, 1) or sodium_increment (change to little endian)
|
/* TODO(irungentoo): use increment_nonce_number(nonce, 1) or
|
||||||
* NOTE don't use breaks inside this loop
|
* sodium_increment (change to little endian).
|
||||||
|
*
|
||||||
|
* NOTE don't use breaks inside this loop.
|
||||||
* In particular, make sure, as far as possible,
|
* In particular, make sure, as far as possible,
|
||||||
* that loop bounds and their potential underflow or overflow
|
* that loop bounds and their potential underflow or overflow
|
||||||
* are independent of user-controlled input (you may have heard of the Heartbleed bug).
|
* are independent of user-controlled input (you may have heard of the Heartbleed bug).
|
||||||
|
@ -219,8 +225,8 @@ void increment_nonce(uint8_t *nonce)
|
||||||
uint_fast16_t carry = 1U;
|
uint_fast16_t carry = 1U;
|
||||||
|
|
||||||
for (; i != 0; --i) {
|
for (; i != 0; --i) {
|
||||||
carry += (uint_fast16_t) nonce[i - 1];
|
carry += (uint_fast16_t)nonce[i - 1];
|
||||||
nonce[i - 1] = (uint8_t) carry;
|
nonce[i - 1] = (uint8_t)carry;
|
||||||
carry >>= 8;
|
carry >>= 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -228,8 +234,7 @@ void increment_nonce(uint8_t *nonce)
|
||||||
static uint32_t host_to_network(uint32_t x)
|
static uint32_t host_to_network(uint32_t x)
|
||||||
{
|
{
|
||||||
#if !defined(BYTE_ORDER) || BYTE_ORDER == LITTLE_ENDIAN
|
#if !defined(BYTE_ORDER) || BYTE_ORDER == LITTLE_ENDIAN
|
||||||
return
|
return ((x >> 24) & 0x000000FF) | // move byte 3 to byte 0
|
||||||
((x >> 24) & 0x000000FF) | // move byte 3 to byte 0
|
|
||||||
((x >> 8) & 0x0000FF00) | // move byte 2 to byte 1
|
((x >> 8) & 0x0000FF00) | // move byte 2 to byte 1
|
||||||
((x << 8) & 0x00FF0000) | // move byte 1 to byte 2
|
((x << 8) & 0x00FF0000) | // move byte 1 to byte 2
|
||||||
((x << 24) & 0xFF000000); // move byte 0 to byte 3
|
((x << 24) & 0xFF000000); // move byte 0 to byte 3
|
||||||
|
@ -247,7 +252,7 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num)
|
||||||
* are independent of user-controlled input (you may have heard of the Heartbleed bug).
|
* are independent of user-controlled input (you may have heard of the Heartbleed bug).
|
||||||
*/
|
*/
|
||||||
const uint32_t big_endian_num = host_to_network(host_order_num);
|
const uint32_t big_endian_num = host_to_network(host_order_num);
|
||||||
const uint8_t *const num_vec = (const uint8_t *) &big_endian_num;
|
const uint8_t *const num_vec = (const uint8_t *)&big_endian_num;
|
||||||
uint8_t num_as_nonce[crypto_box_NONCEBYTES] = {0};
|
uint8_t num_as_nonce[crypto_box_NONCEBYTES] = {0};
|
||||||
num_as_nonce[crypto_box_NONCEBYTES - 4] = num_vec[0];
|
num_as_nonce[crypto_box_NONCEBYTES - 4] = num_vec[0];
|
||||||
num_as_nonce[crypto_box_NONCEBYTES - 3] = num_vec[1];
|
num_as_nonce[crypto_box_NONCEBYTES - 3] = num_vec[1];
|
||||||
|
@ -258,7 +263,7 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num)
|
||||||
uint_fast16_t carry = 0U;
|
uint_fast16_t carry = 0U;
|
||||||
|
|
||||||
for (; i != 0; --i) {
|
for (; i != 0; --i) {
|
||||||
carry += (uint_fast16_t) nonce[i - 1] + (uint_fast16_t) num_as_nonce[i - 1];
|
carry += (uint_fast16_t)nonce[i - 1] + (uint_fast16_t)num_as_nonce[i - 1];
|
||||||
nonce[i - 1] = (uint8_t)carry;
|
nonce[i - 1] = (uint8_t)carry;
|
||||||
carry >>= 8;
|
carry >>= 8;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user