-
Notifications
You must be signed in to change notification settings - Fork 145
/
hybridkem.go
232 lines (199 loc) · 5.85 KB
/
hybridkem.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
package hpke
// This file implements a hybrid KEM for HPKE using a simple concatenation
// combiner.
//
// WARNING It is not safe to combine arbitrary KEMs using this combiner.
// See the draft specification for more details:
// https://bwesterb.github.io/draft-westerbaan-cfrg-hpke-xyber768d00/draft-westerbaan-cfrg-hpke-xyber768d00.html#name-security-considerations
import (
"crypto/rand"
"github.com/cloudflare/circl/kem"
)
type hybridKEM struct {
kemBase
kemA kem.Scheme
kemB kem.Scheme
}
func (h hybridKEM) PrivateKeySize() int { return h.kemA.PrivateKeySize() + h.kemB.PrivateKeySize() }
func (h hybridKEM) SeedSize() int { return 32 }
func (h hybridKEM) CiphertextSize() int { return h.kemA.CiphertextSize() + h.kemB.CiphertextSize() }
func (h hybridKEM) PublicKeySize() int { return h.kemA.PublicKeySize() + h.kemB.PublicKeySize() }
func (h hybridKEM) EncapsulationSeedSize() int {
return h.kemA.EncapsulationSeedSize() + h.kemB.EncapsulationSeedSize()
}
func (h hybridKEM) SharedKeySize() int { return h.kemA.SharedKeySize() + h.kemB.SharedKeySize() }
func (h hybridKEM) Name() string { return h.name }
func (h hybridKEM) AuthDecapsulate(skR kem.PrivateKey,
ct []byte,
pkS kem.PublicKey,
) ([]byte, error) {
panic("AuthDecapsulate is not supported for this KEM")
}
func (h hybridKEM) AuthEncapsulate(pkr kem.PublicKey, sks kem.PrivateKey) (
ct []byte, ss []byte, err error,
) {
panic("AuthEncapsulate is not supported for this KEM")
}
func (h hybridKEM) AuthEncapsulateDeterministically(pkr kem.PublicKey, sks kem.PrivateKey, seed []byte) (ct, ss []byte, err error) {
panic("AuthEncapsulateDeterministically is not supported for this KEM")
}
func (h hybridKEM) Encapsulate(pkr kem.PublicKey) (
ct []byte, ss []byte, err error,
) {
panic("Encapsulate is not implemented")
}
func (h hybridKEM) Decapsulate(skr kem.PrivateKey, ct []byte) ([]byte, error) {
hybridSk := skr.(*hybridKEMPrivKey)
ssA, err := h.kemA.Decapsulate(hybridSk.privA, ct[0:h.kemA.CiphertextSize()])
if err != nil {
return nil, err
}
ssB, err := h.kemB.Decapsulate(hybridSk.privB, ct[h.kemA.CiphertextSize():])
if err != nil {
return nil, err
}
ss := append(ssA, ssB...)
return ss, nil
}
func (h hybridKEM) EncapsulateDeterministically(
pkr kem.PublicKey, seed []byte,
) (ct, ss []byte, err error) {
hybridPk := pkr.(*hybridKEMPubKey)
encA, ssA, err := h.kemA.EncapsulateDeterministically(hybridPk.pubA, seed[0:h.kemA.EncapsulationSeedSize()])
if err != nil {
return nil, nil, err
}
encB, ssB, err := h.kemB.EncapsulateDeterministically(hybridPk.pubB, seed[h.kemA.EncapsulationSeedSize():])
if err != nil {
return nil, nil, err
}
ct = append(encA, encB...)
ss = append(ssA, ssB...)
return ct, ss, nil
}
type hybridKEMPrivKey struct {
scheme kem.Scheme
privA kem.PrivateKey
privB kem.PrivateKey
}
func (k *hybridKEMPrivKey) Scheme() kem.Scheme {
return k.scheme
}
func (k *hybridKEMPrivKey) MarshalBinary() ([]byte, error) {
skA, err := k.privA.MarshalBinary()
if err != nil {
return nil, err
}
skB, err := k.privB.MarshalBinary()
if err != nil {
return nil, err
}
return append(skA, skB...), nil
}
func (k *hybridKEMPrivKey) Equal(sk kem.PrivateKey) bool {
k1, ok := sk.(*hybridKEMPrivKey)
return ok &&
k.privA.Equal(k1.privA) &&
k.privB.Equal(k1.privB)
}
func (k *hybridKEMPrivKey) Public() kem.PublicKey {
return &hybridKEMPubKey{
scheme: k.scheme,
pubA: k.privA.Public(),
pubB: k.privB.Public(),
}
}
type hybridKEMPubKey struct {
scheme kem.Scheme
pubA kem.PublicKey
pubB kem.PublicKey
}
func (k *hybridKEMPubKey) Scheme() kem.Scheme {
return k.scheme
}
func (k hybridKEMPubKey) MarshalBinary() ([]byte, error) {
pkA, err := k.pubA.MarshalBinary()
if err != nil {
return nil, err
}
pkB, err := k.pubB.MarshalBinary()
if err != nil {
return nil, err
}
return append(pkA, pkB...), nil
}
func (k *hybridKEMPubKey) Equal(pk kem.PublicKey) bool {
k1, ok := pk.(*hybridKEMPubKey)
return ok &&
k.pubA.Equal(k1.pubA) &&
k.pubB.Equal(k1.pubB)
}
// Deterministically derives a keypair from a seed. If you're unsure,
// you're better off using GenerateKey().
//
// Panics if seed is not of length SeedSize().
func (h hybridKEM) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
// Implementation based on
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hpke-07.html#name-derivekeypair
if len(seed) != h.SeedSize() {
panic(kem.ErrSeedSize)
}
outputSeedSize := h.kemA.SeedSize() + h.kemB.SeedSize()
dkpPrk := h.labeledExtract([]byte(""), []byte("dkp_prk"), seed)
bytes := h.labeledExpand(
dkpPrk,
[]byte("sk"),
nil,
uint16(outputSeedSize),
)
seedA := bytes[0:h.kemA.SeedSize()]
seedB := bytes[h.kemA.SeedSize():]
pubA, privA := h.kemA.DeriveKeyPair(seedA)
pubB, privB := h.kemB.DeriveKeyPair(seedB)
privKey := &hybridKEMPrivKey{
privA: privA,
privB: privB,
}
pubKey := &hybridKEMPubKey{
pubA: pubA,
pubB: pubB,
}
return pubKey, privKey
}
func (h hybridKEM) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
seed := make([]byte, h.SeedSize())
_, err := rand.Read(seed)
if err != nil {
return nil, nil, err
}
pk, sk := h.DeriveKeyPair(seed)
return pk, sk, nil
}
func (h hybridKEM) UnmarshalBinaryPrivateKey(data []byte) (kem.PrivateKey, error) {
skA, err := h.kemA.UnmarshalBinaryPrivateKey(data[0:h.kemA.PrivateKeySize()])
if err != nil {
return nil, err
}
skB, err := h.kemB.UnmarshalBinaryPrivateKey(data[h.kemA.PrivateKeySize():])
if err != nil {
return nil, err
}
return &hybridKEMPrivKey{
privA: skA,
privB: skB,
}, nil
}
func (h hybridKEM) UnmarshalBinaryPublicKey(data []byte) (kem.PublicKey, error) {
pkA, err := h.kemA.UnmarshalBinaryPublicKey(data[0:h.kemA.PublicKeySize()])
if err != nil {
return nil, err
}
pkB, err := h.kemB.UnmarshalBinaryPublicKey(data[h.kemA.PublicKeySize():])
if err != nil {
return nil, err
}
return &hybridKEMPubKey{
pubA: pkA,
pubB: pkB,
}, nil
}