1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #20116 from bergzand/pr/sha256/no_static

hashes/sha2{24,56}: Remove static variables from sha256
This commit is contained in:
mguetschow 2024-01-16 16:01:56 +00:00 committed by GitHub
commit 084dedcca7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 39 deletions

View File

@ -19,6 +19,7 @@
*/ */
#include <string.h> #include <string.h>
#include <assert.h>
#include "hashes/sha224.h" #include "hashes/sha224.h"
#include "hashes/sha2xx_common.h" #include "hashes/sha2xx_common.h"
@ -40,18 +41,12 @@ void sha224_init(sha224_context_t *ctx)
ctx->state[7] = 0xBEFA4FA4; ctx->state[7] = 0xBEFA4FA4;
} }
void *sha224(const void *data, size_t len, void *digest) void sha224(const void *data, size_t len, void *digest)
{ {
sha224_context_t c; sha224_context_t c;
static unsigned char m[SHA224_DIGEST_LENGTH]; assert(digest);
if (digest == NULL) {
digest = m;
}
sha224_init(&c); sha224_init(&c);
sha224_update(&c, data, len); sha224_update(&c, data, len);
sha224_final(&c, digest); sha224_final(&c, digest);
return digest;
} }

View File

@ -68,20 +68,14 @@ void sha256_init(sha256_context_t *ctx)
ctx->state[7] = 0x5BE0CD19; ctx->state[7] = 0x5BE0CD19;
} }
void *sha256(const void *data, size_t len, void *digest) void sha256(const void *data, size_t len, void *digest)
{ {
sha256_context_t c; sha256_context_t c;
static unsigned char m[SHA256_DIGEST_LENGTH]; assert(digest);
if (digest == NULL) {
digest = m;
}
sha256_init(&c); sha256_init(&c);
sha256_update(&c, data, len); sha256_update(&c, data, len);
sha256_final(&c, digest); sha256_final(&c, digest);
return digest;
} }
void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length) void hmac_sha256_init(hmac_context_t *ctx, const void *key, size_t key_length)
@ -135,19 +129,13 @@ void hmac_sha256_final(hmac_context_t *ctx, void *digest)
{ {
unsigned char tmp[SHA256_DIGEST_LENGTH]; unsigned char tmp[SHA256_DIGEST_LENGTH];
static unsigned char m[SHA256_DIGEST_LENGTH];
if (digest == NULL) {
digest = m;
}
sha256_final(&ctx->c_in, tmp); sha256_final(&ctx->c_in, tmp);
sha2xx_update(&ctx->c_out, tmp, SHA256_DIGEST_LENGTH); sha2xx_update(&ctx->c_out, tmp, SHA256_DIGEST_LENGTH);
sha256_final(&ctx->c_out, digest); sha256_final(&ctx->c_out, digest);
} }
const void *hmac_sha256(const void *key, size_t key_length, void hmac_sha256(const void *key, size_t key_length,
const void *data, size_t len, void *digest) const void *data, size_t len, void *digest)
{ {
hmac_context_t ctx; hmac_context_t ctx;
@ -155,8 +143,6 @@ const void *hmac_sha256(const void *key, size_t key_length,
hmac_sha256_init(&ctx, key, key_length); hmac_sha256_init(&ctx, key, key_length);
hmac_sha256_update(&ctx,data, len); hmac_sha256_update(&ctx,data, len);
hmac_sha256_final(&ctx, digest); hmac_sha256_final(&ctx, digest);
return digest;
} }
/** /**

View File

@ -110,11 +110,10 @@ static inline void sha224_final(sha224_context_t *ctx, void *digest)
* *
* @param[in] data pointer to the buffer to generate hash from * @param[in] data pointer to the buffer to generate hash from
* @param[in] len length of the buffer * @param[in] len length of the buffer
* @param[out] digest optional pointer to an array for the result, length must * @param[out] digest Pointer to an array for the result, length must
* be SHA224_DIGEST_LENGTH * be SHA224_DIGEST_LENGTH
* if digest == NULL, one static buffer is used
*/ */
void *sha224(const void *data, size_t len, void *digest); void sha224(const void *data, size_t len, void *digest);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -128,11 +128,10 @@ static inline void sha256_final(sha256_context_t *ctx, void *digest)
* *
* @param[in] data pointer to the buffer to generate hash from * @param[in] data pointer to the buffer to generate hash from
* @param[in] len length of the buffer * @param[in] len length of the buffer
* @param[out] digest optional pointer to an array for the result, length must * @param[out] digest Pointer to an array for the result, length must
* be SHA256_DIGEST_LENGTH * be SHA256_DIGEST_LENGTH
* if digest == NULL, one static buffer is used
*/ */
void *sha256(const void *data, size_t len, void *digest); void sha256(const void *data, size_t len, void *digest);
/** /**
* @brief hmac_sha256_init HMAC SHA-256 calculation. Initiate calculation of a HMAC * @brief hmac_sha256_init HMAC SHA-256 calculation. Initiate calculation of a HMAC
@ -153,9 +152,7 @@ void hmac_sha256_update(hmac_context_t *ctx, const void *data, size_t len);
/** /**
* @brief hmac_sha256_final HMAC SHA-256 finalization. Finish HMAC calculation and export the value * @brief hmac_sha256_final HMAC SHA-256 finalization. Finish HMAC calculation and export the value
* @param[in] ctx hmac_context_t handle to use * @param[in] ctx hmac_context_t handle to use
* @param[out] digest the computed hmac-sha256, * @param[out] digest the computed hmac-sha256, length MUST be SHA256_DIGEST_LENGTH
* length MUST be SHA256_DIGEST_LENGTH
* if digest == NULL, a static buffer is used
*/ */
void hmac_sha256_final(hmac_context_t *ctx, void *digest); void hmac_sha256_final(hmac_context_t *ctx, void *digest);
@ -168,11 +165,8 @@ void hmac_sha256_final(hmac_context_t *ctx, void *digest);
* @param[in] len the length of the message in bytes * @param[in] len the length of the message in bytes
* @param[out] digest the computed hmac-sha256, * @param[out] digest the computed hmac-sha256,
* length MUST be SHA256_DIGEST_LENGTH * length MUST be SHA256_DIGEST_LENGTH
* if digest == NULL, a static buffer is used
* @returns pointer to the resulting digest.
* if result == NULL the pointer points to the static buffer
*/ */
const void *hmac_sha256(const void *key, size_t key_length, void hmac_sha256(const void *key, size_t key_length,
const void *data, size_t len, void *digest); const void *data, size_t len, void *digest);
/** /**