Bug 1972825

Summary: FIPS mode AES CBC CryptBlocks incorrectly re-initializes IV in file crypto/internal/boring/aes.go
Product: Red Hat Enterprise Linux 8 Reporter: Di Wu <di.wu>
Component: golangAssignee: Alejandro Sáez Morollón <asm>
Status: CLOSED ERRATA QA Contact: Edjunior Barbosa Machado <emachado>
Severity: high Docs Contact: Eva-Lotte Gebhardt <egebhard>
Priority: unspecified    
Version: 8.4CC: asm, dbenoit, deparker, emachado, tschelle, tstellar
Target Milestone: betaKeywords: Bugfix, Triaged, ZStream
Target Release: 8.5   
Hardware: All   
OS: All   
Whiteboard:
Fixed In Version: go-toolset:rhel8:8050020210706181926:8aa62369 Doc Type: Known Issue
Doc Text:
.Using CryptBlocks multiple times over the same input stream leads to incorrect encryption When Go FIPS mode is enabled, AES CBC CryptBlocks incorrectly re-initializes the initialization vector. As a result, using CryptBlocks multiple times over the input stream encrypts files incorrectly. To work around this issue, do not reinitialize IV in the `aes-cbc` interface. This action allows files to be encrypted correctly.
Story Points: ---
Clone Of:
: 1978390 1978567 (view as bug list) Environment:
Last Closed: 2021-11-09 17:40:57 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1978390, 1978557, 1978567    

Description Di Wu 2021-06-16 17:12:48 UTC
Description of problem:
When FIPS mode is enabled with GOLANG_FIPS=1 environment variable, AES CBC is broken.

In the Golang-src package, source file crypto/internal/boring/aes.go function `func (x *aesCBC) CryptBlocks(dst, src []byte)`, _goboringcrypto_EVP_CipherInit_ex is being called unnecessarily with the IV.

The OpenSSL context already got the IV initialized properly in function `NewCBCEncrypter`. The reinitializing of the IV causes the first block of each successive `CryptBlocks()` call to be XOR'ed with an incorrect value, vs chaining the block encryption correctly from the previous context state.

The fix is pretty simple, just remove the extra `C._goboringcrypto_EVP_CipherInit_ex` call from `CryptBlocks`.

```
159a160,167
> 		if C._goboringcrypto_EVP_CipherInit_ex(
> 			x.ctx,
> 			nil, nil, nil,
> 			(*C.uchar)(unsafe.Pointer(&x.iv[0])),
> 			-1) != 1 {
> 			panic("crypto/cipher: CipherInit_ex failed")
> 		}
> 		runtime.KeepAlive(x)

```

Version-Release number of selected component (if applicable):
go version go1.15.7 linux/amd64

How reproducible:
Run AES CBC EncryptBlocks with two successive calls.
Example test program
```
func TestBlobEncryptBasicBlockEncryption(t *testing.T) {
	key := []byte{0x24, 0xcd, 0x8b, 0x13, 0x37, 0xc5, 0xc1, 0xb1, 0x0, 0xbb, 0x27, 0x40, 0x4f, 0xab, 0x5f, 0x7b, 0x2d, 0x0, 0x20, 0xf5, 0x1, 0x84, 0x4, 0xbf, 0xe3, 0xbd, 0xa1, 0xc4, 0xbf, 0x61, 0x2f, 0xc5}
	iv := []byte{0x91, 0xc7, 0xa7, 0x54, 0x52, 0xef, 0x10, 0xdb, 0x91, 0xa8, 0x6c, 0xf9, 0x79, 0xd5, 0xac, 0x74}

	block, err := aes.NewCipher(key)
	require.NoError(t, err)

	blockSize := block.BlockSize()
	require.Equal(t, 16, blockSize)

	encryptor := cipher.NewCBCEncrypter(block, iv)
	require.NotNil(t, encryptor)

	encrypted := make([]byte, 32)

	// First block. 16 bytes.
	srcBlock1 := bytes.Repeat([]byte{0x01}, 16)
	encryptor.CryptBlocks(encrypted, srcBlock1)
	require.Equal(t, []byte{
		0x14, 0xb7, 0x3e, 0x2f, 0xd9, 0xe7, 0x69, 0x7e, 0xb7, 0xd2, 0xc3, 0x5b, 0x31, 0x9c, 0xf0, 0x59,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	}, encrypted)

	// Second block. 16 bytes.
	srcBlock2 := bytes.Repeat([]byte{0x02}, 16)
	encryptor.CryptBlocks(encrypted[16:], srcBlock2)
	require.Equal(t, []byte{
		0x14, 0xb7, 0x3e, 0x2f, 0xd9, 0xe7, 0x69, 0x7e, 0xb7, 0xd2, 0xc3, 0x5b, 0x31, 0x9c, 0xf0, 0x59,
		0xbb, 0xd4, 0x95, 0x25, 0x21, 0x56, 0x87, 0x3b, 0xe6, 0x22, 0xe8, 0xd0, 0x19, 0xa8, 0xed, 0xcd,
	}, encrypted)
}
```

