Skip to content

Commit 1c44798

Browse files
authored
Merge pull request #7416 from SparkiDev/ecc_blind_k
ECC: blind private key after use in signing
2 parents a950e90 + b7eca57 commit 1c44798

11 files changed

Lines changed: 671 additions & 96 deletions

File tree

src/internal.c

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,49 @@ static int SSL_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz,
266266
#endif /* !WOLFSSL_NO_TLS12 */
267267

268268

269+
#if !defined(NO_CERT) && defined(WOLFSSL_BLIND_PRIVATE_KEY)
270+
int wolfssl_priv_der_blind(WC_RNG* rng, DerBuffer* key, DerBuffer** mask)
271+
{
272+
int ret = 0;
273+
WC_RNG local_rng;
274+
275+
if (key != NULL) {
276+
if (*mask != NULL) {
277+
FreeDer(mask);
278+
}
279+
ret = AllocDer(mask, key->length, key->type, key->heap);
280+
if ((ret == 0) && (rng == NULL)) {
281+
if (wc_InitRng(&local_rng) != 0) {
282+
ret = RNG_FAILURE_E;
283+
}
284+
else {
285+
rng = &local_rng;
286+
}
287+
}
288+
if (ret == 0) {
289+
ret = wc_RNG_GenerateBlock(rng, (*mask)->buffer, (*mask)->length);
290+
}
291+
if (ret == 0) {
292+
xorbuf(key->buffer, (*mask)->buffer, (*mask)->length);
293+
}
294+
295+
if (rng == &local_rng) {
296+
wc_FreeRng(rng);
297+
}
298+
}
299+
300+
return ret;
301+
}
302+
303+
void wolfssl_priv_der_unblind(DerBuffer* key, DerBuffer* mask)
304+
{
305+
if (key != NULL) {
306+
xorbuf(key->buffer, mask->buffer, mask->length);
307+
}
308+
}
309+
#endif
310+
311+
269312
#if defined(WOLFSSL_RENESAS_FSPSM_TLS) || defined(WOLFSSL_RENESAS_TSIP_TLS)
270313
#include <wolfssl/wolfcrypt/port/Renesas/renesas_cmn.h>
271314
#endif
@@ -2604,11 +2647,17 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx)
26042647
ForceZero(ctx->privateKey->buffer, ctx->privateKey->length);
26052648
}
26062649
FreeDer(&ctx->privateKey);
2650+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
2651+
FreeDer(&ctx->privateKeyMask);
2652+
#endif
26072653
#ifdef WOLFSSL_DUAL_ALG_CERTS
26082654
if (ctx->altPrivateKey != NULL && ctx->altPrivateKey->buffer != NULL) {
26092655
ForceZero(ctx->altPrivateKey->buffer, ctx->altPrivateKey->length);
26102656
}
26112657
FreeDer(&ctx->altPrivateKey);
2658+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
2659+
FreeDer(&ctx->altPrivateKeyMask);
2660+
#endif
26122661
#endif /* WOLFSSL_DUAL_ALG_CERTS */
26132662
#ifdef OPENSSL_ALL
26142663
wolfSSL_EVP_PKEY_free(ctx->privateKeyPKey);
@@ -6763,14 +6812,45 @@ int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
67636812
#ifdef WOLFSSL_TLS13
67646813
ssl->buffers.certChainCnt = ctx->certChainCnt;
67656814
#endif
6815+
#ifndef WOLFSSL_BLIND_PRIVATE_KEY
67666816
ssl->buffers.key = ctx->privateKey;
6817+
#else
6818+
if (ctx->privateKey != NULL) {
6819+
AllocCopyDer(&ssl->buffers.key, ctx->privateKey->buffer,
6820+
ctx->privateKey->length, ctx->privateKey->type,
6821+
ctx->privateKey->heap);
6822+
ssl->buffers.weOwnKey = 1;
6823+
/* Blind the private key for the SSL with new random mask. */
6824+
wolfssl_priv_der_unblind(ssl->buffers.key, ctx->privateKeyMask);
6825+
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
6826+
&ssl->buffers.keyMask);
6827+
if (ret != 0) {
6828+
return ret;
6829+
}
6830+
}
6831+
#endif
67676832
ssl->buffers.keyType = ctx->privateKeyType;
67686833
ssl->buffers.keyId = ctx->privateKeyId;
67696834
ssl->buffers.keyLabel = ctx->privateKeyLabel;
67706835
ssl->buffers.keySz = ctx->privateKeySz;
67716836
ssl->buffers.keyDevId = ctx->privateKeyDevId;
67726837
#ifdef WOLFSSL_DUAL_ALG_CERTS
6773-
ssl->buffers.altKey = ctx->altPrivateKey;
6838+
#ifndef WOLFSSL_BLIND_PRIVATE_KEY
6839+
ssl->buffers.altKey = ctx->altPrivateKey;
6840+
#else
6841+
if (ctx->altPrivateKey != NULL) {
6842+
AllocCopyDer(&ssl->buffers.altkey, ctx->altPrivateKey->buffer,
6843+
ctx->altPrivateKey->length, ctx->altPrivateKey->type,
6844+
ctx->altPrivateKey->heap);
6845+
/* Blind the private key for the SSL with new random mask. */
6846+
wolfssl_priv_der_unblind(ssl->buffers.altKey, ctx->altPrivateKeyMask);
6847+
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.altKey,
6848+
&ssl->buffers.altKeyMask);
6849+
if (ret != 0) {
6850+
return ret;
6851+
}
6852+
}
6853+
#endif
67746854
ssl->buffers.altKeyType = ctx->altPrivateKeyType;
67756855
ssl->buffers.altKeyId = ctx->altPrivateKeyId;
67766856
ssl->buffers.altKeyLabel = ctx->altPrivateKeyLabel;
@@ -8518,8 +8598,14 @@ void FreeHandshakeResources(WOLFSSL* ssl)
85188598
}
85198599
#endif /* !NO_DH */
85208600

