use AES-256-CBC only
This commit is contained in:
@@ -9,54 +9,24 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// AES key sizes in bytes
|
||||
AES128KeySize = 16 // 128 bits
|
||||
AES192KeySize = 24 // 192 bits
|
||||
// AES256KeySize is the size of an AES-256 key in bytes.
|
||||
AES256KeySize = 32 // 256 bits
|
||||
|
||||
// Default to AES-256
|
||||
DefaultKeySize = AES256KeySize
|
||||
)
|
||||
|
||||
// GenerateAESKey generates a random AES key of the specified size
|
||||
func GenerateAESKey(keySize int) ([]byte, error) {
|
||||
if keySize != AES128KeySize && keySize != AES192KeySize && keySize != AES256KeySize {
|
||||
return nil, errors.New("invalid key size: must be 16, 24, or 32 bytes")
|
||||
}
|
||||
|
||||
key := make([]byte, keySize)
|
||||
// GenerateAES256Key generates a random AES-256 key.
|
||||
func GenerateAES256Key() ([]byte, error) {
|
||||
key := make([]byte, AES256KeySize)
|
||||
if _, err := io.ReadFull(rand.Reader, key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// GenerateAES256Key generates a random AES-256 key (default)
|
||||
func GenerateAES256Key() ([]byte, error) {
|
||||
return GenerateAESKey(AES256KeySize)
|
||||
}
|
||||
|
||||
// EncryptAES256CBC encrypts data using AES-256 in CBC mode
|
||||
// EncryptAES256CBC encrypts data using AES-256 in CBC mode.
|
||||
// The IV is prepended to the ciphertext.
|
||||
func EncryptAES256CBC(key, plaintext []byte) ([]byte, error) {
|
||||
if len(key) != AES256KeySize {
|
||||
return nil, errors.New("key must be 32 bytes for AES-256")
|
||||
}
|
||||
return EncryptAESCBC(key, plaintext)
|
||||
}
|
||||
|
||||
// DecryptAES256CBC decrypts data using AES-256 in CBC mode
|
||||
func DecryptAES256CBC(key, ciphertext []byte) ([]byte, error) {
|
||||
if len(key) != AES256KeySize {
|
||||
return nil, errors.New("key must be 32 bytes for AES-256")
|
||||
}
|
||||
return DecryptAESCBC(key, ciphertext)
|
||||
}
|
||||
|
||||
// EncryptAESCBC encrypts data using AES in CBC mode (accepts any valid AES key size)
|
||||
func EncryptAESCBC(key, plaintext []byte) ([]byte, error) {
|
||||
// Validate key size
|
||||
if len(key) != AES128KeySize && len(key) != AES192KeySize && len(key) != AES256KeySize {
|
||||
return nil, errors.New("invalid key size: must be 16, 24, or 32 bytes")
|
||||
return nil, errors.New("invalid key size: must be 32 bytes for AES-256")
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
@@ -64,13 +34,13 @@ func EncryptAESCBC(key, plaintext []byte) ([]byte, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Generate IV
|
||||
// Generate a random IV.
|
||||
iv := make([]byte, aes.BlockSize)
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add PKCS7 padding
|
||||
// Add PKCS7 padding.
|
||||
padding := aes.BlockSize - len(plaintext)%aes.BlockSize
|
||||
padtext := make([]byte, len(plaintext)+padding)
|
||||
copy(padtext, plaintext)
|
||||
@@ -78,19 +48,20 @@ func EncryptAESCBC(key, plaintext []byte) ([]byte, error) {
|
||||
padtext[i] = byte(padding)
|
||||
}
|
||||
|
||||
// Encrypt
|
||||
mode := cipher.NewCBCEncrypter(block, iv) // #nosec G407
|
||||
// Encrypt the data.
|
||||
mode := cipher.NewCBCEncrypter(block, iv)
|
||||
ciphertext := make([]byte, len(padtext))
|
||||
mode.CryptBlocks(ciphertext, padtext)
|
||||
|
||||
// Prepend the IV to the ciphertext.
|
||||
return append(iv, ciphertext...), nil
|
||||
}
|
||||
|
||||
// DecryptAESCBC decrypts data using AES in CBC mode (accepts any valid AES key size)
|
||||
func DecryptAESCBC(key, ciphertext []byte) ([]byte, error) {
|
||||
// Validate key size
|
||||
if len(key) != AES128KeySize && len(key) != AES192KeySize && len(key) != AES256KeySize {
|
||||
return nil, errors.New("invalid key size: must be 16, 24, or 32 bytes")
|
||||
// DecryptAES256CBC decrypts data using AES-256 in CBC mode.
|
||||
// It assumes the IV is prepended to the ciphertext.
|
||||
func DecryptAES256CBC(key, ciphertext []byte) ([]byte, error) {
|
||||
if len(key) != AES256KeySize {
|
||||
return nil, errors.New("invalid key size: must be 32 bytes for AES-256")
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(key)
|
||||
@@ -99,34 +70,39 @@ func DecryptAESCBC(key, ciphertext []byte) ([]byte, error) {
|
||||
}
|
||||
|
||||
if len(ciphertext) < aes.BlockSize {
|
||||
return nil, errors.New("ciphertext too short")
|
||||
return nil, errors.New("ciphertext is too short")
|
||||
}
|
||||
|
||||
// Extract the IV from the beginning of the ciphertext.
|
||||
iv := ciphertext[:aes.BlockSize]
|
||||
ciphertext = ciphertext[aes.BlockSize:]
|
||||
|
||||
if len(ciphertext)%aes.BlockSize != 0 {
|
||||
return nil, errors.New("ciphertext is not a multiple of block size")
|
||||
return nil, errors.New("ciphertext is not a multiple of the block size")
|
||||
}
|
||||
|
||||
// Decrypt the data.
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
plaintext := make([]byte, len(ciphertext))
|
||||
mode.CryptBlocks(plaintext, ciphertext)
|
||||
|
||||
// Remove PKCS7 padding
|
||||
// Remove PKCS7 padding.
|
||||
if len(plaintext) == 0 {
|
||||
return nil, errors.New("invalid padding: empty plaintext")
|
||||
return nil, errors.New("invalid padding: plaintext is empty")
|
||||
}
|
||||
|
||||
padding := int(plaintext[len(plaintext)-1])
|
||||
if padding == 0 || padding > aes.BlockSize || padding > len(plaintext) {
|
||||
return nil, errors.New("invalid PKCS7 padding")
|
||||
if padding > aes.BlockSize || padding == 0 {
|
||||
return nil, errors.New("invalid padding size")
|
||||
}
|
||||
if len(plaintext) < padding {
|
||||
return nil, errors.New("invalid padding: padding size is larger than plaintext")
|
||||
}
|
||||
|
||||
// Verify all padding bytes are correct
|
||||
// Verify the padding bytes.
|
||||
for i := len(plaintext) - padding; i < len(plaintext); i++ {
|
||||
if plaintext[i] != byte(padding) {
|
||||
return nil, errors.New("invalid PKCS7 padding")
|
||||
return nil, errors.New("invalid padding bytes")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user