-
Notifications
You must be signed in to change notification settings - Fork 0
/
aead.go
49 lines (40 loc) · 1.15 KB
/
aead.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-FileCopyrightText: 2024 Thibault NORMAND <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0 AND MIT
package stronghold
import (
"crypto/aes"
"crypto/cipher"
"golang.org/x/crypto/chacha20poly1305"
)
// AEAD is the authenticated encryption with associated data type.
type AEAD uint8
const (
// AESGCM is the authenticated encryption with associated data.
AESGCM AEAD = iota
// CHACHAPOLY is the authenticated encryption with associated data.
CHACHAPOLY
)
// aeadRegistry is the authenticated encryption with associated data registry.
var aeadRegistry = map[AEAD]func([]byte) (cipher.AEAD, error) {
AESGCM: func(key []byte) (cipher.AEAD, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, &operationError{operation: "aesgcm", err: err}
}
aead, err := cipher.NewGCMWithNonceSize(block, saltSize)
if err != nil {
return nil, &operationError{operation: "aesgcm", err: err}
}
return aead, nil
},
CHACHAPOLY: func(key []byte) (cipher.AEAD, error) {
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, &operationError{
operation: "chachapoly",
err: err}
}
return aead, nil
},
}