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 package java.math; 18 19 final class NativeBN { 20 21 public static native int ERR_get_error(); 22 // unsigned long ERR_get_error(void); 23 24 public static native String ERR_error_string(int e); 25 // char *ERR_error_string(unsigned long e, char *buf); 26 27 public static native int BN_new(); 28 // BIGNUM *BN_new(void); 29 30 public static native void BN_free(int a); 31 // void BN_free(BIGNUM *a); 32 33 public static native int BN_cmp(int a, int b); 34 // int BN_cmp(const BIGNUM *a, const BIGNUM *b); 35 36 public static native boolean BN_copy(int to, int from); 37 // Returns boolean success AND NOT result BIGNUM handle! 38 // BIGNUM *BN_copy(BIGNUM *to, const BIGNUM *from); 39 // public static native int BN_dup(int from); 40 // BIGNUM *BN_dup(const BIGNUM *a); 41 42 43 public static native boolean putLongInt(int a, long dw); 44 45 public static native boolean putULongInt(int a, long dw, boolean neg); 46 47 public static native int BN_dec2bn(int a, String str); 48 // int BN_dec2bn(BIGNUM **a, const char *str); 49 50 public static native int BN_hex2bn(int a, String str); 51 // int BN_hex2bn(BIGNUM **a, const char *str); 52 53 public static native boolean BN_bin2bn(byte[] s, int len, boolean neg, int ret); 54 // Returns boolean success AND NOT result BIGNUM handle! 55 // BIGNUM * BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret); 56 // BN-Docu: s is taken as unsigned big endian; 57 // Additional parameter: neg. 58 59 public static native boolean litEndInts2bn(int[] ints, int len, boolean neg, int ret); 60 61 public static native boolean twosComp2bn(byte[] s, int len, int ret); 62 63 64 public static native long longInt(int a); 65 // unsigned long BN_get_word(BIGNUM *a); 66 67 public static native String BN_bn2dec(int a); 68 // char * BN_bn2dec(const BIGNUM *a); 69 70 public static native String BN_bn2hex(int a); 71 // char * BN_bn2hex(const BIGNUM *a); 72 73 public static native byte[] BN_bn2bin(int a); 74 // Returns result byte[] AND NOT length. 75 // int BN_bn2bin(const BIGNUM *a, unsigned char *to); 76 77 public static native int[] bn2litEndInts(int a); 78 79 public static native int sign(int a); 80 // Returns -1, 0, 1 AND NOT boolean. 81 // #define BN_is_negative(a) ((a)->neg != 0) 82 83 public static native void BN_set_negative(int b, int n); 84 // void BN_set_negative(BIGNUM *b, int n); 85 86 public static native int bitLength(int a); 87 88 public static native boolean BN_is_bit_set(int a, int n); 89 // int BN_is_bit_set(const BIGNUM *a, int n); 90 91 public static native boolean modifyBit(int a, int n, int op); 92 // Returns boolean success. 93 // op: 0 = reset; 1 = set; -1 = flip 94 // uses BN_set_bit(), BN_clear_bit() and BN_is_bit_set() 95 96 public static native boolean BN_shift(int r, int a, int n); 97 // int BN_shift(BIGNUM *r, const BIGNUM *a, int n); 98 99 public static native boolean BN_add_word(int a, int w); 100 // ATTENTION: w is treated as unsigned. 101 // int BN_add_word(BIGNUM *a, BN_ULONG w); 102 103 public static native boolean BN_sub_word(int a, int w); 104 // ATTENTION: w is treated as unsigned. 105 // int BN_sub_word(BIGNUM *a, BN_ULONG w); 106 107 public static native boolean BN_mul_word(int a, int w); 108 // ATTENTION: w is treated as unsigned. 109 // int BN_mul_word(BIGNUM *a, BN_ULONG w); 110 111 public static native int BN_div_word(int a, int w); 112 // ATTENTION: w is treated as unsigned. 113 // BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w); 114 115 public static native int BN_mod_word(int a, int w); 116 // ATTENTION: w is treated as unsigned. 117 // BN_ULONG BN_mod_word(BIGNUM *a, BN_ULONG w); 118 119 public static native boolean BN_add(int r, int a, int b); 120 // int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 121 122 public static native boolean BN_sub(int r, int a, int b); 123 // int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b); 124 125 public static native boolean BN_gcd(int r, int a, int b); 126 // int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 127 128 public static native boolean BN_mul(int r, int a, int b); 129 // int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); 130 131 public static native boolean BN_exp(int r, int a, int p); 132 // int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx); 133 134 // OPTIONAL: 135 // public static native int BN_sqr(BigInteger r, BigInteger a, BN_CTX ctx); 136 // int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx); 137 138 public static native boolean BN_div(int dv, int rem, int m, int d); 139 // int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx); 140 141 public static native boolean BN_nnmod(int r, int a, int m); 142 // int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 143 144 public static native boolean BN_mod_exp(int r, int a, int p, int m); 145 // int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx); 146 147 // OPTIONAL: 148 // public static native boolean BN_mod_sqr(BigInteger r, BigInteger a, BigInteger m, BN_CTX ctx); 149 // int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx); 150 151 152 public static native boolean BN_mod_inverse(int ret, int a, int n); 153 // Returns boolean success AND NOT result BIGNUM handle! 154 // BIGNUM * BN_mod_inverse(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx); 155 156 157 public static native boolean BN_generate_prime_ex(int ret, int bits, boolean safe, 158 int add, int rem, int cb); 159 // int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, 160 // const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb); 161 162 public static native boolean BN_is_prime_ex(int p, int nchecks, int cb); 163 // int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb); 164 165 } 166