]>
Commit | Line | Data |
---|---|---|
3093576a | 1 | #include <stdlib.h> |
47b9e78e | 2 | #include <stdint.h> |
3093576a | 3 | #include <string.h> |
b5efbe58 MC |
4 | #include "org_bitcoin_NativeSecp256k1.h" |
5 | #include "include/secp256k1.h" | |
86e2d07e | 6 | #include "include/secp256k1_ecdh.h" |
3093576a | 7 | #include "include/secp256k1_recovery.h" |
b5efbe58 | 8 | |
3093576a G |
9 | |
10 | SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ctx_1clone | |
11 | (JNIEnv* env, jclass classObject, jlong ctx_l) | |
12 | { | |
47b9e78e | 13 | const secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a | 14 | |
47b9e78e | 15 | jlong ctx_clone_l = (uintptr_t) secp256k1_context_clone(ctx); |
3093576a G |
16 | |
17 | (void)classObject;(void)env; | |
18 | ||
47b9e78e | 19 | return ctx_clone_l; |
3093576a G |
20 | |
21 | } | |
22 | ||
23 | SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1context_1randomize | |
24 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) | |
25 | { | |
47b9e78e | 26 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
27 | |
28 | const unsigned char* seed = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); | |
29 | ||
30 | (void)classObject; | |
31 | ||
32 | return secp256k1_context_randomize(ctx, seed); | |
33 | ||
34 | } | |
35 | ||
36 | SECP256K1_API void JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1destroy_1context | |
37 | (JNIEnv* env, jclass classObject, jlong ctx_l) | |
38 | { | |
47b9e78e | 39 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
40 | |
41 | secp256k1_context_destroy(ctx); | |
42 | ||
43 | (void)classObject;(void)env; | |
44 | } | |
45 | ||
46 | SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1verify | |
47 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint siglen, jint publen) | |
48 | { | |
47b9e78e | 49 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a | 50 | |
3093576a G |
51 | unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); |
52 | const unsigned char* sigdata = { (unsigned char*) (data + 32) }; | |
53 | const unsigned char* pubdata = { (unsigned char*) (data + siglen + 32) }; | |
54 | ||
55 | secp256k1_ecdsa_signature sig; | |
56 | secp256k1_pubkey pubkey; | |
57 | ||
58 | int ret = secp256k1_ecdsa_signature_parse_der(ctx, &sig, sigdata, siglen); | |
59 | ||
60 | if( ret ) { | |
61 | ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pubdata, publen); | |
a40c701c JG |
62 | |
63 | if( ret ) { | |
64 | ret = secp256k1_ecdsa_verify(ctx, &sig, data, &pubkey); | |
65 | } | |
3093576a G |
66 | } |
67 | ||
3093576a G |
68 | (void)classObject; |
69 | ||
a40c701c | 70 | return ret; |
3093576a G |
71 | } |
72 | ||
73 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1sign | |
74 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) | |
75 | { | |
47b9e78e | 76 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
77 | unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); |
78 | unsigned char* secKey = (unsigned char*) (data + 32); | |
79 | ||
80 | jobjectArray retArray; | |
81 | jbyteArray sigArray, intsByteArray; | |
82 | unsigned char intsarray[2]; | |
83 | ||
74e2dbd6 | 84 | secp256k1_ecdsa_signature sig; |
3093576a | 85 | |
74e2dbd6 | 86 | int ret = secp256k1_ecdsa_sign(ctx, &sig, data, secKey, NULL, NULL); |
3093576a G |
87 | |
88 | unsigned char outputSer[72]; | |
89 | size_t outputLen = 72; | |
90 | ||
91 | if( ret ) { | |
74e2dbd6 | 92 | int ret2 = secp256k1_ecdsa_signature_serialize_der(ctx,outputSer, &outputLen, &sig ); (void)ret2; |
3093576a G |
93 | } |
94 | ||
95 | intsarray[0] = outputLen; | |
96 | intsarray[1] = ret; | |
97 | ||
98 | retArray = (*env)->NewObjectArray(env, 2, | |
99 | (*env)->FindClass(env, "[B"), | |
100 | (*env)->NewByteArray(env, 1)); | |
101 | ||
102 | sigArray = (*env)->NewByteArray(env, outputLen); | |
103 | (*env)->SetByteArrayRegion(env, sigArray, 0, outputLen, (jbyte*)outputSer); | |
104 | (*env)->SetObjectArrayElement(env, retArray, 0, sigArray); | |
105 | ||
106 | intsByteArray = (*env)->NewByteArray(env, 2); | |
107 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); | |
108 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
109 | ||
110 | (void)classObject; | |
111 | ||
112 | return retArray; | |
113 | } | |
114 | ||
115 | SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1seckey_1verify | |
116 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) | |
117 | { | |
47b9e78e | 118 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
119 | unsigned char* secKey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); |
120 | ||
121 | (void)classObject; | |
122 | ||
123 | return secp256k1_ec_seckey_verify(ctx, secKey); | |
124 | } | |
125 | ||
126 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1create | |
127 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) | |
128 | { | |
47b9e78e | 129 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
130 | const unsigned char* secKey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); |
131 | ||
132 | secp256k1_pubkey pubkey; | |
133 | ||
134 | jobjectArray retArray; | |
135 | jbyteArray pubkeyArray, intsByteArray; | |
136 | unsigned char intsarray[2]; | |
137 | ||
138 | int ret = secp256k1_ec_pubkey_create(ctx, &pubkey, secKey); | |
139 | ||
140 | unsigned char outputSer[65]; | |
141 | size_t outputLen = 65; | |
142 | ||
143 | if( ret ) { | |
144 | int ret2 = secp256k1_ec_pubkey_serialize(ctx,outputSer, &outputLen, &pubkey,SECP256K1_EC_UNCOMPRESSED );(void)ret2; | |
145 | } | |
146 | ||
147 | intsarray[0] = outputLen; | |
148 | intsarray[1] = ret; | |
149 | ||
150 | retArray = (*env)->NewObjectArray(env, 2, | |
151 | (*env)->FindClass(env, "[B"), | |
152 | (*env)->NewByteArray(env, 1)); | |
153 | ||
154 | pubkeyArray = (*env)->NewByteArray(env, outputLen); | |
155 | (*env)->SetByteArrayRegion(env, pubkeyArray, 0, outputLen, (jbyte*)outputSer); | |
156 | (*env)->SetObjectArrayElement(env, retArray, 0, pubkeyArray); | |
157 | ||
158 | intsByteArray = (*env)->NewByteArray(env, 2); | |
159 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); | |
160 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
161 | ||
162 | (void)classObject; | |
163 | ||
164 | return retArray; | |
165 | ||
166 | } | |
167 | ||
3093576a G |
168 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1add |
169 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) | |
170 | { | |
47b9e78e | 171 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
172 | unsigned char* privkey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); |
173 | const unsigned char* tweak = (unsigned char*) (privkey + 32); | |
174 | ||
175 | jobjectArray retArray; | |
176 | jbyteArray privArray, intsByteArray; | |
177 | unsigned char intsarray[2]; | |
178 | ||
179 | int privkeylen = 32; | |
180 | ||
181 | int ret = secp256k1_ec_privkey_tweak_add(ctx, privkey, tweak); | |
182 | ||
183 | intsarray[0] = privkeylen; | |
184 | intsarray[1] = ret; | |
185 | ||
186 | retArray = (*env)->NewObjectArray(env, 2, | |
187 | (*env)->FindClass(env, "[B"), | |
188 | (*env)->NewByteArray(env, 1)); | |
189 | ||
190 | privArray = (*env)->NewByteArray(env, privkeylen); | |
191 | (*env)->SetByteArrayRegion(env, privArray, 0, privkeylen, (jbyte*)privkey); | |
192 | (*env)->SetObjectArrayElement(env, retArray, 0, privArray); | |
193 | ||
194 | intsByteArray = (*env)->NewByteArray(env, 2); | |
195 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); | |
196 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
197 | ||
198 | (void)classObject; | |
199 | ||
200 | return retArray; | |
201 | } | |
202 | ||
203 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1mul | |
204 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) | |
205 | { | |
47b9e78e | 206 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
207 | unsigned char* privkey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); |
208 | const unsigned char* tweak = (unsigned char*) (privkey + 32); | |
209 | ||
210 | jobjectArray retArray; | |
211 | jbyteArray privArray, intsByteArray; | |
212 | unsigned char intsarray[2]; | |
213 | ||
214 | int privkeylen = 32; | |
215 | ||
216 | int ret = secp256k1_ec_privkey_tweak_mul(ctx, privkey, tweak); | |
217 | ||
218 | intsarray[0] = privkeylen; | |
219 | intsarray[1] = ret; | |
220 | ||
221 | retArray = (*env)->NewObjectArray(env, 2, | |
222 | (*env)->FindClass(env, "[B"), | |
223 | (*env)->NewByteArray(env, 1)); | |
224 | ||
225 | privArray = (*env)->NewByteArray(env, privkeylen); | |
226 | (*env)->SetByteArrayRegion(env, privArray, 0, privkeylen, (jbyte*)privkey); | |
227 | (*env)->SetObjectArrayElement(env, retArray, 0, privArray); | |
228 | ||
229 | intsByteArray = (*env)->NewByteArray(env, 2); | |
230 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); | |
231 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
232 | ||
233 | (void)classObject; | |
234 | ||
235 | return retArray; | |
236 | } | |
237 | ||
238 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1add | |
239 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) | |
b5efbe58 | 240 | { |
47b9e78e | 241 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
242 | /* secp256k1_pubkey* pubkey = (secp256k1_pubkey*) (*env)->GetDirectBufferAddress(env, byteBufferObject);*/ |
243 | unsigned char* pkey = (*env)->GetDirectBufferAddress(env, byteBufferObject); | |
244 | const unsigned char* tweak = (unsigned char*) (pkey + publen); | |
245 | ||
246 | jobjectArray retArray; | |
247 | jbyteArray pubArray, intsByteArray; | |
248 | unsigned char intsarray[2]; | |
249 | unsigned char outputSer[65]; | |
250 | size_t outputLen = 65; | |
251 | ||
252 | secp256k1_pubkey pubkey; | |
253 | int ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pkey, publen); | |
254 | ||
255 | if( ret ) { | |
256 | ret = secp256k1_ec_pubkey_tweak_add(ctx, &pubkey, tweak); | |
257 | } | |
258 | ||
259 | if( ret ) { | |
260 | int ret2 = secp256k1_ec_pubkey_serialize(ctx,outputSer, &outputLen, &pubkey,SECP256K1_EC_UNCOMPRESSED );(void)ret2; | |
261 | } | |
262 | ||
263 | intsarray[0] = outputLen; | |
264 | intsarray[1] = ret; | |
265 | ||
266 | retArray = (*env)->NewObjectArray(env, 2, | |
267 | (*env)->FindClass(env, "[B"), | |
268 | (*env)->NewByteArray(env, 1)); | |
269 | ||
270 | pubArray = (*env)->NewByteArray(env, outputLen); | |
271 | (*env)->SetByteArrayRegion(env, pubArray, 0, outputLen, (jbyte*)outputSer); | |
272 | (*env)->SetObjectArrayElement(env, retArray, 0, pubArray); | |
273 | ||
274 | intsByteArray = (*env)->NewByteArray(env, 2); | |
275 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); | |
276 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
277 | ||
278 | (void)classObject; | |
279 | ||
280 | return retArray; | |
281 | } | |
282 | ||
283 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1mul | |
284 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) | |
285 | { | |
47b9e78e | 286 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
287 | unsigned char* pkey = (*env)->GetDirectBufferAddress(env, byteBufferObject); |
288 | const unsigned char* tweak = (unsigned char*) (pkey + publen); | |
289 | ||
290 | jobjectArray retArray; | |
291 | jbyteArray pubArray, intsByteArray; | |
292 | unsigned char intsarray[2]; | |
293 | unsigned char outputSer[65]; | |
294 | size_t outputLen = 65; | |
295 | ||
296 | secp256k1_pubkey pubkey; | |
297 | int ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pkey, publen); | |
298 | ||
299 | if ( ret ) { | |
300 | ret = secp256k1_ec_pubkey_tweak_mul(ctx, &pubkey, tweak); | |
301 | } | |
302 | ||
303 | if( ret ) { | |
304 | int ret2 = secp256k1_ec_pubkey_serialize(ctx,outputSer, &outputLen, &pubkey,SECP256K1_EC_UNCOMPRESSED );(void)ret2; | |
305 | } | |
306 | ||
307 | intsarray[0] = outputLen; | |
308 | intsarray[1] = ret; | |
309 | ||
310 | retArray = (*env)->NewObjectArray(env, 2, | |
311 | (*env)->FindClass(env, "[B"), | |
312 | (*env)->NewByteArray(env, 1)); | |
313 | ||
314 | pubArray = (*env)->NewByteArray(env, outputLen); | |
315 | (*env)->SetByteArrayRegion(env, pubArray, 0, outputLen, (jbyte*)outputSer); | |
316 | (*env)->SetObjectArrayElement(env, retArray, 0, pubArray); | |
b5efbe58 | 317 | |
3093576a G |
318 | intsByteArray = (*env)->NewByteArray(env, 2); |
319 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); | |
320 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
321 | ||
322 | (void)classObject; | |
323 | ||
324 | return retArray; | |
b5efbe58 MC |
325 | } |
326 | ||
3093576a G |
327 | SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1pubkey_1combine |
328 | (JNIEnv * env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint numkeys) | |
329 | { | |
330 | (void)classObject;(void)env;(void)byteBufferObject;(void)ctx_l;(void)numkeys; | |
b5efbe58 | 331 | |
3093576a | 332 | return 0; |
b5efbe58 MC |
333 | } |
334 | ||
3093576a G |
335 | SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdh |
336 | (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) | |
337 | { | |
47b9e78e | 338 | secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; |
3093576a G |
339 | const unsigned char* secdata = (*env)->GetDirectBufferAddress(env, byteBufferObject); |
340 | const unsigned char* pubdata = (const unsigned char*) (secdata + 32); | |
341 | ||
342 | jobjectArray retArray; | |
343 | jbyteArray outArray, intsByteArray; | |
344 | unsigned char intsarray[1]; | |
345 | secp256k1_pubkey pubkey; | |
346 | unsigned char nonce_res[32]; | |
347 | size_t outputLen = 32; | |
348 | ||
349 | int ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pubdata, publen); | |
350 | ||
351 | if (ret) { | |
352 | ret = secp256k1_ecdh( | |
353 | ctx, | |
354 | nonce_res, | |
355 | &pubkey, | |
b00be650 | 356 | secdata, |
c8fbc3c3 | 357 | NULL, |
b00be650 | 358 | NULL |
3093576a G |
359 | ); |
360 | } | |
361 | ||
362 | intsarray[0] = ret; | |
363 | ||
364 | retArray = (*env)->NewObjectArray(env, 2, | |
365 | (*env)->FindClass(env, "[B"), | |
366 | (*env)->NewByteArray(env, 1)); | |
367 | ||
368 | outArray = (*env)->NewByteArray(env, outputLen); | |
369 | (*env)->SetByteArrayRegion(env, outArray, 0, 32, (jbyte*)nonce_res); | |
370 | (*env)->SetObjectArrayElement(env, retArray, 0, outArray); | |
371 | ||
372 | intsByteArray = (*env)->NewByteArray(env, 1); | |
373 | (*env)->SetByteArrayRegion(env, intsByteArray, 0, 1, (jbyte*)intsarray); | |
374 | (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); | |
375 | ||
376 | (void)classObject; | |
377 | ||
378 | return retArray; | |
86e2d07e | 379 | } |