1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "NativeBN"
#include <stdio.h>
#include <algorithm>
#include <memory>
#include <openssl/bn.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
#include <nativehelper/jni_macros.h>
#include "JniException.h"
struct BN_CTX_Deleter {
void operator()(BN_CTX* p) const {
BN_CTX_free(p);
}
};
typedef std::unique_ptr<BN_CTX, BN_CTX_Deleter> Unique_BN_CTX;
static BIGNUM* toBigNum(jlong address) {
return reinterpret_cast<BIGNUM*>(static_cast<uintptr_t>(address));
}
// Exception handling: We follow the usual JNI convention of "throwing" an
// exception if anything goes wrong, and returning junk, typically null.
// The NativeBN_ routines should only be called from Java, or from code
// that immediately returns the result to Java, and thus the
// Java exception should be thrown before we ever see the junk.
// This null BNs should never become visible, and we do not have to deal with
// junk (nulls) as input.
static void throwException(JNIEnv* env) {
long error = ERR_get_error();
// OpenSSL's error queue may contain multiple errors. Clean up after them.
ERR_clear_error();
if (error == 0) {
// An operation failed but did not push to the error queue. Throw a default
// exception.
jniThrowException(env, "java/lang/ArithmeticException", "Operation failed");
return;
}
char message[256];
ERR_error_string_n(error, message, sizeof(message));
int reason = ERR_GET_REASON(error);
if (reason == BN_R_DIV_BY_ZERO) {
jniThrowException(env, "java/lang/ArithmeticException", "BigInteger division by zero");
} else if (reason == BN_R_NO_INVERSE) {
jniThrowException(env, "java/lang/ArithmeticException", "BigInteger not invertible");
} else if (reason == ERR_R_MALLOC_FAILURE) {
jniThrowOutOfMemoryError(env, message);
} else {
jniThrowException(env, "java/lang/ArithmeticException", message);
}
}
static jlong NativeBN_BN_new(JNIEnv* env, jclass) {
jlong result = static_cast<jlong>(reinterpret_cast<uintptr_t>(BN_new()));
if (!result) {
throwException(env);
}
return result;
}
static void NativeBN_BN_free(JNIEnv*, jclass, jlong a) {
// Do nothing on a zero argument.
BN_free(toBigNum(a));
}
static void NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int len, jboolean neg, jlong ret0) {
BIGNUM* ret = toBigNum(ret0);
ScopedIntArrayRO scopedArray(env, arr);
if (scopedArray.get() == NULL) {
return;
}
// We can simply interpret the little-endian integer stream as a
// little-endian byte stream and use BN_le2bn.
const uint8_t* tmpBytes = reinterpret_cast<const uint8_t *>(scopedArray.get());
size_t numBytes = len * sizeof(int);
if (!BN_le2bn(tmpBytes, numBytes, ret)) {
throwException(env);
}
BN_set_negative(ret, neg);
}
static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, jlong a0) {
BIGNUM* a = toBigNum(a0);
// The number of integers we need is BN_num_bytes(a) / sizeof(int), rounded up
int intLen = (BN_num_bytes(a) + sizeof(int) - 1) / sizeof(int);
// Allocate our result with the JNI boilerplate
jintArray result = env->NewIntArray(intLen);
if (result == NULL) {
throwException(env);
return NULL;
}
ScopedIntArrayRW ints(env, result);
unsigned int* uints = reinterpret_cast<unsigned int*>(ints.get());
if (uints == NULL) {
throwException(env);
return NULL;
}
// We can simply interpret a little-endian byte stream as a little-endian integer stream.
if (!BN_bn2le_padded(reinterpret_cast<uint8_t*>(uints), intLen * sizeof(int), a)) {
throwException(env);
return NULL;
}
return result;
}
static void NativeBN_BN_mul(JNIEnv* env, jclass, jlong r, jlong a, jlong b) {
Unique_BN_CTX ctx(BN_CTX_new());
BN_CTX* ctxp = ctx.get();
if (!ctxp || !BN_mul(toBigNum(r), toBigNum(a), toBigNum(b), ctxp)) {
throwException(env);
}
}
static void NativeBN_BN_div(JNIEnv* env, jclass, jlong q, jlong rem, jlong num, jlong divisor) {
Unique_BN_CTX ctx(BN_CTX_new());
BN_CTX* ctxp = ctx.get();
if (!ctxp || !BN_div(toBigNum(q), toBigNum(rem), toBigNum(num), toBigNum(divisor), ctxp)) {
throwException(env);
}
}
static void NativeBN_BN_mod_exp(JNIEnv* env, jclass, jlong r, jlong a, jlong p, jlong m) {
Unique_BN_CTX ctx(BN_CTX_new());
BN_CTX* ctxp = ctx.get();
if (!ctxp || !BN_mod_exp(toBigNum(r), toBigNum(a), toBigNum(p), toBigNum(m), ctxp)) {
throwException(env);
}
}
static JNINativeMethod gMethods[] = {
NATIVE_METHOD(NativeBN, BN_div, "(JJJJ)V"),
NATIVE_METHOD(NativeBN, BN_free, "(J)V"),
NATIVE_METHOD(NativeBN, BN_mod_exp, "(JJJJ)V"),
NATIVE_METHOD(NativeBN, BN_mul, "(JJJ)V"),
NATIVE_METHOD(NativeBN, BN_new, "()J"),
NATIVE_METHOD(NativeBN, bn2litEndInts, "(J)[I"),
NATIVE_METHOD(NativeBN, litEndInts2bn, "([IIZJ)V"),
};
void register_libcore_math_NativeBN(JNIEnv* env) {
jniRegisterNativeMethods(env, "libcore/math/NativeBN", gMethods, NELEM(gMethods));
}
|