Merge pull request #5430 from mtausig/hash_interface
Unified interface of hashing functions
This commit is contained in:
commit
ad5ad76db2
@ -34,15 +34,15 @@
|
||||
#define SHA1_K40 0x8f1bbcdc
|
||||
#define SHA1_K60 0xca62c1d6
|
||||
|
||||
void sha1_init(sha1_context *s)
|
||||
void sha1_init(sha1_context *ctx)
|
||||
{
|
||||
s->state[0] = 0x67452301;
|
||||
s->state[1] = 0xefcdab89;
|
||||
s->state[2] = 0x98badcfe;
|
||||
s->state[3] = 0x10325476;
|
||||
s->state[4] = 0xc3d2e1f0;
|
||||
s->byte_count = 0;
|
||||
s->buffer_offset = 0;
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xefcdab89;
|
||||
ctx->state[2] = 0x98badcfe;
|
||||
ctx->state[3] = 0x10325476;
|
||||
ctx->state[4] = 0xc3d2e1f0;
|
||||
ctx->byte_count = 0;
|
||||
ctx->buffer_offset = 0;
|
||||
}
|
||||
|
||||
static uint32_t sha1_rol32(uint32_t number, uint8_t bits)
|
||||
@ -108,16 +108,16 @@ static void sha1_add_uncounted(sha1_context *s, uint8_t data)
|
||||
}
|
||||
}
|
||||
|
||||
static void sha1_update_byte(sha1_context *s, unsigned char data)
|
||||
static void sha1_update_byte(sha1_context *ctx, uint8_t data)
|
||||
{
|
||||
++s->byte_count;
|
||||
sha1_add_uncounted(s, data++);
|
||||
++ctx->byte_count;
|
||||
sha1_add_uncounted(ctx, data);
|
||||
}
|
||||
|
||||
void sha1_update(sha1_context *s, const unsigned char *data, size_t len)
|
||||
void sha1_update(sha1_context *ctx, const uint8_t *data, size_t len)
|
||||
{
|
||||
while (len--) {
|
||||
sha1_update_byte(s, *(data++));
|
||||
sha1_update_byte(ctx, *(data++));
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,22 +142,22 @@ static void sha1_pad(sha1_context *s)
|
||||
sha1_add_uncounted(s, s->byte_count << 3);
|
||||
}
|
||||
|
||||
uint8_t *sha1_final(sha1_context *s)
|
||||
void sha1_final(sha1_context *ctx, uint8_t *dst)
|
||||
{
|
||||
/* Pad to complete the last block */
|
||||
sha1_pad(s);
|
||||
sha1_pad(ctx);
|
||||
|
||||
/* Swap byte order back */
|
||||
for (int i = 0; i < 5; i++) {
|
||||
s->state[i] =
|
||||
(((s->state[i]) << 24) & 0xff000000)
|
||||
| (((s->state[i]) << 8) & 0x00ff0000)
|
||||
| (((s->state[i]) >> 8) & 0x0000ff00)
|
||||
| (((s->state[i]) >> 24) & 0x000000ff);
|
||||
ctx->state[i] =
|
||||
(((ctx->state[i]) << 24) & 0xff000000)
|
||||
| (((ctx->state[i]) << 8) & 0x00ff0000)
|
||||
| (((ctx->state[i]) >> 8) & 0x0000ff00)
|
||||
| (((ctx->state[i]) >> 24) & 0x000000ff);
|
||||
}
|
||||
|
||||
/* Return pointer to hash (20 characters) */
|
||||
return (uint8_t *) s->state;
|
||||
/* Copy the content of the hash (20 characters) */
|
||||
memcpy(dst, ctx->state, 20);
|
||||
}
|
||||
|
||||
void sha1(uint8_t *dst, const uint8_t *src, size_t len)
|
||||
@ -166,49 +166,50 @@ void sha1(uint8_t *dst, const uint8_t *src, size_t len)
|
||||
|
||||
sha1_init(&ctx);
|
||||
sha1_update(&ctx, (unsigned char *) src, len);
|
||||
memcpy(dst, sha1_final(&ctx), SHA1_DIGEST_LENGTH);
|
||||
sha1_final(&ctx, dst);
|
||||
}
|
||||
|
||||
#define HMAC_IPAD 0x36
|
||||
#define HMAC_OPAD 0x5c
|
||||
|
||||
void sha1_init_hmac(sha1_context *s, const unsigned char *key, size_t key_length)
|
||||
void sha1_init_hmac(sha1_context *ctx, const uint8_t *key, size_t key_length)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
memset(s->key_buffer, 0, SHA1_BLOCK_LENGTH);
|
||||
memset(ctx->key_buffer, 0, SHA1_BLOCK_LENGTH);
|
||||
if (key_length > SHA1_BLOCK_LENGTH) {
|
||||
/* Hash long keys */
|
||||
sha1_init(s);
|
||||
sha1_init(ctx);
|
||||
while (key_length--) {
|
||||
sha1_update_byte(s, (unsigned char) *key++);
|
||||
sha1_update_byte(ctx, *key++);
|
||||
}
|
||||
memcpy(s->key_buffer, sha1_final(s), SHA1_DIGEST_LENGTH);
|
||||
sha1_final(ctx, ctx->key_buffer);
|
||||
}
|
||||
else {
|
||||
/* Block length keys are used as is */
|
||||
memcpy(s->key_buffer, key, key_length);
|
||||
memcpy(ctx->key_buffer, key, key_length);
|
||||
}
|
||||
/* Start inner hash */
|
||||
sha1_init(s);
|
||||
sha1_init(ctx);
|
||||
for (i = 0; i < SHA1_BLOCK_LENGTH; i++) {
|
||||
sha1_update_byte(s, s->key_buffer[i] ^ HMAC_IPAD);
|
||||
sha1_update_byte(ctx, ctx->key_buffer[i] ^ HMAC_IPAD);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *sha1_final_hmac(sha1_context *s)
|
||||
void sha1_final_hmac(sha1_context *ctx, uint8_t *dst)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
/* Complete inner hash */
|
||||
memcpy(s->inner_hash, sha1_final(s), SHA1_DIGEST_LENGTH);
|
||||
sha1_final(ctx, ctx->inner_hash);
|
||||
/* Calculate outer hash */
|
||||
sha1_init(s);
|
||||
sha1_init(ctx);
|
||||
for (i = 0; i < SHA1_BLOCK_LENGTH; i++) {
|
||||
sha1_update_byte(s, s->key_buffer[i] ^ HMAC_OPAD);
|
||||
sha1_update_byte(ctx, ctx->key_buffer[i] ^ HMAC_OPAD);
|
||||
}
|
||||
for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
|
||||
sha1_update_byte(s, s->inner_hash[i]);
|
||||
sha1_update_byte(ctx, ctx->inner_hash[i]);
|
||||
}
|
||||
return sha1_final(s);
|
||||
|
||||
sha1_final(ctx, dst);
|
||||
}
|
||||
|
||||
@ -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 */
|
||||
@ -190,7 +192,7 @@ void sha256_init(sha256_context_t *ctx)
|
||||
}
|
||||
|
||||
/* Add bytes into the hash */
|
||||
void sha256_update(sha256_context_t *ctx, const void *in, size_t len)
|
||||
void sha256_update(sha256_context_t *ctx, const uint8_t *data, size_t len)
|
||||
{
|
||||
/* Number of bytes left in the buffer from previous updates */
|
||||
uint32_t r = (ctx->count[1] >> 3) & 0x3f;
|
||||
@ -208,12 +210,12 @@ void sha256_update(sha256_context_t *ctx, const void *in, size_t len)
|
||||
|
||||
/* Handle the case where we don't need to perform any transforms */
|
||||
if (len < 64 - r) {
|
||||
memcpy(&ctx->buf[r], in, len);
|
||||
memcpy(&ctx->buf[r], data, len);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Finish the current block */
|
||||
const unsigned char *src = in;
|
||||
const unsigned char *src = data;
|
||||
|
||||
memcpy(&ctx->buf[r], src, 64 - r);
|
||||
sha256_transform(ctx->state, ctx->buf);
|
||||
@ -235,13 +237,13 @@ void sha256_update(sha256_context_t *ctx, const void *in, size_t len)
|
||||
* SHA-256 finalization. Pads the input data, exports the hash value,
|
||||
* and clears the context state.
|
||||
*/
|
||||
void sha256_final(unsigned char digest[32], sha256_context_t *ctx)
|
||||
void sha256_final(sha256_context_t *ctx, uint8_t *dst)
|
||||
{
|
||||
/* Add padding */
|
||||
sha256_pad(ctx);
|
||||
|
||||
/* Write the hash */
|
||||
be32enc_vect(digest, ctx->state, 32);
|
||||
be32enc_vect(dst, ctx->state, 32);
|
||||
|
||||
/* Clear the context state */
|
||||
memset((void *) ctx, 0, sizeof(*ctx));
|
||||
@ -258,7 +260,7 @@ unsigned char *sha256(const unsigned char *d, size_t n, unsigned char *md)
|
||||
|
||||
sha256_init(&c);
|
||||
sha256_update(&c, d, n);
|
||||
sha256_final(md, &c);
|
||||
sha256_final(&c, md);
|
||||
|
||||
return md;
|
||||
}
|
||||
@ -270,6 +272,7 @@ 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) {
|
||||
@ -301,8 +304,8 @@ const unsigned char *hmac_sha256(const unsigned char *key,
|
||||
|
||||
sha256_init(&c);
|
||||
sha256_update(&c, i_key_pad, SHA256_INTERNAL_BLOCK_SIZE);
|
||||
sha256_update(&c, message, message_length);
|
||||
sha256_final(tmp, &c);
|
||||
sha256_update(&c, (uint8_t *)message, message_length);
|
||||
sha256_final(&c, tmp);
|
||||
|
||||
static unsigned char m[SHA256_DIGEST_LENGTH];
|
||||
|
||||
@ -317,7 +320,7 @@ const unsigned char *hmac_sha256(const unsigned char *key,
|
||||
sha256_init(&c);
|
||||
sha256_update(&c, o_key_pad, SHA256_INTERNAL_BLOCK_SIZE);
|
||||
sha256_update(&c, tmp, SHA256_DIGEST_LENGTH);
|
||||
sha256_final(result, &c);
|
||||
sha256_final(&c, result);
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -331,9 +334,10 @@ 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(element, &ctx);
|
||||
sha256_final(&ctx, element);
|
||||
}
|
||||
|
||||
unsigned char *sha256_chain(const unsigned char *seed, size_t seed_length,
|
||||
@ -385,7 +389,7 @@ unsigned char *sha256_chain_with_waypoints(const unsigned char *seed,
|
||||
sha256_context_t ctx;
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, waypoints[(i - 1)].element, SHA256_DIGEST_LENGTH);
|
||||
sha256_final(waypoints[i].element, &ctx);
|
||||
sha256_final(&ctx, waypoints[i].element);
|
||||
waypoints[i].index = i;
|
||||
}
|
||||
|
||||
|
||||
@ -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 */
|
||||
|
||||
@ -66,27 +66,27 @@ typedef struct {
|
||||
/**
|
||||
* @brief Initialize SHA-1 message digest context
|
||||
*
|
||||
* @param[in] s Pointer to the SHA-1 context to initialize
|
||||
* @param[in] ctx Pointer to the SHA-1 context to initialize
|
||||
*/
|
||||
void sha1_init(sha1_context *s);
|
||||
void sha1_init(sha1_context *ctx);
|
||||
|
||||
/**
|
||||
* @brief Update the SHA-1 context with a portion of the message being hashed
|
||||
*
|
||||
* @param[in] s Pointer to the SHA-1 context to update
|
||||
* @param[in] data Pointer to the buffer to be hashed
|
||||
* @param[in] len Length of the buffer
|
||||
* @param[in] ctx Pointer to the SHA-1 context to update
|
||||
* @param[in] data Input data
|
||||
* @param[in] len Length of @p data
|
||||
*/
|
||||
void sha1_update(sha1_context *s, const unsigned char *data, size_t len);
|
||||
void sha1_update(sha1_context *ctx, const uint8_t *data, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Finalizes the SHA-1 message digest
|
||||
*
|
||||
* @param[in] s Pointer to the SHA-1 context
|
||||
* @param[in] ctx Pointer to the SHA-1 context
|
||||
* @param[out] dst Result location, must be 20 byte
|
||||
*
|
||||
* @return Calculated digest
|
||||
*/
|
||||
uint8_t *sha1_final(sha1_context *s);
|
||||
void sha1_final(sha1_context *ctx, uint8_t *dst);
|
||||
|
||||
/**
|
||||
* @brief Calculate a SHA1 hash from the given data
|
||||
@ -100,20 +100,20 @@ void sha1(uint8_t *dst, const uint8_t *src, size_t len);
|
||||
/**
|
||||
* @brief Initialize SHA-1 message digest context with MAC
|
||||
*
|
||||
* @param[in] s Pointer to the SHA-1 context to initialize
|
||||
* @param[in] ctx Pointer to the SHA-1 context to initialize
|
||||
* @param[in] key Key used in the HMAC-SHA1 computation
|
||||
* @param[in] key_length The size in bytes of @p key
|
||||
*/
|
||||
void sha1_init_hmac(sha1_context *s, const unsigned char *key, size_t key_length);
|
||||
void sha1_init_hmac(sha1_context *ctx, const uint8_t *key, size_t key_length);
|
||||
|
||||
/**
|
||||
* @brief Finalizes the SHA-1 message digest with MAC
|
||||
*
|
||||
* @param[in] s Pointer to the SHA-1 context
|
||||
*
|
||||
* @param[out] dst Result location, must be 20 byte
|
||||
* @return Calculated digest
|
||||
*/
|
||||
uint8_t* sha1_final_hmac(sha1_context *s);
|
||||
void sha1_final_hmac(sha1_context *ctx, uint8_t *dst);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -90,10 +90,10 @@ void sha256_init(sha256_context_t *ctx);
|
||||
* @brief Add bytes into the hash
|
||||
*
|
||||
* @param ctx sha256_context_t handle to use
|
||||
* @param in pointer to the input buffer
|
||||
* @param len length of the buffer
|
||||
* @param[in] data Input data
|
||||
* @param[in] len Length of @p data
|
||||
*/
|
||||
void sha256_update(sha256_context_t *ctx, const void *in, size_t len);
|
||||
void sha256_update(sha256_context_t *ctx, const uint8_t *data, size_t len);
|
||||
|
||||
/**
|
||||
* @brief SHA-256 finalization. Pads the input data, exports the hash value,
|
||||
@ -102,7 +102,7 @@ void sha256_update(sha256_context_t *ctx, const void *in, size_t len);
|
||||
* @param digest resulting digest, this is the hash of all the bytes
|
||||
* @param ctx sha256_context_t handle to use
|
||||
*/
|
||||
void sha256_final(unsigned char digest[32], sha256_context_t *ctx);
|
||||
void sha256_final(sha256_context_t *ctx, uint8_t *dst);
|
||||
|
||||
/**
|
||||
* @brief A wrapper function to simplify the generation of a hash, this is
|
||||
|
||||
@ -112,7 +112,7 @@ static int calc_and_compare_hash(const char *str, const char *expected,
|
||||
{
|
||||
sha1_context ctx;
|
||||
|
||||
uint8_t *hash;
|
||||
uint8_t hash[SHA1_DIGEST_LENGTH];
|
||||
char tmp[(3 * SHA1_DIGEST_LENGTH) + 1];
|
||||
|
||||
/* calculate hash */
|
||||
@ -120,7 +120,7 @@ static int calc_and_compare_hash(const char *str, const char *expected,
|
||||
for (long int i = 0; i < repeatcount; ++i) {
|
||||
sha1_update(&ctx, (unsigned char*) str, strlen(str));
|
||||
}
|
||||
hash = sha1_final(&ctx);
|
||||
sha1_final(&ctx, hash);
|
||||
/* copy hash to string */
|
||||
for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) {
|
||||
sprintf(&(tmp[i * 3]), "%02X ", (unsigned) hash[i]);
|
||||
@ -150,19 +150,20 @@ static int calc_and_compare_hash_hmac(const char *str, const char *expected,
|
||||
{
|
||||
sha1_context ctx;
|
||||
|
||||
uint8_t *hash;
|
||||
uint8_t hash[SHA1_DIGEST_LENGTH];
|
||||
char tmp[(3 * SHA1_DIGEST_LENGTH) + 1];
|
||||
|
||||
/* calculate hash */
|
||||
sha1_init_hmac(&ctx, key, key_len);
|
||||
sha1_update(&ctx, (unsigned char*) str, strlen(str));
|
||||
|
||||
hash = sha1_final_hmac(&ctx);
|
||||
sha1_final_hmac(&ctx, hash);
|
||||
/* copy hash to string */
|
||||
for (size_t i = 0; i < SHA1_DIGEST_LENGTH; i++) {
|
||||
sprintf(&(tmp[i * 3]), "%02X ", (unsigned) hash[i]);
|
||||
}
|
||||
tmp[SHA1_DIGEST_LENGTH* 2] = '\0';
|
||||
|
||||
/* compare with result string */
|
||||
return strncmp(tmp, expected, strlen((char*) tmp));
|
||||
}
|
||||
|
||||
@ -60,7 +60,7 @@ static void test_sha256_hash_chain(void)
|
||||
sha256_context_t ctx;
|
||||
sha256_init(&ctx);
|
||||
sha256_update(&ctx, tmp_element, SHA256_DIGEST_LENGTH);
|
||||
sha256_final(tmp_element, &ctx);
|
||||
sha256_final(&ctx, tmp_element);
|
||||
|
||||
TEST_ASSERT(sha256_chain_verify_element(tmp_element, i,
|
||||
tail_hash_chain_element, elements) == 0);
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
#include "tests-hashes.h"
|
||||
|
||||
static int compare_str_vs_digest(const char *str,
|
||||
const unsigned char hash[SHA256_DIGEST_LENGTH])
|
||||
const uint8_t hash[SHA256_DIGEST_LENGTH])
|
||||
{
|
||||
char ch[3] = { 0, 0, 0 };
|
||||
size_t iter_hash = 0;
|
||||
@ -39,7 +39,7 @@ static void test_hashes_hmac_sha256_hash_sequence(void)
|
||||
unsigned char key[64];
|
||||
/* prepare an empty key */
|
||||
memset((void*)key, 0x0, 64);
|
||||
static unsigned char hmac[SHA256_DIGEST_LENGTH];
|
||||
static uint8_t hmac[SHA256_DIGEST_LENGTH];
|
||||
|
||||
/* use an empty message */
|
||||
const unsigned *m = NULL;
|
||||
|
||||
@ -140,8 +140,8 @@ static int calc_and_compare_hash(const char *str, const unsigned char *expected)
|
||||
sha256_context_t sha256;
|
||||
|
||||
sha256_init(&sha256);
|
||||
sha256_update(&sha256, str, strlen(str));
|
||||
sha256_final(hash, &sha256);
|
||||
sha256_update(&sha256, (uint8_t*)str, strlen(str));
|
||||
sha256_final(&sha256, hash);
|
||||
|
||||
return (memcmp(expected, hash, SHA256_DIGEST_LENGTH) == 0);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user