Skip to content

Commit 6a34c60

Browse files
Introduce separate _clean functions for hash module
This gives the caller more control about whether the state should be cleaned (= should be considered secret), which will be useful for example for Schnorr signature verification in the future. Moreover, it gives the caller the possibility to clean a hash struct without finalizing it.
1 parent 244c749 commit 6a34c60

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

src/ecmult_gen_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
204204
memclear(nonce32, sizeof(nonce32));
205205
secp256k1_scalar_clear(&b);
206206
secp256k1_gej_clear(&gb);
207+
secp256k1_rfc6979_hmac_sha256_clear(&rng);
207208
}
208209

209210
#endif /* SECP256K1_ECMULT_GEN_IMPL_H */

src/hash.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct {
1919
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash);
2020
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size);
2121
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32);
22+
static void secp256k1_sha256_clear(secp256k1_sha256 *hash);
2223

2324
typedef struct {
2425
secp256k1_sha256 inner, outer;
@@ -27,6 +28,7 @@ typedef struct {
2728
static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t size);
2829
static void secp256k1_hmac_sha256_write(secp256k1_hmac_sha256 *hash, const unsigned char *data, size_t size);
2930
static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256 *hash, unsigned char *out32);
31+
static void secp256k1_hmac_sha256_clear(secp256k1_hmac_sha256 *hash);
3032

3133
typedef struct {
3234
unsigned char v[32];
@@ -37,5 +39,6 @@ typedef struct {
3739
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen);
3840
static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256 *rng, unsigned char *out, size_t outlen);
3941
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng);
42+
static void secp256k1_rfc6979_hmac_sha256_clear(secp256k1_rfc6979_hmac_sha256 *rng);
4043

4144
#endif /* SECP256K1_HASH_H */

src/hash_impl.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out
164164

165165
memclear(sizedesc, sizeof(sizedesc));
166166
memclear(out, sizeof(out));
167-
memclear(hash, sizeof(secp256k1_sha256));
167+
}
168+
169+
static void secp256k1_sha256_clear(secp256k1_sha256 *hash) {
170+
memclear(hash, sizeof(*hash));
168171
}
169172

170173
static void secp256k1_hmac_sha256_initialize(secp256k1_hmac_sha256 *hash, const unsigned char *key, size_t keylen) {
@@ -207,6 +210,9 @@ static void secp256k1_hmac_sha256_finalize(secp256k1_hmac_sha256 *hash, unsigned
207210
secp256k1_sha256_finalize(&hash->outer, out32);
208211
}
209212

213+
static void secp256k1_hmac_sha256_clear(secp256k1_hmac_sha256 *hash) {
214+
memclear(hash, sizeof(*hash));
215+
}
210216

211217
static void secp256k1_rfc6979_hmac_sha256_initialize(secp256k1_rfc6979_hmac_sha256 *rng, const unsigned char *key, size_t keylen) {
212218
secp256k1_hmac_sha256 hmac;
@@ -270,7 +276,11 @@ static void secp256k1_rfc6979_hmac_sha256_generate(secp256k1_rfc6979_hmac_sha256
270276
}
271277

272278
static void secp256k1_rfc6979_hmac_sha256_finalize(secp256k1_rfc6979_hmac_sha256 *rng) {
273-
memclear(rng, sizeof(secp256k1_rfc6979_hmac_sha256));
279+
(void) rng;
280+
}
281+
282+
static void secp256k1_rfc6979_hmac_sha256_clear(secp256k1_rfc6979_hmac_sha256 *rng) {
283+
memclear(rng, sizeof(*rng));
274284
}
275285

276286
#undef BE32

src/modules/ecdh/main_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static int ecdh_hash_function_sha256(unsigned char *output, const unsigned char
1919
secp256k1_sha256_write(&sha, &version, 1);
2020
secp256k1_sha256_write(&sha, x32, 32);
2121
secp256k1_sha256_finalize(&sha, output);
22+
secp256k1_sha256_clear(&sha);
2223

2324
return 1;
2425
}

src/secp256k1.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,13 @@ static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *m
456456
buffer_append(keydata, &offset, algo16, 16);
457457
}
458458
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
459-
memclear(keydata, sizeof(keydata));
460459
for (i = 0; i <= counter; i++) {
461460
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
462461
}
463462
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
463+
464+
memclear(keydata, sizeof(keydata));
465+
secp256k1_rfc6979_hmac_sha256_clear(&rng);
464466
return 1;
465467
}
466468

0 commit comments

Comments
 (0)