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:
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, CBC, CTR, CFB, and OFB. The encryption algorithm may be strong, but the mode makes the protection ineffective.
The example N1:
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:
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 and padding oracle 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 (Galois/Counter Mode):
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 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: 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" scheme together with the CBC, CTR, CFB, or OFB mode:
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 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:
This diagnostic rule is classified as:
|