Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PaceEcdhProtocol.cs
1using System;
2using System.Xml;
8
10{
14 public abstract class PaceEcdhProtocol() : PaceProtocol()
15 {
16 private EllipticCurve? curve;
17
21 public EllipticCurve? Curve => this.curve;
22
26 public override bool IsConfigured => this.curve is not null;
27
33 public override bool Configure(Vector SecurityInfo)
34 {
35 if (!base.Configure(SecurityInfo))
36 return false;
37
38 if (!this.ParameterId.HasValue ||
39 this.ParameterId.Value < int.MinValue ||
40 this.ParameterId.Value > int.MaxValue)
41 {
42 return false;
43 }
44
45 switch ((int)this.ParameterId.Value)
46 {
47 case 0:
48 return false; // TODO: 1024 - bit MODP Group with 160 - bit Prime Order, RFC 5114
49
50 case 1:
51 return false; // TODO: 2048 - bit MODP Group with 224 - bit Prime Order, RFC 5114
52
53 case 2:
54 return false; // TODO: 2048 - bit MODP Group with 256 - bit Prime Order
55
56 // 3-7: Reserved for future use
57
58 case 8:
59 this.curve = new NistP192();
60 return true;
61
62 case 9:
63 this.curve = new BrainpoolP192();
64 return true;
65
66 case 10:
67 this.curve = new NistP224();
68 return true;
69
70 case 11:
71 this.curve = new BrainpoolP224();
72 return true;
73
74 case 12:
75 this.curve = new NistP256();
76 return true;
77
78 case 13:
79 this.curve = new BrainpoolP256();
80 return true;
81
82 case 14:
83 this.curve = new BrainpoolP320();
84 return true;
85
86 case 15:
87 this.curve = new NistP384();
88 return true;
89
90 case 16:
91 this.curve = new BrainpoolP384();
92 return true;
93
94 case 17:
95 this.curve = new BrainpoolP512();
96 return true;
97
98 case 18:
99 this.curve = new NistP521();
100 return true;
101
102 // 19-31: Reserved for future use
103
104 default:
105 return false;
106 }
107 }
108
115 public override byte[] CreateNewKey(byte[]? Seed, ref int Index)
116 {
117 if (this.curve is null)
118 throw new NotSupportedException("EEC Curve not configured.");
119
120 if (Seed is null)
121 this.curve.GenerateKeys();
122 else
123 {
124 byte[] Secret = this.GenerateSecret(Seed, ref Index);
125 this.curve.SetPrivateKey(Secret);
126 }
127
128 return this.curve.PublicKeyBigEndian;
129 }
130
138 public byte[] GenerateSecret(byte[]? Seed, ref int Index)
139 {
140 if (Seed is null)
141 return this.curve!.GenerateSecret();
142
143 SHAKE256 H = new(this.curve!.BigIntegerBytes << 3);
144 byte[] B;
145 System.Numerics.BigInteger D;
146 System.Numerics.BigInteger? Order;
147
148 if (this.curve is PrimeFieldCurve PrimeFieldCurve)
149 Order = PrimeFieldCurve.Order;
150 else
151 Order = null;
152
153 do
154 {
155 B = H.ComputeVariable(TravelDocumentsClient.CONCAT(Seed, BitConverter.GetBytes(Index++)));
156 B[this.curve.BigIntegerBytes - 1] &= this.curve.MsbOrderMask;
157
158 D = EllipticCurve.ToInt(B);
159 }
160 while (D.IsZero || (Order.HasValue && D >= Order));
161
162 return B;
163 }
164
169 public override byte[] ImportKey(XmlDocument Xml)
170 {
171 if (this.curve is null)
172 throw new NotSupportedException("EEC Curve not configured.");
173
174 if (Xml?.DocumentElement is null)
175 throw new ArgumentNullException(nameof(Xml), "Missing XML.");
176
177 if (Xml.DocumentElement.Name != "EllipticCurve" ||
178 Xml.DocumentElement.NamespaceURI != "http://waher.se/Schema/EllipticCurves.xsd")
179 {
180 throw new ArgumentException("Not an Elliptic Curve export XML document.", nameof(Xml));
181 }
182
183 if (XML.Attribute(Xml.DocumentElement, "type") != this.curve.GetType().FullName)
184 throw new ArgumentException("Exported curve type does not match current curve.", nameof(Xml));
185
186 byte[] Secret = Convert.FromBase64String(XML.Attribute(Xml.DocumentElement, "d"));
187
188 this.curve.SetPrivateKey(Secret);
189
190 return this.curve.PublicKeyBigEndian;
191 }
192
197 public override IPaceProtocol CreateEphemeralKey()
198 {
199 PaceEcdhProtocol Result = (PaceEcdhProtocol)Types.Instantiate(this.GetType());
200
201 if (this.curve is not null)
202 Result.curve = (EllipticCurve)Types.Instantiate(this.curve.GetType());
203
204 return Result;
205 }
206
211 public void SetPrivateKey(byte[] PrivateKey)
212 {
213 if (this.curve is null)
214 throw new NotSupportedException("EEC Curve not configured.");
215
216 this.curve.SetPrivateKey(PrivateKey);
217 }
218
225 public override byte[] GetSharedSecret(byte[] RemotePublicKey)
226 {
227 //return this.curve!.GetSharedKey(RemotePublicKey, this.HashFunction);
228
229 PointOnCurve H = this.curve!.GetSharedPoint(RemotePublicKey, true);
230
231 byte[] X = H.X.ToByteArray(); // Little endian
232 byte[] Y = H.Y.ToByteArray(); // Little endian
233 int c = this.curve.OrderBytes;
234
235 if (X.Length != c)
236 Array.Resize(ref X, c);
237
238 if (Y.Length != c)
239 Array.Resize(ref Y, c);
240
241 Array.Reverse(X); // Most significant byte first.
242 Array.Reverse(Y); // Most significant byte first.
243
244 byte[] Result = new byte[c << 1];
245
246 Buffer.BlockCopy(X, 0, Result, 0, c);
247 Buffer.BlockCopy(Y, 0, Result, c, c);
248
249 return Result;
250 }
251
259 public PointOnCurve GetGenericMap(byte[] s, byte[] RemotePublicKey)
260 {
261 PointOnCurve G = this.curve!.BasePoint;
262 PointOnCurve H = this.curve.GetSharedPoint(RemotePublicKey, true);
263
264 byte[] s2 = (byte[])s.Clone();
265 Array.Reverse(s2);
266
267 PointOnCurve Ĝ = this.curve.ScalarMultiplication(s2, G, true);
268 this.curve.AddTo(ref Ĝ, H);
269
270 return Ĝ;
271 }
272 }
273}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1454
Brainpool P-192 Elliptic Curve, as defined in RFC5639: https://datatracker.ietf.org/doc/html/rfc5639
Brainpool P-224 Elliptic Curve, as defined in RFC5639: https://datatracker.ietf.org/doc/html/rfc5639
Brainpool P-256 Elliptic Curve, as defined in RFC5639: https://datatracker.ietf.org/doc/html/rfc5639
Brainpool P-320 Elliptic Curve, as defined in RFC5639: https://datatracker.ietf.org/doc/html/rfc5639
Brainpool P-384 Elliptic Curve, as defined in RFC5639: https://datatracker.ietf.org/doc/html/rfc5639
Brainpool P-512 Elliptic Curve, as defined in RFC5639: https://datatracker.ietf.org/doc/html/rfc5639
Abstract base class for elliptic curves.
abstract byte[] GenerateSecret()
Generates a new secret.
virtual byte[] PublicKeyBigEndian
Returns a big-endian representation of the public key.
byte MsbOrderMask
Mask for most significant byte of scalars (as byte arrays of size BigIntegerBytes).
static BigInteger ToInt(byte[] Binary, bool BigEndian)
Converts a little-endian binary representation of a big integer to a BigInteger.
int OrderBytes
Number of bytes required to represent the order of the curve.
int BigIntegerBytes
Number of bytes required to represent coordinates using BigInteger of the curve. This number may diff...
void GenerateKeys()
Generates a new Private Key.
PointOnCurve GetSharedPoint(byte[] RemotePublicKey)
Gets a shared key, as a point, using the Elliptic Curve Diffie-Hellman (ECDH) algorithm.
PointOnCurve BasePoint
Base-point of curve.
virtual void SetPrivateKey(byte[] Secret)
Sets the private key (and therefore also the public key) of the curve.
abstract void AddTo(ref PointOnCurve P, PointOnCurve Q)
Adds Q to P .
PointOnCurve ScalarMultiplication(BigInteger N, PointOnCurve P, bool Normalize)
Performs the scalar multiplication of N *P .
NIST P-192 Elliptic Curve, as defined in NIST FIPS BUB 186-4: https://nvlpubs.nist....
Definition: NistP192.cs:10
NIST P-224 Elliptic Curve, as defined in NIST FIPS BUB 186-4: https://nvlpubs.nist....
Definition: NistP224.cs:10
NIST P-256 Elliptic Curve, as defined in NIST FIPS BUB 186-4: https://nvlpubs.nist....
Definition: NistP256.cs:10
NIST P-384 Elliptic Curve, as defined in NIST FIPS BUB 186-4: https://nvlpubs.nist....
Definition: NistP384.cs:10
NIST P-521 Elliptic Curve, as defined in NIST FIPS BUB 186-4: https://nvlpubs.nist....
Definition: NistP521.cs:10
Base class of Elliptic curves over a prime field.
byte[] ComputeVariable(byte[] N)
Computes the SPONGE function, as defined in section 4 of NIST FIPS 202.
Definition: Keccak1600.cs:408
Implements the SHA3 SHAKE256 extendable-output functions, as defined in section 6....
Definition: SHAKE256.cs:9
Basic interface for PACE protocols.
abstract class PaceEcdhProtocol()
Abstract base class for PACE protocols using Elliptic Curve Cryptography (EEC).
Represents a point on a curve.
Definition: PointOnCurve.cs:10