-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctidh511.go
297 lines (252 loc) · 8.67 KB
/
ctidh511.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//go:build Ctidh511
// +build Ctidh511
// DO NOT EDIT: generated code, see gen/main.go
package ctidh
/*
#include "binding511.h"
#include <csidh.h>
extern ctidh_fillrandom fillrandom_custom;
__attribute__((weak))
void custom_gen_private(void *const context, private_key *priv) {
csidh_private_withrng(priv, (uintptr_t)context, fillrandom_custom);
}
*/
import "C"
import (
"crypto/hmac"
"encoding/base64"
"io"
"unsafe"
gopointer "github.com/mattn/go-pointer"
)
var (
// Ctidh511PublicKeySize is the size in bytes of the public key.
Ctidh511PublicKeySize int
// Ctidh511PrivateKeySize is the size in bytes of the private key.
Ctidh511PrivateKeySize int
)
// Ctidh511PublicKey is a public CTIDH key.
type Ctidh511PublicKey struct {
publicKey C.public_key
}
// NewEmptyCtidh511PublicKey returns an uninitialized
// Ctidh511PublicKey which is suitable to be loaded
// via some serialization format via FromBytes
// or FromPEMFile methods.
func NewEmptyCtidh511PublicKey() *Ctidh511PublicKey {
return new(Ctidh511PublicKey)
}
// NewCtidh511PublicKey creates a new public key from
// the given key material or panics if the
// key data is not Ctidh511PublicKeySize.
func NewCtidh511PublicKey(key []byte) *Ctidh511PublicKey {
k := new(Ctidh511PublicKey)
err := k.FromBytes(key)
if err != nil {
panic(err)
}
return k
}
// String returns a string identifying
// this type as a CTIDH public key.
func (p *Ctidh511PublicKey) String() string {
return "Ctidh511_PublicKey"
}
// Reset resets the Ctidh511PublicKey to all zeros.
func (p *Ctidh511PublicKey) Reset() {
zeros := make([]byte, Ctidh511PublicKeySize)
err := p.FromBytes(zeros)
if err != nil {
panic(err)
}
}
// Bytes returns the Ctidh511PublicKey as a byte slice.
func (p *Ctidh511PublicKey) Bytes() []byte {
return C.GoBytes(unsafe.Pointer(&p.publicKey.A.x.c), C.int(C.UINTBIG_LIMBS*8))
}
// FromBytes loads a Ctidh511PublicKey from the given byte slice.
func (p *Ctidh511PublicKey) FromBytes(data []byte) error {
if len(data) != Ctidh511PublicKeySize {
return ErrPublicKeySize
}
p.publicKey = *((*C.public_key)(unsafe.Pointer(&data[0])))
if !C.validate(&p.publicKey) {
return ErrPublicKeyValidation
}
return nil
}
// Equal is a constant time comparison of the two public keys.
func (p *Ctidh511PublicKey) Equal(publicKey *Ctidh511PublicKey) bool {
return hmac.Equal(p.Bytes(), publicKey.Bytes())
}
// Blind performs a blinding operation
// and mutates the public key.
// See notes below about blinding operation with CTIDH.
func (p *Ctidh511PublicKey) Blind(blindingFactor *Ctidh511PrivateKey) error {
blinded, err := BlindCtidh511(blindingFactor, p)
if err != nil {
panic(err)
}
p.publicKey = blinded.publicKey
return nil
}
// MarshalBinary is an implementation of a method on the
// BinaryMarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PublicKey) MarshalBinary() ([]byte, error) {
return p.Bytes(), nil
}
// UnmarshalBinary is an implementation of a method on the
// BinaryUnmarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PublicKey) UnmarshalBinary(data []byte) error {
return p.FromBytes(data)
}
// MarshalText is an implementation of a method on the
// TextMarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PublicKey) MarshalText() ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(p.Bytes())), nil
}
// UnmarshalText is an implementation of a method on the
// TextUnmarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PublicKey) UnmarshalText(data []byte) error {
raw, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}
return p.FromBytes(raw)
}
// Ctidh511PrivateKey is a private CTIDH key.
type Ctidh511PrivateKey struct {
privateKey C.private_key
}
// NewEmptyCtidh511PrivateKey returns an uninitialized
// Ctidh511PrivateKey which is suitable to be loaded
// via some serialization format via FromBytes
// or FromPEMFile methods.
func NewEmptyCtidh511PrivateKey() *Ctidh511PrivateKey {
return new(Ctidh511PrivateKey)
}
// DeriveSecret derives a shared secret.
func (p *Ctidh511PrivateKey) DeriveSecret(publicKey *Ctidh511PublicKey) []byte {
return DeriveSecretCtidh511(p, publicKey)
}
// String returns a string identifying
// this type as a CTIDH private key.
func (p *Ctidh511PrivateKey) String() string {
return "Ctidh511_PrivateKey"
}
// Reset resets the Ctidh511PrivateKey to all zeros.
func (p *Ctidh511PrivateKey) Reset() {
zeros := make([]byte, Ctidh511PrivateKeySize)
err := p.FromBytes(zeros)
if err != nil {
panic(err)
}
}
// Bytes serializes Ctidh511PrivateKey into a byte slice.
func (p *Ctidh511PrivateKey) Bytes() []byte {
return C.GoBytes(unsafe.Pointer(&p.privateKey), C.primes_num)
}
// FromBytes loads a Ctidh511PrivateKey from the given byte slice.
func (p *Ctidh511PrivateKey) FromBytes(data []byte) error {
if len(data) != Ctidh511PrivateKeySize {
return ErrPrivateKeySize
}
p.privateKey = *((*C.private_key)(unsafe.Pointer(&data[0])))
return nil
}
// Equal is a constant time comparison of the two private keys.
func (p *Ctidh511PrivateKey) Equal(privateKey *Ctidh511PrivateKey) bool {
return hmac.Equal(p.Bytes(), privateKey.Bytes())
}
// Public returns the public key associated
// with the given private key.
func (p *Ctidh511PrivateKey) Public() *Ctidh511PublicKey {
return DeriveCtidh511PublicKey(p)
}
// MarshalBinary is an implementation of a method on the
// BinaryMarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PrivateKey) MarshalBinary() ([]byte, error) {
return p.Bytes(), nil
}
// UnmarshalBinary is an implementation of a method on the
// BinaryUnmarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PrivateKey) UnmarshalBinary(data []byte) error {
return p.FromBytes(data)
}
// MarshalText is an implementation of a method on the
// TextMarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PrivateKey) MarshalText() ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(p.Bytes())), nil
}
// UnmarshalText is an implementation of a method on the
// TextUnmarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh511PrivateKey) UnmarshalText(data []byte) error {
raw, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}
return p.FromBytes(raw)
}
// DeriveCtidh511PublicKey derives a public key given a private key.
func DeriveCtidh511PublicKey(privKey *Ctidh511PrivateKey) *Ctidh511PublicKey {
var base C.public_key
baseKey := new(Ctidh511PublicKey)
baseKey.publicKey = base
return groupActionCtidh511(privKey, baseKey)
}
// GenerateCtidh511KeyPair generates a new Ctidh511 private and then
// attempts to compute the Ctidh511 public key.
func GenerateCtidh511KeyPair() (*Ctidh511PrivateKey, *Ctidh511PublicKey) {
privKey := new(Ctidh511PrivateKey)
C.csidh_private(&privKey.privateKey)
return privKey, DeriveCtidh511PublicKey(privKey)
}
// GenerateCtidh511PrivateKey uses the given RNG to derive a new private key.
// This can be used to deterministically generate private keys if the
// entropy source is deterministic, for example an HKDF.
func GenerateCtidh511PrivateKey(rng io.Reader) *Ctidh511PrivateKey {
privKey := &Ctidh511PrivateKey{}
p := gopointer.Save(rng)
C.custom_gen_private(p, &privKey.privateKey)
gopointer.Unref(p)
return privKey
}
// GenerateCtidh511KeyPairWithRNG uses the given RNG to derive a new keypair.
func GenerateCtidh511KeyPairWithRNG(rng io.Reader) (*Ctidh511PrivateKey, *Ctidh511PublicKey) {
privKey := GenerateCtidh511PrivateKey(rng)
return privKey, DeriveCtidh511PublicKey(privKey)
}
func groupActionCtidh511(privateKey *Ctidh511PrivateKey, publicKey *Ctidh511PublicKey) *Ctidh511PublicKey {
sharedKey := new(Ctidh511PublicKey)
ok := C.csidh(&sharedKey.publicKey, &publicKey.publicKey, &privateKey.privateKey)
if !ok {
panic(ErrCTIDH)
}
return sharedKey
}
// DeriveSecret derives a shared secret.
func DeriveSecretCtidh511(privateKey *Ctidh511PrivateKey, publicKey *Ctidh511PublicKey) []byte {
sharedSecret := groupActionCtidh511(privateKey, publicKey)
return sharedSecret.Bytes()
}
// Blind performs a blinding operation returning the blinded public key.
func BlindCtidh511(blindingFactor *Ctidh511PrivateKey, publicKey *Ctidh511PublicKey) (*Ctidh511PublicKey, error) {
return groupActionCtidh511(blindingFactor, publicKey), nil
}
func init() {
if C.BITS != 511 {
panic("CTIDH/cgo: C.BITS must match template Bits")
}
validateBitSize(C.BITS)
Ctidh511PrivateKeySize = C.primes_num
switch C.BITS {
case 511:
Ctidh511PublicKeySize = 64
case 512:
Ctidh511PublicKeySize = 64
case 1024:
Ctidh511PublicKeySize = 128
case 2048:
Ctidh511PublicKeySize = 256
}
}