Thursday, 2 June 2016

FIPS compliant algorithms list in c#

First of all we should know the algorithm that available in our machine.
Code snippet to know all available algorithm in C#
Before running this on your machine make sure to ENABLE FIPS. Otherwise, you will get all ‘Y’ for COMPLIANT.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;//extra
using System.Reflection;//extra

namespace Know_All_FIPS_algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            Assembly core = Assembly.Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            Assembly mscorlib = Assembly.Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            
            Type[] subclasses = new Type[]
            {
                typeof(SymmetricAlgorithm),
                typeof(HashAlgorithm),
                typeof(AsymmetricAlgorithm)
            };

            Print(mscorlib, subclasses);
            Console.WriteLine();
            Console.WriteLine();

            Print(core, subclasses);
            Console.Read();

        }


        private static void Print(Assembly asm, Type[] subclasses)
        {

            string columnFormat = "{0,-35}{1,-15}{2}";
            Console.WriteLine("FIPS Compliant in {0}", asm.GetName());
            Console.WriteLine(columnFormat, "Name", "Compliant", "Subclass");

            foreach (Type type in asm.GetTypes())
            {

                foreach (Type subclass in subclasses)
                {

                    if (type.IsSubclassOf(subclass))
                    {

                        if (!type.IsAbstract)
                        {

                            string isCompliant = null;

                            try
                            {
                                Activator.CreateInstance(type);
                                isCompliant = "Y";
                            }

                            catch (TargetInvocationException)
                            {
                                isCompliant = "N";
                            }

                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }

                            finally
                            {

                                Console.WriteLine(
                                    columnFormat, type.Name, isCompliant, subclass.Name);
                            }
                        }
                    }
                }
            }
        }
    }
}




Output:
------------------------
FIPS Compliant in mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77
a5c561934e089
Name                               Compliant      Subclass
DESCryptoServiceProvider           Y              SymmetricAlgorithm
DSACryptoServiceProvider           Y              AsymmetricAlgorithm
HMACMD5                            Y              HashAlgorithm
HMACRIPEMD160                      Y              HashAlgorithm
HMACSHA1                           Y              HashAlgorithm
HMACSHA256                         Y              HashAlgorithm
HMACSHA384                         Y              HashAlgorithm
HMACSHA512                         Y              HashAlgorithm
MACTripleDES                       Y              HashAlgorithm
MD5CryptoServiceProvider           Y              HashAlgorithm
RC2CryptoServiceProvider           Y              SymmetricAlgorithm
RIPEMD160Managed                   Y              HashAlgorithm
RSACryptoServiceProvider           Y              AsymmetricAlgorithm
RijndaelManaged                    Y              SymmetricAlgorithm
SHA1CryptoServiceProvider          Y              HashAlgorithm
SHA1Managed                        Y              HashAlgorithm
SHA256Managed                      Y              HashAlgorithm
SHA384Managed                      Y              HashAlgorithm
SHA512Managed                      Y              HashAlgorithm
TripleDESCryptoServiceProvider     Y              SymmetricAlgorithm


FIPS Compliant in System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=
b77a5c561934e089
Name                               Compliant      Subclass
AesCryptoServiceProvider           Y              SymmetricAlgorithm
AesManaged                         Y              SymmetricAlgorithm
ECDiffieHellmanCng                 Y              AsymmetricAlgorithm
ECDsaCng                           Y              AsymmetricAlgorithm
MD5Cng                             Y              HashAlgorithm
RSACng                             Y              AsymmetricAlgorithm
SHA1Cng                            Y              HashAlgorithm
SHA256Cng                          Y              HashAlgorithm
SHA256CryptoServiceProvider        Y              HashAlgorithm
SHA384Cng                          Y              HashAlgorithm
SHA384CryptoServiceProvider        Y              HashAlgorithm
SHA512Cng                          Y              HashAlgorithm
SHA512CryptoServiceProvider        Y              HashAlgorithm



Reference for framework 2.0
https://blogs.msdn.microsoft.com/icumove/2009/01/31/working-with-fips-in-net-c/