@@ -381,7 +381,7 @@ func (g *cipherGCM) Overhead() int {
381
381
return gcmTagSize
382
382
}
383
383
384
- func (g * cipherGCM ) Seal (dst , nonce , plaintext , additionalData []byte ) []byte {
384
+ func (g * cipherGCM ) Seal (dst , nonce , plaintext , aad []byte ) []byte {
385
385
if len (nonce ) != gcmStandardNonceSize {
386
386
panic ("cipher: incorrect nonce length given to GCM" )
387
387
}
@@ -392,9 +392,9 @@ func (g *cipherGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
392
392
panic ("cipher: message too large for buffer" )
393
393
}
394
394
if g .tls != cipherGCMTLSNone {
395
- if g .tls == cipherGCMTLS12 && len (additionalData ) != gcmTls12AddSize {
395
+ if g .tls == cipherGCMTLS12 && len (aad ) != gcmTls12AddSize {
396
396
panic ("cipher: incorrect additional data length given to GCM TLS 1.2" )
397
- } else if g .tls == cipherGCMTLS13 && len (additionalData ) != gcmTls13AddSize {
397
+ } else if g .tls == cipherGCMTLS13 && len (aad ) != gcmTls13AddSize {
398
398
panic ("cipher: incorrect additional data length given to GCM TLS 1.3" )
399
399
}
400
400
counter := binary .BigEndian .Uint64 (nonce [gcmTlsFixedNonceSize :])
@@ -457,19 +457,30 @@ func (g *cipherGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
457
457
// relying in the explicit nonce being securely set externally,
458
458
// and it also gives some interesting speed gains.
459
459
// Unfortunately we can't use it because Go expects AEAD.Seal to honor the provided nonce.
460
- if C .go_openssl_EVP_CIPHER_CTX_seal_wrapper (ctx , base (out ), base (nonce ),
461
- base (plaintext ), C .int (len (plaintext )),
462
- base (additionalData ), C .int (len (additionalData ))) != 1 {
463
-
464
- panic (fail ("EVP_CIPHER_CTX_seal" ))
460
+ if C .go_openssl_EVP_EncryptInit_ex (ctx , nil , nil , nil , base (nonce )) != 1 {
461
+ panic (newOpenSSLError ("EVP_EncryptInit_ex" ))
462
+ }
463
+ var outl , discard C.int
464
+ if C .go_openssl_EVP_EncryptUpdate (ctx , nil , & discard , baseNeverEmpty (aad ), C .int (len (aad ))) != 1 ||
465
+ C .go_openssl_EVP_EncryptUpdate (ctx , base (out ), & outl , baseNeverEmpty (plaintext ), C .int (len (plaintext ))) != 1 {
466
+ panic (newOpenSSLError ("EVP_EncryptUpdate" ))
467
+ }
468
+ if len (plaintext ) != int (outl ) {
469
+ panic ("cipher: incorrect length returned from GCM EncryptUpdate" )
470
+ }
471
+ if C .go_openssl_EVP_EncryptFinal_ex (ctx , base (out [outl :]), & discard ) != 1 {
472
+ panic (newOpenSSLError ("EVP_EncryptFinal_ex" ))
473
+ }
474
+ if C .go_openssl_EVP_CIPHER_CTX_ctrl (ctx , C .GO_EVP_CTRL_GCM_GET_TAG , 16 , unsafe .Pointer (base (out [outl :]))) != 1 {
475
+ panic (newOpenSSLError ("EVP_CIPHER_CTX_ctrl" ))
465
476
}
466
477
runtime .KeepAlive (g )
467
478
return ret
468
479
}
469
480
470
481
var errOpen = errors .New ("cipher: message authentication failed" )
471
482
472
- func (g * cipherGCM ) Open (dst , nonce , ciphertext , additionalData []byte ) ([]byte , error ) {
483
+ func (g * cipherGCM ) Open (dst , nonce , ciphertext , aad []byte ) (_ []byte , err error ) {
473
484
if len (nonce ) != gcmStandardNonceSize {
474
485
panic ("cipher: incorrect nonce length given to GCM" )
475
486
}
@@ -497,18 +508,33 @@ func (g *cipherGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte,
497
508
return nil , err
498
509
}
499
510
defer C .go_openssl_EVP_CIPHER_CTX_free (ctx )
500
- ok := C .go_openssl_EVP_CIPHER_CTX_open_wrapper (
501
- ctx , base (out ), base (nonce ),
502
- base (ciphertext ), C .int (len (ciphertext )),
503
- base (additionalData ), C .int (len (additionalData )), base (tag ))
504
- runtime .KeepAlive (g )
505
- if ok == 0 {
506
- // Zero output buffer on error.
507
- for i := range out {
508
- out [i ] = 0
511
+
512
+ defer func () {
513
+ if err != nil {
514
+ // Zero output buffer on error.
515
+ for i := range out {
516
+ out [i ] = 0
517
+ }
509
518
}
519
+ }()
520
+ if C .go_openssl_EVP_DecryptInit_ex (ctx , nil , nil , nil , base (nonce )) != 1 {
521
+ return nil , errOpen
522
+ }
523
+ if C .go_openssl_EVP_CIPHER_CTX_ctrl (ctx , C .GO_EVP_CTRL_GCM_SET_TAG , 16 , unsafe .Pointer (base (tag ))) != 1 {
524
+ return nil , errOpen
525
+ }
526
+ var outl , discard C.int
527
+ if C .go_openssl_EVP_DecryptUpdate (ctx , nil , & discard , baseNeverEmpty (aad ), C .int (len (aad ))) != 1 ||
528
+ C .go_openssl_EVP_DecryptUpdate (ctx , base (out ), & outl , baseNeverEmpty (ciphertext ), C .int (len (ciphertext ))) != 1 {
510
529
return nil , errOpen
511
530
}
531
+ if len (ciphertext ) != int (outl ) {
532
+ return nil , errOpen
533
+ }
534
+ if C .go_openssl_EVP_DecryptFinal_ex (ctx , base (out [outl :]), & discard ) != 1 {
535
+ return nil , errOpen
536
+ }
537
+ runtime .KeepAlive (g )
512
538
return ret , nil
513
539
}
514
540
0 commit comments