Skip to content

Commit 9300007

Browse files
committed
sys/hashes: support for SHA-{384,512/{224,256}}
pkg/esp32_sdk: rename sha384_init to avoid name clash
1 parent c571039 commit 9300007

File tree

16 files changed

+1138
-16
lines changed

16 files changed

+1138
-16
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
From 1a3bd2c8020d9d8e36312f0a64adf9d3bf45f462 Mon Sep 17 00:00:00 2001
2+
From: =?UTF-8?q?Mikolai=20G=C3=BCtschow?= <[email protected]>
3+
Date: Fri, 22 Mar 2024 07:54:19 +0100
4+
Subject: [PATCH] wpa_supplicant: add prefix wpa_ to sha384_init
5+
6+
Prefix `wpa_` added to `sha384_init` function of `wpa_suppplicant` to avoid name conflicts with RIOT module `sys/hashes`.
7+
---
8+
components/wpa_supplicant/src/crypto/crypto_internal.c | 2 +-
9+
components/wpa_supplicant/src/crypto/sha384-internal.c | 4 ++--
10+
components/wpa_supplicant/src/crypto/sha384_i.h | 2 +-
11+
3 files changed, 4 insertions(+), 4 deletions(-)
12+
13+
diff --git a/components/wpa_supplicant/src/crypto/crypto_internal.c b/components/wpa_supplicant/src/crypto/crypto_internal.c
14+
index 7ff588cb..bda80730 100644
15+
--- a/components/wpa_supplicant/src/crypto/crypto_internal.c
16+
+++ b/components/wpa_supplicant/src/crypto/crypto_internal.c
17+
@@ -62,7 +62,7 @@ struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
18+
#endif /* CONFIG_SHA256 */
19+
#ifdef CONFIG_INTERNAL_SHA384
20+
case CRYPTO_HASH_ALG_SHA384:
21+
- sha384_init(&ctx->u.sha384);
22+
+ wpa_sha384_init(&ctx->u.sha384);
23+
break;
24+
#endif /* CONFIG_INTERNAL_SHA384 */
25+
#ifdef CONFIG_INTERNAL_SHA512
26+
diff --git a/components/wpa_supplicant/src/crypto/sha384-internal.c b/components/wpa_supplicant/src/crypto/sha384-internal.c
27+
index 646f7297..5cefa825 100644
28+
--- a/components/wpa_supplicant/src/crypto/sha384-internal.c
29+
+++ b/components/wpa_supplicant/src/crypto/sha384-internal.c
30+
@@ -27,7 +27,7 @@ int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
31+
struct sha384_state ctx;
32+
size_t i;
33+
34+
- sha384_init(&ctx);
35+
+ wpa_sha384_init(&ctx);
36+
for (i = 0; i < num_elem; i++)
37+
if (sha384_process(&ctx, addr[i], len[i]))
38+
return -1;
39+
@@ -49,7 +49,7 @@ int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
40+
@param md The hash state you wish to initialize
41+
@return CRYPT_OK if successful
42+
*/
43+
-void sha384_init(struct sha384_state *md)
44+
+void wpa_sha384_init(struct sha384_state *md)
45+
{
46+
md->curlen = 0;
47+
md->length = 0;
48+
diff --git a/components/wpa_supplicant/src/crypto/sha384_i.h b/components/wpa_supplicant/src/crypto/sha384_i.h
49+
index a00253ff..57860bdf 100644
50+
--- a/components/wpa_supplicant/src/crypto/sha384_i.h
51+
+++ b/components/wpa_supplicant/src/crypto/sha384_i.h
52+
@@ -15,7 +15,7 @@
53+
54+
#define sha384_state sha512_state
55+
56+
-void sha384_init(struct sha384_state *md);
57+
+void wpa_sha384_init(struct sha384_state *md);
58+
int sha384_process(struct sha384_state *md, const unsigned char *in,
59+
unsigned long inlen);
60+
int sha384_done(struct sha384_state *md, unsigned char *out);
61+
--
62+
2.39.2
63+

sys/hashes/sha224.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
* @}
1919
*/
2020

21-
#include <string.h>
2221
#include <assert.h>
2322

2423
#include "hashes/sha224.h"
25-
#include "hashes/sha2xx_common.h"
2624

2725
/* SHA-224 initialization. Begins a SHA-224 operation. */
2826
void sha224_init(sha224_context_t *ctx)

