-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctidh1024.go
297 lines (252 loc) · 8.78 KB
/
ctidh1024.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 Ctidh1024
// +build Ctidh1024
// DO NOT EDIT: generated code, see gen/main.go
package ctidh
/*
#include "binding1024.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 (
// Ctidh1024PublicKeySize is the size in bytes of the public key.
Ctidh1024PublicKeySize int
// Ctidh1024PrivateKeySize is the size in bytes of the private key.
Ctidh1024PrivateKeySize int
)
// Ctidh1024PublicKey is a public CTIDH key.
type Ctidh1024PublicKey struct {
publicKey C.public_key
}
// NewEmptyCtidh1024PublicKey returns an uninitialized
// Ctidh1024PublicKey which is suitable to be loaded
// via some serialization format via FromBytes
// or FromPEMFile methods.
func NewEmptyCtidh1024PublicKey() *Ctidh1024PublicKey {
return new(Ctidh1024PublicKey)
}
// NewCtidh1024PublicKey creates a new public key from
// the given key material or panics if the
// key data is not Ctidh1024PublicKeySize.
func NewCtidh1024PublicKey(key []byte) *Ctidh1024PublicKey {
k := new(Ctidh1024PublicKey)
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 *Ctidh1024PublicKey) String() string {
return "Ctidh1024_PublicKey"
}
// Reset resets the Ctidh1024PublicKey to all zeros.
func (p *Ctidh1024PublicKey) Reset() {
zeros := make([]byte, Ctidh1024PublicKeySize)
err := p.FromBytes(zeros)
if err != nil {
panic(err)
}
}
// Bytes returns the Ctidh1024PublicKey as a byte slice.
func (p *Ctidh1024PublicKey) Bytes() []byte {
return C.GoBytes(unsafe.Pointer(&p.publicKey.A.x.c), C.int(C.UINTBIG_LIMBS*8))
}
// FromBytes loads a Ctidh1024PublicKey from the given byte slice.
func (p *Ctidh1024PublicKey) FromBytes(data []byte) error {
if len(data) != Ctidh1024PublicKeySize {
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 *Ctidh1024PublicKey) Equal(publicKey *Ctidh1024PublicKey) 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 *Ctidh1024PublicKey) Blind(blindingFactor *Ctidh1024PrivateKey) error {
blinded, err := BlindCtidh1024(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 *Ctidh1024PublicKey) 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 *Ctidh1024PublicKey) 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 *Ctidh1024PublicKey) 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 *Ctidh1024PublicKey) UnmarshalText(data []byte) error {
raw, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}
return p.FromBytes(raw)
}
// Ctidh1024PrivateKey is a private CTIDH key.
type Ctidh1024PrivateKey struct {
privateKey C.private_key
}
// NewEmptyCtidh1024PrivateKey returns an uninitialized
// Ctidh1024PrivateKey which is suitable to be loaded
// via some serialization format via FromBytes
// or FromPEMFile methods.
func NewEmptyCtidh1024PrivateKey() *Ctidh1024PrivateKey {
return new(Ctidh1024PrivateKey)
}
// DeriveSecret derives a shared secret.
func (p *Ctidh1024PrivateKey) DeriveSecret(publicKey *Ctidh1024PublicKey) []byte {
return DeriveSecretCtidh1024(p, publicKey)
}
// String returns a string identifying
// this type as a CTIDH private key.
func (p *Ctidh1024PrivateKey) String() string {
return "Ctidh1024_PrivateKey"
}
// Reset resets the Ctidh1024PrivateKey to all zeros.
func (p *Ctidh1024PrivateKey) Reset() {
zeros := make([]byte, Ctidh1024PrivateKeySize)
err := p.FromBytes(zeros)
if err != nil {
panic(err)
}
}
// Bytes serializes Ctidh1024PrivateKey into a byte slice.
func (p *Ctidh1024PrivateKey) Bytes() []byte {
return C.GoBytes(unsafe.Pointer(&p.privateKey), C.primes_num)
}
// FromBytes loads a Ctidh1024PrivateKey from the given byte slice.
func (p *Ctidh1024PrivateKey) FromBytes(data []byte) error {
if len(data) != Ctidh1024PrivateKeySize {
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 *Ctidh1024PrivateKey) Equal(privateKey *Ctidh1024PrivateKey) bool {
return hmac.Equal(p.Bytes(), privateKey.Bytes())
}
// Public returns the public key associated
// with the given private key.
func (p *Ctidh1024PrivateKey) Public() *Ctidh1024PublicKey {
return DeriveCtidh1024PublicKey(p)
}
// MarshalBinary is an implementation of a method on the
// BinaryMarshaler interface defined in https://golang.org/pkg/encoding/
func (p *Ctidh1024PrivateKey) 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 *Ctidh1024PrivateKey) 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 *Ctidh1024PrivateKey) 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 *Ctidh1024PrivateKey) UnmarshalText(data []byte) error {
raw, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}
return p.FromBytes(raw)
}
// DeriveCtidh1024PublicKey derives a public key given a private key.
func DeriveCtidh1024PublicKey(privKey *Ctidh1024PrivateKey) *Ctidh1024PublicKey {
var base C.public_key
baseKey := new(Ctidh1024PublicKey)
baseKey.publicKey = base
return groupActionCtidh1024(privKey, baseKey)
}
// GenerateCtidh1024KeyPair generates a new Ctidh1024 private and then
// attempts to compute the Ctidh1024 public key.
func GenerateCtidh1024KeyPair() (*Ctidh1024PrivateKey, *Ctidh1024PublicKey) {
privKey := new(Ctidh1024PrivateKey)
C.csidh_private(&privKey.privateKey)
return privKey, DeriveCtidh1024PublicKey(privKey)
}
// GenerateCtidh1024PrivateKey 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 GenerateCtidh1024PrivateKey(rng io.Reader) *Ctidh1024PrivateKey {
privKey := &Ctidh1024PrivateKey{}
p := gopointer.Save(rng)
C.custom_gen_private(p, &privKey.privateKey)
gopointer.Unref(p)
return privKey
}
// GenerateCtidh1024KeyPairWithRNG uses the given RNG to derive a new keypair.
func GenerateCtidh1024KeyPairWithRNG(rng io.Reader) (*Ctidh1024PrivateKey, *Ctidh1024PublicKey) {
privKey := GenerateCtidh1024PrivateKey(rng)
return privKey, DeriveCtidh1024PublicKey(privKey)
}
func groupActionCtidh1024(privateKey *Ctidh1024PrivateKey, publicKey *Ctidh1024PublicKey) *Ctidh1024PublicKey {
sharedKey := new(Ctidh1024PublicKey)
ok := C.csidh(&sharedKey.publicKey, &publicKey.publicKey, &privateKey.privateKey)
if !ok {
panic(ErrCTIDH)
}
return sharedKey
}
// DeriveSecret derives a shared secret.
func DeriveSecretCtidh1024(privateKey *Ctidh1024PrivateKey, publicKey *Ctidh1024PublicKey) []byte {
sharedSecret := groupActionCtidh1024(privateKey, publicKey)
return sharedSecret.Bytes()
}
// Blind performs a blinding operation returning the blinded public key.
func BlindCtidh1024(blindingFactor *Ctidh1024PrivateKey, publicKey *Ctidh1024PublicKey) (*Ctidh1024PublicKey, error) {
return groupActionCtidh1024(blindingFactor, publicKey), nil
}
func init() {
if C.BITS != 1024 {
panic("CTIDH/cgo: C.BITS must match template Bits")
}
validateBitSize(C.BITS)
Ctidh1024PrivateKeySize = C.primes_num
switch C.BITS {
case 511:
Ctidh1024PublicKeySize = 64
case 512:
Ctidh1024PublicKeySize = 64
case 1024:
Ctidh1024PublicKeySize = 128
case 2048:
Ctidh1024PublicKeySize = 256
}
}