Some checks failed
Bearer / scan (push) Successful in 9s
Go Build Multi-Platform / build (amd64, linux) (push) Successful in 42s
Go Build Multi-Platform / build (amd64, darwin) (push) Successful in 44s
Go Build Multi-Platform / build (arm, freebsd) (push) Successful in 41s
Go Build Multi-Platform / build (arm, windows) (push) Successful in 39s
Go Build Multi-Platform / build (arm64, windows) (push) Successful in 1m8s
Go Build Multi-Platform / build (wasm, js) (push) Successful in 1m6s
TinyGo Build / tinygo-build (tinygo-wasm, tinygo-wasm, reticulum-go.wasm, wasm) (pull_request) Failing after 1m2s
TinyGo Build / tinygo-build (tinygo-build, tinygo-default, reticulum-go-tinygo, ) (pull_request) Failing after 1m4s
Go Revive Lint / lint (push) Successful in 1m4s
Go Test Multi-Platform / Test (ubuntu-latest, arm64) (push) Successful in 1m24s
Run Gosec / tests (push) Successful in 1m29s
Go Test Multi-Platform / Test (ubuntu-latest, amd64) (push) Successful in 2m31s
Go Build Multi-Platform / build (amd64, freebsd) (push) Successful in 9m28s
Go Build Multi-Platform / build (arm, linux) (push) Successful in 9m28s
Go Build Multi-Platform / build (amd64, windows) (push) Successful in 9m30s
Go Build Multi-Platform / build (arm64, darwin) (push) Successful in 9m27s
Go Build Multi-Platform / build (arm64, linux) (push) Successful in 9m26s
Go Build Multi-Platform / build (arm64, freebsd) (push) Successful in 9m29s
Go Build Multi-Platform / Create Release (push) Has been skipped
113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
// SPDX-License-Identifier: 0BSD
|
|
// Copyright (c) 2024-2026 Sudo-Ivan / Quad4.io
|
|
package cryptography
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
const (
|
|
// AES256KeySize is the size of an AES-256 key in bytes.
|
|
AES256KeySize = 32 // 256 bits
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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("invalid key size: must be 32 bytes for AES-256")
|
|
}
|
|
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Generate a random IV.
|
|
iv := make([]byte, aes.BlockSize)
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Add PKCS7 padding.
|
|
padding := aes.BlockSize - len(plaintext)%aes.BlockSize
|
|
padtext := make([]byte, len(plaintext)+padding)
|
|
copy(padtext, plaintext)
|
|
for i := len(plaintext); i < len(padtext); i++ {
|
|
padtext[i] = byte(padding)
|
|
}
|
|
|
|
// Encrypt the data.
|
|
mode := cipher.NewCBCEncrypter(block, iv) // #nosec G407
|
|
ciphertext := make([]byte, len(padtext))
|
|
mode.CryptBlocks(ciphertext, padtext)
|
|
|
|
// Prepend the IV to the ciphertext.
|
|
return append(iv, ciphertext...), nil
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(ciphertext) < aes.BlockSize {
|
|
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 the block size")
|
|
}
|
|
|
|
// Decrypt the data.
|
|
mode := cipher.NewCBCDecrypter(block, iv)
|
|
plaintext := make([]byte, len(ciphertext))
|
|
mode.CryptBlocks(plaintext, ciphertext)
|
|
|
|
// Remove PKCS7 padding.
|
|
if len(plaintext) == 0 {
|
|
return nil, errors.New("invalid padding: plaintext is empty")
|
|
}
|
|
|
|
padding := int(plaintext[len(plaintext)-1])
|
|
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 the padding bytes.
|
|
for i := len(plaintext) - padding; i < len(plaintext); i++ {
|
|
if plaintext[i] != byte(padding) {
|
|
return nil, errors.New("invalid padding bytes")
|
|
}
|
|
}
|
|
|
|
return plaintext[:len(plaintext)-padding], nil
|
|
}
|