在符合 FIPS 的环境中使用 DevForce 时出现异常

Exception when using DevForce in a FIPS-compliant environment

我们有一些客户想要在“FIPS Mode”中 运行 我们的软件(使用 DevForce)Windows。默认情况下,DevForce 完成的大部分加密似乎都符合 FIPS 标准,但我们 运行 遇到了 DevForce 所做的一些散列问题。具体来说,使用向下调用 AesCryptoProvider.CalcStringHash 的代码如下所示:

/// <summary>
/// Returns a hash encoded as a string with the chars (A-Z,A-z,0-9,_) only.
/// </summary>
/// <remarks>
/// Under the covers this method returns an 128 bit hash code calculated
/// using SHA1.  This code is then encoded into an approx Base64 encode
/// of the chars listed above.  This will usually be approx 28 chars in length,
/// which may then be truncated based on the maxChars parameter. This
/// method can process approx 100K 300 char strings a second.
/// </remarks>
/// <param name="stringToHash"></param>
/// <param name="maxChars"></param>
/// <returns></returns>
public string CalcStringHash(string stringToHash, int maxChars)
{
  return CodingFns.EncodeBase64(new SHA1Managed().ComputeHash(Encoding.Unicode.GetBytes(stringToHash))).Substring(0, maxChars).Replace("=", "").Replace("/", "_d").Replace("+", "_p");
}

该方法直接实例化一个 SHA1Managed 实例,不幸的是,class 不符合 FIPS。

这是上下文的完整堆栈跟踪:

Exception: System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
   at System.Security.Cryptography.SHA1Managed..ctor()
   at IdeaBlade.Core.PlatformServices.AesCryptoProvider.CalcStringHash(String stringToHash, Int32 maxChars)
   at IdeaBlade.Core.DynamicTypeInfo.GetUniqueToken()
   at IdeaBlade.Core.DynamicTypeInfo.BuildDynamicTypeName()
   at IdeaBlade.Core.DynamicTypeInfo..ctor(TypeShape typeShape)
   at IdeaBlade.Core.DynamicTypeInfo.FindOrCreate(IEnumerable`1 propertyNames, IEnumerable`1 propertyTypes)
   at IdeaBlade.Linq.AnonymousProjectionSelector.GetDynamicTypeInfo()
   at IdeaBlade.Linq.AnonymousProjectionSelector.ToLambdaExpression(Type delegateType)
   at IdeaBlade.Linq.QueryableExtensions.Select[TSource](IQueryable`1 source, IProjectionSelector selector)
   at My.Company.ServerClass.MyRemoteServerMethod(IPrincipal principal, EntityManager serverEm, Object[] rawArgs)

是否有任何解决方法可以让 DevForce 在 FIPS 模式下工作?或者这是可以在未来版本中添加的内容吗?

我注意到对该方法的评论谈论的是性能。如果这是一种性能关键的方法并且切换到符合 FIPS 的替代方法会导致性能下降,那么它可能是一个可配置的选项吗?我们只有一位客户抱怨 FIPS,但还有更多客户关心性能,所以我不想让不关心 FIPS 的人的速度变慢。

这是一个错误,我们可以修复它(希望在下一个版本中修复)。此散列逻辑仅在为 "dynamic" 类型生成名称时使用,该类型在处理匿名类型和动态查询时使用。您可以通过投影到自定义类型而不是匿名类型来解决此错误,否则没有解决方法可以避免该问题。

如果性能是一个问题,而且很可能不是,因为这种散列现在仅用于动态类型名称,我们可以考虑在 FIPS/non-FIPS 兼容提供程序之间进行选择的开关。