@@ -2638,10 +2638,18 @@ static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input,
26382638 #endif
26392639
26402640 #ifdef WOLFSSL_CIPHER_TEXT_CHECK
2641+ /* The compare in CIPHER_STATE_END compares dataSz bytes of
2642+ * ciphertext against the saved plaintext. For very small AEAD
2643+ * records (e.g. a 1-byte empty TLS 1.3 app-data record, whose
2644+ * plaintext is just the content-type byte) that makes the glitch
2645+ * check statistically unreliable. With only 1 byte compared,
2646+ * legitimate encryption collides roughly 1/256 of the time. Only
2647+ * do the check when there is a full sanityCheck buffer of
2648+ * plaintext to compare against. */
26412649 if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null &&
2642- dataSz > 0 ) {
2650+ dataSz >= sizeof(ssl->encrypt.sanityCheck) ) {
26432651 XMEMCPY(ssl->encrypt.sanityCheck, input,
2644- min(dataSz, sizeof(ssl->encrypt.sanityCheck) ));
2652+ sizeof(ssl->encrypt.sanityCheck));
26452653 }
26462654 #endif
26472655
@@ -2824,10 +2832,12 @@ static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input,
28242832 #endif
28252833
28262834 #ifdef WOLFSSL_CIPHER_TEXT_CHECK
2835+ /* Only compare when CIPHER_STATE_BEGIN prepared the check with a
2836+ * full sanityCheck buffer of plaintext (see rationale there). */
28272837 if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null &&
2828- dataSz > 0 &&
2838+ dataSz >= sizeof(ssl->encrypt.sanityCheck) &&
28292839 XMEMCMP(output, ssl->encrypt.sanityCheck,
2830- min(dataSz, sizeof(ssl->encrypt.sanityCheck) )) == 0) {
2840+ sizeof(ssl->encrypt.sanityCheck)) == 0) {
28312841
28322842 WOLFSSL_MSG("EncryptTls13 sanity check failed! Glitch?");
28332843 return ENCRYPT_ERROR;
0 commit comments