@@ -36,7 +36,7 @@ static int tls13_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
3636 return OSSL_RECORD_RETURN_FATAL ;
3737 }
3838
39- rl -> nonce = OPENSSL_zalloc (ivlen );
39+ rl -> nonce = OPENSSL_malloc (ivlen );
4040 if (rl -> nonce == NULL ) {
4141 ERR_raise (ERR_LIB_SSL , ERR_R_MALLOC_FAILURE );
4242 return OSSL_RECORD_RETURN_FATAL ;
@@ -89,10 +89,10 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
8989 size_t n_recs , int sending , SSL_MAC_BUF * mac ,
9090 size_t macsize )
9191{
92- EVP_CIPHER_CTX * ctx ;
92+ EVP_CIPHER_CTX * enc_ctx ;
9393 unsigned char recheader [SSL3_RT_HEADER_LENGTH ];
9494 unsigned char tag [EVP_MAX_MD_SIZE ];
95- size_t ivlen , offset , loop , hdrlen , taglen ;
95+ size_t nonce_len , offset , loop , hdrlen , taglen ;
9696 unsigned char * staticiv ;
9797 unsigned char * nonce ;
9898 unsigned char * seq = rl -> sequence ;
@@ -109,11 +109,11 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
109109 return 0 ;
110110 }
111111
112- ctx = rl -> enc_ctx ;
112+ enc_ctx = rl -> enc_ctx ; /* enc_ctx is ignored when rl->mac_ctx != NULL */
113113 staticiv = rl -> iv ;
114114 nonce = rl -> nonce ;
115115
116- if (ctx == NULL && rl -> mac_ctx == NULL ) {
116+ if (enc_ctx == NULL && rl -> mac_ctx == NULL ) {
117117 RLAYERfatal (rl , SSL_AD_INTERNAL_ERROR , ERR_R_INTERNAL_ERROR );
118118 return 0 ;
119119 }
@@ -130,11 +130,11 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
130130 return 1 ;
131131 }
132132
133- /* For Integrity Only, ivlen is same as MAC size */
133+ /* For integrity-only ciphers, nonce_len is same as MAC size */
134134 if (rl -> mac_ctx != NULL )
135- ivlen = EVP_MAC_CTX_get_mac_size (rl -> mac_ctx );
135+ nonce_len = EVP_MAC_CTX_get_mac_size (rl -> mac_ctx );
136136 else
137- ivlen = EVP_CIPHER_CTX_get_iv_length (ctx );
137+ nonce_len = EVP_CIPHER_CTX_get_iv_length (enc_ctx );
138138
139139 if (!sending ) {
140140 /*
@@ -146,13 +146,13 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
146146 rec -> length -= rl -> taglen ;
147147 }
148148
149- /* Set up IV */
150- if (ivlen < SEQ_NUM_SIZE ) {
149+ /* Set up nonce: part of static IV followed by sequence number */
150+ if (nonce_len < SEQ_NUM_SIZE ) {
151151 /* Should not happen */
152152 RLAYERfatal (rl , SSL_AD_INTERNAL_ERROR , ERR_R_INTERNAL_ERROR );
153153 return 0 ;
154154 }
155- offset = ivlen - SEQ_NUM_SIZE ;
155+ offset = nonce_len - SEQ_NUM_SIZE ;
156156 memcpy (nonce , staticiv , offset );
157157 for (loop = 0 ; loop < SEQ_NUM_SIZE ; loop ++ )
158158 nonce [offset + loop ] = staticiv [offset + loop ] ^ seq [loop ];
@@ -179,7 +179,7 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
179179 int ret = 0 ;
180180
181181 if ((mac_ctx = EVP_MAC_CTX_dup (rl -> mac_ctx )) == NULL
182- || !EVP_MAC_update (mac_ctx , nonce , ivlen )
182+ || !EVP_MAC_update (mac_ctx , nonce , nonce_len )
183183 || !EVP_MAC_update (mac_ctx , recheader , sizeof (recheader ))
184184 || !EVP_MAC_update (mac_ctx , rec -> input , rec -> length )
185185 || !EVP_MAC_final (mac_ctx , tag , & taglen , rl -> taglen )) {
@@ -200,15 +200,15 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
200200 return ret ;
201201 }
202202
203- cipher = EVP_CIPHER_CTX_get0_cipher (ctx );
203+ cipher = EVP_CIPHER_CTX_get0_cipher (enc_ctx );
204204 if (cipher == NULL ) {
205205 RLAYERfatal (rl , SSL_AD_INTERNAL_ERROR , ERR_R_INTERNAL_ERROR );
206206 return 0 ;
207207 }
208208 mode = EVP_CIPHER_get_mode (cipher );
209209
210- if (EVP_CipherInit_ex (ctx , NULL , NULL , NULL , nonce , sending ) <= 0
211- || (!sending && EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_AEAD_SET_TAG ,
210+ if (EVP_CipherInit_ex (enc_ctx , NULL , NULL , NULL , nonce , sending ) <= 0
211+ || (!sending && EVP_CIPHER_CTX_ctrl (enc_ctx , EVP_CTRL_AEAD_SET_TAG ,
212212 rl -> taglen ,
213213 rec -> data + rec -> length ) <= 0 )) {
214214 RLAYERfatal (rl , SSL_AD_INTERNAL_ERROR , ERR_R_INTERNAL_ERROR );
@@ -220,19 +220,19 @@ static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
220220 * any AAD.
221221 */
222222 if ((mode == EVP_CIPH_CCM_MODE
223- && EVP_CipherUpdate (ctx , NULL , & lenu , NULL ,
223+ && EVP_CipherUpdate (enc_ctx , NULL , & lenu , NULL ,
224224 (unsigned int )rec -> length ) <= 0 )
225- || EVP_CipherUpdate (ctx , NULL , & lenu , recheader ,
225+ || EVP_CipherUpdate (enc_ctx , NULL , & lenu , recheader ,
226226 sizeof (recheader )) <= 0
227- || EVP_CipherUpdate (ctx , rec -> data , & lenu , rec -> input ,
227+ || EVP_CipherUpdate (enc_ctx , rec -> data , & lenu , rec -> input ,
228228 (unsigned int )rec -> length ) <= 0
229- || EVP_CipherFinal_ex (ctx , rec -> data + lenu , & lenf ) <= 0
229+ || EVP_CipherFinal_ex (enc_ctx , rec -> data + lenu , & lenf ) <= 0
230230 || (size_t )(lenu + lenf ) != rec -> length ) {
231231 return 0 ;
232232 }
233233 if (sending ) {
234234 /* Add the tag */
235- if (EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_AEAD_GET_TAG , rl -> taglen ,
235+ if (EVP_CIPHER_CTX_ctrl (enc_ctx , EVP_CTRL_AEAD_GET_TAG , rl -> taglen ,
236236 rec -> data + rec -> length ) <= 0 ) {
237237 RLAYERfatal (rl , SSL_AD_INTERNAL_ERROR , ERR_R_INTERNAL_ERROR );
238238 return 0 ;
0 commit comments