Skip to content

Commit 251d5fd

Browse files
authored
Merge pull request #121 from golang-fips/errmsg
Reconcile AES and DES error messages with upstream
2 parents 9783f40 + c270a8e commit 251d5fd

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

aes.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ func (c *aesCipher) BlockSize() int {
5656
}
5757

5858
func (c *aesCipher) Encrypt(dst, src []byte) {
59-
c.encrypt(dst, src)
59+
if err := c.encrypt(dst, src); err != nil {
60+
// crypto/aes expects that the panic message starts with "crypto/aes: ".
61+
panic("crypto/aes: " + err.Error())
62+
}
6063
}
6164

6265
func (c *aesCipher) Decrypt(dst, src []byte) {
63-
c.decrypt(dst, src)
66+
if err := c.decrypt(dst, src); err != nil {
67+
// crypto/aes expects that the panic message starts with "crypto/aes: ".
68+
panic("crypto/aes: " + err.Error())
69+
}
6470
}
6571

6672
func (c *aesCipher) NewCBCEncrypter(iv []byte) cipher.BlockMode {

aes_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,31 @@ import (
99
"github.com/golang-fips/openssl/v2"
1010
)
1111

12+
func TestAESShortBlocks(t *testing.T) {
13+
bytes := func(n int) []byte { return make([]byte, n) }
14+
15+
c, _ := openssl.NewAESCipher(bytes(16))
16+
17+
mustPanic(t, "crypto/aes: input not full block", func() { c.Encrypt(bytes(1), bytes(1)) })
18+
mustPanic(t, "crypto/aes: input not full block", func() { c.Decrypt(bytes(1), bytes(1)) })
19+
mustPanic(t, "crypto/aes: input not full block", func() { c.Encrypt(bytes(100), bytes(1)) })
20+
mustPanic(t, "crypto/aes: input not full block", func() { c.Decrypt(bytes(100), bytes(1)) })
21+
mustPanic(t, "crypto/aes: output not full block", func() { c.Encrypt(bytes(1), bytes(100)) })
22+
mustPanic(t, "crypto/aes: output not full block", func() { c.Decrypt(bytes(1), bytes(100)) })
23+
}
24+
25+
func mustPanic(t *testing.T, msg string, f func()) {
26+
defer func() {
27+
err := recover()
28+
if err == nil {
29+
t.Errorf("function did not panic, wanted %q", msg)
30+
} else if err != msg {
31+
t.Errorf("got panic %v, wanted %q", err, msg)
32+
}
33+
}()
34+
f()
35+
}
36+
1237
func TestNewGCMNonce(t *testing.T) {
1338
key := []byte("D249BF6DEC97B1EBD69BC4D6B3A3C49D")
1439
ci, err := openssl.NewAESCipher(key)

cipher.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -166,57 +166,59 @@ func (c *evpCipher) finalize() {
166166
}
167167
}
168168

169-
func (c *evpCipher) encrypt(dst, src []byte) {
169+
func (c *evpCipher) encrypt(dst, src []byte) error {
170170
if len(src) < c.blockSize {
171-
panic("crypto/cipher: input not full block")
171+
return errors.New("input not full block")
172172
}
173173
if len(dst) < c.blockSize {
174-
panic("crypto/cipher: output not full block")
174+
return errors.New("output not full block")
175175
}
176176
// Only check for overlap between the parts of src and dst that will actually be used.
177177
// This matches Go standard library behavior.
178178
if inexactOverlap(dst[:c.blockSize], src[:c.blockSize]) {
179-
panic("crypto/cipher: invalid buffer overlap")
179+
return errors.New("invalid buffer overlap")
180180
}
181181
if c.enc_ctx == nil {
182182
var err error
183183
c.enc_ctx, err = newCipherCtx(c.kind, cipherModeECB, cipherOpEncrypt, c.key, nil)
184184
if err != nil {
185-
panic(err)
185+
return err
186186
}
187187
}
188188

189189
if C.go_openssl_EVP_EncryptUpdate_wrapper(c.enc_ctx, base(dst), base(src), C.int(c.blockSize)) != 1 {
190-
panic("crypto/cipher: EncryptUpdate failed")
190+
return errors.New("EncryptUpdate failed")
191191
}
192192
runtime.KeepAlive(c)
193+
return nil
193194
}
194195

195-
func (c *evpCipher) decrypt(dst, src []byte) {
196+
func (c *evpCipher) decrypt(dst, src []byte) error {
196197
if len(src) < c.blockSize {
197-
panic("crypto/cipher: input not full block")
198+
return errors.New("input not full block")
198199
}
199200
if len(dst) < c.blockSize {
200-
panic("crypto/cipher: output not full block")
201+
return errors.New("output not full block")
201202
}
202203
// Only check for overlap between the parts of src and dst that will actually be used.
203204
// This matches Go standard library behavior.
204205
if inexactOverlap(dst[:c.blockSize], src[:c.blockSize]) {
205-
panic("crypto/cipher: invalid buffer overlap")
206+
return errors.New("invalid buffer overlap")
206207
}
207208
if c.dec_ctx == nil {
208209
var err error
209210
c.dec_ctx, err = newCipherCtx(c.kind, cipherModeECB, cipherOpDecrypt, c.key, nil)
210211
if err != nil {
211-
panic(err)
212+
return err
212213
}
213214
if C.go_openssl_EVP_CIPHER_CTX_set_padding(c.dec_ctx, 0) != 1 {
214-
panic("crypto/cipher: could not disable cipher padding")
215+
return errors.New("could not disable cipher padding")
215216
}
216217
}
217218

218219
C.go_openssl_EVP_DecryptUpdate_wrapper(c.dec_ctx, base(dst), base(src), C.int(c.blockSize))
219220
runtime.KeepAlive(c)
221+
return nil
220222
}
221223

222224
type cipherCBC struct {

des.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ func (c *desCipher) BlockSize() int {
7575
}
7676

7777
func (c *desCipher) Encrypt(dst, src []byte) {
78-
c.encrypt(dst, src)
78+
if err := c.encrypt(dst, src); err != nil {
79+
// crypto/des expects that the panic message starts with "crypto/des: ".
80+
panic("crypto/des: " + err.Error())
81+
}
7982
}
8083

8184
func (c *desCipher) Decrypt(dst, src []byte) {
82-
c.decrypt(dst, src)
85+
if err := c.decrypt(dst, src); err != nil {
86+
// crypto/des expects that the panic message starts with "crypto/des: ".
87+
panic("crypto/des: " + err.Error())
88+
}
8389
}
8490

8591
func (c *desCipher) NewCBCEncrypter(iv []byte) cipher.BlockMode {

0 commit comments

Comments
 (0)