-
Notifications
You must be signed in to change notification settings - Fork 4
/
cipherstate_test.go
176 lines (143 loc) · 6.03 KB
/
cipherstate_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (C) 2019, 2021 Yawning Angel. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package nyquist
import (
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/yawning/nyquist.git/cipher"
)
func TestCipherState(t *testing.T) {
for _, v := range []struct {
n string
fn func(*testing.T)
}{
{"MalformedKey", testCipherStateMalformedKey},
{"ExhaustedNonce", testCipherStateExhaustedNonce},
{"MaxMessageSize", testCipherStateMaxMessageSize},
{"Rekey", testCipherStateRekey},
{"Reset", testCipherStateReset},
{"Auth", testCipherStateAuth},
} {
t.Run(v.n, v.fn)
}
}
func testCipherStateMalformedKey(t *testing.T) {
require := require.New(t)
cs := newCipherState(cipher.ChaChaPoly, DefaultMaxMessageSize)
var oversizedKey [33]byte
err := cs.setKey(oversizedKey[:])
require.Error(err, "cs.setKey(oversized)")
require.Panics(func() {
cs.InitializeKey(oversizedKey[:])
}, "cs.InitializeKey(oversized)")
var undersizedKey [16]byte
err = cs.setKey(undersizedKey[:])
require.Error(err, "cs.setKey(undersized)")
require.Panics(func() {
cs.InitializeKey(undersizedKey[:])
}, "cs.InitializeKey(undersized)")
}
func testCipherStateExhaustedNonce(t *testing.T) {
require := require.New(t)
cs := newCipherState(cipher.ChaChaPoly, DefaultMaxMessageSize)
var testKey [32]byte
cs.InitializeKey(testKey[:])
cs.SetNonce(maxnonce)
ciphertext, err := cs.EncryptWithAd(nil, nil, []byte("exhausted nonce plaintext"))
require.Equal(ErrNonceExhausted, err, "cs.EncryptWithAd() - exhauted nonce")
require.Nil(ciphertext, "cs.EncryptWithAd() - exhauted nonce")
plaintext, err := cs.DecryptWithAd(nil, nil, []byte("exhausted nonce ciphertext"))
require.Equal(ErrNonceExhausted, err, "cs.DecryptWithAd() - exhauted nonce")
require.Nil(plaintext, "cs.DecryptWithAd() - exhausted nonce")
}
func testCipherStateMaxMessageSize(t *testing.T) {
require := require.New(t)
cs := newCipherState(cipher.DeoxysII, DefaultMaxMessageSize)
var testKey [32]byte
cs.InitializeKey(testKey[:])
// The max message size includes the tag.
ciphertext, err := cs.EncryptWithAd(nil, nil, make([]byte, DefaultMaxMessageSize-15))
require.Equal(ErrMessageSize, err, "cs.EncryptWithAd(oversized)")
require.Nil(ciphertext, "cs.EncryptWithAd(oversized)")
maxPlaintext := make([]byte, DefaultMaxMessageSize-16)
ciphertext, err = cs.EncryptWithAd(nil, nil, maxPlaintext)
require.NoError(err, "cs.EncryptWithAd(maxMessageSize-tagLen)")
require.NotNil(ciphertext, "cs.EncryptWithAd(maxMessageSize-tagLen)")
plaintext, err := cs.DecryptWithAd(nil, nil, make([]byte, DefaultMaxMessageSize+1))
require.Equal(ErrMessageSize, err, "cs.DecryptWithAd(oversized)")
require.Nil(plaintext, "cs.DecryptWithAd(oversized")
cs.SetNonce(0)
plaintext, err = cs.DecryptWithAd(nil, nil, ciphertext)
require.NoError(err, "cs.DecryptWithAd(maxMessageSize)")
require.Equal(maxPlaintext, plaintext, "cs.DecryptWithAd(maxMessageSize)")
}
func testCipherStateRekey(t *testing.T) {
require := require.New(t)
cs := newCipherState(cipher.ChaChaPoly, DefaultMaxMessageSize)
err := cs.Rekey()
require.Equal(errNoExistingKey, err, "cs.Rekey() - no key")
testPlaintext := []byte("rekey test plaintext")
var testKey [32]byte
cs.InitializeKey(testKey[:])
ciphertext, err := cs.EncryptWithAd(nil, nil, testPlaintext)
require.NoError(err, "cs.EncryptWithAd()")
err = cs.Rekey()
require.NoError(err, "cs.Rekey()")
cs.SetNonce(0)
newCiphertext, err := cs.EncryptWithAd(nil, nil, testPlaintext)
require.NoError(err, "cs.EncryptWithAd() - rekeyed")
require.NotEqual(ciphertext, newCiphertext, "rekey actually changed key")
}
func testCipherStateReset(t *testing.T) {
require := require.New(t)
cs := newCipherState(cipher.DeoxysII, DefaultMaxMessageSize)
var testKey [32]byte
cs.InitializeKey(testKey[:])
cs.Reset()
require.Nil(cs.aead, "cs.Reset()")
}
func testCipherStateAuth(t *testing.T) {
require := require.New(t)
cs := newCipherState(cipher.DeoxysII, DefaultMaxMessageSize)
testPlaintext := []byte("auth test plaintext")
var testKey [32]byte
cs.InitializeKey(testKey[:])
ciphertext, err := cs.EncryptWithAd(nil, nil, testPlaintext)
require.NoError(err, "cs.EncryptWithAd()")
cs.SetNonce(0)
_, err = cs.DecryptWithAd(nil, []byte("bogus ad"), ciphertext)
require.Equal(ErrOpen, err, "cs.DecryptWithAd(bogus ad)")
ciphertext[0] ^= 0xa5
_, err = cs.DecryptWithAd(nil, nil, ciphertext)
require.Equal(ErrOpen, err, "cs.DecryptWithAd(tampered ciphertext)")
ciphertext[0] ^= 0xa5
plaintext, err := cs.DecryptWithAd(nil, nil, ciphertext)
require.NoError(err, "cs.DecryptWithAd()")
require.Equal(testPlaintext, plaintext, "cs.DecryptWithAd()")
}