Skip to content

Commit 05ed151

Browse files
committed
add type for allowed cipher operations
1 parent ed65759 commit 05ed151

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

aes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (c *aesCipher) Decrypt(dst, src []byte) {
6464
}
6565

6666
func (c *aesCipher) NewCBCEncrypter(iv []byte) cipher.BlockMode {
67-
return c.newCBC(iv, true)
67+
return c.newCBC(iv, cipherOpEncrypt)
6868
}
6969

7070
func (c *aesCipher) NewCBCDecrypter(iv []byte) cipher.BlockMode {
71-
return c.newCBC(iv, false)
71+
return c.newCBC(iv, cipherOpDecrypt)
7272
}
7373

7474
func (c *aesCipher) NewCTR(iv []byte) cipher.Stream {

cipher.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ const (
5050
cipherModeGCM
5151
)
5252

53+
// cipherOp is the allowed operations for a cipher,
54+
// as documented in [EVP_CipherInit_ex].
55+
//
56+
// [EVP_CipherInit_ex]: https://www.openssl.org/docs/man3.0/man3/EVP_CipherInit_ex.html
57+
type cipherOp int8
58+
59+
const (
60+
cipherOpNone cipherOp = -1 // leaves the value of the previous call, if any.
61+
cipherOpDecrypt cipherOp = 0
62+
cipherOpEncrypt cipherOp = 1
63+
)
64+
5365
// cacheCipher is a cache of cipherKind to GO_EVP_CIPHER_PTR.
5466
var cacheCipher sync.Map
5567

@@ -168,7 +180,7 @@ func (c *evpCipher) encrypt(dst, src []byte) {
168180
}
169181
if c.enc_ctx == nil {
170182
var err error
171-
c.enc_ctx, err = newCipherCtx(c.kind, cipherModeECB, 1, c.key, nil)
183+
c.enc_ctx, err = newCipherCtx(c.kind, cipherModeECB, cipherOpEncrypt, c.key, nil)
172184
if err != nil {
173185
panic(err)
174186
}
@@ -194,7 +206,7 @@ func (c *evpCipher) decrypt(dst, src []byte) {
194206
}
195207
if c.dec_ctx == nil {
196208
var err error
197-
c.dec_ctx, err = newCipherCtx(c.kind, cipherModeECB, 0, c.key, nil)
209+
c.dec_ctx, err = newCipherCtx(c.kind, cipherModeECB, cipherOpDecrypt, c.key, nil)
198210
if err != nil {
199211
panic(err)
200212
}
@@ -240,17 +252,13 @@ func (x *cipherCBC) SetIV(iv []byte) {
240252
if len(iv) != x.blockSize {
241253
panic("cipher: incorrect length IV")
242254
}
243-
if C.go_openssl_EVP_CipherInit_ex(x.ctx, nil, nil, nil, base(iv), -1) != 1 {
255+
if C.go_openssl_EVP_CipherInit_ex(x.ctx, nil, nil, nil, base(iv), C.int(cipherOpNone)) != 1 {
244256
panic("cipher: unable to initialize EVP cipher ctx")
245257
}
246258
}
247259

248-
func (c *evpCipher) newCBC(iv []byte, encrypt bool) cipher.BlockMode {
249-
enc := 1
250-
if !encrypt {
251-
enc = 0
252-
}
253-
ctx, err := newCipherCtx(c.kind, cipherModeCBC, enc, c.key, iv)
260+
func (c *evpCipher) newCBC(iv []byte, op cipherOp) cipher.BlockMode {
261+
ctx, err := newCipherCtx(c.kind, cipherModeCBC, op, c.key, iv)
254262
if err != nil {
255263
panic(err)
256264
}
@@ -283,7 +291,7 @@ func (x *cipherCTR) XORKeyStream(dst, src []byte) {
283291
}
284292

285293
func (c *evpCipher) newCTR(iv []byte) cipher.Stream {
286-
ctx, err := newCipherCtx(c.kind, cipherModeCTR, 1, c.key, iv)
294+
ctx, err := newCipherCtx(c.kind, cipherModeCTR, cipherOpEncrypt, c.key, iv)
287295
if err != nil {
288296
panic(err)
289297
}
@@ -341,7 +349,7 @@ func (c *evpCipher) newGCMChecked(nonceSize, tagSize int) (cipher.AEAD, error) {
341349
}
342350

343351
func (c *evpCipher) newGCM(tls bool) (cipher.AEAD, error) {
344-
ctx, err := newCipherCtx(c.kind, cipherModeGCM, -1, c.key, nil)
352+
ctx, err := newCipherCtx(c.kind, cipherModeGCM, cipherOpNone, c.key, nil)
345353
if err != nil {
346354
return nil, err
347355
}
@@ -469,7 +477,7 @@ func sliceForAppend(in []byte, n int) (head, tail []byte) {
469477
return
470478
}
471479

472-
func newCipherCtx(kind cipherKind, mode cipherMode, encrypt int, key, iv []byte) (C.GO_EVP_CIPHER_CTX_PTR, error) {
480+
func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []byte) (C.GO_EVP_CIPHER_CTX_PTR, error) {
473481
cipher := loadCipher(kind, mode)
474482
if cipher == nil {
475483
panic("crypto/cipher: unsupported cipher: " + kind.String())

des.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ func (c *desCipher) Decrypt(dst, src []byte) {
6969
}
7070

7171
func (c *desCipher) NewCBCEncrypter(iv []byte) cipher.BlockMode {
72-
return c.newCBC(iv, true)
72+
return c.newCBC(iv, cipherOpEncrypt)
7373
}
7474

7575
func (c *desCipher) NewCBCDecrypter(iv []byte) cipher.BlockMode {
76-
return c.newCBC(iv, false)
76+
return c.newCBC(iv, cipherOpDecrypt)
7777
}

goopenssl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ go_openssl_EVP_CIPHER_CTX_seal_wrapper(const GO_EVP_CIPHER_CTX_PTR ctx,
119119
if (in_len == 0) in = "";
120120
if (aad_len == 0) aad = "";
121121

122-
if (go_openssl_EVP_CipherInit_ex(ctx, NULL, NULL, NULL, nonce, 1) != 1)
122+
if (go_openssl_EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, nonce) != 1)
123123
return 0;
124124

125125
int discard_len, out_len;
@@ -147,7 +147,7 @@ go_openssl_EVP_CIPHER_CTX_open_wrapper(const GO_EVP_CIPHER_CTX_PTR ctx,
147147
if (in_len == 0) in = "";
148148
if (aad_len == 0) aad = "";
149149

150-
if (go_openssl_EVP_CipherInit_ex(ctx, NULL, NULL, NULL, nonce, 0) != 1)
150+
if (go_openssl_EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, nonce) != 1)
151151
return 0;
152152

153153
int discard_len, out_len;

shims.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ DEFINEFUNC(int, EVP_CipherUpdate, (GO_EVP_CIPHER_CTX_PTR ctx, unsigned char *out
226226
DEFINEFUNC(int, EVP_EncryptInit_ex, (GO_EVP_CIPHER_CTX_PTR ctx, const GO_EVP_CIPHER_PTR type, GO_ENGINE_PTR impl, const unsigned char *key, const unsigned char *iv), (ctx, type, impl, key, iv)) \
227227
DEFINEFUNC(int, EVP_EncryptUpdate, (GO_EVP_CIPHER_CTX_PTR ctx, unsigned char *out, int *outl, const unsigned char *in, int inl), (ctx, out, outl, in, inl)) \
228228
DEFINEFUNC(int, EVP_EncryptFinal_ex, (GO_EVP_CIPHER_CTX_PTR ctx, unsigned char *out, int *outl), (ctx, out, outl)) \
229+
DEFINEFUNC(int, EVP_DecryptInit_ex, (GO_EVP_CIPHER_CTX_PTR ctx, const GO_EVP_CIPHER_PTR type, GO_ENGINE_PTR impl, const unsigned char *key, const unsigned char *iv), (ctx, type, impl, key, iv)) \
229230
DEFINEFUNC(int, EVP_DecryptUpdate, (GO_EVP_CIPHER_CTX_PTR ctx, unsigned char *out, int *outl, const unsigned char *in, int inl), (ctx, out, outl, in, inl)) \
230231
DEFINEFUNC(int, EVP_DecryptFinal_ex, (GO_EVP_CIPHER_CTX_PTR ctx, unsigned char *outm, int *outl), (ctx, outm, outl)) \
231232
DEFINEFUNC_3_0(GO_EVP_CIPHER_PTR, EVP_CIPHER_fetch, (GO_OSSL_LIB_CTX_PTR ctx, const char *algorithm, const char *properties), (ctx, algorithm, properties)) \

0 commit comments

Comments
 (0)