Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PaceProtocol.cs
1using System;
2using System.Globalization;
4using System.Threading.Tasks;
5using System.Xml;
9
11{
15 public abstract class PaceProtocol() : SecurityObject, IPaceProtocol
16 {
17 private System.Numerics.BigInteger version;
18 private System.Numerics.BigInteger? parameterId;
19 private bool configured;
20
24 public abstract Grade SecurityStrength { get; }
25
29 public virtual bool ChipAuthenticationMapping => false;
30
34 public System.Numerics.BigInteger Version => this.version;
35
39 public System.Numerics.BigInteger? ParameterId => this.parameterId;
40
44 public abstract int Bits { get; }
45
51 public override Grade Supports(string Object)
52 {
53 return Object == this.Oid ? this.SecurityStrength : Grade.NotAtAll;
54 }
55
59 public override bool IsConfigured => this.configured;
60
66 public override bool Configure(Vector SecurityInfo)
67 {
68 if (SecurityInfo.Length < 2)
69 return false;
70
71 if (SecurityInfo[1] is not System.Numerics.BigInteger Version)
72 return false;
73
74 this.version = Version;
75
76 if (SecurityInfo.Length > 3)
77 return false;
78 else if (SecurityInfo.Length == 2)
79 {
80 this.configured = true;
81 return true;
82 }
83 else if (SecurityInfo[2] is not System.Numerics.BigInteger ParameterId)
84 return false;
85 else
86 {
87 this.parameterId = ParameterId;
88 this.configured = true;
89 return true;
90 }
91 }
92
99 public abstract byte[] CreateNewKey(byte[]? Seed, ref int Index);
100
105 public abstract byte[] ImportKey(XmlDocument Xml);
106
111 public abstract IPaceProtocol CreateEphemeralKey();
112
119 public virtual byte[] GetSharedSecret(byte[] RemotePublicKey)
120 {
121 // TODO
122 throw new NotImplementedException("This protocol does not implement ephemeral key generation.");
123 }
124
130 public virtual Task<bool> Authenticate(TravelDocumentsClient Client)
131 {
132 return Task.FromResult(false);
133 }
134
139 public byte[] KDFπ(DocumentInformation Info)
140 {
141 return TravelDocumentsClient.KDF(TravelDocumentsClient.PACE_K(Info), 3,
142 this.AdjustParity, this.KdfHashFunction, this.KdfHashKeyLength);
143 }
144
149 public byte[] KDF_Enc(byte[] KSeed)
150 {
151 return TravelDocumentsClient.KDF(KSeed, 1, this.AdjustParity, this.KdfHashFunction, this.KdfHashKeyLength);
152 }
153
158 public byte[] KDF_Mac(byte[] KSeed)
159 {
160 return TravelDocumentsClient.KDF(KSeed, 2, this.AdjustParity, this.KdfHashFunction, this.KdfHashKeyLength);
161 }
162
166 public virtual bool AdjustParity => false;
167
171 public virtual HashFunctionArray KdfHashFunction => Hashes.ComputeSHA1Hash;
172
176 public virtual int KdfHashKeyLength => 16;
177
181 public virtual int BlockLength => 16;
182
190 public byte[] DecryptNonce(DocumentInformation Info, byte[] EncryptedNonce)
191 {
192 byte[] Kπ = this.KDFπ(Info);
193 return this.DecryptNonce(Kπ, EncryptedNonce);
194 }
195
202 public virtual byte[] DecryptNonce(byte[] Kπ, byte[] EncryptedNonce)
203 {
204 return this.DecryptAes(Kπ, zeroIv16, EncryptedNonce);
205 }
206
213 public static byte[] CreateAssociatedData(string Oid, byte[] PublicKey)
214 {
215 string[] OidParts = Oid.Split('.');
216 int i, c = OidParts.Length;
217 byte[] OidBytes = new byte[c];
218
219 for (i = 0; i < c; i++)
220 OidBytes[i] = byte.Parse(OidParts[i], CultureInfo.InvariantCulture);
221
222 int d = PublicKey.Length;
223 byte[] AssociatedData = new byte[7 + c + d];
224
225 AssociatedData[0] = 0x7f;
226 AssociatedData[1] = 0x49;
227 AssociatedData[2] = (byte)(4 + c + d);
228 AssociatedData[3] = 0x06;
229 AssociatedData[4] = (byte)(c - 1);
230
231 Buffer.BlockCopy(OidBytes, 1, AssociatedData, 5, c - 1);
232 i = 4 + c;
233
234 AssociatedData[i++] = 0x86;
235 AssociatedData[i++] = (byte)(1 + d);
236 AssociatedData[i++] = 0x04;
237
238 Buffer.BlockCopy(PublicKey, 0, AssociatedData, i, d);
239
240 return AssociatedData;
241 }
242
248 public abstract CMac GetAuthenticator(byte[] Key);
249
257 public virtual byte[] Encrypt(byte[] Key, byte[] IV, byte[] Data)
258 {
259 return this.EncryptAes(Key, IV, Data);
260 }
261
269 public virtual byte[] Decrypt(byte[] Key, byte[] IV, byte[] Data)
270 {
271 return this.DecryptAes(Key, IV, Data);
272 }
280 protected byte[] EncryptAes(byte[] Key, byte[] IV, byte[] Data)
281 {
282 using Aes Cipher = Aes.Create();
283 Cipher.Mode = CipherMode.CBC;
284 Cipher.Padding = PaddingMode.None;
285 Cipher.BlockSize = this.BlockLength << 3;
286 Cipher.KeySize = Key.Length << 3;
287
288 using ICryptoTransform Encryptor = Cipher.CreateEncryptor(Key, IV);
289
290 return Encryptor.TransformFinalBlock(Data, 0, Data.Length);
291 }
292
300 protected byte[] Encrypt3Des(byte[] Key, byte[] IV, byte[] Data)
301 {
302 using TripleDES Cipher = TripleDES.Create();
303 Cipher.Mode = CipherMode.CBC;
304 Cipher.Padding = PaddingMode.None;
305
306 using ICryptoTransform Encryptor = Cipher.CreateEncryptor(Key, IV);
307
308 return Encryptor.TransformFinalBlock(Data, 0, Data.Length);
309 }
310
318 protected byte[] DecryptAes(byte[] Key, byte[] IV, byte[] EncryptedData)
319 {
320 using Aes Cipher = Aes.Create();
321 Cipher.Mode = CipherMode.CBC;
322 Cipher.Padding = PaddingMode.None;
323 Cipher.BlockSize = this.BlockLength << 3;
324 Cipher.KeySize = Key.Length << 3;
325
326 using ICryptoTransform Decryptor = Cipher.CreateDecryptor(Key, IV);
327
328 return Decryptor.TransformFinalBlock(EncryptedData, 0, EncryptedData.Length);
329 }
330
338 protected byte[] Decrypt3Des(byte[] Key, byte[] IV, byte[] EncryptedData)
339 {
340 using TripleDES Cipher = TripleDES.Create();
341 Cipher.Mode = CipherMode.CBC;
342 Cipher.Padding = PaddingMode.None;
343
344 using ICryptoTransform Decryptor = Cipher.CreateDecryptor(Key, IV);
345
346 return Decryptor.TransformFinalBlock(EncryptedData, 0, EncryptedData.Length);
347 }
348
352 protected static readonly byte[] zeroIv16 = new byte[16];
353
357 protected static readonly byte[] zeroIv8 = new byte[8];
358 }
359}
Contains parsed information from a machine-readable document information string.
Abstract base class for security objects.
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeSHA1Hash(byte[] Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:415
Grade
Grade enumeration
Definition: Grade.cs:7
delegate byte[] HashFunctionArray(byte[] Data)
Delegate to hash function.