-
Notifications
You must be signed in to change notification settings - Fork 3
/
hybrid.go
249 lines (210 loc) · 6.24 KB
/
hybrid.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
// Package hybrid defines several hybrid classical/quantum KEMs.
//
// KEMs are combined by hashing of shared secrets, cipher texts,
// public keys, etc, see
//
// https://eprint.iacr.org/2018/024.pdf
//
// For deriving a KEM keypair deterministically and encapsulating
// deterministically, we expand a single seed to both using Blake2b hash and then XOF,
// so that a non-uniform seed (such as a shared secret generated by a hybrid
// KEM where one of the KEMs is weak) doesn't impact just one of the KEMs.
package hybrid
import (
"errors"
"fmt"
"github.com/katzenpost/hpqc/kem"
"github.com/katzenpost/hpqc/kem/pem"
"github.com/katzenpost/hpqc/kem/util"
"golang.org/x/crypto/blake2b"
)
var (
ErrUninitialized = errors.New("public or private key not initialized")
)
var _ kem.PrivateKey = (*PrivateKey)(nil)
var _ kem.PublicKey = (*PublicKey)(nil)
var _ kem.Scheme = (*Scheme)(nil)
// Public key of a hybrid KEM.
type PublicKey struct {
scheme *Scheme
first kem.PublicKey
second kem.PublicKey
}
// Private key of a hybrid KEM.
type PrivateKey struct {
scheme *Scheme
first kem.PrivateKey
second kem.PrivateKey
}
// Scheme for a hybrid KEM.
type Scheme struct {
name string
first kem.Scheme
second kem.Scheme
}
// New creates a new hybrid KEM given the first and second KEMs.
func New(name string, first kem.Scheme, second kem.Scheme) *Scheme {
return &Scheme{
name: name,
first: first,
second: second,
}
}
func (sch *Scheme) Name() string { return sch.name }
func (sch *Scheme) PublicKeySize() int {
return sch.first.PublicKeySize() + sch.second.PublicKeySize()
}
func (sch *Scheme) PrivateKeySize() int {
return sch.first.PrivateKeySize() + sch.second.PrivateKeySize()
}
func (sch *Scheme) SeedSize() int {
return sch.first.SeedSize() + sch.second.SeedSize()
}
func (sch *Scheme) SharedKeySize() int {
return blake2b.Size256
}
func (sch *Scheme) CiphertextSize() int {
return sch.first.CiphertextSize() + sch.second.CiphertextSize()
}
func (sk *PrivateKey) Scheme() kem.Scheme { return sk.scheme }
func (pk *PublicKey) Scheme() kem.Scheme { return pk.scheme }
func (sk *PrivateKey) MarshalBinary() ([]byte, error) {
if sk.first == nil || sk.second == nil {
return nil, ErrUninitialized
}
first, err := sk.first.MarshalBinary()
if err != nil {
return nil, err
}
second, err := sk.second.MarshalBinary()
if err != nil {
return nil, err
}
return append(first, second...), nil
}
func (sk *PublicKey) MarshalText() (text []byte, err error) {
return pem.ToPublicPEMBytes(sk), nil
}
func (sk *PrivateKey) Equal(other kem.PrivateKey) bool {
oth, ok := other.(*PrivateKey)
if !ok {
return false
}
return sk.first.Equal(oth.first) && sk.second.Equal(oth.second)
}
func (sk *PrivateKey) Public() kem.PublicKey {
return &PublicKey{sk.scheme, sk.first.Public(), sk.second.Public()}
}
func (pk *PublicKey) Equal(other kem.PublicKey) bool {
oth, ok := other.(*PublicKey)
if !ok {
return false
}
return pk.first.Equal(oth.first) && pk.second.Equal(oth.second)
}
func (pk *PublicKey) MarshalBinary() ([]byte, error) {
if pk.first == nil || pk.second == nil {
return nil, ErrUninitialized
}
first, err := pk.first.MarshalBinary()
if err != nil {
return nil, err
}
second, err := pk.second.MarshalBinary()
if err != nil {
return nil, err
}
return append(first, second...), nil
}
func (sch *Scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
pk1, sk1, err := sch.first.GenerateKeyPair()
if err != nil {
return nil, nil, err
}
pk2, sk2, err := sch.second.GenerateKeyPair()
if err != nil {
return nil, nil, err
}
return &PublicKey{sch, pk1, pk2}, &PrivateKey{sch, sk1, sk2}, nil
}
func (sch *Scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
if len(seed) != sch.first.SeedSize()+sch.second.SeedSize() {
panic(fmt.Sprintf("seed size must be %d", sch.first.SeedSize()+sch.second.SeedSize()))
}
pk1, sk1 := sch.first.DeriveKeyPair(seed[:sch.first.SeedSize()])
pk2, sk2 := sch.second.DeriveKeyPair(seed[sch.first.SeedSize():])
return &PublicKey{sch, pk1, pk2}, &PrivateKey{sch, sk1, sk2}
}
func (sch *Scheme) Encapsulate(pk kem.PublicKey) (ct, ss []byte, err error) {
pub, ok := pk.(*PublicKey)
if !ok {
return nil, nil, kem.ErrTypeMismatch
}
ct1, ss1, err := sch.first.Encapsulate(pub.first)
if err != nil {
return nil, nil, err
}
ct2, ss2, err := sch.second.Encapsulate(pub.second)
if err != nil {
return nil, nil, err
}
return append(ct1, ct2...), util.PairSplitPRF(ss1, ss2, ct1, ct2), nil
}
func (sch *Scheme) EncapsulateDeterministically(publicKey kem.PublicKey, seed []byte) (ct, ss []byte, err error) {
panic("not implemented")
}
func (sch *Scheme) Decapsulate(sk kem.PrivateKey, ct []byte) ([]byte, error) {
if len(ct) != sch.CiphertextSize() {
return nil, kem.ErrCiphertextSize
}
priv, ok := sk.(*PrivateKey)
if !ok {
return nil, kem.ErrTypeMismatch
}
firstSize := sch.first.CiphertextSize()
ss1, err := sch.first.Decapsulate(priv.first, ct[:firstSize])
if err != nil {
return nil, err
}
ss2, err := sch.second.Decapsulate(priv.second, ct[firstSize:])
if err != nil {
return nil, err
}
return util.PairSplitPRF(ss1, ss2, ct[:firstSize], ct[firstSize:]), nil
}
func (sch *Scheme) UnmarshalBinaryPublicKey(buf []byte) (kem.PublicKey, error) {
if len(buf) != sch.PublicKeySize() {
return nil, kem.ErrPubKeySize
}
firstSize := sch.first.PublicKeySize()
pk1, err := sch.first.UnmarshalBinaryPublicKey(buf[:firstSize])
if err != nil {
return nil, err
}
pk2, err := sch.second.UnmarshalBinaryPublicKey(buf[firstSize:])
if err != nil {
return nil, err
}
return &PublicKey{sch, pk1, pk2}, nil
}
func (sch *Scheme) UnmarshalBinaryPrivateKey(buf []byte) (kem.PrivateKey, error) {
if len(buf) != sch.PrivateKeySize() {
return nil, kem.ErrPrivKeySize
}
firstSize := sch.first.PrivateKeySize()
sk1, err := sch.first.UnmarshalBinaryPrivateKey(buf[:firstSize])
if err != nil {
return nil, err
}
sk2, err := sch.second.UnmarshalBinaryPrivateKey(buf[firstSize:])
if err != nil {
return nil, err
}
return &PrivateKey{sch, sk1, sk2}, nil
}
func (a *Scheme) UnmarshalTextPublicKey(text []byte) (kem.PublicKey, error) {
return pem.FromPublicPEMBytes(text, a)
}
func (a *Scheme) UnmarshalTextPrivateKey(text []byte) (kem.PrivateKey, error) {
return pem.FromPrivatePEMBytes(text, a)
}