8521-
#ifndef NO_CERTS
8522-
wolfSSL_UnloadCertsKeys(ssl);
8601+
#if !defined(NO_CERTS) && !defined(OPENSSL_EXTRA) && \
8602+
!defined(WOLFSSL_WPAS_SMALL)
8603+
#ifndef WOLFSSL_POST_HANDSHAKE_AUTH
8604+
if (ssl->options.side != WOLFSSL_CLIENT_END)
8605+
#endif
8606+
{
8607+
wolfSSL_UnloadCertsKeys(ssl);
8608+
}
85238609
#endif
85248610
#ifdef HAVE_PK_CALLBACKS
85258611
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
@@ -28322,6 +28408,10 @@ int DecodeAltPrivateKey(WOLFSSL *ssl, word32* length)
2832228408
ERROR_OUT(NO_PRIVATE_KEY, exit_dapk);
2832328409
}
2832428410

28411+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
28412+
wolfssl_priv_der_unblind(ssl->buffers.altKey, ssl->buffers.altKeyMask);
28413+
#endif
28414+
2832528415
#ifdef WOLF_PRIVATE_KEY_ID
2832628416
if (ssl->buffers.altKeyDevId != INVALID_DEVID &&
2832728417
(ssl->buffers.altKeyId || ssl->buffers.altKeyLabel)) {
@@ -28724,6 +28814,16 @@ int DecodeAltPrivateKey(WOLFSSL *ssl, word32* length)
2872428814
(void)length;
2872528815

2872628816
exit_dapk:
28817+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
28818+
if (ret == 0) {
28819+
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.altKey,
28820+
&ssl->buffers.altKeyMask);
28821+
}
28822+
else {
28823+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
28824+
}
28825+
#endif
28826+
2872728827
if (ret != 0) {
2872828828
WOLFSSL_ERROR_VERBOSE(ret);
2872928829
}
@@ -32746,6 +32846,10 @@ int SendCertificateVerify(WOLFSSL* ssl)
3274632846
WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND);
3274732847
WOLFSSL_ENTER("SendCertificateVerify");
3274832848

