forked from milanbella/gocosem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_test.go
88 lines (72 loc) · 2.68 KB
/
crypto_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gocosem
import (
"gocosem/crypto/aes"
"gocosem/crypto/cipher"
"testing"
)
func TestCrypto_aesgcm(t *testing.T) {
IV := []byte{0x00, 0x00, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
ADD := []byte{0x30, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF}
EK := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}
plaintext := []byte("1234567890123456789")
err, ciphertext, tag := aesgcm(EK, IV, ADD, ([]byte)(plaintext), 0)
if nil != err {
t.Fatal(err)
}
t.Logf("plaintext: %s", string(plaintext))
t.Logf("ciphertext: % 0X", ciphertext)
t.Logf("tag: % 0X", tag)
plaintext = []byte("0000000000000000000")
tag = []byte{}
err, plaintext, tag = aesgcm(EK, IV, ADD, ([]byte)(ciphertext), 0)
if nil != err {
t.Fatal(err)
}
t.Logf("plaintext: %s", string(plaintext))
t.Logf("ciphertext: % 0X", ciphertext)
t.Logf("tag: % 0X", tag)
}
func TestCrypto_goAesgcm(t *testing.T) {
IV := []byte{0x00, 0x00, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
ADD := []byte{0x30, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF}
EK := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}
plaintext := []byte("1234567890123456789")
block, err := aes.NewCipher(EK)
if err != nil {
panic(err.Error())
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
ciphertext, tag := aesgcm.SealDlms(IV, plaintext, ADD)
t.Logf("plaintext: %s", string(plaintext))
t.Logf("ciphertext: % 0X", ciphertext)
t.Logf("tag: % 0X", tag)
plaintext, tag = aesgcm.OpenDlms(IV, ciphertext, ADD)
t.Logf("plaintext: %s", string(plaintext))
t.Logf("ciphertext: % 0X", ciphertext)
t.Logf("tag: % 0X", tag)
}
func TestCrypto_goAesgcm_noplaintext(t *testing.T) {
IV := []byte{0x00, 0x00, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
ADD := []byte{0x30, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF}
EK := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}
plaintext := []byte{}
block, err := aes.NewCipher(EK)
if err != nil {
panic(err.Error())
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
ciphertext, tag := aesgcm.SealDlms(IV, plaintext, ADD)
t.Logf("plaintext: %s", string(plaintext))
t.Logf("ciphertext: % 0X", ciphertext)
t.Logf("tag: % 0X", tag)
plaintext, tag = aesgcm.OpenDlms(IV, ciphertext, ADD)
t.Logf("plaintext: %s", string(plaintext))
t.Logf("ciphertext: % 0X", ciphertext)
t.Logf("tag: % 0X", tag)
}