Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,12 @@ internal static partial class Interop
// TODO: [AndroidCrypto] Rename class to AndroidCrypto once all consumers are split in Android vs. Unix
internal static partial class Crypto
{
[DllImport(Libraries.CryptoNative, EntryPoint = "AndroidCryptoNative_BigNumFromBinary")]
private static extern unsafe SafeBignumHandle BigNumFromBinary(byte* s, int len);

[DllImport(Libraries.CryptoNative, EntryPoint = "AndroidCryptoNative_BigNumToBinary")]
private static extern unsafe int BigNumToBinary(SafeBignumHandle a, byte* to);

[DllImport(Libraries.CryptoNative, EntryPoint = "AndroidCryptoNative_GetBigNumBytes")]
private static extern int GetBigNumBytes(SafeBignumHandle a);

internal static SafeBignumHandle CreateBignum(ReadOnlySpan<byte> bigEndianValue)
{
unsafe
{
fixed (byte* pBigEndianValue = bigEndianValue)
{
SafeBignumHandle ret = BigNumFromBinary(pBigEndianValue, bigEndianValue.Length);
if (ret.IsInvalid)
{
ret.Dispose();
throw new CryptographicException();
}

return ret;
}
}
}

internal static unsafe byte[]? ExtractBignum(SafeBignumHandle? bignum, int targetSize)
{
if (bignum == null || bignum.IsInvalid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@

#include "pal_bignum.h"

jobject AndroidCryptoNative_BigNumFromBinary(uint8_t* bytes, int32_t len)
{
// return new BigInteger(bytes)
JNIEnv* env = GetJNIEnv();
jbyteArray buffArray = (*env)->NewByteArray(env, len);
(*env)->SetByteArrayRegion(env, buffArray, 0, len, (jbyte*)bytes);
jobject bigNum = (*env)->NewObject(env, g_bigNumClass, g_bigNumCtorWithSign, 1, buffArray);
(*env)->DeleteLocalRef(env, buffArray);
return CheckJNIExceptions(env) ? FAIL : ToGRef(env, bigNum);
}

int32_t AndroidCryptoNative_BigNumToBinary(jobject bignum, uint8_t* output)
{
// bigNum.toByteArray()
Expand Down Expand Up @@ -45,6 +34,17 @@ int32_t AndroidCryptoNative_GetBigNumBytes(jobject bignum)
return CheckJNIExceptions(env) ? FAIL : (int32_t)bytesLen;
}

jobject AndroidCryptoNative_BigNumFromBinary(uint8_t* bytes, int32_t len)
{
// return new BigInteger(bytes)
JNIEnv* env = GetJNIEnv();
jbyteArray buffArray = (*env)->NewByteArray(env, len);
(*env)->SetByteArrayRegion(env, buffArray, 0, len, (jbyte*)bytes);
jobject bigNum = (*env)->NewObject(env, g_bigNumClass, g_bigNumCtorWithSign, 1, buffArray);
(*env)->DeleteLocalRef(env, buffArray);
return CheckJNIExceptions(env) ? FAIL : bigNum;
}

int32_t AndroidCryptoNative_GetBigNumBytesIncludingPaddingByteForSign(jobject bignum)
{
// Use the array here to get the leading zero byte if it exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

#include "pal_jni.h"

PALEXPORT jobject AndroidCryptoNative_BigNumFromBinary(uint8_t* bytes, int32_t len);
PALEXPORT int32_t AndroidCryptoNative_BigNumToBinary(jobject bignum, uint8_t* output);
PALEXPORT int32_t AndroidCryptoNative_GetBigNumBytes(jobject bignum);

/*
Create a BigInteger from its binary representation.

The returned jobject will be a local reference.
*/
jobject AndroidCryptoNative_BigNumFromBinary(uint8_t* bytes, int32_t len);
int32_t AndroidCryptoNative_GetBigNumBytesIncludingPaddingByteForSign(jobject bignum);
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ int32_t AndroidCryptoNative_DsaKeyCreateByExplicitParameters(
error:
returnValue = FAIL;
cleanup:
RELEASE_LOCALS_ENV(bn, ReleaseGRef);
RELEASE_LOCALS_ENV(bn, ReleaseLRef);
RELEASE_LOCALS_ENV(loc, ReleaseLRef);

return returnValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static jobject AndroidCryptoNative_CreateKeyPairFromCurveParameters(
}

cleanup:
RELEASE_LOCALS_ENV(bn, ReleaseGRef);
RELEASE_LOCALS_ENV(bn, ReleaseLRef);
RELEASE_LOCALS_ENV(loc, ReleaseLRef);
return keyPair;
}
Expand Down Expand Up @@ -546,7 +546,7 @@ EC_KEY* AndroidCryptoNative_EcKeyCreateByExplicitParameters(ECCurveType curveTyp
keyInfo = AndroidCryptoNative_NewEcKey(AddGRef(env, loc[paramSpec]), keyPair);

error:
RELEASE_LOCALS_ENV(bn, ReleaseGRef);
RELEASE_LOCALS_ENV(bn, ReleaseLRef);
RELEASE_LOCALS_ENV(loc, ReleaseLRef);
return keyInfo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,51 +338,41 @@ PALEXPORT int32_t AndroidCryptoNative_SetRsaParameters(RSA* rsa,
return FAIL;

JNIEnv* env = GetJNIEnv();
INIT_LOCALS(bn, N, E, D, P, Q, DMP1, DMQ1, IQMP);
INIT_LOCALS(loc, algName, keyFactory, rsaPubKeySpec, rsaPrivateKeySpec);

jobject nObj = AndroidCryptoNative_BigNumFromBinary(n, nLength);
jobject eObj = AndroidCryptoNative_BigNumFromBinary(e, eLength);
bn[N] = AndroidCryptoNative_BigNumFromBinary(n, nLength);
bn[E] = AndroidCryptoNative_BigNumFromBinary(e, eLength);

rsa->keyWidthInBits = nLength * 8;

jobject algName = JSTRING("RSA");
jobject keyFactory = (*env)->CallStaticObjectMethod(env, g_KeyFactoryClass, g_KeyFactoryGetInstanceMethod, algName);
loc[algName] = JSTRING("RSA");
loc[keyFactory] = (*env)->CallStaticObjectMethod(env, g_KeyFactoryClass, g_KeyFactoryGetInstanceMethod, loc[algName]);

if (dLength > 0)
{
// private key section
jobject dObj = AndroidCryptoNative_BigNumFromBinary(d, dLength);
jobject pObj = AndroidCryptoNative_BigNumFromBinary(p, pLength);
jobject qObj = AndroidCryptoNative_BigNumFromBinary(q, qLength);
jobject dmp1Obj = AndroidCryptoNative_BigNumFromBinary(dmp1, dmp1Length);
jobject dmq1Obj = AndroidCryptoNative_BigNumFromBinary(dmq1, dmq1Length);
jobject iqmpObj = AndroidCryptoNative_BigNumFromBinary(iqmp, iqmpLength);
bn[D] = AndroidCryptoNative_BigNumFromBinary(d, dLength);
bn[P] = AndroidCryptoNative_BigNumFromBinary(p, pLength);
bn[Q] = AndroidCryptoNative_BigNumFromBinary(q, qLength);
bn[DMP1] = AndroidCryptoNative_BigNumFromBinary(dmp1, dmp1Length);
bn[DMQ1] = AndroidCryptoNative_BigNumFromBinary(dmq1, dmq1Length);
bn[IQMP] = AndroidCryptoNative_BigNumFromBinary(iqmp, iqmpLength);

jobject rsaPrivateKeySpec = (*env)->NewObject(env, g_RSAPrivateCrtKeySpecClass, g_RSAPrivateCrtKeySpecCtor,
nObj, eObj, dObj, pObj, qObj, dmp1Obj, dmq1Obj, iqmpObj);
loc[rsaPrivateKeySpec] = (*env)->NewObject(env, g_RSAPrivateCrtKeySpecClass, g_RSAPrivateCrtKeySpecCtor,
bn[N], bn[E], bn[D], bn[P], bn[Q], bn[DMP1], bn[DMQ1], bn[IQMP]);

ReleaseGRef(env, rsa->privateKey);
rsa->privateKey = ToGRef(env, (*env)->CallObjectMethod(env, keyFactory, g_KeyFactoryGenPrivateMethod, rsaPrivateKeySpec));

(*env)->DeleteGlobalRef(env, dObj);
(*env)->DeleteGlobalRef(env, pObj);
(*env)->DeleteGlobalRef(env, qObj);
(*env)->DeleteGlobalRef(env, dmp1Obj);
(*env)->DeleteGlobalRef(env, dmq1Obj);
(*env)->DeleteGlobalRef(env, iqmpObj);
(*env)->DeleteLocalRef(env, rsaPrivateKeySpec);
rsa->privateKey = ToGRef(env, (*env)->CallObjectMethod(env, loc[keyFactory], g_KeyFactoryGenPrivateMethod, loc[rsaPrivateKeySpec]));
}

jobject rsaPubKeySpec = (*env)->NewObject(env, g_RSAPublicCrtKeySpecClass, g_RSAPublicCrtKeySpecCtor, nObj, eObj);
loc[rsaPubKeySpec] = (*env)->NewObject(env, g_RSAPublicCrtKeySpecClass, g_RSAPublicCrtKeySpecCtor, bn[N], bn[E]);

ReleaseGRef(env, rsa->publicKey);
rsa->publicKey = ToGRef(env, (*env)->CallObjectMethod(env, keyFactory, g_KeyFactoryGenPublicMethod, rsaPubKeySpec));

(*env)->DeleteLocalRef(env, algName);
(*env)->DeleteLocalRef(env, keyFactory);
(*env)->DeleteGlobalRef(env, nObj);
(*env)->DeleteGlobalRef(env, eObj);
(*env)->DeleteLocalRef(env, rsaPubKeySpec);
rsa->publicKey = ToGRef(env, (*env)->CallObjectMethod(env, loc[keyFactory], g_KeyFactoryGenPublicMethod, loc[rsaPubKeySpec]));

RELEASE_LOCALS(bn, env);
RELEASE_LOCALS(loc, env);
return CheckJNIExceptions(env) ? FAIL : SUCCESS;
}

Expand Down