gmessages/libgm/crypto/aesgcm.go

136 lines
3.4 KiB
Go
Raw Permalink Normal View History

2023-06-30 09:54:08 +00:00
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
2023-07-17 14:56:34 +00:00
"encoding/binary"
2023-06-30 09:54:08 +00:00
"fmt"
"math"
)
2023-07-17 14:56:34 +00:00
type AESGCMHelper struct {
2023-06-30 09:54:08 +00:00
key []byte
2023-07-17 14:56:34 +00:00
gcm cipher.AEAD
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
func NewAESGCMHelper(key []byte) (*AESGCMHelper, error) {
2023-06-30 09:54:08 +00:00
if len(key) != 32 {
return nil, fmt.Errorf("unsupported AES key length (got=%d expected=32)", len(key))
}
2023-07-17 14:56:34 +00:00
block, err := aes.NewCipher(key)
2023-06-30 09:54:08 +00:00
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
2023-07-17 14:56:34 +00:00
return &AESGCMHelper{key: key, gcm: gcm}, nil
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
func (c *AESGCMHelper) encryptChunk(data []byte, aad []byte) []byte {
nonce := make([]byte, c.gcm.NonceSize(), c.gcm.NonceSize()+len(data))
_, err := rand.Read(nonce)
2023-06-30 09:54:08 +00:00
if err != nil {
2023-07-17 14:56:34 +00:00
panic(fmt.Errorf("out of randomness: %w", err))
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
// Pass nonce as the dest, so we have it pre-appended to the output
return c.gcm.Seal(nonce, nonce, data, aad)
}
2023-06-30 09:54:08 +00:00
2023-07-17 14:56:34 +00:00
func (c *AESGCMHelper) decryptChunk(data []byte, aad []byte) ([]byte, error) {
if len(data) < c.gcm.NonceSize() {
2023-06-30 09:54:08 +00:00
return nil, fmt.Errorf("invalid encrypted data length (got=%d)", len(data))
}
2023-07-17 14:56:34 +00:00
nonce := data[:c.gcm.NonceSize()]
ciphertext := data[c.gcm.NonceSize():]
2023-06-30 09:54:08 +00:00
2023-07-17 14:56:34 +00:00
decrypted, err := c.gcm.Open(nil, nonce, ciphertext, aad)
2023-06-30 09:54:08 +00:00
if err != nil {
return nil, err
}
return decrypted, nil
}
2023-07-17 14:56:34 +00:00
const outgoingRawChunkSize = 1 << 15
2023-06-30 09:54:08 +00:00
2023-07-17 14:56:34 +00:00
func (c *AESGCMHelper) EncryptData(data []byte) ([]byte, error) {
chunkOverhead := c.gcm.NonceSize() + c.gcm.Overhead()
chunkSize := outgoingRawChunkSize - chunkOverhead
chunkCount := int(math.Ceil(float64(len(data)) / float64(chunkSize)))
encrypted := make([]byte, 2, 2+len(data)+28*chunkCount)
encrypted[0] = 0
encrypted[1] = byte(math.Log2(float64(outgoingRawChunkSize)))
var chunkIndex uint32
2023-06-30 09:54:08 +00:00
for i := 0; i < len(data); i += chunkSize {
2023-07-17 14:56:34 +00:00
isLastChunk := false
if i+chunkSize >= len(data) {
2023-06-30 09:54:08 +00:00
chunkSize = len(data) - i
2023-07-17 14:56:34 +00:00
isLastChunk = true
2023-06-30 09:54:08 +00:00
}
chunk := make([]byte, chunkSize)
copy(chunk, data[i:i+chunkSize])
2023-07-17 14:56:34 +00:00
aad := c.calculateAAD(chunkIndex, isLastChunk)
encrypted = append(encrypted, c.encryptChunk(data[i:i+chunkSize], aad)...)
2023-06-30 09:54:08 +00:00
chunkIndex++
}
2023-07-17 14:56:34 +00:00
return encrypted, nil
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
func (c *AESGCMHelper) DecryptData(encryptedData []byte) ([]byte, error) {
if len(encryptedData) == 0 || len(c.key) != 32 {
2023-06-30 09:54:08 +00:00
return encryptedData, nil
}
if encryptedData[0] != 0 {
return nil, fmt.Errorf("invalid first-byte header signature (got=%o , expected=%o)", encryptedData[0], 0)
}
chunkSize := 1 << encryptedData[1]
encryptedData = encryptedData[2:]
2023-07-17 14:56:34 +00:00
var chunkIndex uint32
chunkCount := int(math.Ceil(float64(len(encryptedData)) / float64(chunkSize)))
decryptedData := make([]byte, 0, len(encryptedData)-28*chunkCount)
2023-06-30 09:54:08 +00:00
for i := 0; i < len(encryptedData); i += chunkSize {
2023-07-17 14:56:34 +00:00
isLastChunk := false
if i+chunkSize >= len(encryptedData) {
2023-06-30 09:54:08 +00:00
chunkSize = len(encryptedData) - i
2023-07-17 14:56:34 +00:00
isLastChunk = true
2023-06-30 09:54:08 +00:00
}
chunk := make([]byte, chunkSize)
copy(chunk, encryptedData[i:i+chunkSize])
2023-07-17 14:56:34 +00:00
aad := c.calculateAAD(chunkIndex, isLastChunk)
decryptedChunk, err := c.decryptChunk(chunk, aad)
if err != nil {
return nil, fmt.Errorf("failed to decrypt chunk #%d: %w", chunkIndex+1, err)
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
decryptedData = append(decryptedData, decryptedChunk...)
chunkIndex++
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
return decryptedData, nil
2023-06-30 09:54:08 +00:00
}
2023-07-17 14:56:34 +00:00
func (c *AESGCMHelper) calculateAAD(index uint32, isLastChunk bool) []byte {
2023-06-30 09:54:08 +00:00
aad := make([]byte, 5)
2023-07-17 14:56:34 +00:00
binary.BigEndian.PutUint32(aad[1:5], index)
if isLastChunk {
2023-06-30 09:54:08 +00:00
aad[0] = 1
}
return aad
2023-06-30 09:55:49 +00:00
}