Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DESCryptoServiceProvider.cs « System.Security.Cryptography « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e04c3d3622717b1dca4897d9a0dc95fec420267f (plain)
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
//
// System.Security.Cryptography.DESCryptoServiceProvider
//
// Author:
//   Sergey Chaban (serge@wildwestsoftware.com)
//



using System;
using System.Security.Cryptography;


namespace System.Security.Cryptography {


	internal class DESTransformBase : ICryptoTransform {

		internal enum Mode : int {
			ENCRYPTOR = 0,
			DECRYPTOR = 1
		}

		protected delegate void Filter (byte [] workBuff);

		private DESCore core;

		private DESCore.DESCall cryptFn;
		private Filter preprocess;
		private Filter postprocess;

		private byte [] iv;
		private byte [] tmpBlock;

		protected DESTransformBase (Mode mode, byte [] key, byte [] iv)
		{
			core = new DESCore ();

			if (mode == Mode.ENCRYPTOR) {
				cryptFn = new DESCore.DESCall (core.Encrypt);
				preprocess = new Filter (this.EncPreprocess);
				postprocess = new Filter (this.EncPostprocess);
			} else {
				cryptFn = new DESCore.DESCall (core.Decrypt);
				preprocess = new Filter (this.DecPreprocess);
				postprocess = new Filter (this.DecPostprocess);
			}

			core.SetKey (key);
			this.iv = new byte [DESCore.BLOCK_BYTE_SIZE];
			Array.Copy (iv, 0, this.iv, 0, DESCore.BLOCK_BYTE_SIZE);

			tmpBlock = new byte [DESCore.BLOCK_BYTE_SIZE];
		}


		public virtual bool CanTransformMultipleBlocks {
			get {
				return true;
			}
		}


		public virtual int InputBlockSize {
			get {
				return DESCore.BLOCK_BYTE_SIZE;
			}
		}

		public virtual int OutputBlockSize {
			get {
				return DESCore.BLOCK_BYTE_SIZE;
			}
		}

		private void EncPreprocess (byte [] workBuff)
		{
			byte [] iv = this.iv;
			for (int i = 0; i < DESCore.BLOCK_BYTE_SIZE; i++) {
				workBuff [i] ^= iv [i];
			}
		}

		private void EncPostprocess (byte [] workBuff)
		{
			Array.Copy (workBuff, 0, iv, 0, DESCore.BLOCK_BYTE_SIZE);
		}


		private void DecPreprocess (byte [] workBuff)
		{
			Array.Copy (workBuff, 0, tmpBlock, 0, DESCore.BLOCK_BYTE_SIZE);
		}

		private void DecPostprocess (byte [] workBuff)
		{
			EncPreprocess (workBuff);
			Array.Copy (tmpBlock, 0, iv, 0, DESCore.BLOCK_BYTE_SIZE);
		}



		private void Transform (byte [] workBuff)
		{
			preprocess (workBuff);
			cryptFn (workBuff, null);
			postprocess (workBuff);
		}


		public virtual int TransformBlock (byte [] inputBuffer, int inputOffset, int inputCount, byte [] outputBuffer, int outputOffset)
		{
			if ((inputCount & (DESCore.BLOCK_BYTE_SIZE-1)) != 0)
				throw new CryptographicException ("Invalid input block size.");

			if (outputOffset + inputCount > outputBuffer.Length)
				throw new CryptographicException ("Insufficient output buffer size.");

			int step = InputBlockSize;
			int offs = inputOffset;
			int full = inputCount / step;

			byte [] workBuff = new byte [step];

			for (int i = 0; i < full; i++) {
				Array.Copy (inputBuffer, offs, workBuff, 0, step);
				Transform (workBuff);
				Array.Copy (workBuff, 0, outputBuffer, outputOffset, step);
				offs += step;
				outputOffset += step;
			}

			return (full * step);
		}


		[MonoTODO]
		public virtual byte [] TransformFinalBlock (byte [] inputBuffer, int inputOffset, int inputCount)
		{
			// TODO: add decryption support

			int num = (inputCount + DESCore.BLOCK_BYTE_SIZE) & (~(DESCore.BLOCK_BYTE_SIZE-1));
			byte [] res = new byte [num];
			int full = num - DESCore.BLOCK_BYTE_SIZE;

			TransformBlock (inputBuffer, inputOffset, full, res, 0);

			int rem = inputCount & (DESCore.BLOCK_BYTE_SIZE-1);

			// PKCS-5 padding
			for (int i = num; --i >= (num - rem);) {
				res [i] = (byte) rem;
			}

			Array.Copy (inputBuffer, inputOffset + full, res, full, rem);

			// the last padded block will be transformed in-place
			TransformBlock (res, full, DESCore.BLOCK_BYTE_SIZE, res, full);

			/*
			byte [] workBuff = new byte [DESCore.BLOCK_BYTE_SIZE];
			Array.Copy (res, full, workBuff, 0, DESCore.BLOCK_BYTE_SIZE);
			preprocess (workBuff);
			cryptFn (workBuff, null);
			Array.Copy (workBuff, 0, res, full, DESCore.BLOCK_BYTE_SIZE);
			*/

			return res;
		}


	} // DESTransformBase


	internal sealed class DESEncryptor : DESTransformBase {
		internal DESEncryptor (byte [] key, byte [] iv)
		: base (DESTransformBase.Mode.ENCRYPTOR, key, iv)
		{
		}
	} // DESEncryptor


	internal sealed class DESDecryptor : DESTransformBase {
		internal DESDecryptor (byte [] key, byte [] iv)
		: base (DESTransformBase.Mode.DECRYPTOR, key, iv)
		{
		}
	} // DESDecryptor


	public sealed class DESCryptoServiceProvider {
		private byte [] iv;
		private byte [] key;

		public DESCryptoServiceProvider ()
		{
		}


		//
		// Factories
		//

		public ICryptoTransform CreateEncryptor()
		{
			return new DESEncryptor (key, iv);
		}

		public ICryptoTransform CreateDecryptor()
		{
			return new DESDecryptor (key, iv);
		}



		// FIXME: Ought to be in DES.cs

		[MonoTODO ("Ought to be in DES.cs")]
		public /*override*/ byte[] Key {
			get {
				return this.key;
			}
			set {
				this.key = new byte [DESCore.KEY_BYTE_SIZE];
				Array.Copy (value, 0, this.key, 0, DESCore.KEY_BYTE_SIZE);
			}
		}

		public byte[] IV {
			get {
				return this.iv;
			}
			set {
				this.iv = new byte [DESCore.KEY_BYTE_SIZE];
				Array.Copy (value, 0, this.iv, 0, DESCore.KEY_BYTE_SIZE);
			}
		}





		public override string ToString ()
		{
			return "mono::System.Security.Cryptography.DESCryptoServiceProvider";
		}

	} // DESCryptoServiceProvider

} // System.Security.Cryptography