Skip to content

Commit ada6bb8

Browse files
committed
Fix for CIPHER_TEXT_CHECK
1 parent b573823 commit ada6bb8

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/internal.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20673,9 +20673,16 @@ static WC_INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input,
2067320673
}
2067420674

2067520675
#ifdef WOLFSSL_CIPHER_TEXT_CHECK
20676-
if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) {
20676+
/* The compare in CIPHER_STATE_END compares sz bytes of ciphertext
20677+
* against the saved plaintext. For very small records that makes
20678+
* the glitch check statistically unreliable (e.g. a 1-byte
20679+
* compare legitimately collides roughly 1/256 of the time). Only
20680+
* dothe check when there is a full sanityCheck buffer of
20681+
* plaintext to compare against. */
20682+
if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null &&
20683+
sz >= sizeof(ssl->encrypt.sanityCheck)) {
2067720684
XMEMCPY(ssl->encrypt.sanityCheck, input,
20678-
min(sz, sizeof(ssl->encrypt.sanityCheck)));
20685+
sizeof(ssl->encrypt.sanityCheck));
2067920686
}
2068020687
#endif
2068120688

@@ -20761,9 +20768,12 @@ static WC_INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input,
2076120768
case CIPHER_STATE_END:
2076220769
{
2076320770
#ifdef WOLFSSL_CIPHER_TEXT_CHECK
20771+
/* Only compare when CIPHER_STATE_BEGIN prepared the check with a
20772+
* full sanityCheck buffer of plaintext (see rationale there). */
2076420773
if (ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null &&
20774+
sz >= sizeof(ssl->encrypt.sanityCheck) &&
2076520775
XMEMCMP(out, ssl->encrypt.sanityCheck,
20766-
min(sz, sizeof(ssl->encrypt.sanityCheck))) == 0) {
20776+
sizeof(ssl->encrypt.sanityCheck)) == 0) {
2076720777

2076820778
WOLFSSL_MSG("Encrypt sanity check failed! Glitch?");
2076920779
WOLFSSL_ERROR_VERBOSE(ENCRYPT_ERROR);

src/tls13.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)