-
Notifications
You must be signed in to change notification settings - Fork 9
/
key_test.go
152 lines (112 loc) · 3.14 KB
/
key_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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2016 - Will Glozer. All rights reserved.
package main
import (
"bytes"
"testing"
"github.com/magical/argon2"
"github.com/wg/arc/binary"
"github.com/wg/ecies/xchacha20poly1305"
)
func TestPublicKeyFormat(t *testing.T) {
p, _ := keypair(t)
b, c := StorePublicKey(t, p)
if b.buffer[1] != Public {
t.Fatal("serialized type incorrect")
}
CheckKeyFormat(t, (*[56]byte)(p), b, c)
}
func TestPrivateKeyFormat(t *testing.T) {
_, p := keypair(t)
b, c := StorePrivateKey(t, p)
if b.buffer[1] != Private {
t.Fatal("serialized type incorrect")
}
CheckKeyFormat(t, (*[56]byte)(p), b, c)
}
func CheckKeyFormat(t *testing.T, k *[56]byte, b *Buffer, c *KeyContainer) {
key, err := argon2.Key(c.Password, c.Salt[:], int(c.Iterations), 1, int64(c.Memory), KeySize)
if err != nil {
t.Fatal("password key derivation failed", err)
}
x := xchacha20poly1305.XChaCha20Poly1305{}
if err := x.Init(key, c.Nonce[:]); err != nil {
t.Fatal(err)
}
dst := [56]byte{}
tag := [TagSize]byte{}
x.Decrypt(dst[:], c.Key[:])
x.Tag(tag[:0])
if !bytes.Equal(k[:], dst[:]) {
t.Fatal("decrypted key incorrect")
}
if !bytes.Equal(tag[:], c.Tag[:]) {
t.Fatal("authentication tag incorrect")
}
if binary.LE.Uint32(b.buffer[2:6]) != c.Iterations {
t.Fatal("serialized iterations incorrect")
}
if binary.LE.Uint32(b.buffer[6:10]) != c.Memory {
t.Fatal("serialized memory incorrect")
}
if !bytes.Equal(b.buffer[10:42], c.Salt[:]) {
t.Fatal("serialized salt incorrect")
}
if !bytes.Equal(b.buffer[42:58], tag[:]) {
t.Fatal("serialized tag incorrect")
}
if !bytes.Equal(b.buffer[58:82], c.Nonce[:]) {
t.Fatal("serialized nonce incorrect")
}
}
func TestPublicPrivateKeypair(t *testing.T) {
pub, priv := keypair(t)
shared0, err := ComputeSharedKey(pub, priv, KeySize)
if err != nil {
t.Fatal(err)
}
_, puc := StorePublicKey(t, pub)
_, prc := StorePrivateKey(t, priv)
priv.Zero()
if err := puc.ReadPublicKey(pub); err != nil {
t.Fatal("failed to load public key", err)
}
if err := prc.ReadPrivateKey(priv); err != nil {
t.Fatal("failed to load private key", err)
}
shared1, err := ComputeSharedKey(pub, priv, KeySize)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(shared0, shared1) {
t.Fatal("serialized keys incorrect")
}
}
func TestWrongKeyType(t *testing.T) {
pub, priv := keypair(t)
_, pubc := StorePublicKey(t, pub)
_, privc := StorePrivateKey(t, priv)
if err := pubc.ReadPrivateKey(priv); err != ErrInvalidPrivateKey {
t.Fatal("loaded public key as private key")
}
if err := privc.ReadPublicKey(pub); err != ErrInvalidPublicKey {
t.Fatal("loaded private key as public key")
}
}
func StorePublicKey(t *testing.T, key *PublicKey) (*Buffer, *KeyContainer) {
b := &Buffer{}
c := NewKeyContainer(b, []byte(""), 1, 8)
if err := c.WritePublicKey(key); err != nil {
t.Fatal("failed to store public key", err)
}
b.Rewind()
return b, c
}
func StorePrivateKey(t *testing.T, key *PrivateKey) (*Buffer, *KeyContainer) {
b := &Buffer{}
c := NewKeyContainer(b, []byte("secret"), 1, 8)
if err := c.WritePrivateKey(key); err != nil {
t.Fatal("failed to store private key", err)
}
b.Rewind()
return b, c
}