Skip to content

Commit f3fc934

Browse files
committed
Secure-clear key material on free and stack
1 parent 3ec0431 commit f3fc934

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/wp_dec_epki2pki.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static int wp_epki2pki_decode(wp_Epki2Pki* ctx, OSSL_CORE_BIO* coreBio,
261261
}
262262

263263
/* Dispose of the EPKI data buffer. */
264-
OPENSSL_free(data);
264+
OPENSSL_clear_free(data, len);
265265

266266
OPENSSL_cleanse(password, sizeof(password));
267267

src/wp_ecx_exch.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,21 +248,32 @@ static int wp_x25519_derive(wp_EcxCtx* ctx, unsigned char* secret,
248248
ok = 0;
249249
}
250250
if (ok) {
251+
/* Constant-time: always subtract, then select based on
252+
* whether secret >= order. */
253+
unsigned char reduced[CURVE25519_KEYSIZE];
254+
int16_t carry = 0;
255+
byte gt = 0;
256+
byte eq = 0xFF;
257+
258+
for (i = CURVE25519_KEYSIZE - 1; i >= 0; i--) {
259+
carry += secret[i];
260+
carry -= wp_curve25519_order[i];
261+
reduced[i] = (unsigned char)carry;
262+
carry >>= 8;
263+
}
264+
/* Determine if secret >= order in constant time. */
251265
for (i = 0; i < CURVE25519_KEYSIZE; i++) {
252-
if (secret[i] != wp_curve25519_order[i]) {
253-
break;
254-
}
266+
gt |= eq & wp_ct_int_mask_gte(secret[i],
267+
wp_curve25519_order[i] + 1);
268+
eq &= wp_ct_byte_mask_eq(secret[i],
269+
wp_curve25519_order[i]);
255270
}
256-
if ((i < CURVE25519_KEYSIZE) &&
257-
(secret[i] > wp_curve25519_order[i])) {
258-
int16_t carry = 0;
259-
for (i = CURVE25519_KEYSIZE - 1; i >= 0; i--) {
260-
carry += secret[i];
261-
carry -= wp_curve25519_order[i];
262-
secret[i] = (unsigned char)carry;
263-
carry >>= 8;
264-
}
271+
/* Select reduced if secret >= order. */
272+
for (i = 0; i < CURVE25519_KEYSIZE; i++) {
273+
secret[i] = wp_ct_byte_mask_sel(gt | eq, reduced[i],
274+
secret[i]);
265275
}
276+
OPENSSL_cleanse(reduced, sizeof(reduced));
266277
}
267278
if (ok) {
268279
*secLen = len;

src/wp_gmac.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ static wp_GmacCtx* wp_gmac_new(WOLFPROV_CTX* provCtx)
9393
static void wp_gmac_free(wp_GmacCtx* macCtx)
9494
{
9595
if (macCtx != NULL) {
96+
wc_AesFree(&macCtx->gmac.aes);
9697
OPENSSL_cleanse(macCtx->key, macCtx->keyLen);
9798
OPENSSL_cleanse(macCtx->iv, macCtx->ivLen);
9899
OPENSSL_clear_free(macCtx->data, macCtx->dataLen);
99-
OPENSSL_free(macCtx);
100+
OPENSSL_clear_free(macCtx, sizeof(*macCtx));
100101
}
101102
}
102103

0 commit comments

Comments
 (0)