1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 04:32:46 +01:00

sys/crypto: Fix code style

This commit is contained in:
Mathias Tausig 2019-10-08 15:07:28 +02:00
parent d749e2de5d
commit 89db40b563
25 changed files with 278 additions and 222 deletions

View File

@ -34,7 +34,8 @@
#include "byteorder.h"
#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
# error "This code is implementented in a way that it will only work for little-endian systems!"
# error \
"This code is implementented in a way that it will only work for little-endian systems!"
#endif
#include <string.h>
@ -46,9 +47,11 @@ static void _r(uint32_t *d, uint32_t *a, const uint32_t *b, unsigned c)
*d = (tmp << c) | (tmp >> (32 - c));
}
static void _doubleround(void *output_, const uint32_t input[16], uint8_t rounds)
static void _doubleround(void *output_, const uint32_t input[16],
uint8_t rounds)
{
uint32_t *output = (uint32_t *)output_;
memcpy(output, input, 64);
rounds *= 4;
@ -115,6 +118,7 @@ void chacha_keystream_bytes(chacha_ctx *ctx, void *x)
void chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c)
{
uint8_t x[64];
chacha_keystream_bytes(ctx, x);
for (unsigned i = 0; i < 64; ++i) {
c[i] = m[i] ^ x[i];

View File

@ -32,13 +32,15 @@ int cipher_init(cipher_t* cipher, cipher_id_t cipher_id, const uint8_t* key,
}
int cipher_encrypt(const cipher_t* cipher, const uint8_t* input, uint8_t* output)
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
uint8_t *output)
{
return cipher->interface->encrypt(&cipher->context, input, output);
}
int cipher_decrypt(const cipher_t* cipher, const uint8_t* input, uint8_t* output)
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
uint8_t *output)
{
return cipher->interface->decrypt(&cipher->context, input, output);
}

View File

