-
Notifications
You must be signed in to change notification settings - Fork 2
/
encodings.cljc
333 lines (289 loc) · 12.6 KB
/
encodings.cljc
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
(ns fluree.crypto.encodings
(:require [clojure.string :as str]
#?@(:cljs [[fluree.crypto.asn1 :as asn1]
["@fluree/sjcl" :as sjcl]
[fluree.crypto.bn :as bn]])
[alphabase.core :as alphabase])
#?(:clj
(:import (org.bouncycastle.asn1 ASN1Integer
ASN1InputStream
ASN1Sequence
DERSequenceGenerator)
(org.bouncycastle.crypto.params ECDomainParameters)
(org.bouncycastle.math.ec ECPoint)
(java.io ByteArrayOutputStream))))
#?(:clj (set! *warn-on-reflection* true))
(defn ^:export pad-hex
"Pads a hex value with a leading zero if odd."
[hex]
(if (odd? (count hex))
(str "0" hex)
hex))
(defn biginteger->hex
"Hex-encode java.math.BigInteger (clj) or sjcl.bn (cljs)."
[bn]
#?(:clj (-> ^BigInteger bn (.toString 16) pad-hex)
:cljs (-> bn .toString (.replace #"^0x" "") pad-hex)))
(defn biginteger->bytes
"Return bytes of java.math.BigInteger (clj) or sjcl.bn (cljs)."
([bn] (biginteger->bytes bn nil))
([bn l]
#?(:clj (-> ^BigInteger bn .toByteArray)
:cljs (-> bn (.toBits l) sjcl/codec.bytes.fromBits))))
(defn bytes->biginteger
"Return bytes of java.math.BigInteger (clj) or sjcl.bn (cljs)."
[^bytes ba]
#?(:clj (BigInteger. ba)
:cljs (-> ba sjcl/codec.bytes.toBits (sjcl/bn.))))
(defn hex->biginteger
"Return bytes of java.math.BigInteger (clj) or sjcl.bn (cljs)."
[^String hex]
#?(:clj (BigInteger. hex 16)
:cljs (.initWith (sjcl/bn.) hex)))
(defn byte->int [the-bytes]
(let [the-bytes (bytes the-bytes)]
(-> (aget the-bytes 0)
int)))
#?(:cljs
(defn bn-even?
"Tests is an sjcl.bn (cljs) is even. Returns boolean if so."
[sjcl-bn]
(-> sjcl-bn
.-limbs ;; .limbs holds array of numbers
(get 0) ;; first array number is lowest bits
(bit-and 1)
(zero?))))
;; adapted from https://github.com/Sepia-Officinalis/secp256k1
#?(:cljs
(defn modular-square-root
"Compute the square root of a number modulo a prime.
Number and modulus should be big numbers (bn)."
[n modulus]
(let [n (.mod n modulus)
mod8 (-> modulus (.mod 8) .toString js/parseInt)]
(assert (bn/>= modulus 0), "Modulus must be non-negative")
(cond
(.equals n 0) n
(.equals n 1) n
;; http://www.mersennewiki.org/index.php/Modular_Square_Root#Modulus_equal_to_2
(.equals modulus 2)
(.mod n modulus)
;; http://www.mersennewiki.org/index.php/Modular_Square_Root#Modulus_congruent_to_3_modulo_4
(or (= mod8 3) (= mod8 7))
(let [m (-> modulus (.add 1) .normalize .halveM .halveM)]
(.powermod n m modulus))
;; http://www.mersennewiki.org/index.php/Modular_Square_Root#Modulus_congruent_to_5_modulo_8
(= mod8 5)
(let [m (-> modulus (.sub 5) .normalize .halveM .halveM .halveM)
v (.powermod (.add n n) m modulus)
i (-> (.multiply v v) (.multiply n) (.multiply 2) (.sub 1) (.mod modulus))]
(-> n (.multiply v) (.multiply i) (.mod modulus)))
;; http://www.mersennewiki.org/index.php/Modular_Square_Root#Modulus_congruent_to_1_modulo_8
(= mod8 1)
(let [q (-> modulus (.sub 1) .normalize)
e (->> q
(iterate #(.halveM %))
(take-while even?)
count)
two (sjcl/bn. 2)
z (->> (range) rest rest
(map #(sjcl/bn. %))
(map #(.powermod % q modulus))
(filter
#(not
(.equals
(.powermod % (.pow two (- e 1)) modulus)
1)))
first)
x (.powermod n (-> q (.sub 1) .normalize .halveM) modulus)]
(loop [y z,
r e,
v (-> n (.multiply x) (.mod modulus)),
w (-> n (.multiply x) (.multiply x) (.mod modulus))]
(if (.equals w 1)
v
(let [k (->> (range)
(map #(vector
%
(.powermod w (.pow two %) modulus)))
(filter #(.equals (second %) 1))
first first)
d (.powermod y (.pow two (- r k 1)) modulus)
y (.mod (.multiply d d) modulus)
v (.mod (.multiply d v) modulus)
w (.mod (.multiply w y) modulus)]
(recur y k v w)))))
:else
(throw (ex-info "Cannot compute a square root for a non-prime modulus"
{:argument n,
:modulus modulus}))))))
(defn pad-to-length
"Left-pads string s to length len with zeroes."
[s len]
(let [pad-len (- len (count s))]
(if (pos? pad-len)
(str/join (concat (repeat pad-len \0) s))
s)))
(defn compute-point
"Compute an elliptic curve point for a y-coordinate parity and x-coordinate"
[y-even? x-coordinate ^ECDomainParameters curve]
#?(:clj
(let [l (-> curve .getN .bitLength (/ 8))
raw (->> x-coordinate
biginteger
.toByteArray)
input (cond (= l (count raw)) raw
(< l (count raw)) (drop-while zero? raw)
(> l (count raw)) (let [out (byte-array l)]
(System/arraycopy
raw 0
out (- l (count raw))
(count raw))
out))]
(-> (cons (if y-even? 0x02 0x03) input)
byte-array
alphabase/bytes->hex
(pad-to-length 64)))
:cljs
(let [modulus (-> curve .-field .-modulus)
; √(x * (a + x**2) + b) % p
y-candidate (modular-square-root
(.add
(.mul x-coordinate (.add (.-a curve) (.square x-coordinate)))
(.-b curve))
modulus)
y (if (= y-even? (bn-even? y-candidate))
y-candidate
(.sub modulus y-candidate))]
#js {:x (sjcl/bn. x-coordinate)
:y (sjcl/bn. y)})))
;; X92.61 encode / decode
(defn- x962-hex-compressed-decode
[encoded-key ^ECDomainParameters curve]
#?(:cljs
(let [x (-> (subs encoded-key 2) hex->biginteger)
y-even? (= (subs encoded-key 0 2) "02")]
(compute-point y-even? x curve))
:clj (let [point (.decodePoint (.getCurve curve)
(alphabase/base-to-byte-array
encoded-key :hex))
x (-> point .getXCoord .toBigInteger)
y (-> point .getYCoord .toBigInteger)]
(-> curve
.getCurve
(.createPoint x y)
.normalize))))
(defn- x962-hex-uncompressed-decode
"Decode a hex encoded public key into x and y coordinates as bytes."
[encoded-key ^ECDomainParameters curve]
(let [size (- (count encoded-key) 2) ;; first hex byte is 0x04, rest is x and y coords
x (subs encoded-key 2 (+ 2 size))
y (subs encoded-key (+ 2 size))]
#?(:clj (-> curve .getCurve (.createPoint x y) .normalize)
:cljs #js {:x (.initWith (sjcl/bn.) x)
:y (.initWith (sjcl/bn.) y)})))
(defn x962-decode
"Decode a X9.62 encoded public key from hex"
^ECPoint
[public-key curve]
(when-not (#{"02" "03" "04"} (subs public-key 0 2))
(throw
(ex-info
"X9.62 encoded public key must have a first byte of 0x02, 0x03 or 0x04."
{:public-key public-key})))
(cond
(#{"02" "03"} (subs public-key 0 2))
(x962-hex-compressed-decode public-key curve)
(= "04" (subs public-key 0 2))
(x962-hex-uncompressed-decode public-key curve)
:else
(throw (ex-info "Invalid encoding on public key"
{:encoded-key public-key}))))
(defn x962-encode
"Encodes x and y coords in hex to X9.62 with optional compression (default true).
x coords and y coords should be supplied in hex format."
([x-coord y-coord] (x962-encode x-coord y-coord true))
([^String x-coord ^String y-coord compressed?]
(if-not compressed?
(str "04" (pad-hex x-coord) (pad-hex y-coord))
(let [y-even? #?(:clj (let [y-bi (BigInteger. y-coord 16)]
(even? y-bi))
:cljs (-> (sjcl/bn.)
(.initWith y-coord)
(bn-even?)))]
(if y-even?
(str "02" (pad-to-length (pad-hex x-coord) 64))
(str "03" (pad-to-length (pad-hex x-coord) 64)))))))
;; DER encode / decode
(defn- DER-decode-standard
"Decodes an ordinary encoded list of numbers from a hexadecimal following the distinguished encoding rules.
Returns R and S as bigintegers (clj). "
[asn1]
(assert (= "30" (subs asn1 0 2)), "Input must start with the code 30")
#?(:clj (let [^bytes signature (alphabase/base-to-byte-array asn1 :hex)]
(with-open [decoder (ASN1InputStream. signature)]
(let [^ASN1Sequence sequence (.readObject decoder)]
[(-> sequence ^ASN1Integer (.getObjectAt 0) .getValue)
(-> sequence ^ASN1Integer (.getObjectAt 1) .getValue)])))
:cljs (let [{:keys [length remaining]} (asn1/decode-asn1-length (subs asn1 2))]
(when-not (= (* length 2) (count remaining))
(throw (ex-info "Decoded header length does not match actual length of message"
{:decoded-header-length (* 2 length)
:actual-length (count remaining)
:message remaining
:full-asn1 asn1})))
(loop [ret [], remaining remaining]
(if (empty? remaining)
(mapv hex->biginteger ret)
(let [{:keys [integer remaining]} (asn1/decode-asn1-integer remaining)]
(recur (conj ret integer) remaining)))))))
(defn DER-decode
"Decodes a list of numbers including an optional recovery byte, following BitCoin's convention"
[asn1]
(let [asn1 (str/lower-case asn1)
first-byte (subs asn1 0 2)]
(cond
(#{"1b" "1c" "1d" "1e"} first-byte) ;; recovery bytes
(-> (conj (DER-decode-standard (subs asn1 2))
(-> (alphabase/hex->bytes first-byte)
byte->int)))
(= "30" first-byte)
(DER-decode-standard asn1)
:else
(throw (ex-info "Input must start with the code 30, or start with a recovery code (either 1b, 1c, 1d, or 1e)"
{:argument asn1})))))
(defn DER-decode-ECDSA-signature
"Formats an ECDSA signature from hex.
Returns R, S and recover as hex values."
[ecdsa]
(let [[R S recover] (DER-decode ecdsa)]
{:R R
:S S
:recover recover}))
;; TODO - should be able to take biginteger/bignumber, convert to bytes, then
;; TODO - use a common clojure/script ASN1 capability (started in fluree.crypto.asn1.cljs)
(defn DER-encode-ECDSA-signature
"Create a DER encoded signature.
Both R and S should be bigintegers (clj) /bignumbers (cljs).
recover should also be biginteger"
[^BigInteger R ^BigInteger S recover curve]
#?(:cljs (let [l (-> curve .-r .bitLength)
R-hex (-> R (.toBits l) sjcl/codec.hex.fromBits)
S-hex (-> S (.toBits l) sjcl/codec.hex.fromBits)
recover-hex (.toString recover 16)
R-asn1 (asn1/encode-asn1-unsigned-integer-hex R-hex)
S-asn1 (asn1/encode-asn1-unsigned-integer-hex S-hex)]
(->> (str R-asn1 S-asn1)
(asn1/encode-asn1-unsigned-integer-hex)
(#(subs % 2))
(str recover-hex "30")
alphabase/hex->bytes))
:clj (let [bos (ByteArrayOutputStream.)]
(with-open [der-gen (DERSequenceGenerator. bos)]
(doto der-gen
(.addObject (ASN1Integer. R))
(.addObject (ASN1Integer. S))))
(let [result (.toByteArray bos)]
(if (nil? recover)
result
(byte-array (cons recover result)))))))