diff options
author | Elliott Hughes <enh@google.com> | 2010-06-11 12:58:06 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2010-06-11 12:58:06 -0700 |
commit | 63710430c17f7c0a1e74b926cd21248fde8e9589 (patch) | |
tree | b7367fab1a3371f2742b14163b2724c545fdf223 | |
parent | 9a0668a7f531b04dd001a4d193c159dabdb7bf0c (diff) | |
download | libcore-63710430c17f7c0a1e74b926cd21248fde8e9589.zip libcore-63710430c17f7c0a1e74b926cd21248fde8e9589.tar.gz libcore-63710430c17f7c0a1e74b926cd21248fde8e9589.tar.bz2 |
Make BigInteger thread-safe.
We were sharing an openssl BN_CTX between threads, which is unsafe. Stop
doing that, creating BN_CTXes on demand (which seems to be what openssl
does internally). For 1024-bit integers, this makes division faster,
multiplication slower, and makes no convincing difference to gcd. I
instrumented a build to report any time one of the methods that needs
a BN_CTX gets called, and couldn't find anywhere they're used during
boot or https browser usage. (These things do use BigInteger; they just
don't use the methods this change affects.)
Bug: 2652542
Change-Id: I98c94b41df95566cb4c8598f299911e641f72f63
-rw-r--r-- | luni/src/main/java/java/math/BigInt.java | 71 | ||||
-rw-r--r-- | luni/src/main/java/java/math/BigInteger.java | 22 | ||||
-rw-r--r-- | luni/src/main/java/java/math/Primality.java | 2 | ||||
-rw-r--r-- | openssl/src/main/java/org/openssl/NativeBN.java | 20 | ||||
-rw-r--r-- | openssl/src/main/native/NativeBN.cpp | 249 |
5 files changed, 110 insertions, 254 deletions
diff --git a/luni/src/main/java/java/math/BigInt.java b/luni/src/main/java/java/math/BigInt.java index 9c638e4..70448ee 100644 --- a/luni/src/main/java/java/math/BigInt.java +++ b/luni/src/main/java/java/math/BigInt.java @@ -24,38 +24,13 @@ import org.openssl.NativeBN; * Any Bit-Operations, including Shifting, solely regard the unsigned magnitude. * Moreover BigInt objects are mutable and offer efficient in-place-operations. */ - -class BigInt -// extends Number -// implements Comparable<BigInt>, -// Serializable -{ - - class Context { - int bnctx; - Context() { - bnctx = NativeBN.BN_CTX_new(); - } - } - static BigInt dummy; - static Context defaultContext; - - static { - dummy = new BigInt(); - defaultContext = dummy.new Context(); - } - - static int getCtx (Context t) { - return (t != null) ? t.bnctx : defaultContext.bnctx; - } - +class BigInt { /** This is the serialVersionUID used by the sun implementation */ private static final long serialVersionUID = -8287574255936472291L; /* Fields used for the internal representation. */ transient int bignum = 0; - public void dispose() { if (this.bignum != 0) { NativeBN.BN_free(this.bignum); @@ -348,40 +323,40 @@ class BigInt } - public static BigInt gcd(BigInt a, BigInt b, Context t) { + public static BigInt gcd(BigInt a, BigInt b) { BigInt r = newBigInt(); - Check(NativeBN.BN_gcd(r.bignum, a.bignum, b.bignum, getCtx(t))); + Check(NativeBN.BN_gcd(r.bignum, a.bignum, b.bignum)); return r; } - public static BigInt product(BigInt a, BigInt b, Context t) { + public static BigInt product(BigInt a, BigInt b) { BigInt r = newBigInt(); - Check(NativeBN.BN_mul(r.bignum, a.bignum, b.bignum, getCtx(t))); + Check(NativeBN.BN_mul(r.bignum, a.bignum, b.bignum)); return r; } - public void multiplyBy(BigInt a, Context t) { - Check(NativeBN.BN_mul(this.bignum, this.bignum, a.bignum, getCtx(t))); + public void multiplyBy(BigInt a) { + Check(NativeBN.BN_mul(this.bignum, this.bignum, a.bignum)); } - public static BigInt bigExp(BigInt a, BigInt p, Context t) { + public static BigInt bigExp(BigInt a, BigInt p) { // Sign of p is ignored! BigInt r = newBigInt(); - Check(NativeBN.BN_exp(r.bignum, a.bignum, p.bignum, getCtx(t))); + Check(NativeBN.BN_exp(r.bignum, a.bignum, p.bignum)); return r; } - public static BigInt exp(BigInt a, int p, Context t) { + public static BigInt exp(BigInt a, int p) { // Sign of p is ignored! BigInt power = new BigInt(); power.putLongInt(p); - return bigExp(a, power, t); + return bigExp(a, power); // OPTIONAL: // public int BN_sqr(BigInteger r, BigInteger a, BN_CTX ctx); // int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx); } - public static void division(BigInt dividend, BigInt divisor, Context t, + public static void division(BigInt dividend, BigInt divisor, BigInt quotient, BigInt remainder) { int quot, rem; if (quotient != null) { @@ -394,20 +369,20 @@ class BigInt rem = remainder.bignum; } else rem = 0; - Check(NativeBN.BN_div(quot, rem, dividend.bignum, divisor.bignum, getCtx(t))); + Check(NativeBN.BN_div(quot, rem, dividend.bignum, divisor.bignum)); } - public static BigInt modulus(BigInt a, BigInt m, Context t) { + public static BigInt modulus(BigInt a, BigInt m) { // Sign of p is ignored! ? BigInt r = newBigInt(); - Check(NativeBN.BN_nnmod(r.bignum, a.bignum, m.bignum, getCtx(t))); + Check(NativeBN.BN_nnmod(r.bignum, a.bignum, m.bignum)); return r; } - public static BigInt modExp(BigInt a, BigInt p, BigInt m, Context t) { + public static BigInt modExp(BigInt a, BigInt p, BigInt m) { // Sign of p is ignored! BigInt r = newBigInt(); - Check(NativeBN.BN_mod_exp(r.bignum, a.bignum, p.bignum, m.bignum, getCtx(t))); + Check(NativeBN.BN_mod_exp(r.bignum, a.bignum, p.bignum, m.bignum)); // OPTIONAL: // int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); @@ -415,26 +390,26 @@ class BigInt } - public static BigInt modInverse(BigInt a, BigInt m, Context t) { + public static BigInt modInverse(BigInt a, BigInt m) { BigInt r = newBigInt(); - Check(NativeBN.BN_mod_inverse(r.bignum, a.bignum, m.bignum, getCtx(t))); + Check(NativeBN.BN_mod_inverse(r.bignum, a.bignum, m.bignum)); return r; } - public static BigInt generatePrimeDefault(int bitLength, Random rnd, Context t) { + public static BigInt generatePrimeDefault(int bitLength, Random rnd) { BigInt r = newBigInt(); Check(NativeBN.BN_generate_prime_ex(r.bignum, bitLength, false, 0, 0, 0)); return r; } - public static BigInt generatePrimeSafe(int bitLength, Random rnd, Context t) { + public static BigInt generatePrimeSafe(int bitLength, Random rnd) { BigInt r = newBigInt(); Check(NativeBN.BN_generate_prime_ex(r.bignum, bitLength, true, 0, 0, 0)); return r; } - public boolean isPrime(int certainty, Random rnd, Context t) { - return NativeBN.BN_is_prime_ex(bignum, certainty, getCtx(t), 0); + public boolean isPrime(int certainty, Random rnd) { + return NativeBN.BN_is_prime_ex(bignum, certainty, 0); } } diff --git a/luni/src/main/java/java/math/BigInteger.java b/luni/src/main/java/java/math/BigInteger.java index c113d6b..8f5160c 100644 --- a/luni/src/main/java/java/math/BigInteger.java +++ b/luni/src/main/java/java/math/BigInteger.java @@ -264,7 +264,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, if (bitLength < 2) { throw new ArithmeticException("bitLength < 2"); } - bigInt = BigInt.generatePrimeDefault(bitLength, rnd, null); + bigInt = BigInt.generatePrimeDefault(bitLength, rnd); bigIntIsValid = true; // !oldReprIsValid } @@ -1023,7 +1023,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, */ public BigInteger gcd(BigInteger val) { validate2("gcd", this, val); - return new BigInteger(BigInt.gcd(bigInt, val.bigInt, null)); + return new BigInteger(BigInt.gcd(bigInt, val.bigInt)); } /** @@ -1037,7 +1037,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, */ public BigInteger multiply(BigInteger val) { validate2("multiply", this, val); - return new BigInteger(BigInt.product(bigInt, val.bigInt, null)); + return new BigInteger(BigInt.product(bigInt, val.bigInt)); } /** @@ -1054,7 +1054,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, throw new ArithmeticException("exp < 0"); } validate1("pow", this); - return new BigInteger(BigInt.exp(bigInt, exp, null)); + return new BigInteger(BigInt.exp(bigInt, exp)); } /** @@ -1075,7 +1075,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, validate2("divideAndRemainder", this, divisor); BigInt quotient = new BigInt(); BigInt remainder = new BigInt(); - BigInt.division(bigInt, divisor.bigInt, null, quotient, remainder); + BigInt.division(bigInt, divisor.bigInt, quotient, remainder); BigInteger[] a = new BigInteger[2]; a[0] = new BigInteger(quotient); a[1] = new BigInteger(remainder); @@ -1098,7 +1098,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, public BigInteger divide(BigInteger divisor) { validate2("divide", this, divisor); BigInt quotient = new BigInt(); - BigInt.division(bigInt, divisor.bigInt, null, quotient, null); + BigInt.division(bigInt, divisor.bigInt, quotient, null); return new BigInteger(quotient); } @@ -1118,7 +1118,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, public BigInteger remainder(BigInteger divisor) { validate2("remainder", this, divisor); BigInt remainder = new BigInt(); - BigInt.division(bigInt, divisor.bigInt, null, null, remainder); + BigInt.division(bigInt, divisor.bigInt, null, remainder); return new BigInteger(remainder); } @@ -1142,7 +1142,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, throw new ArithmeticException("modulus not positive"); } validate2("modInverse", this, m); - return new BigInteger(BigInt.modInverse(bigInt, m.bigInt, null)); + return new BigInteger(BigInt.modInverse(bigInt, m.bigInt)); } /** @@ -1176,7 +1176,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, base = this; } validate3("modPow", base, exponent, m); - return new BigInteger(BigInt.modExp(base.bigInt, exponent.bigInt, m.bigInt, null)); + return new BigInteger(BigInt.modExp(base.bigInt, exponent.bigInt, m.bigInt)); } /** @@ -1199,7 +1199,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, throw new ArithmeticException("modulus not positive"); } validate2("mod", this, m); - return new BigInteger(BigInt.modulus(bigInt, m.bigInt, null)); + return new BigInteger(BigInt.modulus(bigInt, m.bigInt)); } /** @@ -1219,7 +1219,7 @@ public class BigInteger extends Number implements Comparable<BigInteger>, return true; } validate1("isProbablePrime", this); - return bigInt.isPrime(certainty, null, null); + return bigInt.isPrime(certainty, null); } /** diff --git a/luni/src/main/java/java/math/Primality.java b/luni/src/main/java/java/math/Primality.java index 48557a6..f967da6 100644 --- a/luni/src/main/java/java/math/Primality.java +++ b/luni/src/main/java/java/math/Primality.java @@ -140,7 +140,7 @@ class Primality { if (!isDivisible[j]) { probPrime.putCopy(startPoint); probPrime.addPositiveInt(j); - if (probPrime.isPrime(100, null, null)) { + if (probPrime.isPrime(100, null)) { return new BigInteger(probPrime); } } diff --git a/openssl/src/main/java/org/openssl/NativeBN.java b/openssl/src/main/java/org/openssl/NativeBN.java index 0650425..7653cba 100644 --- a/openssl/src/main/java/org/openssl/NativeBN.java +++ b/openssl/src/main/java/org/openssl/NativeBN.java @@ -24,9 +24,6 @@ public class NativeBN { public static native String ERR_error_string(int e); // char *ERR_error_string(unsigned long e, char *buf); - public static native int BN_CTX_new(); - // BN_CTX *BN_CTX_new(void); - public static native int BN_new(); // BIGNUM *BN_new(void); @@ -125,27 +122,26 @@ public class NativeBN { public static native boolean BN_sub(int r, int a, int b); // int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); - - public static native boolean BN_gcd(int r, int a, int b, int ctx); + public static native boolean BN_gcd(int r, int a, int b); // int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); - public static native boolean BN_mul(int r, int a, int b, int ctx); + public static native boolean BN_mul(int r, int a, int b); // int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); - public static native boolean BN_exp(int r, int a, int p, int ctx); + public static native boolean BN_exp(int r, int a, int p); // int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); // OPTIONAL: // public static native int BN_sqr(BigInteger r, BigInteger a, BN_CTX ctx); // int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx); - public static native boolean BN_div(int dv, int rem, int m, int d, int ctx); + public static native boolean BN_div(int dv, int rem, int m, int d); // int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); - public static native boolean BN_nnmod(int r, int a, int m, int ctx); + public static native boolean BN_nnmod(int r, int a, int m); // int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); - public static native boolean BN_mod_exp(int r, int a, int p, int m, int ctx); + public static native boolean BN_mod_exp(int r, int a, int p, int m); // int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx); // OPTIONAL: @@ -153,7 +149,7 @@ public class NativeBN { // int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); - public static native boolean BN_mod_inverse(int ret, int a, int n, int ctx); + public static native boolean BN_mod_inverse(int ret, int a, int n); // Returns boolean success AND NOT result BIGNUM handle! // BIGNUM * BN_mod_inverse(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); @@ -163,7 +159,7 @@ public class NativeBN { // int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, // const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb); - public static native boolean BN_is_prime_ex(int p, int nchecks, int ctx, int cb); + public static native boolean BN_is_prime_ex(int p, int nchecks, int cb); // int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb); // OPTIONAL: diff --git a/openssl/src/main/native/NativeBN.cpp b/openssl/src/main/native/NativeBN.cpp index 6270926..3d129c8 100644 --- a/openssl/src/main/native/NativeBN.cpp +++ b/openssl/src/main/native/NativeBN.cpp @@ -23,6 +23,7 @@ #include "JNIHelp.h" #include "ScopedPrimitiveArray.h" #include "ScopedUtfChars.h" +#include "UniquePtr.h" #include "jni.h" #include <assert.h> #include <openssl/bn.h> @@ -30,6 +31,13 @@ #include <openssl/err.h> #include <stdio.h> +struct BN_CTX_Deleter { + void operator()(BN_CTX* p) const { + BN_CTX_free(p); + } +}; +typedef UniquePtr<BN_CTX, BN_CTX_Deleter> Unique_BN_CTX; + static int isValidHandle (JNIEnv* env, void* handle, const char *message) { if (handle == NULL) { jniThrowNullPointerException(env, message); @@ -61,67 +69,34 @@ static int fourValidHandles (JNIEnv* env, void* a, void *b, void* c, void* d) return isValidHandle(env, d, "Mandatory handle (fourth) passed as null"); } - -/** - * public static native int ERR_get_error(); - */ static unsigned long NativeBN_ERR_get_error(JNIEnv*, jclass) { return ERR_get_error(); } -/** - * public static native String ERR_error_string(int); - */ static jstring NativeBN_ERR_error_string(JNIEnv* env, jclass, unsigned long e) { char* errStr = ERR_error_string(e, NULL); return env->NewStringUTF(errStr); } - -/** - * public static native int BN_CTX_new() - */ -static BN_CTX* NativeBN_BN_CTX_new(JNIEnv*, jclass) { - return BN_CTX_new(); -} - - -/** - * public static native int BN_new() - */ static BIGNUM* NativeBN_BN_new(JNIEnv*, jclass) { return BN_new(); } -/** - * public static native int BN_free() - */ static void NativeBN_BN_free(JNIEnv* env, jclass, BIGNUM* a) { if (!oneValidHandle(env, a)) return; BN_free(a); } - -/** - * public static native int BN_cmp(int, int) - */ static int NativeBN_BN_cmp(JNIEnv* env, jclass, BIGNUM* a, BIGNUM* b) { if (!twoValidHandles(env, a, b)) return 1; return BN_cmp(a, b); } -/** - * public static native int BN_copy(int, int) - */ static jboolean NativeBN_BN_copy(JNIEnv* env, jclass, BIGNUM* to, BIGNUM* from) { if (!twoValidHandles(env, to, from)) return JNI_FALSE; return (BN_copy(to, from) != NULL); } - -/** - * public static native int putULongInt(int, long, int) - */ static jboolean NativeBN_putULongInt(JNIEnv* env, jclass, BIGNUM* a, unsigned long long dw, jboolean neg) { if (!oneValidHandle(env, a)) return JNI_FALSE; unsigned int hi = dw >> 32; // This shifts without sign extension. @@ -140,17 +115,11 @@ static jboolean NativeBN_putULongInt(JNIEnv* env, jclass, BIGNUM* a, unsigned lo else return JNI_FALSE; } -/** - * public static native int putLongInt(int, long) - */ static jboolean NativeBN_putLongInt(JNIEnv* env, jclass cls, BIGNUM* a, long long dw) { if (dw >= 0) return NativeBN_putULongInt(env, cls, a, dw, JNI_FALSE); else return NativeBN_putULongInt(env, cls, a, -dw, JNI_TRUE); } -/** - * public static native int BN_dec2bn(int, java.lang.String) - */ static int NativeBN_BN_dec2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { if (!oneValidHandle(env, a)) return -1; ScopedUtfChars chars(env, str); @@ -160,9 +129,6 @@ static int NativeBN_BN_dec2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { return BN_dec2bn(&a, chars.c_str()); } -/** - * public static native int BN_hex2bn(int, java.lang.String) - */ static int NativeBN_BN_hex2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { if (!oneValidHandle(env, a)) return -1; ScopedUtfChars chars(env, str); @@ -172,9 +138,6 @@ static int NativeBN_BN_hex2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { return BN_hex2bn(&a, chars.c_str()); } -/** - * public static native boolean BN_bin2bn(byte[], int, int, int) - */ static jboolean NativeBN_BN_bin2bn(JNIEnv* env, jclass, jbyteArray arr, int len, jboolean neg, BIGNUM* ret) { if (!oneValidHandle(env, ret)) return JNI_FALSE; ScopedByteArrayRO bytes(env, arr); @@ -281,9 +244,6 @@ static jboolean negBigEndianBytes2bn(JNIEnv*, jclass, const unsigned char* bytes else return JNI_FALSE; } -/** - * public static native boolean twosComp2bn(byte[], int, int) - */ static jboolean NativeBN_twosComp2bn(JNIEnv* env, jclass cls, jbyteArray arr, int bytesLen, BIGNUM* ret) { if (!oneValidHandle(env, ret)) return JNI_FALSE; ScopedByteArrayRO bytes(env, arr); @@ -308,10 +268,6 @@ static jboolean NativeBN_twosComp2bn(JNIEnv* env, jclass cls, jbyteArray arr, in return success; } - -/** - * public static native long longInt(int) - */ static long long NativeBN_longInt(JNIEnv* env, jclass, BIGNUM* a) { if (!oneValidHandle(env, a)) return -1; bn_check_top(a); @@ -329,7 +285,6 @@ static long long NativeBN_longInt(JNIEnv* env, jclass, BIGNUM* a) { } } - static char* leadingZerosTrimmed(char* s) { char* p = s; if (*p == '-') { @@ -343,9 +298,6 @@ static char* leadingZerosTrimmed(char* s) { return p; } -/** - * public static native java.lang.String BN_bn2dec(int) - */ static jstring NativeBN_BN_bn2dec(JNIEnv* env, jclass, BIGNUM* a) { if (!oneValidHandle(env, a)) return NULL; char* tmpStr; @@ -360,9 +312,6 @@ static jstring NativeBN_BN_bn2dec(JNIEnv* env, jclass, BIGNUM* a) { else return NULL; } -/** - * public static native java.lang.String BN_bn2hex(int) - */ static jstring NativeBN_BN_bn2hex(JNIEnv* env, jclass, BIGNUM* a) { if (!oneValidHandle(env, a)) return NULL; char* tmpStr; @@ -411,9 +360,6 @@ static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, BIGNUM* a) { return result; } -/** - * public static native int sign(int) - */ static int NativeBN_sign(JNIEnv* env, jclass, BIGNUM* a) { if (!oneValidHandle(env, a)) return -2; if (BN_is_zero(a)) return 0; @@ -421,17 +367,11 @@ static int NativeBN_sign(JNIEnv* env, jclass, BIGNUM* a) { else return 1; } -/** - * public static native void BN_set_negative(int, int) - */ static void NativeBN_BN_set_negative(JNIEnv* env, jclass, BIGNUM* b, int n) { if (!oneValidHandle(env, b)) return; BN_set_negative(b, n); } -/** - * public static native int bitLength(int) - */ static int NativeBN_bitLength(JNIEnv* env, jclass, BIGNUM* a) { // We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes: // @@ -452,17 +392,11 @@ static int NativeBN_bitLength(JNIEnv* env, jclass, BIGNUM* a) { return (intLen - 1) * 32 + BN_num_bits_word(msd); } -/** - * public static native boolean BN_is_bit_set(int, int) - */ static jboolean NativeBN_BN_is_bit_set(JNIEnv* env, jclass, BIGNUM* a, int n) { if (!oneValidHandle(env, a)) return JNI_FALSE; return (jboolean)BN_is_bit_set(a, n); } -/** - * public static native void modifyBit(int, int, int) - */ static jboolean NativeBN_modifyBit(JNIEnv* env, jclass, BIGNUM* a, int n, int op) { // LOGD("NativeBN_BN_modifyBit"); if (!oneValidHandle(env, a)) return JNI_FALSE; @@ -476,191 +410,142 @@ static jboolean NativeBN_modifyBit(JNIEnv* env, jclass, BIGNUM* a, int n, int op return JNI_FALSE; } -/** - * public static native int BN_shift(int, int, int) - */ static jboolean NativeBN_BN_shift(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, int n) { if (!twoValidHandles(env, r, a)) return JNI_FALSE; return (n >= 0) ? BN_lshift(r, a, n) : BN_rshift(r, a, -n); } -/** - * public static native boolean BN_add_word(int, int) - */ static jboolean NativeBN_BN_add_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { if (!oneValidHandle(env, a)) return JNI_FALSE; return BN_add_word(a, w); } -/** - * public static native boolean BN_sub_word(int, int) - */ static jboolean NativeBN_BN_sub_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { if (!oneValidHandle(env, a)) return JNI_FALSE; return BN_sub_word(a, w); } -/** - * public static native boolean BN_mul_word(int, int) - */ static jboolean NativeBN_BN_mul_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { if (!oneValidHandle(env, a)) return JNI_FALSE; return BN_mul_word(a, w); } -/** - * public static native boolean BN_div_word(int, int) - */ static BN_ULONG NativeBN_BN_div_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { if (!oneValidHandle(env, a)) return JNI_FALSE; return BN_div_word(a, w); } -/** - * public static native boolean BN_mod_word(int, int) - */ static BN_ULONG NativeBN_BN_mod_word(JNIEnv* env, jclass, BIGNUM *a, BN_ULONG w) { if (!oneValidHandle(env, a)) return JNI_FALSE; return BN_mod_word(a, w); } - - -/** - * public static native int BN_add(int, int, int) - */ static jboolean NativeBN_BN_add(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; return BN_add(r, a, b); } -/** - * public static native int BN_sub(int, int, int) - */ static jboolean NativeBN_BN_sub(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; return BN_sub(r, a, b); } - -/** - * public static native int BN_gcd(int, int, int, int) - */ -static jboolean NativeBN_BN_gcd(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b, BN_CTX* ctx) { +static jboolean NativeBN_BN_gcd(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; - return BN_gcd(r, a, b, ctx); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_gcd(r, a, b, ctx.get()); } -/** - * public static native int BN_mul(int, int, int, int) - */ -static jboolean NativeBN_BN_mul(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b, BN_CTX* ctx) { +static jboolean NativeBN_BN_mul(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) { if (!threeValidHandles(env, r, a, b)) return JNI_FALSE; - return BN_mul(r, a, b, ctx); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_mul(r, a, b, ctx.get()); } -/** - * public static native int BN_exp(int, int, int, int) - */ -static jboolean NativeBN_BN_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p, BN_CTX* ctx) { +static jboolean NativeBN_BN_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p) { if (!threeValidHandles(env, r, a, p)) return JNI_FALSE; - return BN_exp(r, a, p, ctx); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_exp(r, a, p, ctx.get()); } -/** - * public static native boolean BN_div(int, int, int, int, int) - */ -static jboolean NativeBN_BN_div(JNIEnv* env, jclass, BIGNUM* dv, BIGNUM* rem, BIGNUM* m, BIGNUM* d, BN_CTX* ctx) { +static jboolean NativeBN_BN_div(JNIEnv* env, jclass, BIGNUM* dv, BIGNUM* rem, BIGNUM* m, BIGNUM* d) { if (!fourValidHandles(env, (rem ? rem : dv), (dv ? dv : rem), m, d)) return JNI_FALSE; - return BN_div(dv, rem, m, d, ctx); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_div(dv, rem, m, d, ctx.get()); } -/** - * public static native int BN_nnmod(int, int, int, int) - */ -static jboolean NativeBN_BN_nnmod(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* m, BN_CTX* ctx) { +static jboolean NativeBN_BN_nnmod(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* m) { if (!threeValidHandles(env, r, a, m)) return JNI_FALSE; - return BN_nnmod(r, a, m, ctx); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_nnmod(r, a, m, ctx.get()); } -/** - * public static native int BN_mod_exp(int, int, int, int, int) - */ -static jboolean NativeBN_BN_mod_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p, BIGNUM* m, BN_CTX* ctx) { +static jboolean NativeBN_BN_mod_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p, BIGNUM* m) { if (!fourValidHandles(env, r, a, p, m)) return JNI_FALSE; - return BN_mod_exp(r, a, p, m, ctx); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_mod_exp(r, a, p, m, ctx.get()); } - -/** - * public static native int BN_mod_inverse(int, int, int, int) - */ -static jboolean NativeBN_BN_mod_inverse(JNIEnv* env, jclass, BIGNUM* ret, BIGNUM* a, BIGNUM* n, BN_CTX* ctx) { +static jboolean NativeBN_BN_mod_inverse(JNIEnv* env, jclass, BIGNUM* ret, BIGNUM* a, BIGNUM* n) { if (!threeValidHandles(env, ret, a, n)) return JNI_FALSE; - return (BN_mod_inverse(ret, a, n, ctx) != NULL); + Unique_BN_CTX ctx(BN_CTX_new()); + return (BN_mod_inverse(ret, a, n, ctx.get()) != NULL); } - -/** - * public static native int BN_generate_prime_ex(int, int, boolean, int, int, int) - */ static jboolean NativeBN_BN_generate_prime_ex(JNIEnv* env, jclass, BIGNUM* ret, int bits, jboolean safe, BIGNUM* add, BIGNUM* rem, jint cb) { if (!oneValidHandle(env, ret)) return JNI_FALSE; return BN_generate_prime_ex(ret, bits, safe, add, rem, (BN_GENCB*) cb); } -/** - * public static native int BN_mod_inverse(int, int, int, int) - */ -static jboolean NativeBN_BN_is_prime_ex(JNIEnv* env, jclass, BIGNUM* p, int nchecks, BN_CTX* ctx, jint cb) { +static jboolean NativeBN_BN_is_prime_ex(JNIEnv* env, jclass, BIGNUM* p, int nchecks, jint cb) { if (!oneValidHandle(env, p)) return JNI_FALSE; - return BN_is_prime_ex(p, nchecks, ctx, (BN_GENCB*) cb); + Unique_BN_CTX ctx(BN_CTX_new()); + return BN_is_prime_ex(p, nchecks, ctx.get(), (BN_GENCB*) cb); } -static JNINativeMethod METHODS[] = { - { "ERR_get_error", "()I", (void*)NativeBN_ERR_get_error }, - { "ERR_error_string", "(I)Ljava/lang/String;", (void*)NativeBN_ERR_error_string }, - { "BN_CTX_new", "()I", (void*)NativeBN_BN_CTX_new }, - { "BN_new", "()I", (void*)NativeBN_BN_new }, - { "BN_free", "(I)V", (void*)NativeBN_BN_free }, +static JNINativeMethod gMethods[] = { + { "BN_add", "(III)Z", (void*)NativeBN_BN_add }, + { "BN_add_word", "(II)Z", (void*)NativeBN_BN_add_word }, + { "BN_bin2bn", "([BIZI)Z", (void*)NativeBN_BN_bin2bn }, + { "BN_bn2bin", "(I)[B", (void*)NativeBN_BN_bn2bin }, + { "BN_bn2dec", "(I)Ljava/lang/String;", (void*)NativeBN_BN_bn2dec }, + { "BN_bn2hex", "(I)Ljava/lang/String;", (void*)NativeBN_BN_bn2hex }, { "BN_cmp", "(II)I", (void*)NativeBN_BN_cmp }, { "BN_copy", "(II)Z", (void*)NativeBN_BN_copy }, - { "putLongInt", "(IJ)Z", (void*)NativeBN_putLongInt }, - { "putULongInt", "(IJZ)Z", (void*)NativeBN_putULongInt }, { "BN_dec2bn", "(ILjava/lang/String;)I", (void*)NativeBN_BN_dec2bn }, + { "BN_div", "(IIII)Z", (void*)NativeBN_BN_div }, + { "BN_div_word", "(II)I", (void*)NativeBN_BN_div_word }, + { "BN_exp", "(III)Z", (void*)NativeBN_BN_exp }, + { "BN_free", "(I)V", (void*)NativeBN_BN_free }, + { "BN_gcd", "(III)Z", (void*)NativeBN_BN_gcd }, + { "BN_generate_prime_ex", "(IIZIII)Z", (void*)NativeBN_BN_generate_prime_ex }, { "BN_hex2bn", "(ILjava/lang/String;)I", (void*)NativeBN_BN_hex2bn }, - { "BN_bin2bn", "([BIZI)Z", (void*)NativeBN_BN_bin2bn }, - { "litEndInts2bn", "([IIZI)Z", (void*)NativeBN_litEndInts2bn }, - { "twosComp2bn", "([BII)Z", (void*)NativeBN_twosComp2bn }, - { "longInt", "(I)J", (void*)NativeBN_longInt }, - { "BN_bn2dec", "(I)Ljava/lang/String;", (void*)NativeBN_BN_bn2dec }, - { "BN_bn2hex", "(I)Ljava/lang/String;", (void*)NativeBN_BN_bn2hex }, - { "BN_bn2bin", "(I)[B", (void*)NativeBN_BN_bn2bin }, - { "bn2litEndInts", "(I)[I", (void*)NativeBN_bn2litEndInts }, - { "sign", "(I)I", (void*)NativeBN_sign }, - { "BN_set_negative", "(II)V", (void*)NativeBN_BN_set_negative }, - { "bitLength", "(I)I", (void*)NativeBN_bitLength }, { "BN_is_bit_set", "(II)Z", (void*)NativeBN_BN_is_bit_set }, - { "modifyBit", "(III)Z", (void*)NativeBN_modifyBit }, - { "BN_shift", "(III)Z", (void*)NativeBN_BN_shift }, - { "BN_add_word", "(II)Z", (void*)NativeBN_BN_add_word }, - { "BN_sub_word", "(II)Z", (void*)NativeBN_BN_sub_word }, - { "BN_mul_word", "(II)Z", (void*)NativeBN_BN_mul_word }, - { "BN_div_word", "(II)I", (void*)NativeBN_BN_div_word }, + { "BN_is_prime_ex", "(III)Z", (void*)NativeBN_BN_is_prime_ex }, + { "BN_mod_exp", "(IIII)Z", (void*)NativeBN_BN_mod_exp }, + { "BN_mod_inverse", "(III)Z", (void*)NativeBN_BN_mod_inverse }, { "BN_mod_word", "(II)I", (void*)NativeBN_BN_mod_word }, - { "BN_add", "(III)Z", (void*)NativeBN_BN_add }, + { "BN_mul", "(III)Z", (void*)NativeBN_BN_mul }, + { "BN_mul_word", "(II)Z", (void*)NativeBN_BN_mul_word }, + { "BN_new", "()I", (void*)NativeBN_BN_new }, + { "BN_nnmod", "(III)Z", (void*)NativeBN_BN_nnmod }, + { "BN_set_negative", "(II)V", (void*)NativeBN_BN_set_negative }, + { "BN_shift", "(III)Z", (void*)NativeBN_BN_shift }, { "BN_sub", "(III)Z", (void*)NativeBN_BN_sub }, - { "BN_gcd", "(IIII)Z", (void*)NativeBN_BN_gcd }, - { "BN_mul", "(IIII)Z", (void*)NativeBN_BN_mul }, - { "BN_exp", "(IIII)Z", (void*)NativeBN_BN_exp }, - { "BN_div", "(IIIII)Z", (void*)NativeBN_BN_div }, - { "BN_nnmod", "(IIII)Z", (void*)NativeBN_BN_nnmod }, - { "BN_mod_exp", "(IIIII)Z", (void*)NativeBN_BN_mod_exp }, - { "BN_mod_inverse", "(IIII)Z", (void*)NativeBN_BN_mod_inverse }, - { "BN_generate_prime_ex", "(IIZIII)Z", (void*)NativeBN_BN_generate_prime_ex }, - { "BN_is_prime_ex", "(IIII)Z", (void*)NativeBN_BN_is_prime_ex } + { "BN_sub_word", "(II)Z", (void*)NativeBN_BN_sub_word }, + { "ERR_error_string", "(I)Ljava/lang/String;", (void*)NativeBN_ERR_error_string }, + { "ERR_get_error", "()I", (void*)NativeBN_ERR_get_error }, + { "bitLength", "(I)I", (void*)NativeBN_bitLength }, + { "bn2litEndInts", "(I)[I", (void*)NativeBN_bn2litEndInts }, + { "litEndInts2bn", "([IIZI)Z", (void*)NativeBN_litEndInts2bn }, + { "longInt", "(I)J", (void*)NativeBN_longInt }, + { "modifyBit", "(III)Z", (void*)NativeBN_modifyBit }, + { "putLongInt", "(IJ)Z", (void*)NativeBN_putLongInt }, + { "putULongInt", "(IJZ)Z", (void*)NativeBN_putULongInt }, + { "sign", "(I)I", (void*)NativeBN_sign }, + { "twosComp2bn", "([BII)Z", (void*)NativeBN_twosComp2bn }, }; int register_org_openssl_NativeBN(JNIEnv* env) { - return jniRegisterNativeMethods(env, "org/openssl/NativeBN", METHODS, NELEM(METHODS)); + return jniRegisterNativeMethods(env, "org/openssl/NativeBN", gMethods, NELEM(gMethods)); } |