private static KeySizes[] FindSupportedKeySizes(SafeCspHandle csp, out int defaultKeySize) { if (s_supportedKeySizes == null) { List <KeySizes> list = new List <KeySizes>(); int num = 0; for (CapiNative.PROV_ENUMALGS prov_enumalgs = CapiNative.GetProviderParameterStruct <CapiNative.PROV_ENUMALGS>(csp, CapiNative.ProviderParameter.EnumerateAlgorithms, CapiNative.ProviderParameterFlags.RestartEnumeration); prov_enumalgs.aiAlgId != CapiNative.AlgorithmId.None; prov_enumalgs = CapiNative.GetProviderParameterStruct <CapiNative.PROV_ENUMALGS>(csp, CapiNative.ProviderParameter.EnumerateAlgorithms, CapiNative.ProviderParameterFlags.None)) { switch (prov_enumalgs.aiAlgId) { case CapiNative.AlgorithmId.Aes128: list.Add(new KeySizes(0x80, 0x80, 0)); if (0x80 > num) { num = 0x80; } break; case CapiNative.AlgorithmId.Aes192: list.Add(new KeySizes(0xc0, 0xc0, 0)); if (0xc0 > num) { num = 0xc0; } break; case CapiNative.AlgorithmId.Aes256: list.Add(new KeySizes(0x100, 0x100, 0)); if (0x100 > num) { num = 0x100; } break; } } s_supportedKeySizes = list.ToArray(); s_defaultKeySize = num; } defaultKeySize = s_defaultKeySize; return(s_supportedKeySizes); }
private static KeySizes[] FindSupportedKeySizes(SafeCspHandle csp, out int defaultKeySize) { Contract.Requires(csp != null); Contract.Ensures(Contract.Result <KeySizes[]>() != null); // If this platform has any supported algorithm sizes, then the default key size should be set to a // reasonable value. Contract.Ensures(Contract.Result <KeySizes[]>().Length == 0 || (Contract.ValueAtReturn <int>(out defaultKeySize) > 0 && Contract.ValueAtReturn <int>(out defaultKeySize) % 8 == 0)); if (s_supportedKeySizes == null) { List <KeySizes> keySizes = new List <KeySizes>(); int maxKeySize = 0; // // Enumerate the CSP's supported algorithms to see what key sizes it supports for AES // CapiNative.PROV_ENUMALGS algorithm = CapiNative.GetProviderParameterStruct <CapiNative.PROV_ENUMALGS>(csp, CapiNative.ProviderParameter.EnumerateAlgorithms, CapiNative.ProviderParameterFlags.RestartEnumeration); // Translate between CAPI AES algorithm IDs and supported key sizes while (algorithm.aiAlgId != CapiNative.AlgorithmId.None) { switch (algorithm.aiAlgId) { case CapiNative.AlgorithmId.Aes128: keySizes.Add(new KeySizes(128, 128, 0)); if (128 > maxKeySize) { maxKeySize = 128; } break; case CapiNative.AlgorithmId.Aes192: keySizes.Add(new KeySizes(192, 192, 0)); if (192 > maxKeySize) { maxKeySize = 192; } break; case CapiNative.AlgorithmId.Aes256: keySizes.Add(new KeySizes(256, 256, 0)); if (256 > maxKeySize) { maxKeySize = 256; } break; default: break; } algorithm = CapiNative.GetProviderParameterStruct <CapiNative.PROV_ENUMALGS>(csp, CapiNative.ProviderParameter.EnumerateAlgorithms, CapiNative.ProviderParameterFlags.None); } s_supportedKeySizes = keySizes.ToArray(); s_defaultKeySize = maxKeySize; } defaultKeySize = s_defaultKeySize; return(s_supportedKeySizes); }