﻿# V5905\. OWASP\. An insecure encryption mode is used in security\-sensitive code\. Consider using an authenticated encryption mode\.

The analyzer has detected the use of a cryptographically weak encryption mode in security\-sensitive code\. This mode ensures only confidentiality; it does not verify data integrity\.

Vulnerabilities related to weak encryption can be classified under the following categories in the OWASP Top 10 Application Security Risks 2025:

* [A04:2025 Cryptographic Failures](https://top10.owasp.org/2025/A04_2025-Cryptographic_Failures/)
* [A07:2025 Authentication Failures](https://top10.owasp.org/2025/A07_2025-Authentication_Failures/)

An encryption mode is a method used to apply a block cipher algorithm to data\. Encryption modes are used to protect data integrity and to alter the encryption process so that the result does not reveal any information about the original data structure\.

Some encryption modes enable attackers to obtain information about the plaintext structure and/or secretly modify the ciphertext because data integrity is not verified\. These modes include [ECB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#:~:text=R%20%3D%20IV-,Electronic%20codebook%20(ECB),-edit), [CBC](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#:~:text=citation%20needed%5D-,Cipher%20block%20chaining%20(CBC),-edit), [CTR](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#:~:text=%5B29%5D-,Counter%20(CTR),-edit), [CFB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#:~:text=in%20Kerberos%20v5.-,Cipher%20feedback%20(CFB),-edit), and [OFB](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#:~:text=make%20padding%20unnecessary).-,Output%20feedback%20(OFB),-edit)\. The encryption algorithm may be strong, but the mode makes the protection ineffective\.

The example N1:

```cpp
func ProcessRequest(w http.ResponseWriter, r *http.Request) {
  data := []byte(r.FormValue("data"))

  // Check that the message length is a multiple of the block size
  ....

  key := []byte(....)
  block, _ := aes.NewCipher(key)
  ct := make([]byte, len(data))
  bs := block.BlockSize()

  // ECB: every block is encrypted independently with the same key
  for i := 0; i < len(data); i += bs {
    block.Encrypt(ct[i:i+bs], data[i:i+bs])          // <=
  }

  json.NewEncoder(w).Encode(
    map[string]string { "ct": hex.EncodeToString(ct) },
  )
}
```

In this example, the ECB mode is implemented: each plaintext block is encrypted independently, so identical plaintext blocks produce identical ciphertext blocks\. An attacker who obtains the ciphertext can see the data structure without decrypting it: recurring blocks can be used to identify recurring fragments \(identical fields, values, or fillers\)\. In addition, ciphertext blocks can be rearranged, deleted, or duplicated without the recipient noticing, since the ciphertext integrity is not verified\.

The example N2:

```cpp
func ProcessRequest(w http.ResponseWriter, r *http.Request) {
  data := []byte(r.FormValue("data"))

  // Check that the message length is a multiple of the block size
  ....

  key := []byte(....)
  block, _ := aes.NewCipher(key)

  // IV || ciphertext
  ct := make([]byte, aes.BlockSize + len(data))

  iv := ct[:aes.BlockSize]
  if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    http.Error(w, "internal error", http.StatusInternalServerError)
    return
  }

  cipher.NewCBCEncrypter(block, iv). // <=
         CryptBlocks(ct[aes.BlockSize:], data)

  json.NewEncoder(w).Encode(
    map[string]string { "ct": hex.EncodeToString(ct) },
  )
}
```

This example uses a secure encryption algorithm \(AES\) in the CBC mode that the `cipher.NewCBCEncrypter` function implements\. Compared to ECB, this mode is better: before encryption, each plaintext block is XORed with the previous ciphertext block, so identical plaintext blocks produce different ciphertext blocks, and the source data structure is hidden from an attacker\. However, CBC ensures only confidentiality; the ciphertext integrity is not verified, so it can be altered without the recipient noticing\. This enables [bit\-flipping](https://en.wikipedia.org/wiki/Bit-flipping_attack) and [padding oracle](https://en.wikipedia.org/wiki/Padding_oracle_attack) attacks\.

There are two ways to prevent potential application security issues\.

**Option N1**\. Change the encryption mode to a more secure one, such as [GCM](https://en.wikipedia.org/wiki/Galois/Counter_Mode) \(Galois/Counter Mode\):

```cpp
func ProcessRequest(w http.ResponseWriter, r *http.Request) {
  data := []byte(r.FormValue("data"))
  ....
  key := []byte(....)
  block, _ := aes.NewCipher(key)

  gcm, _ := cipher.NewGCM(block) 
  nonce := make([]byte, gcm.NonceSize())
  if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
    http.Error(w, "internal error", http.StatusInternalServerError)
    return
  }

  // nonce || ciphertext || tag
  ct := gcm.Seal(nonce, nonce, data, nil)

  json.NewEncoder(w).Encode(
    map[string]string { "ct": hex.EncodeToString(ct) },
  )
}
```

The first method uses the AES\-GCM authenticated encryption mode, created using the [`cipher.NewGCM`](https://pkg.go.dev/crypto/cipher#NewGCM) function\. Unlike the ECB, this mode does not reveal the structure of the plaintext: a unique random `nonce` is generated for each message, so identical data produces different ciphertext\. Unlike CBC, it verifies [integrity](https://pkg.go.dev/crypto/cipher#AEAD): the `AEAD.Seal` method adds an authentication tag to the ciphertext, and the `AEAD.Open` method checks it before returning the data and throws an error if the ciphertext has been altered\. Therefore, ciphertext bit\-flipping does not stay hidden, and the padding oracle attack is impossible because the mode does not use padding\. The `nonce` value is not secret and is written at the beginning of the ciphertext so that it can be used during decryption\.

The fourth argument of the Seal method is AAD \(additional authenticated data\)\. This data is not encrypted, but it is included in the tag calculation, so any tampering with it is also detected\. You can associate the ciphertext with a specific context, such as a user ID or format version, by passing that context to AAD\. When decrypting, provide the same value; otherwise, the validation will fail\. This prevents the ciphertext from being secretly inserted into a different context\. In this example, the `nil` value is passed, so such binding is not used\.

**Option N2**\. Use the "[encrypt\-then\-MAC](https://en.wikipedia.org/wiki/Authenticated_encryption#:~:text=edit-,Encrypt%2Dthen%2DMAC%20(EtM),-edit)" scheme together with the CBC, CTR, CFB, or OFB mode:

```cpp
func ProcessRequest(w http.ResponseWriter, r *http.Request) {
  data := []byte(r.FormValue("data"))

  // Check that the message length is a multiple of the block size
  ....

  encKey := []byte(....)
  macKey := []byte(....)
  block, _ := aes.NewCipher(encKey)

  // IV || ciphertext
  ct := make([]byte, aes.BlockSize + len(data))

  iv := ct[:aes.BlockSize]
  if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    ....
  }

  cipher.NewCBCEncrypter(block, iv).
         CryptBlocks(ct[aes.BlockSize:], data)
  
  mac := hmac.New(sha256.New, macKey)
  mac.Write(ct)

  // IV || ciphertext || tag
  ct = mac.Sum(ct)

  json.NewEncoder(w).Encode(
    map[string]string { "ct": hex.EncodeToString(ct) },
  )
}
```

The data is encrypted in the CBC mode using the `encKey`\. Then, an [HMAC](https://en.wikipedia.org/wiki/HMAC) authentication tag is calculated from `IV` and the ciphertext using the separate `macKey`, which is added to the end of the message \(similar to how GCM does it\)\. The secret key is used to calculate it, so without it, the modified data cannot be tagged correctly\. The recipient should verify the tag before decrypting the data\. This ensures that a tampered message is discarded before decryption and that bit\-flipping and padding oracle attacks are impossible\.

You can find more recommendations for secure data encryption on the OWASP Cheat Sheet Series pages, such as:

* [Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)
* [Cryptographic Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cryptographic_Storage_Cheat_Sheet.html)