Reformat md32_common.h, part 2.

Manual tweaks and then clang-formatted again.

Change-Id: I809fdb71b2135343e5c1264dd659b464780fc54a
Reviewed-on: https://boringssl-review.googlesource.com/6649
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2015-12-06 21:48:45 -05:00 committed by Adam Langley
parent fea1137e55
commit 78fefbf3bb
4 changed files with 63 additions and 72 deletions

View File

@ -51,11 +51,13 @@
#include <openssl/base.h> #include <openssl/base.h>
#include <assert.h>
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
#define asm __asm__ #define asm __asm__
/* This is a generic 32-bit "collector" for message digest algorithms. It /* This is a generic 32-bit "collector" for message digest algorithms. It
@ -74,9 +76,9 @@ extern "C" {
* *
* typedef struct <name>_state_st { * typedef struct <name>_state_st {
* uint32_t h[<chaining length> / sizeof(uint32_t)]; * uint32_t h[<chaining length> / sizeof(uint32_t)];
* uint32_t Nl,Nh; * uint32_t Nl, Nh;
* uint32_t data[HASH_CBLOCK / sizeof(uint32_t)]; * uint32_t data[HASH_CBLOCK / sizeof(uint32_t)];
* unsigned int num * unsigned num;
* ... * ...
* } <NAME>_CTX; * } <NAME>_CTX;
* *
@ -134,26 +136,22 @@ extern "C" {
#error "HASH_BLOCK_DATA_ORDER must be defined!" #error "HASH_BLOCK_DATA_ORDER must be defined!"
#endif #endif
/* #ifndef HASH_MAKE_STRING
* Engage compiler specific rotate intrinsic function if available. #error "HASH_MAKE_STRING must be defined!"
*/ #endif
#undef ROTATE #undef ROTATE
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define ROTATE(a, n) _lrotl(a, n) #define ROTATE(a, n) _lrotl(a, n)
#elif defined(__ICC) #elif defined(__ICC)
#define ROTATE(a, n) _rotl(a, n) #define ROTATE(a, n) _rotl(a, n)
#elif defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM) #elif defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM)
/*
* Some GNU C inline assembler templates. Note that these are
* rotates by *constant* number of bits! But that's exactly
* what we need here...
* <appro@fy.chalmers.se>
*/
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
/* Note this macro requires |n| be a constant. */
#define ROTATE(a, n) \ #define ROTATE(a, n) \
({ \ ({ \
register uint32_t ret; \ register uint32_t ret; \
asm("roll %1,%0" : "=r"(ret) : "I"(n), "0"((uint32_t)(a)) : "cc"); \ asm("roll %1, %0" : "=r"(ret) : "I"(n), "0"((uint32_t)(a)) : "cc"); \
ret; \ ret; \
}) })
#endif /* OPENSSL_X86 || OPENSSL_X86_64 */ #endif /* OPENSSL_X86 || OPENSSL_X86_64 */
@ -165,15 +163,12 @@ extern "C" {
#if defined(DATA_ORDER_IS_BIG_ENDIAN) #if defined(DATA_ORDER_IS_BIG_ENDIAN)
#ifndef PEDANTIC #if !defined(PEDANTIC) && defined(__GNUC__) && __GNUC__ >= 2 && \
#if defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM) !defined(OPENSSL_NO_ASM)
#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
/* /* The first macro gives a ~30-40% performance improvement in SHA-256 compiled
* This gives ~30-40% performance improvement in SHA-256 compiled * with gcc on P4. This can only be done on x86, where unaligned data fetches
* with gcc [on P4]. Well, first macro to be frank. We can pull * are possible. */
* this trick on x86* platforms only, because these CPUs can fetch
* unaligned data without raising an exception.
*/
#define HOST_c2l(c, l) \ #define HOST_c2l(c, l) \
({ \ ({ \
uint32_t r = *((const uint32_t *)(c)); \ uint32_t r = *((const uint32_t *)(c)); \
@ -189,21 +184,20 @@ extern "C" {
(c) += 4; \ (c) += 4; \
r; \ r; \
}) })
#elif defined(__aarch64__) #elif defined(__aarch64__) && defined(__BYTE_ORDER__)
#if defined(__BYTE_ORDER__)
#if defined(__ORDER_LITTLE_ENDIAN__) && \ #if defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define HOST_c2l(c, l) \ #define HOST_c2l(c, l) \
({ \ ({ \
uint32_t r; \ uint32_t r; \
asm("rev %w0,%w1" : "=r"(r) : "r"(*((const uint32_t *)(c)))); \ asm("rev %w0, %w1" : "=r"(r) : "r"(*((const uint32_t *)(c)))); \
(c) += 4; \ (c) += 4; \
(l) = r; \ (l) = r; \
}) })
#define HOST_l2c(l, c) \ #define HOST_l2c(l, c) \
({ \ ({ \
uint32_t r; \ uint32_t r; \
asm("rev %w0,%w1" : "=r"(r) : "r"((uint32_t)(l))); \ asm("rev %w0, %w1" : "=r"(r) : "r"((uint32_t)(l))); \
*((uint32_t *)(c)) = r; \ *((uint32_t *)(c)) = r; \
(c) += 4; \ (c) += 4; \
r; \ r; \
@ -211,11 +205,9 @@ extern "C" {
#elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ #elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define HOST_c2l(c, l) (void)((l) = *((const uint32_t *)(c)), (c) += 4) #define HOST_c2l(c, l) (void)((l) = *((const uint32_t *)(c)), (c) += 4)
#define HOST_l2c(l, c) (*((uint32_t *)(c)) = (l), (c) += 4, (l)) #define HOST_l2c(l, c) (*((uint32_t *)(c)) = (l), (c) += 4, (l))
#endif #endif /* __aarch64__ && __BYTE_ORDER__ */
#endif #endif /* ARCH */
#endif #endif /* !PEDANTIC && GNUC && !NO_ASM */
#endif
#endif
#ifndef HOST_c2l #ifndef HOST_c2l
#define HOST_c2l(c, l) \ #define HOST_c2l(c, l) \
@ -223,6 +215,7 @@ extern "C" {
l |= (((uint32_t)(*((c)++))) << 16), \ l |= (((uint32_t)(*((c)++))) << 16), \
l |= (((uint32_t)(*((c)++))) << 8), l |= (((uint32_t)(*((c)++))))) l |= (((uint32_t)(*((c)++))) << 8), l |= (((uint32_t)(*((c)++)))))
#endif #endif
#ifndef HOST_l2c #ifndef HOST_l2c
#define HOST_l2c(l, c) \ #define HOST_l2c(l, c) \
(*((c)++) = (uint8_t)(((l) >> 24) & 0xff), \ (*((c)++) = (uint8_t)(((l) >> 24) & 0xff), \
@ -237,7 +230,7 @@ extern "C" {
/* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */ /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
#define HOST_c2l(c, l) (void)((l) = *((const uint32_t *)(c)), (c) += 4) #define HOST_c2l(c, l) (void)((l) = *((const uint32_t *)(c)), (c) += 4)
#define HOST_l2c(l, c) (*((uint32_t *)(c)) = (l), (c) += 4, l) #define HOST_l2c(l, c) (*((uint32_t *)(c)) = (l), (c) += 4, l)
#endif #endif /* OPENSSL_X86 || OPENSSL_X86_64 */
#ifndef HOST_c2l #ifndef HOST_c2l
#define HOST_c2l(c, l) \ #define HOST_c2l(c, l) \
@ -245,6 +238,7 @@ extern "C" {
l |= (((uint32_t)(*((c)++))) << 16), \ l |= (((uint32_t)(*((c)++))) << 16), \
l |= (((uint32_t)(*((c)++))) << 24)) l |= (((uint32_t)(*((c)++))) << 24))
#endif #endif
#ifndef HOST_l2c #ifndef HOST_l2c
#define HOST_l2c(l, c) \ #define HOST_l2c(l, c) \
(*((c)++) = (uint8_t)(((l)) & 0xff), \ (*((c)++) = (uint8_t)(((l)) & 0xff), \
@ -253,28 +247,26 @@ extern "C" {
*((c)++) = (uint8_t)(((l) >> 24) & 0xff), l) *((c)++) = (uint8_t)(((l) >> 24) & 0xff), l)
#endif #endif
#endif #endif /* DATA_ORDER */
int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) { int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
const uint8_t *data = data_; const uint8_t *data = data_;
uint8_t *p;
uint32_t l;
size_t n;
if (len == 0) if (len == 0) {
return 1; return 1;
}
l = (c->Nl + (((uint32_t)len) << 3)) & 0xffffffffUL; uint32_t l = c->Nl + (((uint32_t)len) << 3);
/* 95-05-24 eay Fixed a bug with the overflow handling, thanks to if (l < c->Nl) {
* Wei Dai <weidai@eskimo.com> for pointing it out. */ /* Handle carries. */
if (l < c->Nl) /* overflow */
c->Nh++; c->Nh++;
c->Nh += (uint32_t)(len >> 29); /* might cause compiler warning on 16-bit */ }
c->Nh += (uint32_t)(len >> 29);
c->Nl = l; c->Nl = l;
n = c->num; size_t n = c->num;
if (n != 0) { if (n != 0) {
p = (uint8_t *)c->data; uint8_t *p = (uint8_t *)c->data;
if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
memcpy(p + n, data, HASH_CBLOCK - n); memcpy(p + n, data, HASH_CBLOCK - n);
@ -283,10 +275,11 @@ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
data += n; data += n;
len -= n; len -= n;
c->num = 0; c->num = 0;
memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ /* Keep |c->data| zeroed when unused. */
memset(p, 0, HASH_CBLOCK);
} else { } else {
memcpy(p + n, data, len); memcpy(p + n, data, len);
c->num += (unsigned int)len; c->num += (unsigned)len;
return 1; return 1;
} }
} }
@ -300,8 +293,8 @@ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
} }
if (len != 0) { if (len != 0) {
p = (uint8_t *)c->data; uint8_t *p = (uint8_t *)c->data;
c->num = (unsigned int)len; c->num = (unsigned)len;
memcpy(p, data, len); memcpy(p, data, len);
} }
return 1; return 1;
@ -317,7 +310,10 @@ int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
uint8_t *p = (uint8_t *)c->data; uint8_t *p = (uint8_t *)c->data;
size_t n = c->num; size_t n = c->num;
p[n] = 0x80; /* there is always room for one */ /* |c->data| always has room for at least one byte. A full block would have
* been consumed. */
assert(n < HASH_CBLOCK);
p[n] = 0x80;
n++; n++;
if (n > (HASH_CBLOCK - 8)) { if (n > (HASH_CBLOCK - 8)) {
@ -340,12 +336,7 @@ int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
c->num = 0; c->num = 0;
memset(p, 0, HASH_CBLOCK); memset(p, 0, HASH_CBLOCK);
#ifndef HASH_MAKE_STRING
#error "HASH_MAKE_STRING must be defined!"
#else
HASH_MAKE_STRING(c, md); HASH_MAKE_STRING(c, md);
#endif
return 1; return 1;
} }

View File

@ -91,7 +91,7 @@ struct md4_state_st {
uint32_t h[4]; uint32_t h[4];
uint32_t Nl, Nh; uint32_t Nl, Nh;
uint32_t data[16]; uint32_t data[16];
unsigned int num; unsigned num;
}; };

View File

@ -96,7 +96,7 @@ struct md5_state_st {
uint32_t h[4]; uint32_t h[4];
uint32_t Nl, Nh; uint32_t Nl, Nh;
uint32_t data[16]; uint32_t data[16];
unsigned int num; unsigned num;
}; };

View File

@ -116,7 +116,7 @@ struct sha_state_st {
#endif #endif
uint32_t Nl, Nh; uint32_t Nl, Nh;
uint32_t data[16]; uint32_t data[16];
unsigned int num; unsigned num;
}; };
@ -177,7 +177,7 @@ struct sha256_state_st {
uint32_t h[8]; uint32_t h[8];
uint32_t Nl, Nh; uint32_t Nl, Nh;
uint32_t data[16]; uint32_t data[16];
unsigned int num, md_len; unsigned num, md_len;
}; };
@ -245,7 +245,7 @@ struct sha512_state_st {
uint64_t d[16]; uint64_t d[16];
uint8_t p[128]; uint8_t p[128];
} u; } u;
unsigned int num, md_len; unsigned num, md_len;
}; };