-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
263 lines (221 loc) · 7.61 KB
/
client.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
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
// SPDX-License-Identifier: MIT
//
// Copyright (C) 2020-2022 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 (
"errors"
"fmt"
group "github.com/bytemare/crypto"
"github.com/bytemare/opaque/internal"
"github.com/bytemare/opaque/internal/ake"
"github.com/bytemare/opaque/internal/encoding"
"github.com/bytemare/opaque/internal/keyrecovery"
"github.com/bytemare/opaque/internal/masking"
"github.com/bytemare/opaque/internal/oprf"
"github.com/bytemare/opaque/internal/tag"
"github.com/bytemare/opaque/message"
)
var (
// errInvalidMaskedLength happens when unmasking a masked response.
errInvalidMaskedLength = errors.New("invalid masked response length")
// errKe1Missing happens when GenerateKE3 is called and the client has no Ke1 in state.
errKe1Missing = errors.New("missing KE1 in client state")
)
// Client represents an OPAQUE Client, exposing its functions and holding its state.
type Client struct {
Deserialize *Deserializer
OPRF *oprf.Client
Ake *ake.Client
conf *internal.Configuration
}
// NewClient returns a new Client instantiation given the application Configuration.
func NewClient(c *Configuration) (*Client, error) {
if c == nil {
c = DefaultConfiguration()
}
conf, err := c.toInternal()
if err != nil {
return nil, err
}
return &Client{
OPRF: conf.OPRF.Client(),
Ake: ake.NewClient(),
Deserialize: &Deserializer{conf: conf},
conf: conf,
}, nil
}
// GetConf returns the internal configuration.
func (c *Client) GetConf() *internal.Configuration {
return c.conf
}
// buildPRK derives the randomized password from the OPRF output.
func (c *Client) buildPRK(evaluation *group.Element) []byte {
output := c.OPRF.Finalize(evaluation)
stretched := c.conf.KSF.Harden(output, nil, c.conf.OPRF.Group().ElementLength())
return c.conf.KDF.Extract(nil, encoding.Concat(output, stretched))
}
// ClientRegistrationInitOptions enables setting internal client values for the client registration.
type ClientRegistrationInitOptions struct {
// OPRFBlind: optional
OPRFBlind *group.Scalar
}
func getClientRegistrationInitBlind(options []ClientRegistrationInitOptions) *group.Scalar {
if len(options) == 0 {
return nil
}
return options[0].OPRFBlind
}
// RegistrationInit returns a RegistrationRequest message blinding the given password.
func (c *Client) RegistrationInit(
password []byte,
options ...ClientRegistrationInitOptions,
) *message.RegistrationRequest {
m := c.OPRF.Blind(password, getClientRegistrationInitBlind(options))
return &message.RegistrationRequest{
BlindedMessage: m,
}
}
// ClientRegistrationFinalizeOptions enables setting optional client values for the client registration.
type ClientRegistrationFinalizeOptions struct {
// ClientIdentity: optional
ClientIdentity []byte
// ServerIdentity: optional
ServerIdentity []byte
// EnvelopeNonce : optional
EnvelopeNonce []byte
}
func initClientRegistrationFinalizeOptions(options []ClientRegistrationFinalizeOptions) *keyrecovery.Credentials {
if len(options) == 0 {
return &keyrecovery.Credentials{
ClientIdentity: nil,
ServerIdentity: nil,
EnvelopeNonce: nil,
}
}
return &keyrecovery.Credentials{
ClientIdentity: options[0].ClientIdentity,
ServerIdentity: options[0].ServerIdentity,
EnvelopeNonce: options[0].EnvelopeNonce,
}
}
// RegistrationFinalize returns a RegistrationRecord message given the identities and the server's RegistrationResponse.
func (c *Client) RegistrationFinalize(
resp *message.RegistrationResponse,
options ...ClientRegistrationFinalizeOptions,
) (record *message.RegistrationRecord, exportKey []byte) {
credentials := initClientRegistrationFinalizeOptions(options)
randomizedPassword := c.buildPRK(resp.EvaluatedMessage)
maskingKey := c.conf.KDF.Expand(randomizedPassword, []byte(tag.MaskingKey), c.conf.KDF.Size())
envelope, clientPublicKey, exportKey := keyrecovery.Store(c.conf, randomizedPassword, resp.Pks, credentials)
return &message.RegistrationRecord{
PublicKey: clientPublicKey,
MaskingKey: maskingKey,
Envelope: envelope.Serialize(),
}, exportKey
}
// GenerateKE1Options enables setting optional values for the session, which default to secure random values if not
// set.
type GenerateKE1Options struct {
// Blind: optional
Blind *group.Scalar
// KeyShareSeed: optional
KeyShareSeed []byte
// Nonce: optional
Nonce []byte
// NonceLength: optional
NonceLength uint
}
func (c GenerateKE1Options) get() (*group.Scalar, ake.Options) {
return c.Blind, ake.Options{
KeyShareSeed: c.KeyShareSeed,
Nonce: c.Nonce,
NonceLength: c.NonceLength,
}
}
func getGenerateKE1Options(options []GenerateKE1Options) (*group.Scalar, ake.Options) {
if len(options) != 0 {
return options[0].get()
}
return nil, ake.Options{
KeyShareSeed: nil,
Nonce: nil,
NonceLength: internal.NonceLength,
}
}
// GenerateKE1 initiates the authentication process, returning a KE1 message blinding the given password.
func (c *Client) GenerateKE1(password []byte, options ...GenerateKE1Options) *message.KE1 {
blind, akeOptions := getGenerateKE1Options(options)
m := c.OPRF.Blind(password, blind)
ke1 := c.Ake.Start(c.conf.Group, akeOptions)
ke1.CredentialRequest = message.NewCredentialRequest(m)
c.Ake.Ke1 = ke1.Serialize()
return ke1
}
// GenerateKE3Options enables setting optional client values for the client registration.
type GenerateKE3Options struct {
// ClientIdentity: optional
ClientIdentity []byte
// ServerIdentity: optional
ServerIdentity []byte
}
func initGenerateKE3Options(options []GenerateKE3Options) *ake.Identities {
if len(options) == 0 {
return &ake.Identities{
ClientIdentity: nil,
ServerIdentity: nil,
}
}
return &ake.Identities{
ClientIdentity: options[0].ClientIdentity,
ServerIdentity: options[0].ServerIdentity,
}
}
// GenerateKE3 returns a KE3 message given the server's KE2 response message and the identities. If the idc
// or ids parameters are nil, the client and server's public keys are taken as identities for both.
func (c *Client) GenerateKE3(
ke2 *message.KE2, options ...GenerateKE3Options,
) (ke3 *message.KE3, exportKey []byte, err error) {
if len(c.Ake.Ke1) == 0 {
return nil, nil, errKe1Missing
}
// This test is very important as it avoids buffer overflows in subsequent parsing.
if len(ke2.MaskedResponse) != c.conf.Group.ElementLength()+c.conf.EnvelopeSize {
return nil, nil, errInvalidMaskedLength
}
identities := initGenerateKE3Options(options)
// Finalize the OPRF.
randomizedPassword := c.buildPRK(ke2.EvaluatedMessage)
// Decrypt the masked response.
serverPublicKey, serverPublicKeyBytes,
envelope, err := masking.Unmask(c.conf, randomizedPassword, ke2.MaskingNonce, ke2.MaskedResponse)
if err != nil {
return nil, nil, fmt.Errorf("unmasking: %w", err)
}
// Recover the client keys.
clientSecretKey, clientPublicKey,
exportKey, err := keyrecovery.Recover(
c.conf,
randomizedPassword,
serverPublicKeyBytes,
identities.ClientIdentity,
identities.ServerIdentity,
envelope)
if err != nil {
return nil, nil, fmt.Errorf("key recovery: %w", err)
}
// Finalize the AKE.
identities.SetIdentities(clientPublicKey, serverPublicKeyBytes)
ke3, err = c.Ake.Finalize(c.conf, identities, clientSecretKey, serverPublicKey, ke2)
if err != nil {
return nil, nil, fmt.Errorf("finalizing AKE: %w", err)
}
return ke3, exportKey, nil
}
// SessionKey returns the session key if the previous call to GenerateKE3() was successful.
func (c *Client) SessionKey() []byte {
return c.Ake.SessionKey()
}