mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathKemUtilities.cs
More file actions
217 lines (177 loc) · 7.03 KB
/
Copy pathKemUtilities.cs
File metadata and controls
217 lines (177 loc) · 7.03 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Kems;
using Org.BouncyCastle.Crypto.Parameters;
namespace Org.BouncyCastle.Security
{
public static class KemUtilities
{
private static readonly Dictionary<string, string> ByName =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private static readonly Dictionary<DerObjectIdentifier, string> ByOid =
new Dictionary<DerObjectIdentifier, string>();
static KemUtilities()
{
/*
* ML-KEM
*/
foreach (MLKemParameters mlKem in MLKemParameters.ByName.Values)
{
Register(mlKem.Name, mlKem.Oid);
}
#if DEBUG
foreach (var name in ByName.Keys)
{
// OIDS have their own lookup
if (DerObjectIdentifier.TryFromID(name, out var ignore))
throw new Exception($"OID mapping belongs in {nameof(ByOid)}: {name}");
}
var mechanisms = new HashSet<string>(ByName.Values);
mechanisms.UnionWith(ByOid.Values);
foreach (var mechanism in mechanisms)
{
// All mechanisms must have a self-mapping
if (!ByName.TryGetValue(mechanism, out var check) || check != mechanism)
throw new Exception($"Mechanism must have {nameof(ByName)} mapping to self: {mechanism}");
}
#endif
}
private static void Register(string name, DerObjectIdentifier oid)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
ByName.Add(name, name);
if (oid != null)
{
ByOid.Add(oid, name);
}
}
internal static byte[] Decapsulate(IKemDecapsulator kemDecapsulator, byte[] encBuf, int encOff, int encLen)
{
if (kemDecapsulator == null)
throw new ArgumentNullException(nameof(kemDecapsulator));
byte[] sec = new byte[kemDecapsulator.SecretLength];
kemDecapsulator.Decapsulate(encBuf, encOff, encLen, sec, 0, sec.Length);
return sec;
}
internal static Tuple<byte[], byte[]> Encapsulate(IKemEncapsulator kemEncapsulator)
{
if (kemEncapsulator == null)
throw new ArgumentNullException(nameof(kemEncapsulator));
byte[] enc = new byte[kemEncapsulator.EncapsulationLength];
byte[] sec = new byte[kemEncapsulator.SecretLength];
kemEncapsulator.Encapsulate(enc, 0, enc.Length, sec, 0, sec.Length);
return Tuple.Create(enc, sec);
}
public static IKemDecapsulator GetDecapsulator(DerObjectIdentifier oid)
{
if (TryGetDecapsulator(oid, out var decapsulator))
return decapsulator;
throw new SecurityUtilityException("KEM OID not recognised.");
}
public static IKemDecapsulator GetDecapsulator(string name)
{
if (TryGetDecapsulator(name, out var decapsulator))
return decapsulator;
throw new SecurityUtilityException("KEM name not recognised.");
}
public static IKemEncapsulator GetEncapsulator(DerObjectIdentifier oid)
{
if (TryGetEncapsulator(oid, out var encapsulator))
return encapsulator;
throw new SecurityUtilityException("KEM OID not recognised.");
}
public static IKemEncapsulator GetEncapsulator(string name)
{
if (TryGetEncapsulator(name, out var encapsulator))
return encapsulator;
throw new SecurityUtilityException("KEM name not recognised.");
}
public static bool TryGetDecapsulator(DerObjectIdentifier oid, out IKemDecapsulator decapsulator)
{
if (oid == null)
throw new ArgumentNullException(nameof(oid));
if (TryGetMechanism(oid, out var mechanism))
{
var decap = GetDecapForMechanism(mechanism);
if (decap != null)
{
decapsulator = decap;
return true;
}
}
decapsulator = default;
return false;
}
public static bool TryGetDecapsulator(string name, out IKemDecapsulator decapsulator)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
if (TryGetMechanism(name, out var mechanism))
{
var decap = GetDecapForMechanism(mechanism);
if (decap != null)
{
decapsulator = decap;
return true;
}
}
decapsulator = default;
return false;
}
public static bool TryGetEncapsulator(DerObjectIdentifier oid, out IKemEncapsulator encapsulator)
{
if (oid == null)
throw new ArgumentNullException(nameof(oid));
if (TryGetMechanism(oid, out var mechanism))
{
var encap = GetEncapForMechanism(mechanism);
if (encap != null)
{
encapsulator = encap;
return true;
}
}
encapsulator = default;
return false;
}
public static bool TryGetEncapsulator(string name, out IKemEncapsulator encapsulator)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
if (TryGetMechanism(name, out var mechanism))
{
var encap = GetEncapForMechanism(mechanism);
if (encap != null)
{
encapsulator = encap;
return true;
}
}
encapsulator = default;
return false;
}
private static IKemDecapsulator GetDecapForMechanism(string mechanism)
{
if (MLKemParameters.ByName.TryGetValue(mechanism, out MLKemParameters mlKemParameters))
return new MLKemDecapsulator(mlKemParameters);
return null;
}
private static IKemEncapsulator GetEncapForMechanism(string mechanism)
{
if (MLKemParameters.ByName.TryGetValue(mechanism, out MLKemParameters mlKemParameters))
return new MLKemEncapsulator(mlKemParameters);
return null;
}
private static bool TryGetMechanism(DerObjectIdentifier oid, out string mechanism) =>
ByOid.TryGetValue(oid, out mechanism);
private static bool TryGetMechanism(string name, out string mechanism)
{
if (DerObjectIdentifier.TryFromID(name, out var oid))
return TryGetMechanism(oid, out mechanism);
return ByName.TryGetValue(name, out mechanism);
}
}
}