-
Notifications
You must be signed in to change notification settings - Fork 1
/
aesgcm.go
executable file
·65 lines (55 loc) · 1.74 KB
/
aesgcm.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package keyexchange
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
)
// AESGCM is the Advanced Encryption Standard (AES) Galois/Counter Mode (GCM) struct. AESGCM conforms with the AEAD interface.
type AESGCM struct {
AEAD
}
// NewAESGCM creates a new AESGCM struct.
func NewAESGCM() *AESGCM {
return &AESGCM{}
}
// Encrypt encrypts using the symmetricKey, kdfNonce, plaintext, aeadNonce, and additionalData.
func (c *AESGCM) Encrypt(symmetricKey, kdfNonce, plaintext, aeadNonce, additionalData []byte) (*AEADStore, error) {
block, err := aes.NewCipher(symmetricKey)
if err != nil {
return nil, err
}
// Never use more than 2^32 random nonces with a given key because of the risk of a repeat.
if aeadNonce == nil {
aeadNonce = make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, aeadNonce); err != nil {
return nil, err
}
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
// Creates combined ciphertext -> nonce + cyphertext + additionalData
cyphertext := aeadNonce[:]
return &AEADStore{
KDFNonce: kdfNonce,
Ciphertext: gcm.Seal(cyphertext, aeadNonce, plaintext, additionalData),
AEADNonce: aeadNonce,
AdditionalData: additionalData,
}, nil
}
// Decrypt decrypts using the symmetricKey, ciphertext, aeadNonce, and additionalData.
func (c *AESGCM) Decrypt(symmetricKey, ciphertext, aeadNonce, additionalData []byte) (plaintext []byte, err error) {
block, err := aes.NewCipher(symmetricKey)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
// Strip nonce from the start of the combined ciphertext
ciphertextWithoutNonce := ciphertext[len(aeadNonce):]
return gcm.Open(nil, aeadNonce, ciphertextWithoutNonce, additionalData)
}