Skip to content

Commit a690779

Browse files
committed
hash: fix memory allocation
Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent 9ba33c6 commit a690779

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

drng/api/selftest_rng.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern "C" {
3030

3131
#define LC_SELFTEST_DRNG_STATE_SIZE (LC_SHAKE_128_CTX_SIZE)
3232
#define LC_SELFTEST_DRNG_CTX_SIZE \
33-
(sizeof(struct lc_rng) + LC_SELFTEST_DRNG_STATE_SIZE)
33+
(sizeof(struct lc_rng_ctx) + LC_SELFTEST_DRNG_STATE_SIZE)
3434

3535
extern const struct lc_rng *lc_selftest_drng;
3636

@@ -39,7 +39,6 @@ extern const struct lc_rng *lc_selftest_drng;
3939
#define LC_SELFTEST_RNG_CTX(name) \
4040
LC_RNG_CTX(name, lc_selftest_drng); \
4141
LC_SELFTEST_HASH_SET_CTX((struct lc_hash_ctx *)name->rng_state); \
42-
lc_rng_zero(name); \
4342
CKINT(lc_hash_init(name->rng_state))
4443

4544
/*

hash/api/lc_sha3.h

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ struct lc_sha3_224_state {
6060

6161
#define LC_SHA3_224_STATE_SIZE (sizeof(struct lc_sha3_224_state))
6262
#define LC_SHA3_224_CTX_SIZE \
63-
(sizeof(struct lc_hash_ctx) + LC_SHA3_224_STATE_SIZE)
63+
LC_SHA3_STATE_SIZE_ALIGN( \
64+
(sizeof(struct lc_hash_ctx) + LC_SHA3_224_STATE_SIZE))
6465

6566
#define LC_SHA3_224_CTX(name) \
6667
LC_HASH_SET_CTX(name, lc_sha3_224); \
@@ -75,10 +76,8 @@ struct lc_sha3_224_state {
7576
#define LC_SHA3_224_CTX_ON_STACK(name) \
7677
_Pragma("GCC diagnostic push") _Pragma( \
7778
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
78-
LC_ALIGNED_BUFFER( \
79-
name##_ctx_buf, \
80-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_224_CTX_SIZE), \
81-
LC_HASH_COMMON_ALIGNMENT); \
79+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_224_CTX_SIZE, \
80+
LC_HASH_COMMON_ALIGNMENT); \
8281
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
8382
LC_SHA3_224_CTX(name); \
8483
_Pragma("GCC diagnostic pop")
@@ -109,7 +108,8 @@ struct lc_sha3_256_state {
109108

110109
#define LC_SHA3_256_STATE_SIZE (sizeof(struct lc_sha3_256_state))
111110
#define LC_SHA3_256_CTX_SIZE \
112-
(sizeof(struct lc_hash_ctx) + LC_SHA3_256_STATE_SIZE)
111+
LC_SHA3_STATE_SIZE_ALIGN( \
112+
(sizeof(struct lc_hash_ctx) + LC_SHA3_256_STATE_SIZE))
113113

114114
#define LC_SHA3_256_CTX(name) \
115115
LC_HASH_SET_CTX(name, lc_sha3_256); \
@@ -124,10 +124,8 @@ struct lc_sha3_256_state {
124124
#define LC_SHA3_256_CTX_ON_STACK(name) \
125125
_Pragma("GCC diagnostic push") _Pragma( \
126126
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
127-
LC_ALIGNED_BUFFER( \
128-
name##_ctx_buf, \
129-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE), \
130-
LC_HASH_COMMON_ALIGNMENT); \
127+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_256_CTX_SIZE, \
128+
LC_HASH_COMMON_ALIGNMENT); \
131129
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
132130
LC_SHA3_256_CTX(name); \
133131
_Pragma("GCC diagnostic pop")
@@ -158,7 +156,8 @@ struct lc_sha3_384_state {
158156

159157
#define LC_SHA3_384_STATE_SIZE (sizeof(struct lc_sha3_384_state))
160158
#define LC_SHA3_384_CTX_SIZE \
161-
(sizeof(struct lc_hash_ctx) + LC_SHA3_384_STATE_SIZE)
159+
LC_SHA3_STATE_SIZE_ALIGN( \
160+
(sizeof(struct lc_hash_ctx) + LC_SHA3_384_STATE_SIZE))
162161

163162
#define LC_SHA3_384_CTX(name) \
164163
LC_HASH_SET_CTX(name, lc_sha3_384); \
@@ -173,10 +172,8 @@ struct lc_sha3_384_state {
173172
#define LC_SHA3_384_CTX_ON_STACK(name) \
174173
_Pragma("GCC diagnostic push") _Pragma( \
175174
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
176-
LC_ALIGNED_BUFFER( \
177-
name##_ctx_buf, \
178-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_384_CTX_SIZE), \
179-
LC_HASH_COMMON_ALIGNMENT); \
175+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_384_CTX_SIZE, \
176+
LC_HASH_COMMON_ALIGNMENT); \
180177
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
181178
LC_SHA3_384_CTX(name); \
182179
_Pragma("GCC diagnostic pop")
@@ -207,7 +204,8 @@ struct lc_sha3_512_state {
207204

208205
#define LC_SHA3_512_STATE_SIZE (sizeof(struct lc_sha3_512_state))
209206
#define LC_SHA3_512_CTX_SIZE \
210-
(sizeof(struct lc_hash_ctx) + LC_SHA3_512_STATE_SIZE)
207+
LC_SHA3_STATE_SIZE_ALIGN( \
208+
(sizeof(struct lc_hash_ctx) + LC_SHA3_512_STATE_SIZE))
211209

212210
#define LC_SHA3_512_CTX(name) \
213211
LC_HASH_SET_CTX(name, lc_sha3_512); \
@@ -222,10 +220,8 @@ struct lc_sha3_512_state {
222220
#define LC_SHA3_512_CTX_ON_STACK(name) \
223221
_Pragma("GCC diagnostic push") _Pragma( \
224222
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
225-
LC_ALIGNED_BUFFER( \
226-
name##_ctx_buf, \
227-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_512_CTX_SIZE), \
228-
LC_HASH_COMMON_ALIGNMENT); \
223+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_512_CTX_SIZE, \
224+
LC_HASH_COMMON_ALIGNMENT); \
229225
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
230226
LC_SHA3_512_CTX(name); \
231227
_Pragma("GCC diagnostic pop")
@@ -254,7 +250,9 @@ struct lc_shake_128_state {
254250
};
255251

256252
#define LC_SHAKE_128_STATE_SIZE (sizeof(struct lc_shake_128_state))
257-
#define LC_SHAKE_128_CTX_SIZE (sizeof(struct lc_hash) + LC_SHAKE_128_STATE_SIZE)
253+
#define LC_SHAKE_128_CTX_SIZE \
254+
LC_SHA3_STATE_SIZE_ALIGN( \
255+
(sizeof(struct lc_hash_ctx) + LC_SHAKE_128_STATE_SIZE))
258256

259257
#define LC_SHAKE_128_CTX(name) \
260258
LC_HASH_SET_CTX(name, lc_shake128); \
@@ -269,10 +267,8 @@ struct lc_shake_128_state {
269267
#define LC_SHAKE_128_CTX_ON_STACK(name) \
270268
_Pragma("GCC diagnostic push") _Pragma( \
271269
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
272-
LC_ALIGNED_BUFFER( \
273-
name##_ctx_buf, \
274-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHAKE_128_CTX_SIZE), \
275-
LC_HASH_COMMON_ALIGNMENT); \
270+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHAKE_128_CTX_SIZE, \
271+
LC_HASH_COMMON_ALIGNMENT); \
276272
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
277273
LC_SHAKE_128_CTX(name); \
278274
_Pragma("GCC diagnostic pop")
@@ -304,10 +300,8 @@ extern const struct lc_hash *lc_shake256;
304300
#define LC_SHAKE_256_CTX_ON_STACK(name) \
305301
_Pragma("GCC diagnostic push") _Pragma( \
306302
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
307-
LC_ALIGNED_BUFFER( \
308-
name##_ctx_buf, \
309-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHAKE_256_CTX_SIZE), \
310-
LC_HASH_COMMON_ALIGNMENT); \
303+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHAKE_256_CTX_SIZE, \
304+
LC_HASH_COMMON_ALIGNMENT); \
311305
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
312306
LC_SHAKE_256_CTX(name); \
313307
_Pragma("GCC diagnostic pop")
@@ -339,10 +333,8 @@ extern const struct lc_hash *lc_shake512;
339333
#define LC_SHAKE_512_CTX_ON_STACK(name) \
340334
_Pragma("GCC diagnostic push") _Pragma( \
341335
"GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
342-
LC_ALIGNED_BUFFER( \
343-
name##_ctx_buf, \
344-
LC_SHA3_STATE_SIZE_ALIGN(LC_SHAKE_512_CTX_SIZE), \
345-
LC_HASH_COMMON_ALIGNMENT); \
336+
LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHAKE_512_CTX_SIZE, \
337+
LC_HASH_COMMON_ALIGNMENT); \
346338
struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
347339
LC_SHAKE_512_CTX(name); \
348340
_Pragma("GCC diagnostic pop")

0 commit comments

Comments
 (0)