This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
curve.go
154 lines (125 loc) · 4.22 KB
/
curve.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
// Package curve is a thin wrapper around the libsignal curve25519
// implementation. It aims to be the Go equivalent of
// https://github.com/signalapp/curve25519-java
package curve // import "github.com/Tolsi/vrf-lottery/curve"
import "crypto/rand"
import "fmt"
// #cgo CFLAGS: -g -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -O3
// #include <stdint.h>
// #include "curve25519-donna.h"
// #include "curve_sigs.h"
// #include "gen_x.h"
import "C"
const djbKeyLen = 0x20
const curveSignatureLen = 0x40
const vrfSignatureLen = 0x60
const vrfVerifyLen = 0x20
type u8p = *C.uchar
type PrivateKey []byte
type PublicKey []byte
type CError struct {
Fn string
Code int
}
func (err CError) Error() string {
return fmt.Sprintf("C function %s failed with code %d", err.Fn, err.Code)
}
func newCError(fn string, code C.int) error {
if code >= 0 {
return nil
} else {
return CError{fn, int(code)}
}
}
func GeneratePrivateKey() (key PrivateKey, err error) {
key = make([]byte, djbKeyLen)
_, err = rand.Read(key)
if err != nil {
return
}
key[0] &= 0xf8
key[0x1f] &= 0x7f
key[0x1f] |= 0x40
return
}
func (privateKey PrivateKey) GeneratePublicKey() (publicKey PublicKey, err error) {
if len(privateKey) != djbKeyLen {
return nil, fmt.Errorf("Bad key length: %d", len(privateKey))
}
publicKey = make([]byte, djbKeyLen)
basepoint := [djbKeyLen]byte{9}
result := C.curve25519_donna(u8p(&publicKey[0]),
u8p(&privateKey[0]), u8p(&basepoint[0]))
return publicKey, newCError("curve25519_donna", result)
}
func GenerateKeypair() (privKey PrivateKey, pubKey PublicKey, err error) {
privKey, err = GeneratePrivateKey()
if err != nil {
return
}
pubKey, err = privKey.GeneratePublicKey()
if err != nil {
return
}
return
}
func (privateKey PrivateKey) CalculateAgreement(publicKey PublicKey) (sharedKey []byte, err error) {
if len(publicKey) != djbKeyLen {
return nil, fmt.Errorf("Bad key length: %d", len(publicKey))
}
if len(privateKey) != djbKeyLen {
return nil, fmt.Errorf("Bad key length: %d", len(privateKey))
}
sharedKey = make([]byte, djbKeyLen)
result := C.curve25519_donna(u8p(&sharedKey[0]),
u8p(&privateKey[0]), u8p(&publicKey[0]))
return sharedKey, newCError("curve25519_donna", result)
}
func (key PrivateKey) CalculateSignature(message []byte) (signature []byte, err error) {
if len(key) != djbKeyLen {
return nil, fmt.Errorf("Bad key length: %d", len(key))
}
var random_data [curveSignatureLen]byte
_, err = rand.Read(random_data[:])
if err != nil {
return
}
signature = make([]byte, curveSignatureLen)
result := C.curve25519_sign(u8p(&signature[0]), u8p(&key[0]),
u8p(&message[0]), C.size_t(len(message)), u8p(&random_data[0]))
return signature, newCError("curve25519_sign", result)
}
func (key PublicKey) VerifySignature(message []byte, signature []byte) (good bool, err error) {
if len(key) != djbKeyLen {
return false, fmt.Errorf("Bad key length: %d", len(key))
}
if len(signature) != curveSignatureLen {
return false, fmt.Errorf("Bad signature length: %d", len(signature))
}
result := C.curve25519_verify(u8p(&signature[0]), u8p(&key[0]),
u8p(&message[0]), C.size_t(len(message)))
return result == 0, newCError("curve25519_verify", result)
}
func (key PrivateKey) CalculateVrfSignature(message []byte, random_data []byte) (signature []byte, err error) {
if len(key) != djbKeyLen {
return nil, fmt.Errorf("Bad key length: %d", len(key))
}
signature = make([]byte, vrfSignatureLen)
result := C.generalized_xveddsa_25519_sign(u8p(&signature[0]),
u8p(&key[0]), u8p(&message[0]), C.size_t(len(message)),
u8p(&random_data[0]), nil, C.ulong(0))
return signature, newCError("generalized_xveddsa_25519_sign", result)
}
func (key PublicKey) VerifyVrfSignature(message []byte, signature []byte) (vrfOutput []byte, err error) {
if len(key) != djbKeyLen {
return nil, fmt.Errorf("Bad key length: %d", len(key))
}
if len(signature) != vrfSignatureLen {
return nil, fmt.Errorf("Bad signature length: %d", len(signature))
}
vrfOutput = make([]byte, vrfVerifyLen)
result := C.generalized_xveddsa_25519_verify(u8p(&vrfOutput[0]),
u8p(&signature[0]), u8p(&key[0]), u8p(&message[0]),
C.size_t(len(message)), nil, C.ulong(0))
return vrfOutput, newCError("generalized_xveddsa_25519_verify", result)
}