Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ParameterEncryptionAlgorithm.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
6
8{
14 {
15 private readonly ContractsClient client;
16 private readonly SymmetricCipherAlgorithms algorithm;
17 private readonly IE2eSymmetricCipher instance;
18 private readonly byte[] key;
19
21 IE2eSymmetricCipher Instance, byte[] Key, ContractsClient Client)
22 {
23 this.algorithm = Algorithm;
24 this.instance = Instance;
25 this.key = Key;
26 this.client = Client;
27 }
28
32 public byte[] Key => this.key;
33
37 public SymmetricCipherAlgorithms Algorithm => this.algorithm;
38
45 public static Task<ParameterEncryptionAlgorithm> Create(SymmetricCipherAlgorithms Algorithm, ContractsClient Client)
46 {
47 return Create(null, Algorithm, Client);
48 }
49
57 public static async Task<ParameterEncryptionAlgorithm> Create(string ContractId, SymmetricCipherAlgorithms DefaultAlgorithm,
58 ContractsClient Client)
59 {
60 string CreatorJid = Client.Client.BareJID;
61 byte[] Key = null;
62
63 if (!string.IsNullOrEmpty(ContractId))
64 {
65 Tuple<SymmetricCipherAlgorithms, string, byte[]> T = await Client.TryLoadContractSharedSecret(ContractId);
66
67 if (!(T.Item3 is null))
68 {
69 DefaultAlgorithm = T.Item1;
70 CreatorJid = T.Item2;
71 Key = T.Item3;
72 }
73 }
74
75 return await Create(ContractId, DefaultAlgorithm, Client, CreatorJid, Key);
76 }
77
87 public static async Task<ParameterEncryptionAlgorithm> Create(string ContractId, SymmetricCipherAlgorithms Algorithm,
88 ContractsClient Client, string CreatorJid, byte[] Key)
89 {
91
92 if (Key is null)
93 {
94 Key = Instance.GenerateKey();
95
96 if (!string.IsNullOrEmpty(ContractId))
97 await Client.SaveContractSharedSecret(ContractId, CreatorJid, Key, Algorithm, false);
98 }
99
100 return new ParameterEncryptionAlgorithm(Algorithm, Instance, Key, Client);
101 }
102
113 public byte[] Encrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce,
114 string ClearText)
115 {
116 byte[] Data;
117
118 Data = new byte[this.key.Length + ContractNonce.Length + 4];
119 int c = this.key.Length;
120 int d = ContractNonce.Length;
121
122 Buffer.BlockCopy(this.key, 0, Data, 0, c);
123 Buffer.BlockCopy(ContractNonce, 0, Data, c, d);
124 Buffer.BlockCopy(BitConverter.GetBytes(ParameterIndex), 0, Data, c + d, 4);
125
126 Data = Hashes.ComputeSHA256Hash(Data);
127
128 int SuffixLength = Data[0];
129
130 if (ClearText is null)
131 Data = new byte[SuffixLength];
132 else
133 {
134 int Prefix = -1;
135 int i, j;
136
137 for (i = 1, c = Data.Length; i < c; i++)
138 {
139 if ((j = Data[i]) != 0)
140 {
141 Prefix = j;
142 break;
143 }
144 }
145
146 if (Prefix < 0)
147 Prefix = 1;
148
149 byte[] Data2 = Encoding.UTF8.GetBytes(ClearText);
150 c = Data2.Length;
151
152 Data = new byte[1 + c + SuffixLength];
153
154 Data[0] = (byte)Prefix;
155 Buffer.BlockCopy(Data2, 0, Data, 1, c);
156 }
157
158 byte[] IV = this.instance.GetIV(ParameterName, ParameterType, CreatorJid,
159 Convert.ToBase64String(ContractNonce), ParameterIndex);
160 byte[] AssociatedData = Encoding.UTF8.GetBytes(ParameterName);
161
162 byte[] Result = this.instance.Encrypt(Data, this.key, IV, AssociatedData, E2eBufferFillAlgorithm.Zeroes);
163
164 return Result;
165 }
166
177 public string Decrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce,
178 byte[] CipherText)
179 {
180 byte[] IV = this.instance.GetIV(ParameterName, ParameterType, CreatorJid, Convert.ToBase64String(ContractNonce), ParameterIndex);
181 byte[] AssociatedData = Encoding.UTF8.GetBytes(ParameterName);
182 byte[] Data = this.instance.Decrypt(CipherText, this.key, IV, AssociatedData);
183
184 if (Data.Length == 0 || Data[0] == 0)
185 return null;
186
187 int c = Data.Length - 1;
188
189 while (c >= 0 && Data[c] == 0)
190 c--;
191
192 return Encoding.UTF8.GetString(Data, 1, c);
193 }
194 }
195}
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Implements parameter encryption using symmetric ciphers avaialble through IE2eSymmetricCipher in the ...
static Task< ParameterEncryptionAlgorithm > Create(SymmetricCipherAlgorithms Algorithm, ContractsClient Client)
Implements parameter encryption using symmetric ciphers avaialble through IE2eSymmetricCipher in the ...
SymmetricCipherAlgorithms Algorithm
Symmetric Cipher Algorithm used to encrypt parameters.
static async Task< ParameterEncryptionAlgorithm > Create(string ContractId, SymmetricCipherAlgorithms DefaultAlgorithm, ContractsClient Client)
Creates a object that implements parameter encryption using symmetric ciphers avaialble through IE2eS...
static async Task< ParameterEncryptionAlgorithm > Create(string ContractId, SymmetricCipherAlgorithms Algorithm, ContractsClient Client, string CreatorJid, byte[] Key)
Creates a object that implements parameter encryption using symmetric ciphers avaialble through IE2eS...
string Decrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce, byte[] CipherText)
Decrypts an encrypted parameter value.
byte[] Encrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce, string ClearText)
Encrypts a parameter value.
static IE2eSymmetricCipher Create(SymmetricCipherAlgorithms Algorithm)
Creates an instance of a symmetric cipher algorithm.
XmppClient Client
XMPP Client.
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:469
Interface for symmetric ciphers.
byte[] GenerateKey()
Generates a new key. Used when the asymmetric cipher cannot calculate a shared secret.
SymmetricCipherAlgorithms
Enumeration of symmetric cipher algorithms available in the library.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.