-
Notifications
You must be signed in to change notification settings - Fork 4
/
liboqs_test.go
167 lines (136 loc) · 4.46 KB
/
liboqs_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
// Copyright 2019 Thales UK Limited
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package goliboqs
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const libPath = "/usr/local/liboqs/lib/liboqs.so"
func TestRoundTrip(t *testing.T) {
kems := []KemType{
KemBike1L1,
KemBike1L3,
KemBike1L5,
KemBike2L1,
KemBike2L3,
KemBike2L5,
KemBike3L1,
KemBike3L3,
KemBike3L5,
KemFrodo640AES,
KemFrodo640Shake,
KemFrodo976AES,
KemFrodo976Shake,
KemFrodo1344AES,
KemFrodo1344Shake,
KemNewHope512,
KemNewHope1024,
KemKyber512,
KemKyber768,
KemKyber1024,
KemSidhP503,
KemSidhP751,
KemSikeP503,
KemSikeP751,
}
k, err := LoadLib(libPath)
require.NoError(t, err)
defer func() { require.NoError(t, k.Close()) }()
for _, kemAlg := range kems {
t.Run(string(kemAlg), func(t *testing.T) {
//t.Parallel() <-- cannot use this because https://github.com/stretchr/testify/issues/187
testKEM, err := k.GetKem(kemAlg)
if err == errAlgDisabledOrUnknown {
t.Skipf("Skipping disabled/unknown algorithm %q", kemAlg)
}
require.NoError(t, err)
defer func() { require.NoError(t, testKEM.Close()) }()
publicKey, secretKey, err := testKEM.KeyPair()
require.NoError(t, err)
sharedSecret, ciphertext, err := testKEM.Encaps(publicKey)
require.NoError(t, err)
recoveredSecret, err := testKEM.Decaps(ciphertext, secretKey)
require.NoError(t, err)
assert.Equal(t, sharedSecret, recoveredSecret)
})
}
}
func TestBadLibrary(t *testing.T) {
_, err := LoadLib("bad")
require.Error(t, err)
require.Contains(t, err.Error(), "failed to load module")
}
func TestReEntrantLibrary(t *testing.T) {
k1, err := LoadLib(libPath)
require.NoError(t, err)
defer func() { require.NoError(t, k1.Close()) }()
k2, err := LoadLib(libPath)
require.NoError(t, err)
defer func() { require.NoError(t, k2.Close()) }()
}
func TestLibraryClosed(t *testing.T) {
k, err := LoadLib(libPath)
require.NoError(t, err)
require.NoError(t, k.Close())
const expectedMsg = "library closed"
t.Run("GetKEM", func(t *testing.T) {
_, err := k.GetKem(KemBike1L1)
require.Error(t, err)
assert.Contains(t, err.Error(), expectedMsg)
})
t.Run("Close", func(t *testing.T) {
err := k.Close()
require.Error(t, err)
assert.Contains(t, err.Error(), expectedMsg)
})
}
func TestKEMClosed(t *testing.T) {
k, err := LoadLib(libPath)
require.NoError(t, err)
defer func() { require.NoError(t, k.Close()) }()
testKEM, err := k.GetKem(KemKyber512)
require.NoError(t, err)
require.NoError(t, testKEM.Close())
t.Run("KeyPair", func(t *testing.T) {
_, _, err := testKEM.KeyPair()
assert.Equal(t, errAlreadyClosed, err)
})
t.Run("Encaps", func(t *testing.T) {
_, _, err := testKEM.Encaps(nil)
assert.Equal(t, errAlreadyClosed, err)
})
t.Run("Decaps", func(t *testing.T) {
_, err := testKEM.Decaps(nil, nil)
assert.Equal(t, errAlreadyClosed, err)
})
t.Run("Decaps", func(t *testing.T) {
err := testKEM.Close()
assert.Equal(t, errAlreadyClosed, err)
})
}
func TestInvalidKEMAlg(t *testing.T) {
k, err := LoadLib(libPath)
require.NoError(t, err)
defer func() { require.NoError(t, k.Close()) }()
_, err = k.GetKem(KemType("this will never be valid"))
assert.Equal(t, errAlgDisabledOrUnknown, err)
}
func TestLibErr(t *testing.T) {
// Difficult to test this without a deliberately failing KEM library (which could
// be a future idea...)
err := libError(operationFailed, "test%d", 123)
assert.EqualError(t, err, "test123")
}