JNI: synchronize wc_ecc_sign_hash() on rngLock, add sanity check for wc_ecc_sig_size()
parent
3198d3e8da
commit
30a1916233
|
@ -821,7 +821,9 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1sign_1hash(
|
||||||
RNG* rng = NULL;
|
RNG* rng = NULL;
|
||||||
byte* hash = NULL;
|
byte* hash = NULL;
|
||||||
byte* signature = NULL;
|
byte* signature = NULL;
|
||||||
word32 hashSz = 0, signatureSz = 0;
|
word32 hashSz = 0;
|
||||||
|
word32 expectedSigSz = 0;
|
||||||
|
word32 signatureSz = 0;
|
||||||
word32 signatureBufSz = 0;
|
word32 signatureBufSz = 0;
|
||||||
|
|
||||||
ecc = (ecc_key*) getNativeStruct(env, this);
|
ecc = (ecc_key*) getNativeStruct(env, this);
|
||||||
|
@ -844,7 +846,8 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1sign_1hash(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
signatureSz = wc_ecc_sig_size(ecc);
|
expectedSigSz = wc_ecc_sig_size(ecc);
|
||||||
|
signatureSz = expectedSigSz;
|
||||||
signatureBufSz = signatureSz;
|
signatureBufSz = signatureSz;
|
||||||
|
|
||||||
signature = (byte*)XMALLOC(signatureSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
signature = (byte*)XMALLOC(signatureSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
@ -860,25 +863,39 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1sign_1hash(
|
||||||
ret = wc_ecc_sign_hash(hash, hashSz, signature, &signatureSz, rng, ecc);
|
ret = wc_ecc_sign_hash(hash, hashSz, signature, &signatureSz, rng, ecc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ret == 0) {
|
||||||
|
/* Sanity check on wc_ecc_sig_size() and actual length */
|
||||||
|
if (expectedSigSz < signatureSz) {
|
||||||
|
ret = BUFFER_E;
|
||||||
|
throwWolfCryptException(env,
|
||||||
|
"wc_ecc_sig_size() less than actual sig size");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
result = (*env)->NewByteArray(env, signatureSz);
|
result = (*env)->NewByteArray(env, signatureSz);
|
||||||
|
|
||||||
if (result) {
|
if (result != NULL) {
|
||||||
(*env)->SetByteArrayRegion(env, result, 0, signatureSz,
|
(*env)->SetByteArrayRegion(env, result, 0, signatureSz,
|
||||||
(const jbyte*)signature);
|
(const jbyte*)signature);
|
||||||
} else {
|
} else {
|
||||||
|
releaseByteArray(env, hash_object, hash, JNI_ABORT);
|
||||||
throwWolfCryptException(env, "Failed to allocate signature");
|
throwWolfCryptException(env, "Failed to allocate signature");
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
releaseByteArray(env, hash_object, hash, JNI_ABORT);
|
||||||
throwWolfCryptExceptionFromError(env, ret);
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogStr("wc_ecc_sign_hash(input, inSz, output, &outSz, rng, ecc) = %d\n",
|
LogStr("wc_ecc_sign_hash(input, inSz, output, &outSz, rng, ecc) = %d\n",
|
||||||
ret);
|
ret);
|
||||||
LogStr("signature[%u]: [%p]\n", (word32)signatureSz, signature);
|
|
||||||
LogHex((byte*) signature, 0, signatureSz);
|
|
||||||
|
|
||||||
if (signature != NULL) {
|
if (signature != NULL) {
|
||||||
|
LogStr("signature[%u]: [%p]\n", (word32)signatureSz, signature);
|
||||||
|
LogHex((byte*) signature, 0, signatureSz);
|
||||||
|
|
||||||
XMEMSET(signature, 0, signatureBufSz);
|
XMEMSET(signature, 0, signatureBufSz);
|
||||||
XFREE(signature, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
XFREE(signature, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,9 +151,10 @@ byte* getByteArray(JNIEnv* env, jbyteArray array)
|
||||||
|
|
||||||
void releaseByteArray(JNIEnv* env, jbyteArray array, byte* elements, jint abort)
|
void releaseByteArray(JNIEnv* env, jbyteArray array, byte* elements, jint abort)
|
||||||
{
|
{
|
||||||
if (elements)
|
if ((env != NULL) && (array != NULL) && (elements != NULL)) {
|
||||||
(*env)->ReleaseByteArrayElements(env, array, (jbyte*) elements,
|
(*env)->ReleaseByteArrayElements(env, array, (jbyte*) elements,
|
||||||
abort ? JNI_ABORT : 0);
|
abort ? JNI_ABORT : 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
word32 getByteArrayLength(JNIEnv* env, jbyteArray array)
|
word32 getByteArrayLength(JNIEnv* env, jbyteArray array)
|
||||||
|
|
|
@ -500,7 +500,9 @@ public class Ecc extends NativeStruct {
|
||||||
synchronized (stateLock) {
|
synchronized (stateLock) {
|
||||||
if (state == WolfCryptState.READY) {
|
if (state == WolfCryptState.READY) {
|
||||||
synchronized (pointerLock) {
|
synchronized (pointerLock) {
|
||||||
signature = wc_ecc_sign_hash(hash, rng);
|
synchronized (rngLock) {
|
||||||
|
signature = wc_ecc_sign_hash(hash, rng);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
|
|
Loading…
Reference in New Issue