-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsign.go
276 lines (240 loc) · 6.42 KB
/
sign.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
package sodium
// #cgo pkg-config: libsodium
// #include <stdlib.h>
// #include <sodium.h>
import "C"
var (
cryptoSignBytes = int(C.crypto_sign_bytes())
cryptoSignSeedBytes = int(C.crypto_sign_seedbytes())
cryptoSignPublicKeyBytes = int(C.crypto_sign_publickeybytes())
cryptoSignSecretKeyBytes = int(C.crypto_sign_secretkeybytes())
cryptoSignPrimitive = C.GoString(C.crypto_sign_primitive())
)
type SignKP struct {
PublicKey SignPublicKey
SecretKey SignSecretKey
}
//MakeSignKP generates a keypair for signing
func MakeSignKP() SignKP {
pkb := make([]byte, cryptoSignPublicKeyBytes)
skb := make([]byte, cryptoSignSecretKeyBytes)
if int(C.crypto_sign_keypair(
(*C.uchar)(&pkb[0]),
(*C.uchar)(&skb[0]))) != 0 {
panic("see libsodium")
}
return SignKP{
SignPublicKey{pkb},
SignSecretKey{skb},
}
}
//SeedSignKP generates a keypair for signing from a SignSeed.
//
//The same pair of keys will be generated with the same 'seed'
func SeedSignKP(seed SignSeed) SignKP {
checkTypedSize(&seed, "seed")
pkb := make([]byte, cryptoSignPublicKeyBytes)
skb := make([]byte, cryptoSignSecretKeyBytes)
if int(C.crypto_sign_seed_keypair(
(*C.uchar)(&pkb[0]),
(*C.uchar)(&skb[0]),
(*C.uchar)(&seed.Bytes[0]))) != 0 {
panic("see libsodium")
}
return SignKP{
SignPublicKey{pkb},
SignSecretKey{skb},
}
}
//ToBox converts a signing secret key into a box secret key - ed25519 to curve25519 - returns BoxSecretKey.
func (k SignSecretKey) ToBox() BoxSecretKey {
checkTypedSize(&k, "Sign SecretKey")
skb := make([]byte, cryptoBoxSecretKeyBytes)
C.crypto_sign_ed25519_sk_to_curve25519(
(*C.uchar)(&skb[0]),
(*C.uchar)(&k.Bytes[0]))
return BoxSecretKey{skb}
}
//ToBox converts a signing public key into a box public key - ed25519 to curve25519 - returns BoxPublicKey.
func (k SignPublicKey) ToBox() BoxPublicKey {
checkTypedSize(&k, "Sign PublicKey")
pkb := make([]byte, cryptoBoxPublicKeyBytes)
C.crypto_sign_ed25519_pk_to_curve25519(
(*C.uchar)(&pkb[0]),
(*C.uchar)(&k.Bytes[0]))
return BoxPublicKey{pkb}
}
//ToBox converts a pair of signing key into a pair of box key - ed25519 to curve25519 - returns BoxKP.
func (p SignKP) ToBox() BoxKP {
return BoxKP{
p.PublicKey.ToBox(),
p.SecretKey.ToBox(),
}
}
type SignSeed struct {
Bytes
}
func (k SignSeed) Size() int {
return cryptoSignSeedBytes
}
type SignSecretKey struct {
Bytes
}
func (k SignSecretKey) Size() int {
return cryptoSignSecretKeyBytes
}
//Seed extracts the seed used when generating the key pair.
func (k SignSecretKey) Seed() SignSeed {
checkTypedSize(&k, "Sign SecretKey")
sb := make([]byte, cryptoSignSeedBytes)
C.crypto_sign_ed25519_sk_to_seed(
(*C.uchar)(&sb[0]),
(*C.uchar)(&k.Bytes[0]))
return SignSeed{sb}
}
//PublicKey extracts the SignPublicKey from the SignSecretKey.
func (k SignSecretKey) PublicKey() SignPublicKey {
checkTypedSize(&k, "Sign SecretKey")
pkb := make([]byte, cryptoSignPublicKeyBytes)
C.crypto_sign_ed25519_sk_to_pk(
(*C.uchar)(&pkb[0]),
(*C.uchar)(&k.Bytes[0]))
return SignPublicKey{pkb}
}
type SignPublicKey struct {
Bytes
}
func (k SignPublicKey) Size() int {
return cryptoSignPublicKeyBytes
}
type Signature struct {
Bytes
}
func (b Signature) Size() int {
return cryptoSignBytes
}
//Sign returns 'sm': signature+message
func (b Bytes) Sign(key SignSecretKey) (sm Bytes) {
checkTypedSize(&key, "Sign SecretKey")
bp, bl := plen(b)
sm = make([]byte, bl+cryptoSignBytes)
var smlen C.ulonglong
if int(C.crypto_sign(
(*C.uchar)(&sm[0]),
&smlen,
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&key.Bytes[0]))) != 0 {
panic("see libsodium")
}
sm = sm[:smlen]
return
}
//SignDetached signs the message with 'key' and returns only the signature.
func (b Bytes) SignDetached(key SignSecretKey) (sig Signature) {
checkTypedSize(&key, "Sign SecretKey")
sigb := make([]byte, cryptoSignBytes)
bp, bl := plen(b)
var siglen C.ulonglong
if int(C.crypto_sign_detached(
(*C.uchar)(&sigb[0]),
&siglen,
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&key.Bytes[0]))) != 0 {
panic("see libsodium")
}
sig = Signature{sigb[:siglen]}
return
}
//SignVerifyDetached verifies the message and its detached 'sig' with 'key'.
//
//It returns an error if verification failed.
func (b Bytes) SignVerifyDetached(sig Signature, key SignPublicKey) (err error) {
checkTypedSize(&sig, "Signature")
checkTypedSize(&key, "Sign PublicKey")
bp, bl := plen(b)
if int(C.crypto_sign_verify_detached(
(*C.uchar)(&sig.Bytes[0]),
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&key.Bytes[0]))) != 0 {
err = ErrOpenSign
}
return
}
//SignOpen returns message 'm' from signature+message, verified by 'key'.
//
//It returns an error if verification failed.
func (b Bytes) SignOpen(key SignPublicKey) (m Bytes, err error) {
checkTypedSize(&key, "Sign PublicKey")
bp, bl := plen(b)
m = make([]byte, bl-cryptoSignBytes)
mp, _ := plen(m)
var mlen C.ulonglong
if int(C.crypto_sign_open(
(*C.uchar)(mp),
&mlen,
(*C.uchar)(bp),
(C.ulonglong)(bl),
(*C.uchar)(&key.Bytes[0]))) != 0 {
err = ErrOpenSign
}
m = m[:mlen]
return
}
type SignState struct {
state C.struct_crypto_sign_ed25519ph_state
}
// NewSignState creates an empty state for multi-part messages that can't fit
// in memory.
func NewSignState() *SignState {
s := SignState{}
if int(C.crypto_sign_init(
&s.state)) != 0 {
panic("see libsodium")
}
return &s
}
// Update the state by add more data.
func (s *SignState) Update(b []byte) {
bp, bl := plen(b)
if int(C.crypto_sign_update(
&s.state,
(*C.uchar)(bp),
(C.ulonglong)(bl))) != 0 {
panic("see libsodium")
}
return
}
// Sign a signature for the current state.
//
// The underlying state is freed after this call.
func (s *SignState) Sign(key SignSecretKey) Signature {
checkTypedSize(&key, "Sign SecretKey")
sigb := make([]byte, cryptoSignBytes)
var siglen C.ulonglong
if int(C.crypto_sign_final_create(
&s.state,
(*C.uchar)(&sigb[0]),
&siglen,
(*C.uchar)(&key.Bytes[0]))) != 0 {
panic("see libsodium")
}
s.state = C.struct_crypto_sign_ed25519ph_state{}
return Signature{sigb[:siglen]}
}
// Verify the signature with the current state and public key.
//
// It returns an error if verification failed.
func (s *SignState) Verify(sig Signature, key SignPublicKey) (err error) {
checkTypedSize(&sig, "Signature")
checkTypedSize(&key, "Sign PublicKey")
if int(C.crypto_sign_final_verify(
&s.state,
(*C.uchar)(&sig.Bytes[0]),
(*C.uchar)(&key.Bytes[0]))) != 0 {
err = ErrOpenSign
}
return
}