-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpresent_test.go
More file actions
124 lines (113 loc) · 2.72 KB
/
Copy pathpresent_test.go
File metadata and controls
124 lines (113 loc) · 2.72 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package present_test
import (
"encoding/hex"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yi-jiayu/PRESENT.go"
)
var cases = []struct {
Key string
Plaintext string
Ciphertext string
}{
{
Key: "00000000000000000000",
Plaintext: "0000000000000000",
Ciphertext: "5579C1387B228445",
},
{
Key: "FFFFFFFFFFFFFFFFFFFF",
Plaintext: "0000000000000000",
Ciphertext: "E72C46C0F5945049",
},
{
Key: "00000000000000000000",
Plaintext: "FFFFFFFFFFFFFFFF",
Ciphertext: "A112FFC72F68417B",
},
{
Key: "FFFFFFFFFFFFFFFFFFFF",
Plaintext: "FFFFFFFFFFFFFFFF",
Ciphertext: "3333DCD3213210D2",
},
// test vector for 128-bit key from pypresent.py example
{
Key: "0123456789abcdef0123456789abcdef",
Plaintext: "0123456789abcdef",
Ciphertext: "0e9d28685e671dd6",
},
// test vectors generated by generate_test_vectors.py
{
Key: "00000000000000000000000000000000",
Plaintext: "0000000000000000",
Ciphertext: "96db702a2e6900af",
},
{
Key: "00000000000000000000000000000000",
Plaintext: "FFFFFFFFFFFFFFFF",
Ciphertext: "3c6019e5e5edd563",
},
{
Key: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
Plaintext: "0000000000000000",
Ciphertext: "13238c710272a5d8",
},
{
Key: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
Plaintext: "FFFFFFFFFFFFFFFF",
Ciphertext: "628d9fbd4218e5b4",
},
}
func decodeHex(s string) []byte {
src := []byte(s)
dst := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(dst, src)
if err != nil {
panic(err)
}
return dst
}
func TestNewCipher(t *testing.T) {
t.Run("invalid key size", func(t *testing.T) {
var key []byte
_, err := present.NewCipher(key)
if err == nil {
t.Fail()
}
assert.Equal(t, present.KeySizeError(0), err)
assert.Equal(t, "present: invalid key size 0", err.Error())
})
}
func TestBlock_Encrypt(t *testing.T) {
for _, c := range cases {
t.Run(fmt.Sprintf("%d-bit key", len(c.Key)*4), func(t *testing.T) {
key := decodeHex(c.Key)
cipher, err := present.NewCipher(key)
if err != nil {
t.Fatal(err)
}
plaintext := decodeHex(c.Plaintext)
dst := make([]byte, cipher.BlockSize())
cipher.Encrypt(dst, plaintext)
ciphertext := decodeHex(c.Ciphertext)
assert.Equal(t, ciphertext, dst)
})
}
}
func TestBlock_Decrypt(t *testing.T) {
for _, c := range cases {
t.Run(fmt.Sprintf("%d-bit key", len(c.Key)*4), func(t *testing.T) {
key := decodeHex(c.Key)
cipher, err := present.NewCipher(key)
if err != nil {
t.Fatal(err)
}
ciphertext := decodeHex(c.Ciphertext)
dst := make([]byte, cipher.BlockSize())
cipher.Decrypt(dst, ciphertext)
plaintext := decodeHex(c.Plaintext)
assert.Equal(t, plaintext, dst)
})
}
}