runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Generate UUID-5 in FIPS-140-3 mode using SHA-1\MD-5 (RFC-4122)

Open krishnan-unni opened this issue 3 years ago • 6 comments

Description

According to RFC-4122 (https://www.rfc-editor.org/rfc/rfc4122#section-4.3), it is recommended to use SHA-1 or MD-5 for hashing if we need to generate a UUID-5 (name based).

As per the US government FIPS-140-3, it is not allowed to use SHA-1 for security-related cryptographic purposes but for non-security purposes.

Now, it works fine, but if we turn on the FIPS mode in Windows, the SHA-1\MD-5 functions won't be available, and it will result in an InvalidOperationException.

What is the alternative to this? Is there a FIPS compliant module that we can target to both net48 and net6.0-windows

Reproduction Steps

The below code would work when FIPS is turned OFF, but it throws an InvalidOperationException when FIPS is turned ON.

using System;
using System.Security.Cryptography;

public class Program
{
    public static void Main()
    {
        try
        {
            using (SHA1Managed sha1 = new SHA1Managed())
            {
                byte[] data = System.Text.Encoding.ASCII.GetBytes("test data");
                byte[] hashData = sha1.ComputeHash(data);
                string hash = Convert.ToBase64String(hashData);
                Console.WriteLine(hash);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Expected behavior

If SHA-1 doesn't work, we need an alternative for UUID-5 generation adhering to RFC-4122.

Actual behavior

SHA-1 has a blanket ban when FIPS is ON even if it is for simple UUID 5 generation.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

krishnan-unni avatar Jun 21 '23 20:06 krishnan-unni

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones See info in area-owners.md if you want to be subscribed.

Issue Details

Description

According to RFC-4122 (https://www.rfc-editor.org/rfc/rfc4122#section-4.3), it is recommended to use SHA-1 or MD-5 for hashing if we need to generate a UUID-5 (name based).

As per the US government FIPS-140-3, it is not allowed to use SHA-1 for security-related cryptographic purposes but for non-security purposes.

Now, it works fine, but if we turn on the FIPS mode in Windows, the SHA-1\MD-5 functions won't be available, and it will result in an InvalidOperationException.

What is the alternative to this? Is there a FIPS compliant module that we can target to both net48 and net6.0-windows

Reproduction Steps

The below code would work when FIPS is turned OFF, but it throws an InvalidOperationException when FIPS is turned ON.

using System;
using System.Security.Cryptography;

public class Program
{
    public static void Main()
    {
        try
        {
            using (SHA1Managed sha1 = new SHA1Managed())
            {
                byte[] data = System.Text.Encoding.ASCII.GetBytes("test data");
                byte[] hashData = sha1.ComputeHash(data);
                string hash = Convert.ToBase64String(hashData);
                Console.WriteLine(hash);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Expected behavior

If SHA-1 doesn't work, we need an alternative for UUID-5 generation adhering to RFC-4122.

Actual behavior

SHA-1 has a blanket ban when FIPS is ON even if it is for simple UUID 5 generation.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: krishnan-unni
Assignees: -
Labels:

area-System.Security

Milestone: -

ghost avatar Jun 21 '23 20:06 ghost

.NET doesn't enforce FIPS algorithms. Are you, perhaps, using Red Hat Enterprise Linux and the system cryptography policy is banning the use of SHA-1? If so, you'll have to configure the RHEL crypto policy.

bartonjs avatar Jun 21 '23 22:06 bartonjs

.NET doesn't enforce FIPS algorithms

Based on

both net48

It looks like they are targeting .NET Framework. I presume .NET Framework still does FIPS enforcement?

vcsjones avatar Jun 22 '23 02:06 vcsjones

I presume .NET Framework still does FIPS enforcement?

Not if the application is targeting .NET 4.8 or higher. https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/retargeting/4.7.2-4.8#managed-cryptography-classes-do-not-throw-a-cryptographyexception-in-fips-mode

I was also going to say "SHA-1 was never blocked by the .NET FIPS enforcement policy, since it was defined back in the netfx20 days", but I see that the code here is explicitly using SHA1Managed. SHA1Managed would be blocked by the FIPS policy (as managed implementations can never be certified), but net48+ (or opting in to the net48 behavior) avoids this by making SHA1Managed defer to SHA1CSP when FIPS mode is turned on in the OS.

But since .NET Core (and thus .NET 6) has never had any FIPS enforcement code in it (or actual managed implementations, except on Browser/WASM), the fact that it was also reporting against net6 is where I got confused.

@krishnan-unni You should generally avoid caring what provider you get. Instead of using (SHA1Managed sha1 = new SHA1Managed()) you should write using (SHA1 sha1 = SHA1.Create()). It's pretty much always the better code to write.

bartonjs avatar Jun 22 '23 02:06 bartonjs

@bartonjs Let me correct that initial statement. Found out that I had set it to 4.8, but it was targeting 4.7.2 since it was getting overridden at the project level.

Here is a screenshot while using MD5 in 4.7.2. I don't get this error in 4.8.

image

The issue is with MD5 (not SHA-1) and .Net 4.7.2 (not 4.8). Based on what I see here: https://learn.microsoft.com/en-us/windows/security/threat-protection/fips-140-validation Windows currently is validated only for FIPS-140-2 (not 140-3). That is why SHA1 is not throwing errors like MD5 (like you stated above).

So the question is:

  • Is there a timeline or plan for Windows validating to enforce FIPS 140-3?
  • If we turn on FIPS, is there an alternative rather than updating the application to 4.8 to continue using SHA1\MD5 for UUID generation (or other non-security related) purposes?

And yes, I agree, we should almost always use SHA1. The posted code was for producing the issue as part of the security validation test.

krishnan-unni avatar Jul 14 '23 14:07 krishnan-unni

Is there a timeline or plan for Windows validating to enforce FIPS 140-3?

This may be related https://learn.microsoft.com/en-us/azure/compliance/offerings/offering-fips-140-2.

If we turn on FIPS, is there an alternative rather than updating the application to 4.8 to continue using SHA1\MD5 for UUID generation (or other non-security related) purposes?

I think the alternative was mentioned on https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/retargeting/4.7.2-4.8#suggestion.

If your application targets .NET Framework 4.7.2 or earlier, you can also opt in to this change by adding the following AppContextSwitchOverrides configuration setting to the section of your application configuration file:

<runtime>
  <AppContextSwitchOverrides value="Switch.System.Security.Cryptography.UseLegacyFipsThrow=false" />
</runtime>

jozkee avatar Jul 24 '23 16:07 jozkee