This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
generator.cs
237 lines (209 loc) · 7.3 KB
/
generator.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
//
// Author:
// Sebastien Pouliot <sebastien@gmail.com>
//
// Copyright 2012 Symform Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
class Program {
static void GenerateHash (string name)
{
string template = @"// NOTE: Generated code DO NOT EDIT
//
// Author:
// Sebastien Pouliot <sebastien@gmail.com>
//
// Copyright 2012 Symform Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// 'Software'), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Security.Cryptography;
using Crimson.CryptoDev;
namespace Crimson.Security.Cryptography {
public class {0}Kernel : {0} {
HashHelper helper;
static {0}Kernel()
{
string blockSize = Environment.GetEnvironmentVariable (""CRIMSON_{0}_BUFFER_BLOCK_SIZE"");
BufferBlockSize = blockSize == null ? Int32.MaxValue : Convert.ToInt32 (blockSize);
}
public static int BufferBlockSize { get; set; }
protected override void Dispose (bool disposing)
{
if (disposing && helper != null) {
helper.Dispose ();
helper = null;
}
base.Dispose (disposing);
}
public override void Initialize ()
{
helper = new HashHelper (Cipher.{0});
helper.BufferBlockSize = BufferBlockSize;
}
protected override void HashCore (byte[] data, int start, int length)
{
if (helper == null)
Initialize ();
helper.Update (data, start, length);
}
protected override byte[] HashFinal ()
{
if (helper == null)
Initialize ();
return helper.Final (HashSize >> 3);
}
}
}";
string filename = Path.Combine (OutputDirectory,
String.Format ("{0}Kernel.g.cs", name));
string content = template.Replace ("{0}", name).
Replace ("{1}", name.ToUpperInvariant ());
File.WriteAllText (filename, content);
}
static void GenerateSymmetricAlgorithm (string name, string fallback, string ecb, string cbc)
{
string template = @"// NOTE: Generated code DO NOT EDIT
//
// Author: Sebastien Pouliot <sebastien@gmail.com>
// See LICENSE for copyrights and restrictions
//
using System;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using Mono.Security.Cryptography;
using Crimson.CryptoDev;
namespace Crimson.Security.Cryptography {
public class {0}Kernel : {0} {
static {0}Kernel()
{
string blockSize = Environment.GetEnvironmentVariable (""CRIMSON_{4}_BUFFER_BLOCK_SIZE"");
BufferBlockSize = blockSize == null ? Int32.MaxValue : Convert.ToInt32 (blockSize);
}
public static int BufferBlockSize { get; set; }
public {0}Kernel ()
{
}
public override void GenerateIV ()
{
IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
}
public override void GenerateKey ()
{
KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
}
{1} Fallback ()
{
{1} r = new {1} ();
r.Mode = Mode;
r.Padding = Padding;
return r;
}
public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
{
try {
switch (Mode) {
case CipherMode.CBC:
return new CryptoDevTransform (this, Cipher.{3}, false, rgbKey, rgbIV, BufferBlockSize);
case CipherMode.ECB:
if (Helper.Mode == KernelMode.CryptoDev)
return new CryptoDevTransform (this, Cipher.{2}, false, rgbKey, rgbIV, BufferBlockSize);
break;
}
}
catch (CryptographicException) {
// the kernel might not have the required mode (even for 'generic') available
}
// other modes, effectivelty CFB, will be implemented on top
// on ECB, one block at the time
return Fallback ().CreateDecryptor (rgbKey, rgbIV);
}
public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
{
try {
switch (Mode) {
case CipherMode.CBC:
return new CryptoDevTransform (this, Cipher.{3}, true, rgbKey, rgbIV, BufferBlockSize);
case CipherMode.ECB:
if (Helper.Mode == KernelMode.CryptoDev)
return new CryptoDevTransform (this, Cipher.{2}, true, rgbKey, rgbIV, BufferBlockSize);
break;
}
}
catch (CryptographicException) {
// the kernel might not have the required mode (even for 'generic') available
}
// other modes, effectivelty CFB, will be implemented on top
// on ECB, one block at the time
return Fallback ().CreateEncryptor (rgbKey, rgbIV);
}
}
}";
string filename = Path.Combine (OutputDirectory,
String.Format ("{0}Kernel.g.cs", name));
string content = template.Replace ("{0}", name).
Replace ("{1}", fallback).
Replace ("{2}", ecb).
Replace ("{3}", cbc).
Replace ("{4}", name.ToUpperInvariant ());
File.WriteAllText (filename, content);
}
static string OutputDirectory { get; set; }
static void Main (string[] args)
{
OutputDirectory = args.Length == 0 ? "." : args [0];
GenerateHash ("SHA1"); // CRYPTO_SHA1
GenerateHash ("SHA256"); // CRYPTO_SHA256
#if UNTESTED
GenerateHash ("MD5"); // CRYPTO_MD5
GenerateHash ("RIPEMD160"); // CRYPTO_RIPEMD160
GenerateHash ("SHA384"); // CRYPTO_SHA2_384
GenerateHash ("SHA512"); // CRYPTO_SHA2_512
#endif
GenerateSymmetricAlgorithm ("Aes", "RijndaelManaged", "AES_ECB", "AES_CBC");
#if UNTESTED
GenerateSymmetricAlgorithm ("Des", "DESCryptoServiceProvider", null, "DES_CBC");
GenerateSymmetricAlgorithm ("TripleDes", "TripleDESCryptoServiceProvider", null, "3DES_CBC");
// BLF (Blowfish), CAST, SKIPJACK and Camellia are not part of the .NET framework
// and would require a additional base classes and fallbacks
#endif
}
}