Skip to content

Commit c56b716

Browse files
authored
Backport of 773766f to release/6.0. (#77283)
Fix Android crypto asserts (#61827) This fixes three asserts that were started occurring in the native Android cryptographic primitives. - One shot hashing now tolerates empty/null input. - Hashing and HMAC will now no-op if the append is empty. - RSA encryption now tolerates empty/null input.
1 parent 15d285f commit c56b716

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RC2/RC2CipherOneShotTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace System.Security.Cryptography.Encryption.RC2.Tests
1111
{
1212
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
13+
[ConditionalClass(typeof(RC2Factory), nameof(RC2Factory.IsSupported))]
1314
public class RC2CipherOneShotTests : SymmetricOneShotBase
1415
{
1516
protected override byte[] Key => new byte[]

src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_evp.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ static jobject GetMessageDigestInstance(JNIEnv* env, intptr_t type)
5050

5151
int32_t CryptoNative_EvpDigestOneShot(intptr_t type, void* source, int32_t sourceSize, uint8_t* md, uint32_t* mdSize)
5252
{
53-
abort_if_invalid_pointer_argument (source);
54-
55-
if (!type || !md || !mdSize || sourceSize < 0)
53+
if (!type || !md || !mdSize || sourceSize < 0 || (sourceSize > 0 && !source))
5654
return FAIL;
5755

5856
JNIEnv* env = GetJNIEnv();

src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_hmac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ int32_t CryptoNative_HmacReset(jobject ctx)
8282

8383
int32_t CryptoNative_HmacUpdate(jobject ctx, uint8_t* data, int32_t len)
8484
{
85-
if (!ctx)
85+
// Callers are expected to skip update calls with no data.
86+
if (!ctx || !data || len <= 0)
8687
return FAIL;
8788

88-
abort_if_invalid_pointer_argument (data);
8989
JNIEnv* env = GetJNIEnv();
9090
jbyteArray dataBytes = make_java_byte_array(env, len);
9191
(*env)->SetByteArrayRegion(env, dataBytes, 0, len, (jbyte*)data);

src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_rsa.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ PALEXPORT void AndroidCryptoNative_RsaDestroy(RSA* rsa)
4444

4545
PALEXPORT int32_t AndroidCryptoNative_RsaPublicEncrypt(int32_t flen, uint8_t* from, uint8_t* to, RSA* rsa, RsaPadding padding)
4646
{
47-
abort_if_invalid_pointer_argument (from);
4847
abort_if_invalid_pointer_argument (to);
4948
abort_if_invalid_pointer_argument (rsa);
5049

50+
if ((flen > 0 && !from) || flen < 0)
51+
return RSA_FAIL;
52+
5153
JNIEnv* env = GetJNIEnv();
5254

5355
int32_t ret = RSA_FAIL;

src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public EvpHashProvider(IntPtr algorithmEvp)
9494

9595
public override void AppendHashData(ReadOnlySpan<byte> data)
9696
{
97+
if (data.IsEmpty)
98+
{
99+
return;
100+
}
101+
97102
_running = true;
98103
Check(Interop.Crypto.EvpDigestUpdate(_ctx, data, data.Length));
99104
}
@@ -166,6 +171,11 @@ public HmacHashProvider(IntPtr algorithmEvp, ReadOnlySpan<byte> key)
166171

167172
public override void AppendHashData(ReadOnlySpan<byte> data)
168173
{
174+
if (data.IsEmpty)
175+
{
176+
return;
177+
}
178+
169179
_running = true;
170180
Check(Interop.Crypto.HmacUpdate(_hmacCtx, data, data.Length));
171181
}

0 commit comments

Comments
 (0)