-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.lisp
347 lines (283 loc) · 8.59 KB
/
crypto.lisp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#|
Copyright 2014-2015 Guillaume LE VAILLANT
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
|#
(in-package peercoin-vote)
;;; Use elliptic curve functions from openssl
(define-foreign-library libcrypto
(t (:default "libcrypto")))
(use-foreign-library libcrypto)
;;; Constants
(defconstant +nid-secp256k1+ 714)
(defconstant +point-conversion-compressed+ 2)
(defconstant +point-conversion-uncompressed+ 4)
;;; Foreign types
(defctype bn-ctx :pointer)
(defctype bn :pointer)
(defctype ec-group :pointer)
(defctype ec-point :pointer)
(defctype ec-key :pointer)
;;; Foreign functions
(defcfun ("BN_CTX_new" bn-ctx-new)
bn-ctx)
(defcfun ("BN_CTX_free" bn-ctx-free)
:void
(ctx bn-ctx))
(defcfun ("BN_CTX_start" bn-ctx-start)
:void
(ctx bn-ctx))
(defcfun ("BN_CTX_end" bn-ctx-end)
:void
(ctx bn-ctx))
(defcfun ("BN_CTX_get" bn-ctx-get)
bn
(ctx bn-ctx))
(defcfun ("BN_new" bn-new)
bn)
(defcfun ("BN_clear_free" bn-clear-free)
:void
(a bn))
(defcfun ("BN_copy" bn-copy)
bn
(to bn)
(from bn))
(defcfun ("BN_mul_word" bn-mul-word)
:int
(a bn)
(w :unsigned-long))
(defcfun ("BN_add" bn-add)
:int
(r bn)
(a bn)
(b bn))
(defcfun ("BN_cmp" bn-cmp)
:int
(a bn)
(b bn))
(defcfun ("BN_set_word" bn-set-word)
:int
(a bn)
(w :unsigned-long))
(defcfun ("BN_mod_sub" bn-mod-sub)
:int
(r bn)
(a bn)
(b bn)
(m bn)
(ctx bn-ctx))
(defcfun ("BN_mod_inverse" bn-mod-inverse)
bn
(r bn)
(a bn)
(n bn)
(ctx bn-ctx))
(defcfun ("BN_mod_mul" bn-mod-mul)
:int
(r bn)
(a bn)
(b bn)
(m bn)
(ctx bn-ctx))
(defcfun ("BN_bin2bn" bn-bin2bn)
bn
(s :pointer :unsigned-char)
(len :int)
(ret bn))
(defcfun ("BN_rshift" bn-rshift)
:int
(r bn)
(a bn)
(n :int))
(defcfun ("EC_POINT_new" ec-point-new)
ec-point
(group ec-group))
(defcfun ("EC_POINT_clear_free" ec-point-clear-free)
:void
(point ec-point))
(defcfun ("EC_POINT_mul" ec-point-mul)
:int
(group ec-group)
(r ec-point)
(n bn)
(q ec-point)
(m bn)
(ctx bn-ctx))
(defcfun ("EC_POINT_set_compressed_coordinates_GFp" ec-point-set-compressed-coordinates-gfp)
:int
(group ec-group)
(p ec-point)
(x bn)
(y_bit :int)
(ctx bn-ctx))
(defcfun ("EC_GROUP_get_order" ec-group-get-order)
:int
(group ec-group)
(order bn)
(ctx bn-ctx))
(defcfun ("EC_GROUP_get_curve_GFp" ec-group-get-curve-gfp)
:int
(group ec-group)
(p bn)
(a bn)
(b bn)
(ctx bn-ctx))
(defcfun ("EC_GROUP_get_degree" ec-group-get-degree)
:int
(group ec-group))
(defcfun ("EC_KEY_new_by_curve_name" ec-key-new-by-curve-name)
ec-key
(nid :int))
(defcfun ("EC_KEY_free" ec-key-free)
:void
(key ec-key))
(defcfun ("EC_KEY_get0_group" ec-key-get0-group)
ec-group
(key ec-key))
(defcfun ("EC_KEY_set_public_key" ec-key-set-public-key)
:int
(key ec-key)
(pub ec-point))
(defcfun ("EC_KEY_set_conv_form" ec-key-set-conv-form)
:void
(key ec-key)
(form :int))
(defcfun ("i2o_ECPublicKey" i2o-ecpublickey)
:int
(key ec-key)
(out :pointer :string))
;;; Hash functions
(defun sha256 (data)
"Compute the sha256 hash of the byte sequence DATA."
(ironclad:digest-sequence :sha256 data))
(defun sha256d (data)
"Compute the double sha256 hash of the byte sequence DATA."
(sha256 (sha256 data)))
(defun ripemd160 (data)
"Compute the ripemd160 hash of the byte sequence DATA."
(ironclad:digest-sequence :ripemd-160 data))
;;; Base58 functions
(defconstant +peercoin-version-byte+ 55)
(defconstant +peercoin-testnet-version-byte+ 111)
(defparameter *testnet* nil)
(defparameter +base58-symbols+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
(defun set-testnet (b)
"If B is NIL, use main net constants. If not, use test net constants."
(setf *testnet* (if b t nil)))
(defun base58-encode (data)
"Make a string containing the base58 encoding of the DATA."
(let ((x 0)
(len (length data))
address)
(dotimes (i len)
(setf (ldb (byte 8 (* 8 i)) x) (aref data (- len 1 i))))
(loop
while (plusp x)
do (multiple-value-bind (q r) (floor x 58)
(setf x q)
(push (elt +base58-symbols+ r) address)))
(loop
for i from 0 below len
while (zerop (aref data i))
do (push (elt +base58-symbols+ 0) address))
(coerce address 'string)))
(defun pretty-print-address (hash)
"Make a string containing the base58 encoding of an address given its HASH."
(when hash
(let ((script (vector (if *testnet*
+peercoin-testnet-version-byte+
+peercoin-version-byte+))))
(setf script (concatenate '(vector (unsigned-byte 8)) script hash))
(setf script (concatenate 'vector script (subseq (sha256d script) 0 4)))
(base58-encode script))))
;;; Main functions
(defun pubkey-to-address (pubkey)
"Compute the Peercoin address associated with the ecdsa public key PUBKEY."
(pretty-print-address (ripemd160 (sha256 pubkey))))
(defun signature-recover-key (eckey sig-r sig-s msg recid)
"Recover an ecdsa public key from a compact signature."
(let (group ctx x e order sor eor field r q rr zero n i pubkey pmsg)
(setf n 0)
(setf i (floor recid 2))
(setf group (ec-key-get0-group eckey))
(setf ctx (bn-ctx-new))
(bn-ctx-start ctx)
(setf order (bn-ctx-get ctx))
(ec-group-get-order group order ctx)
(setf x (bn-ctx-get ctx))
(bn-copy x order)
(bn-mul-word x i)
(bn-add x x sig-r)
(setf field (bn-ctx-get ctx))
(ec-group-get-curve-gfp group field (null-pointer) (null-pointer) ctx)
(when (>= (bn-cmp x field) 0)
(return-from signature-recover-key nil))
(setf r (ec-point-new group))
(ec-point-set-compressed-coordinates-gfp group r x (mod recid 2) ctx)
(setf q (ec-point-new group))
(setf n (ec-group-get-degree group))
(setf e (bn-ctx-get ctx))
(setf pmsg (foreign-alloc :unsigned-char :count (length msg) :initial-contents msg))
(bn-bin2bn pmsg (length msg) e)
(foreign-free pmsg)
(when (> (* 8 (length msg)) n)
(bn-rshift e e (- 8 (logand n 7))))
(setf zero (bn-ctx-get ctx))
(bn-set-word zero 0)
(bn-mod-sub e zero e order ctx)
(setf rr (bn-ctx-get ctx))
(bn-mod-inverse rr sig-r order ctx)
(setf sor (bn-ctx-get ctx))
(bn-mod-mul sor sig-s rr order ctx)
(setf eor (bn-ctx-get ctx))
(bn-mod-mul eor e rr order ctx)
(ec-point-mul group q eor r sor ctx)
(ec-key-set-public-key eckey q)
(let* ((len (i2o-ecpublickey eckey (null-pointer)))
(ppubkey (foreign-alloc :unsigned-char :count len))
(pppubkey (foreign-alloc :pointer :initial-element ppubkey)))
(i2o-ecpublickey eckey pppubkey)
(setf pubkey (loop
for i from 0 below len
collect (mem-aref ppubkey :unsigned-char i)))
(setf pubkey (coerce pubkey '(vector (unsigned-byte 8))))
(foreign-free pppubkey)
(foreign-free ppubkey))
(ec-point-clear-free r)
(ec-point-clear-free q)
(bn-ctx-end ctx)
(bn-ctx-free ctx)
pubkey))
(defun verify-compact-signature (hash signature address)
"Check whether a compact ecdsa SIGNATURE was made on HASH by ADDRESS."
(let (psig sig-r sig-s eckey nv pubkey ok)
(when (/= (length signature) 65)
(return-from verify-compact-signature ok))
(setf nv (elt signature 0))
(when (or (< nv 27) (>= nv 35))
(return-from verify-compact-signature ok))
(setf psig (foreign-alloc :unsigned-char :count (length signature) :initial-contents signature))
(setf sig-r (bn-new))
(setf sig-s (bn-new))
(bn-bin2bn (inc-pointer psig 1) 32 sig-r)
(bn-bin2bn (inc-pointer psig 33) 32 sig-s)
(foreign-free psig)
(setf eckey (ec-key-new-by-curve-name +nid-secp256k1+))
(when (>= nv 31)
(ec-key-set-conv-form eckey +point-conversion-compressed+)
(decf nv 4))
(setf pubkey (signature-recover-key eckey sig-r sig-s hash (- nv 27)))
(bn-clear-free sig-r)
(bn-clear-free sig-s)
(ec-key-free eckey)
(when (and pubkey (equal (pubkey-to-address pubkey) address))
(setf ok t))
ok))