-
Notifications
You must be signed in to change notification settings - Fork 2
/
hpke.go
570 lines (514 loc) · 16.5 KB
/
hpke.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package hpke
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"errors"
"io"
"math/big"
"github.com/aead/ecdh"
"github.com/danielhavir/xchacha20blake2b"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf"
)
// GenerateKeyPair generates a key pair for a given parameter set.
// Argument `random` is optional. If `nil` crypto/rand.Reader is used
func GenerateKeyPair(params *Params, random io.Reader) (private crypto.PrivateKey, public crypto.PublicKey, err error) {
private, public, err = params.curve.GenerateKey(random)
return
}
// Marshall produces a fixed-length octet string encoding the public key "pk" checks whether a generic
// interface matches the right type of a HPKE public key
// performs casting from crypto.PublicKey to ecdh.Point (when using public key of generic curves)
// or byte array
// Reference 1: https://github.com/aead/ecdh/blob/master/generic.go#L99-L121
// Reference 2: https://github.com/aead/ecdh/blob/master/curve25519.go#L88-L108
func Marshall(params *Params, key interface{}) (keyBytes []byte, err error) {
switch t := key.(type) {
case ecdh.Point:
keyBytes = marshallGeneric(t, (params.curve.Params().BitSize+7)>>3)
case *ecdh.Point:
keyBytes = marshallGeneric(*t, (params.curve.Params().BitSize+7)>>3)
case [32]byte:
keyBytes = make([]byte, 32)
copy(keyBytes[:], t[:])
case *[32]byte:
keyBytes = make([]byte, 32)
copy(keyBytes[:], t[:])
case []byte:
if len(t) == 32 {
keyBytes = make([]byte, 32)
copy(keyBytes[:], t)
} else {
err = errors.New("incorrect key")
}
case *[]byte:
if len(*t) == 32 {
keyBytes = make([]byte, 32)
copy(keyBytes[:], *t)
} else {
err = errors.New("incorrect key")
}
default:
err = errors.New("incorrect key")
}
return
}
// MarshallPrivate a fixed-length octet string encoding the private key
func MarshallPrivate(params *Params, key interface{}) (keyBytes []byte, err error) {
switch t := key.(type) {
case []byte:
if len(t) == 32 {
keyBytes = make([]byte, 32)
copy(keyBytes[:], t)
} else if len(t) == 66 {
keyBytes = make([]byte, 66)
copy(keyBytes[:], t)
} else {
err = errors.New("incorrect private key")
}
case *[]byte:
if len(*t) == 32 {
keyBytes = make([]byte, 32)
copy(keyBytes[:], *t)
} else if len(*t) == 66 {
keyBytes = make([]byte, 66)
copy(keyBytes[:], *t)
} else {
err = errors.New("incorrect private key")
}
case [32]byte:
if len(t) == 32 {
keyBytes = make([]byte, 32)
copy(keyBytes[:], t[:])
} else {
err = errors.New("incorrect private key")
}
case *[32]byte:
if len(*t) == 32 {
keyBytes = make([]byte, 32)
copy(keyBytes[:], (*t)[:])
} else {
err = errors.New("incorrect private key")
}
default:
err = errors.New("incorrect private key")
}
return
}
// helper function for marshalling a point on an elliptic curve into a byte array
func marshallGeneric(key ecdh.Point, byteSize int) (pubBytes []byte) {
pubBytes = make([]byte, 2*byteSize)
offset := byteSize - len(key.X.Bytes())
copy(pubBytes[offset:byteSize], key.X.Bytes())
offset = byteSize - len(key.Y.Bytes())
copy(pubBytes[byteSize+offset:], key.Y.Bytes())
return
}
// Unmarshall parses a fixed-length octet string to recover a public key
// restores the public key from byte array after marshall
func Unmarshall(params *Params, keyBytes []byte) (pub crypto.PublicKey, err error) {
switch params.ciphersuite {
case 1, 2, 5, 6, 7, 9:
pub = unmarshallGeneric(keyBytes, (params.curve.Params().BitSize+7)>>3)
case 3, 4, 8:
if len(keyBytes) == 32 {
pub = keyBytes
} else {
err = errors.New("unknown size")
}
default:
err = errors.New("unknown ciphersuite")
}
return
}
// UnmarshallPrivate parses a fixed-length octet string to recover a private key
// restores the private key from byte array after marshall
func UnmarshallPrivate(params *Params, keyBytes []byte) (prv crypto.PrivateKey, err error) {
switch params.ciphersuite {
case 5, 6, 9:
if len(keyBytes) == 66 {
prv = keyBytes
} else {
err = errors.New("unknown size")
}
case 1, 2, 3, 4, 7, 8:
if len(keyBytes) == 32 {
prv = keyBytes
} else {
err = errors.New("unknown size")
}
default:
err = errors.New("unknown ciphersuite")
}
return
}
// helper function for unmarshalling a point on an elliptic curve to a byte array
func unmarshallGeneric(pubBytes []byte, byteSize int) (key ecdh.Point) {
x := new(big.Int).SetBytes(pubBytes[:byteSize])
y := new(big.Int).SetBytes(pubBytes[byteSize:])
return ecdh.Point{X: x, Y: y}
}
// encap generates an ephemeral symmetric key and a fixed-length encapsulation of that key that can be decapsulated by
// the holder of the private key corresponding to pk
func encap(params *Params, pkR crypto.PublicKey, random io.Reader) (shared, enc []byte, err error) {
skE, pkE, err := GenerateKeyPair(params, random)
if err != nil {
return
}
shared = params.curve.ComputeSecret(skE, pkR)
enc, err = Marshall(params, pkE)
return
}
// decap uses the private key "sk" to recover the ephemeral symmetric key from its encapsulated representation "enc"
func decap(params *Params, skR crypto.PrivateKey, enc []byte) (shared []byte, err error) {
pkE, err := Unmarshall(params, enc)
if err != nil {
return
}
if err = params.curve.Check(pkE); err != nil {
return
}
shared = params.curve.ComputeSecret(skR, pkE)
return
}
// authEncap is same as Encap(), but the outputs encode an assurance that the ephemeral shared key is known
// only to the holder of the private key "skI"
func authEncap(params *Params, pkR crypto.PublicKey, skI crypto.PrivateKey, random io.Reader) (shared, enc []byte, err error) {
skE, pkE, err := GenerateKeyPair(params, random)
if err != nil {
return
}
shared = append(params.curve.ComputeSecret(skE, pkR), params.curve.ComputeSecret(skI, pkR)...)
enc, err = Marshall(params, pkE)
return
}
// authDecap is same as Decap(), but the holder of the private key "skI" is assured that the ephemeral shared
// key is known only to the holder of the private key corresponding to "pkI"
func authDecap(params *Params, skR crypto.PrivateKey, pkI crypto.PublicKey, enc []byte) (shared []byte, err error) {
pkE, err := Unmarshall(params, enc)
if err != nil {
return
}
if err = params.curve.Check(pkE); err != nil {
return
}
shared = append(params.curve.ComputeSecret(skR, pkE), params.curve.ComputeSecret(skR, pkI)...)
return
}
// setupCore is a function with the common part of the process in setting up the shared secret for different HPKE variants
// Section 6.1. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.1
func setupCore(params *Params, mode byte, secret, kemContext []byte) (key []byte, err error) {
context := make([]byte, 3+len(kemContext))
context[0] = params.ciphersuite
context[1] = mode
context[2] = uint8(len(kemContext))
copy(context[3:3+len(kemContext)], kemContext)
key = make([]byte, params.nk)
_, err = hkdf.Expand(params.hashFn, secret, append([]byte("hpke key"), context...)).Read(key)
return
}
// setupBase is the common setup in the base mode for the Initiator and the Receiver
// Section 6.1. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.1
func setupBase(params *Params, pkR crypto.PublicKey, shared, enc []byte) (key []byte, err error) {
pkRBytes, err := Marshall(params, pkR)
if err != nil {
err = errors.New("incorrect receiver's public key")
return
}
kemContext := append(enc, pkRBytes...)
secret := hkdf.Extract(params.hashFn, shared, make([]byte, params.nh))
return setupCore(params, mode_base, secret, kemContext)
}
// setupPSK is the common setup in the psk mode for the Initiator and the Receiver
// Section 6.2. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.2
func setupPsk(params *Params, pkR crypto.PublicKey, psk, pskID, shared, enc []byte) (key []byte, err error) {
pkRBytes, err := Marshall(params, pkR)
if err != nil {
err = errors.New("incorrect receiver's public key")
return
}
kemContext := append(enc, pkRBytes...)
kemContext = append(kemContext, pskID...)
secret := hkdf.Extract(params.hashFn, shared, psk)
return setupCore(params, mode_psk, secret, kemContext)
}
// setupAuth is the common setup in the auth mode for the Initiator and the Receiver
// Section 6.3. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.3
func setupAuth(params *Params, pkR, pkI crypto.PublicKey, shared, enc []byte) (key []byte, err error) {
// TODO: kemContext := append(pkE, pkR, pkI...)
pkRBytes, err := Marshall(params, pkR)
if err != nil {
err = errors.New("incorrect receiver's public key")
return
}
pkIBytes, err := Marshall(params, pkI)
if err != nil {
err = errors.New("incorrect initiator's public key")
return
}
kemContext := append(enc, pkRBytes...)
kemContext = append(kemContext, pkIBytes...)
secret := hkdf.Extract(params.hashFn, shared, make([]byte, params.nh))
return setupCore(params, mode_auth, secret, kemContext)
}
// setupBaseI is the setup for the Initiator in the base mode
// Section 6.1. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.1
func setupBaseI(params *Params, pkR crypto.PublicKey, random io.Reader) (key, enc []byte, err error) {
shared, enc, err := encap(params, pkR, random)
if err != nil {
return
}
key, err = setupBase(params, pkR, shared, enc)
return
}
// setupBaseR is the setup for the Receiver in the base mode
// Section 6.1. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.1
func setupBaseR(params *Params, skR crypto.PrivateKey, enc []byte) (key []byte, err error) {
shared, err := decap(params, skR, enc)
if err != nil {
return
}
return setupBase(params, params.curve.PublicKey(skR), shared, enc)
}
// setupPSKI is the setup for the Initiator in the psk mode
// Section 6.2. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.2
func setupPskI(params *Params, pkR crypto.PublicKey, random io.Reader, psk, pskID []byte) (key, enc []byte, err error) {
shared, enc, err := encap(params, pkR, random)
if err != nil {
return
}
key, err = setupPsk(params, pkR, psk, pskID, shared, enc)
return
}
// setupPskR is the setup for the Receiver in the psk mode
// Section 6.2. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.2
func setupPskR(params *Params, skR crypto.PrivateKey, enc, psk, pskID []byte) (key []byte, err error) {
shared, err := decap(params, skR, enc)
if err != nil {
return
}
return setupPsk(params, params.curve.PublicKey(skR), psk, pskID, shared, enc)
}
// setupAuthI is the setup for the Initiator in the auth mode
// Section 6.3. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.3
func setupAuthI(params *Params, pkR crypto.PublicKey, skI crypto.PrivateKey, random io.Reader) (key, enc []byte, err error) {
shared, enc, err := authEncap(params, pkR, skI, random)
if err != nil {
return
}
key, err = setupAuth(params, pkR, params.curve.PublicKey(skI), shared, enc)
return
}
// setupAuthR is the setup for the Receiver in the auth mode
// Section 6.3. https://tools.ietf.org/html/draft-barnes-cfrg-hpke-01#section-6.3
func setupAuthR(params *Params, skR crypto.PrivateKey, pkI crypto.PublicKey, enc []byte) (key []byte, err error) {
shared, err := authDecap(params, skR, pkI, enc)
if err != nil {
return
}
return setupAuth(params, params.curve.PublicKey(skR), pkI, shared, enc)
}
// return the appropriate aead ciphersuite based on params
func getAead(params *Params, key []byte) (cphr cipher.AEAD, err error) {
switch params.ciphersuite {
case 1, 3, 5:
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
cphr, err = cipher.NewGCM(block)
if err != nil {
return nil, err
}
case 2, 4, 6:
cphr, err = chacha20poly1305.New(key)
if err != nil {
return
}
case 7, 8, 9:
cphr, err = xchacha20blake2b.New(key)
if err != nil {
return
}
default:
err = errors.New("unknown cipher choice")
return
}
return
}
// xorNonce XORs the derived nonce with y-byte string containing the binary representation
// of state counter seq in big-endian byte order.
// this function is optimized so that the xor is performed simultaneously with the big endian encoding
func xorNonce(nonce []byte, seq, nonceSize int) []byte {
useq := uint64(seq)
var xByte byte
for i := nonceSize - 1; i >= 0; i-- {
xByte = byte(useq)
nonce[i] ^= (xByte & 0xff)
useq = useq >> 8
}
return nonce
}
func encryptSymmetric(params *Params, rand io.Reader, cphr cipher.AEAD, pt, aad []byte) (ct []byte, err error) {
var nonce []byte
switch params.ciphersuite {
case 1, 2, 3, 4, 5, 6:
nonce = make([]byte, cphr.NonceSize())
_, err = io.ReadFull(rand, nonce)
if err != nil {
return
}
case 7, 8, 9:
nonce = nil
default:
err = errors.New("unknown ciphersuite")
return
}
ct = cphr.Seal(nil, nonce, pt, aad)
ct = append(nonce, ct...)
return
}
func decryptSymmetric(params *Params, cphr cipher.AEAD, ct, aad []byte) (pt []byte, err error) {
switch params.ciphersuite {
case 1, 2, 3, 4, 5, 6:
nonce := ct[:cphr.NonceSize()]
pt, err = cphr.Open(nil, nonce, ct[cphr.NonceSize():], aad)
case 7, 8, 9:
pt, err = cphr.Open(nil, nil, ct, aad)
default:
err = errors.New("unknown ciphersuite")
return
}
return
}
// EncryptBase is a function for encryption in the base mode
// Optional arguments:
// `random` is optional. If `nil` crypto/rand.Reader is used
// `aad` and `info` are optional.
func EncryptBase(params *Params, random io.Reader, pkR crypto.PublicKey, pt, aad []byte) (ct, enc []byte, err error) {
if params.mode != mode_base {
err = errors.New("params specify different mode")
return
}
if random == nil {
random = rand.Reader
}
key, enc, err := setupBaseI(params, pkR, random)
if err != nil {
return
}
cphr, err := getAead(params, key)
if err != nil {
return
}
ct, err = encryptSymmetric(params, random, cphr, pt, aad)
if err != nil {
return
}
return
}
// DecryptBase is a function for decryption in the base mode
func DecryptBase(params *Params, skR crypto.PrivateKey, enc, ct, aad []byte) (pt []byte, err error) {
if params.mode != mode_base {
err = errors.New("params specify different mode")
return
}
key, err := setupBaseR(params, skR, enc)
cphr, err := getAead(params, key)
if err != nil {
return
}
pt, err = decryptSymmetric(params, cphr, ct, aad)
if err != nil {
return
}
return
}
// EncryptPSK is a function for encryption in the pre-shared key mode
// Optional arguments:
// `random` is optional. If `nil` crypto/rand.Reader is used
// `aad` and `info` are optional.
func EncryptPSK(params *Params, random io.Reader, pkR crypto.PublicKey, pt, aad, psk, pskID []byte) (ct, enc []byte, err error) {
if params.mode != mode_psk {
err = errors.New("params specify different mode")
return
}
if random == nil {
random = rand.Reader
}
key, enc, err := setupPskI(params, pkR, random, psk, pskID)
if err != nil {
return
}
cphr, err := getAead(params, key)
if err != nil {
return
}
ct, err = encryptSymmetric(params, random, cphr, pt, aad)
if err != nil {
return
}
return
}
// DecryptPSK is a function for decryption in the pre-shared key mode
func DecryptPSK(params *Params, skR crypto.PrivateKey, enc, ct, aad, psk, pskID []byte) (pt []byte, err error) {
if params.mode != mode_psk {
err = errors.New("params specify different mode")
return
}
key, err := setupPskR(params, skR, enc, psk, pskID)
cphr, err := getAead(params, key)
if err != nil {
return
}
pt, err = decryptSymmetric(params, cphr, ct, aad)
if err != nil {
return
}
return
}
// EncryptAuth is a function for encryption in the asymmetric key mode
// Optional arguments:
// `random` is optional. If `nil` crypto/rand.Reader is used
// `aad` and `info` are optional.
func EncryptAuth(params *Params, random io.Reader, pkR crypto.PublicKey, skI crypto.PrivateKey, pt, aad []byte) (ct, enc []byte, err error) {
if params.mode != mode_auth {
err = errors.New("params specify different mode")
return
}
if random == nil {
random = rand.Reader
}
key, enc, err := setupAuthI(params, pkR, skI, random)
if err != nil {
return
}
cphr, err := getAead(params, key)
if err != nil {
return
}
ct, err = encryptSymmetric(params, random, cphr, pt, aad)
if err != nil {
return
}
return
}
// DecryptAuth is a function for decryption in the asymmetric key mode
func DecryptAuth(params *Params, skR crypto.PrivateKey, pkI crypto.PublicKey, enc, ct, aad []byte) (pt []byte, err error) {
if params.mode != mode_auth {
err = errors.New("params specify different mode")
return
}
key, err := setupAuthR(params, skR, pkI, enc)
cphr, err := getAead(params, key)
if err != nil {
return
}
pt, err = decryptSymmetric(params, cphr, ct, aad)
if err != nil {
return
}
return
}