diff --git a/src/libraries/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Bignum.cs b/src/libraries/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Bignum.cs index 0ceb04a8715720..5936cee56d6d33 100644 --- a/src/libraries/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Bignum.cs +++ b/src/libraries/Common/src/Interop/Android/System.Security.Cryptography.Native.Android/Interop.Bignum.cs @@ -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 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) diff --git a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.c b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.c index 684cf6f8b4c571..6280b4fd5c0073 100644 --- a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.c +++ b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.c @@ -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() @@ -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. diff --git a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.h b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.h index ed2b9de58dd4fa..0abfc619f446be 100644 --- a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.h +++ b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_bignum.h @@ -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); diff --git a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_dsa.c b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_dsa.c index 52709e9fff49e3..eb9190de8faccd 100644 --- a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_dsa.c +++ b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_dsa.c @@ -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; diff --git a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_ecc_import_export.c b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_ecc_import_export.c index cd661731bb549c..e42845d260a931 100644 --- a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_ecc_import_export.c +++ b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_ecc_import_export.c @@ -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; } @@ -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; } diff --git a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.c b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.c index 03307ed7ed0642..54686329647305 100644 --- a/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.c +++ b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.c @@ -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; }