-
Notifications
You must be signed in to change notification settings - Fork 2
/
AlgorithmSupport.cs
360 lines (339 loc) · 12.3 KB
/
AlgorithmSupport.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
/* ---------------------------------------------------------------------------
*
* Copyright (c) Routrek Networks, Inc. All Rights Reserved..
*
* This file is a part of the Granados SSH Client Library that is subject to
* the license included in the distributed package.
* You may not use this file except in compliance with the license.
*
* ---------------------------------------------------------------------------
*/
using System;
using Windows.Security.Cryptography.Core;
using GranadosRT.Routrek.Crypto;
using System.Runtime.InteropServices.WindowsRuntime;
namespace GranadosRT.Routrek.SSHC
{
/*
* Cipher
* The numbers at the tail of the class names indicates the version of SSH protocol.
* The difference between V1 and V2 is the CBC procedure
*/
internal interface Cipher {
void Encrypt(byte[] data, int offset, int len, byte[] result, int result_offset);
void Decrypt(byte[] data, int offset, int len, byte[] result, int result_offset);
int BlockSize { get; }
}
internal class BlowfishCipher1 : Cipher {
private Blowfish _bf;
public BlowfishCipher1(byte[] key) {
_bf = new Blowfish();
_bf.initializeKey(key);
}
public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
_bf.encryptSSH1Style(data, offset, len, result, ro);
}
public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
_bf.decryptSSH1Style(data, offset, len, result, ro);
}
public int BlockSize { get { return 8; } }
}
internal class BlowfishCipher2 : Cipher {
private Blowfish _bf;
public BlowfishCipher2(byte[] key) {
_bf = new Blowfish();
_bf.initializeKey(key);
}
public BlowfishCipher2(byte[] key, byte[] iv) {
_bf = new Blowfish();
_bf.SetIV(iv);
_bf.initializeKey(key);
}
public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
_bf.encryptCBC(data, offset, len, result, ro);
}
public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
_bf.decryptCBC(data, offset, len, result, ro);
}
public int BlockSize { get { return 8; } }
}
internal class TripleDESCipher1 : Cipher {
private DES _DESCipher1;
private DES _DESCipher2;
private DES _DESCipher3;
public TripleDESCipher1(byte[] key) {
_DESCipher1 = new DES();
_DESCipher2 = new DES();
_DESCipher3 = new DES();
_DESCipher1.InitializeKey(key, 0);
_DESCipher2.InitializeKey(key, 8);
_DESCipher3.InitializeKey(key,16);
}
public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
byte[] buf1 = new byte[len];
_DESCipher1.EncryptCBC(data, offset, len, result, ro);
_DESCipher2.DecryptCBC(result, ro, buf1.Length, buf1, 0);
_DESCipher3.EncryptCBC(buf1, 0, buf1.Length, result, ro);
}
public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
byte[] buf1 = new byte[len];
_DESCipher3.DecryptCBC(data, offset, len, result, ro);
_DESCipher2.EncryptCBC(result, ro, buf1.Length, buf1, 0);
_DESCipher1.DecryptCBC(buf1, 0, buf1.Length, result, ro);
}
public int BlockSize { get { return 8; } }
}
internal class TripleDESCipher2 : Cipher {
private DES _DESCipher1;
private DES _DESCipher2;
private DES _DESCipher3;
public TripleDESCipher2(byte[] key) {
_DESCipher1 = new DES();
_DESCipher2 = new DES();
_DESCipher3 = new DES();
_DESCipher1.InitializeKey(key, 0);
_DESCipher2.InitializeKey(key, 8);
_DESCipher3.InitializeKey(key,16);
}
public TripleDESCipher2(byte[] key, byte[] iv) {
_DESCipher1 = new DES();
_DESCipher1.SetIV(iv);
_DESCipher2 = new DES();
_DESCipher2.SetIV(iv);
_DESCipher3 = new DES();
_DESCipher3.SetIV(iv);
_DESCipher1.InitializeKey(key, 0);
_DESCipher2.InitializeKey(key, 8);
_DESCipher3.InitializeKey(key,16);
}
public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
byte[] buf1 = new byte[8];
int n = 0;
while(n < len) {
_DESCipher1.EncryptCBC(data, offset+n, 8, result, ro+n);
_DESCipher2.DecryptCBC(result, ro+n, 8, buf1, 0);
_DESCipher3.EncryptCBC(buf1, 0, 8, result, ro+n);
_DESCipher1.SetIV(result, ro+n);
_DESCipher2.SetIV(result, ro+n);
_DESCipher3.SetIV(result, ro+n);
n += 8;
}
}
public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
byte[] buf1 = new byte[8];
int n = 0;
while(n < len) {
_DESCipher3.DecryptCBC(data, offset+n, 8, result, ro+n);
_DESCipher2.EncryptCBC(result, ro+n, 8, buf1, 0);
_DESCipher1.DecryptCBC(buf1, 0, 8, result, ro+n);
_DESCipher3.SetIV(data, offset+n);
_DESCipher2.SetIV(data, offset+n);
_DESCipher1.SetIV(data, offset+n);
n += 8;
}
}
public int BlockSize { get { return 8; } }
}
internal class RijindaelCipher2 : Cipher {
private Rijndael _rijindael;
private bool isCTR;
public RijindaelCipher2(byte[] key, byte[] iv, CipherAlgorithm algorithm)
{
_rijindael = new Rijndael();
_rijindael.SetIV(iv);
_rijindael.InitializeKey(key);
if (algorithm == CipherAlgorithm.AES256CTR ||
algorithm == CipherAlgorithm.AES192CTR ||
algorithm == CipherAlgorithm.AES128CTR)
isCTR = true;
else
isCTR = false;
}
public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
if (isCTR)
_rijindael.encryptCTR(data, offset, len, result, ro);
else
_rijindael.encryptCBC(data, offset, len, result, ro);
}
public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
if (isCTR)
_rijindael.decryptCTR(data, offset, len, result, ro);
else
_rijindael.decryptCBC(data, offset, len, result, ro);
}
public int BlockSize { get { return _rijindael.GetBlockSize(); } }
}
/// <summary>
/// Creates a cipher from given parameters
/// </summary>
internal class CipherFactory {
public static Cipher CreateCipher(SSHProtocol protocol, CipherAlgorithm algorithm, byte[] key) {
if(protocol==SSHProtocol.SSH1) {
switch(algorithm) {
case CipherAlgorithm.TripleDES:
return new TripleDESCipher1(key);
case CipherAlgorithm.Blowfish:
return new BlowfishCipher1(key);
default:
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
}
else {
switch(algorithm) {
case CipherAlgorithm.TripleDES:
return new TripleDESCipher2(key);
case CipherAlgorithm.Blowfish:
return new BlowfishCipher2(key);
default:
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
}
}
public static Cipher CreateCipher(SSHProtocol protocol, CipherAlgorithm algorithm, byte[] key, byte[] iv) {
if(protocol==SSHProtocol.SSH1) {
return CreateCipher(protocol, algorithm, key);
}
else {
switch(algorithm) {
case CipherAlgorithm.TripleDES:
return new TripleDESCipher2(key, iv);
case CipherAlgorithm.Blowfish:
return new BlowfishCipher2(key, iv);
case CipherAlgorithm.AES128:
case CipherAlgorithm.AES192:
case CipherAlgorithm.AES256:
case CipherAlgorithm.AES128CTR:
case CipherAlgorithm.AES192CTR:
case CipherAlgorithm.AES256CTR:
return new RijindaelCipher2(key, iv, algorithm);
default:
throw new Exception("unknown algorithm " + algorithm);
}
}
}
/// <summary>
/// returns necessary key size from Algorithm in bytes
/// </summary>
public static int GetKeySize(CipherAlgorithm algorithm) {
switch(algorithm) {
case CipherAlgorithm.TripleDES:
return 24;
case CipherAlgorithm.Blowfish:
case CipherAlgorithm.AES128:
case CipherAlgorithm.AES128CTR:
return 16;
case CipherAlgorithm.AES192:
case CipherAlgorithm.AES192CTR:
return 24;
case CipherAlgorithm.AES256:
case CipherAlgorithm.AES256CTR:
return 32;
default:
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
}
/// <summary>
/// returns the block size from Algorithm in bytes
/// </summary>
public static int GetBlockSize(CipherAlgorithm algorithm) {
switch(algorithm) {
case CipherAlgorithm.TripleDES:
case CipherAlgorithm.Blowfish:
return 8;
case CipherAlgorithm.AES128:
case CipherAlgorithm.AES192:
case CipherAlgorithm.AES256:
case CipherAlgorithm.AES128CTR:
case CipherAlgorithm.AES192CTR:
case CipherAlgorithm.AES256CTR:
return 16;
default:
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
}
public static string AlgorithmToSSH2Name(CipherAlgorithm algorithm) {
switch(algorithm) {
case CipherAlgorithm.TripleDES:
return "3des-cbc";
case CipherAlgorithm.Blowfish:
return "blowfish-cbc";
case CipherAlgorithm.AES128:
return "aes128-cbc";
case CipherAlgorithm.AES192:
return "aes192-cbc";
case CipherAlgorithm.AES256:
return "aes256-cbc";
case CipherAlgorithm.AES128CTR:
return "aes128-ctr";
case CipherAlgorithm.AES192CTR:
return "aes192-ctr";
case CipherAlgorithm.AES256CTR:
return "aes256-ctr";
default:
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
}
public static CipherAlgorithm SSH2NameToAlgorithm(string name) {
switch (name)
{
case "3des-cbc":
return CipherAlgorithm.TripleDES;
case "blowfish-cbc":
return CipherAlgorithm.Blowfish;
case "aes128-cbc":
return CipherAlgorithm.AES128;
case "aes192-cbc":
return CipherAlgorithm.AES192;
case "aes256-cbc":
return CipherAlgorithm.AES256;
case "aes128-ctr":
return CipherAlgorithm.AES128CTR;
case "aes192-ctr":
return CipherAlgorithm.AES192CTR;
case "aes256-ctr":
return CipherAlgorithm.AES256CTR;
default:
throw new Exception("Unknown algorithm " + name);
}
}
}
/********** MAC ***********/
interface MAC {
byte[] Calc(byte[] data);
int Size { get; }
}
internal class MACSHA1 : MAC {
private MacAlgorithmProvider _algorithm = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
private CryptographicKey key;
public MACSHA1(byte[] key) {
this.key = _algorithm.CreateKey(key.AsBuffer());
//_algorithm = new HMACSHA1(key);
}
public byte[] Calc(byte[] data) {
//_algorithm.Initialize();
return CryptographicEngine.Sign(key,data.AsBuffer()).ToArray();
}
public int Size { get { return 20; } }
}
internal class MACFactory {
public static MAC CreateMAC(MACAlgorithm algorithm, byte[] key) {
if(algorithm==MACAlgorithm.HMACSHA1)
return new MACSHA1(key);
else
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
public static int GetSize(MACAlgorithm algorithm) {
if(algorithm==MACAlgorithm.HMACSHA1)
return 20;
else
//throw new Exception("unknown algorithm " + algorithm);
throw new Exception("unknown algorithm " + algorithm);
}
}
}