//******************************************************************************************************
// Cipher.cs - Gbtc
//
// Copyright © 2017, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 04/11/2017 - Ritchie Carroll
// Generated partial version of class in GSF.Shared.
//
//******************************************************************************************************
using System.Security.Cryptography;
using Microsoft.Win32;
namespace GSF.Security.Cryptography
{
///
/// Provides general use cryptographic functions.
///
///
/// This class exists to simplify usage of basic cryptography functionality.
///
public static partial class Cipher
{
// Switch to turn off managed encryption and use wrappers over FIPS-compliant algorithms.
private static bool s_managedEncryption;
///
/// Gets a flag that determines if system will allow use of managed, i.e., non-FIPS compliant, security algorithms.
///
public static bool SystemAllowsManagedEncryption => s_managedEncryption;
///
/// Static constructor for the class.
///
static Cipher()
{
#if MONO
// Common .NET FIPS wrapper algorithms are implemented as managed code under Mono, check status of Crimson project
s_managedEncryption = true;
#else
const string fipsKeyOld = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Lsa";
const string fipsKeyNew = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Lsa\\FipsAlgorithmPolicy";
// Determine if the user needs to use FIPS-compliant algorithms
try
{
s_managedEncryption = (Registry.GetValue(fipsKeyNew, "Enabled", 0) ?? Registry.GetValue(fipsKeyOld, "FIPSAlgorithmPolicy", 0)).ToString() == "0";
}
catch
{
s_managedEncryption = true;
}
#endif
// Continue with any needed static construction activities
OnCreated();
}
static partial void OnCreated();
///
/// Creates a hashing algorithm that respects current FIPS setting.
///
/// New hashing algorithm that respects current FIPS setting.
public static SHA1 CreateSHA1()
{
return s_managedEncryption ? new SHA1Managed() : new SHA1CryptoServiceProvider() as SHA1;
}
///
/// Creates a hashing algorithm that respects current FIPS setting.
///
/// New hashing algorithm that respects current FIPS setting.
public static SHA256 CreateSHA256()
{
return s_managedEncryption ? new SHA256Managed() : new SHA256CryptoServiceProvider() as SHA256;
}
///
/// Creates a hashing algorithm that respects current FIPS setting.
///
/// New hashing algorithm that respects current FIPS setting.
public static SHA384 CreateSHA384()
{
return s_managedEncryption ? new SHA384Managed() : new SHA384CryptoServiceProvider() as SHA384;
}
///
/// Creates a hashing algorithm that respects current FIPS setting.
///
/// New hashing algorithm that respects current FIPS setting.
public static SHA512 CreateSHA512()
{
return s_managedEncryption ? new SHA512Managed() : new SHA512CryptoServiceProvider() as SHA512;
}
///
/// Creates an encryption algorithm that respects current FIPS setting.
///
/// New encryption algorithm that respects current FIPS setting.
public static Aes CreateAes()
{
return s_managedEncryption ? new AesManaged() : new AesCryptoServiceProvider() as Aes;
}
}
}