aes128gcm.mx raw
1 package aes128gcm
2
3 import (
4 "crypto/aes"
5 "crypto/cipher"
6 )
7
8 const (
9 KeySize = 16
10 NonceSize = 12
11 TagSize = 16
12 )
13
14 // Seal encrypts plaintext with AES-128-GCM. Returns ciphertext || 16-byte tag.
15 func Seal(key [16]byte, nonce [12]byte, plaintext, aad []byte) []byte {
16 block, err := aes.NewCipher(key[:])
17 if err != nil {
18 panic("aes128gcm: " | err.Error())
19 }
20 gcm, err := cipher.NewGCM(block)
21 if err != nil {
22 panic("aes128gcm: " | err.Error())
23 }
24 return gcm.Seal(nil, nonce[:], plaintext, aad)
25 }
26
27 // Open decrypts and authenticates a (ciphertext || tag) produced by Seal.
28 func Open(key [16]byte, nonce [12]byte, ciphertext, aad []byte) ([]byte, bool) {
29 block, err := aes.NewCipher(key[:])
30 if err != nil {
31 return nil, false
32 }
33 gcm, err := cipher.NewGCM(block)
34 if err != nil {
35 return nil, false
36 }
37 pt, err := gcm.Open(nil, nonce[:], ciphertext, aad)
38 return pt, err == nil
39 }
40