...

Source file src/github.com/ProtonMail/go-crypto/openpgp/internal/algorithm/aead.go

Documentation: github.com/ProtonMail/go-crypto/openpgp/internal/algorithm

     1  // Copyright (C) 2019 ProtonTech AG
     2  
     3  package algorithm
     4  
     5  import (
     6  	"crypto/cipher"
     7  	"github.com/ProtonMail/go-crypto/eax"
     8  	"github.com/ProtonMail/go-crypto/ocb"
     9  )
    10  
    11  // AEADMode defines the Authenticated Encryption with Associated Data mode of
    12  // operation.
    13  type AEADMode uint8
    14  
    15  // Supported modes of operation (see RFC4880bis [EAX] and RFC7253)
    16  const (
    17  	AEADModeEAX = AEADMode(1)
    18  	AEADModeOCB = AEADMode(2)
    19  	AEADModeGCM = AEADMode(3)
    20  )
    21  
    22  // TagLength returns the length in bytes of authentication tags.
    23  func (mode AEADMode) TagLength() int {
    24  	switch mode {
    25  	case AEADModeEAX:
    26  		return 16
    27  	case AEADModeOCB:
    28  		return 16
    29  	case AEADModeGCM:
    30  		return 16
    31  	default:
    32  		return 0
    33  	}
    34  }
    35  
    36  // NonceLength returns the length in bytes of nonces.
    37  func (mode AEADMode) NonceLength() int {
    38  	switch mode {
    39  	case AEADModeEAX:
    40  		return 16
    41  	case AEADModeOCB:
    42  		return 15
    43  	case AEADModeGCM:
    44  		return 12
    45  	default:
    46  		return 0
    47  	}
    48  }
    49  
    50  // New returns a fresh instance of the given mode
    51  func (mode AEADMode) New(block cipher.Block) (alg cipher.AEAD) {
    52  	var err error
    53  	switch mode {
    54  	case AEADModeEAX:
    55  		alg, err = eax.NewEAX(block)
    56  	case AEADModeOCB:
    57  		alg, err = ocb.NewOCB(block)
    58  	case AEADModeGCM:
    59  		alg, err = cipher.NewGCM(block)
    60  	}
    61  	if err != nil {
    62  		panic(err.Error())
    63  	}
    64  	return alg
    65  }
    66  

View as plain text