Skip to content

Commit c5672cc

Browse files
authored
handle unsupported AES-CTR (#220)
1 parent e17dbeb commit c5672cc

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

aes.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type extraModes interface {
2020
NewGCMTLS() (cipher.AEAD, error)
2121
}
2222

23-
var _ extraModes = (*aesCipher)(nil)
23+
var _ extraModes = (*aesWithCTR)(nil)
2424

2525
func NewAESCipher(key []byte) (cipher.Block, error) {
2626
var kind cipherKind
@@ -38,19 +38,32 @@ func NewAESCipher(key []byte) (cipher.Block, error) {
3838
if err != nil {
3939
return nil, err
4040
}
41-
return &aesCipher{c}, nil
41+
ac := aesCipher{c}
42+
// The SymCrypt provider doesn't support AES-CTR.
43+
// Prove that the provider supports AES-CTR before
44+
// returning an aesWithCTR.
45+
if loadCipher(kind, cipherModeCTR) != nil {
46+
return &aesWithCTR{ac}, nil
47+
}
48+
return &ac, nil
4249
}
4350

4451
// NewGCMTLS returns a GCM cipher specific to TLS
4552
// and should not be used for non-TLS purposes.
4653
func NewGCMTLS(c cipher.Block) (cipher.AEAD, error) {
47-
return c.(*aesCipher).NewGCMTLS()
54+
if c, ok := c.(*aesCipher); ok {
55+
return c.NewGCMTLS()
56+
}
57+
return c.(*aesWithCTR).NewGCMTLS()
4858
}
4959

5060
// NewGCMTLS13 returns a GCM cipher specific to TLS 1.3 and should not be used
5161
// for non-TLS purposes.
5262
func NewGCMTLS13(c cipher.Block) (cipher.AEAD, error) {
53-
return c.(*aesCipher).NewGCMTLS13()
63+
if c, ok := c.(*aesCipher); ok {
64+
return c.NewGCMTLS13()
65+
}
66+
return c.(*aesWithCTR).NewGCMTLS13()
5467
}
5568

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

86-
func (c *aesCipher) NewCTR(iv []byte) cipher.Stream {
87-
return c.newCTR(iv)
88-
}
89-
9099
func (c *aesCipher) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
91100
return c.newGCMChecked(nonceSize, tagSize)
92101
}
@@ -98,3 +107,11 @@ func (c *aesCipher) NewGCMTLS() (cipher.AEAD, error) {
98107
func (c *aesCipher) NewGCMTLS13() (cipher.AEAD, error) {
99108
return c.newGCM(cipherGCMTLS13)
100109
}
110+
111+
type aesWithCTR struct {
112+
aesCipher
113+
}
114+
115+
func (c *aesWithCTR) NewCTR(iv []byte) cipher.Stream {
116+
return c.newCTR(iv)
117+
}

0 commit comments

Comments
 (0)