@@ -84,7 +84,7 @@ func loadCipher(k cipherKind, mode cipherMode) (cipher _EVP_CIPHER_PTR) {
84
84
// not created by EVP_CIPHER has negative performance
85
85
// implications, as cipher operations will have
86
86
// to fetch it on every call. Better to just fetch it once here.
87
- cipher = go_openssl_EVP_CIPHER_fetch (nil , go_openssl_EVP_CIPHER_get0_name (cipher ), nil )
87
+ cipher , _ = go_openssl_EVP_CIPHER_fetch (nil , go_openssl_EVP_CIPHER_get0_name (cipher ), nil )
88
88
}
89
89
cacheCipher .Store (cacheCipherKey {k , mode }, cipher )
90
90
}()
@@ -178,8 +178,8 @@ func (c *evpCipher) encrypt(dst, src []byte) error {
178
178
defer go_openssl_EVP_CIPHER_CTX_free (enc_ctx )
179
179
180
180
var outl int32
181
- if go_openssl_EVP_EncryptUpdate (enc_ctx , base (dst ), & outl , base (src ), int32 (c .blockSize )) != 1 {
182
- return errors . New ( "EncryptUpdate failed" )
181
+ if _ , err := go_openssl_EVP_EncryptUpdate (enc_ctx , base (dst ), & outl , base (src ), int32 (c .blockSize )); err != nil {
182
+ return err
183
183
}
184
184
runtime .KeepAlive (c )
185
185
return nil
@@ -203,8 +203,8 @@ func (c *evpCipher) decrypt(dst, src []byte) error {
203
203
}
204
204
defer go_openssl_EVP_CIPHER_CTX_free (dec_ctx )
205
205
206
- if go_openssl_EVP_CIPHER_CTX_set_padding (dec_ctx , 0 ) != 1 {
207
- return errors . New ( "could not disable cipher padding" )
206
+ if _ , err := go_openssl_EVP_CIPHER_CTX_set_padding (dec_ctx , 0 ); err != nil {
207
+ return err
208
208
}
209
209
210
210
var outl int32
@@ -236,19 +236,19 @@ func (x *cipherCBC) CryptBlocks(dst, src []byte) {
236
236
}
237
237
if len (src ) > 0 {
238
238
var outl int32
239
- if go_openssl_EVP_CipherUpdate (x .ctx , base (dst ), & outl , base (src ), int32 (len (src ))) != 1 {
240
- panic ("crypto/cipher: CipherUpdate failed" )
239
+ if _ , err := go_openssl_EVP_CipherUpdate (x .ctx , base (dst ), & outl , base (src ), int32 (len (src ))); err != nil {
240
+ panic ("crypto/cipher: " + err . Error () )
241
241
}
242
242
runtime .KeepAlive (x )
243
243
}
244
244
}
245
245
246
246
func (x * cipherCBC ) SetIV (iv []byte ) {
247
247
if len (iv ) != x .blockSize {
248
- panic ("cipher: incorrect length IV" )
248
+ panic ("crypto/ cipher: incorrect length IV" )
249
249
}
250
- if go_openssl_EVP_CipherInit_ex (x .ctx , nil , nil , nil , base (iv ), int32 (cipherOpNone )) != 1 {
251
- panic ("cipher: unable to initialize EVP cipher ctx" )
250
+ if _ , err := go_openssl_EVP_CipherInit_ex (x .ctx , nil , nil , nil , base (iv ), int32 (cipherOpNone )); err != nil {
251
+ panic ("crypto/ cipher: " + err . Error () )
252
252
}
253
253
}
254
254
@@ -259,8 +259,8 @@ func (c *evpCipher) newCBC(iv []byte, op cipherOp) cipher.BlockMode {
259
259
}
260
260
x := & cipherCBC {ctx : ctx , blockSize : c .blockSize }
261
261
runtime .SetFinalizer (x , (* cipherCBC ).finalize )
262
- if go_openssl_EVP_CIPHER_CTX_set_padding (x .ctx , 0 ) != 1 {
263
- panic ("cipher: unable to set padding" )
262
+ if _ , err := go_openssl_EVP_CIPHER_CTX_set_padding (x .ctx , 0 ); err != nil {
263
+ panic ("crypto/ cipher: " + err . Error () )
264
264
}
265
265
return x
266
266
}
@@ -280,8 +280,8 @@ func (x *cipherCTR) XORKeyStream(dst, src []byte) {
280
280
return
281
281
}
282
282
var outl int32
283
- if go_openssl_EVP_EncryptUpdate (x .ctx , base (dst ), & outl , base (src ), int32 (len (src ))) != 1 {
284
- panic ("crypto/cipher: EncryptUpdate failed" )
283
+ if _ , err := go_openssl_EVP_EncryptUpdate (x .ctx , base (dst ), & outl , base (src ), int32 (len (src ))); err != nil {
284
+ panic ("crypto/cipher: " + err . Error () )
285
285
}
286
286
runtime .KeepAlive (x )
287
287
}
@@ -455,22 +455,24 @@ func (g *cipherGCM) Seal(dst, nonce, plaintext, aad []byte) []byte {
455
455
// relying in the explicit nonce being securely set externally,
456
456
// and it also gives some interesting speed gains.
457
457
// Unfortunately we can't use it because Go expects AEAD.Seal to honor the provided nonce.
458
- if go_openssl_EVP_EncryptInit_ex (ctx , nil , nil , nil , base (nonce )) != 1 {
459
- panic (newOpenSSLError ( "EVP_EncryptInit_ex" ) )
458
+ if _ , err := go_openssl_EVP_EncryptInit_ex (ctx , nil , nil , nil , base (nonce )); err != nil {
459
+ panic (err )
460
460
}
461
461
var outl , discard int32
462
- if go_openssl_EVP_EncryptUpdate (ctx , nil , & discard , baseNeverEmpty (aad ), int32 (len (aad ))) != 1 ||
463
- go_openssl_EVP_EncryptUpdate (ctx , base (out ), & outl , baseNeverEmpty (plaintext ), int32 (len (plaintext ))) != 1 {
464
- panic (newOpenSSLError ("EVP_EncryptUpdate" ))
462
+ if _ , err := go_openssl_EVP_EncryptUpdate (ctx , nil , & discard , baseNeverEmpty (aad ), int32 (len (aad ))); err != nil {
463
+ panic (err )
464
+ }
465
+ if _ , err := go_openssl_EVP_EncryptUpdate (ctx , base (out ), & outl , baseNeverEmpty (plaintext ), int32 (len (plaintext ))); err != nil {
466
+ panic (err )
465
467
}
466
468
if len (plaintext ) != int (outl ) {
467
469
panic ("cipher: incorrect length returned from GCM EncryptUpdate" )
468
470
}
469
- if go_openssl_EVP_EncryptFinal_ex (ctx , base (out [outl :]), & discard ) != 1 {
470
- panic (newOpenSSLError ( "EVP_EncryptFinal_ex" ) )
471
+ if _ , err := go_openssl_EVP_EncryptFinal_ex (ctx , base (out [outl :]), & discard ); err != nil {
472
+ panic (err )
471
473
}
472
- if go_openssl_EVP_CIPHER_CTX_ctrl (ctx , _EVP_CTRL_GCM_GET_TAG , 16 , unsafe .Pointer (base (out [outl :]))) != 1 {
473
- panic (newOpenSSLError ( "EVP_CIPHER_CTX_ctrl" ) )
474
+ if _ , err := go_openssl_EVP_CIPHER_CTX_ctrl (ctx , _EVP_CTRL_GCM_GET_TAG , 16 , unsafe .Pointer (base (out [outl :]))); err != nil {
475
+ panic (err )
474
476
}
475
477
runtime .KeepAlive (g )
476
478
return ret
@@ -515,21 +517,23 @@ func (g *cipherGCM) Open(dst, nonce, ciphertext, aad []byte) (_ []byte, err erro
515
517
}
516
518
}
517
519
}()
518
- if go_openssl_EVP_DecryptInit_ex (ctx , nil , nil , nil , base (nonce )) != 1 {
520
+ if _ , err := go_openssl_EVP_DecryptInit_ex (ctx , nil , nil , nil , base (nonce )); err != nil {
519
521
return nil , errOpen
520
522
}
521
- if go_openssl_EVP_CIPHER_CTX_ctrl (ctx , _EVP_CTRL_GCM_SET_TAG , 16 , unsafe .Pointer (base (tag ))) != 1 {
523
+ if _ , err := go_openssl_EVP_CIPHER_CTX_ctrl (ctx , _EVP_CTRL_GCM_SET_TAG , 16 , unsafe .Pointer (base (tag ))); err != nil {
522
524
return nil , errOpen
523
525
}
524
526
var outl , discard int32
525
- if go_openssl_EVP_DecryptUpdate (ctx , nil , & discard , baseNeverEmpty (aad ), int32 (len (aad ))) != 1 ||
526
- go_openssl_EVP_DecryptUpdate (ctx , base (out ), & outl , baseNeverEmpty (ciphertext ), int32 (len (ciphertext ))) != 1 {
527
+ if _ , err := go_openssl_EVP_DecryptUpdate (ctx , nil , & discard , baseNeverEmpty (aad ), int32 (len (aad ))); err != nil {
528
+ return nil , errOpen
529
+ }
530
+ if _ , err := go_openssl_EVP_DecryptUpdate (ctx , base (out ), & outl , baseNeverEmpty (ciphertext ), int32 (len (ciphertext ))); err != nil {
527
531
return nil , errOpen
528
532
}
529
533
if len (ciphertext ) != int (outl ) {
530
534
return nil , errOpen
531
535
}
532
- if go_openssl_EVP_DecryptFinal_ex (ctx , base (out [outl :]), & discard ) != 1 {
536
+ if _ , err := go_openssl_EVP_DecryptFinal_ex (ctx , base (out [outl :]), & discard ); err != nil {
533
537
return nil , errOpen
534
538
}
535
539
runtime .KeepAlive (g )
@@ -553,9 +557,9 @@ func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []
553
557
if cipher == nil {
554
558
panic ("crypto/cipher: unsupported cipher: " + kind .String ())
555
559
}
556
- ctx := go_openssl_EVP_CIPHER_CTX_new ()
557
- if ctx = = nil {
558
- return nil , fail ( "unable to create EVP cipher ctx" )
560
+ ctx , err := go_openssl_EVP_CIPHER_CTX_new ()
561
+ if err ! = nil {
562
+ return nil , err
559
563
}
560
564
defer func () {
561
565
if err != nil {
@@ -566,17 +570,17 @@ func newCipherCtx(kind cipherKind, mode cipherMode, encrypt cipherOp, key, iv []
566
570
// RC4 cipher supports a variable key length.
567
571
// We need to set the key length before setting the key,
568
572
// and to do so we need to have an initialized cipher ctx.
569
- if go_openssl_EVP_CipherInit_ex (ctx , cipher , nil , nil , nil , int32 (encrypt )) != 1 {
570
- return nil , newOpenSSLError ( "EVP_CipherInit_ex" )
573
+ if _ , err := go_openssl_EVP_CipherInit_ex (ctx , cipher , nil , nil , nil , int32 (encrypt )); err != nil {
574
+ return nil , err
571
575
}
572
- if go_openssl_EVP_CIPHER_CTX_set_key_length (ctx , int32 (len (key ))) != 1 {
573
- return nil , newOpenSSLError ( "EVP_CIPHER_CTX_set_key_length" )
576
+ if _ , err := go_openssl_EVP_CIPHER_CTX_set_key_length (ctx , int32 (len (key ))); err != nil {
577
+ return nil , err
574
578
}
575
579
// Pass nil to the next call to EVP_CipherInit_ex to avoid resetting ctx's cipher.
576
580
cipher = nil
577
581
}
578
- if go_openssl_EVP_CipherInit_ex (ctx , cipher , nil , base (key ), base (iv ), int32 (encrypt )) != 1 {
579
- return nil , newOpenSSLError ( "unable to initialize EVP cipher ctx" )
582
+ if _ , err := go_openssl_EVP_CipherInit_ex (ctx , cipher , nil , base (key ), base (iv ), int32 (encrypt )); err != nil {
583
+ return nil , err
580
584
}
581
585
return ctx , nil
582
586
}
0 commit comments