32849+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
32850+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
32851+
#endif
32852+
3274932853
#ifdef WOLFSSL_ASYNC_IO
3275032854
if (ssl->async == NULL) {
3275132855
ssl->async = (struct WOLFSSL_ASYNC*)
@@ -32792,6 +32896,10 @@ int SendCertificateVerify(WOLFSSL* ssl)
3279232896
case TLS_ASYNC_BEGIN:
3279332897
{
3279432898
if (ssl->options.sendVerify == SEND_BLANK_CERT) {
32899+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
32900+
wolfssl_priv_der_unblind(ssl->buffers.key,
32901+
ssl->buffers.keyMask);
32902+
#endif
3279532903
return 0; /* sent blank cert, can't verify */
3279632904
}
3279732905

@@ -33196,6 +33304,15 @@ int SendCertificateVerify(WOLFSSL* ssl)
3319633304
} /* switch(ssl->options.asyncState) */
3319733305

3319833306
exit_scv:
33307+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
33308+
if (ret == 0) {
33309+
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
33310+
&ssl->buffers.keyMask);
33311+
}
33312+
else {
33313+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
33314+
}
33315+
#endif
3319933316

3320033317
WOLFSSL_LEAVE("SendCertificateVerify", ret);
3320133318
WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND);
@@ -33859,6 +33976,10 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3385933976
WOLFSSL_START(WC_FUNC_SERVER_KEY_EXCHANGE_SEND);
3386033977
WOLFSSL_ENTER("SendServerKeyExchange");
3386133978

33979+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
33980+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
33981+
#endif
33982+
3386233983
#ifdef WOLFSSL_ASYNC_IO
3386333984
if (ssl->async == NULL) {
3386433985
ssl->async = (struct WOLFSSL_ASYNC*)
@@ -35415,6 +35536,16 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3541535536

3541635537
exit_sske:
3541735538

35539+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
35540+
if (ret == 0) {
35541+
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
35542+
&ssl->buffers.keyMask);
35543+
}
35544+
else {
35545+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
35546+
}
35547+
#endif
35548+
3541835549
WOLFSSL_LEAVE("SendServerKeyExchange", ret);
3541935550
WOLFSSL_END(WC_FUNC_SERVER_KEY_EXCHANGE_SEND);
3542035551

@@ -38937,6 +39068,10 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ],
3893739068
WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_DO);
3893839069
WOLFSSL_ENTER("DoClientKeyExchange");
3893939070

39071+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
39072+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
39073+
#endif
39074+
3894039075
#ifdef WOLFSSL_ASYNC_CRYPT
3894139076
if (ssl->async == NULL) {
3894239077
ssl->async = (struct WOLFSSL_ASYNC*)
@@ -40131,6 +40266,16 @@ static int DefTicketEncCb(WOLFSSL* ssl, byte key_name[WOLFSSL_TICKET_NAME_SZ],
4013140266

4013240267
exit_dcke:
4013340268

40269+
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
40270+
if (ret == 0) {
40271+
ret = wolfssl_priv_der_blind(ssl->rng, ssl->buffers.key,
40272+
&ssl->buffers.keyMask);
40273+
}
40274+
else {
40275+
wolfssl_priv_der_unblind(ssl->buffers.key, ssl->buffers.keyMask);
40276+
}
40277+
#endif
40278+
4013440279
WOLFSSL_LEAVE("DoClientKeyExchange", ret);
4013540280
WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_DO);
4013640281
#ifdef WOLFSSL_ASYNC_CRYPT

src/pk.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11704,7 +11704,8 @@ static int wolfssl_ec_key_int_copy(ecc_key* dst, const ecc_key* src)
1170411704

1170511705
if (ret == 0) {
1170611706
/* Copy private key. */
11707-
ret = mp_copy(wc_ecc_key_get_priv(src), wc_ecc_key_get_priv(dst));
11707+
ret = mp_copy(wc_ecc_key_get_priv((ecc_key*)src),
11708+
wc_ecc_key_get_priv(dst));
1170811709
if (ret != MP_OKAY) {
1170911710
WOLFSSL_MSG("mp_copy error");
1171011711
}

0 commit comments

Comments
 (0)