forked from wolfSSL/wolfssl
tls: refactoring MakeTlsMasterSecret to reduce stack usage:
--- variable seed moved to the heap (up to 64 bytes saved)
This commit is contained in:
31
src/tls.c
31
src/tls.c
@ -452,29 +452,44 @@ int DeriveTlsKeys(CYASSL* ssl)
|
|||||||
int MakeTlsMasterSecret(CYASSL* ssl)
|
int MakeTlsMasterSecret(CYASSL* ssl)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
byte* seed;
|
||||||
|
#else
|
||||||
byte seed[SEED_LEN];
|
byte seed[SEED_LEN];
|
||||||
|
#endif
|
||||||
XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
|
|
||||||
XMEMCPY(&seed[RAN_LEN], ssl->arrays->serverRandom, RAN_LEN);
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
seed = (byte*)XMALLOC(SEED_LEN, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
if (seed == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
|
||||||
|
XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
|
||||||
|
|
||||||
ret = PRF(ssl->arrays->masterSecret, SECRET_LEN,
|
ret = PRF(ssl->arrays->masterSecret, SECRET_LEN,
|
||||||
ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
|
ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
|
||||||
master_label, MASTER_LABEL_SZ,
|
master_label, MASTER_LABEL_SZ,
|
||||||
seed, SEED_LEN, IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm);
|
seed, SEED_LEN, IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm);
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
#ifdef SHOW_SECRETS
|
if (ret == 0) {
|
||||||
{
|
#ifdef SHOW_SECRETS
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
printf("master secret: ");
|
printf("master secret: ");
|
||||||
for (i = 0; i < SECRET_LEN; i++)
|
for (i = 0; i < SECRET_LEN; i++)
|
||||||
printf("%02x", ssl->arrays->masterSecret[i]);
|
printf("%02x", ssl->arrays->masterSecret[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ret = DeriveTlsKeys(ssl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(seed, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return DeriveTlsKeys(ssl);
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user