The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Native glue for Java class org.openssl.NativeBN |
| 19 | */ |
| 20 | |
Elliott Hughes | abf945f | 2010-06-03 19:55:20 -0700 | [diff] [blame] | 21 | #define LOG_TAG "NativeBN" |
| 22 | |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 23 | #include "JNIHelp.h" |
| 24 | #include "ScopedPrimitiveArray.h" |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 25 | #include "ScopedUtfChars.h" |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 26 | #include "UniquePtr.h" |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 27 | #include "jni.h" |
| 28 | #include <assert.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 29 | #include <openssl/bn.h> |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 30 | #include <openssl/crypto.h> |
| 31 | #include <openssl/err.h> |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 34 | struct BN_CTX_Deleter { |
| 35 | void operator()(BN_CTX* p) const { |
| 36 | BN_CTX_free(p); |
| 37 | } |
| 38 | }; |
| 39 | typedef UniquePtr<BN_CTX, BN_CTX_Deleter> Unique_BN_CTX; |
| 40 | |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 41 | static int isValidHandle (JNIEnv* env, void* handle, const char *message) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 42 | if (handle == NULL) { |
Elliott Hughes | da4f31d | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 43 | jniThrowNullPointerException(env, message); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 44 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 45 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 46 | return JNI_TRUE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int oneValidHandle (JNIEnv* env, void* a) |
| 50 | { |
| 51 | return isValidHandle(env, a, "Mandatory handle (first) passed as null"); |
| 52 | } |
| 53 | |
| 54 | static int twoValidHandles (JNIEnv* env, void* a, void *b) |
| 55 | { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 56 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 57 | return isValidHandle(env, b, "Mandatory handle (second) passed as null"); |
| 58 | } |
| 59 | |
| 60 | static int threeValidHandles (JNIEnv* env, void* a, void *b, void* c) |
| 61 | { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 62 | if (!twoValidHandles(env, a, b)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 63 | return isValidHandle(env, c, "Mandatory handle (third) passed as null"); |
| 64 | } |
| 65 | |
| 66 | static int fourValidHandles (JNIEnv* env, void* a, void *b, void* c, void* d) |
| 67 | { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 68 | if (!threeValidHandles(env, a, b, c)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 69 | return isValidHandle(env, d, "Mandatory handle (fourth) passed as null"); |
| 70 | } |
| 71 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 72 | static unsigned long NativeBN_ERR_get_error(JNIEnv*, jclass) { |
| 73 | return ERR_get_error(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 76 | static jstring NativeBN_ERR_error_string(JNIEnv* env, jclass, unsigned long e) { |
| 77 | char* errStr = ERR_error_string(e, NULL); |
| 78 | return env->NewStringUTF(errStr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 81 | static BIGNUM* NativeBN_BN_new(JNIEnv*, jclass) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 82 | return BN_new(); |
| 83 | } |
| 84 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 85 | static void NativeBN_BN_free(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 86 | if (!oneValidHandle(env, a)) return; |
| 87 | BN_free(a); |
| 88 | } |
| 89 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 90 | static int NativeBN_BN_cmp(JNIEnv* env, jclass, BIGNUM* a, BIGNUM* b) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 91 | if (!twoValidHandles(env, a, b)) return 1; |
| 92 | return BN_cmp(a, b); |
| 93 | } |
| 94 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 95 | static jboolean NativeBN_BN_copy(JNIEnv* env, jclass, BIGNUM* to, BIGNUM* from) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 96 | if (!twoValidHandles(env, to, from)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 97 | return (BN_copy(to, from) != NULL); |
| 98 | } |
| 99 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 100 | static jboolean NativeBN_putULongInt(JNIEnv* env, jclass, BIGNUM* a, unsigned long long dw, jboolean neg) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 101 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | unsigned int hi = dw >> 32; // This shifts without sign extension. |
| 103 | int lo = (int)dw; // This truncates implicitely. |
| 104 | |
| 105 | // cf. litEndInts2bn: |
| 106 | bn_check_top(a); |
| 107 | if (bn_wexpand(a, 2) != NULL) { |
| 108 | a->d[0] = lo; |
| 109 | a->d[1] = hi; |
| 110 | a->top = 2; |
| 111 | a->neg = neg; |
| 112 | bn_correct_top(a); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 113 | return JNI_TRUE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 114 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 115 | else return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 116 | } |
| 117 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 118 | static jboolean NativeBN_putLongInt(JNIEnv* env, jclass cls, BIGNUM* a, long long dw) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 119 | if (dw >= 0) return NativeBN_putULongInt(env, cls, a, dw, JNI_FALSE); |
| 120 | else return NativeBN_putULongInt(env, cls, a, -dw, JNI_TRUE); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 123 | static int NativeBN_BN_dec2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 124 | if (!oneValidHandle(env, a)) return -1; |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 125 | ScopedUtfChars chars(env, str); |
| 126 | if (chars.c_str() == NULL) { |
| 127 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 128 | } |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 129 | return BN_dec2bn(&a, chars.c_str()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 132 | static int NativeBN_BN_hex2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 133 | if (!oneValidHandle(env, a)) return -1; |
| 134 | ScopedUtfChars chars(env, str); |
| 135 | if (chars.c_str() == NULL) { |
| 136 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 137 | } |
Elliott Hughes | 0596087 | 2010-05-26 17:45:07 -0700 | [diff] [blame] | 138 | return BN_hex2bn(&a, chars.c_str()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 141 | static jboolean NativeBN_BN_bin2bn(JNIEnv* env, jclass, jbyteArray arr, int len, jboolean neg, BIGNUM* ret) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 142 | if (!oneValidHandle(env, ret)) return JNI_FALSE; |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 143 | ScopedByteArrayRO bytes(env, arr); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 144 | if (bytes.get() == NULL) { |
| 145 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 147 | jboolean success = (BN_bin2bn(reinterpret_cast<const unsigned char*>(bytes.get()), len, ret) != NULL); |
| 148 | if (success && neg) { |
| 149 | BN_set_negative(ret, 1); |
| 150 | } |
| 151 | return success; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * public static native boolean litEndInts2bn(int[], int, int, int) |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 156 | * Note: |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 157 | * This procedure directly writes the internal representation of BIGNUMs. |
| 158 | * We do so as there is no direct interface based on Little Endian Integer Arrays. |
| 159 | * Also note that the same representation is used in the Cordoba Java Implementation of BigIntegers, |
| 160 | * whereof certain functionality is still being used. |
| 161 | */ |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 162 | static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int len, jboolean neg, BIGNUM* ret) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 163 | if (!oneValidHandle(env, ret)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 164 | bn_check_top(ret); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 165 | if (len > 0) { |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 166 | ScopedIntArrayRO scopedArray(env, arr); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 167 | assert(sizeof(BN_ULONG) == sizeof(jint)); |
| 168 | const BN_ULONG* tmpInts = reinterpret_cast<const BN_ULONG*>(scopedArray.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 169 | if ((tmpInts != NULL) && (bn_wexpand(ret, len) != NULL)) { |
| 170 | int i = len; do { i--; ret->d[i] = tmpInts[i]; } while (i > 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 171 | ret->top = len; |
| 172 | ret->neg = neg; |
| 173 | // need to call this due to clear byte at top if avoiding |
| 174 | // having the top bit set (-ve number) |
| 175 | // Basically get rid of top zero ints: |
| 176 | bn_correct_top(ret); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 177 | return JNI_TRUE; |
| 178 | } else { |
| 179 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 180 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 181 | } else { // (len = 0) means value = 0 and sign will be 0, too. |
| 182 | ret->top = 0; |
| 183 | return JNI_TRUE; |
| 184 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | #define BYTES2INT(bytes, k) \ |
| 189 | ( (bytes[k + 3] & 0xFF) \ |
| 190 | | (bytes[k + 2] & 0xFF) << 8 \ |
| 191 | | (bytes[k + 1] & 0xFF) << 16 \ |
| 192 | | (bytes[k + 0] & 0xFF) << 24 ) |
| 193 | |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 194 | static jboolean negBigEndianBytes2bn(JNIEnv*, jclass, const unsigned char* bytes, int bytesLen, BIGNUM* ret) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 195 | // We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes: |
| 196 | // |
| 197 | bn_check_top(ret); |
| 198 | // FIXME: ASSERT (bytesLen > 0); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 199 | int intLen = (bytesLen + 3) / 4; |
| 200 | int firstNonzeroDigit = -2; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 201 | if (bn_wexpand(ret, intLen) != NULL) { |
| 202 | BN_ULONG* d = ret->d; |
| 203 | BN_ULONG di; |
| 204 | ret->top = intLen; |
| 205 | int highBytes = bytesLen % 4; |
| 206 | int k = bytesLen; |
| 207 | // Put bytes to the int array starting from the end of the byte array |
| 208 | int i = 0; |
| 209 | while (k > highBytes) { |
| 210 | k -= 4; |
| 211 | di = BYTES2INT(bytes, k); |
| 212 | if (di != 0) { |
| 213 | d[i] = -di; |
| 214 | firstNonzeroDigit = i; |
| 215 | i++; |
| 216 | while (k > highBytes) { |
| 217 | k -= 4; |
| 218 | d[i] = ~BYTES2INT(bytes, k); |
| 219 | i++; |
| 220 | } |
| 221 | break; |
| 222 | } else { |
| 223 | d[i] = 0; |
| 224 | i++; |
| 225 | } |
| 226 | } |
| 227 | if (highBytes != 0) { |
| 228 | di = -1; |
| 229 | // Put the first bytes in the highest element of the int array |
| 230 | if (firstNonzeroDigit != -2) { |
| 231 | for (k = 0; k < highBytes; k++) { |
| 232 | di = (di << 8) | (bytes[k] & 0xFF); |
| 233 | } |
| 234 | d[i] = ~di; |
| 235 | } else { |
| 236 | for (k = 0; k < highBytes; k++) { |
| 237 | di = (di << 8) | (bytes[k] & 0xFF); |
| 238 | } |
| 239 | d[i] = -di; |
| 240 | } |
| 241 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 242 | return JNI_TRUE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 243 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 244 | else return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 245 | } |
| 246 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 247 | static jboolean NativeBN_twosComp2bn(JNIEnv* env, jclass cls, jbyteArray arr, int bytesLen, BIGNUM* ret) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 248 | if (!oneValidHandle(env, ret)) return JNI_FALSE; |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 249 | ScopedByteArrayRO bytes(env, arr); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 250 | if (bytes.get() == NULL) { |
| 251 | return -1; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 252 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 253 | jboolean success; |
| 254 | const unsigned char* s = reinterpret_cast<const unsigned char*>(bytes.get()); |
| 255 | if ((bytes[0] & 0X80) == 0) { // Positive value! |
| 256 | // |
| 257 | // We can use the existing BN implementation for unsigned big endian bytes: |
| 258 | // |
| 259 | success = (BN_bin2bn(s, bytesLen, ret) != NULL); |
| 260 | BN_set_negative(ret, JNI_FALSE); |
| 261 | } else { // Negative value! |
| 262 | // |
| 263 | // We need to apply two's complement: |
| 264 | // |
| 265 | success = negBigEndianBytes2bn(env, cls, s, bytesLen, ret); |
| 266 | BN_set_negative(ret, JNI_TRUE); |
| 267 | } |
| 268 | return success; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 271 | static long long NativeBN_longInt(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 272 | if (!oneValidHandle(env, a)) return -1; |
| 273 | bn_check_top(a); |
| 274 | int intLen = a->top; |
| 275 | BN_ULONG* d = a->d; |
| 276 | switch (intLen) { |
| 277 | case 0: |
| 278 | return 0; |
| 279 | case 1: |
| 280 | if (!a->neg) return d[0] & 0X00000000FFFFFFFFLL; |
| 281 | else return -(d[0] & 0X00000000FFFFFFFFLL); |
| 282 | default: |
| 283 | if (!a->neg) return ((long long)d[1] << 32) | (d[0] & 0XFFFFFFFFLL); |
| 284 | else return -(((long long)d[1] << 32) | (d[0] & 0XFFFFFFFFLL)); |
| 285 | } |
| 286 | } |
| 287 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 288 | static char* leadingZerosTrimmed(char* s) { |
| 289 | char* p = s; |
| 290 | if (*p == '-') { |
| 291 | p++; |
| 292 | while ((*p == '0') && (*(p + 1) != 0)) { p++; } |
| 293 | p--; |
| 294 | *p = '-'; |
| 295 | } else { |
| 296 | while ((*p == '0') && (*(p + 1) != 0)) { p++; } |
| 297 | } |
| 298 | return p; |
| 299 | } |
| 300 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 301 | static jstring NativeBN_BN_bn2dec(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 302 | if (!oneValidHandle(env, a)) return NULL; |
| 303 | char* tmpStr; |
| 304 | char* retStr; |
| 305 | tmpStr = BN_bn2dec(a); |
| 306 | if (tmpStr != NULL) { |
| 307 | retStr = leadingZerosTrimmed(tmpStr); |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 308 | jstring returnJString = env->NewStringUTF(retStr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 309 | OPENSSL_free(tmpStr); |
| 310 | return returnJString; |
| 311 | } |
| 312 | else return NULL; |
| 313 | } |
| 314 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 315 | static jstring NativeBN_BN_bn2hex(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 316 | if (!oneValidHandle(env, a)) return NULL; |
| 317 | char* tmpStr; |
| 318 | char* retStr; |
| 319 | tmpStr = BN_bn2hex(a); |
| 320 | if (tmpStr != NULL) { |
| 321 | retStr = leadingZerosTrimmed(tmpStr); |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 322 | jstring returnJString = env->NewStringUTF(retStr); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 323 | OPENSSL_free(tmpStr); |
| 324 | return returnJString; |
| 325 | } |
| 326 | else return NULL; |
| 327 | } |
| 328 | |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 329 | static jbyteArray NativeBN_BN_bn2bin(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 330 | if (!oneValidHandle(env, a)) return NULL; |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 331 | jbyteArray result = env->NewByteArray(BN_num_bytes(a)); |
| 332 | if (result == NULL) { |
| 333 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 334 | } |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 335 | ScopedByteArrayRW bytes(env, result); |
| 336 | if (bytes.get() == NULL) { |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 337 | return NULL; |
| 338 | } |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 339 | BN_bn2bin(a, reinterpret_cast<unsigned char*>(bytes.get())); |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 340 | return result; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 343 | static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 344 | if (!oneValidHandle(env, a)) return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 345 | bn_check_top(a); |
| 346 | int len = a->top; |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 347 | if (len == 0) { |
| 348 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 349 | } |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 350 | jintArray result = env->NewIntArray(len); |
| 351 | if (result == NULL) { |
| 352 | return NULL; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 353 | } |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 354 | ScopedIntArrayRW ints(env, result); |
| 355 | BN_ULONG* ulongs = reinterpret_cast<BN_ULONG*>(ints.get()); |
| 356 | if (ulongs == NULL) { |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 357 | return NULL; |
| 358 | } |
Elliott Hughes | ebca53a | 2010-05-20 20:54:45 -0700 | [diff] [blame] | 359 | int i = len; do { i--; ulongs[i] = a->d[i]; } while (i > 0); |
Elliott Hughes | 4e21632 | 2010-05-18 15:06:16 -0700 | [diff] [blame] | 360 | return result; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 363 | static int NativeBN_sign(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 364 | if (!oneValidHandle(env, a)) return -2; |
| 365 | if (BN_is_zero(a)) return 0; |
| 366 | else if (BN_is_negative(a)) return -1; |
| 367 | else return 1; |
| 368 | } |
| 369 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 370 | static void NativeBN_BN_set_negative(JNIEnv* env, jclass, BIGNUM* b, int n) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 371 | if (!oneValidHandle(env, b)) return; |
| 372 | BN_set_negative(b, n); |
| 373 | } |
| 374 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 375 | static int NativeBN_bitLength(JNIEnv* env, jclass, BIGNUM* a) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 376 | // We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes: |
| 377 | // |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 378 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 379 | bn_check_top(a); |
| 380 | int intLen = a->top; |
| 381 | if (intLen == 0) return 0; |
| 382 | BN_ULONG* d = a->d; |
| 383 | int i = intLen - 1; |
| 384 | BN_ULONG msd = d[i]; // most significant digit |
| 385 | if (a->neg) { |
| 386 | // Handle negative values correctly: |
| 387 | // i.e. decrement the msd if all other digits are 0: |
| 388 | // while ((i > 0) && (d[i] != 0)) { i--; } |
| 389 | do { i--; } while (!((i < 0) || (d[i] != 0))); |
| 390 | if (i < 0) msd--; // Only if all lower significant digits are 0 we decrement the most significant one. |
| 391 | } |
| 392 | return (intLen - 1) * 32 + BN_num_bits_word(msd); |
| 393 | } |
| 394 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 395 | static jboolean NativeBN_BN_is_bit_set(JNIEnv* env, jclass, BIGNUM* a, int n) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 396 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 397 | return (jboolean)BN_is_bit_set(a, n); |
| 398 | } |
| 399 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 400 | static jboolean NativeBN_modifyBit(JNIEnv* env, jclass, BIGNUM* a, int n, int op) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 401 | // LOGD("NativeBN_BN_modifyBit"); |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 402 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 403 | switch (op) { |
| 404 | case 1: return BN_set_bit(a, n); |
| 405 | case 0: return BN_clear_bit(a, n); |
| 406 | case -1: |
| 407 | if (BN_is_bit_set(a, n)) return BN_clear_bit(a, n); |
| 408 | else return BN_set_bit(a, n); |
| 409 | } |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 410 | return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 413 | static jboolean NativeBN_BN_shift(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, int n) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 414 | if (!twoValidHandles(env, r, a)) return JNI_FALSE; |
Elliott Hughes | 13d47bd | 2010-02-03 15:23:07 -0800 | [diff] [blame] | 415 | return (n >= 0) ? BN_lshift(r, a, n) : BN_rshift(r, a, -n); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 418 | static jboolean NativeBN_BN_add_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 419 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 420 | return BN_add_word(a, w); |
| 421 | } |
| 422 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 423 | static jboolean NativeBN_BN_sub_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 424 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 425 | return BN_sub_word(a, w); |
| 426 | } |
| 427 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 428 | static jboolean NativeBN_BN_mul_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 429 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 430 | return BN_mul_word(a, w); |
| 431 | } |
| 432 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 433 | static BN_ULONG NativeBN_BN_div_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 434 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 435 | return BN_div_word(a, w); |
| 436 | } |
| 437 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 438 | static BN_ULONG NativeBN_BN_mod_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 439 | if (!oneValidHandle(env, a)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 440 | return BN_mod_word(a, w); |
| 441 | } |
| 442 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 443 | static jboolean NativeBN_BN_add(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 444 | if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 445 | return BN_add(r, a, b); |
| 446 | } |
| 447 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 448 | static jboolean NativeBN_BN_sub(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 449 | if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 450 | return BN_sub(r, a, b); |
| 451 | } |
| 452 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 453 | static jboolean NativeBN_BN_gcd(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 454 | if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 455 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 456 | return BN_gcd(r, a, b, ctx.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 459 | static jboolean NativeBN_BN_mul(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 460 | if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 461 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 462 | return BN_mul(r, a, b, ctx.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 465 | static jboolean NativeBN_BN_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 466 | if (!threeValidHandles(env, r, a, p)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 467 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 468 | return BN_exp(r, a, p, ctx.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 471 | static jboolean NativeBN_BN_div(JNIEnv* env, jclass, BIGNUM* dv, BIGNUM* rem, BIGNUM* m, BIGNUM* d) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 472 | if (!fourValidHandles(env, (rem ? rem : dv), (dv ? dv : rem), m, d)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 473 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 474 | return BN_div(dv, rem, m, d, ctx.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 477 | static jboolean NativeBN_BN_nnmod(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* m) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 478 | if (!threeValidHandles(env, r, a, m)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 479 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 480 | return BN_nnmod(r, a, m, ctx.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 483 | static jboolean NativeBN_BN_mod_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p, BIGNUM* m) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 484 | if (!fourValidHandles(env, r, a, p, m)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 485 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 486 | return BN_mod_exp(r, a, p, m, ctx.get()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 489 | static jboolean NativeBN_BN_mod_inverse(JNIEnv* env, jclass, BIGNUM* ret, BIGNUM* a, BIGNUM* n) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 490 | if (!threeValidHandles(env, ret, a, n)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 491 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 492 | return (BN_mod_inverse(ret, a, n, ctx.get()) != NULL); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Brian Carlstrom | ec2f593 | 2010-05-07 16:58:16 -0700 | [diff] [blame] | 495 | static jboolean NativeBN_BN_generate_prime_ex(JNIEnv* env, jclass, BIGNUM* ret, int bits, jboolean safe, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 496 | BIGNUM* add, BIGNUM* rem, jint cb) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 497 | if (!oneValidHandle(env, ret)) return JNI_FALSE; |
Elliott Hughes | 13d47bd | 2010-02-03 15:23:07 -0800 | [diff] [blame] | 498 | return BN_generate_prime_ex(ret, bits, safe, add, rem, (BN_GENCB*) cb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 501 | static jboolean NativeBN_BN_is_prime_ex(JNIEnv* env, jclass, BIGNUM* p, int nchecks, jint cb) { |
Elliott Hughes | 99c59bf | 2010-05-17 16:22:04 -0700 | [diff] [blame] | 502 | if (!oneValidHandle(env, p)) return JNI_FALSE; |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 503 | Unique_BN_CTX ctx(BN_CTX_new()); |
| 504 | return BN_is_prime_ex(p, nchecks, ctx.get(), (BN_GENCB*) cb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 507 | static JNINativeMethod gMethods[] = { |
| 508 | { "BN_add", "(III)Z", (void*)NativeBN_BN_add }, |
| 509 | { "BN_add_word", "(II)Z", (void*)NativeBN_BN_add_word }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 510 | { "BN_bin2bn", "([BIZI)Z", (void*)NativeBN_BN_bin2bn }, |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 511 | { "BN_bn2bin", "(I)[B", (void*)NativeBN_BN_bn2bin }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 512 | { "BN_bn2dec", "(I)Ljava/lang/String;", (void*)NativeBN_BN_bn2dec }, |
| 513 | { "BN_bn2hex", "(I)Ljava/lang/String;", (void*)NativeBN_BN_bn2hex }, |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 514 | { "BN_cmp", "(II)I", (void*)NativeBN_BN_cmp }, |
| 515 | { "BN_copy", "(II)Z", (void*)NativeBN_BN_copy }, |
| 516 | { "BN_dec2bn", "(ILjava/lang/String;)I", (void*)NativeBN_BN_dec2bn }, |
| 517 | { "BN_div", "(IIII)Z", (void*)NativeBN_BN_div }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 518 | { "BN_div_word", "(II)I", (void*)NativeBN_BN_div_word }, |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 519 | { "BN_exp", "(III)Z", (void*)NativeBN_BN_exp }, |
| 520 | { "BN_free", "(I)V", (void*)NativeBN_BN_free }, |
| 521 | { "BN_gcd", "(III)Z", (void*)NativeBN_BN_gcd }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 522 | { "BN_generate_prime_ex", "(IIZIII)Z", (void*)NativeBN_BN_generate_prime_ex }, |
Elliott Hughes | 6371043 | 2010-06-11 12:58:06 -0700 | [diff] [blame] | 523 | { "BN_hex2bn", "(ILjava/lang/String;)I", (void*)NativeBN_BN_hex2bn }, |
| 524 | { "BN_is_bit_set", "(II)Z", (void*)NativeBN_BN_is_bit_set }, |
| 525 | { "BN_is_prime_ex", "(III)Z", (void*)NativeBN_BN_is_prime_ex }, |
| 526 | { "BN_mod_exp", "(IIII)Z", (void*)NativeBN_BN_mod_exp }, |
| 527 | { "BN_mod_inverse", "(III)Z", (void*)NativeBN_BN_mod_inverse }, |
| 528 | { "BN_mod_word", "(II)I", (void*)NativeBN_BN_mod_word }, |
| 529 | { "BN_mul", "(III)Z", (void*)NativeBN_BN_mul }, |
| 530 | { "BN_mul_word", "(II)Z", (void*)NativeBN_BN_mul_word }, |
| 531 | { "BN_new", "()I", (void*)NativeBN_BN_new }, |
| 532 | { "BN_nnmod", "(III)Z", (void*)NativeBN_BN_nnmod }, |
| 533 | { "BN_set_negative", "(II)V", (void*)NativeBN_BN_set_negative }, |
| 534 | { "BN_shift", "(III)Z", (void*)NativeBN_BN_shift }, |
| 535 | { "BN_sub", "(III)Z", (void*)NativeBN_BN_sub }, |
| 536 | { "BN_sub_word", "(II)Z", (void*)NativeBN_BN_sub_word }, |
| 537 | { "ERR_error_string", "(I)Ljava/lang/String;", (void*)NativeBN_ERR_error_string }, |
| 538 | { "ERR_get_error", "()I", (void*)NativeBN_ERR_get_error }, |
| 539 | { "bitLength", "(I)I", (void*)NativeBN_bitLength }, |
| 540 | { "bn2litEndInts", "(I)[I", (void*)NativeBN_bn2litEndInts }, |
| 541 | { "litEndInts2bn", "([IIZI)Z", (void*)NativeBN_litEndInts2bn }, |
| 542 | { "longInt", "(I)J", (void*)NativeBN_longInt }, |
| 543 | { "modifyBit", "(III)Z", (void*)NativeBN_modifyBit }, |
| 544 | { "putLongInt", "(IJ)Z", (void*)NativeBN_putLongInt }, |
| 545 | { "putULongInt", "(IJZ)Z", (void*)NativeBN_putULongInt }, |
| 546 | { "sign", "(I)I", (void*)NativeBN_sign }, |
| 547 | { "twosComp2bn", "([BII)Z", (void*)NativeBN_twosComp2bn }, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 548 | }; |
Brian Carlstrom | 12cd1f0 | 2010-06-22 23:43:20 -0700 | [diff] [blame^] | 549 | int register_java_math_NativeBN(JNIEnv* env) { |
| 550 | return jniRegisterNativeMethods(env, "java/math/NativeBN", gMethods, NELEM(gMethods)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 551 | } |