@ -12,6 +12,7 @@
void crypto_block_inc_ctr(uint8_t block[16], int L)
{
uint8_t *b = &block[15];
for (int i = 0; i < L; ++i, --b) {
if (++*b != 0) {
break;
@ -22,6 +23,7 @@ void crypto_block_inc_ctr(uint8_t block[16], int L)
int crypto_equals(const uint8_t *a, const uint8_t *b, size_t len)
{
uint8_t diff = 0;
for (size_t i = 0; i < len; ++i, ++a, ++b) {
diff |= (*a ^ *b);
}
@ -38,6 +40,7 @@ int crypto_equals(const uint8_t *a, const uint8_t *b, size_t len)
void crypto_secure_wipe(void *buf, size_t len)
{
volatile uint8_t *vbuf = (uint8_t *)buf;
for (size_t i = 0; i < len; i++) {
vbuf[i] = 0;
}

View File

@ -117,13 +117,15 @@ int ccm_compute_adata_mac(cipher_t* cipher, const uint8_t* auth_data,
auth_data_encoded[1] = auth_data_len & 0xFF;
auth_data_encoded[0] = (auth_data_len >> 8) & 0xFF;
} else {
}
else {
DEBUG("UNSUPPORTED Adata length: %" PRIu32 "\n", auth_data_len);
return -1;
}
memcpy(auth_data_encoded + len_encoding, auth_data, auth_data_len);
len = ccm_compute_cbc_mac(cipher, X1, auth_data_encoded, auth_data_len + len_encoding, X1);
len = ccm_compute_cbc_mac(cipher, X1, auth_data_encoded,
auth_data_len + len_encoding, X1);
if (len < 0) {
return -1;
}
@ -140,6 +142,7 @@ static inline int _fits_in_nbytes(size_t value, uint8_t num_bytes)
* So we shift by maximum num bits of size_t -1 and compare to 1
*/
unsigned shift = (8 * min(sizeof(size_t), num_bytes)) - 1;
return (value >> shift) <= 1;
}
@ -217,7 +220,8 @@ int cipher_decrypt_ccm(cipher_t* cipher,
{
int len = -1;
uint8_t nonce_counter[16] = { 0 }, mac_iv[16] = { 0 }, mac[16] = { 0 },
mac_recv[16] = {0}, stream_block[16] = {0}, zero_block[16] = {0},
mac_recv[16] = { 0 }, stream_block[16] = { 0 },
zero_block[16] = { 0 },
plain_len, block_size;
if (mac_length % 2 != 0 || mac_length < 4 || mac_length > 16) {
@ -232,7 +236,8 @@ int cipher_decrypt_ccm(cipher_t* cipher,
/* Compute first stream block */
nonce_counter[0] = length_encoding - 1;
block_size = cipher_get_block_size(cipher);
memcpy(&nonce_counter[1], nonce, min(nonce_len, (size_t) 15 - length_encoding));
memcpy(&nonce_counter[1], nonce, min(nonce_len,
(size_t)15 - length_encoding));
len = cipher_encrypt_ctr(cipher, nonce_counter, block_size, zero_block,
block_size, stream_block);
if (len < 0) {

View File

@ -69,14 +69,16 @@ static void calculate_l_i(uint8_t l_zero[16], size_t i, uint8_t output[16])
}
}
static void xor_block(uint8_t block1[16], uint8_t block2[16], uint8_t output[16])
static void xor_block(uint8_t block1[16], uint8_t block2[16],
uint8_t output[16])
{
for (uint8_t i = 0; i < 16; ++i) {
output[i] = block1[i] ^ block2[i];
}
}
static void processBlock(ocb_state_t *state, size_t blockNumber, uint8_t input[16], uint8_t output[16], uint8_t mode)
static void processBlock(ocb_state_t *state, size_t blockNumber,
uint8_t input[16], uint8_t output[16], uint8_t mode)
{
/* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
uint8_t l_i[16];
@ -87,10 +89,12 @@ static void processBlock(ocb_state_t *state, size_t blockNumber, uint8_t input[1
uint8_t cipher_output[16], cipher_input[16];
xor_block(input, state->offset, cipher_input);
if (mode == OCB_MODE_ENCRYPT) {
state->cipher->interface->encrypt(&(state->cipher->context), cipher_input, cipher_output);
state->cipher->interface->encrypt(&(state->cipher->context),
cipher_input, cipher_output);
}
else if (mode == OCB_MODE_DECRYPT) {
state->cipher->interface->decrypt(&(state->cipher->context), cipher_input, cipher_output);
state->cipher->interface->decrypt(&(state->cipher->context),
cipher_input, cipher_output);
}
xor_block(state->offset, cipher_output, output);
/* Checksum_i = Checksum_{i-1} xor P_i */
@ -102,7 +106,8 @@ static void processBlock(ocb_state_t *state, size_t blockNumber, uint8_t input[1
}
}
static void hash(ocb_state_t *state, uint8_t *data, size_t data_len, uint8_t output[16])
static void hash(ocb_state_t *state, uint8_t *data, size_t data_len,
uint8_t output[16])
{
/* Calculate the number of full blocks in data */
size_t m = (data_len - (data_len % 16)) / 16;
@ -121,7 +126,8 @@ static void hash(ocb_state_t *state, uint8_t *data, size_t data_len, uint8_t out
/* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
uint8_t enciphered_block[16], cipher_input[16];
xor_block(data, offset, cipher_input);
state->cipher->interface->encrypt(&(state->cipher->context), cipher_input, enciphered_block);
state->cipher->interface->encrypt(&(state->cipher->context),
cipher_input, enciphered_block);
xor_block(output, enciphered_block, output);
data += 16;
@ -137,12 +143,14 @@ static void hash(ocb_state_t *state, uint8_t *data, size_t data_len, uint8_t out
xor_block(cipher_input, offset, cipher_input);
/* Sum = Sum_m xor ENCIPHER(K, CipherInput) */
uint8_t enciphered_block[16];
state->cipher->interface->encrypt(&(state->cipher->context), cipher_input, enciphered_block);
state->cipher->interface->encrypt(&(state->cipher->context),
cipher_input, enciphered_block);
xor_block(output, enciphered_block, output);
}
}
static void init_ocb(cipher_t *cipher, uint8_t tag_len, uint8_t *nonce, size_t nonce_len, ocb_state_t *state)
static void init_ocb(cipher_t *cipher, uint8_t tag_len, uint8_t *nonce,
size_t nonce_len, ocb_state_t *state)
{
state->cipher = cipher;
@ -186,16 +194,21 @@ static void init_ocb(cipher_t *cipher, uint8_t tag_len, uint8_t *nonce, size_t n
uint8_t offset_start_byte = bottom / 8;
uint8_t offset_start_bit = bottom - offset_start_byte * 8;
for (uint8_t i = 0; i < 16; ++i) {
state->offset[i] = (stretch[offset_start_byte + i] << offset_start_bit) | (stretch[offset_start_byte + i + 1] >> (8 - offset_start_bit));
state->offset[i] =
(stretch[offset_start_byte + i] << offset_start_bit) |
(stretch[offset_start_byte + i + 1] >> (8 - offset_start_bit));
}
/* Checksum_0 = zeros(128) */
memset(state->checksum, 0, 16);
}
static int32_t run_ocb(cipher_t *cipher, uint8_t *auth_data, uint32_t auth_data_len,
uint8_t tag[16], uint8_t tag_len, uint8_t *nonce, size_t nonce_len,
uint8_t *input, size_t input_len, uint8_t *output, uint8_t mode)
static int32_t run_ocb(cipher_t *cipher, uint8_t *auth_data,
uint32_t auth_data_len,
uint8_t tag[16], uint8_t tag_len, uint8_t *nonce,
size_t nonce_len,
uint8_t *input, size_t input_len, uint8_t *output,
uint8_t mode)
{
/* OCB mode only works for ciphers of block length 16 */
@ -275,7 +288,8 @@ static int32_t run_ocb(cipher_t *cipher, uint8_t *auth_data, uint32_t auth_data_
return output_pos;
}
int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_data_len,
int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data,
size_t auth_data_len,
uint8_t tag_len, uint8_t *nonce, size_t nonce_len,
uint8_t *input, size_t input_len, uint8_t *output)
{
@ -288,7 +302,8 @@ int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_dat
int cipher_text_length = run_ocb(cipher, auth_data, auth_data_len,
tag, tag_len, nonce, nonce_len,
input, input_len, output, OCB_MODE_ENCRYPT);
input, input_len, output,
OCB_MODE_ENCRYPT);
if (cipher_text_length < 0) {
// An error occured. Return the error code
@ -299,7 +314,8 @@ int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_dat
return (cipher_text_length + tag_len);
}
int32_t cipher_decrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_data_len,
int32_t cipher_decrypt_ocb(cipher_t *cipher, uint8_t *auth_data,
size_t auth_data_len,
uint8_t tag_len, uint8_t *nonce, size_t nonce_len,
uint8_t *input, size_t input_len, uint8_t *output)
{
@ -312,7 +328,8 @@ int32_t cipher_decrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_dat
uint8_t tag[16];
int plain_text_length = run_ocb(cipher, auth_data, auth_data_len,
tag, tag_len, nonce, nonce_len,
input, input_len - tag_len, output, OCB_MODE_DECRYPT);
input, input_len - tag_len, output,
OCB_MODE_DECRYPT);
if (plain_text_length < 0) {
// An error occured. Retur the error code

View File

@ -96,6 +96,7 @@ static void _take_input(poly1305_ctx_t *ctx, uint8_t input)
{
size_t word = ctx->c_idx >> 2;
size_t byte = ctx->c_idx & 3;
ctx->c[word] |= (uint32_t)input << (byte * 8);
ctx->c_idx++;
}
@ -162,7 +163,8 @@ void poly1305_finish(poly1305_ctx_t *ctx, uint8_t *mac)
}
void poly1305_auth(uint8_t *mac, const uint8_t *data, size_t len, const uint8_t *key)
void poly1305_auth(uint8_t *mac, const uint8_t *data, size_t len,
const uint8_t *key)
{
poly1305_ctx_t ctx;

View File

@ -99,7 +99,8 @@ void chacha_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c);
/**
* @copydoc chacha_encrypt_bytes()
*/
static inline void chacha_decrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c)
static inline void chacha_decrypt_bytes(chacha_ctx *ctx, const uint8_t *m,
uint8_t *c)
{
chacha_encrypt_bytes(ctx, m, c);
}

View File

@ -41,8 +41,7 @@ extern "C" {
/**
* @brief Chacha20poly1305 state struct
*/
typedef union
{
typedef union {
/* We need both the state matrix and the poly1305 state, but nearly not at
* the same time. This works as long as the first 8 members of state
* overlap fully or completely not with the first and second key parts

View File

@ -148,7 +148,8 @@ int cipher_init(cipher_t *cipher, cipher_id_t cipher_id, const uint8_t *key,
* cipher, which is always 1 in case of success
* @return A negative value for an error
*/
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output);
int cipher_encrypt(const cipher_t *cipher, const uint8_t *input,
uint8_t *output);
/**
@ -164,7 +165,8 @@ int cipher_encrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output
* cipher, which is always 1 in case of success
* @return A negative value for an error
*/
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input, uint8_t *output);
int cipher_decrypt(const cipher_t *cipher, const uint8_t *input,
uint8_t *output);
/**

View File

@ -77,7 +77,8 @@ extern "C" {
* It has to be of size data_len + tag_len.
* @return Length of the encrypted data (including the tag) or a (negative) error code
*/
int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_data_len,
int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data,
size_t auth_data_len,
uint8_t tag_len, uint8_t *nonce, size_t nonce_len,
uint8_t *input, size_t input_len, uint8_t *output);
@ -99,7 +100,8 @@ int32_t cipher_encrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_dat
* Will contain only zeroes, if the authentication fails.
* @return Length of the plaintext data or a (negative) error code
*/
int32_t cipher_decrypt_ocb(cipher_t *cipher, uint8_t *auth_data, size_t auth_data_len,
int32_t cipher_decrypt_ocb(cipher_t *cipher, uint8_t *auth_data,
size_t auth_data_len,
uint8_t tag_len, uint8_t *nonce, size_t nonce_len,
uint8_t *input, size_t input_len, uint8_t *output);
#ifdef __cplusplus

View File

@ -51,14 +51,16 @@ static void test_crypto_aes_encrypt(void)
err = aes_encrypt(&ctx, TEST_0_INP, data);
TEST_ASSERT_EQUAL_INT(1, err);
TEST_ASSERT_MESSAGE(1 == compare(TEST_0_ENC, data, AES_BLOCK_SIZE), "wrong ciphertext");
TEST_ASSERT_MESSAGE(1 == compare(TEST_0_ENC, data,
AES_BLOCK_SIZE), "wrong ciphertext");
err = aes_init(&ctx, TEST_1_KEY, sizeof(TEST_1_KEY));
TEST_ASSERT_EQUAL_INT(1, err);
err = aes_encrypt(&ctx, TEST_1_INP, data);
TEST_ASSERT_EQUAL_INT(1, err);
TEST_ASSERT_MESSAGE(1 == compare(TEST_1_ENC, data, AES_BLOCK_SIZE), "wrong ciphertext");
TEST_ASSERT_MESSAGE(1 == compare(TEST_1_ENC, data,
AES_BLOCK_SIZE), "wrong ciphertext");
}
static void test_crypto_aes_decrypt(void)
@ -72,14 +74,16 @@ static void test_crypto_aes_decrypt(void)
err = aes_decrypt(&ctx, TEST_0_ENC, data);
TEST_ASSERT_EQUAL_INT(1, err);
TEST_ASSERT_MESSAGE(1 == compare(TEST_0_INP, data, AES_BLOCK_SIZE), "wrong plaintext");
TEST_ASSERT_MESSAGE(1 == compare(TEST_0_INP, data,
AES_BLOCK_SIZE), "wrong plaintext");
err = aes_init(&ctx, TEST_1_KEY, sizeof(TEST_1_KEY));
TEST_ASSERT_EQUAL_INT(1, err);
err = aes_decrypt(&ctx, TEST_1_ENC, data);
TEST_ASSERT_EQUAL_INT(1, err);
TEST_ASSERT_MESSAGE(1 == compare(TEST_1_INP, data, AES_BLOCK_SIZE), "wrong plaintext");
TEST_ASSERT_MESSAGE(1 == compare(TEST_1_INP, data,
AES_BLOCK_SIZE), "wrong plaintext");
}
static void test_crypto_aes_init_key_length(void)

View File

@ -97,7 +97,8 @@ static const uint8_t TC8_CHACHA20_BLOCK1[64] = {
static void _test_crypto_chacha(unsigned rounds, unsigned keylen,
const uint8_t key[32], const uint8_t iv[8],
const uint32_t after_init[16],
const uint8_t block0[64], const uint8_t block1[64])
const uint8_t block0[64],
const uint8_t block1[64])
{
chacha_ctx ctx;
uint8_t block[64];

View File

@ -78,13 +78,19 @@ static void test_crypto_cipher_init_aes_key_length(void)
uint8_t unsupported_key_3[8];
memset(unsupported_key_3, 0, sizeof(unsupported_key_3));
err = cipher_init(&cipher, CIPHER_AES_128, unsupported_key_1, sizeof(unsupported_key_1));
err =
cipher_init(&cipher, CIPHER_AES_128, unsupported_key_1,
sizeof(unsupported_key_1));
TEST_ASSERT_EQUAL_INT(CIPHER_ERR_INVALID_KEY_SIZE, err);
err = cipher_init(&cipher, CIPHER_AES_128, unsupported_key_2, sizeof(unsupported_key_2));
err =
cipher_init(&cipher, CIPHER_AES_128, unsupported_key_2,
sizeof(unsupported_key_2));
TEST_ASSERT_EQUAL_INT(CIPHER_ERR_INVALID_KEY_SIZE, err);
err = cipher_init(&cipher, CIPHER_AES_128, unsupported_key_3, sizeof(unsupported_key_3));
err =
cipher_init(&cipher, CIPHER_AES_128, unsupported_key_3,
sizeof(unsupported_key_3));
TEST_ASSERT_EQUAL_INT(CIPHER_ERR_INVALID_KEY_SIZE, err);
}

View File

@ -985,7 +985,8 @@ typedef int (*func_ccm_t)(cipher_t*, const uint8_t*, uint32_t,
const uint8_t *, size_t, uint8_t *);
static int _test_ccm_len(func_ccm_t func, uint8_t len_encoding,
const uint8_t *input, size_t input_len, size_t adata_len)
const uint8_t *input, size_t input_len,
size_t adata_len)
{
int ret;
cipher_t cipher;
@ -994,6 +995,7 @@ static int _test_ccm_len(func_ccm_t func, uint8_t len_encoding,
uint8_t key[16] = { 0 };
uint8_t nonce_len = nonce_and_len_encoding_size - len_encoding;
cipher_init(&cipher, CIPHER_AES_128, key, 16);
ret = func(&cipher, NULL, adata_len, mac_length, len_encoding,
@ -1006,6 +1008,7 @@ static int _test_ccm_len(func_ccm_t func, uint8_t len_encoding,
static void test_crypto_modes_ccm_check_len(void)
{
int ret;
/* Just 1 to big to fit */
ret = _test_ccm_len(cipher_encrypt_ccm, 2, NULL, 1 << 16, 0);
TEST_ASSERT_EQUAL_INT(CCM_ERR_INVALID_LENGTH_ENCODING, ret);

View File

@ -104,7 +104,8 @@ static void test_crypto_modes_ecb_encrypt(void)
static void test_crypto_modes_ecb_decrypt(void)
{
test_decrypt_op(TEST_1_KEY, TEST_1_KEY_LEN, TEST_1_CIPHER, TEST_1_CIPHER_LEN,
test_decrypt_op(TEST_1_KEY, TEST_1_KEY_LEN, TEST_1_CIPHER,
TEST_1_CIPHER_LEN,
TEST_1_PLAIN, TEST_1_PLAIN_LEN);
}

View File

@ -414,16 +414,20 @@ static void test_crypto_modes_ocb_bad_parameter_values(void)
cipher_init(&cipher, CIPHER_AES_128, key, 16);
/* tag length must be positive */
int rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 0, nonce, 15, input, sizeof(input), output);
int rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 0, nonce,
15, input, sizeof(input), output);
TEST_ASSERT_EQUAL_INT(OCB_ERR_INVALID_TAG_LENGTH, rv);
/* tag length must be <= 16 */
rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 17, nonce, 15, input, sizeof(input), output);
rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 17, nonce,
15, input, sizeof(input), output);
TEST_ASSERT_EQUAL_INT(OCB_ERR_INVALID_TAG_LENGTH, rv);
/* nonce must not be empty */
rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 16, nonce, 0, input, sizeof(input), output);
rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 16, nonce, 0,
input, sizeof(input), output);
TEST_ASSERT_EQUAL_INT(OCB_ERR_INVALID_NONCE_LENGTH, rv);
/* nonce must be <=15 */
rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 16, nonce, 16, input, sizeof(input), output);
rv = cipher_encrypt_ocb(&cipher, auth_data, sizeof(auth_data), 16, nonce,
16, input, sizeof(input), output);
TEST_ASSERT_EQUAL_INT(OCB_ERR_INVALID_NONCE_LENGTH, rv);
}