-
Notifications
You must be signed in to change notification settings - Fork 0
/
SssEngine.cs
170 lines (141 loc) · 6.01 KB
/
SssEngine.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.Math;
namespace shamirsSecretSharing
{
/// <summary>
/// Crypto-Engine used to perform threshold encryption based on the shamir secret shaing scheme
/// </summary>
public class SssEngine
{
private PublicKey PublicKey
{
get;
set;
}
/// <summary>
/// Creates a SSSEngine with the given parameters
/// </summary>
/// <param name="n"> Number of shares needed to decrypt the secret</param>
/// <param name="m"> Number of shares to be created</param>
/// <param name="size"> BitSize of the prime modulo</param>
public SssEngine(uint n, uint m, uint size)
{
PublicKey = new PublicKey(n, m, size);
}
/// <summary>
/// Creates a SSSEngine with the given parameters
/// </summary>
/// <param name="pub"> Public Key containing parameters</param>
public SssEngine(PublicKey pub)
{
PublicKey = pub;
}
/// <summary>
/// Encrypt the given secret using this engine
/// </summary>
/// <param name="secret"> Public Key containing parameters</param>
/// <returns>The public key and the shares from the shamir secret sharing scheme</returns>
public Tuple<PublicKey,Share[]> Encrypt(byte [] secret)
{
return Encrypt(PublicKey, secret);
}
/// <summary>
/// Encrypt the given secret using these parameters
/// </summary>
/// <param name="n"> Number of shares needed to decrypt the secret</param>
/// <param name="m"> Number of shares to be created</param>
/// <param name="size"> BitSize of the prime modulo</param>
/// <param name="secret"> Public Key containing parameters</param>
/// <returns>The public key and the shares from the shamir secret sharing scheme</returns>
public static Tuple<PublicKey, Share[]> Encrypt(uint n, uint m, uint size, byte[] secret)
{
return Encrypt(new PublicKey(n, m, size), secret);
}
/// <summary>
/// Encrypt the given secret using this public key
/// </summary>
/// <param name="pub"> Number of shares needed to decrypt the secret</param>
/// <param name="secret"> Public Key containing parameters</param>
/// <returns>The public key and the shares from the shamir secret sharing scheme</returns>
public static Tuple<PublicKey, Share[]> Encrypt(PublicKey pub, byte[] secret)
{
if (secret.Length + 1 > (pub.ModSize / 8) - 1) throw new ArgumentException("Secret exceeds the size of the prime modulo");
// Added a padding of 0x1 to allow for zeros in the beginning.
byte[] paddedSecret = new byte[secret.Length + 1];
paddedSecret[0] = 0x1;
Array.Copy(secret, 0, paddedSecret, 1, secret.Length);
Polynomial poly = new Polynomial(pub);
poly.Init(paddedSecret);
byte[][] xValues = new byte[pub.M][];
byte[][] yValues;
// just for testing, add custom points later
for (int i = 0; i < xValues.Length; i++)
{
xValues[i] = BitConverter.GetBytes((uint)(i + 1));
}
yValues = poly.CalculatePoints(xValues);
Share[] shares = new Share[yValues.Length];
for (int i = 0; i < shares.Length; i++)
{
shares[i] = new Share(xValues[i], yValues[i]);
for (int j = 0; j < yValues[i].Length; j++)
{
yValues[i][j] = 0;
}
}
for (int i = 0; i < secret.Length; i++)
{
paddedSecret[i + 1] = 0;
}
pub.CalculateHashes(shares);
poly.Destroy();
return new Tuple<PublicKey, Share[]>(pub, shares);
}
/// <summary>
/// Decrypt the given shares using this engine
/// </summary>
/// <param name="shares"> Shares for the decryption</param>
/// <returns>The secret</returns>
public byte[] Decrypt(Share[] shares)
{
return Decrypt(PublicKey, shares);
}
/// <summary>
/// Decrypt the given shares using this public key
/// </summary>
/// <param name="pub"> The public Key</param>
/// <param name="shares"> Shares for the decryption</param>
/// <returns>The secret</returns>
public static byte[] Decrypt(PublicKey pub, Share[] shares)
{
if (shares.Length < pub.N) throw new ArgumentException(string.Format("Not enough shares. Needed: {0} Provided: {1}", pub.N, shares.Length));
if (shares.Distinct().Count() != shares.Count()) throw new ArgumentException("The provided shares contain duplicates");
for (int i = 0; i < pub.N; i++)
{
if (!pub.ContainsShare(shares[i])) throw new ArgumentException(string.Format("The share #{0} doesn't belong to this public key", i));
}
byte[][] xValues, yValues;
xValues = new byte[pub.N][];
yValues = new byte[pub.N][];
for (int i = 0; i < pub.N; i++)
{
xValues[i] = shares[i].X;
yValues[i] = shares[i].Y;
}
byte[] paddedSecret, secret;
paddedSecret = Polynomial.Reconstruct(xValues, yValues, new byte[0], pub.PrimeModulo);
if (paddedSecret[0] != 0x1) throw new FormatException("Returned secret has an incorrect padding");
secret = new byte[paddedSecret.Length - 1];
Array.Copy(paddedSecret, 1, secret, 0, paddedSecret.Length - 1);
for (int i = 0; i < paddedSecret.Length; i++)
{
paddedSecret[i] = 0;
}
return secret;
}
}
}