Skip to content

Commit

Permalink
Backport of 773766f to release/6.0. (#77283)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vcsjones committed Nov 7, 2022
1 parent 15d285f commit c56b716
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace System.Security.Cryptography.Encryption.RC2.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
[ConditionalClass(typeof(RC2Factory), nameof(RC2Factory.IsSupported))]
public class RC2CipherOneShotTests : SymmetricOneShotBase
{
protected override byte[] Key => new byte[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ static jobject GetMessageDigestInstance(JNIEnv* env, intptr_t type)

int32_t CryptoNative_EvpDigestOneShot(intptr_t type, void* source, int32_t sourceSize, uint8_t* md, uint32_t* mdSize)
{
abort_if_invalid_pointer_argument (source);

if (!type || !md || !mdSize || sourceSize < 0)
if (!type || !md || !mdSize || sourceSize < 0 || (sourceSize > 0 && !source))
return FAIL;

JNIEnv* env = GetJNIEnv();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ int32_t CryptoNative_HmacReset(jobject ctx)

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

abort_if_invalid_pointer_argument (data);
JNIEnv* env = GetJNIEnv();
jbyteArray dataBytes = make_java_byte_array(env, len);
(*env)->SetByteArrayRegion(env, dataBytes, 0, len, (jbyte*)data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ PALEXPORT void AndroidCryptoNative_RsaDestroy(RSA* rsa)

PALEXPORT int32_t AndroidCryptoNative_RsaPublicEncrypt(int32_t flen, uint8_t* from, uint8_t* to, RSA* rsa, RsaPadding padding)
{
abort_if_invalid_pointer_argument (from);
abort_if_invalid_pointer_argument (to);
abort_if_invalid_pointer_argument (rsa);

if ((flen > 0 && !from) || flen < 0)
return RSA_FAIL;

JNIEnv* env = GetJNIEnv();

int32_t ret = RSA_FAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public EvpHashProvider(IntPtr algorithmEvp)

public override void AppendHashData(ReadOnlySpan<byte> data)
{
if (data.IsEmpty)
{
return;
}

_running = true;
Check(Interop.Crypto.EvpDigestUpdate(_ctx, data, data.Length));
}
Expand Down Expand Up @@ -166,6 +171,11 @@ public HmacHashProvider(IntPtr algorithmEvp, ReadOnlySpan<byte> key)

public override void AppendHashData(ReadOnlySpan<byte> data)
{
if (data.IsEmpty)
{
return;
}

_running = true;
Check(Interop.Crypto.HmacUpdate(_hmacCtx, data, data.Length));
}
Expand Down

0 comments on commit c56b716

Please sign in to comment.