sys/hashes/sha384.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2023 TU Dresden
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup sys_hashes
11+
*
12+
* @{
13+
* @file
14+
* @brief SHA384 hash function implementation
15+
*
16+
* @author Mikolai Gütschow <[email protected]>
17+
*
18+
* @}
19+
*/
20+
21+
#include <assert.h>
22+
23+
#include "hashes/sha384.h"
24+
25+
void sha384_init(sha384_context_t *ctx)
26+
{
27+
/* Zero bits processed so far */
28+
ctx->count[0] = ctx->count[1] = 0;
29+
30+
/* Magic initialization constants */
31+
ctx->state[0] = 0xcbbb9d5dc1059ed8;
32+
ctx->state[1] = 0x629a292a367cd507;
33+
ctx->state[2] = 0x9159015a3070dd17;
34+
ctx->state[3] = 0x152fecd8f70e5939;
35+
ctx->state[4] = 0x67332667ffc00b31;
36+
ctx->state[5] = 0x8eb44a8768581511;
37+
ctx->state[6] = 0xdb0c2e0d64f98fa7;
38+
ctx->state[7] = 0x47b5481dbefa4fa4;
39+
}
40+
41+
void sha384(const void *data, size_t len, void *digest)
42+
{
43+
sha384_context_t c;
44+
assert(digest);
45+
46+
sha384_init(&c);
47+
sha384_update(&c, data, len);
48+
sha384_final(&c, digest);
49+
}

sys/hashes/sha512.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
* @}
1919
*/
2020

21-
#include <string.h>
2221
#include <assert.h>
2322

2423
#include "hashes/sha512.h"
25-
#include "hashes/sha512_common.h"
2624

