forked from milanbella/gocosem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
37 lines (30 loc) · 822 Bytes
/
crypto.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
package gocosem
import (
"fmt"
"gocosem/crypto/aes"
"gocosem/crypto/cipher"
)
const GCM_TAG_LEN = 12
// Note: Use 'direction' 0 for encrypt, 'direction' 1 for decrypt.
func aesgcm(key []byte, IV []byte, adata []byte, pdu []byte, direction int) (err error, opdu []byte, tag []byte) {
block, err := aes.NewCipher(key)
if err != nil {
errorLog("%s", err)
return err, nil, nil
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
errorLog("%s", err)
return err, nil, nil
}
if 0 == direction {
opdu, tag = aesgcm.SealDlms(IV, pdu, adata) // encrypt
} else if 1 == direction {
opdu, tag = aesgcm.OpenDlms(IV, pdu, adata) // decrypt
} else {
err = fmt.Errorf("wrogn direction parameter: use 0 for encrypt, 1 for decrypt")
errorLog("%s", err)
return err, nil, nil
}
return nil, opdu, tag
}