From ca5564f643723f6ce25fc2de13b2b710ee58df50 Mon Sep 17 00:00:00 2001 From: Mathias Tausig Date: Thu, 12 May 2016 09:26:13 +0200 Subject: [PATCH] Cleanup: Corrected code style with uncrustify --- sys/hashes/md5.c | 60 ++++++++++++++++++++-------------------- sys/hashes/sha1.c | 2 +- sys/hashes/sha256.c | 20 ++++++++------ sys/include/hashes/md5.h | 3 +- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/sys/hashes/md5.c b/sys/hashes/md5.c index c9f4f148c..869d9fee5 100644 --- a/sys/hashes/md5.c +++ b/sys/hashes/md5.c @@ -97,10 +97,10 @@ static const uint32_t T[4][16] = { * All of these operations are bitwise, and so not impacted by endian-ness. * @{ */ -#define md5F( X, Y, Z ) ( ((X) & (Y)) | ((~(X)) & (Z)) ) -#define md5G( X, Y, Z ) ( ((X) & (Z)) | ((Y) & (~(Z))) ) -#define md5H( X, Y, Z ) ( (X) ^ (Y) ^ (Z) ) -#define md5I( X, Y, Z ) ( (Y) ^ ((X) | (~(Z))) ) +#define md5F( X, Y, Z ) (((X) &(Y)) | ((~(X)) & (Z))) +#define md5G( X, Y, Z ) (((X) &(Z)) | ((Y) &(~(Z)))) +#define md5H( X, Y, Z ) ((X) ^ (Y) ^ (Z)) +#define md5I( X, Y, Z ) ((Y) ^ ((X) | (~(Z)))) /** @} */ /** @@ -109,7 +109,7 @@ static const uint32_t T[4][16] = { * A value of 0 for indicates the lowest order byte, while 3 indicates * the highest order byte. */ -#define GETBYTE(L, idx) ((uint8_t)(( L >> (((idx) & 0x03) << 3) ) & 0xFF)) +#define GETBYTE(L, idx) ((uint8_t)((L >> (((idx) & 0x03) << 3)) & 0xFF)) /** * @brief Permute the ABCD "registers" using the 64-byte as a driver @@ -144,14 +144,14 @@ static void permute(uint32_t abcd[4], const uint8_t block[64] ) uint32_t x[16]; /* Store the current ABCD values for later re-use */ - for(int i = 0; i < 4; i++ ) { + for (int i = 0; i < 4; i++) { keep_abcd[i] = abcd[i]; } /* Convert the input block into an array of unsigned longs, taking care * to read the block in Little Endian order (the algorithm assumes this). * The uint32_t values are then handled in host order. */ - for(int i = 0, j = 0; i < 16; i++ ) { + for (int i = 0, j = 0; i < 16; i++) { x[i] = (uint32_t)block[j++]; x[i] |= ((uint32_t)block[j++] << 8); x[i] |= ((uint32_t)block[j++] << 16); @@ -171,23 +171,23 @@ static void permute(uint32_t abcd[4], const uint8_t block[64] ) * * (My implementation appears to be a poor compromise between speed, size, * and clarity. Ugh. [crh]) */ - for(int round = 0; round < 4; round++) { - for(int i = 0; i < 16; i++) { + for (int round = 0; round < 4; round++) { + for (int i = 0; i < 16; i++) { /* handles the rotation of ABCD */ int j = (4 - (i % 4)) & 0x3; /* is the bit shift for this iteration */ - s = S[round][i%4]; + s = S[round][i % 4]; /* Copy the b,c,d values per ABCD rotation. This isn't really * necessary, it just looks clean & will hopefully be optimized * away. */ - b = abcd[(j+1) & 0x3]; - c = abcd[(j+2) & 0x3]; - d = abcd[(j+3) & 0x3]; + b = abcd[(j + 1) & 0x3]; + c = abcd[(j + 2) & 0x3]; + d = abcd[(j + 3) & 0x3]; /* The actual perumation function. * This is broken out to minimize the code within the switch(). */ - switch( round ) { + switch (round) { case 0: /* round 1 */ a = md5F( b, c, d ) + x[i]; break; @@ -201,14 +201,14 @@ static void permute(uint32_t abcd[4], const uint8_t block[64] ) a = md5I( b, c, d ) + x[ K[2][i] ]; break; } - a = 0xFFFFFFFF & ( abcd[j] + a + T[round][i] ); - abcd[j] = b + (0xFFFFFFFF & (( a << s ) | ( a >> (32 - s) ))); - } + a = 0xFFFFFFFF & (abcd[j] + a + T[round][i]); + abcd[j] = b + (0xFFFFFFFF & ((a << s) | (a >> (32 - s)))); + } } /* Use the stored original A, B, C, D values to perform * one last convolution. */ - for(int i = 0; i < 4; i++) { + for (int i = 0; i < 4; i++) { abcd[i] = (abcd[i] + keep_abcd[i]); } } @@ -239,10 +239,10 @@ void md5_update(md5_ctx_t *ctx, const uint8_t *data, size_t len) /* Copy the new block's data into the context block. * Call the permute() function whenever the context block is full. */ - for(size_t i = 0; i < len; i++) { + for (size_t i = 0; i < len; i++) { ctx->block[ctx->b_used] = data[i]; (ctx->b_used)++; - if(64 == ctx->b_used) { + if (64 == ctx->b_used) { permute(ctx->abcd, ctx->block); ctx->b_used = 0; } @@ -263,15 +263,15 @@ void md5_final(md5_ctx_t *ctx, uint8_t *dst) (ctx->b_used)++; /* Zero out any remaining free bytes in the context block. */ - for(int i = ctx->b_used; i < 64; i++) { + for (int i = ctx->b_used; i < 64; i++) { ctx->block[i] = 0; } /* We need 8 bytes to store the length field. * If we don't have 8, call permute() and reset the context block. */ - if(56 < ctx->b_used) { + if (56 < ctx->b_used) { permute(ctx->abcd, ctx->block); - for(int i = 0; i < 64; i++) { + for (int i = 0; i < 64; i++) { ctx->block[i] = 0; } } @@ -281,18 +281,18 @@ void md5_final(md5_ctx_t *ctx, uint8_t *dst) * and shifted to the correct position. This neatly avoids * any MAXINT numeric overflow issues. */ l = ctx->len << 3; - for(int i = 0; i < 4; i++) { - ctx->block[56+i] |= GETBYTE(l, i); + for (int i = 0; i < 4; i++) { + ctx->block[56 + i] |= GETBYTE(l, i); } ctx->block[60] = ((GETBYTE(ctx->len, 3) & 0xE0) >> 5); /* See Above! */ permute(ctx->abcd, ctx->block); /* Now copy the result into the output buffer and we're done */ - for(int i = 0; i < 4; i++) { - dst[ 0+i] = GETBYTE(ctx->abcd[0], i); - dst[ 4+i] = GETBYTE(ctx->abcd[1], i); - dst[ 8+i] = GETBYTE(ctx->abcd[2], i); - dst[12+i] = GETBYTE(ctx->abcd[3], i); + for (int i = 0; i < 4; i++) { + dst[ 0 + i] = GETBYTE(ctx->abcd[0], i); + dst[ 4 + i] = GETBYTE(ctx->abcd[1], i); + dst[ 8 + i] = GETBYTE(ctx->abcd[2], i); + dst[12 + i] = GETBYTE(ctx->abcd[3], i); } } diff --git a/sys/hashes/sha1.c b/sys/hashes/sha1.c index 9778d0acc..2b0d72604 100644 --- a/sys/hashes/sha1.c +++ b/sys/hashes/sha1.c @@ -180,7 +180,7 @@ void sha1_init_hmac(sha1_context *ctx, const uint8_t *key, size_t key_length) if (key_length > SHA1_BLOCK_LENGTH) { /* Hash long keys */ sha1_init(ctx); - while(key_length--) { + while (key_length--) { sha1_update_byte(ctx, *key++); } sha1_final(ctx, ctx->key_buffer); diff --git a/sys/hashes/sha256.c b/sys/hashes/sha256.c index 2034a0a3e..5317dfe06 100644 --- a/sys/hashes/sha256.c +++ b/sys/hashes/sha256.c @@ -66,6 +66,7 @@ static void be32enc_vect(void *dst_, const void *src_, size_t len) { uint32_t *dst = dst_; const uint32_t *src = src_; + for (size_t i = 0; i < len / 4; i++) { dst[i] = __builtin_bswap32(src[i]); } @@ -161,6 +162,7 @@ static void sha256_pad(sha256_context_t *ctx) * than later because the length will change after we pad. */ unsigned char len[8]; + be32enc_vect(len, ctx->count, 8); /* Add 1--64 bytes so that the resulting length is 56 mod 64 */ @@ -270,13 +272,14 @@ const unsigned char *hmac_sha256(const unsigned char *key, unsigned char *result) { unsigned char k[SHA256_INTERNAL_BLOCK_SIZE]; + memset((void *)k, 0x00, SHA256_INTERNAL_BLOCK_SIZE); if (key_length > SHA256_INTERNAL_BLOCK_SIZE) { sha256(key, key_length, k); } else { - memcpy((void*)k, key, key_length); + memcpy((void *)k, key, key_length); } /* @@ -288,8 +291,8 @@ const unsigned char *hmac_sha256(const unsigned char *key, unsigned char i_key_pad[SHA256_INTERNAL_BLOCK_SIZE]; for (size_t i = 0; i < SHA256_INTERNAL_BLOCK_SIZE; ++i) { - o_key_pad[i] = 0x5c^k[i]; - i_key_pad[i] = 0x36^k[i]; + o_key_pad[i] = 0x5c ^ k[i]; + i_key_pad[i] = 0x36 ^ k[i]; } /* @@ -331,6 +334,7 @@ const unsigned char *hmac_sha256(const unsigned char *key, static inline void sha256_inplace(unsigned char element[SHA256_DIGEST_LENGTH]) { sha256_context_t ctx; + sha256_init(&ctx); sha256_update(&ctx, element, SHA256_DIGEST_LENGTH); sha256_final(&ctx, element); @@ -402,7 +406,7 @@ unsigned char *sha256_chain_with_waypoints(const unsigned char *seed, /* 1st waypoint iteration */ sha256(seed, seed_length, tmp_element); for (size_t i = 1; i < waypoint_streak; ++i) { - sha256_inplace(tmp_element); + sha256_inplace(tmp_element); } memcpy(waypoints[0].element, tmp_element, SHA256_DIGEST_LENGTH); waypoints[0].index = (waypoint_streak - 1); @@ -437,9 +441,9 @@ unsigned char *sha256_chain_with_waypoints(const unsigned char *seed, } int sha256_chain_verify_element(unsigned char *element, - size_t element_index, - unsigned char *tail_element, - size_t chain_length) + size_t element_index, + unsigned char *tail_element, + size_t chain_length) { unsigned char tmp_element[SHA256_DIGEST_LENGTH]; @@ -448,7 +452,7 @@ int sha256_chain_verify_element(unsigned char *element, /* assert if we have an index mismatch */ assert(delta_count >= 1); - memcpy((void*)tmp_element, (void*)element, SHA256_DIGEST_LENGTH); + memcpy((void *)tmp_element, (void *)element, SHA256_DIGEST_LENGTH); /* perform all consecutive iterations down to tail_element */ for (int i = 0; i < (delta_count - 1); ++i) { diff --git a/sys/include/hashes/md5.h b/sys/include/hashes/md5.h index 43c610ff8..922f4d90c 100644 --- a/sys/include/hashes/md5.h +++ b/sys/include/hashes/md5.h @@ -69,8 +69,7 @@ extern "C" { /** * @brief MD5 calculation context */ -typedef struct -{ +typedef struct { uint32_t len; /**< overall number of bytes processed */ uint32_t abcd[4]; /**< virtual registers for hash calculation */ int b_used; /**< number of bytes used in the current block */