AgreementUtilities.cs
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Collections;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Agreement;
using Org.BouncyCastle.Crypto.Agreement.Kdf;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Security
{
/// <remarks>
/// Utility class for creating IBasicAgreement objects from their names/Oids
/// </remarks>
public sealed class AgreementUtilities
{
private AgreementUtilities()
{
}
private static readonly IDictionary algorithms = Platform.CreateHashtable();
//private static readonly IDictionary oids = Platform.CreateHashtable();
static AgreementUtilities()
{
algorithms[X9ObjectIdentifiers.DHSinglePassCofactorDHSha1KdfScheme.Id] = "ECCDHWITHSHA1KDF";
algorithms[X9ObjectIdentifiers.DHSinglePassStdDHSha1KdfScheme.Id] = "ECDHWITHSHA1KDF";
algorithms[X9ObjectIdentifiers.MqvSinglePassSha1KdfScheme.Id] = "ECMQVWITHSHA1KDF";
}
public static IBasicAgreement GetBasicAgreement(
DerObjectIdentifier oid)
{
return GetBasicAgreement(oid.Id);
}
public static IBasicAgreement GetBasicAgreement(
string algorithm)
{
string upper = Platform.ToUpperInvariant(algorithm);
string mechanism = (string) algorithms[upper];
if (mechanism == null)
{
mechanism = upper;
}
if (mechanism == "DH" || mechanism == "DIFFIEHELLMAN")
return new DHBasicAgreement();
if (mechanism == "ECDH")
return new ECDHBasicAgreement();
if (mechanism == "ECDHC" || mechanism == "ECCDH")
return new ECDHCBasicAgreement();
if (mechanism == "ECMQV")
return new ECMqvBasicAgreement();
throw new SecurityUtilityException("Basic Agreement " + algorithm + " not recognised.");
}
public static IBasicAgreement GetBasicAgreementWithKdf(
DerObjectIdentifier oid,
string wrapAlgorithm)
{
return GetBasicAgreementWithKdf(oid.Id, wrapAlgorithm);
}
public static IBasicAgreement GetBasicAgreementWithKdf(
string agreeAlgorithm,
string wrapAlgorithm)
{
string upper = Platform.ToUpperInvariant(agreeAlgorithm);
string mechanism = (string) algorithms[upper];
if (mechanism == null)
{
mechanism = upper;
}
// 'DHWITHSHA1KDF' retained for backward compatibility
if (mechanism == "DHWITHSHA1KDF" || mechanism == "ECDHWITHSHA1KDF")
return new ECDHWithKdfBasicAgreement(
wrapAlgorithm,
new ECDHKekGenerator(
new Sha1Digest()));
if (mechanism == "ECMQVWITHSHA1KDF")
return new ECMqvWithKdfBasicAgreement(
wrapAlgorithm,
new ECDHKekGenerator(
new Sha1Digest()));
throw new SecurityUtilityException("Basic Agreement (with KDF) " + agreeAlgorithm + " not recognised.");
}
public static string GetAlgorithmName(
DerObjectIdentifier oid)
{
return (string) algorithms[oid.Id];
}
}
}