Zum Inhalt springen

Static IV (ARCH5114)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key go.security.static-iv · Severity error · Category security · CWE CWE-329

Using a constant or zero IV/nonce makes identical plaintexts under the same key produce identical ciphertext, destroying the semantic security of CBC/CTR/GCM.

func encrypt(block cipher.Block, plaintext []byte) []byte {
iv := make([]byte, block.BlockSize()) // always zero
stream := cipher.NewCTR(block, iv)
out := make([]byte, len(plaintext))
stream.XORKeyStream(out, plaintext)
return out
}
func encrypt(block cipher.Block, plaintext []byte) ([]byte, error) {
iv := make([]byte, block.BlockSize())
if _, err := rand.Read(iv); err != nil {
return nil, err
}
stream := cipher.NewCTR(block, iv)
out := make([]byte, len(plaintext))
stream.XORKeyStream(out, plaintext)
return append(iv, out...), nil // prepend IV
}

Generate a unique, random IV/nonce with crypto/rand for every encryption and store it alongside the ciphertext.