-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymmetric_aead.go
55 lines (42 loc) · 1.21 KB
/
symmetric_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
50
51
52
53
54
55
package ecies
import (
"crypto/cipher"
"io"
)
var GCMCipherMode = SymmetricCipherMode{
Encryptor: GCMEncrypt,
Decryptor: GCMDecrypt,
}
// GCMEncrypt performs Galois/Counter Mode encryption using the block cipher specified in
// the parameters
func GCMEncrypt(rand io.Reader, params *ECIESParams, key, plaintext, authenticationData []byte) (ciphertext []byte, err error) {
blockCipher, err := params.Cipher(key)
if err != nil {
return
}
gcmCipher, err := cipher.NewGCM(blockCipher)
if err != nil {
return
}
nonce, err := GenerateIV(gcmCipher.NonceSize(), rand)
if err != nil {
return
}
ciphertext = gcmCipher.Seal(nonce, nonce, plaintext, authenticationData)
return
}
// GCMDecrypt performs Galois/Counter Mode decryption using the block cipher specified in
// the parameters
func GCMDecrypt(rand io.Reader, params *ECIESParams, key, ciphertext, authenticationData []byte) (plaintext []byte, err error) {
blockCipher, err := params.Cipher(key)
if err != nil {
return
}
gcmCipher, err := cipher.NewGCM(blockCipher)
if err != nil {
return
}
nonce := ciphertext[:gcmCipher.NonceSize()]
plaintext, err = gcmCipher.Open(nil, nonce, ciphertext[gcmCipher.NonceSize():], authenticationData)
return
}