Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ms-go1.23-support] Handle unsupported AES-CTR #220

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type extraModes interface {
NewGCMTLS() (cipher.AEAD, error)
}

var _ extraModes = (*aesCipher)(nil)
var _ extraModes = (*aesWithCTR)(nil)

func NewAESCipher(key []byte) (cipher.Block, error) {
var kind cipherKind
Expand All @@ -38,19 +38,32 @@ func NewAESCipher(key []byte) (cipher.Block, error) {
if err != nil {
return nil, err
}
return &aesCipher{c}, nil
ac := aesCipher{c}
// The SymCrypt provider doesn't support AES-CTR.
// Prove that the provider supports AES-CTR before
// returning an aesWithCTR.
if loadCipher(kind, cipherModeCTR) != nil {
return &aesWithCTR{ac}, nil
}
return &ac, nil
}

// NewGCMTLS returns a GCM cipher specific to TLS
// and should not be used for non-TLS purposes.
func NewGCMTLS(c cipher.Block) (cipher.AEAD, error) {
return c.(*aesCipher).NewGCMTLS()
if c, ok := c.(*aesCipher); ok {
return c.NewGCMTLS()
}
return c.(*aesWithCTR).NewGCMTLS()
}

// NewGCMTLS13 returns a GCM cipher specific to TLS 1.3 and should not be used
// for non-TLS purposes.
func NewGCMTLS13(c cipher.Block) (cipher.AEAD, error) {
return c.(*aesCipher).NewGCMTLS13()
if c, ok := c.(*aesCipher); ok {
return c.NewGCMTLS13()
}
return c.(*aesWithCTR).NewGCMTLS13()
}

type aesCipher struct {
Expand Down Expand Up @@ -83,10 +96,6 @@ func (c *aesCipher) NewCBCDecrypter(iv []byte) cipher.BlockMode {
return c.newCBC(iv, cipherOpDecrypt)
}

func (c *aesCipher) NewCTR(iv []byte) cipher.Stream {
return c.newCTR(iv)
}

func (c *aesCipher) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
return c.newGCMChecked(nonceSize, tagSize)
}
Expand All @@ -98,3 +107,11 @@ func (c *aesCipher) NewGCMTLS() (cipher.AEAD, error) {
func (c *aesCipher) NewGCMTLS13() (cipher.AEAD, error) {
return c.newGCM(cipherGCMTLS13)
}

type aesWithCTR struct {
aesCipher
}

func (c *aesWithCTR) NewCTR(iv []byte) cipher.Stream {
return c.newCTR(iv)
}
Loading