Description
MD5 and SHA-1 are vulnerable to collisions. For encryption, DES has a short key, 3DES has a small block size, and RC4 produces a biased keystream.
Potential impact
Altered files or signatures may pass verification, and a weak token authenticator can permit forgery. Exposure of encrypted data depends on the cipher and how much data it protects.
Remediation
- For file checks, compare a SHA-2 digest such as SHA-256, or a SHA-3 digest, with a trusted reference. For message authentication, use HMAC-SHA256 with a dedicated secret key and verify with
hmac.Equal. Store passwords with a dedicated password hash. - Encrypt with AES-GCM. Never reuse a nonce under the same key. With random nonces as shown here, limit each key to
2^32encryptions. - If CBC is unavoidable, use an unpredictable IV and authenticate the IV and ciphertext together with a separate key, and verify the tag before decryption (encrypt-then-MAC). Supply and rotate keys through an approved secret or key-management system.
Examples
The original 3DES function encrypts individual blocks without authentication. It can panic when the input length is not a multiple of the block size.
Before
go
package insecure
import (
"crypto/des"
"crypto/md5"
"crypto/rc4"
"crypto/sha1"
)
// MD5 for file integrity checks (weak)
func CheckFileIntegrityMD5(data []byte) [16]byte {
return md5.Sum(data)
}
// SHA-1 for token authentication (weak)
func SignTokenSHA1(token string, secret []byte) []byte {
h := sha1.New()
h.Write(secret)
h.Write([]byte(token))
return h.Sum(nil)
}
// 3DES for sensitive data encryption (weak)
func EncryptWith3DES(key, plaintext []byte) ([]byte, error) {
block, err := des.NewTripleDESCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, len(plaintext))
// Simplified example: direct blocks without a mode (also unsafe)
for i := 0; i < len(plaintext); i += block.BlockSize() {
block.Encrypt(ciphertext[i:], plaintext[i:])
}
return ciphertext, nil
}
// RC4 stream encryption (weak)
func EncryptWithRC4(key, plaintext []byte) ([]byte, error) {
c, err := rc4.NewCipher(key)
if err != nil {
return nil, err
}
dst := make([]byte, len(plaintext))
c.XORKeyStream(dst, plaintext)
return dst, nil
}
After
go
package secure
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"io"
"fmt"
)
// SHA-256 for file integrity checks
func CheckFileIntegritySHA256(data []byte) [32]byte {
return sha256.Sum256(data)
}
// HMAC-SHA256 for token authentication
func SignTokenHMAC(token string, secret []byte) []byte {
mac := hmac.New(sha256.New, secret)
mac.Write([]byte(token))
return mac.Sum(nil)
}
// Authenticated encryption using AES-GCM
func EncryptWithAESGCM(key, plaintext []byte) (nonce []byte, ciphertext []byte, err error) {
block, err := aes.NewCipher(key) // 16-, 24- or 32-byte key (AES-128/192/256)
if err != nil {
return nil, nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, nil, err
}
nonce = make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, nil, err
}
ciphertext = gcm.Seal(nil, nonce, plaintext, nil)
return nonce, ciphertext, nil
}
func DecryptWithAESGCM(key, nonce, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
if len(nonce) != gcm.NonceSize() {
return nil, fmt.Errorf("invalid nonce length")
}
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
The GCM function returns the nonce and the ciphertext with its tag. Store them together. The decryption function checks the nonce length before calling Open. Discard the result on error.