[AndroidCrypto] Make RC2 transform explicitly throw PlatformNotSupportedException...
authorElinor Fung <elfung@microsoft.com>
Fri, 5 Mar 2021 20:53:53 +0000 (12:53 -0800)
committerGitHub <noreply@github.com>
Fri, 5 Mar 2021 20:53:53 +0000 (12:53 -0800)
src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_cipher.c
src/libraries/Native/Unix/System.Security.Cryptography.Native.Android/pal_cipher.h
src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Android.cs [new file with mode: 0644]
src/libraries/System.Security.Cryptography.Algorithms/src/Resources/Strings.resx
src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj

index f9c624a..99b2fd5 100644 (file)
@@ -7,9 +7,8 @@
 enum
 {
     CIPHER_NONE = 0,
-    CIPHER_IS_SUPPORTED = 1,
-    CIPHER_HAS_TAG = 2,
-    CIPHER_REQUIRES_IV = 4,
+    CIPHER_HAS_TAG = 1,
+    CIPHER_REQUIRES_IV = 2,
 };
 typedef uint32_t CipherFlags;
 
@@ -23,14 +22,7 @@ typedef struct CipherInfo
 #define DEFINE_CIPHER(cipherId, width, javaName, flags) \
 CipherInfo* AndroidCryptoNative_ ## cipherId() \
 { \
-    static CipherInfo info = { flags | CIPHER_IS_SUPPORTED, width, javaName }; \
-    return &info; \
-}
-
-#define DEFINE_UNSUPPORTED_CIPHER(cipherId) \
-CipherInfo* AndroidCryptoNative_ ## cipherId() \
-{ \
-    static CipherInfo info = { CIPHER_NONE, 0, NULL }; \
+    static CipherInfo info = { flags, width, javaName }; \
     return &info; \
 }
 
@@ -59,14 +51,6 @@ DEFINE_CIPHER(Des3Ecb,      128, "DESede/ECB/NoPadding", CIPHER_NONE)
 DEFINE_CIPHER(Des3Cbc,      128, "DESede/CBC/NoPadding", CIPHER_REQUIRES_IV)
 DEFINE_CIPHER(Des3Cfb8,     128, "DESede/CFB8/NoPadding", CIPHER_REQUIRES_IV)
 DEFINE_CIPHER(Des3Cfb64,    128, "DESede/CFB/NoPadding", CIPHER_REQUIRES_IV)
-DEFINE_UNSUPPORTED_CIPHER(RC2Ecb)
-DEFINE_UNSUPPORTED_CIPHER(RC2Cbc)
-
-
-static bool IsSupported(CipherInfo* type)
-{
-    return (type->flags & CIPHER_IS_SUPPORTED) == CIPHER_IS_SUPPORTED;
-}
 
 static bool HasTag(CipherInfo* type)
 {
@@ -78,24 +62,8 @@ static bool RequiresIV(CipherInfo* type)
     return (type->flags & CIPHER_REQUIRES_IV) == CIPHER_REQUIRES_IV;
 }
 
-static int32_t GetAlgorithmDefaultWidth(CipherInfo* type)
-{
-    if (!IsSupported(type))
-    {
-        assert(false);
-        return FAIL;
-    }
-    return type->width;
-}
-
 static jobject GetAlgorithmName(JNIEnv* env, CipherInfo* type)
 {
-    if (!IsSupported(type))
-    {
-        LOG_ERROR("This cipher is not supported");
-        assert(false);
-        return FAIL;
-    }
     return JSTRING(type->name);
 }
 
@@ -118,7 +86,7 @@ CipherCtx* AndroidCryptoNative_CipherCreatePartial(CipherInfo* type)
     ctx->cipher = cipher;
     ctx->type = type;
     ctx->tagLength = TAG_MAX_LENGTH;
-    ctx->keySizeInBits = GetAlgorithmDefaultWidth(type);
+    ctx->keySizeInBits = type->width;
     ctx->ivLength = 0;
     ctx->encMode = 0;
     ctx->key = NULL;
