/
ECCurve.cs
264 lines (231 loc) · 8.23 KB
/
ECCurve.cs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.Versioning;
namespace System.Security.Cryptography
{
/// <summary>
/// An elliptic curve.
/// </summary>
/// <remarks>
/// The CurveType property determines whether the curve is a named curve or an explicit curve
/// which is either a prime curve or a characteristic-2 curve.
/// </remarks>
[DebuggerDisplay("ECCurve = {Oid}")]
public partial struct ECCurve
{
/// <summary>
/// Coefficient A. Applies only to Explicit curves.
/// </summary>
public byte[]? A;
/// <summary>
/// Coefficient B. Applies only to Explicit curves.
/// </summary>
public byte[]? B;
/// <summary>
/// Base Point. Applies only to Explicit curves.
/// </summary>
public ECPoint G;
/// <summary>
/// Order of the group generated by G = (x,y). Applies only to Explicit curves.
/// </summary>
public byte[]? Order;
/// <summary>
/// Cofactor (optional). Applies only to Explicit curves.
/// </summary>
public byte[]? Cofactor;
/// <summary>
/// Seed of the curve (optional). Applies only to Explicit curves.
/// </summary>
public byte[]? Seed;
/// <summary>
/// Curve Type.
/// </summary>
public ECCurveType CurveType;
/// <summary>
/// The hash algorithm used to generate A and B from the Seed. Applies only to Explicit curves.
/// </summary>
public HashAlgorithmName? Hash;
/// <summary>
/// The binary polynomial. Applies only to Characteristic2 curves.
/// </summary>
public byte[]? Polynomial;
/// <summary>
/// The prime specifying the base field. Applies only to Prime curves.
/// </summary>
public byte[]? Prime;
private Oid _oid;
/// <summary>
/// The Oid representing the named curve. Applies only to Named curves.
/// </summary>
public Oid Oid
{
get => _oid;
private set
{
ArgumentNullException.ThrowIfNull(value, nameof(Oid));
if (string.IsNullOrEmpty(value.Value) && string.IsNullOrEmpty(value.FriendlyName))
throw new ArgumentException(SR.Format(SR.Cryptography_InvalidCurveOid, value.Value));
_oid = value;
}
}
/// <summary>
/// Create a curve from the given cref="Oid".
/// </summary>
/// <param name="curveOid">The Oid to use.</param>
/// <returns>An ECCurve representing a named curve.</returns>
public static ECCurve CreateFromOid(Oid curveOid)
{
ECCurve curve = default;
curve.CurveType = ECCurveType.Named;
curve.Oid = curveOid;
return curve;
}
/// <summary>
/// Create a curve from the given cref="Oid" friendly name.
/// </summary>
/// <param name="oidFriendlyName">The Oid friendly name to use.</param>
/// <returns>An ECCurve representing a named curve.</returns>
public static ECCurve CreateFromFriendlyName(string oidFriendlyName)
{
ArgumentNullException.ThrowIfNull(oidFriendlyName);
return ECCurve.CreateFromValueAndName(null, oidFriendlyName);
}
/// <summary>
/// Create a curve from the given cref="Oid" value.
/// </summary>
/// <param name="oidValue">The Oid value to use.</param>
/// <returns>An ECCurve representing a named curve.</returns>
public static ECCurve CreateFromValue(string oidValue)
{
ArgumentNullException.ThrowIfNull(oidValue);
return ECCurve.CreateFromValueAndName(oidValue, null);
}
private static ECCurve CreateFromValueAndName(string? oidValue, string? oidFriendlyName)
{
Oid? oid = null;
if (oidValue == null && oidFriendlyName != null)
{
try
{
oid = Oid.FromFriendlyName(oidFriendlyName, OidGroup.PublicKeyAlgorithm);
}
catch (CryptographicException)
{
}
}
oid ??= new Oid(oidValue, oidFriendlyName);
return ECCurve.CreateFromOid(oid);
}
public bool IsPrime
{
get
{
return CurveType == ECCurve.ECCurveType.PrimeShortWeierstrass ||
CurveType == ECCurve.ECCurveType.PrimeMontgomery ||
CurveType == ECCurve.ECCurveType.PrimeTwistedEdwards;
}
}
public bool IsCharacteristic2
{
get
{
return CurveType == ECCurve.ECCurveType.Characteristic2;
}
}
public bool IsExplicit
{
get
{
return IsPrime || IsCharacteristic2;
}
}
public bool IsNamed
{
get
{
return CurveType == ECCurve.ECCurveType.Named;
}
}
/// <summary>
/// Validate the current curve.
/// </summary>
/// <exception cref="CryptographicException">
/// if the curve parameters are not valid for the current CurveType.
/// </exception>
public void Validate()
{
if (IsNamed)
{
if (HasAnyExplicitParameters())
{
throw new CryptographicException(SR.Cryptography_InvalidECNamedCurve);
}
if (Oid == null ||
(string.IsNullOrEmpty(Oid.FriendlyName) && string.IsNullOrEmpty(Oid.Value)))
{
throw new CryptographicException(SR.Format(SR.Cryptography_InvalidCurveOid, Oid?.Value));
}
}
else if (IsExplicit)
{
bool hasErrors = false;
if (A == null ||
B == null || B.Length != A.Length ||
G.X == null || G.X.Length != A.Length ||
G.Y == null || G.Y.Length != A.Length ||
Order == null || Order.Length == 0 ||
Cofactor == null || Cofactor.Length == 0)
{
hasErrors = true;
}
if (IsPrime)
{
if (!hasErrors)
{
if (Prime == null || Prime.Length != A!.Length)
{
hasErrors = true;
}
}
if (hasErrors)
throw new CryptographicException(SR.Cryptography_InvalidECPrimeCurve);
}
else if (IsCharacteristic2)
{
if (!hasErrors)
{
if (Polynomial == null || Polynomial.Length == 0)
{
hasErrors = true;
}
}
if (hasErrors)
throw new CryptographicException(SR.Cryptography_InvalidECCharacteristic2Curve);
}
}
else
{
// Implicit; if there are any values, throw
Debug.Assert(CurveType == ECCurveType.Implicit);
if (HasAnyExplicitParameters() || Oid != null)
{
throw new CryptographicException(SR.Format(SR.Cryptography_CurveNotSupported, CurveType.ToString()));
}
}
}
private bool HasAnyExplicitParameters()
{
return (A != null ||
B != null ||
G.X != null ||
G.Y != null ||
Order != null ||
Cofactor != null ||
Prime != null ||
Polynomial != null ||
Seed != null ||
Hash != null);
}
}
}