forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurve448.cs
More file actions
218 lines (187 loc) · 8.19 KB
/
Curve448.cs
File metadata and controls
218 lines (187 loc) · 8.19 KB
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
using System;
using System.Globalization;
using System.IO;
using System.Numerics;
namespace Waher.Security.EllipticCurves
{
/// <summary>
/// Curve448 (Goldilocks), as defined in RFC 7748:
/// https://tools.ietf.org/html/rfc7748
/// </summary>
public class Curve448 : MontgomeryCurve
{
private static readonly BigInteger p0 = BigInteger.Pow(2, 448) - BigInteger.Pow(2, 224) - 1;
private static readonly BigInteger A0 = 156326;
private static readonly BigInteger A24 = (A0 - 2) / 4;
private static readonly BigInteger n0 = BigInteger.Pow(2, 446) - BigInteger.Parse("008335dc163bb124b65129c96fde933d8d723a70aadc873d6d54a7bb0d", NumberStyles.HexNumber);
private static readonly BigInteger BasePointU = 5;
private static readonly BigInteger BasePointV = BigInteger.Parse("355293926785568175264127502063783334808976399387714271831880898435169088786967410002932673765864550910142774147268105838985595290606362");
/// <summary>
/// Curve448 (Goldilocks), as defined in RFC 7748:
/// https://tools.ietf.org/html/rfc7748
/// </summary>
public Curve448()
: base(p0, new PointOnCurve(BasePointU, BasePointV), n0, 4)
{
}
/// <summary>
/// Curve448 (Goldilocks), as defined in RFC 7748:
/// https://tools.ietf.org/html/rfc7748
/// </summary>
/// <param name="Secret">Secret.</param>
public Curve448(byte[] Secret)
: base(p0, new PointOnCurve(BasePointU, BasePointV), n0, 4, Secret)
{
}
/// <summary>
/// Name of curve.
/// </summary>
public override string CurveName => "Curve448";
/// <summary>
/// a Coefficient in the definition of the curve E: v²=u³+A*u²+u
/// </summary>
protected override BigInteger A => A0;
/// <summary>
/// Converts a pair of (U,V) coordinates to a pair of (X,Y) coordinates
/// in the birational Edwards curve.
/// </summary>
/// <param name="UV">(U,V) coordinates.</param>
/// <returns>(X,Y) coordinates.</returns>
public override PointOnCurve ToXY(PointOnCurve UV)
{
BigInteger U2 = this.modP.Multiply(UV.X, UV.X);
BigInteger U3 = this.modP.Multiply(U2, UV.X);
BigInteger U4 = this.modP.Multiply(U2, U2);
BigInteger U5 = this.modP.Multiply(U3, U2);
BigInteger V2 = this.modP.Multiply(UV.Y, UV.Y);
BigInteger TwoU2 = U2 << 1;
BigInteger TwoU3 = U3 << 1;
BigInteger X = this.modP.Divide(this.modP.Multiply(UV.Y << 2, U2 - 1),
(U4 - TwoU2 + (V2 << 2) + BigInteger.One));
BigInteger Y = this.modP.Divide(-(U5 - TwoU3 -
this.modP.Multiply((UV.X << 2), V2) + UV.X),
(U5 - this.modP.Multiply(TwoU2, V2) - TwoU3 - (V2 << 1) + UV.X));
if (X.Sign < 0)
X += this.p;
if (Y.Sign < 0)
Y += this.p;
return new PointOnCurve(X, Y);
}
/// <summary>
/// Converts a pair of (X,Y) coordinates for the birational Edwards curve
/// to a pair of (U,V) coordinates.
/// </summary>
/// <param name="XY">(X,Y) coordinates.</param>
/// <returns>(U,V) coordinates.</returns>
public override PointOnCurve ToUV(PointOnCurve XY)
{
BigInteger X2 = this.modP.Multiply(XY.X, XY.X);
BigInteger Y2 = this.modP.Multiply(XY.Y, XY.Y);
BigInteger U = this.modP.Divide(Y2, X2);
BigInteger X3 = this.modP.Multiply(XY.X, X2);
BigInteger V = this.modP.Divide(this.modP.Multiply(Two - X2 - Y2, XY.Y), X3);
if (U.Sign < 0)
U += this.p;
if (V.Sign < 0)
V += this.p;
return new PointOnCurve(U, V);
}
/// <summary>
/// Performs the scalar multiplication of <paramref name="N"/>*<paramref name="U"/>.
/// </summary>
/// <param name="N">Scalar</param>
/// <param name="U">U-coordinate of point</param>
/// <returns><paramref name="N"/>*<paramref name="U"/></returns>
public override BigInteger ScalarMultiplication(byte[] N, BigInteger U)
{
return XFunction(N, U, A24, this.p, 448);
}
/// <summary>
/// Calculates a private key from a secret.
/// </summary>
/// <param name="Secret">Binary secret.</param>
/// <returns>Private key</returns>
public override Tuple<byte[], byte[]> CalculatePrivateKey(byte[] Secret)
{
byte[] Bin = Secret;
switch (Bin.Length)
{
case 56:
Array.Resize<byte>(ref Bin, 57);
break;
case 57:
break;
default:
Bin = Hashes.ComputeSHA512Hash(Secret);
Array.Resize<byte>(ref Bin, 57);
break;
}
Bin[0] &= 0xfc;
Bin[55] |= 0x80;
Bin[56] |= 0;
return new Tuple<byte[], byte[]>(Bin, null);
}
/// <summary>
/// Creates the Edwards Curve pair.
/// </summary>
/// <returns>Edwards curve.</returns>
public override EdwardsCurveBase CreatePair()
{
PointOnCurve PublicKeyUV = this.PublicKeyPoint;
PointOnCurve PublicKeyXY = this.ToXY(PublicKeyUV);
Edwards448 Candidate = new Edwards448(this.PrivateKey, false);
PointOnCurve PublicKeyXY2 = Candidate.PublicKeyPoint;
PublicKeyXY2 = Candidate.ScalarMultiplication(4, PublicKeyXY2, true);
if (PublicKeyXY.Y.Equals(PublicKeyXY2.Y))
return Candidate;
else
throw new InvalidOperationException("Unable to create pair curve.");
}
/// <summary>
/// Creates a signature of <paramref name="Data"/> using the XEdDSA algorithm.
/// </summary>
/// <param name="Data">Payload to sign.</param>
/// <returns>Signature.</returns>
public override byte[] Sign(byte[] Data)
{
throw new NotSupportedException("Signatures not supported.");
//return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
}
/// <summary>
/// Creates a signature of <paramref name="Data"/> using the XEdDSA algorithm.
/// </summary>
/// <param name="Data">Payload to sign.</param>
/// <returns>Signature.</returns>
public override byte[] Sign(Stream Data)
{
throw new NotSupportedException("Signatures not supported.");
//return XEdDSA.Sign(Data, this.PrivateKey, Hashes.ComputeSHA512Hash, this);
}
/// <summary>
/// Verifies a signature of <paramref name="Data"/> made by the EdDSA algorithm.
/// </summary>
/// <param name="Data">Payload to sign.</param>
/// <param name="PublicKey">Public Key of the entity that generated the signature.</param>
/// <param name="Signature">Signature</param>
/// <returns>If the signature is valid.</returns>
public override bool Verify(byte[] Data, byte[] PublicKey, byte[] Signature)
{
throw new NotSupportedException("Signatures not supported.");
//return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
// Signature, 448, 446);
}
/// <summary>
/// Verifies a signature of <paramref name="Data"/> made by the EdDSA algorithm.
/// </summary>
/// <param name="Data">Payload to sign.</param>
/// <param name="PublicKey">Public Key of the entity that generated the signature.</param>
/// <param name="Signature">Signature</param>
/// <returns>If the signature is valid.</returns>
public override bool Verify(Stream Data, byte[] PublicKey, byte[] Signature)
{
throw new NotSupportedException("Signatures not supported.");
//return XEdDSA.Verify(Data, PublicKey, Hashes.ComputeSHA512Hash, this,
// Signature, 448, 446);
}
}
}