This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathKeyHelper.cs
157 lines (133 loc) · 6.59 KB
/
KeyHelper.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
using System.Collections.Generic;
using NSec.Cryptography;
using Omemo.Classes.Keys;
using X25519;
namespace Omemo.Classes
{
public static class KeyHelper
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
public static int PUB_KEY_SIZE = SignatureAlgorithm.Ed25519.PublicKeySize;
public static int PRIV_KEY_SIZE = SignatureAlgorithm.Ed25519.PrivateKeySize;
#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--
#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--
#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--
/// <summary>
/// Generates a new Ed25519 <see cref="IdentityKeyPairModel"/> and returns it.
/// </summary>
public static IdentityKeyPairModel GenerateIdentityKeyPair()
{
GenericECKeyPairModel pair = GenerateKeyPair();
return new IdentityKeyPairModel(pair.privKey, pair.pubKey);
}
/// <summary>
/// Generates a list of <see cref="PreKeyModel"/>s and returns them.
/// <para/>
/// To keep the <see cref="PreKeyModel"/>-IDs unique ensure to set start to (start + count) of the last run.
/// Rolls over to one if ids grow larger than 0x7FFFFFFF (2^31-1).
/// </summary>
/// <param name="start">The start it of the new <see cref="PreKeyModel"/>.</param>
/// <param name="count">How many <see cref="PreKeyModel"/>s should be generated.</param>
public static List<PreKeyModel> GeneratePreKeys(uint start, uint count)
{
const uint MIN_VALID_INDEX = 1;
const uint MAX_VALID_INDEX = 0x7FFFFFFF; // 2^31-1
uint keyId = start;
List<PreKeyModel> preKeys = new List<PreKeyModel>();
for (uint i = 0; i < count; i++)
{
if (keyId > MAX_VALID_INDEX)
{
keyId = MIN_VALID_INDEX;
}
else
{
keyId++;
}
preKeys.Add(GeneratePreKey(keyId));
}
return preKeys;
}
/// <summary>
/// Generates a new Ed25519 <see cref="PreKeyModel"/> and returns it.
/// </summary>
/// <param name="id">The id of the <see cref="PreKeyModel"/>.</param>
public static PreKeyModel GeneratePreKey(uint id)
{
GenericECKeyPairModel pair = GenerateKeyPair();
return new PreKeyModel(pair.privKey, pair.pubKey, id);
}
/// <summary>
/// Generates a new Ed25519 <see cref="SignedPreKeyModel"/> and returns it.
/// </summary>
/// <param name="id">The id of the <see cref="SignedPreKeyModel"/>.</param>
/// <param name="identiyKey">The private part of an <see cref="IdentityKeyPairModel"/> used for signing.</param>
public static SignedPreKeyModel GenerateSignedPreKey(uint id, ECPrivKeyModel identiyKey)
{
PreKeyModel preKey = GeneratePreKey(id);
byte[] signature = SignPreKey(preKey, identiyKey);
return new SignedPreKeyModel(preKey, signature);
}
/// <summary>
/// Generates the signature of the given <paramref name="preKey"/> and returns it.
/// </summary>
/// <param name="preKey">The <see cref="PreKeyModel"/> that should be signed.</param>
/// <param name="identiyKey">The private Key used for signing the given <paramref name="preKey"/>.</param>
/// <returns>The signature of the given <paramref name="preKey"/>.</returns>
public static byte[] SignPreKey(PreKeyModel preKey, ECPrivKeyModel identiyKey)
{
byte[] pubKey = preKey.pubKey.ToByteArrayWithPrefix();
Key key = Key.Import(SignatureAlgorithm.Ed25519, identiyKey.key, KeyBlobFormat.RawPrivateKey);
return SignatureAlgorithm.Ed25519.Sign(key, pubKey);
}
/// <summary>
/// Verifies the signature of the given data with the given <see cref="ECPubKeyModel"/>.
/// </summary>
/// <param name="identityKey">The <see cref="ECPubKeyModel"/> of the IdentityKey used for validating the signature.</param>
/// <param name="preKey">The <see cref="ECPubKeyModel"/> of the PreKey, the signature should be verified for.</param>
/// <param name="signature">The signature that should be verified.</param>
/// <returns>True in case the signature is valid.</returns>
public static bool VerifySignature(ECPubKeyModel identityKey, ECPubKeyModel preKey, byte[] signature)
{
return SignatureAlgorithm.Ed25519.Verify(PublicKey.Import(SignatureAlgorithm.Ed25519, identityKey.key, KeyBlobFormat.RawPublicKey), preKey.ToByteArrayWithPrefix(), signature);
}
/// <summary>
/// Generates a new Ed25519 <see cref="EphemeralKeyPairModel"/> and returns it.
/// </summary>
public static EphemeralKeyPairModel GenerateEphemeralKeyPair()
{
GenericECKeyPairModel pair = GenerateKeyPair();
return new EphemeralKeyPairModel(pair.privKey, pair.pubKey);
}
/// <summary>
/// Generates a new Ed25519 <see cref="GenericECKeyPairModel"/> and returns it.
/// </summary>
public static GenericECKeyPairModel GenerateKeyPair()
{
X25519KeyPair pair = X25519KeyAgreement.GenerateKeyPair();
return new GenericECKeyPairModel(new ECPrivKeyModel(pair.PrivateKey), new ECPubKeyModel(pair.PublicKey));
}
/// <summary>
/// Generates a 32 byte long cryptographically secure random data symmetric key and returns it.
/// </summary>
public static byte[] GenerateSymetricKey()
{
return CryptoUtils.NextBytesSecureRandom(32);
}
#endregion
#region --Misc Methods (Private)--
#endregion
#region --Misc Methods (Protected)--
#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--
#endregion
}
}