-
Notifications
You must be signed in to change notification settings - Fork 16
/
state.go
1077 lines (913 loc) · 29.3 KB
/
state.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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mls
import (
"bytes"
"fmt"
"math/rand"
"reflect"
"github.com/cisco/go-tls-syntax"
)
///
/// GroupContext
///
type GroupContext struct {
GroupID []byte `tls:"head=1"`
Epoch Epoch
TreeHash []byte `tls:"head=1"`
ConfirmedTranscriptHash []byte `tls:"head=1"`
Extensions ExtensionList
}
///
/// State
///
type ProposalRef uint64
func toRef(id ProposalID) ProposalRef {
ref := uint64(0)
for i := uint(0); i < 8; i++ {
ref |= uint64(id.Hash[i]) << i
}
return ProposalRef(ref)
}
type updateSecrets struct {
Secret []byte `tls:"head=1"`
IdentityPriv *SignaturePrivateKey `tls:"optional"`
}
var supportedGroupExtensions = []ExtensionType{
// TODO
}
type State struct {
// Shared confirmed state
CipherSuite CipherSuite
GroupID []byte `tls:"head=1"`
Epoch Epoch
Tree TreeKEMPublicKey
ConfirmedTranscriptHash []byte `tls:"head=1"`
InterimTranscriptHash []byte `tls:"head=1"`
Extensions ExtensionList
// Per-participant non-secret state
Index LeafIndex `tls:"omit"`
IdentityPriv SignaturePrivateKey `tls:"omit"`
TreePriv TreeKEMPrivateKey `tls:"omit"`
Scheme SignatureScheme `tls:"omit"`
PendingProposals []MLSPlaintext `tls:"omit"`
// Secret state
PendingUpdates map[ProposalRef]updateSecrets `tls:"omit"`
Keys keyScheduleEpoch `tls:"omit"`
// Helpful information
NewCredentials map[LeafIndex]bool
}
func NewEmptyState(groupID []byte, leafSecret []byte, sigPriv SignaturePrivateKey, kp KeyPackage) (*State, error) {
return NewEmptyStateWithExtensions(groupID, leafSecret, sigPriv, kp, NewExtensionList())
}
func NewEmptyStateWithExtensions(groupID []byte, leafSecret []byte, sigPriv SignaturePrivateKey, kp KeyPackage, ext ExtensionList) (*State, error) {
suite := kp.CipherSuite
tree := NewTreeKEMPublicKey(suite)
index := tree.AddLeaf(kp)
treePriv := NewTreeKEMPrivateKey(suite, tree.Size(), index, leafSecret)
// Verify that the creator supports the group's extensions
for _, ext := range ext.Entries {
if !kp.Extensions.Has(ext.ExtensionType) {
return nil, fmt.Errorf("Unsupported extension type [%04x]", ext.ExtensionType)
}
}
secret := make([]byte, suite.newDigest().Size())
kse := newKeyScheduleEpoch(suite, 1, secret, []byte{})
s := &State{
CipherSuite: kp.CipherSuite,
GroupID: groupID,
Epoch: 0,
Tree: *tree,
Keys: kse,
Index: 0,
IdentityPriv: sigPriv,
TreePriv: *treePriv,
Scheme: kp.Credential.Scheme(),
PendingUpdates: map[ProposalRef]updateSecrets{},
ConfirmedTranscriptHash: []byte{},
InterimTranscriptHash: []byte{},
Extensions: ext,
NewCredentials: map[LeafIndex]bool{},
}
return s, nil
}
func NewStateFromWelcome(suite CipherSuite, epochSecret []byte, welcome Welcome) (*State, LeafIndex, []byte, error) {
// Decrypt the GroupInfo
gi, err := welcome.Decrypt(suite, epochSecret)
if err != nil {
return nil, 0, nil, err
}
// Construct the new state
s := &State{
CipherSuite: suite,
Epoch: gi.Epoch,
Tree: gi.Tree.Clone(),
GroupID: gi.GroupID,
ConfirmedTranscriptHash: gi.ConfirmedTranscriptHash,
InterimTranscriptHash: gi.InterimTranscriptHash,
Extensions: gi.Extensions,
PendingProposals: []MLSPlaintext{},
PendingUpdates: map[ProposalRef]updateSecrets{},
NewCredentials: map[LeafIndex]bool{},
}
// At this point, every leaf in the tree is new
// XXX(RLB) ... except our own
for i := LeafIndex(0); i < LeafIndex(s.Tree.Size()); i++ {
s.NewCredentials[i] = true
}
return s, gi.SignerIndex, gi.Confirmation, nil
}
func NewJoinedState(initSecret []byte, sigPrivs []SignaturePrivateKey, kps []KeyPackage, welcome Welcome) (*State, error) {
var initPriv HPKEPrivateKey
var sigPriv SignaturePrivateKey
var keyPackage KeyPackage
var encGroupSecrets EncryptedGroupSecrets
var found = false
suite := welcome.CipherSuite
// extract the keyPackage for init secret
for idx, kp := range kps {
data, err := syntax.Marshal(kp)
if err != nil {
return nil, fmt.Errorf("mls.state: kp %d marshal failure %v", idx, err)
}
kphash := welcome.CipherSuite.Digest(data)
// parse the encryptedKeyPackage to find our right kp
for _, egs := range welcome.Secrets {
found = bytes.Equal(kphash, egs.KeyPackageHash)
if found {
initPriv, err = kp.CipherSuite.hpke().Derive(initSecret)
if err != nil {
return nil, err
}
if !initPriv.PublicKey.Equals(kp.InitKey) {
return nil, fmt.Errorf("Incorrect init secret")
}
sigPriv = sigPrivs[idx]
keyPackage = kp
encGroupSecrets = egs
break
}
}
if found {
break
}
}
if !found {
return nil, fmt.Errorf("mls.state: unable to decrypt welcome message")
}
if keyPackage.CipherSuite != welcome.CipherSuite {
return nil, fmt.Errorf("mls.state: ciphersuite mismatch")
}
pt, err := suite.hpke().Decrypt(initPriv, []byte{}, encGroupSecrets.EncryptedGroupSecrets)
if err != nil {
return nil, fmt.Errorf("mls.state: encKeyPkg decryption failure %v", err)
}
var groupSecrets GroupSecrets
_, err = syntax.Unmarshal(pt, &groupSecrets)
if err != nil {
return nil, fmt.Errorf("mls.state: keyPkg unmarshal failure %v", err)
}
// Construct a new state based on the GroupInfo
s, signerIndex, confirmation, err := NewStateFromWelcome(suite, groupSecrets.EpochSecret, welcome)
if err != nil {
return nil, err
}
s.IdentityPriv = sigPriv
s.Scheme = keyPackage.Credential.Scheme()
// Verify that the joiner supports the group's extensions
for _, ext := range s.Extensions.Entries {
if !keyPackage.Extensions.Has(ext.ExtensionType) {
return nil, fmt.Errorf("Unsupported extension type [%04x]", ext.ExtensionType)
}
}
// Construct TreeKEM private key from parts provided
index, res := s.Tree.Find(keyPackage)
if !res {
return nil, fmt.Errorf("mls.state: new joiner not in the tree")
}
s.Index = index
commonAncestor := ancestor(s.Index, signerIndex)
var pathSecret []byte
if groupSecrets.PathSecret != nil {
pathSecret = groupSecrets.PathSecret.Data
}
treePriv := NewTreeKEMPrivateKeyForJoiner(s.CipherSuite, s.Index, s.Tree.Size(), initSecret, commonAncestor, pathSecret)
s.TreePriv = *treePriv
// Start up the key schedule
encGrpCtx, err := syntax.Marshal(s.groupContext())
if err != nil {
return nil, fmt.Errorf("mls.state: groupCtx marshal failure %v", err)
}
s.Keys = newKeyScheduleEpoch(suite, LeafCount(s.Tree.Size()), groupSecrets.EpochSecret, encGrpCtx)
// confirmation verification
if !s.verifyConfirmation(confirmation) {
return nil, fmt.Errorf("mls.state: confirmation failed to verify")
}
return s, nil
}
func (s State) Add(kp KeyPackage) (*MLSPlaintext, error) {
// Verify that the new member supports the group's extensions
for _, ext := range s.Extensions.Entries {
if !kp.Extensions.Has(ext.ExtensionType) {
return nil, fmt.Errorf("Unsupported extension type [%04x]", ext.ExtensionType)
}
}
addProposal := Proposal{
Add: &AddProposal{
KeyPackage: kp,
},
}
return s.sign(addProposal)
}
func (s State) Update(secret []byte, sigPriv *SignaturePrivateKey, kp KeyPackage) (*MLSPlaintext, error) {
updateProposal := Proposal{
Update: &UpdateProposal{
KeyPackage: kp,
},
}
pt, err := s.sign(updateProposal)
if err != nil {
return nil, err
}
ref := toRef(s.proposalID(*pt))
s.PendingUpdates[ref] = updateSecrets{dup(secret), sigPriv}
return pt, nil
}
func (s *State) Remove(removed LeafIndex) (*MLSPlaintext, error) {
removeProposal := Proposal{
Remove: &RemoveProposal{
Removed: removed,
},
}
pt, err := s.sign(removeProposal)
if err != nil {
return nil, err
}
return pt, nil
}
func (s *State) Commit(leafSecret []byte) (*MLSPlaintext, *Welcome, *State, error) {
// Construct and apply a commit message
commit := Commit{}
var joiners []KeyPackage
for _, pp := range s.PendingProposals {
pid := s.proposalID(pp)
proposal := pp.Content.Proposal
switch proposal.Type() {
case ProposalTypeAdd:
commit.Adds = append(commit.Adds, pid)
joiners = append(joiners, proposal.Add.KeyPackage)
case ProposalTypeUpdate:
commit.Updates = append(commit.Updates, pid)
case ProposalTypeRemove:
commit.Removes = append(commit.Removes, pid)
}
}
// init new state to apply commit and ratchet forward
next := s.Clone()
err := next.apply(commit)
if err != nil {
return nil, nil, nil, err
}
// reset after commit the proposals
next.PendingProposals = nil
// KEM new entropy to the new group if needed
if commit.PathRequired() {
ctx, err := syntax.Marshal(next.groupContext())
if err != nil {
return nil, nil, nil, err
}
treePriv, treePath, err := next.Tree.Encap(s.Index, ctx, leafSecret, next.IdentityPriv, nil)
if err != nil {
return nil, nil, nil, err
}
next.TreePriv = *treePriv
commit.Path = treePath
}
// Create the Commit message and advance the transcripts / key schedule
pt, err := next.ratchetAndSign(commit, next.TreePriv.UpdateSecret, s.groupContext(), s.IdentityPriv)
if err != nil {
return nil, nil, nil, fmt.Errorf("mls.state: racthet forward failed %v", err)
}
// Complete the GroupInfo and form the Welcome
gi := &GroupInfo{
GroupID: next.GroupID,
Epoch: next.Epoch,
Tree: next.Tree,
ConfirmedTranscriptHash: next.ConfirmedTranscriptHash,
InterimTranscriptHash: next.InterimTranscriptHash,
Confirmation: pt.Content.Commit.Confirmation.Data,
}
err = gi.sign(next.Index, &next.IdentityPriv)
if err != nil {
return nil, nil, nil, fmt.Errorf("mls.state: groupInfo sign failure %v", err)
}
welcome := newWelcome(s.CipherSuite, next.Keys.EpochSecret, gi)
for _, kp := range joiners {
leaf, ok := next.Tree.Find(kp)
if !ok {
return nil, nil, nil, fmt.Errorf("mls.state: New joiner not in tree")
}
_, pathSecret, ok := next.TreePriv.SharedPathSecret(leaf)
welcome.EncryptTo(kp, pathSecret)
}
return pt, welcome, next, nil
}
/// Proposal processing helpers
func (s *State) apply(commit Commit) error {
// state to identify proposals being processed
// in the PendingProposals. Avoids linear loop to
// remove entries from PendingProposals.
var processedProposals = map[string]bool{}
err := s.applyProposals(commit.Updates, processedProposals)
if err != nil {
return err
}
err = s.applyProposals(commit.Removes, processedProposals)
if err != nil {
return err
}
err = s.applyProposals(commit.Adds, processedProposals)
if err != nil {
return err
}
return nil
}
func (s *State) applyAddProposal(add *AddProposal) error {
if add.KeyPackage.CipherSuite != s.CipherSuite {
return fmt.Errorf("mls.state: new member kp does not use group ciphersuite")
}
if !add.KeyPackage.Verify() {
return fmt.Errorf("mls.state: Invalid kp")
}
target := s.Tree.AddLeaf(add.KeyPackage)
s.NewCredentials[target] = true
return nil
}
func (s *State) applyRemoveProposal(remove *RemoveProposal) {
s.Tree.BlankPath(LeafIndex(remove.Removed))
}
func (s *State) applyUpdateProposal(target LeafIndex, update *UpdateProposal) error {
if update.KeyPackage.CipherSuite != s.CipherSuite {
panic(fmt.Errorf("mls.state: update kp does not use group ciphersuite %v != %v", update.KeyPackage.CipherSuite, s.CipherSuite))
}
if !update.KeyPackage.Verify() {
return fmt.Errorf("mls.state: Invalid kp")
}
currKP, ok := s.Tree.KeyPackage(target)
if !ok {
return fmt.Errorf("mls.state: Attempt to update an empty leaf")
}
if !update.KeyPackage.Credential.Equals(currKP.Credential) {
s.NewCredentials[target] = true
}
s.Tree.UpdateLeaf(target, update.KeyPackage)
return nil
}
func (s *State) applyProposals(ids []ProposalID, processed map[string]bool) error {
for _, id := range ids {
pt, ok := s.findProposal(id)
if !ok {
return fmt.Errorf("mls.state: commit of unknown proposal %s", id)
}
// we have processed this proposal already
if processed[id.String()] {
continue
} else {
processed[id.String()] = true
}
proposal := pt.Content.Proposal
switch proposal.Type() {
case ProposalTypeAdd:
err := s.applyAddProposal(proposal.Add)
if err != nil {
return err
}
case ProposalTypeUpdate:
if pt.Sender.Type != SenderTypeMember {
return fmt.Errorf("mls.state: update from non-member")
}
senderIndex := LeafIndex(pt.Sender.Sender)
err := s.applyUpdateProposal(senderIndex, proposal.Update)
if err != nil {
return err
}
if senderIndex == s.Index {
secrets, ok := s.PendingUpdates[toRef(id)]
if !ok {
return fmt.Errorf("mls.state: self-update with no cached secret")
}
s.TreePriv.SetLeafSecret(secrets.Secret)
if secrets.IdentityPriv != nil {
s.IdentityPriv = *secrets.IdentityPriv
}
}
case ProposalTypeRemove:
s.applyRemoveProposal(proposal.Remove)
default:
return fmt.Errorf("mls.state: invalid proposal type")
}
}
return nil
}
func (s State) findProposal(id ProposalID) (MLSPlaintext, bool) {
for _, pt := range s.PendingProposals {
otherPid := s.proposalID(pt)
if bytes.Equal(otherPid.Hash, id.Hash) {
return pt, true
}
}
// we can return may be reference
// regardless, the call has to do a check before
// using the returned value
return MLSPlaintext{}, false
}
func (s State) proposalID(plaintext MLSPlaintext) ProposalID {
enc, err := syntax.Marshal(plaintext)
if err != nil {
panic(fmt.Errorf("mls.state: mlsPlainText marshal failure %v", err))
}
return ProposalID{
Hash: s.CipherSuite.Digest(enc),
}
}
func (s State) groupContext() GroupContext {
return GroupContext{
GroupID: s.GroupID,
Epoch: s.Epoch,
TreeHash: s.Tree.RootHash(),
ConfirmedTranscriptHash: s.ConfirmedTranscriptHash,
Extensions: s.Extensions,
}
}
func (s State) sign(p Proposal) (*MLSPlaintext, error) {
pt := &MLSPlaintext{
GroupID: s.GroupID,
Epoch: s.Epoch,
Sender: Sender{SenderTypeMember, uint32(s.Index)},
Content: MLSPlaintextContent{
Proposal: &p,
},
}
err := pt.sign(s.groupContext(), s.IdentityPriv, s.Scheme)
if err != nil {
return nil, err
}
return pt, nil
}
func (s *State) updateEpochSecrets(secret []byte) {
ctx, err := syntax.Marshal(GroupContext{
GroupID: s.GroupID,
Epoch: s.Epoch,
TreeHash: s.Tree.RootHash(),
ConfirmedTranscriptHash: s.ConfirmedTranscriptHash,
})
if err != nil {
panic(fmt.Errorf("mls.state: update epoch secret failed %v", err))
}
// TODO(RLB) Provide an API to provide PSKs
s.Keys = s.Keys.Next(LeafCount(s.Tree.Size()), nil, secret, ctx)
}
func (s *State) ratchetAndSign(op Commit, commitSecret []byte, prevGrpCtx GroupContext, sigPriv SignaturePrivateKey) (*MLSPlaintext, error) {
pt := &MLSPlaintext{
GroupID: s.GroupID,
Epoch: s.Epoch,
Sender: Sender{SenderTypeMember, uint32(s.Index)},
Content: MLSPlaintextContent{
Commit: &CommitData{
Commit: op,
},
},
}
// Update the Confirmed Transcript Hash
digest := s.CipherSuite.newDigest()
digest.Write(s.InterimTranscriptHash)
digest.Write(pt.commitContent())
s.ConfirmedTranscriptHash = digest.Sum(nil)
// Advance the key schedule
s.Epoch += 1
s.updateEpochSecrets(commitSecret)
// generate the confirmation based on the new keys
commit := pt.Content.Commit
hmac := s.CipherSuite.NewHMAC(s.Keys.ConfirmationKey)
hmac.Write(s.ConfirmedTranscriptHash)
commit.Confirmation.Data = hmac.Sum(nil)
// sign the MLSPlainText and update state hashes
// as a result of ratcheting.
err := pt.sign(prevGrpCtx, sigPriv, s.Scheme)
if err != nil {
return nil, err
}
authData, err := pt.commitAuthData()
if err != nil {
return nil, err
}
digest = s.CipherSuite.newDigest()
digest.Write(s.ConfirmedTranscriptHash)
digest.Write(authData)
s.InterimTranscriptHash = digest.Sum(nil)
return pt, nil
}
func (s State) signerPublicKey(sender Sender) (*SignaturePublicKey, error) {
switch sender.Type {
case SenderTypeMember:
kp, ok := s.Tree.KeyPackage(LeafIndex(sender.Sender))
if !ok {
return nil, fmt.Errorf("mls.state: Received from blank leaf")
}
return kp.Credential.PublicKey(), nil
default:
// TODO(RLB): Support add sent by new member
// TODO(RLB): Support add/remove signed by preconfigured key
return nil, fmt.Errorf("mls.state: Unsupported sender type")
}
}
func (s *State) Handle(pt *MLSPlaintext) (*State, error) {
if !bytes.Equal(pt.GroupID, s.GroupID) {
return nil, fmt.Errorf("mls.state: groupId mismatch")
}
if pt.Epoch != s.Epoch {
return nil, fmt.Errorf("mls.state: epoch mismatch, have %v, got %v", s.Epoch, pt.Epoch)
}
sigPubKey, err := s.signerPublicKey(pt.Sender)
if err != nil {
return nil, err
}
if !pt.verify(s.groupContext(), sigPubKey, s.Scheme) {
return nil, fmt.Errorf("invalid handshake message signature")
}
// Proposals get queued, do not result in a state transition
contentType := pt.Content.Type()
if contentType == ContentTypeProposal {
s.PendingProposals = append(s.PendingProposals, *pt)
return nil, nil
}
if contentType != ContentTypeCommit {
return nil, fmt.Errorf("mls.state: incorrect content type")
} else if pt.Sender.Type != SenderTypeMember {
return nil, fmt.Errorf("mls.state: commit from non-member")
}
if LeafIndex(pt.Sender.Sender) == s.Index {
return nil, fmt.Errorf("mls.state: handle own commits with caching")
}
// apply the commit and discard any remaining pending proposals
senderIndex := LeafIndex(pt.Sender.Sender)
commitData := pt.Content.Commit
next := s.Clone()
err = next.apply(commitData.Commit)
if err != nil {
return nil, err
}
next.PendingProposals = next.PendingProposals[:0]
// apply the direct path, if provided
commitSecret := s.CipherSuite.zero()
if commitData.Commit.Path != nil {
ctx, err := syntax.Marshal(GroupContext{
GroupID: next.GroupID,
Epoch: next.Epoch,
TreeHash: next.Tree.RootHash(),
ConfirmedTranscriptHash: next.ConfirmedTranscriptHash,
})
if err != nil {
return nil, fmt.Errorf("mls.state: failure to create context %v", err)
}
err = next.TreePriv.Decap(senderIndex, next.Tree, ctx, *commitData.Commit.Path)
if err != nil {
return nil, err
}
commitSecret = next.TreePriv.UpdateSecret
err = next.Tree.Merge(senderIndex, *commitData.Commit.Path)
if err != nil {
return nil, err
}
}
// Update the confirmed transcript hash
digest := next.CipherSuite.newDigest()
digest.Write(next.InterimTranscriptHash)
digest.Write(pt.commitContent())
next.ConfirmedTranscriptHash = digest.Sum(nil)
// Advance the key schedule
next.Epoch += 1
next.updateEpochSecrets(commitSecret)
// Verify confirmation MAC
if !next.verifyConfirmation(commitData.Confirmation.Data) {
return nil, fmt.Errorf("mls.state: confirmation failed to verify")
}
authData, err := pt.commitAuthData()
if err != nil {
return nil, err
}
// Update the interim transcript hash
digest = next.CipherSuite.newDigest()
digest.Write(next.ConfirmedTranscriptHash)
digest.Write(authData)
next.InterimTranscriptHash = digest.Sum(nil)
return next, nil
}
///// protect/unprotect and helpers
func (s State) verifyConfirmation(confirmation []byte) bool {
hmac := s.CipherSuite.NewHMAC(s.Keys.ConfirmationKey)
hmac.Write(s.ConfirmedTranscriptHash)
confirm := hmac.Sum(nil)
if !bytes.Equal(confirm, confirmation) {
return false
}
return true
}
func applyGuard(nonceIn []byte, reuseGuard [4]byte) []byte {
nonceOut := dup(nonceIn)
for i := range reuseGuard {
nonceOut[i] ^= reuseGuard[i]
}
return nonceOut
}
func (s *State) encrypt(pt *MLSPlaintext) (*MLSCiphertext, error) {
var generation uint32
var keys keyAndNonce
switch pt.Content.Type() {
case ContentTypeApplication:
generation, keys = s.Keys.ApplicationKeys.Next(s.Index)
case ContentTypeProposal, ContentTypeCommit:
generation, keys = s.Keys.HandshakeKeys.Next(s.Index)
default:
return nil, fmt.Errorf("mls.state: encrypt unknown content type")
}
var reuseGuard [4]byte
rand.Read(reuseGuard[:])
stream := syntax.NewWriteStream()
err := stream.WriteAll(s.Index, generation, reuseGuard)
if err != nil {
return nil, fmt.Errorf("mls.state: sender data marshal failure %v", err)
}
senderData := stream.Data()
senderDataNonce := make([]byte, s.CipherSuite.Constants().NonceSize)
rand.Read(senderDataNonce)
senderDataAADVal := senderDataAAD(s.GroupID, s.Epoch, pt.Content.Type(), senderDataNonce)
sdAead, _ := s.CipherSuite.NewAEAD(s.Keys.SenderDataKey)
sdCt := sdAead.Seal(nil, senderDataNonce, senderData, senderDataAADVal)
// content data
stream = syntax.NewWriteStream()
err = stream.Write(pt.Content)
if err == nil {
err = stream.Write(pt.Signature)
}
if err != nil {
return nil, fmt.Errorf("mls.state: content marshal failure %v", err)
}
content := stream.Data()
aad := contentAAD(s.GroupID, s.Epoch, pt.Content.Type(),
pt.AuthenticatedData, senderDataNonce, sdCt)
aead, _ := s.CipherSuite.NewAEAD(keys.Key)
contentCt := aead.Seal(nil, applyGuard(keys.Nonce, reuseGuard), content, aad)
// set up MLSCipherText
ct := &MLSCiphertext{
GroupID: s.GroupID,
Epoch: s.Epoch,
ContentType: pt.Content.Type(),
AuthenticatedData: pt.AuthenticatedData,
SenderDataNonce: senderDataNonce,
EncryptedSenderData: sdCt,
Ciphertext: contentCt,
}
return ct, nil
}
func (s *State) decrypt(ct *MLSCiphertext) (*MLSPlaintext, error) {
if !bytes.Equal(ct.GroupID, s.GroupID) {
return nil, fmt.Errorf("mls.state: ciphertext not from this group")
}
if ct.Epoch != s.Epoch {
return nil, fmt.Errorf("mls.state: ciphertext not from this epoch")
}
// handle sender data
sdAAD := senderDataAAD(ct.GroupID, ct.Epoch, ContentType(ct.ContentType), ct.SenderDataNonce)
sdAead, _ := s.CipherSuite.NewAEAD(s.Keys.SenderDataKey)
sd, err := sdAead.Open(nil, ct.SenderDataNonce, ct.EncryptedSenderData, sdAAD)
if err != nil {
return nil, fmt.Errorf("mls.state: senderData decryption failure %v", err)
}
// parse the senderData
var sender LeafIndex
var generation uint32
var reuseGuard [4]byte
stream := syntax.NewReadStream(sd)
_, err = stream.ReadAll(&sender, &generation, &reuseGuard)
if err != nil {
return nil, fmt.Errorf("mls.state: senderData unmarshal failure %v", err)
}
var keys keyAndNonce
contentType := ContentType(ct.ContentType)
switch contentType {
case ContentTypeApplication:
keys, err = s.Keys.ApplicationKeys.Get(sender, generation)
if err != nil {
return nil, fmt.Errorf("mls.state: application keys extraction failed %v", err)
}
s.Keys.ApplicationKeys.Erase(sender, generation)
case ContentTypeProposal, ContentTypeCommit:
keys, err = s.Keys.HandshakeKeys.Get(sender, generation)
if err != nil {
return nil, fmt.Errorf("mls.state: handshake keys extraction failed %v", err)
}
s.Keys.HandshakeKeys.Erase(sender, generation)
default:
return nil, fmt.Errorf("mls.state: unsupported content type")
}
aad := contentAAD(ct.GroupID, ct.Epoch, ContentType(ct.ContentType),
ct.AuthenticatedData, ct.SenderDataNonce, ct.EncryptedSenderData)
aead, _ := s.CipherSuite.NewAEAD(keys.Key)
content, err := aead.Open(nil, applyGuard(keys.Nonce, reuseGuard), ct.Ciphertext, aad)
if err != nil {
return nil, fmt.Errorf("mls.state: content decryption failure %v", err)
}
// parse the Content and Signature
stream = syntax.NewReadStream(content)
var mlsContent MLSPlaintextContent
var signature Signature
_, err = stream.Read(&mlsContent)
if err == nil {
_, err = stream.Read(&signature)
}
if err != nil {
return nil, fmt.Errorf("mls.state: content unmarshal failure %v", err)
}
_, _ = syntax.Unmarshal(content, &mlsContent)
pt := &MLSPlaintext{
GroupID: s.GroupID,
Epoch: s.Epoch,
Sender: Sender{SenderTypeMember, uint32(sender)},
AuthenticatedData: ct.AuthenticatedData,
Content: mlsContent,
Signature: signature,
}
return pt, nil
}
func (s *State) Protect(data []byte) (*MLSCiphertext, error) {
pt := &MLSPlaintext{
GroupID: s.GroupID,
Epoch: s.Epoch,
Sender: Sender{SenderTypeMember, uint32(s.Index)},
Content: MLSPlaintextContent{
Application: &ApplicationData{
Data: data,
},
},
}
err := pt.sign(s.groupContext(), s.IdentityPriv, s.Scheme)
if err != nil {
return nil, err
}
return s.encrypt(pt)
}
func (s *State) Unprotect(ct *MLSCiphertext) ([]byte, error) {
pt, err := s.decrypt(ct)
if err != nil {
return nil, err
}
sigPubKey, err := s.signerPublicKey(pt.Sender)
if err != nil {
return nil, err
}
if !pt.verify(s.groupContext(), sigPubKey, s.Scheme) {
return nil, fmt.Errorf("invalid message signature")
}
if pt.Content.Type() != ContentTypeApplication {
return nil, fmt.Errorf("unprotect attempted on non-application message")
}
return pt.Content.Application.Data, nil
}
func senderDataAAD(gid []byte, epoch Epoch, contentType ContentType, nonce []byte) []byte {
s := syntax.NewWriteStream()
err := s.Write(struct {
GroupID []byte `tls:"head=1"`
Epoch Epoch
ContentType ContentType
SenderDataNonce []byte `tls:"head=1"`
}{
GroupID: gid,
Epoch: epoch,
ContentType: contentType,
SenderDataNonce: nonce,
})
if err != nil {
return nil
}
return s.Data()
}
func contentAAD(gid []byte, epoch Epoch,
contentType ContentType, authenticatedData []byte,
nonce []byte, encSenderData []byte) []byte {
s := syntax.NewWriteStream()
err := s.Write(struct {
GroupID []byte `tls:"head=1"`
Epoch Epoch
ContentType ContentType
AuthenticatedData []byte `tls:"head=4"`
SenderDataNonce []byte `tls:"head=1"`
EncryptedSenderData []byte `tls:"head=1"`
}{
GroupID: gid,
Epoch: epoch,
ContentType: contentType,
AuthenticatedData: authenticatedData,
SenderDataNonce: nonce,
EncryptedSenderData: encSenderData,
})
if err != nil {
return nil
}
return s.Data()
}
func (s State) Clone() *State {
// Note: all the slice/map copy operations below on state are mere
// reference copies.
clone := &State{
CipherSuite: s.CipherSuite,
GroupID: dup(s.GroupID),
Epoch: s.Epoch,
Tree: s.Tree.Clone(),
ConfirmedTranscriptHash: nil,
InterimTranscriptHash: dup(s.InterimTranscriptHash),
Keys: s.Keys,
Index: s.Index,
IdentityPriv: s.IdentityPriv,
TreePriv: s.TreePriv.Clone(),
Scheme: s.Scheme,
PendingUpdates: s.PendingUpdates,
PendingProposals: make([]MLSPlaintext, len(s.PendingProposals)),
NewCredentials: map[LeafIndex]bool{},
}
copy(clone.PendingProposals, s.PendingProposals)
return clone
}
// Compare the public and shared private aspects of two nodes
func (s State) Equals(o State) bool {
suite := s.CipherSuite == o.CipherSuite
groupID := bytes.Equal(s.GroupID, o.GroupID)
epoch := s.Epoch == o.Epoch
tree := s.Tree.Equals(o.Tree)
cth := bytes.Equal(s.ConfirmedTranscriptHash, o.ConfirmedTranscriptHash)
ith := bytes.Equal(s.InterimTranscriptHash, o.InterimTranscriptHash)
keys := reflect.DeepEqual(s.Keys, o.Keys)
return suite && groupID && epoch && tree && cth && ith && keys