-
Notifications
You must be signed in to change notification settings - Fork 15
/
crypto.go
839 lines (666 loc) · 18.6 KB
/
crypto.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
package hpke
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/hmac"
"crypto/subtle"
"encoding/binary"
"fmt"
"io"
"math/big"
_ "crypto/sha256"
_ "crypto/sha512"
"github.com/cloudflare/circl/dh/x448"
"golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/curve25519"
)
////////
// DHKEM
type dhScheme interface {
ID() KEMID
DeriveKeyPair(ikm []byte) (KEMPrivateKey, KEMPublicKey, error)
SerializePublicKey(pk KEMPublicKey) []byte
DeserializePublicKey(enc []byte) (KEMPublicKey, error)
DH(priv KEMPrivateKey, pub KEMPublicKey) ([]byte, error)
PublicKeySize() int
PrivateKeySize() int
SerializePrivateKey(sk KEMPrivateKey) []byte
DeserializePrivateKey(enc []byte) (KEMPrivateKey, error)
internalKDF() KDFScheme
}
type dhkemScheme struct {
group dhScheme
skE KEMPrivateKey
}
func (s dhkemScheme) ID() KEMID {
return s.group.ID()
}
func (s dhkemScheme) DeriveKeyPair(ikm []byte) (KEMPrivateKey, KEMPublicKey, error) {
return s.group.DeriveKeyPair(ikm)
}
func (s dhkemScheme) SerializePublicKey(pk KEMPublicKey) []byte {
return s.group.SerializePublicKey(pk)
}
func (s dhkemScheme) SerializePrivateKey(sk KEMPrivateKey) []byte {
return s.group.SerializePrivateKey(sk)
}
func (s dhkemScheme) DeserializePublicKey(enc []byte) (KEMPublicKey, error) {
return s.group.DeserializePublicKey(enc)
}
func (s dhkemScheme) DeserializePrivateKey(enc []byte) (KEMPrivateKey, error) {
return s.group.DeserializePrivateKey(enc)
}
func (s *dhkemScheme) setEphemeralKeyPair(skE KEMPrivateKey) {
s.skE = skE
}
func (s dhkemScheme) getEphemeralKeyPair(rand io.Reader) (KEMPrivateKey, KEMPublicKey, error) {
if s.skE != nil {
return s.skE, s.skE.PublicKey(), nil
}
ikm := make([]byte, s.PrivateKeySize())
rand.Read(ikm)
return s.group.DeriveKeyPair(ikm)
}
func (s dhkemScheme) extractAndExpand(dh []byte, kemContext []byte, Nsecret int) []byte {
suiteID := kemSuiteFromID(s.ID())
eae_prk := s.group.internalKDF().LabeledExtract(nil, suiteID, "eae_prk", dh)
return s.group.internalKDF().LabeledExpand(eae_prk, suiteID, "shared_secret", kemContext, Nsecret)
}
func (s dhkemScheme) Encap(rand io.Reader, pkR KEMPublicKey) ([]byte, []byte, error) {
skE, pkE, err := s.getEphemeralKeyPair(rand)
if err != nil {
return nil, nil, err
}
dh, err := s.group.DH(skE, pkR)
if err != nil {
return nil, nil, err
}
enc := s.group.SerializePublicKey(pkE)
pkRm := s.group.SerializePublicKey(pkR)
kemContext := make([]byte, len(enc)+len(pkRm))
copy(kemContext, enc)
copy(kemContext[len(enc):], pkRm)
Nsecret := s.group.internalKDF().OutputSize()
sharedSecret := s.extractAndExpand(dh, kemContext, Nsecret)
return sharedSecret, enc, nil
}
func (s dhkemScheme) Decap(enc []byte, skR KEMPrivateKey) ([]byte, error) {
pkE, err := s.group.DeserializePublicKey(enc)
if err != nil {
return nil, err
}
dh, err := s.group.DH(skR, pkE)
if err != nil {
return nil, err
}
pkRm := s.group.SerializePublicKey(skR.PublicKey())
kemContext := make([]byte, len(enc)+len(pkRm))
copy(kemContext, enc)
copy(kemContext[len(enc):], pkRm)
Nsecret := s.group.internalKDF().OutputSize()
sharedSecret := s.extractAndExpand(dh, kemContext, Nsecret)
return sharedSecret, nil
}
func (s dhkemScheme) AuthEncap(rand io.Reader, pkR KEMPublicKey, skS KEMPrivateKey) ([]byte, []byte, error) {
skE, pkE, err := s.getEphemeralKeyPair(rand)
if err != nil {
return nil, nil, err
}
dhER, err := s.group.DH(skE, pkR)
if err != nil {
return nil, nil, err
}
dhIR, err := s.group.DH(skS, pkR)
if err != nil {
return nil, nil, err
}
dh := append(dhER, dhIR...)
enc := s.group.SerializePublicKey(pkE)
pkRm := s.group.SerializePublicKey(pkR)
pkSm := s.group.SerializePublicKey(skS.PublicKey())
Nenc := len(enc)
Npk := len(pkRm)
Nsk := len(pkSm)
kemContext := make([]byte, Nenc+Npk+Nsk)
copy(kemContext[:Nenc], enc)
copy(kemContext[Nenc:Nenc+Npk], pkRm)
copy(kemContext[Nenc+Npk:], pkSm)
Nsecret := s.group.internalKDF().OutputSize()
sharedSecret := s.extractAndExpand(dh, kemContext, Nsecret)
return sharedSecret, enc, nil
}
func (s dhkemScheme) AuthDecap(enc []byte, skR KEMPrivateKey, pkS KEMPublicKey) ([]byte, error) {
pkE, err := s.group.DeserializePublicKey(enc)
if err != nil {
return nil, err
}
dhER, err := s.group.DH(skR, pkE)
if err != nil {
return nil, err
}
dhIR, err := s.group.DH(skR, pkS)
if err != nil {
return nil, err
}
dh := append(dhER, dhIR...)
pkRm := s.group.SerializePublicKey(skR.PublicKey())
pkSm := s.group.SerializePublicKey(pkS)
Nenc := len(enc)
Npk := len(pkRm)
Nsk := len(pkSm)
kemContext := make([]byte, Nenc+Npk+Nsk)
copy(kemContext[:Nenc], enc)
copy(kemContext[Nenc:Nenc+Npk], pkRm)
copy(kemContext[Nenc+Npk:], pkSm)
Nsecret := s.group.internalKDF().OutputSize()
sharedSecret := s.extractAndExpand(dh, kemContext, Nsecret)
return sharedSecret, nil
}
func (s dhkemScheme) PublicKeySize() int {
return s.group.PublicKeySize()
}
func (s dhkemScheme) PrivateKeySize() int {
return s.group.PrivateKeySize()
}
////////////////////////
// ECDH with NIST curves
type ecdhPrivateKey struct {
curve elliptic.Curve
d []byte
x, y *big.Int
}
func (priv ecdhPrivateKey) PublicKey() KEMPublicKey {
return &ecdhPublicKey{priv.curve, priv.x, priv.y}
}
type ecdhPublicKey struct {
curve elliptic.Curve
x, y *big.Int
}
type ecdhScheme struct {
curve elliptic.Curve
KDF KDFScheme
skE KEMPrivateKey
}
func (s ecdhScheme) internalKDF() KDFScheme {
return s.KDF
}
func (s ecdhScheme) ID() KEMID {
switch s.curve.Params().Name {
case "P-256":
return DHKEM_P256
case "P-521":
return DHKEM_P521
}
panic(fmt.Sprintf("Unsupported curve: %s", s.curve.Params().Name))
}
func (s ecdhScheme) privateKeyBitmask() uint8 {
switch s.curve.Params().Name {
case "P-256":
return 0xFF
case "P-521":
return 0x01
}
panic(fmt.Sprintf("Unsupported curve: %s", s.curve.Params().Name))
}
func (s ecdhScheme) DeriveKeyPair(ikm []byte) (KEMPrivateKey, KEMPublicKey, error) {
suiteID := kemSuiteFromID(s.ID())
dkp_prk := s.KDF.LabeledExtract(nil, suiteID, "dkp_prk", ikm)
counter := 0
for {
if counter > 255 {
return nil, nil, fmt.Errorf("Error deriving key pair")
}
bytes := s.KDF.LabeledExpand(dkp_prk, suiteID, "candidate", []byte{uint8(counter)}, s.PrivateKeySize())
bytes[0] = bytes[0] & s.privateKeyBitmask()
sk, err := s.DeserializePrivateKey(bytes)
if err == nil {
return sk, sk.PublicKey(), nil
}
counter = counter + 1
}
return nil, nil, fmt.Errorf("Error deriving key pair")
}
func (s ecdhScheme) SerializePublicKey(pk KEMPublicKey) []byte {
if pk == nil {
return nil
}
raw := pk.(*ecdhPublicKey)
return elliptic.Marshal(raw.curve, raw.x, raw.y)
}
func (s ecdhScheme) SerializePrivateKey(sk KEMPrivateKey) []byte {
if sk == nil {
return nil
}
raw := sk.(*ecdhPrivateKey)
copied := make([]byte, len(raw.d))
copy(copied, raw.d)
return copied
}
func (s ecdhScheme) DeserializePublicKey(enc []byte) (KEMPublicKey, error) {
x, y := elliptic.Unmarshal(s.curve, enc)
if x == nil {
return nil, fmt.Errorf("Error deserializing public key")
}
return &ecdhPublicKey{s.curve, x, y}, nil
}
func (s ecdhScheme) DeserializePrivateKey(enc []byte) (KEMPrivateKey, error) {
if enc == nil {
return nil, fmt.Errorf("Invalid input")
}
x, y := s.curve.Params().ScalarBaseMult(enc)
return &ecdhPrivateKey{s.curve, enc, x, y}, nil
}
func (s ecdhScheme) DH(priv KEMPrivateKey, pub KEMPublicKey) ([]byte, error) {
ecdhPriv, ok := priv.(*ecdhPrivateKey)
if !ok {
return nil, fmt.Errorf("Private key not suitable for ECDH")
}
ecdhPub, ok := pub.(*ecdhPublicKey)
if !ok {
return nil, fmt.Errorf("Public key not suitable for ECDH")
}
x, _ := s.curve.Params().ScalarMult(ecdhPub.x, ecdhPub.y, ecdhPriv.d)
xx := x.Bytes()
size := (s.curve.Params().BitSize + 7) >> 3
pad := make([]byte, size-len(xx))
dh := append(pad, xx...)
return dh, nil
}
func (s ecdhScheme) PublicKeySize() int {
feSize := (s.curve.Params().BitSize + 7) >> 3
return 1 + 2*feSize
}
func (s ecdhScheme) PrivateKeySize() int {
return (s.curve.Params().BitSize + 7) >> 3
}
///////////////////
// ECDH with X25519
type x25519PrivateKey struct {
val [32]byte
}
func (priv x25519PrivateKey) PublicKey() KEMPublicKey {
pub := &x25519PublicKey{}
curve25519.ScalarBaseMult(&pub.val, &priv.val)
return pub
}
type x25519PublicKey struct {
val [32]byte
}
type x25519Scheme struct {
skE KEMPrivateKey
}
func (s x25519Scheme) internalKDF() KDFScheme {
return hkdfScheme{hash: crypto.SHA256}
}
func (s x25519Scheme) ID() KEMID {
return DHKEM_X25519
}
func (s x25519Scheme) DeriveKeyPair(ikm []byte) (KEMPrivateKey, KEMPublicKey, error) {
suiteID := kemSuiteFromID(s.ID())
dkp_prk := s.internalKDF().LabeledExtract(nil, suiteID, "dkp_prk", ikm)
sk_bytes := s.internalKDF().LabeledExpand(dkp_prk, suiteID, "sk", nil, s.PrivateKeySize())
sk, err := s.DeserializePrivateKey(sk_bytes)
if err != nil {
return nil, nil, err
} else {
return sk, sk.PublicKey(), nil
}
}
func (s x25519Scheme) SerializePublicKey(pk KEMPublicKey) []byte {
if pk == nil {
return nil
}
raw := pk.(*x25519PublicKey)
return raw.val[:]
}
func (s x25519Scheme) SerializePrivateKey(sk KEMPrivateKey) []byte {
if sk == nil {
return nil
}
raw := sk.(*x25519PrivateKey)
return raw.val[:]
}
func (s x25519Scheme) DeserializePublicKey(enc []byte) (KEMPublicKey, error) {
if len(enc) != 32 {
return nil, fmt.Errorf("Error deserializing X25519 public key")
}
pub := &x25519PublicKey{}
copy(pub.val[:], enc)
return pub, nil
}
func (s x25519Scheme) DeserializePrivateKey(enc []byte) (KEMPrivateKey, error) {
if enc == nil {
return nil, fmt.Errorf("Invalid input")
}
if len(enc) != 32 {
return nil, fmt.Errorf("Error deserializing X25519 private key")
}
key := &x25519PrivateKey{}
copy(key.val[:], enc[0:32])
return key, nil
}
func (s x25519Scheme) DH(priv KEMPrivateKey, pub KEMPublicKey) ([]byte, error) {
xPriv, ok := priv.(*x25519PrivateKey)
if !ok {
return nil, fmt.Errorf("Private key not suitable for X25519: %+v", priv)
}
xPub, ok := pub.(*x25519PublicKey)
if !ok {
return nil, fmt.Errorf("Private key not suitable for X25519")
}
sharedSecret, err := curve25519.X25519(xPriv.val[:], xPub.val[:])
return sharedSecret, err
}
func (s x25519Scheme) PublicKeySize() int {
return 32
}
func (s x25519Scheme) PrivateKeySize() int {
return 32
}
///////////////////
// ECDH with X448
type x448PrivateKey struct {
val x448.Key
}
func (priv x448PrivateKey) PublicKey() KEMPublicKey {
pub := &x448PublicKey{}
x448.KeyGen(&pub.val, &priv.val)
return pub
}
type x448PublicKey struct {
val x448.Key
}
type x448Scheme struct {
skE KEMPrivateKey
}
func (s x448Scheme) internalKDF() KDFScheme {
return hkdfScheme{hash: crypto.SHA512}
}
func (s x448Scheme) ID() KEMID {
return DHKEM_X448
}
func (s x448Scheme) DeriveKeyPair(ikm []byte) (KEMPrivateKey, KEMPublicKey, error) {
suiteID := kemSuiteFromID(s.ID())
dkp_prk := s.internalKDF().LabeledExtract(nil, suiteID, "dkp_prk", ikm)
sk_bytes := s.internalKDF().LabeledExpand(dkp_prk, suiteID, "sk", nil, s.PrivateKeySize())
sk, err := s.DeserializePrivateKey(sk_bytes)
if err != nil {
return nil, nil, err
} else {
return sk, sk.PublicKey(), nil
}
}
func (s x448Scheme) SerializePublicKey(pk KEMPublicKey) []byte {
if pk == nil {
return nil
}
raw := pk.(*x448PublicKey)
return raw.val[:]
}
func (s x448Scheme) SerializePrivateKey(sk KEMPrivateKey) []byte {
if sk == nil {
return nil
}
raw := sk.(*x448PrivateKey)
return raw.val[:]
}
func (s x448Scheme) DeserializePublicKey(enc []byte) (KEMPublicKey, error) {
if len(enc) != 56 {
return nil, fmt.Errorf("Error deserializing X448 public key")
}
pub := &x448PublicKey{}
copy(pub.val[:], enc)
return pub, nil
}
func (s x448Scheme) DeserializePrivateKey(enc []byte) (KEMPrivateKey, error) {
if enc == nil {
return nil, fmt.Errorf("Invalid input")
}
if len(enc) != 56 {
return nil, fmt.Errorf("Error deserializing X448 private key")
}
key := &x448PrivateKey{}
copy(key.val[:], enc[0:56])
return key, nil
}
func (s x448Scheme) DH(priv KEMPrivateKey, pub KEMPublicKey) ([]byte, error) {
xPriv, ok := priv.(*x448PrivateKey)
if !ok {
return nil, fmt.Errorf("Private key not suitable for X448: %+v", priv)
}
xPub, ok := pub.(*x448PublicKey)
if !ok {
return nil, fmt.Errorf("Public key not suitable for X448: %+v", pub)
}
var sharedSecret, zero x448.Key
x448.Shared(&sharedSecret, &xPriv.val, &xPub.val)
if subtle.ConstantTimeCompare(sharedSecret[:], zero[:]) == 1 {
return nil, fmt.Errorf("bad input point: low order point")
}
return sharedSecret[:], nil
}
func (s x448Scheme) PublicKeySize() int {
return 56
}
func (s x448Scheme) PrivateKeySize() int {
return 56
}
//////////
// AES-GCM
type aesgcmScheme struct {
keySize int
}
func (s aesgcmScheme) ID() AEADID {
switch s.keySize {
case 16:
return AEAD_AESGCM128
case 32:
return AEAD_AESGCM256
}
panic(fmt.Sprintf("Unsupported key size: %d", s.keySize))
}
func (s aesgcmScheme) New(key []byte) (cipher.AEAD, error) {
if len(key) != s.keySize {
return nil, fmt.Errorf("Incorrect key size %d != %d", len(key), s.keySize)
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewGCM(block)
}
func (s aesgcmScheme) KeySize() int {
return s.keySize
}
func (s aesgcmScheme) NonceSize() int {
return 12
}
//////////
// ChaCha20-Poly1305
type chachaPolyScheme struct {
}
func (s chachaPolyScheme) ID() AEADID {
return AEAD_CHACHA20POLY1305
}
func (s chachaPolyScheme) New(key []byte) (cipher.AEAD, error) {
return chacha20poly1305.New(key)
}
func (s chachaPolyScheme) KeySize() int {
return chacha20poly1305.KeySize
}
func (s chachaPolyScheme) NonceSize() int {
return chacha20poly1305.NonceSize
}
//////////
// Export-only AEAD scheme
type exportOnlyScheme struct {
}
func (s exportOnlyScheme) ID() AEADID {
return AEAD_EXPORT_ONLY
}
func (s exportOnlyScheme) New(key []byte) (cipher.AEAD, error) {
panic("Not supported")
}
func (s exportOnlyScheme) KeySize() int {
panic("Not supported")
}
func (s exportOnlyScheme) NonceSize() int {
panic("Not supported")
}
///////
// HKDF
type hkdfScheme struct {
hash crypto.Hash
}
func (s hkdfScheme) ID() KDFID {
switch s.hash {
case crypto.SHA256:
return KDF_HKDF_SHA256
case crypto.SHA384:
return KDF_HKDF_SHA384
case crypto.SHA512:
return KDF_HKDF_SHA512
}
panic(fmt.Sprintf("Unsupported hash: %d", s.hash))
}
func (s hkdfScheme) Hash(message []byte) []byte {
h := s.hash.New()
h.Write(message)
return h.Sum(nil)
}
func (s hkdfScheme) Extract(salt, ikm []byte) []byte {
saltOrZero := salt
// if [salt is] not provided, it is set to a string of HashLen zeros
if salt == nil {
saltOrZero = make([]byte, s.hash.Size())
}
h := hmac.New(s.hash.New, saltOrZero)
h.Write(ikm)
return h.Sum(nil)
}
func (s hkdfScheme) Expand(prk, info []byte, outLen int) []byte {
out := []byte{}
T := []byte{}
i := byte(1)
for len(out) < outLen {
block := append(T, info...)
block = append(block, i)
h := hmac.New(s.hash.New, prk)
h.Write(block)
T = h.Sum(nil)
out = append(out, T...)
i++
}
return out[:outLen]
}
func (s hkdfScheme) LabeledExtract(salt []byte, suiteID []byte, label string, ikm []byte) []byte {
labeledIKM := append([]byte(versionLabel), suiteID...)
labeledIKM = append(labeledIKM, []byte(label)...)
labeledIKM = append(labeledIKM, ikm...)
return s.Extract(salt, labeledIKM)
}
func (s hkdfScheme) LabeledExpand(prk []byte, suiteID []byte, label string, info []byte, L int) []byte {
if L > (1 << 16) {
panic("Expand length cannot be larger than 2^16")
}
lengthBuffer := make([]byte, 2)
binary.BigEndian.PutUint16(lengthBuffer, uint16(L))
labeledLength := append(lengthBuffer, []byte(versionLabel)...)
labeledInfo := append(labeledLength, suiteID...)
labeledInfo = append(labeledInfo, []byte(label)...)
labeledInfo = append(labeledInfo, info...)
return s.Expand(prk, labeledInfo, L)
}
func (s hkdfScheme) OutputSize() int {
return s.hash.Size()
}
///////////////////////////
// Pre-defined KEM identifiers
type KEMID uint16
const (
DHKEM_P256 KEMID = 0x0010
DHKEM_P521 KEMID = 0x0012
DHKEM_X25519 KEMID = 0x0020
DHKEM_X448 KEMID = 0x0021
// The following identifiers were used for prototype SIKE integration. Since
// SIKE has been broken, we have removed this prototype, but in case anyone is
// using these identifiers, we list them here as reserved.
reserved1 KEMID = 0xFFFE // Was KEM_SIKE503
reserved2 KEMID = 0xFFFF // Was KEM_SIKE751
)
var kems = map[KEMID]KEMScheme{
DHKEM_X25519: &dhkemScheme{group: x25519Scheme{}},
DHKEM_X448: &dhkemScheme{group: x448Scheme{}},
DHKEM_P256: &dhkemScheme{group: ecdhScheme{curve: elliptic.P256(), KDF: hkdfScheme{hash: crypto.SHA256}}},
DHKEM_P521: &dhkemScheme{group: ecdhScheme{curve: elliptic.P521(), KDF: hkdfScheme{hash: crypto.SHA512}}},
}
func newKEMScheme(kemID KEMID) (KEMScheme, bool) {
switch kemID {
case DHKEM_X25519:
return &dhkemScheme{group: x25519Scheme{}}, true
case DHKEM_X448:
return &dhkemScheme{group: x448Scheme{}}, true
case DHKEM_P256:
return &dhkemScheme{group: ecdhScheme{curve: elliptic.P256(), KDF: hkdfScheme{hash: crypto.SHA256}}}, true
case DHKEM_P521:
return &dhkemScheme{group: ecdhScheme{curve: elliptic.P521(), KDF: hkdfScheme{hash: crypto.SHA512}}}, true
default:
return nil, false
}
}
///////////////////////////
// Pre-defined KDF identifiers
type KDFID uint16
const (
KDF_HKDF_SHA256 KDFID = 0x0001
KDF_HKDF_SHA384 KDFID = 0x0002
KDF_HKDF_SHA512 KDFID = 0x0003
)
var kdfs = map[KDFID]KDFScheme{
KDF_HKDF_SHA256: hkdfScheme{hash: crypto.SHA256},
KDF_HKDF_SHA384: hkdfScheme{hash: crypto.SHA384},
KDF_HKDF_SHA512: hkdfScheme{hash: crypto.SHA512},
}
///////////////////////////
// Pre-defined AEAD identifiers
type AEADID uint16
const (
AEAD_AESGCM128 AEADID = 0x0001
AEAD_AESGCM256 AEADID = 0x0002
AEAD_CHACHA20POLY1305 AEADID = 0x0003
AEAD_EXPORT_ONLY AEADID = 0xFFFF
)
var aeads = map[AEADID]AEADScheme{
AEAD_AESGCM128: aesgcmScheme{keySize: 16},
AEAD_AESGCM256: aesgcmScheme{keySize: 32},
AEAD_CHACHA20POLY1305: chachaPolyScheme{},
AEAD_EXPORT_ONLY: exportOnlyScheme{},
}
func AssembleCipherSuite(kemID KEMID, kdfID KDFID, aeadID AEADID) (CipherSuite, error) {
kem, ok := newKEMScheme(kemID)
if !ok {
return CipherSuite{}, fmt.Errorf("Unknown KEM id")
}
kdf, ok := kdfs[kdfID]
if !ok {
return CipherSuite{}, fmt.Errorf("Unknown KDF id")
}
aead, ok := aeads[aeadID]
if !ok {
return CipherSuite{}, fmt.Errorf("Unknown AEAD id")
}
return CipherSuite{
KEM: kem,
KDF: kdf,
AEAD: aead,
}, nil
}
//////////
// Helpers
func kemSuiteFromID(id KEMID) []byte {
idBuffer := make([]byte, 2)
binary.BigEndian.PutUint16(idBuffer, uint16(id))
return append([]byte("KEM"), idBuffer...)
}