-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoptions.go
More file actions
429 lines (342 loc) · 11.3 KB
/
options.go
File metadata and controls
429 lines (342 loc) · 11.3 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// 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 (
"fmt"
"slices"
"github.com/bytemare/ecc"
"github.com/bytemare/opaque/internal"
"github.com/bytemare/opaque/internal/ksf"
)
// ServerOptions override the secure default values or internally generated values.
// Only use this if you know what you're doing. Reusing seeds and nonces across sessions is a security risk,
// and breaks forward secrecy.
type ServerOptions struct {
ClientOPRFKey *ecc.Scalar
AKE *AKEOptions
MaskingNonce []byte
}
func (s *Server) resolveServerInputs(options []*ServerOptions) (*serverInputs, error) {
inputs := newServerInputs()
defer func() {
if inputs.SecretKeyShare == nil {
inputs.SecretKeyShare = s.conf.MakeSecretKeyShare(nil)
}
if inputs.AKENonce == nil {
inputs.AKENonce = internal.RandomBytes(s.conf.Sizes.Nonce)
}
}()
if len(options) == 0 || options[0] == nil {
return inputs, nil
}
// ClientOPRFKey
if options[0].ClientOPRFKey != nil {
if err := IsValidScalar(s.conf.OPRF.Group(), options[0].ClientOPRFKey); err != nil {
return nil, ErrServerOptions.Join(internal.ErrClientOPRFKey, err)
}
inputs.ClientOPRFKey = s.conf.OPRF.Group().NewScalar().Set(options[0].ClientOPRFKey)
}
// MaskingNonce.
if len(options[0].MaskingNonce) != 0 {
if len(options[0].MaskingNonce) != s.conf.Sizes.Nonce {
return nil, ErrServerOptions.Join(internal.ErrMaskingNonceLength)
}
inputs.MaskingNonce = make([]byte, len(options[0].MaskingNonce))
copy(inputs.MaskingNonce, options[0].MaskingNonce)
}
// AKE options.
if options[0].AKE == nil {
return inputs, nil
}
// AKE nonce.
if len(options[0].AKE.Nonce) != 0 {
inputs.AKENonce = make([]byte, len(options[0].AKE.Nonce))
copy(inputs.AKENonce, options[0].AKE.Nonce)
}
// Ephemeral secret key share.
var err error
inputs.SecretKeyShare, err = options[0].AKE.getSecretKeyShare(s.conf)
if err != nil {
return nil, ErrServerOptions.Join(err)
}
return inputs, nil
}
// ClientOptions override the secure default values or internally generated values.
// Only use this if you know what you're doing. Reusing seeds and nonces across sessions is a security risk,
// and breaks forward secrecy.
type ClientOptions struct {
OPRFBlind *ecc.Scalar
// ResumePassword is only used in RegistrationFinalize and GenerateKE3, to complete a run from a different
// client instance that does not hold the prior state.
ResumePassword []byte
AKE *AKEOptions
// ResumeKE1 is only used in GenerateKE3, to complete a run from a different client instance that does not hold
// the prior state.
ResumeKE1 []byte
KSFSalt []byte
// RegistrationEnvelopeNonce is only used in RegistrationFinalize.
RegistrationEnvelopeNonce []byte
KDFSalt []byte
KSFParameters []uint64
// RegistrationEnvelopeNonceLength is only used in RegistrationFinalize.
RegistrationEnvelopeNonceLength int
KSFLength int // If 0 or not set, will default to the OPRF length.
}
func (c *Client) validateOPRFBlindOption(blind *ecc.Scalar) (*ecc.Scalar, error) {
if blind != nil {
if err := IsValidScalar(c.conf.OPRF.Group(), blind); err != nil {
return nil, ErrClientOptions.Join(internal.ErrInvalidOPRFBlind, err)
}
}
return blind, nil
}
func resolveEnvelopeNonce(clientOptions ...*ClientOptions) ([]byte, error) {
if len(clientOptions) == 0 { // sanity check, but never triggered since we check before calling.
return internal.RandomBytes(internal.NonceLength), nil
}
nonce := clientOptions[0].RegistrationEnvelopeNonce
nonceLength := clientOptions[0].RegistrationEnvelopeNonceLength
if err := validateOptionsLength(nonce, nonceLength, internal.NonceLength); err != nil {
return nil, ErrClientOptions.Join(internal.ErrEnvelopeNonceOptions, err)
}
if nonce == nil {
if nonceLength == 0 {
nonceLength = internal.NonceLength
}
nonce = internal.RandomBytes(nonceLength)
}
return nonce, nil
}
type clientInputs struct {
Password []byte
OPRFBlind *ecc.Scalar
SecretKeyShare *ecc.Scalar
KE1 []byte
KSF *ksf.Parameters
EnvelopeNonce []byte
KDFSalt []byte
AKENonce []byte
}
func newClientInputs(conf *internal.Configuration) *clientInputs {
return &clientInputs{
Password: nil,
OPRFBlind: nil,
SecretKeyShare: nil,
KE1: nil,
KDFSalt: nil,
KSF: ksf.NewParameters(conf.OPRF.Group().ElementLength()),
AKENonce: nil,
EnvelopeNonce: nil,
}
}
func (c *Client) resolveKSFInputs(out *ksf.Parameters, in *ClientOptions) error {
if err := out.Set(c.conf.KSF, in.KSFSalt, in.KSFParameters, in.KSFLength); err != nil {
return ErrClientOptions.Join(err)
}
return nil
}
// resolveKE1 resolves the KE1 state from the client or options without mutating the client.
func (c *Client) resolveKE1(options *ClientOptions) ([]byte, error) {
if len(c.ake.ke1) != 0 {
if len(options.ResumeKE1) != 0 {
return nil, ErrClientOptions.Join(internal.ErrDoubleKE1)
}
return c.ake.ke1, nil
}
if len(options.ResumeKE1) == 0 {
return nil, ErrClientOptions.Join(internal.ErrKE1Missing)
}
// Validate that the provided KE1 message is well-formed.
if _, err := c.Deserialize.KE1(options.ResumeKE1); err != nil {
return nil, ErrClientOptions.Join(err)
}
return options.ResumeKE1, nil
}
func (c *Client) resolveFinalizeBlindCandidate(optionBlind *ecc.Scalar) (*ecc.Scalar, bool, error) {
switch {
case c.oprf.blind != nil && optionBlind != nil:
return nil, false, ErrClientOptions.Join(internal.ErrDoubleOPRFBlind)
case optionBlind != nil:
return optionBlind, true, nil
case c.oprf.blind == nil:
return nil, false, ErrClientOptions.Join(internal.ErrNoOPRFBlind)
default:
return c.oprf.blind, false, nil
}
}
func (c *Client) resolveFinalizePassword(optionPassword []byte) ([]byte, error) {
switch {
case c.oprf.password != nil && optionPassword != nil:
return nil, ErrClientOptions.Join(internal.ErrDoublePassword)
case optionPassword != nil:
return slices.Clone(optionPassword), nil
case c.oprf.password == nil:
return nil, ErrClientOptions.Join(internal.ErrNoPassword)
default:
return slices.Clone(c.oprf.password), nil
}
}
func (c *Client) resolveOPRFFinalizeInputs(inputs *clientInputs, optionBlind *ecc.Scalar, optionPassword []byte) error {
blind, validateBlind, err := c.resolveFinalizeBlindCandidate(optionBlind)
if err != nil {
return err
}
password, err := c.resolveFinalizePassword(optionPassword)
if err != nil {
return err
}
if validateBlind {
blind, err = c.validateOPRFBlindOption(blind)
if err != nil {
return err
}
}
inputs.OPRFBlind = blind
inputs.Password = password
return nil
}
func (c *Client) resolveRegistrationFinalizeInputs(options []*ClientOptions) (*clientInputs, error) {
inputs := newClientInputs(c.conf)
if len(options) == 0 {
if err := c.resolveOPRFFinalizeInputs(inputs, nil, nil); err != nil {
return nil, err
}
inputs.EnvelopeNonce = internal.RandomBytes(internal.NonceLength)
return inputs, nil
}
// OPRF Blind.
if err := c.resolveOPRFFinalizeInputs(inputs, options[0].OPRFBlind, options[0].ResumePassword); err != nil {
return nil, err
}
// KDF salt.
if options[0].KDFSalt != nil {
inputs.KDFSalt = options[0].KDFSalt
}
// KSF options.
if err := c.resolveKSFInputs(inputs.KSF, options[0]); err != nil {
return nil, err
}
// Envelope nonce.
var err error
inputs.EnvelopeNonce, err = resolveEnvelopeNonce(options...)
if err != nil {
return nil, ErrClientOptions.Join(err)
}
return inputs, nil
}
func (c *Client) resolveKE1Inputs(options []*ClientOptions) (*clientInputs, error) {
inputs := newClientInputs(c.conf)
if len(options) == 0 || options[0] == nil {
c.ake.SecretKeyShare = c.conf.MakeSecretKeyShare(nil)
inputs.AKENonce = internal.RandomBytes(internal.NonceLength)
return inputs, nil
}
// OPRF Blind.
var err error
inputs.OPRFBlind, err = c.validateOPRFBlindOption(options[0].OPRFBlind)
if err != nil {
return nil, err
}
// AKE options.
if options[0].AKE == nil {
c.ake.SecretKeyShare = c.conf.MakeSecretKeyShare(nil)
inputs.AKENonce = internal.RandomBytes(internal.NonceLength)
return inputs, nil
}
// AKE nonce.
inputs.AKENonce = options[0].AKE.Nonce
if len(inputs.AKENonce) == 0 {
inputs.AKENonce = internal.RandomBytes(internal.NonceLength)
}
// Ephemeral secret key share.
c.ake.SecretKeyShare, err = options[0].AKE.getSecretKeyShare(c.conf)
if err != nil {
return nil, ErrClientOptions.Join(err)
}
return inputs, nil
}
func (c *Client) resolveKE3StateInputs(inputs *clientInputs) (*clientInputs, error) {
if err := c.resolveOPRFFinalizeInputs(inputs, nil, nil); err != nil {
return nil, err
}
if c.ake.SecretKeyShare == nil {
return nil, ErrClientOptions.Join(internal.ErrClientNoKeyShare)
}
if len(c.ake.ke1) == 0 {
return nil, ErrClientOptions.Join(internal.ErrKE1Missing)
}
inputs.SecretKeyShare = c.ake.SecretKeyShare
inputs.KE1 = c.ake.ke1
return inputs, nil
}
func (c *Client) resolveKE3Inputs(options []*ClientOptions) (*clientInputs, error) {
inputs := newClientInputs(c.conf)
if len(options) == 0 || options[0] == nil {
return c.resolveKE3StateInputs(inputs)
}
// OPRF Blind.
if err := c.resolveOPRFFinalizeInputs(inputs, options[0].OPRFBlind, options[0].ResumePassword); err != nil {
return nil, err
}
// KDF salt.
if options[0].KDFSalt != nil {
inputs.KDFSalt = options[0].KDFSalt
}
// KSF options.
if err := c.resolveKSFInputs(inputs.KSF, options[0]); err != nil {
return nil, err
}
// KE1.
var err error
inputs.KE1, err = c.resolveKE1(options[0])
if err != nil {
return nil, err
}
// Ephemeral secret key share.
inputs.SecretKeyShare, err = c.parseOptionsKE3ESK(options[0].AKE)
if err != nil {
return nil, ErrClientOptions.Join(err)
}
return inputs, nil
}
func (c *Client) parseOptionsKE3ESK(options *AKEOptions) (*ecc.Scalar, error) {
if c.ake.SecretKeyShare != nil {
// return an error if options are present
if options != nil && (options.SecretKeyShare != nil || len(options.SecretKeyShareSeed) != 0) {
return nil, ErrClientOptions.Join(internal.ErrClientExistingKeyShare)
}
return c.ake.SecretKeyShare, nil
}
if options == nil || (options.SecretKeyShare == nil && len(options.SecretKeyShareSeed) == 0) {
return nil, ErrClientOptions.Join(internal.ErrClientNoKeyShare)
}
return options.getSecretKeyShare(c.conf)
}
// validateOptionsLength returns an error if the input slice does not match the provided length (if != 0) or is shorter
// than the reference length.
func validateOptionsLength(input []byte, length int, referenceLength uint32) error {
if input == nil {
return nil
}
if length < 0 {
return internal.ErrProvidedLengthNegative
}
// If the length is 0, it means the required length is not overridden, and the input slice must be at least the
// reference length.
if length == 0 {
if len(input) < int(referenceLength) {
return fmt.Errorf("%w: want %d, got %d", internal.ErrSliceShorterLength, referenceLength, len(input))
}
return nil
}
// If a length is provided, the input slice must match it.
if length != len(input) {
return fmt.Errorf("%w: want %d, got %d", internal.ErrSliceDifferentLength, length, len(input))
}
return nil
}