-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_he.go
149 lines (133 loc) · 4.66 KB
/
session_he.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
package ratchet
import "fmt"
// SessionHE is the session of the party involved the Double Ratchet Algorithm with encrypted header modification.
type SessionHE interface {
// RatchetEncrypt performs a symmetric-key ratchet step, then AEAD-encrypts
// the header-encrypted message with the resulting message key.
RatchetEncrypt(plaintext, associatedData []byte) MessageHE
// RatchetDecrypt is called to AEAD-decrypt header-encrypted messages.
RatchetDecrypt(m MessageHE, associatedData []byte) ([]byte, error)
}
type sessionHE struct {
state
}
// NewHE creates session with the shared keys.
func NewHE(sharedKey, sharedHka, sharedNhkb Key, keyPair DHPair, opts ...option) (SessionHE, error) {
state, err := newState(sharedKey, opts...)
if err != nil {
return nil, err
}
state.DHs = keyPair
state.NHKs = sharedNhkb
state.HKs = sharedHka
state.NHKr = sharedHka
return &sessionHE{state}, nil
}
// NewHEWithRemoteKey creates session with the shared keys and public key of the other party.
func NewHEWithRemoteKey(sharedKey, sharedHka, sharedNhkb, remoteKey Key, opts ...option) (SessionHE, error) {
state, err := newState(sharedKey, opts...)
if err != nil {
return nil, err
}
state.DHs, err = state.Crypto.GenerateDH()
if err != nil {
return nil, fmt.Errorf("can't generate key pair: %s", err)
}
state.DHr = remoteKey
state.SendCh, state.NHKs = state.RootCh.step(state.Crypto.DH(state.DHs, state.DHr))
state.HKs = sharedHka
state.NHKr = sharedNhkb
state.HKr = sharedHka
return &sessionHE{state}, nil
}
// RatchetEncrypt performs a symmetric-key ratchet step, then encrypts the header with
// the corresponding header key and the message with resulting message key.
func (s *sessionHE) RatchetEncrypt(plaintext, ad []byte) MessageHE {
var (
h = MessageHeader{
DH: s.DHs.PublicKey(),
N: s.SendCh.N,
PN: s.PN,
}
mk = s.SendCh.step()
hEnc = s.Crypto.Encrypt(s.HKs, h.Encode(), nil)
)
return MessageHE{
Header: hEnc,
Ciphertext: s.Crypto.Encrypt(mk, plaintext, append(ad, hEnc...)),
}
}
// RatchetDecrypt is called to AEAD-decrypt header-encrypted messages.
func (s *sessionHE) RatchetDecrypt(m MessageHE, ad []byte) ([]byte, error) {
// Is the message one of the skipped?
if plaintext, err := s.trySkippedMessages(m, ad); err != nil || plaintext != nil {
return plaintext, err
}
h, step, err := s.decryptHeader(m.Header)
if err != nil {
return nil, fmt.Errorf("can't decrypt header: %s", err)
}
var (
// All changes must be applied on a different session object, so that this session won't be modified nor left in a dirty session.
sc state = s.state
skippedKeys1 []skippedKey
skippedKeys2 []skippedKey
)
if step {
if skippedKeys1, err = sc.skipMessageKeys(sc.HKr, uint(h.PN)); err != nil {
return nil, fmt.Errorf("can't skip previous chain message keys: %s", err)
}
if err = sc.dhRatchet(h); err != nil {
return nil, fmt.Errorf("can't perform ratchet step: %s", err)
}
}
// After all, update the current chain.
if skippedKeys2, err = sc.skipMessageKeys(sc.HKr, uint(h.N)); err != nil {
return nil, fmt.Errorf("can't skip current chain message keys: %s", err)
}
mk := sc.RecvCh.step()
plaintext, err := s.Crypto.Decrypt(mk, m.Ciphertext, append(ad, m.Header...))
if err != nil {
return nil, fmt.Errorf("can't decrypt: %s", err)
}
s.applyChanges(sc, append(skippedKeys1, skippedKeys2...))
if step {
s.deleteSkippedKeys(s.HKr)
}
return plaintext, nil
}
func (s *sessionHE) decryptHeader(encHeader []byte) (MessageHeader, bool, error) {
if encoded, err := s.Crypto.Decrypt(s.HKr, encHeader, nil); err == nil {
h, err := MessageEncHeader(encoded).Decode()
return h, false, err
}
if encoded, err := s.Crypto.Decrypt(s.NHKr, encHeader, nil); err == nil {
h, err := MessageEncHeader(encoded).Decode()
return h, true, err
}
return MessageHeader{}, false, fmt.Errorf("invalid header")
}
func (s *sessionHE) trySkippedMessages(m MessageHE, ad []byte) ([]byte, error) {
for hk, keys := range s.MkSkipped.All() {
for n, mk := range keys {
hEnc, err := s.Crypto.Decrypt(hk, m.Header, nil)
if err != nil {
continue
}
h, err := MessageEncHeader(hEnc).Decode()
if err != nil {
return nil, fmt.Errorf("can't decode header %s for skipped message key under (%s, %d)", hEnc, hk, n)
}
if uint(h.N) != n {
continue
}
plaintext, err := s.Crypto.Decrypt(mk, m.Ciphertext, append(ad, m.Header...))
if err != nil {
return nil, fmt.Errorf("can't decrypt skipped message: %s", err)
}
s.MkSkipped.DeleteMk(hk, n)
return plaintext, nil
}
}
return nil, nil
}