Skip to content

Commit eba09b8

Browse files
committed
Secure-clear key material on free and stack
1 parent d3d620d commit eba09b8

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
@@ -250,21 +250,32 @@ static int wp_x25519_derive(wp_EcxCtx* ctx, unsigned char* secret,
250250
ok = 0;
251251
}
252252
if (ok) {
253+
/* Constant-time: always subtract, then select based on
254+
* whether secret >= order. */
255+
unsigned char reduced[CURVE25519_KEYSIZE];
256+
int16_t carry = 0;
257+
byte gt = 0;
258+
byte eq = 0xFF;
259+
260+
for (i = CURVE25519_KEYSIZE - 1; i >= 0; i--) {
261+
carry += secret[i];
262+
carry -= wp_curve25519_order[i];
263+
reduced[i] = (unsigned char)carry;
264+
carry >>= 8;
265+
}
266+
/* Determine if secret >= order in constant time. */
253267
for (i = 0; i < CURVE25519_KEYSIZE; i++) {
254-
if (secret[i] != wp_curve25519_order[i]) {
255-
break;
256-
}
268+
gt |= eq & wp_ct_int_mask_gte(secret[i],
269+
wp_curve25519_order[i] + 1);
270+
eq &= wp_ct_byte_mask_eq(secret[i],
271+
wp_curve25519_order[i]);
257272
}
258-
if ((i < CURVE25519_KEYSIZE) &&
259-
(secret[i] > wp_curve25519_order[i])) {
260-
int16_t carry = 0;
261-
for (i = CURVE25519_KEYSIZE - 1; i >= 0; i--) {
262-
carry += secret[i];
263-
carry -= wp_curve25519_order[i];
264-
secret[i] = (unsigned char)carry;
265-
carry >>= 8;
266-
}
273+
/* Select reduced if secret >= order. */
274+
for (i = 0; i < CURVE25519_KEYSIZE; i++) {
275+
secret[i] = wp_ct_byte_mask_sel(gt | eq, reduced[i],
276+
secret[i]);
267277
}
278+
OPENSSL_cleanse(reduced, sizeof(reduced));
268279
}
269280
if (ok) {
270281
*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)