index 0a79c48..b9b9e1e 100644 (file)
@@ -60,5 +60,3 @@ PALEXPORT CipherInfo* AndroidCryptoNative_Des3Cfb64(void);
 PALEXPORT CipherInfo* AndroidCryptoNative_DesEcb(void);
 PALEXPORT CipherInfo* AndroidCryptoNative_DesCfb8(void);
 PALEXPORT CipherInfo* AndroidCryptoNative_DesCbc(void);
-PALEXPORT CipherInfo* AndroidCryptoNative_RC2Ecb(void);
-PALEXPORT CipherInfo* AndroidCryptoNative_RC2Cbc(void);
diff --git a/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Android.cs b/src/libraries/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RC2Implementation.Android.cs
new file mode 100644 (file)
index 0000000..fa5604a
--- /dev/null
@@ -0,0 +1,26 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Security.Cryptography;
+
+namespace Internal.Cryptography
+{
+    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350", Justification = "We are providing the implementation for RC2, not consuming it.")]
+    internal sealed partial class RC2Implementation : RC2
+    {
+        private static ICryptoTransform CreateTransformCore(
+            CipherMode cipherMode,
+            PaddingMode paddingMode,
+            byte[] key,
+            int effectiveKeyLength,
+            byte[]? iv,
+            int blockSize,
+            int feedbackSizeInBytes,
+            int paddingSize,
+            bool encrypting)
+        {
+            throw new PlatformNotSupportedException(SR.Format(SR.Cryptography_AlgorithmNotSupported, nameof(RC2)));
+        }
+    }
+}
index a0008c8..9d07bd5 100644 (file)
@@ -96,6 +96,9 @@
   <data name="Cryptography_AlgKdfRequiresChars" xml:space="preserve">
     <value>The KDF for algorithm '{0}' requires a char-based password input.</value>
   </data>
+  <data name="Cryptography_AlgorithmNotSupported" xml:space="preserve">
+    <value>Algorithm '{0}' is not supported on this platform.</value>
+  </data>
   <data name="Cryptography_ArgECDHKeySizeMismatch" xml:space="preserve">
     <value>The keys from both parties must be the same size to generate a secret agreement.</value>
   </data>
index c05dd9a..465a757 100644 (file)
     <Compile Include="Internal\Cryptography\OpenSslCipher.cs" />
     <Compile Condition="'$(TargetsBrowser)' != 'true'" Include="Internal\Cryptography\RandomNumberGeneratorImplementation.Unix.cs" />
     <Compile Condition="'$(TargetsBrowser)' != 'true'" Include="Internal\Cryptography\Pbkdf2Implementation.Unix.cs" />
-    <Compile Include="Internal\Cryptography\RC2Implementation.Unix.cs" />
     <Compile Include="Internal\Cryptography\TripleDesImplementation.Unix.cs" />
   </ItemGroup>
   <ItemGroup Condition="'$(UseAppleCrypto)' == 'true'">
              Link="Common\Microsoft\Win32\SafeHandles\SafeRsaHandle.Unix.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\RSAOpenSsl.cs"
              Link="Common\System\Security\Cryptography\RSAOpenSsl.cs" />
+    <Compile Include="Internal\Cryptography\RC2Implementation.Unix.cs" />
     <Compile Include="System\Security\Cryptography\ECDiffieHellman.Create.OpenSsl.cs" />
   </ItemGroup>
   <ItemGroup Condition="'$(TargetsUnix)' == 'true' and '$(UseAndroidCrypto)' == 'true'">
              Link="Common\System\Security\Cryptography\ECDsaAndroid.cs" />
     <Compile Include="$(CommonPath)System\Security\Cryptography\RSAAndroid.cs"
              Link="Common\System\Security\Cryptography\RSAAndroid.cs" />
+    <Compile Include="Internal\Cryptography\RC2Implementation.Android.cs" />
     <Compile Include="System\Security\Cryptography\AesCcm.Android.cs" />
     <Compile Include="System\Security\Cryptography\AesGcm.Android.cs" />
     <Compile Include="System\Security\Cryptography\ECDiffieHellmanAndroid.Derive.cs" />