@@ -7497,7 +7497,7 @@ int SendKexDhReply(WOLFSSH* ssh)
74977497#ifdef WOLFSSH_SMALL_STACK
74987498 r_ptr = (byte * )WMALLOC (rSz , heap , DYNTYPE_BUFFER );
74997499 s_ptr = (byte * )WMALLOC (sSz , heap , DYNTYPE_BUFFER );
7500- if (r_ptr == NULL || r_ptr == NULL )
7500+ if (r_ptr == NULL || s_ptr == NULL )
75017501 ret = WS_MEMORY_E ;
75027502#else
75037503 byte r_s [MAX_ECC_BYTES + ECC_MAX_PAD_SZ ];
@@ -8635,13 +8635,30 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
86358635 word32 begin ;
86368636 enum wc_HashType hashId = WC_HASH_TYPE_SHA ;
86378637 int ret = WS_SUCCESS ;
8638- byte * r ;
8639- byte * s ;
8640- byte sig [139 ]; /* wc_ecc_sig_size() for a prime521 key. */
8641- word32 sigSz = sizeof (sig ), rSz , sSz ;
8638+ byte * r_ptr ;
8639+ byte * s_ptr ;
8640+ byte * sig_ptr ;
8641+ word32 rSz = ECC_MAX_SIG_SIZE / 2 ;
8642+ word32 sSz = ECC_MAX_SIG_SIZE / 2 ;
8643+ word32 sigSz = ECC_MAX_SIG_SIZE ;
86428644 byte * checkData = NULL ;
86438645 word32 checkDataSz = 0 ;
86448646
8647+ #ifdef WOLFSSH_SMALL_STACK
8648+ r_ptr = (byte * )WMALLOC (rSz , ssh -> ctx -> heap , DYNTYPE_BUFFER );
8649+ s_ptr = (byte * )WMALLOC (sSz , ssh -> ctx -> heap , DYNTYPE_BUFFER );
8650+ sig_ptr = (byte * )WMALLOC (sigSz , ssh -> ctx -> heap , DYNTYPE_BUFFER );
8651+ if (r_ptr == NULL || s_ptr == NULL || sig_ptr == NULL )
8652+ ret = WS_MEMORY_E ;
8653+ #else
8654+ byte r_s [ECC_MAX_SIG_SIZE / 2 ];
8655+ byte s_s [ECC_MAX_SIG_SIZE / 2 ];
8656+ byte sig_s [ECC_MAX_SIG_SIZE ];
8657+ r_ptr = r_s ;
8658+ s_ptr = s_s ;
8659+ sig_ptr = sig_s ;
8660+ #endif
8661+
86458662 if (ssh == NULL || output == NULL || idx == NULL || authData == NULL ||
86468663 sigStart == NULL || keySig == NULL ) {
86478664 ret = WS_BAD_ARGUMENT ;
@@ -8674,13 +8691,13 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
86748691 if (ssh -> agentEnabled ) {
86758692 if (ret == WS_SUCCESS )
86768693 ret = wolfSSH_AGENT_SignRequest (ssh , checkData , checkDataSz ,
8677- sig , & sigSz ,
8694+ sig_ptr , & sigSz ,
86788695 authData -> sf .publicKey .publicKey ,
86798696 authData -> sf .publicKey .publicKeySz , 0 );
86808697 if (ret == WS_SUCCESS ) {
86818698 c32toa (sigSz , output + begin );
86828699 begin += LENGTH_SZ ;
8683- XMEMCPY (output + begin , sig , sigSz );
8700+ XMEMCPY (output + begin , sig_ptr , sigSz );
86848701 begin += sigSz ;
86858702 }
86868703 }
@@ -8695,7 +8712,7 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
86958712 if (ret == WS_SUCCESS )
86968713 ret = wc_HashFinal (& hash , hashId , digest );
86978714 if (ret == WS_SUCCESS )
8698- ret = wc_ecc_sign_hash (digest , digestSz , sig , & sigSz ,
8715+ ret = wc_ecc_sign_hash (digest , digestSz , sig_ptr , & sigSz ,
86998716 ssh -> rng , & keySig -> ks .ecc .key );
87008717 if (ret != WS_SUCCESS ) {
87018718 WLOG (WS_LOG_DEBUG , "SUAR: Bad ECC Sign" );
@@ -8704,19 +8721,16 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
87048721 }
87058722
87068723 if (ret == WS_SUCCESS ) {
8707- rSz = sSz = sizeof (sig ) / 2 ;
8708- r = sig ;
8709- s = sig + rSz ;
8710- ret = wc_ecc_sig_to_rs (sig , sigSz , r , & rSz , s , & sSz );
8724+ ret = wc_ecc_sig_to_rs (sig_ptr , sigSz , r_ptr , & rSz , s_ptr , & sSz );
87118725 }
87128726
87138727 if (ret == WS_SUCCESS ) {
87148728 byte rPad ;
87158729 byte sPad ;
87168730
87178731 /* adds a byte of padding if needed to avoid negative values */
8718- rPad = (r [0 ] & 0x80 ) ? 1 : 0 ;
8719- sPad = (s [0 ] & 0x80 ) ? 1 : 0 ;
8732+ rPad = (r_ptr [0 ] & 0x80 ) ? 1 : 0 ;
8733+ sPad = (s_ptr [0 ] & 0x80 ) ? 1 : 0 ;
87208734 c32toa (rSz + rPad + sSz + sPad +
87218735 cannedKeyAlgoEcc256NamesSz + LENGTH_SZ * 4 ,
87228736 output + begin );
@@ -8738,7 +8752,7 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
87388752 if (rPad )
87398753 output [begin ++ ] = 0 ;
87408754
8741- WMEMCPY (output + begin , r , rSz );
8755+ WMEMCPY (output + begin , r_ptr , rSz );
87428756 begin += rSz ;
87438757
87448758 c32toa (sSz + sPad , output + begin );
@@ -8747,7 +8761,7 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
87478761 if (sPad )
87488762 output [begin ++ ] = 0 ;
87498763
8750- WMEMCPY (output + begin , s , sSz );
8764+ WMEMCPY (output + begin , s_ptr , sSz );
87518765 begin += sSz ;
87528766 }
87538767 }
@@ -8760,6 +8774,14 @@ static int BuildUserAuthRequestEcc(WOLFSSH* ssh,
87608774 WFREE (checkData , ssh -> ctx -> heap , DYNTYPE_TEMP );
87618775 }
87628776
8777+ #ifdef WOLFSSH_SMALL_STACK
8778+ if (r_ptr )
8779+ WFREE (r_ptr , ssh -> ctx -> heap , DYNTYPE_BUFFER );
8780+ if (s_ptr )
8781+ WFREE (s_ptr , ssh -> ctx -> heap , DYNTYPE_BUFFER );
8782+ if (sig_ptr )
8783+ WFREE (sig_ptr , ssh -> ctx -> heap , DYNTYPE_BUFFER );
8784+ #endif
87638785 return ret ;
87648786}
87658787#endif
0 commit comments