Static IV (ARCH5114)
هذا المحتوى غير متوفر بلغتك بعد.
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.
Incorrect
Section titled “Incorrect”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}Correct
Section titled “Correct”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.