@@ -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.
5466var 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
285293func (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
343351func (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 ())
0 commit comments