forked from mullvad/wgephemeralpeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pq.go
73 lines (58 loc) · 1.44 KB
/
pq.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
package wgephemeralpeer
import (
"errors"
"github.com/cloudflare/circl/kem"
"github.com/cloudflare/circl/kem/kyber/kyber1024"
"github.com/cloudflare/circl/kem/mceliece/mceliece460896"
"github.com/cloudflare/circl/kem/mlkem/mlkem1024"
)
var (
ErrInvalidCiphertextsCount = errors.New("invalid ciphertexts count")
)
var (
schemeMcEliece460896Round3 kem.Scheme = mceliece460896.Scheme()
schemeKyber1024 kem.Scheme = kyber1024.Scheme()
schemeMLKEM1024 kem.Scheme = mlkem1024.Scheme()
)
type pqkem struct {
scheme kem.Scheme
privateKey []byte
publicKey []byte
}
func (ep *ephemeralPeer) initPQ() error {
for _, s := range ep.kemSchemes {
publicKey, privateKey, err := s.GenerateKeyPair()
if err != nil {
return err
}
k := pqkem{scheme: s}
k.privateKey, _ = privateKey.MarshalBinary()
k.publicKey, _ = publicKey.MarshalBinary()
ep.kems = append(ep.kems, k)
}
return nil
}
func (ep *ephemeralPeer) generatePSK(ciphertexts [][]byte) ([]byte, error) {
if len(ciphertexts) != len(ep.kems) {
return nil, ErrInvalidCiphertextsCount
}
var psk []byte
for i, k := range ep.kems {
ct := ciphertexts[i]
csk, err := k.scheme.UnmarshalBinaryPrivateKey(k.privateKey)
if err != nil {
return nil, err
}
s, err := k.scheme.Decapsulate(csk, ct)
if err != nil {
return nil, err
}
if psk == nil {
psk = make([]byte, len(s))
}
for j, v := range s {
psk[j] ^= v
}
}
return psk, nil
}