-
Notifications
You must be signed in to change notification settings - Fork 1
/
handshake.go
128 lines (95 loc) · 2.46 KB
/
handshake.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package rosenpass
import (
"errors"
"github.com/cloudflare/circl/kem"
)
var (
// TODO: Only expose errors which are need on the public API.
errUnexpectedMsgType = errors.New("received unexpected message type")
errPeerNotFound = errors.New("peer not found")
errSessionNotFound = errors.New("session not found")
errInvalidAuthTag = errors.New("invalid authentication tag")
errReplayDetected = errors.New("detected replay")
errStaleNonce = errors.New("stale nonce")
errInvalidBiscuit = errors.New("failed decrypt biscuit")
)
type handshake struct {
peer *peer
server *Server
sidi sid // Initiator session ID
sidr sid // Responder session ID
ck key // The chaining key
epki epk // The initiator’s ephemeral public key
txnm uint64 // My transmission nonce
txnt uint64 // Their transmission nonce
txkm key // My transmission key
txkt key // Their transmission key
osk key // Output shared key
}
// Helpers
func (hs *handshake) enterLive() {
hs.txnm = 0
hs.txnt = 0
hs.osk = hs.ck.hash(khOSK[:])
hs.peer.logger.Debug("Enter live")
}
func (hs *handshake) mix(data ...[]byte) {
hs.ck = hs.ck.mix(data...)
}
func (hs *handshake) encryptAndMix(pt []byte) ([]byte, error) {
k := hs.ck.hash(khHsEnc[:])
n := nonce{}
ad := []byte{}
aead, err := newAEAD(k)
if err != nil {
return nil, err
}
ct := aead.Seal(nil, n[:], pt, ad)
hs.mix(ct)
return ct, nil
}
func (hs *handshake) decryptAndMix(ct []byte) ([]byte, error) {
k := hs.ck.hash(khHsEnc[:])
n := nonce{}
ad := []byte{}
aead, err := newAEAD(k)
if err != nil {
return nil, err
}
pt, err := aead.Open(nil, n[:], ct, ad)
if err != nil {
return nil, err
}
hs.mix(ct)
return pt, nil
}
func (hs *handshake) encapAndMix(typ kem.Scheme, pk []byte) ([]byte, error) {
kem, err := newKEM(typ, pk)
if err != nil {
return nil, err
}
ct, shk, err := kem.EncapSecret(pk)
if err != nil {
return nil, err
}
hs.mix(pk, shk, ct)
return ct, nil
}
func (hs *handshake) decapAndMix(typ kem.Scheme, sk, pk, ct []byte) error {
kem, err := newKEM(typ, sk)
if err != nil {
return err
}
shk, err := kem.DecapSecret(ct)
if err != nil {
return err
}
hs.mix(pk, shk, ct)
return nil
}
func (hs *handshake) send(pl payload, ep Endpoint) error {
hs.peer.logger.Debug("Sending message", "type", msgTypeFromPayload(pl))
return hs.server.conn.Send(pl, hs.peer.spkt, ep)
}