@@ -24,6 +24,23 @@ const (
2424 cipherDES3
2525)
2626
27+ func (c cipherKind ) String () string {
28+ switch c {
29+ case cipherAES128 :
30+ return "AES-128"
31+ case cipherAES192 :
32+ return "AES-192"
33+ case cipherAES256 :
34+ return "AES-256"
35+ case cipherDES :
36+ return "DES"
37+ case cipherDES3 :
38+ return "DES3"
39+ default :
40+ panic ("unknown cipher kind: " + strconv .Itoa (int (c )))
41+ }
42+ }
43+
2744type cipherMode int8
2845
2946const (
@@ -109,10 +126,23 @@ func loadCipher(k cipherKind, mode cipherMode) (cipher C.GO_EVP_CIPHER_PTR) {
109126}
110127
111128type evpCipher struct {
112- key []byte
113- enc_ctx C.GO_EVP_CIPHER_CTX_PTR
114- dec_ctx C.GO_EVP_CIPHER_CTX_PTR
115- kind cipherKind
129+ key []byte
130+ enc_ctx C.GO_EVP_CIPHER_CTX_PTR
131+ dec_ctx C.GO_EVP_CIPHER_CTX_PTR
132+ kind cipherKind
133+ blockSize int
134+ }
135+
136+ func newEVPCipher (key []byte , kind cipherKind ) (* evpCipher , error ) {
137+ cipher := loadCipher (kind , cipherModeECB )
138+ if cipher == nil {
139+ return nil , errors .New ("crypto/cipher: unsupported cipher: " + kind .String ())
140+ }
141+ c := & evpCipher {key : make ([]byte , len (key )), kind : kind }
142+ copy (c .key , key )
143+ c .blockSize = int (C .go_openssl_EVP_CIPHER_get_block_size (cipher ))
144+ runtime .SetFinalizer (c , (* evpCipher ).finalize )
145+ return c , nil
116146}
117147
118148func (c * evpCipher ) finalize () {
@@ -124,28 +154,16 @@ func (c *evpCipher) finalize() {
124154 }
125155}
126156
127- func (c * evpCipher ) blockSize () int {
128- switch c .kind {
129- case cipherAES128 , cipherAES192 , cipherAES256 :
130- return aesBlockSize
131- case cipherDES , cipherDES3 :
132- return desBlockSize
133- default :
134- panic ("openssl: unsupported cipher: " + strconv .Itoa (int (c .kind )))
135- }
136- }
137-
138157func (c * evpCipher ) encrypt (dst , src []byte ) {
139- blockSize := c .blockSize ()
140- if len (src ) < blockSize {
158+ if len (src ) < c .blockSize {
141159 panic ("crypto/cipher: input not full block" )
142160 }
143- if len (dst ) < blockSize {
161+ if len (dst ) < c . blockSize {
144162 panic ("crypto/cipher: output not full block" )
145163 }
146164 // Only check for overlap between the parts of src and dst that will actually be used.
147165 // This matches Go standard library behavior.
148- if inexactOverlap (dst [:blockSize ], src [:blockSize ]) {
166+ if inexactOverlap (dst [:c . blockSize ], src [:c . blockSize ]) {
149167 panic ("crypto/cipher: invalid buffer overlap" )
150168 }
151169 if c .enc_ctx == nil {
@@ -156,23 +174,22 @@ func (c *evpCipher) encrypt(dst, src []byte) {
156174 }
157175 }
158176
159- if C .go_openssl_EVP_EncryptUpdate_wrapper (c .enc_ctx , base (dst ), base (src ), C .int (blockSize )) != 1 {
177+ if C .go_openssl_EVP_EncryptUpdate_wrapper (c .enc_ctx , base (dst ), base (src ), C .int (c . blockSize )) != 1 {
160178 panic ("crypto/cipher: EncryptUpdate failed" )
161179 }
162180 runtime .KeepAlive (c )
163181}
164182
165183func (c * evpCipher ) decrypt (dst , src []byte ) {
166- blockSize := c .blockSize ()
167- if len (src ) < blockSize {
184+ if len (src ) < c .blockSize {
168185 panic ("crypto/cipher: input not full block" )
169186 }
170- if len (dst ) < blockSize {
187+ if len (dst ) < c . blockSize {
171188 panic ("crypto/cipher: output not full block" )
172189 }
173190 // Only check for overlap between the parts of src and dst that will actually be used.
174191 // This matches Go standard library behavior.
175- if inexactOverlap (dst [:blockSize ], src [:blockSize ]) {
192+ if inexactOverlap (dst [:c . blockSize ], src [:c . blockSize ]) {
176193 panic ("crypto/cipher: invalid buffer overlap" )
177194 }
178195 if c .dec_ctx == nil {
@@ -186,7 +203,7 @@ func (c *evpCipher) decrypt(dst, src []byte) {
186203 }
187204 }
188205
189- C .go_openssl_EVP_DecryptUpdate_wrapper (c .dec_ctx , base (dst ), base (src ), C .int (blockSize ))
206+ C .go_openssl_EVP_DecryptUpdate_wrapper (c .dec_ctx , base (dst ), base (src ), C .int (c . blockSize ))
190207 runtime .KeepAlive (c )
191208}
192209
@@ -237,7 +254,7 @@ func (c *evpCipher) newCBC(iv []byte, encrypt bool) cipher.BlockMode {
237254 if err != nil {
238255 panic (err )
239256 }
240- x := & cipherCBC {ctx : ctx , blockSize : c .blockSize () }
257+ x := & cipherCBC {ctx : ctx , blockSize : c .blockSize }
241258 runtime .SetFinalizer (x , (* cipherCBC ).finalize )
242259 if C .go_openssl_EVP_CIPHER_CTX_set_padding (x .ctx , 0 ) != 1 {
243260 panic ("cipher: unable to set padding" )
@@ -298,7 +315,7 @@ type noGCM struct {
298315}
299316
300317func (g * noGCM ) BlockSize () int {
301- return g .blockSize ()
318+ return g .blockSize
302319}
303320
304321func (g * noGCM ) Encrypt (dst , src []byte ) {
@@ -328,7 +345,7 @@ func (c *evpCipher) newGCM(tls bool) (cipher.AEAD, error) {
328345 if err != nil {
329346 return nil , err
330347 }
331- g := & cipherGCM {ctx : ctx , tls : tls , blockSize : c .blockSize () }
348+ g := & cipherGCM {ctx : ctx , tls : tls , blockSize : c .blockSize }
332349 runtime .SetFinalizer (g , (* cipherGCM ).finalize )
333350 return g , nil
334351}
@@ -455,7 +472,7 @@ func sliceForAppend(in []byte, n int) (head, tail []byte) {
455472func newCipherCtx (kind cipherKind , mode cipherMode , encrypt int , key , iv []byte ) (C.GO_EVP_CIPHER_CTX_PTR , error ) {
456473 cipher := loadCipher (kind , mode )
457474 if cipher == nil {
458- panic ("openssl : unsupported cipher: " + strconv . Itoa ( int ( kind ) ))
475+ panic ("crypto/cipher : unsupported cipher: " + kind . String ( ))
459476 }
460477 ctx := C .go_openssl_EVP_CIPHER_CTX_new ()
461478 if ctx == nil {
0 commit comments