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