-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdeserializer.go
More file actions
301 lines (239 loc) · 9.63 KB
/
deserializer.go
File metadata and controls
301 lines (239 loc) · 9.63 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
// 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 (
"errors"
"github.com/bytemare/ecc"
"github.com/bytemare/opaque/internal"
"github.com/bytemare/opaque/message"
)
// Deserializer exposes the message deserialization methods.
type Deserializer struct {
conf *internal.Configuration
}
// RegistrationRequest takes a serialized RegistrationRequest message and returns a deserialized
// RegistrationRequest structure.
func (d *Deserializer) RegistrationRequest(registrationRequest []byte) (*message.RegistrationRequest, error) {
if len(registrationRequest) != d.conf.OPRF.Group().ElementLength() {
return nil, ErrRegistrationRequest.Join(internal.ErrInvalidMessageLength)
}
blindedMessage, err := DeserializeElement(d.conf.OPRF.Group(), registrationRequest)
if err != nil {
return nil, ErrRegistrationRequest.Join(internal.ErrInvalidBlindedMessage, err)
}
return &message.RegistrationRequest{BlindedMessage: blindedMessage}, nil
}
// RegistrationResponse takes a serialized RegistrationResponse message and returns a deserialized
// RegistrationResponse structure.
func (d *Deserializer) RegistrationResponse(registrationResponse []byte) (*message.RegistrationResponse, error) {
if len(registrationResponse) != (d.conf.OPRF.Group().ElementLength() + d.conf.Group.ElementLength()) {
return nil, ErrRegistrationResponse.Join(internal.ErrInvalidMessageLength)
}
evaluatedMessage, err := DeserializeElement(d.conf.OPRF.Group(),
registrationResponse[:d.conf.OPRF.Group().ElementLength()])
if err != nil {
return nil, ErrRegistrationResponse.Join(internal.ErrInvalidEvaluatedMessage, err)
}
pksBytes := registrationResponse[d.conf.OPRF.Group().ElementLength() : 2*d.conf.OPRF.Group().ElementLength()]
_, err = DeserializeElement(d.conf.Group, pksBytes)
if err != nil {
return nil, ErrRegistrationResponse.Join(
internal.ErrInvalidServerPublicKey,
internal.ErrInvalidPublicKeyBytes,
err,
)
}
return &message.RegistrationResponse{
EvaluatedMessage: evaluatedMessage,
ServerPublicKey: pksBytes,
}, nil
}
// RegistrationRecord takes a serialized RegistrationRecord message and returns a deserialized
// RegistrationRecord structure.
func (d *Deserializer) RegistrationRecord(record []byte) (*message.RegistrationRecord, error) {
if len(record) != d.recordLength() {
return nil, ErrRegistrationRecord.Join(internal.ErrInvalidMessageLength)
}
pk := record[:d.conf.Group.ElementLength()]
maskingKey := record[d.conf.Group.ElementLength() : d.conf.Group.ElementLength()+d.conf.Hash.Size()]
env := record[d.conf.Group.ElementLength()+d.conf.Hash.Size():]
pku, err := DeserializeElement(d.conf.Group, pk)
if err != nil {
return nil, ErrRegistrationRecord.Join(internal.ErrInvalidClientPublicKey, err)
}
if isAllZeros(maskingKey) {
return nil, ErrRegistrationRecord.Join(internal.ErrInvalidMaskingKey, internal.ErrSliceIsAllZeros)
}
// Note: we do not check whether the envelope is all zeros, as it must be supported for the
// anti-enumeration "fake envelope" feature.
return &message.RegistrationRecord{
ClientPublicKey: pku,
MaskingKey: maskingKey,
Envelope: env,
}, nil
}
// KE1 takes a serialized KE1 message and returns a deserialized KE1 structure.
func (d *Deserializer) KE1(ke1 []byte) (*message.KE1, error) {
if len(ke1) != d.ke1Length() {
return nil, ErrKE1.Join(internal.ErrInvalidMessageLength)
}
request, err := d.deserializeCredentialRequest(ke1[:d.conf.OPRF.Group().ElementLength()])
if err != nil {
return nil, ErrKE1.Join(err)
}
nonceU := ke1[d.conf.OPRF.Group().ElementLength() : d.conf.OPRF.Group().ElementLength()+d.conf.NonceLen]
if isAllZeros(nonceU) {
return nil, ErrKE1.Join(internal.ErrMissingNonce, internal.ErrSliceIsAllZeros)
}
offset := d.conf.OPRF.Group().ElementLength() + d.conf.NonceLen
epku, err := DeserializeElement(d.conf.Group, ke1[offset:offset+d.conf.Group.ElementLength()])
if err != nil {
return nil, ErrKE1.Join(internal.ErrInvalidClientKeyShare, err)
}
return &message.KE1{
CredentialRequest: *request,
ClientNonce: nonceU,
ClientKeyShare: epku,
}, nil
}
// KE2 takes a serialized KE2 message and returns a deserialized KE2 structure.
func (d *Deserializer) KE2(ke2 []byte) (*message.KE2, error) {
// size of credential response
maxResponseLength := d.credentialResponseLength()
// Verify it matches the size of a legal KE2
if len(ke2) != maxResponseLength+d.ke2LengthWithoutCreds() {
return nil, ErrKE2.Join(internal.ErrInvalidMessageLength)
}
cresp, err := d.deserializeCredentialResponse(ke2, maxResponseLength)
if err != nil {
return nil, ErrKE2.Join(err)
}
if isAllZeros(cresp.MaskingNonce) {
return nil, ErrKE2.Join(internal.ErrCredentialResponseInvalid,
internal.ErrCredentialResponseNoMaskingNonce, internal.ErrSliceIsAllZeros)
}
if isAllZeros(cresp.MaskedResponse) {
return nil, ErrKE2.Join(internal.ErrCredentialResponseInvalid,
internal.ErrCredentialResponseInvalidMaskedResponse, internal.ErrSliceIsAllZeros)
}
nonceS := ke2[maxResponseLength : maxResponseLength+d.conf.NonceLen]
offset := maxResponseLength + d.conf.NonceLen
epk := ke2[offset : offset+d.conf.Group.ElementLength()]
offset += d.conf.Group.ElementLength()
mac := ke2[offset:]
epks, err := DeserializeElement(d.conf.Group, epk)
if err != nil {
return nil, ErrKE2.Join(internal.ErrInvalidServerKeyShare, err)
}
if isAllZeros(nonceS) {
return nil, ErrKE2.Join(internal.ErrMissingNonce, internal.ErrSliceIsAllZeros)
}
if isAllZeros(mac) {
return nil, ErrKE2.Join(internal.ErrMissingMAC, internal.ErrSliceIsAllZeros)
}
return &message.KE2{
CredentialResponse: cresp,
ServerNonce: nonceS,
ServerKeyShare: epks,
ServerMac: mac,
}, nil
}
// KE3 takes a serialized KE3 message and returns a deserialized KE3 structure.
func (d *Deserializer) KE3(ke3 []byte) (*message.KE3, error) {
if len(ke3) != d.conf.MAC.Size() {
return nil, ErrKE3.Join(internal.ErrInvalidMessageLength)
}
if isAllZeros(ke3) {
return nil, ErrKE3.Join(internal.ErrInvalidClientMac, internal.ErrSliceIsAllZeros)
}
return &message.KE3{ClientMac: ke3}, nil
}
// DecodePrivateKey takes a serialized private key (a scalar) and attempts to return it's decoded form.
func (d *Deserializer) DecodePrivateKey(encoded []byte) (*ecc.Scalar, error) {
sk, err := DeserializeScalar(d.conf.Group, encoded)
if err != nil {
return nil, errors.Join(internal.ErrInvalidPrivateKey, err)
}
return sk, nil
}
// DecodePublicKey takes a serialized public key (a point) and attempts to return it's decoded form.
func (d *Deserializer) DecodePublicKey(encoded []byte) (*ecc.Element, error) {
pk, err := DeserializeElement(d.conf.Group, encoded)
if err != nil {
return nil, errors.Join(internal.ErrInvalidPublicKey, err)
}
return pk, nil
}
func (d *Deserializer) recordLength() int {
return d.conf.Group.ElementLength() + d.conf.Hash.Size() + d.conf.EnvelopeSize
}
func (d *Deserializer) deserializeCredentialRequest(input []byte) (*message.CredentialRequest, error) {
blindedMessage, err := DeserializeElement(d.conf.OPRF.Group(), input)
if err != nil {
return nil, errors.Join(internal.ErrInvalidBlindedMessage, err)
}
return message.NewCredentialRequest(blindedMessage), nil
}
func (d *Deserializer) deserializeCredentialResponse(
input []byte,
maxResponseLength int,
) (*message.CredentialResponse, error) {
evaluatedMessage, err := DeserializeElement(d.conf.OPRF.Group(), input[:d.conf.OPRF.Group().ElementLength()])
if err != nil {
return nil, errors.Join(internal.ErrInvalidEvaluatedMessage, err)
}
return message.NewCredentialResponse(evaluatedMessage,
input[d.conf.OPRF.Group().ElementLength():d.conf.OPRF.Group().ElementLength()+d.conf.NonceLen],
input[d.conf.OPRF.Group().ElementLength()+d.conf.NonceLen:maxResponseLength]), nil
}
func (d *Deserializer) ke1Length() int {
return d.conf.OPRF.Group().ElementLength() + d.conf.NonceLen + d.conf.Group.ElementLength()
}
func (d *Deserializer) ke2LengthWithoutCreds() int {
return d.conf.NonceLen + d.conf.Group.ElementLength() + d.conf.MAC.Size()
}
func (d *Deserializer) credentialResponseLength() int {
return d.conf.OPRF.Group().ElementLength() + d.conf.NonceLen + d.conf.Group.ElementLength() + d.conf.EnvelopeSize
}
func isAllZeros(input []byte) bool {
for _, b := range input {
if b != 0 {
return false
}
}
return true
}
// DeserializeElement takes a byte slice and attempts to decode it into an Element of the given group, and returns an
// error if the decoding fails or if the element is the identity element (point at infinity).
func DeserializeElement(g ecc.Group, input []byte) (*ecc.Element, error) {
e := g.NewElement()
if len(input) != g.ElementLength() {
return nil, errors.Join(internal.ErrInvalidElement, internal.ErrInvalidEncodingLength)
}
err := e.Decode(input[:g.ElementLength()])
if err != nil {
return nil, errors.Join(internal.ErrInvalidElement, err)
}
return e, nil
}
// DeserializeScalar takes a byte slice and attempts to decode it into a Scalar of the given group, and returns an
// error if the decoding fails or if the scalar is zero.
func DeserializeScalar(g ecc.Group, input []byte) (*ecc.Scalar, error) {
s := g.NewScalar()
if len(input) != g.ScalarLength() {
return nil, errors.Join(internal.ErrInvalidScalar, internal.ErrInvalidEncodingLength)
}
err := s.Decode(input)
if err != nil {
return nil, errors.Join(internal.ErrInvalidScalar, err)
}
if s.IsZero() {
return nil, errors.Join(internal.ErrInvalidScalar, internal.ErrScalarZero)
}
return s, nil
}