/
PrivateKey.cs
419 lines (383 loc) · 18.1 KB
/
PrivateKey.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Security;
using ECPoint = Org.BouncyCastle.Math.EC.ECPoint;
namespace Libplanet.Crypto
{
/// <summary>
/// A secret part of a key pair involved in
/// <a href="https://en.wikipedia.org/wiki/ECDSA">ECDSA</a>, the digital
/// signature algorithm on which the Libplanet is based. It can be used to
/// create signatures, which can be verified with the corresponding
/// <see cref="Libplanet.Crypto.PublicKey"/>, as well as to decrypt
/// messages which were encrypted with the corresponding
/// <see cref="Libplanet.Crypto.PublicKey"/>.
/// <para>Note that it uses <a href="https://en.bitcoin.it/wiki/Secp256k1"
/// >secp256k1</a> as the parameters of the elliptic curve, which is
/// the same to <a href="https://bitcoin.org/">Bitcoin</a> and
/// <a href="https://www.ethereum.org/">Ethereum</a>.
/// It means private keys generated for Bitcoin/Ethereum can be used by
/// Libplanet-backed games/apps too.</para>
/// </summary>
/// <remarks>
/// These (and any derived representations, e.g., <see cref="ByteArray"/>)
/// must be kept secret, if they are exposed, an attacker will be able to
/// forge signatures.
/// <para>Every <see cref="PrivateKey"/> object is immutable.</para>
/// </remarks>
/// <seealso cref="Libplanet.Crypto.PublicKey"/>
[Equals]
public class PrivateKey
{
private const int KeyByteSize = 32;
private PublicKey? _publicKey;
/// <summary>
/// Generates a new unique <see cref="PrivateKey"/> instance.
/// It can be analogous to creating a new account in a degree.
/// </summary>
public PrivateKey()
: this(GenerateKeyParam())
{
}
/// <summary>
/// Creates a <see cref="PrivateKey"/> instance from the given <see cref="byte"/>s (i.e.,
/// <paramref name="privateKey"/>), which encodes a valid
/// <a href="https://en.wikipedia.org/wiki/ECDSA">ECDSA</a> private key.
/// </summary>
/// <param name="privateKey">A valid <see cref="byte"/>s that encodes an ECDSA private key.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the given
/// <paramref name="privateKey"/> is too short or too long.</exception>
/// <remarks>A valid <see cref="byte"/> array for a <see cref="PrivateKey"/> can be encoded
/// using <see cref="ByteArray"/> property.</remarks>
/// <seealso cref="ByteArray"/>
public PrivateKey(IReadOnlyList<byte> privateKey)
: this(privateKey is byte[] ba ? ba : privateKey.ToArray(), informedConsent: true)
{
if (privateKey.Count != KeyByteSize)
{
throw new ArgumentOutOfRangeException(
nameof(privateKey),
$"The key must be {KeyByteSize} bytes."
);
}
}
internal PrivateKey(byte[] unverifiedKey, bool informedConsent)
: this(GenerateKeyFromBytes(unverifiedKey))
{
// The `informedConsent` parameter mainly purposes to prevent this overload from
// being chosen instead of PrivatKey(IReadOnly<byte>) by mistake.
if (!informedConsent)
{
throw new ArgumentException(
nameof(informedConsent),
"The caller should ensure the key is valid and safe enough."
);
}
}
private PrivateKey(ECPrivateKeyParameters keyParam)
{
KeyParam = keyParam;
}
/// <summary>
/// The corresponding <see cref="Libplanet.Crypto.PublicKey"/> of
/// this private key.
/// </summary>
[IgnoreDuringEquals]
public PublicKey PublicKey
{
get
{
if (_publicKey is null)
{
ECDomainParameters ecParams = GetECParameters();
ECPoint q = ecParams.G.Multiply(KeyParam.D);
var kp = new ECPublicKeyParameters("ECDSA", q, ecParams);
_publicKey = new PublicKey(kp);
}
return _publicKey;
}
}
/// <summary>
/// An encoded <see cref="byte"/> array representation.
/// </summary>
/// <remarks>
/// An encoded <see cref="byte"/> array representation can be recovered to a <see
/// cref="PrivateKey"/> instance again using <see cref="PrivateKey(IReadOnlyList{byte})"/>
/// constructor.
/// <para>As like <see cref="PrivateKey"/> instances, it also must be kept secret.
/// In practice, this must not be sent over the network, and be securely stored in the file
/// system. If you just want to store the in-memory private key in the persistent storage,
/// use <see cref="KeyStore.ProtectedPrivateKey"/> or <see cref="KeyStore.Web3KeyStore"/>.
/// </para>
/// <para>To get a mutable array instead of immutable one, use <see cref="ToByteArray()"/>
/// method instead.</para>
/// </remarks>
/// <seealso cref="ToByteArray()"/>
/// <seealso cref="PrivateKey(IReadOnlyList{byte})"/>
[Pure]
public ImmutableArray<byte> ByteArray => ToByteArray().ToImmutableArray();
internal ECPrivateKeyParameters KeyParam { get; }
public static bool operator ==(PrivateKey left, PrivateKey right) =>
Operator.Weave(left, right);
public static bool operator !=(PrivateKey left, PrivateKey right) =>
Operator.Weave(left, right);
/// <summary>
/// Creates a <see cref="PrivateKey"/> instance from hexadecimal string of bytes.
/// </summary>
/// <param name="hex">A hexadecimal string of a private key's
/// <see cref="ByteArray"/>.</param>
/// <returns>A created <see cref="PrivateKey"/> instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when the given <paramref name="hex"/>
/// string is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the length of the given
/// <paramref name="hex"/> string is too short or too long.</exception>
/// <exception cref="FormatException">Thrown when the given <paramref name="hex"/> string is
/// not a valid hexadecimal string.</exception>
[Pure]
public static PrivateKey FromString(string hex)
{
byte[] bytes = ByteUtil.ParseHex(hex);
if (bytes.Length != KeyByteSize)
{
throw new ArgumentOutOfRangeException(
nameof(hex),
$"Expected {KeyByteSize * 2} hexadecimal digits."
);
}
return new PrivateKey(unverifiedKey: bytes, informedConsent: true);
}
/// <summary>
/// Creates a signature from the given <paramref name="message"/>.
/// <para>
/// A created signature can be verified by the corresponding
/// <see cref="PublicKey"/>.
/// </para>
/// <para>
/// Signatures can be created by only the <see cref="PrivateKey"/>
/// which corresponds a <see cref="PublicKey"/> to verify these
/// signatures.
/// </para>
/// <para>
/// To sum up, a signature is used to guarantee:
/// </para>
/// <list type="bullet">
/// <item><description>that the <paramref name="message"/> was created
/// by someone possessing the corresponding <see cref="PrivateKey"/>,
/// </description></item>
/// <item><description>that the possessor cannot deny having sent the
/// <paramref name="message"/>, and</description></item>
/// <item><description>that the <paramref name="message"/> was not
/// forged in the middle of transit.</description></item>
/// </list>
/// </summary>
/// <param name="message">A message <see cref="byte"/>s to sign.</param>
/// <returns>A signature that proves the authenticity of the <paramref name="message"/>.
/// It can be verified using <see cref="Libplanet.Crypto.PublicKey.Verify"/> method.
/// </returns>
/// <seealso cref="Libplanet.Crypto.PublicKey.Verify"/>
public byte[] Sign(byte[] message)
{
HashDigest<SHA256> hashed = HashDigest<SHA256>.DeriveFrom(message);
return CryptoConfig.CryptoBackend.Sign(hashed, this);
}
/// <summary>
/// Creates a signature from the given <paramref name="message"/>.
/// <para>A created signature can be verified by the corresponding <see cref="PublicKey"/>.
/// </para>
/// <para>Signatures can be created by only the <see cref="PrivateKey"/> which corresponds
/// a <see cref="PublicKey"/> to verify these signatures.</para>
/// <para>To sum up, a signature is used to guarantee:</para>
/// <list type="bullet">
/// <item><description>that the <paramref name="message"/> was created by someone possessing
/// the corresponding <see cref="PrivateKey"/>,</description></item>
/// <item><description>that the possessor cannot deny having sent the
/// <paramref name="message"/>, and</description></item>
/// <item><description>that the <paramref name="message"/> was not forged in the middle of
/// transit.</description></item>
/// </list>
/// </summary>
/// <param name="message">A message <see cref="byte"/>s to sign.</param>
/// <returns>A signature that proves the authenticity of the <paramref name="message"/>.
/// It can be verified using <see cref="Libplanet.Crypto.PublicKey.Verify"/> method.
/// </returns>
/// <seealso cref="Libplanet.Crypto.PublicKey.Verify"/>
public ImmutableArray<byte> Sign(ImmutableArray<byte> message)
{
HashDigest<SHA256> hashed = HashDigest<SHA256>.DeriveFrom(message);
byte[] sig = CryptoConfig.CryptoBackend.Sign(hashed, this);
return Unsafe.As<byte[], ImmutableArray<byte>>(ref sig);
}
/// <summary>
/// Decrypts a <paramref name="ciphertext"/> which was encrypted with the corresponding
/// <see cref="PublicKey"/> to the original plain text.
/// </summary>
/// <param name="ciphertext">The encrypted message.</param>
/// <returns>The decrypted original message.</returns>
/// <exception cref="InvalidCiphertextException">Thrown when the given
/// <paramref name="ciphertext"/> is invalid.</exception>
/// <remarks>
/// Although the parameter name <paramref name="ciphertext"/> has the
/// word “text”, both a <paramref name="ciphertext"/> and a returned message
/// are <see cref="byte"/>s, not Unicode <see cref="string"/>s.
/// </remarks>
/// <seealso cref="Libplanet.Crypto.PublicKey.Encrypt(byte[])"/>
[Pure]
public byte[] Decrypt(byte[] ciphertext)
{
PublicKey pubKey = new PublicKey(ciphertext.Take(33).ToArray());
SymmetricKey aes = ExchangeKey(pubKey);
return aes.Decrypt(ciphertext, 33);
}
/// <summary>
/// Decrypts a <paramref name="ciphertext"/> which was encrypted with the corresponding
/// <see cref="PublicKey"/> to the original plain text.
/// </summary>
/// <param name="ciphertext">The encrypted message.</param>
/// <returns>The decrypted original message.</returns>
/// <exception cref="InvalidCiphertextException">Thrown when the given
/// <paramref name="ciphertext"/> is invalid.</exception>
/// <remarks>
/// Although the parameter name <paramref name="ciphertext"/> has the
/// word “text”, both a <paramref name="ciphertext"/> and a returned message
/// are <see cref="byte"/>s, not Unicode <see cref="string"/>s.
/// </remarks>
/// <seealso cref="Libplanet.Crypto.PublicKey.Encrypt(ImmutableArray{byte})"/>
[Pure]
public ImmutableArray<byte> Decrypt(ImmutableArray<byte> ciphertext) =>
Decrypt(ciphertext.ToBuilder().ToArray()).ToImmutableArray();
/// <summary>
/// Securely exchange a <see cref="SymmetricKey"/> with a peer's
/// <see cref="PublicKey"/>.
/// Two parties can agree on a (new, unique, and typically temporal)
/// key without revealing to any eavesdropping party what key has been
/// agreed upon.
/// <para>Technically it is <a href="https://en.wikipedia.org/wiki/ECDH"
/// >ECDH</a>, a <a
/// href="https://en.wikipedia.org/wiki/DH_key_exchange"
/// >Diffie–Hellman key exchange</a> of elliptic-curve version.
/// </para>
/// </summary>
/// <param name="publicKey">The <see cref="PublicKey"/> possessed by
/// a peer to whom exchange a private key with.</param>
/// <returns>An exchanged (agreed) <see cref="SymmetricKey"/>.
/// Note that it is not an elliptic-curve private key, but an <a
/// href="https://en.wikipedia.org/wiki/Advanced_Encryption_Standard"
/// >AES</a> key.</returns>
[Pure]
public SymmetricKey ExchangeKey(PublicKey publicKey)
{
ECPoint p = CalculatePoint(publicKey.KeyParam);
BigInteger x = p.AffineXCoord.ToBigInteger();
BigInteger y = p.AffineYCoord.ToBigInteger();
byte[] xbuf = x.ToByteArrayUnsigned();
var ybuf = new byte[] { (byte)(y.TestBit(0) ? 0x03 : 0x02) };
var hash = new Sha256Digest();
var result = new byte[hash.GetDigestSize()];
hash.BlockUpdate(ybuf, 0, ybuf.Length);
hash.BlockUpdate(xbuf, 0, xbuf.Length);
hash.DoFinal(result, 0);
return new SymmetricKey(result);
}
/// <summary>
/// Encodes the private key into a corresponding mutable <see cref="byte"/> array
/// representation.
/// </summary>
/// <returns>An encoded <see cref="byte"/> array representation. It guarantees that
/// returned arrays are never reused, and mutating on them does not affect
/// <see cref="PrivateKey"/> instance's internal states.</returns>
/// <remarks>
/// An encoded <see cref="byte"/> array representation can be recovered to a <see
/// cref="PrivateKey"/> instance again using <see cref="PrivateKey(IReadOnlyList{byte})"/>
/// constructor.
/// <para>As like <see cref="PrivateKey"/> instances, it also must be kept secret.
/// In practice, this must not be sent over the network, and be securely stored in the file
/// system. If you just want to store the in-memory private key in the persistent storage,
/// use <see cref="KeyStore.ProtectedPrivateKey"/> or <see cref="KeyStore.Web3KeyStore"/>.
/// </para>
/// <para>To get an immutable array instead of mutable one, use <see cref="ByteArray"/>
/// property.</para>
/// </remarks>
/// <seealso cref="ByteArray"/>
/// <seealso cref="PrivateKey(IReadOnlyList{byte})"/>
[Pure]
public byte[] ToByteArray() =>
KeyParam.D.ToByteArrayUnsigned();
internal static ECDomainParameters GetECParameters()
{
return GetECParameters("secp256k1");
}
private static ECDomainParameters GetECParameters(string name)
{
X9ECParameters ps = SecNamedCurves.GetByName(name);
return new ECDomainParameters(ps.Curve, ps.G, ps.N, ps.H);
}
private static ECPrivateKeyParameters GenerateKeyParam()
{
var gen = new ECKeyPairGenerator();
var secureRandom = new SecureRandom();
ECDomainParameters ecParams = GetECParameters();
var keyGenParam =
new ECKeyGenerationParameters(ecParams, secureRandom);
gen.Init(keyGenParam);
return (ECPrivateKeyParameters)gen.GenerateKeyPair().Private;
}
private static ECPrivateKeyParameters GenerateKeyFromBytes(byte[] privateKey)
{
var param = new ECPrivateKeyParameters(
"ECDSA",
new BigInteger(1, privateKey),
GetECParameters()
);
// For sanity check.
#pragma warning disable SA1312, S1481
var _ = new PrivateKey(param).PublicKey;
#pragma warning restore SA1312, S1481
return param;
}
private ECPoint CalculatePoint(ECPublicKeyParameters pubKeyParams)
{
ECDomainParameters dp = KeyParam.Parameters;
if (!dp.Equals(pubKeyParams.Parameters))
{
throw new InvalidOperationException(
"ECDH public key has wrong domain parameters"
);
}
BigInteger d = KeyParam.D;
ECPoint q = dp.Curve.DecodePoint(pubKeyParams.Q.GetEncoded(true));
if (q.IsInfinity)
{
throw new InvalidOperationException(
"Infinity is not a valid public key for ECDH"
);
}
BigInteger h = dp.H;
if (!h.Equals(BigInteger.One))
{
d = dp.H.ModInverse(dp.N).Multiply(d).Mod(dp.N);
q = ECAlgorithms.ReferenceMultiply(q, h);
}
ECPoint p = q.Multiply(d).Normalize();
if (p.IsInfinity)
{
throw new InvalidOperationException(
"Infinity is not a valid agreement value for ECDH"
);
}
return p;
}
}
}