-
Notifications
You must be signed in to change notification settings - Fork 136
/
sike.go
196 lines (157 loc) · 4.68 KB
/
sike.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
// Code generated from pkg.templ.go. DO NOT EDIT.
// Package sikep503 implements the key encapsulation mechanism SIKEp503.
package sikep503
import (
"github.com/cloudflare/circl/dh/sidh"
"github.com/cloudflare/circl/internal/sha3"
"github.com/cloudflare/circl/kem"
"bytes"
cryptoRand "crypto/rand"
"crypto/subtle"
"io"
)
type PrivateKey struct {
sk *sidh.PrivateKey
pk *sidh.PublicKey
}
type PublicKey sidh.PublicKey
const (
SeedSize = 32
EncapsulationSeedSize = 32
)
type scheme struct{}
var sch kem.Scheme = &scheme{}
// Scheme returns a KEM interface.
func Scheme() kem.Scheme { return sch }
var params *sidh.KEM
func (*scheme) Name() string { return "SIKEp503" }
func (*scheme) PublicKeySize() int { return params.PublicKeySize() }
func (*scheme) PrivateKeySize() int { return params.PrivateKeySize() }
func (*scheme) SeedSize() int { return SeedSize }
func (*scheme) SharedKeySize() int { return params.SharedSecretSize() }
func (*scheme) CiphertextSize() int { return params.CiphertextSize() }
func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize }
func (sk *PrivateKey) Scheme() kem.Scheme { return sch }
func (pk *PublicKey) Scheme() kem.Scheme { return sch }
func (sk *PrivateKey) MarshalBinary() ([]byte, error) {
ret := make([]byte, sk.sk.Size())
sk.sk.Export(ret)
return ret, nil
}
func (sk *PrivateKey) Equal(other kem.PrivateKey) bool {
oth, ok := other.(*PrivateKey)
if !ok {
return false
}
a, _ := sk.MarshalBinary()
b, _ := oth.MarshalBinary()
return subtle.ConstantTimeCompare(a, b) == 1
}
func (sk *PrivateKey) Public() kem.PublicKey {
if sk.pk == nil {
sk.pk = sidh.NewPublicKey(sidh.Fp503, sidh.KeyVariantSike)
sk.sk.GeneratePublicKey(sk.pk)
}
return (*PublicKey)(sk.pk)
}
func (pk *PublicKey) Equal(other kem.PublicKey) bool {
oth, ok := other.(*PublicKey)
if !ok {
return false
}
a, _ := pk.MarshalBinary()
b, _ := oth.MarshalBinary()
return bytes.Equal(a, b)
}
func (pk *PublicKey) MarshalBinary() ([]byte, error) {
cpk := (*sidh.PublicKey)(pk)
ret := make([]byte, cpk.Size())
cpk.Export(ret)
return ret, nil
}
func GenerateKeyPair(rand io.Reader) (kem.PublicKey, kem.PrivateKey, error) {
sk := sidh.NewPrivateKey(sidh.Fp503, sidh.KeyVariantSike)
if err := sk.Generate(rand); err != nil {
return nil, nil, err
}
priv := &PrivateKey{sk: sk}
return priv.Public(), priv, nil
}
func (*scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
return GenerateKeyPair(cryptoRand.Reader)
}
func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
if len(seed) != SeedSize {
panic(kem.ErrSeedSize)
}
h := sha3.NewShake256()
_, _ = h.Write(seed[:])
pk, sk, err := GenerateKeyPair(&h)
if err != nil {
panic(err)
}
return pk, sk
}
func (sch *scheme) Encapsulate(pk kem.PublicKey) (ct []byte, ss []byte, err error) {
var seed [EncapsulationSeedSize]byte
cryptoRand.Read(seed[:])
return sch.EncapsulateDeterministically(pk, seed[:])
}
func (sch *scheme) EncapsulateDeterministically(pk kem.PublicKey, seed []byte) (ct []byte, ss []byte, err error) {
if len(seed) != EncapsulationSeedSize {
return nil, nil, kem.ErrSeedSize
}
pub, ok := pk.(*PublicKey)
if !ok {
return nil, nil, kem.ErrTypeMismatch
}
ct = make([]byte, sch.CiphertextSize())
ss = make([]byte, sch.SharedKeySize())
h := sha3.NewShake256()
_, _ = h.Write(seed[:])
ctx := sidh.NewSike503(&h)
if err := ctx.Encapsulate(ct, ss, (*sidh.PublicKey)(pub)); err != nil {
return nil, nil, err
}
return ct, ss, nil
}
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
}
sikePub := sidh.NewPublicKey(sidh.Fp503, sidh.KeyVariantSike)
priv.sk.GeneratePublicKey(sikePub)
ss := make([]byte, sch.SharedKeySize())
ctx := sidh.NewSike503(nil)
if err := ctx.Decapsulate(ss, priv.sk, sikePub, ct); err != nil {
return nil, err
}
return ss, nil
}
func (sch *scheme) UnmarshalBinaryPublicKey(buf []byte) (kem.PublicKey, error) {
if len(buf) != sch.PublicKeySize() {
return nil, kem.ErrPubKeySize
}
pk := sidh.NewPublicKey(sidh.Fp503, sidh.KeyVariantSike)
if err := pk.Import(buf); err != nil {
return nil, err
}
return (*PublicKey)(pk), nil
}
func (sch *scheme) UnmarshalBinaryPrivateKey(buf []byte) (kem.PrivateKey, error) {
if len(buf) != sch.PrivateKeySize() {
return nil, kem.ErrPrivKeySize
}
sk := sidh.NewPrivateKey(sidh.Fp503, sidh.KeyVariantSike)
if err := sk.Import(buf); err != nil {
return nil, err
}
return &PrivateKey{sk: sk}, nil
}
func init() {
params = sidh.NewSike503(nil)
}