extern "C" void Java_java_math_NativeBN_BN_1shift(JNIEnv* env, jclass, jlong r, jlong a, int n) { if (!twoValidHandles(env, r, a)) return; if (n >= 0) { BN_lshift(toBigNum(r), toBigNum(a), n); } else { BN_rshift(toBigNum(r), toBigNum(a), -n); } throwExceptionIfNecessary(env); }
static int NativeBN_BN_cmp(JNIEnv* env, jclass, jlong a, jlong b) { if (!twoValidHandles(env, a, b)) return 1; return BN_cmp(toBigNum(a), toBigNum(b)); }
static int threeValidHandles(JNIEnv* env, jlong a, jlong b, jlong c) { if (!twoValidHandles(env, a, b)) return JNI_FALSE; return isValidHandle(env, c, "Mandatory handle (third) passed as null"); }
static void NativeBN_BN_copy(JNIEnv* env, jclass, jlong to, jlong from) { if (!twoValidHandles(env, to, from)) return; BN_copy(toBigNum(to), toBigNum(from)); throwExceptionIfNecessary(env); }
static int threeValidHandles (JNIEnv* env, void* a, void *b, void* c) { if (!twoValidHandles(env, a, b)) return FALSE; return isValidHandle(env, c, "Mandatory handle (third) passed as null"); }
/** * public static native int BN_lshift(int, int, int) */ static jboolean NativeBN_BN_lshift(JNIEnv* env, jclass cls, BIGNUM* r, BIGNUM* a, int n) { // LOGD("NativeBN_BN_lshift %p %p %d", r, a, n); if (!twoValidHandles(env, r, a)) return FALSE; if (n >= 0) return BN_lshift(r, a, n); n = -n; // return BN_rshift(r, a, n); // Following code insourced from bn_shift.c in order to have bug fixed: // FIXME: Should report to openssl team!!! int i,j,nw,lb,rb; BN_ULONG *t,*f; BN_ULONG l,tmp; bn_check_top(r); bn_check_top(a); nw=n/BN_BITS2; rb=n%BN_BITS2; lb=BN_BITS2-rb; // Changed "nw > a->top || a->top == 0" to nw >= a->top" as considering this a bug: if (nw >= a->top) { BN_zero(r); return(1); } if (r != a) { r->neg=a->neg; if (bn_wexpand(r,a->top-nw+1) == NULL) return(0); } else { if (n == 0) return 1; /* or the copying loop will go berserk */ } f= &(a->d[nw]); t=r->d; j=a->top-nw; r->top=j; if (rb == 0) { for (i=j; i != 0; i--) *(t++)= *(f++); } else { l= *(f++); for (i=j-1; i != 0; i--) { tmp =(l>>rb)&BN_MASK2; l= *(f++); *(t++) =(tmp|(l<<lb))&BN_MASK2; } *(t++) =(l>>rb)&BN_MASK2; } bn_correct_top(r); bn_check_top(r); return(1); }
/** * public static native int BN_copy(int, int) */ static jboolean NativeBN_BN_copy(JNIEnv* env, jclass cls, BIGNUM* to, BIGNUM* from) { if (!twoValidHandles(env, to, from)) return FALSE; return (BN_copy(to, from) != NULL); }
/** * public static native int BN_cmp(int, int) */ static int NativeBN_BN_cmp(JNIEnv* env, jclass cls, BIGNUM* a, BIGNUM* b) { if (!twoValidHandles(env, a, b)) return 1; return BN_cmp(a, b); }
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); }
extern "C" void Java_java_math_NativeBN_BN_1copy(JNIEnv* env, jclass, jlong to, jlong from) { if (!twoValidHandles(env, to, from)) return; BN_copy(toBigNum(to), toBigNum(from)); throwExceptionIfNecessary(env); }
extern "C" int Java_java_math_NativeBN_BN_1cmp(JNIEnv* env, jclass, jlong a, jlong b) { if (!twoValidHandles(env, a, b)) return 1; return BN_cmp(toBigNum(a), toBigNum(b)); }