Skip to content

Commit

Permalink
Delete AESGCMInsecureIV inlining cipher.AEAD usage
Browse files Browse the repository at this point in the history
This is more readable, and avoids duplicate testing.

PiperOrigin-RevId: 690592032
Change-Id: Id76b081105155937587ca9c086ab0755c5902a31
  • Loading branch information
morambro authored and copybara-github committed Oct 28, 2024
1 parent 1e587f0 commit 3cf2455
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 516 deletions.
23 changes: 17 additions & 6 deletions aead/subtle/aes_gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package subtle

import (
"crypto/cipher"
"fmt"

internalaead "github.com/tink-crypto/tink-go/v2/internal/aead"
Expand All @@ -27,11 +28,13 @@ const (
AESGCMIVSize = 12
// AESGCMTagSize is the acceptable tag size defined by RFC 5116.
AESGCMTagSize = 16

maxIntPlaintextSize = maxInt - AESGCMIVSize - AESGCMTagSize
)

// AESGCM is an implementation of AEAD interface.
type AESGCM struct {
aesGCMInsecureIV *internalaead.AESGCMInsecureIV
cipher cipher.AEAD
}

// Assert that AESGCM implements the AEAD interface.
Expand All @@ -40,8 +43,11 @@ var _ tink.AEAD = (*AESGCM)(nil)
// NewAESGCM returns an AESGCM instance, where key is the AES key with length
// 16 bytes (AES-128) or 32 bytes (AES-256).
func NewAESGCM(key []byte) (*AESGCM, error) {
aesGCMInsecureIV, err := internalaead.NewAESGCMInsecureIV(key, true /*=prependIV*/)
return &AESGCM{aesGCMInsecureIV}, err
c, err := internalaead.NewAESGCMCipher(key)
if err != nil {
return nil, err
}
return &AESGCM{cipher: c}, nil
}

// Encrypt encrypts plaintext with associatedData. The returned ciphertext
Expand All @@ -50,15 +56,20 @@ func NewAESGCM(key []byte) (*AESGCM, error) {
// Note: The crypto library's AES-GCM implementation always returns the
// ciphertext with an AESGCMTagSize (16-byte) tag.
func (a *AESGCM) Encrypt(plaintext, associatedData []byte) ([]byte, error) {
if err := internalaead.CheckPlaintextSize(uint64(len(plaintext))); err != nil {
return nil, err
}
iv := random.GetRandomBytes(AESGCMIVSize)
return a.aesGCMInsecureIV.Encrypt(iv, plaintext, associatedData)
dst := make([]byte, 0, len(iv)+len(plaintext)+a.cipher.Overhead())
dst = append(dst, iv...)
return a.cipher.Seal(dst, iv, plaintext, associatedData), nil
}

// Decrypt decrypts ciphertext with associatedData.
func (a *AESGCM) Decrypt(ciphertext, associatedData []byte) ([]byte, error) {
if len(ciphertext) < AESGCMIVSize {
if len(ciphertext) < AESGCMIVSize+AESGCMTagSize {
return nil, fmt.Errorf("ciphertext with size %d is too short", len(ciphertext))
}
iv := ciphertext[:AESGCMIVSize]
return a.aesGCMInsecureIV.Decrypt(iv, ciphertext, associatedData)
return a.cipher.Open(nil, iv, ciphertext[AESGCMIVSize:], associatedData)
}
2 changes: 1 addition & 1 deletion aead/subtle/aes_gcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestAESGCMTagLength(t *testing.T) {
}
actualTagSize := len(ct) - subtle.AESGCMIVSize - len(pt)
if actualTagSize != subtle.AESGCMTagSize {
t.Errorf("tag size is not 128 bit, it is %d bit", actualTagSize*8)
t.Errorf("tag size is not 16, it is %d", actualTagSize)
}
}
}
Expand Down
33 changes: 18 additions & 15 deletions hybrid/internal/hpke/aes_gcm_aead.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,35 @@ func (a *aesGCMAEAD) seal(key, nonce, plaintext, associatedData []byte) ([]byte,
if len(key) != a.keyLen {
return nil, fmt.Errorf("unexpected key length: got %d, want %d", len(key), a.keyLen)
}
i, err := internalaead.NewAESGCMInsecureIV(key, false /*=prependIV*/)
if len(nonce) != a.nonceLength() {
return nil, fmt.Errorf("unexpected nonce length: got %d, want %d", len(nonce), a.nonceLength())
}
if err := internalaead.CheckPlaintextSize(uint64(len(plaintext))); err != nil {
return nil, err
}
c, err := internalaead.NewAESGCMCipher(key)
if err != nil {
return nil, fmt.Errorf("NewAESGCMInsecureIV: %v", err)
return nil, err
}
return i.Encrypt(nonce, plaintext, associatedData)
return c.Seal(nil, nonce, plaintext, associatedData), nil
}

func (a *aesGCMAEAD) open(key, nonce, ciphertext, associatedData []byte) ([]byte, error) {
if len(key) != a.keyLen {
return nil, fmt.Errorf("unexpected key length: got %d, want %d", len(key), a.keyLen)
}
i, err := internalaead.NewAESGCMInsecureIV(key, false /*=prependIV*/)
if len(nonce) != a.nonceLength() {
return nil, fmt.Errorf("unexpected nonce length: got %d, want %d", len(nonce), a.nonceLength())
}
c, err := internalaead.NewAESGCMCipher(key)
if err != nil {
return nil, fmt.Errorf("NewAESGCMInsecureIV: %v", err)
return nil, err
}
return i.Decrypt(nonce, ciphertext, associatedData)
return c.Open(nil, nonce, ciphertext, associatedData)
}

func (a *aesGCMAEAD) id() uint16 {
return a.aeadID
}
func (a *aesGCMAEAD) id() uint16 { return a.aeadID }

func (a *aesGCMAEAD) keyLength() int {
return a.keyLen
}
func (a *aesGCMAEAD) keyLength() int { return a.keyLen }

func (a *aesGCMAEAD) nonceLength() int {
return internalaead.AESGCMIVSize
}
func (a *aesGCMAEAD) nonceLength() int { return internalaead.AESGCMIVSize }
138 changes: 0 additions & 138 deletions internal/aead/aes_gcm_insecure_iv.go

This file was deleted.

Loading

0 comments on commit 3cf2455

Please sign in to comment.