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