Steps to Reproduce:
1. Turn on FIPS mode with env variable GOLANG_FIPS=1 
2. Run the encryption test
3. Verify that test passes

Actual results:
Test fails because the second call to CryptBlocks() is doing incorrect block chaining due to re-initializing the context with the IV again.

Expected results:
Test should pass

Additional info:

Comment 3 Di Wu 2021-06-28 16:12:22 UTC
Hi guys,
Is the fix we proposed the correct fix for this problem?
We would like to know whether this patch is correct (or if you guys are fixing it some other way) and pre-apply it to our internal build without needing to wait for the full RHEL release cycle.
This is a blocker for us at the moment.
Thanks!
Di

Comment 4 Derek Parker 2021-06-28 16:13:55 UTC
Hello,

Yes the fix proposed is the correct fix for this issue. We are applying it internally ourselves.

Comment 10 Di Wu 2021-07-07 17:53:00 UTC
There's also another bug when it comes to decrypting. Seems like the decryption context is also not being carried across calls correctly, see report https://bugzilla.redhat.com/show_bug.cgi?id=1979100

Comment 11 Derek Parker 2021-07-12 16:28:19 UTC
*** Bug 1979100 has been marked as a duplicate of this bug. ***

Comment 12 Eva-Lotte Gebhardt 2021-08-05 14:16:43 UTC
Hi Alejandro, 

would you please let me know what needs to be mentioned about this bugfix in the release note? 

Thank you,
Eva

Comment 13 Eva-Lotte Gebhardt 2021-08-18 15:40:36 UTC
Hi Alejandro,

sorry, I didn't see you already provided the info in the doctext field!

Here is my first draft of this RN. Please let me know what can to be improved:


.Using CryptBlocks multiple times over the same input stream leads to incorrect encryption 

When Go FIPS mode is enabled, AES CBC CryptBlocks incorrectly re-initialize IV. As a result, using CryptBlocks multiple times over the input stream encrypts files incorrectly. To work around this issue, do not reinitialize IV in the `aes-cbc` interface. As a result, files are encrypted correctly. 

Could you please let me know what "IV" is, in this context?

Thank you very much!

Comment 14 Derek Parker 2021-08-18 15:48:26 UTC
(In reply to Eva-Lotte Gebhardt from comment #13)
> Hi Alejandro,
> 
> sorry, I didn't see you already provided the info in the doctext field!
> 
> Here is my first draft of this RN. Please let me know what can to be
> improved:
> 
> 
> .Using CryptBlocks multiple times over the same input stream leads to
> incorrect encryption 
> 
> When Go FIPS mode is enabled, AES CBC CryptBlocks incorrectly re-initialize
> IV. As a result, using CryptBlocks multiple times over the input stream
> encrypts files incorrectly. To work around this issue, do not reinitialize
> IV in the `aes-cbc` interface. As a result, files are encrypted correctly. 

That sounds great.

> 
> Could you please let me know what "IV" is, in this context?

"Initialization vector"

> 
> Thank you very much!

Comment 17 errata-xmlrpc 2021-11-09 17:40:57 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: go-toolset:rhel8 security, bug fix, and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2021:4156