No commit suggestions generated

This commit is contained in:
Prad Nukala
2025-10-09 15:10:39 -04:00
commit a934caa7d3
323 changed files with 98121 additions and 0 deletions
+246
View File
@@ -0,0 +1,246 @@
package daed
import (
"crypto/aes"
"crypto/cipher"
"crypto/subtle"
"errors"
"fmt"
"math"
// Placeholder for internal crypto/cipher allowlist, please ignore.
// Placeholder for internal crypto/subtle allowlist, please ignore.
)
// AESSIV is an implementation of AES-SIV-CMAC as defined in
// https://tools.ietf.org/html/rfc5297.
//
// AESSIV implements a deterministic encryption with associated data (i.e. the
// DeterministicAEAD interface). Hence the implementation below is restricted
// to one AD component.
//
// Security Note:
//
// Chatterjee, Menezes and Sarkar analyze AES-SIV in Section 5.1 of
// https://www.math.uwaterloo.ca/~ajmeneze/publications/tightness.pdf
//
// Their analysis shows that AES-SIV is susceptible to an attack in
// a multi-user setting. Concretely, if an attacker knows the encryption
// of a message m encrypted and authenticated with k different keys,
// then it is possible to find one of the MAC keys in time 2^b / k
// where b is the size of the MAC key. A consequence of this attack
// is that 128-bit MAC keys give unsufficient security.
// Since 192-bit AES keys are not supported by tink for voodoo reasons
// and RFC 5297 only supports same size encryption and MAC keys this
// implies that keys must be 64 bytes (2*256 bits) long.
type AESSIV struct {
Cipher cipher.Block
K1 []byte
K2 []byte
CmacK1 []byte
CmacK2 []byte
}
const (
// AESSIVKeySize is the key size in bytes.
AESSIVKeySize = 64
intSize = 32 << (^uint(0) >> 63) // 32 or 64
maxInt = 1<<(intSize-1) - 1
)
// NewAESSIV returns an AESSIV instance.
func NewAESSIV(key []byte) (*AESSIV, error) {
if len(key) != AESSIVKeySize {
return nil, fmt.Errorf("aes_siv: invalid key size %d", len(key))
}
k1 := key[:32]
k2 := key[32:]
c, err := aes.NewCipher(k1)
if err != nil {
return nil, fmt.Errorf("aes_siv: aes.NewCipher(%s) failed, %v", k1, err)
}
block := make([]byte, aes.BlockSize)
c.Encrypt(block, block)
multiplyByX(block)
cmacK1 := make([]byte, aes.BlockSize)
copy(cmacK1, block)
multiplyByX(block)
cmacK2 := make([]byte, aes.BlockSize)
copy(cmacK2, block)
return &AESSIV{
K1: k1,
K2: k2,
CmacK1: cmacK1,
CmacK2: cmacK2,
Cipher: c,
}, nil
}
// multiplyByX multiplies an element in GF(2^128) by its generator.
//
// This function is incorrectly named "doubling" in section 2.3 of RFC 5297.
func multiplyByX(block []byte) {
carry := int(block[0] >> 7)
for i := 0; i < aes.BlockSize-1; i++ {
block[i] = (block[i] << 1) | (block[i+1] >> 7)
}
block[aes.BlockSize-1] = (block[aes.BlockSize-1] << 1) ^ byte(
subtle.ConstantTimeSelect(carry, 0x87, 0x00),
)
}
// EncryptDeterministically deterministically encrypts plaintext with associatedData.
func (asc *AESSIV) EncryptDeterministically(plaintext, associatedData []byte) ([]byte, error) {
if len(plaintext) > maxInt-aes.BlockSize {
return nil, fmt.Errorf("aes_siv: plaintext too long")
}
siv := make([]byte, aes.BlockSize)
asc.s2v(plaintext, associatedData, siv)
ct := make([]byte, len(plaintext)+aes.BlockSize)
copy(ct[:aes.BlockSize], siv)
if err := asc.ctrCrypt(siv, plaintext, ct[aes.BlockSize:]); err != nil {
return nil, err
}
return ct, nil
}
// DecryptDeterministically deterministically decrypts ciphertext with associatedData.
func (asc *AESSIV) DecryptDeterministically(ciphertext, associatedData []byte) ([]byte, error) {
if len(ciphertext) < aes.BlockSize {
return nil, errors.New("aes_siv: ciphertext is too short")
}
pt := make([]byte, len(ciphertext)-aes.BlockSize)
siv := ciphertext[:aes.BlockSize]
asc.ctrCrypt(siv, ciphertext[aes.BlockSize:], pt)
s2v := make([]byte, aes.BlockSize)
asc.s2v(pt, associatedData, s2v)
diff := byte(0)
for i := 0; i < aes.BlockSize; i++ {
diff |= siv[i] ^ s2v[i]
}
if diff != 0 {
return nil, errors.New("aes_siv: invalid ciphertext")
}
return pt, nil
}
// ctrCrypt encrypts (or decrypts) the bytes in in using an SIV and writes the
// result to out.
func (asc *AESSIV) ctrCrypt(siv, in, out []byte) error {
// siv might be used outside of ctrCrypt(), so making a copy of it.
iv := make([]byte, aes.BlockSize)
copy(iv, siv)
iv[8] &= 0x7f
iv[12] &= 0x7f
c, err := aes.NewCipher(asc.K2)
if err != nil {
return fmt.Errorf("aes_siv: aes.NewCipher(%s) failed, %v", asc.K2, err)
}
steam := cipher.NewCTR(c, iv)
steam.XORKeyStream(out, in)
return nil
}
// s2v is a Pseudo-Random Function (PRF) construction:
// https://tools.ietf.org/html/rfc5297.
func (asc *AESSIV) s2v(msg, ad, siv []byte) {
block := make([]byte, aes.BlockSize)
asc.cmac(block, block)
multiplyByX(block)
adMac := make([]byte, aes.BlockSize)
asc.cmac(ad, adMac)
xorBlock(adMac, block)
if len(msg) >= aes.BlockSize {
asc.cmacLong(msg, block, siv)
} else {
multiplyByX(block)
for i := 0; i < len(msg); i++ {
block[i] ^= msg[i]
}
block[len(msg)] ^= 0x80
asc.cmac(block, siv)
}
}
// cmacLong computes CMAC(XorEnd(data, last)), where XorEnd xors the bytes in
// last to the last bytes in data.
//
// The size of the data must be at least 16 bytes.
func (asc *AESSIV) cmacLong(data, last, mac []byte) {
block := make([]byte, aes.BlockSize)
copy(block, data[:aes.BlockSize])
idx := aes.BlockSize
for aes.BlockSize <= len(data)-idx {
asc.Cipher.Encrypt(block, block)
xorBlock(data[idx:idx+aes.BlockSize], block)
idx += aes.BlockSize
}
remaining := len(data) - idx
for i := 0; i < aes.BlockSize-remaining; i++ {
block[remaining+i] ^= last[i]
}
if remaining == 0 {
xorBlock(asc.CmacK1, block)
} else {
asc.Cipher.Encrypt(block, block)
for i := 0; i < remaining; i++ {
block[i] ^= last[aes.BlockSize-remaining+i]
block[i] ^= data[idx+i]
}
block[remaining] ^= 0x80
xorBlock(asc.CmacK2, block)
}
asc.Cipher.Encrypt(mac, block)
}
// cmac computes a CMAC of some data.
func (asc *AESSIV) cmac(data, mac []byte) {
numBs := int(math.Ceil(float64(len(data)) / aes.BlockSize))
if numBs == 0 {
numBs = 1
}
lastBSize := len(data) - (numBs-1)*aes.BlockSize
block := make([]byte, aes.BlockSize)
idx := 0
for i := 0; i < numBs-1; i++ {
xorBlock(data[idx:idx+aes.BlockSize], block)
asc.Cipher.Encrypt(block, block)
idx += aes.BlockSize
}
for j := 0; j < lastBSize; j++ {
block[j] ^= data[idx+j]
}
if lastBSize == aes.BlockSize {
xorBlock(asc.CmacK1, block)
} else {
block[lastBSize] ^= 0x80
xorBlock(asc.CmacK2, block)
}
asc.Cipher.Encrypt(mac, block)
}
// xorBlock sets block[i] = x[i] ^ block[i].
func xorBlock(x, block []byte) {
for i := 0; i < aes.BlockSize; i++ {
block[i] ^= x[i]
}
}
+303
View File
@@ -0,0 +1,303 @@
package daed_test
import (
"bytes"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"testing"
subtle "github.com/sonr-io/sonr/crypto/daed"
"github.com/sonr-io/sonr/crypto/subtle/random"
)
type testData struct {
Algorithm string
GeneratorVersion string
TestGroups []*testGroup
NumberOfTests uint32
}
type testGroup struct {
Type string
Tests []*testCase
KeySize uint32
}
type testCase struct {
Key string
Aad string
Msg string
Ct string
Result string
TcID uint32
}
func TestAESSIV_EncryptDecrypt(t *testing.T) {
keyStr := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
"00112233445566778899aabbccddeefff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
key, _ := hex.DecodeString(keyStr)
msg := []byte("Some data to encrypt.")
aad := []byte("Additional data")
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
}
ct, err := a.EncryptDeterministically(msg, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, aad); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, msg) {
t.Errorf("Mismatched plaintexts: got %v, want %v", pt, msg)
}
}
func TestAESSIV_EmptyPlaintext(t *testing.T) {
keyStr := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
"00112233445566778899aabbccddeefff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
key, _ := hex.DecodeString(keyStr)
aad := []byte("Additional data")
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
}
ct, err := a.EncryptDeterministically(nil, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, aad); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, []byte{}) {
t.Errorf("Mismatched plaintexts: got %v, want []", pt)
}
ct, err = a.EncryptDeterministically([]byte{}, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, aad); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, []byte{}) {
t.Errorf("Mismatched plaintexts: got %v, want []", pt)
}
}
func TestAESSIV_EmptyAdditionalData(t *testing.T) {
keyStr := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
"00112233445566778899aabbccddeefff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
key, _ := hex.DecodeString(keyStr)
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
}
ct, err := a.EncryptDeterministically(nil, nil)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, nil); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, []byte{}) {
t.Errorf("Mismatched plaintexts: got %v, want []", pt)
}
if pt, err := a.DecryptDeterministically(ct, []byte{}); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, []byte{}) {
t.Errorf("Mismatched plaintexts: got %v, want []", pt)
}
}
func TestAESSIV_KeySizes(t *testing.T) {
keyStr := "198371900187498172316311acf81d238ff7619873a61983d619c87b63a1987f" +
"987131819803719b847126381cd763871638aa71638176328761287361231321" +
"812731321de508761437195ff231765aa4913219873ac6918639816312130011" +
"abc900bba11400187984719827431246bbab1231eb4145215ff7141436616beb" +
"9817298148712fed3aab61000ff123313e"
key, _ := hex.DecodeString(keyStr)
for i := 0; i < len(key); i++ {
_, err := subtle.NewAESSIV(key[:i])
if i == subtle.AESSIVKeySize && err != nil {
t.Errorf("Rejected valid key size: %v, %v", i, err)
}
if i != subtle.AESSIVKeySize && err == nil {
t.Errorf("Allowed invalid key size: %v", i)
}
}
}
func TestAESSIV_MessageSizes(t *testing.T) {
keyStr := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
"00112233445566778899aabbccddeefff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
key, _ := hex.DecodeString(keyStr)
aad := []byte("Additional data")
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
}
for i := uint32(0); i < 1024; i++ {
msg := random.GetRandomBytes(i)
ct, err := a.EncryptDeterministically(msg, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, aad); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, msg) {
t.Errorf("Mismatched plaintexts: got %v, want %v", pt, msg)
}
}
for i := uint32(1024); i < 100000; i += 5000 {
msg := random.GetRandomBytes(i)
ct, err := a.EncryptDeterministically(msg, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, aad); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, msg) {
t.Errorf("Mismatched plaintexts: got %v, want %v", pt, msg)
}
}
}
func TestAESSIV_AdditionalDataSizes(t *testing.T) {
keyStr := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
"00112233445566778899aabbccddeefff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
key, _ := hex.DecodeString(keyStr)
msg := []byte("Some data to encrypt.")
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
}
for i := uint32(0); i < 1024; i++ {
aad := random.GetRandomBytes(i)
ct, err := a.EncryptDeterministically(msg, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
if pt, err := a.DecryptDeterministically(ct, aad); err != nil {
t.Errorf("Unexpected decryption error: %v", err)
} else if !bytes.Equal(pt, msg) {
t.Errorf("Mismatched plaintexts: got %v, want %v", pt, msg)
}
}
}
func TestAESSIV_CiphertextModifications(t *testing.T) {
keyStr := "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" +
"00112233445566778899aabbccddeefff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
key, _ := hex.DecodeString(keyStr)
aad := []byte("Additional data")
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
}
for i := uint32(0); i < 50; i++ {
msg := random.GetRandomBytes(i)
ct, err := a.EncryptDeterministically(msg, aad)
if err != nil {
t.Errorf("Unexpected encryption error: %v", err)
}
for j := 0; j < len(ct); j++ {
for b := uint32(0); b < 8; b++ {
ct[j] ^= 1 << b
if _, err := a.DecryptDeterministically(ct, aad); err == nil {
t.Errorf("Modified ciphertext decrypted: byte %d, bit %d", j, b)
}
ct[j] ^= 1 << b
}
}
}
}
func TestAESSIV_WycheproofVectors(t *testing.T) {
srcDir, ok := os.LookupEnv("TEST_SRCDIR")
if !ok {
t.Skip("TEST_SRCDIR not set")
}
f, err := os.Open(filepath.Join(srcDir, "wycheproof/testvectors/aes_siv_cmac_test.json"))
if err != nil {
t.Fatalf("Cannot open file: %s", err)
}
parser := json.NewDecoder(f)
data := new(testData)
if err := parser.Decode(data); err != nil {
t.Fatalf("Cannot decode test data: %s", err)
}
for _, g := range data.TestGroups {
if g.KeySize/8 != subtle.AESSIVKeySize {
continue
}
for _, tc := range g.Tests {
key, err := hex.DecodeString(tc.Key)
if err != nil {
t.Errorf("#%d, cannot decode key: %s", tc.TcID, err)
}
aad, err := hex.DecodeString(tc.Aad)
if err != nil {
t.Errorf("#%d, cannot decode aad: %s", tc.TcID, err)
}
msg, err := hex.DecodeString(tc.Msg)
if err != nil {
t.Errorf("#%d, cannot decode msg: %s", tc.TcID, err)
}
ct, err := hex.DecodeString(tc.Ct)
if err != nil {
t.Errorf("#%d, cannot decode ct: %s", tc.TcID, err)
}
a, err := subtle.NewAESSIV(key)
if err != nil {
t.Errorf("NewAESSIV(key) = _, %v, want _, nil", err)
continue
}
// EncryptDeterministically should always succeed since msg and aad are valid inputs.
gotCt, err := a.EncryptDeterministically(msg, aad)
if err != nil {
t.Errorf("#%d, unexpected encryption error: %v", tc.TcID, err)
} else {
if tc.Result == "valid" && !bytes.Equal(gotCt, ct) {
t.Errorf("#%d, incorrect encryption: got %v, want %v", tc.TcID, gotCt, ct)
}
if tc.Result == "invalid" && bytes.Equal(gotCt, ct) {
t.Errorf("#%d, invalid encryption: got %v, want %v", tc.TcID, gotCt, ct)
}
}
pt, err := a.DecryptDeterministically(ct, aad)
if tc.Result == "valid" {
if err != nil {
t.Errorf("#%d, unexpected decryption error: %v", tc.TcID, err)
} else if !bytes.Equal(pt, msg) {
t.Errorf("#%d, incorrect decryption: got %v, want %v", tc.TcID, pt, msg)
}
} else {
if err == nil {
t.Errorf("#%d, decryption error expected: got nil", tc.TcID)
}
}
}
}
}