forked from AlexMAS/GostCryptography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGostKeyExchangeFormatter.cs
160 lines (132 loc) · 5.08 KB
/
GostKeyExchangeFormatter.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
using System;
using System.Security.Cryptography;
using GostCryptography.Asn1.Common;
using GostCryptography.Properties;
namespace GostCryptography.Cryptography
{
/// <summary>
/// Реализует шифрацию общего секретного ключа по ГОСТ Р 34.10.
/// </summary>
public sealed class GostKeyExchangeFormatter : AsymmetricKeyExchangeFormatter
{
/// <summary>
/// Конструктор.
/// </summary>
public GostKeyExchangeFormatter()
{
}
/// <summary>
/// Конструктор.
/// </summary>
/// <param name="publicKey">Открытый ключ для шифрации общего секретного ключа.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public GostKeyExchangeFormatter(AsymmetricAlgorithm publicKey)
{
SetKey(publicKey);
}
private Gost3410AsymmetricAlgorithmBase _publicKey;
/// <summary>
/// Параметры алгоритма.
/// </summary>
public override string Parameters
{
get { return null; }
}
/// <summary>
/// Устанавливает открытый ключ для шифрации общего секретного ключа.
/// </summary>
/// <param name="publicKey">Открытый ключ для шифрации общего секретного ключа.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public override void SetKey(AsymmetricAlgorithm publicKey)
{
if (publicKey == null)
{
throw ExceptionUtility.ArgumentNull("publicKey");
}
if (!(publicKey is Gost3410AsymmetricAlgorithmBase))
{
throw ExceptionUtility.ArgumentOutOfRange("publicKey", Resources.ShouldSupportGost3410);
}
_publicKey = (Gost3410AsymmetricAlgorithmBase)publicKey;
}
/// <summary>
/// Шифрует общий секретный ключ.
/// </summary>
/// <param name="keyExchangeData">Общий секретный ключ.</param>
/// <exception cref="ArgumentNullException"></exception>
public override byte[] CreateKeyExchange(byte[] keyExchangeData)
{
if (keyExchangeData == null)
{
throw ExceptionUtility.ArgumentNull("keyExchangeData");
}
using (var keyExchangeAlgorithm = new Gost28147SymmetricAlgorithm())
{
keyExchangeAlgorithm.Key = keyExchangeData;
return CreateKeyExchangeData(keyExchangeAlgorithm);
}
}
/// <summary>
/// Шифрует общий секретный ключ.
/// </summary>
/// <param name="keyExchangeData">Общий секретный ключ.</param>
/// <param name="keyExchangeAlgorithmType">Тип алгоритма шифрации общего секретного ключа.</param>
/// <exception cref="ArgumentNullException"></exception>
/// <returns></returns>
public override byte[] CreateKeyExchange(byte[] keyExchangeData, Type keyExchangeAlgorithmType)
{
if (keyExchangeData == null)
{
throw ExceptionUtility.ArgumentNull("keyExchangeData");
}
using (var keyExchangeAlgorithm = (SymmetricAlgorithm)Activator.CreateInstance(keyExchangeAlgorithmType))
{
keyExchangeAlgorithm.Key = keyExchangeData;
return CreateKeyExchangeData(keyExchangeAlgorithm);
}
}
/// <summary>
/// Шифрует общий секретный ключ.
/// </summary>
/// <param name="keyExchangeAlgorithm">Алгоритм шифрации общего секретного ключа.</param>
/// <exception cref="ArgumentNullException"></exception>
public byte[] CreateKeyExchangeData(SymmetricAlgorithm keyExchangeAlgorithm)
{
if (keyExchangeAlgorithm == null)
{
throw ExceptionUtility.ArgumentNull("keyExchangeAlgorithm");
}
var keyExchangeInfo = CreateKeyExchangeInfo(keyExchangeAlgorithm);
return keyExchangeInfo.Encode();
}
/// <summary>
/// Шифрует общий секретный ключ.
/// </summary>
/// <param name="keyExchangeAlgorithm">Алгоритм шифрации общего секретного ключа.</param>
/// <exception cref="ArgumentNullException"></exception>
public GostKeyExchange CreateKeyExchangeInfo(SymmetricAlgorithm keyExchangeAlgorithm)
{
if (keyExchangeAlgorithm == null)
{
throw ExceptionUtility.ArgumentNull("keyExchangeAlgorithm");
}
var keyExchange = new GostKeyExchange();
var keyExchangeParameters = _publicKey.ExportParameters(false);
using (var keyExchangeAsym = new Gost3410EphemeralAsymmetricAlgorithm(keyExchangeParameters))
{
byte[] encodedKeyExchangeInfo;
using (var keyExchangeAlg = keyExchangeAsym.CreateKeyExchange(keyExchangeParameters))
{
encodedKeyExchangeInfo = keyExchangeAlg.EncodeKeyExchange(keyExchangeAlgorithm, GostKeyExchangeExportMethod.CryptoProKeyExport);
}
var keyExchangeInfo = new GostKeyExchangeInfo();
keyExchangeInfo.Decode(encodedKeyExchangeInfo);
keyExchange.SessionEncryptedKey = keyExchangeInfo;
keyExchange.TransportParameters = keyExchangeAsym.ExportParameters(false);
}
return keyExchange;
}
}
}