diff options
-rw-r--r-- | board/cr50/dcrypto/dcrypto.h | 80 | ||||
-rw-r--r-- | board/cr50/dcrypto/internal.h | 69 |
2 files changed, 76 insertions, 73 deletions
diff --git a/board/cr50/dcrypto/dcrypto.h b/board/cr50/dcrypto/dcrypto.h index 0a8c05a536..561a1df7a9 100644 --- a/board/cr50/dcrypto/dcrypto.h +++ b/board/cr50/dcrypto/dcrypto.h @@ -50,6 +50,10 @@ enum hashing_mode { #define __warn_unused_result __attribute__((warn_unused_result)) #endif +#ifndef __always_inline +#define __always_inline __inline __attribute__((always_inline)) +#endif + /** * SHA1/SHA2, HMAC API */ @@ -401,8 +405,8 @@ const uint8_t *DCRYPTO_SHA1_hash(const void *data, size_t n, uint8_t *digest); * @param data input data * @param len length of data */ -static inline void HASH_update(union hash_ctx *const ctx, const void *data, - size_t len) +__always_inline void HASH_update(union hash_ctx *const ctx, const void *data, + size_t len) { ctx->f->update(ctx, data, len); } @@ -414,7 +418,7 @@ static inline void HASH_update(union hash_ctx *const ctx, const void *data, * * @return pointer to computed digest inside ctx. */ -static inline const union sha_digests *HASH_final(union hash_ctx *const ctx) +__always_inline const union sha_digests *HASH_final(union hash_ctx *const ctx) { return ctx->f->final(ctx); } @@ -426,8 +430,8 @@ static inline const union sha_digests *HASH_final(union hash_ctx *const ctx) * @param data input data * @param len length of data */ -static inline void SHA256_update(struct sha256_ctx *const ctx, const void *data, - size_t len) +__always_inline void SHA256_update(struct sha256_ctx *const ctx, + const void *data, size_t len) { ctx->f->update((union hash_ctx *)ctx, data, len); } @@ -440,28 +444,28 @@ static inline void SHA256_update(struct sha256_ctx *const ctx, const void *data, * * @return pointer to computed digest inside ctx. */ -static inline const struct sha256_digest *SHA256_final( +__always_inline const struct sha256_digest *SHA256_final( struct sha256_ctx *const ctx) { return &ctx->f->final((union hash_ctx *)ctx)->sha256; } -static inline void HMAC_SHA256_update(struct hmac_sha256_ctx *const ctx, - const void *data, size_t len) +__always_inline void HMAC_SHA256_update(struct hmac_sha256_ctx *const ctx, + const void *data, size_t len) { ctx->hash.f->update((union hash_ctx *)&ctx->hash, data, len); } -static inline const struct sha256_digest *HMAC_SHA256_final( +__always_inline const struct sha256_digest *HMAC_SHA256_final( struct hmac_sha256_ctx *ctx) { return &ctx->hash.f->hmac_final((union hmac_ctx *)ctx)->sha256; } -static inline void SHA1_update(struct sha1_ctx *const ctx, const void *data, - size_t len) +__always_inline void SHA1_update(struct sha1_ctx *const ctx, const void *data, + size_t len) { ctx->f->update((union hash_ctx *)ctx, data, len); } -static inline const struct sha1_digest *SHA1_final(struct sha1_ctx *const ctx) +__always_inline const struct sha1_digest *SHA1_final(struct sha1_ctx *const ctx) { return &ctx->f->final((union hash_ctx *)ctx)->sha1; } @@ -473,7 +477,7 @@ static inline const struct sha1_digest *SHA1_final(struct sha1_ctx *const ctx) * @return DCRYPTO_OK if successful */ -static inline __warn_unused_result enum dcrypto_result DCRYPTO_hw_sha256_init( +__always_inline __warn_unused_result enum dcrypto_result DCRYPTO_hw_sha256_init( struct sha256_ctx *ctx) { return DCRYPTO_hw_hash_init((union hash_ctx *)ctx, HASH_SHA256); @@ -487,7 +491,7 @@ static inline __warn_unused_result enum dcrypto_result DCRYPTO_hw_sha256_init( * @param len length of key * @return DCRYPTO_OK if successful */ -static inline __warn_unused_result enum dcrypto_result +__always_inline __warn_unused_result enum dcrypto_result DCRYPTO_hw_hmac_sha256_init(struct hmac_sha256_ctx *ctx, const void *key, size_t len) { @@ -514,7 +518,7 @@ enum dcrypto_result DCRYPTO_hw_hmac_sha256_init(struct hmac_sha256_ctx *ctx, /** * Returns digest size for configured hash. */ -static inline size_t HASH_size(union hash_ctx *const ctx) +__always_inline size_t HASH_size(union hash_ctx *const ctx) { return ctx->f->digest_size; } @@ -522,82 +526,82 @@ static inline size_t HASH_size(union hash_ctx *const ctx) /** * Return block size for configured hash. */ -static inline size_t HASH_block_size(union hash_ctx *const ctx) +__always_inline size_t HASH_block_size(union hash_ctx *const ctx) { return ctx->f->block_size; } /* HMAC_update() is same as HASH_update(). */ -static inline void HMAC_update(union hmac_ctx *const ctx, const void *data, - size_t len) +__always_inline void HMAC_update(union hmac_ctx *const ctx, const void *data, + size_t len) { ctx->f->update(&ctx->hash, data, len); } -static inline size_t HMAC_size(union hmac_ctx *const ctx) +__always_inline size_t HMAC_size(union hmac_ctx *const ctx) { return ctx->f->digest_size; } -static inline const union sha_digests *HMAC_final(union hmac_ctx *const ctx) +__always_inline const union sha_digests *HMAC_final(union hmac_ctx *const ctx) { return ctx->f->hmac_final(ctx); } -static inline void HMAC_SHA1_update(struct hmac_sha1_ctx *const ctx, - const void *data, size_t len) +__always_inline void HMAC_SHA1_update(struct hmac_sha1_ctx *const ctx, + const void *data, size_t len) { ctx->hash.f->update((union hash_ctx *)&ctx->hash, data, len); } -static inline const struct sha1_digest *HMAC_SHA1_final( +__always_inline const struct sha1_digest *HMAC_SHA1_final( struct hmac_sha1_ctx *const ctx) { return &ctx->hash.f->hmac_final((union hmac_ctx *)ctx)->sha1; } #ifdef CONFIG_UPTO_SHA512 -static inline void SHA384_update(struct sha384_ctx *const ctx, const void *data, - size_t len) +__always_inline void SHA384_update(struct sha384_ctx *const ctx, + const void *data, size_t len) { ctx->f->update((union hash_ctx *)ctx, data, len); } -static inline const struct sha384_digest *SHA384_final( +__always_inline const struct sha384_digest *SHA384_final( struct sha384_ctx *const ctx) { return &ctx->f->final((union hash_ctx *)ctx)->sha384; } -static inline void SHA512_update(struct sha512_ctx *const ctx, const void *data, - size_t len) +__always_inline void SHA512_update(struct sha512_ctx *const ctx, + const void *data, size_t len) { ctx->f->update((union hash_ctx *)ctx, data, len); } -static inline const struct sha512_digest *SHA512_final( +__always_inline const struct sha512_digest *SHA512_final( struct sha512_ctx *const ctx) { return &ctx->f->final((union hash_ctx *)ctx)->sha512; } -static inline void HMAC_SHA384_update(struct hmac_sha384_ctx *ctx, - const void *data, size_t len) +__always_inline void HMAC_SHA384_update(struct hmac_sha384_ctx *ctx, + const void *data, size_t len) { ctx->hash.f->update((union hash_ctx *)&ctx->hash, data, len); } -static inline const struct sha384_digest *HMAC_SHA384_final( +__always_inline const struct sha384_digest *HMAC_SHA384_final( struct hmac_sha384_ctx *ctx) { return &ctx->hash.f->hmac_final((union hmac_ctx *)ctx)->sha384; } -static inline void HMAC_SHA512_update(struct hmac_sha512_ctx *ctx, - const void *data, size_t len) +__always_inline void HMAC_SHA512_update(struct hmac_sha512_ctx *ctx, + const void *data, size_t len) { ctx->hash.f->update((union hash_ctx *)&ctx->hash, data, len); } -static inline const struct sha512_digest *HMAC_SHA512_final( +__always_inline const struct sha512_digest *HMAC_SHA512_final( struct hmac_sha512_ctx *ctx) { return &ctx->hash.f->hmac_final((union hmac_ctx *)ctx)->sha512; @@ -725,7 +729,7 @@ void DCRYPTO_bn_wrap(struct LITE_BIGNUM *b, void *buf, size_t len); * @param b pointer to big number * @return length in bits */ -static inline size_t bn_bits(const struct LITE_BIGNUM *b) +inline size_t bn_bits(const struct LITE_BIGNUM *b) { return b->dmax * sizeof(*b->d) * 8; } @@ -735,7 +739,7 @@ static inline size_t bn_bits(const struct LITE_BIGNUM *b) * @param b pointer to big number * @return length in bits */ -static inline size_t bn_size(const struct LITE_BIGNUM *b) +inline size_t bn_size(const struct LITE_BIGNUM *b) { return b->dmax * sizeof(*b->d); } @@ -1160,7 +1164,7 @@ uint64_t fips_trng_rand32(void); * @return true if rand contains valid random */ -static inline bool rand_valid(uint64_t rand) +inline bool rand_valid(uint64_t rand) { return (rand >> 32) != 0; } diff --git a/board/cr50/dcrypto/internal.h b/board/cr50/dcrypto/internal.h index fc3a0a5d30..1fb66f710c 100644 --- a/board/cr50/dcrypto/internal.h +++ b/board/cr50/dcrypto/internal.h @@ -53,7 +53,7 @@ void dcrypto_sha_fifo_load(const void *data, size_t n); * Reset hash context with the same hash function as configured. * Will crash if previously not configured! Used for HMAC. */ -static inline void HASH_reinit(union hash_ctx *const ctx) +__always_inline void HASH_reinit(union hash_ctx *const ctx) { ctx->f->init(ctx); } @@ -77,7 +77,6 @@ const struct sha224_digest *SHA224_sw_final(struct sha224_ctx *const ctx); const struct sha224_digest *SHA224_sw_hash(const void *data, size_t len, struct sha224_digest *digest); - /** * Initialize HMAC for pre-configured hash. * This is generic function which can initialize HMAC with any supported @@ -89,21 +88,21 @@ const union sha_digests *HMAC_sw_final(union hmac_ctx *const ctx); /** * HMAC SHA2-224 initialization. */ -static inline void HMAC_SHA224_sw_init(struct hmac_sha224_ctx *const ctx, - const void *key, size_t len) +__always_inline void HMAC_SHA224_sw_init( + struct hmac_sha224_ctx *const ctx, const void *key, size_t len) { SHA224_sw_init(&ctx->hash); HMAC_sw_init((union hmac_ctx *)ctx, key, len); } -static inline void HMAC_SHA224_update(struct hmac_sha224_ctx *const ctx, - const void *data, size_t len) +__always_inline void HMAC_SHA224_update( + struct hmac_sha224_ctx *const ctx, const void *data, size_t len) { ctx->hash.f->update((union hash_ctx *)&ctx->hash, data, len); } -static inline const struct sha224_digest * -HMAC_SHA224_final(struct hmac_sha224_ctx *const ctx) +__always_inline const struct sha224_digest *HMAC_SHA224_final( + struct hmac_sha224_ctx *const ctx) { return &ctx->hash.f->hmac_final((union hmac_ctx *)ctx)->sha224; } @@ -111,19 +110,18 @@ HMAC_SHA224_final(struct hmac_sha224_ctx *const ctx) /** * HMAC SHA2-256 initialization. */ -static inline void HMAC_SHA256_sw_init(struct hmac_sha256_ctx *const ctx, - const void *key, size_t len) +__always_inline void HMAC_SHA256_sw_init( + struct hmac_sha256_ctx *const ctx, const void *key, size_t len) { SHA256_sw_init(&ctx->hash); HMAC_sw_init((union hmac_ctx *)ctx, key, len); } - /** * HMAC SHA1 initialization. */ -static inline void HMAC_SHA1_sw_init(struct hmac_sha1_ctx *const ctx, - const void *key, size_t len) +__always_inline void HMAC_SHA1_sw_init(struct hmac_sha1_ctx *const ctx, + const void *key, size_t len) { SHA1_sw_init(&ctx->hash); HMAC_sw_init((union hmac_ctx *)ctx, key, len); @@ -162,8 +160,8 @@ const struct sha512_digest *SHA512_hw_hash(const void *data, size_t len, /** * HMAC SHA2-384 initialization. */ -static inline void HMAC_SHA384_sw_init(struct hmac_sha384_ctx *ctx, - const void *key, size_t len) +__always_inline void HMAC_SHA384_sw_init(struct hmac_sha384_ctx *ctx, + const void *key, size_t len) { SHA384_sw_init(&ctx->hash); HMAC_sw_init((union hmac_ctx *)ctx, key, len); @@ -171,8 +169,8 @@ static inline void HMAC_SHA384_sw_init(struct hmac_sha384_ctx *ctx, /** * HMAC SHA2-512 initialization. */ -static inline void HMAC_SHA512_sw_init(struct hmac_sha512_ctx *ctx, - const void *key, size_t len) +__always_inline void HMAC_SHA512_sw_init(struct hmac_sha512_ctx *ctx, + const void *key, size_t len) { SHA512_sw_init(&ctx->hash); HMAC_sw_init((union hmac_ctx *)ctx, key, len); @@ -262,7 +260,7 @@ struct drbg_ctx { #define HMAC_DRBG_DO_NOT_AUTO_RESEED 0xFFFFFFFF /* Check that DRBG is properly initialized. */ -static inline bool hmac_drbg_ctx_valid(const struct drbg_ctx *drbg) +__always_inline bool hmac_drbg_ctx_valid(const struct drbg_ctx *drbg) { return drbg->magic_cookie == DCRYPTO_OK; } @@ -515,64 +513,64 @@ uint32_t dcrypto_dmem_load(size_t offset, const void *words, size_t n_words); #endif /* rotate 32-bit value right */ -static inline uint32_t ror(uint32_t value, int bits) +__always_inline uint32_t ror(uint32_t value, int bits) { /* return __builtin_rotateright32(value, bits); */ return (value >> bits) | (value << (32 - bits)); } /* rotate 64-bit value right */ -static inline uint64_t ror64(uint64_t value, int bits) +__always_inline uint64_t ror64(uint64_t value, int bits) { /* return __builtin_rotateright64(value, bits); */ return (value >> bits) | (value << (64 - bits)); } /* rotate 32-bit value left */ -static inline uint32_t rol(uint32_t value, int bits) +__always_inline uint32_t rol(uint32_t value, int bits) { /* return __builtin_rotateleft32(value, bits); */ return (value << bits) | (value >> (32 - bits)); } /* rotate 64-bit value left */ -static inline uint64_t rol64(uint64_t value, int bits) +__always_inline uint64_t rol64(uint64_t value, int bits) { /* return __builtin_rotateleft64(value, bits); */ return (value << bits) | (value >> (64 - bits)); } /* Functions to convert between uint32_t and uint64_t */ -static inline uint32_t lo32(uint64_t v) +__always_inline uint32_t lo32(uint64_t v) { return (uint32_t)v; } -static inline uint32_t hi32(uint64_t v) +__always_inline uint32_t hi32(uint64_t v) { return (uint32_t)(v >> 32); } -static inline uint64_t make64(uint32_t hi, uint32_t lo) +__always_inline uint64_t make64(uint32_t hi, uint32_t lo) { return (((uint64_t)hi) << 32) | lo; } -static inline uint32_t lo16(uint32_t v) +__always_inline uint32_t lo16(uint32_t v) { return (uint32_t)(v)&0xffff; } -static inline uint32_t hi16(uint32_t v) +__always_inline uint32_t hi16(uint32_t v) { return (uint32_t)(v >> 16); } -static inline int count_leading_zeros(uint32_t x) +__always_inline int count_leading_zeros(uint32_t x) { /* __builtin_clz(0) is undefined, so explicitly return bit size. */ return (x) ? __builtin_clz(x) : 32; } -static inline int count_trailing_zeros(uint32_t x) +__always_inline int count_trailing_zeros(uint32_t x) { /* __builtin_ctz(0) is undefined, so explicitly return bit size. */ return (x) ? __builtin_ctz(x) : 32; @@ -587,7 +585,7 @@ static inline int count_trailing_zeros(uint32_t x) #define WORD_MASK (sizeof(uintptr_t) - 1) /* return true if pointer is not word aligned. */ -static inline bool is_not_aligned(const void *ptr) +__always_inline bool is_not_aligned(const void *ptr) { return (uintptr_t)ptr & WORD_MASK; } @@ -614,7 +612,7 @@ static inline bool is_not_aligned(const void *ptr) * @return An integer which will happen to have the same value as `val` at * runtime. */ -static inline uintptr_t value_barrier(uintptr_t val) +__always_inline uintptr_t value_barrier(uintptr_t val) { /** * The +r constraint tells the compiler that this is an "inout" @@ -626,7 +624,7 @@ static inline uintptr_t value_barrier(uintptr_t val) asm("" : "+r"(val)); return val; } -static inline void *value_barrier_ptr(void *val) +__always_inline void *value_barrier_ptr(void *val) { /** * The +r constraint tells the compiler that this is an "inout" @@ -654,8 +652,9 @@ static inline void *value_barrier_ptr(void *val) * Example: * hardened_select_if_zero(test, CRYPTO_FAIL, CRYPTO_OK) */ -static inline __attribute__((always_inline)) -uintptr_t hardened_select_if_zero(uintptr_t test, uintptr_t a, uintptr_t b) +__always_inline uintptr_t hardened_select_if_zero(uintptr_t test, + uintptr_t a, + uintptr_t b) { uintptr_t bma = value_barrier(b - a); @@ -665,7 +664,7 @@ uintptr_t hardened_select_if_zero(uintptr_t test, uintptr_t a, uintptr_t b) } /* Convenience wrapper to return DCRYPTO_OK iff val == 0. */ -static inline enum dcrypto_result dcrypto_ok_if_zero(uintptr_t val) +__always_inline enum dcrypto_result dcrypto_ok_if_zero(uintptr_t val) { return (enum dcrypto_result)hardened_select_if_zero(val, DCRYPTO_FAIL, DCRYPTO_OK); |