-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathskm.go
More file actions
238 lines (193 loc) · 7.05 KB
/
skm.go
File metadata and controls
238 lines (193 loc) · 7.05 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2020-2025 Daniel Bourdrez. All Rights Reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree or at
// https://spdx.org/licenses/MIT.html
package opaque
import (
"encoding/hex"
"errors"
"slices"
"github.com/bytemare/ecc"
"github.com/bytemare/opaque/internal"
"github.com/bytemare/opaque/internal/encoding"
)
// ServerKeyMaterial holds the server's long-term identity and key material for OPAQUE registration and authentication
// sessions. Note that, depending on the setup, these values are not client specific and can be reused across clients.
type ServerKeyMaterial struct {
// The server's long-term secret key. Required only for Login, and not used in Registration.
PrivateKey *ecc.Scalar
// The server's public key in bytes. Must be provided during registration and login.
PublicKeyBytes []byte
// The seed to derive the OPRF key for the clients with. Required if client OPRF keys won't be provided directly.
OPRFGlobalSeed []byte
// The server's identity. If empty, the server's public key will be used as the identity.
Identity []byte
}
// Flush does a best-effort attempt to clear the server key material from memory. It is not guaranteed that the contents
// are correctly wiped from memory.
func (s *ServerKeyMaterial) Flush() {
internal.ClearScalar(&s.PrivateKey)
internal.ClearSlice(&s.PublicKeyBytes)
internal.ClearSlice(&s.OPRFGlobalSeed)
internal.ClearSlice(&s.Identity)
}
// Encode encodes the server key material into a byte slice.
func (s *ServerKeyMaterial) Encode() []byte {
return encoding.Concatenate(
[]byte{byte(s.PrivateKey.Group())},
encoding.EncodeVector(s.PrivateKey.Encode()),
encoding.EncodeVector(s.PublicKeyBytes),
encoding.EncodeVector(s.OPRFGlobalSeed),
encoding.EncodeVector(s.Identity),
)
}
// Hex encodes the server key material into a hex string.
func (s *ServerKeyMaterial) Hex() string {
return hex.EncodeToString(s.Encode())
}
// DecodeServerKeyMaterial decodes the server key material from a byte slice.
func (c *Configuration) DecodeServerKeyMaterial(data []byte) (*ServerKeyMaterial, error) {
if err := c.skmVerifyEncoding(data); err != nil {
return nil, ErrServerKeyMaterial.Join(err)
}
var skBytes, pkBytes, seed, id []byte
if err := encoding.DecodeLongVector(data[1:], &skBytes, &pkBytes, &seed, &id); err != nil {
return nil, ErrServerKeyMaterial.Join(err)
}
g := c.AKE.Group()
sk, err := DeserializeScalar(g, skBytes)
if err != nil {
return nil, ErrServerKeyMaterial.Join(internal.ErrInvalidPrivateKey, err)
}
pk, err := DeserializeElement(g, pkBytes)
if err != nil {
return nil, ErrServerKeyMaterial.Join(
internal.ErrInvalidServerPublicKey,
internal.ErrInvalidPublicKeyBytes,
err,
)
}
if c.AKE.Group().Base().Equal(pk) {
return nil, ErrServerKeyMaterial.Join(internal.ErrInvalidServerPublicKey, internal.ErrElementIsBase)
}
if !pk.Equal(g.Base().Multiply(sk)) {
return nil, ErrServerKeyMaterial.Join(internal.ErrInvalidServerPublicKey)
}
// The following is never triggered due to the structure check and failsafe above, but is kept for safety.
// If the seed is empty, we set it to nil to avoid confusion.
// This is useful for cases where the seed is not used, and the OPRF client key is provided directly.
if len(seed) != c.Hash.Size() {
if len(seed) != 0 {
return nil, ErrServerKeyMaterial.Join(internal.ErrInvalidOPRFSeedLength)
}
seed = nil
}
return &ServerKeyMaterial{
Identity: slices.Clone(id),
PrivateKey: sk,
PublicKeyBytes: slices.Clone(pkBytes),
OPRFGlobalSeed: slices.Clone(seed),
}, nil
}
// skmVerifyEncoding checks the structure of the server key material data given the internal length headers.
func (c *Configuration) skmVerifyEncoding(data []byte) error {
if len(data) < 9 {
return internal.ErrInvalidEncodingLength
}
g := Group(data[0])
if !g.Available() {
return internal.ErrInvalidGroupEncoding
}
if g.Group() != c.AKE.Group() {
return internal.ErrWrongGroup
}
// calculate minimal length given the group, when the private key is not set and seed and identity lengths are 0
// 1 byte for group, 2 bytes for sk length, 2 bytes for pk length, public key length, 2 bytes for seed length,
// 2 bytes for id length.
minLength := 1 + 2 + 2 + g.Group().ElementLength() + 2 + 2
if len(data) < minLength {
return internal.ErrInvalidEncodingLength
}
skh := encoding.OS2IP(data[1:3]) // sk
if g.Group().ScalarLength() != skh {
return errors.Join(internal.ErrInvalidPrivateKey, internal.ErrInvalidScalar, internal.ErrInvalidEncodingLength)
}
// The following is never triggered due to the structure check and failsafe above, but is kept for safety.
// The earlier minLength check guarantees len(data) ≥ elementLen+9, and for every configured curve
// elementLen+9 ≥ 5+scalarLen = offset+2, so len(data) < offset+2 can never fire.
offset := 3 + skh
if len(data) < offset+2 {
return errors.Join(
internal.ErrInvalidServerPublicKey,
internal.ErrInvalidElement,
internal.ErrInvalidEncodingLength,
)
}
// Verify public key length
step, err := skmVerifyEncodingHeaderPublicKey(data[offset:], g.Group().ElementLength())
if err != nil {
return errors.Join(internal.ErrInvalidServerPublicKey, internal.ErrInvalidElement, err)
}
offset += step
if len(data) < offset+2 {
return internal.ErrInvalidEncodingLength
}
// Verify seed length
step, err = skmVerifyEncodingHeaderOPRFSeed(data[offset:], c.Hash.Size())
if err != nil {
return errors.Join(internal.ErrInvalidOPRFSeedLength, err)
}
offset += step
if len(data) < offset+2 {
return internal.ErrInvalidEncodingLength
}
// Verify id length (allowed to be 0)
idh := encoding.OS2IP(data[offset : offset+2])
offset += 2
if len(data) != offset+idh {
return internal.ErrInvalidEncodingLength
}
return nil
}
func skmVerifyEncodingHeaderPublicKey(data []byte, refLength int) (int, error) {
// The following is never triggered due to the structure check and failsafe before the call, but is kept for safety.
if len(data) < 2 {
return 0, internal.ErrInvalidEncodingLength
}
header := encoding.OS2IP(data[:2])
if refLength != header {
return 0, internal.ErrInvalidEncodingLength
}
offset := 2
if len(data) < offset+header {
return 0, internal.ErrInvalidEncodingLength
}
return offset + header, nil
}
func skmVerifyEncodingHeaderOPRFSeed(data []byte, refLength int) (int, error) {
// OPRF seed can be of length 0
sh := encoding.OS2IP(data[:2])
if sh != 0 {
if refLength != sh {
return 0, internal.ErrInvalidOPRFSeedLength
}
if len(data) < 2+sh {
return 0, internal.ErrInvalidOPRFSeedLength
}
}
return 2 + sh, nil
}
// DecodeServerKeyMaterialHex decodes the server key material from a hex string.
func (c *Configuration) DecodeServerKeyMaterialHex(data string) (*ServerKeyMaterial, error) {
if data == "" {
return nil, ErrServerKeyMaterial.Join(internal.ErrDecodingEmptyHex)
}
decoded, err := hex.DecodeString(data)
if err != nil {
return nil, ErrServerKeyMaterial.Join(err)
}
return c.DecodeServerKeyMaterial(decoded)
}