2725
/* SHA-512 initialization. Begins a SHA-512 operation. */
2826
void sha512_init(sha512_context_t *ctx)
@@ -44,6 +42,7 @@ void sha512_init(sha512_context_t *ctx)
4442
void sha512(const void *data, size_t len, void *digest)
4543
{
4644
sha512_context_t c;
45+
assert(digest);
4746

4847
sha512_init(&c);
4948
sha512_update(&c, data, len);

sys/hashes/sha512_224.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2023 TU Dresden
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup sys_hashes
11+
*
12+
* @{
13+
* @file
14+
* @brief SHA512/224 hash function implementation
15+
*
16+
* @author Mikolai Gütschow <[email protected]>
17+
*
18+
* @}
19+
*/
20+
21+
#include <assert.h>
22+
23+
#include "hashes/sha512_224.h"
24+
25+
void sha512_224_init(sha512_224_context_t *ctx)
26+
{
27+
/* Zero bits processed so far */
28+
ctx->count[0] = ctx->count[1] = 0;
29+
30+
/* Magic initialization constants */
31+
ctx->state[0] = 0x8C3D37C819544DA2;
32+
ctx->state[1] = 0x73E1996689DCD4D6;
33+
ctx->state[2] = 0x1DFAB7AE32FF9C82;
34+
ctx->state[3] = 0x679DD514582F9FCF;
35+
ctx->state[4] = 0x0F6D2B697BD44DA8;
36+
ctx->state[5] = 0x77E36F7304C48942;
37+
ctx->state[6] = 0x3F9D85A86A1D36C8;
38+
ctx->state[7] = 0x1112E6AD91D692A1;
39+
}
40+
41+
void sha512_224(const void *data, size_t len, void *digest)
42+
{
43+
sha512_224_context_t c;
44+
assert(digest);
45+
46+
sha512_224_init(&c);
47+
sha512_224_update(&c, data, len);
48+
sha512_224_final(&c, digest);
49+
}

sys/hashes/sha512_256.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2023 TU Dresden
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup sys_hashes
11+
*
12+
* @{
13+
* @file
14+
* @brief SHA512/256 hash function implementation
15+
*
16+
* @author Mikolai Gütschow <[email protected]>
17+
*
18+
* @}
19+
*/
20+
21+
#include <assert.h>
22+
23+
#include "hashes/sha512_256.h"
24+
25+
void sha512_256_init(sha512_256_context_t *ctx)
26+
{
27+
/* Zero bits processed so far */
28+
ctx->count[0] = ctx->count[1] = 0;
29+
30+
/* Magic initialization constants */
31+
ctx->state[0] = 0x22312194FC2BF72C;
32+
ctx->state[1] = 0x9F555FA3C84C64C2;
33+
ctx->state[2] = 0x2393B86B6F53B151;
34+
ctx->state[3] = 0x963877195940EABD;
35+
ctx->state[4] = 0x96283EE2A88EFFE3;
36+
ctx->state[5] = 0xBE5E1E2553863992;
37+
ctx->state[6] = 0x2B0199FC2C85B8AA;
38+
ctx->state[7] = 0x0EB72DDC81C52CA2;
39+
}
40+
41+
void sha512_256(const void *data, size_t len, void *digest)
42+
{
43+
sha512_256_context_t c;
44+
assert(digest);
45+
46+
sha512_256_init(&c);
47+
sha512_256_update(&c, data, len);
48+
sha512_256_final(&c, digest);
49+
}

sys/hashes/sha512_common.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,25 @@
3434
#else /* !__BIG_ENDIAN__ */
3535

3636
/*
37-
* Encode a length len/8 vector of (uint64_t) into a length len vector of
38-
* (unsigned char) in big-endian form. Assumes len is a multiple of 8.
37+
* Encode a length ceil(len/8) vector of (uint64_t) into a length len vector of
38+
* (unsigned char) in big-endian form.
3939
*/
4040
static void be64enc_vect(void *dst_, const void *src_, size_t len)
4141
{
42-
/* Assert if len is not a multiple of 8 */
43-
assert(!(len & 7));
44-
42+
size_t i;
4543
if ((uintptr_t)dst_ % sizeof(uint64_t) == 0 &&
4644
(uintptr_t)src_ % sizeof(uint64_t) == 0) {
4745
uint64_t *dst = dst_;
4846
const uint64_t *src = src_;
49-
for (size_t i = 0; i < len / 8; i++) {
47+
for (i = 0; i < len / 8; i++) {
5048
dst[i] = __builtin_bswap64(src[i]);
5149
}
50+
i *= 8;
5251
}
5352
else {
5453
uint8_t *dst = dst_;
5554
const uint8_t *src = src_;
56-
for (size_t i = 0; i < len; i += 8) {
55+
for (i = 0; i < len-7; i += 8) {
5756
dst[i] = src[i + 7];
5857
dst[i + 1] = src[i + 6];
5958
dst[i + 2] = src[i + 5];
@@ -64,6 +63,14 @@ static void be64enc_vect(void *dst_, const void *src_, size_t len)
6463
dst[i + 7] = src[i];
6564
}
6665
}
66+
/* copy len%8 remaining bytes */
67+
if (i < len) {
68+
uint8_t *dst = dst_;
69+
const uint8_t *src = src_;
70+
for (size_t j = 0; j < len-i; j++) {
71+
dst[i + j] = src[i+7 - j];
72+
}
73+
}
6774
}
6875

6976
/*

sys/include/hashes/sha384.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (C) 2023 TU Dresden
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @defgroup sys_hashes_sha384 SHA-384
11+
* @ingroup sys_hashes_unkeyed
12+
* @brief Implementation of the SHA-384 hashing function
13+
* @{
14+
*
15+
* @file
16+
* @brief Header definitions for the SHA384 hash function
17+
*
18+
* @author Mikolai Gütschow <[email protected]>
19+
*/
20+
21+
#ifndef HASHES_SHA384_H
22+
#define HASHES_SHA384_H
23+
24+
#include <inttypes.h>
25+
#include <stddef.h>
26+
27+
#include "hashes/sha512_common.h"
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
/**
34+
* @brief Length of SHA384 digests in bytes
35+
*/
36+
#define SHA384_DIGEST_LENGTH (48)
37+
38+
/**
39+
* @brief 1024 Bit (128 Byte) internally used block size for sha384
40+
*/
41+
#define SHA384_INTERNAL_BLOCK_SIZE (128)
42+
43+
/**
44+
* @brief Context for cipher operations based on sha384
45+
*/
46+
typedef sha512_common_context_t sha384_context_t;
47+
48+
/**
49+
* @brief SHA-384 initialization. Begins a SHA-384 operation.
50+
*
51+
* @param ctx sha384_context_t handle to init, must not be NULL
52+
*/
53+
void sha384_init(sha384_context_t *ctx);
54+
55+
/**
56+
* @brief Add bytes into the hash
57+
*
58+
* @param ctx sha384_context_t handle to use, must not be NULL
59+
* @param[in] data Input data
60+
* @param[in] len Length of @p data
61+
*/
62+
static inline void sha384_update(sha384_context_t *ctx, const void *data, size_t len)
63+
{
64+
sha512_common_update(ctx, data, len);
65+
}
66+
67+
/**
68+
* @brief SHA-384 finalization. Pads the input data, exports the hash value,
69+
* and clears the context state.
70+
*
71+
* @param ctx sha384_context_t handle to use, must not be NULL
72+
* @param[out] digest pointer to resulting digest, this is the hash of all the bytes.
73+
* Length must be at least SHA384_DIGEST_LENGTH
74+
*/
75+
static inline void sha384_final(sha384_context_t *ctx, void *digest)
76+
{
77+
sha512_common_final(ctx, digest, SHA384_DIGEST_LENGTH);
78+
}
79+
80+
/**
81+
* @brief A wrapper function to simplify the generation of a hash. This is
82+
* useful for generating sha384 for one single buffer in a single step.
83+
*
84+
* @param[in] data pointer to the buffer to generate hash from
85+
* @param[in] len length of the buffer
86+
* @param[out] digest optional pointer to an array for the result, length must
87+
* be at least SHA384_DIGEST_LENGTH
88+
*/
89+
void sha384(const void *data, size_t len, void *digest);
90+
91+
#ifdef __cplusplus
92+
}
93+
#endif
94+
95+
/** @} */
96+
#endif /* HASHES_SHA384_H */

0 commit comments

Comments
 (0)