[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / MD4Managed.cs
blobadbd7fb876b841c88f77e7947e9ad8e158cfaba2
1 //
2 // MD4Managed.cs - Message Digest 4 Managed Implementation
3 //
4 // Author:
5 // Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005,2010 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #if !MONOTOUCH && !XAMMAC
32 using System;
34 namespace Mono.Security.Cryptography {
36 // References:
37 // a. RFC1320: The MD4 Message-Digest Algorithm
38 // http://www.ietf.org/rfc/rfc1320.txt
40 #if !INSIDE_CORLIB
41 public
42 #endif
43 class MD4Managed : MD4 {
45 private uint[] state;
46 private byte[] buffer;
47 private uint[] count;
48 private uint[] x;
50 private const int S11 = 3;
51 private const int S12 = 7;
52 private const int S13 = 11;
53 private const int S14 = 19;
54 private const int S21 = 3;
55 private const int S22 = 5;
56 private const int S23 = 9;
57 private const int S24 = 13;
58 private const int S31 = 3;
59 private const int S32 = 9;
60 private const int S33 = 11;
61 private const int S34 = 15;
63 private byte[] digest;
65 //--- constructor -----------------------------------------------------------
67 public MD4Managed ()
69 // we allocate the context memory
70 state = new uint [4];
71 count = new uint [2];
72 buffer = new byte [64];
73 digest = new byte [16];
74 // temporary buffer in MD4Transform that we don't want to keep allocate on each iteration
75 x = new uint [16];
76 // the initialize our context
77 Initialize ();
80 public override void Initialize ()
82 count [0] = 0;
83 count [1] = 0;
84 state [0] = 0x67452301;
85 state [1] = 0xefcdab89;
86 state [2] = 0x98badcfe;
87 state [3] = 0x10325476;
88 // Zeroize sensitive information
89 Array.Clear (buffer, 0, 64);
90 Array.Clear (x, 0, 16);
93 protected override void HashCore (byte[] array, int ibStart, int cbSize)
95 /* Compute number of bytes mod 64 */
96 int index = (int) ((count [0] >> 3) & 0x3F);
97 /* Update number of bits */
98 count [0] += (uint) (cbSize << 3);
99 if (count [0] < (cbSize << 3))
100 count [1]++;
101 count [1] += (uint) (cbSize >> 29);
103 int partLen = 64 - index;
104 int i = 0;
105 /* Transform as many times as possible. */
106 if (cbSize >= partLen) {
107 //MD4_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
108 Buffer.BlockCopy (array, ibStart, buffer, index, partLen);
109 MD4Transform (state, buffer, 0);
111 for (i = partLen; i + 63 < cbSize; i += 64) {
112 // MD4Transform (context->state, &input[i]);
113 MD4Transform (state, array, ibStart + i);
116 index = 0;
119 /* Buffer remaining input */
120 //MD4_memcpy ((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
121 Buffer.BlockCopy (array, ibStart + i, buffer, index, (cbSize-i));
124 protected override byte[] HashFinal ()
126 /* Save number of bits */
127 byte[] bits = new byte [8];
128 Encode (bits, count);
130 /* Pad out to 56 mod 64. */
131 uint index = ((count [0] >> 3) & 0x3f);
132 int padLen = (int) ((index < 56) ? (56 - index) : (120 - index));
133 HashCore (Padding (padLen), 0, padLen);
135 /* Append length (before padding) */
136 HashCore (bits, 0, 8);
138 /* Store state in digest */
139 Encode (digest, state);
141 // Zeroize sensitive information.
142 Initialize ();
144 return digest;
147 //--- private methods ---------------------------------------------------
149 private byte[] Padding (int nLength)
151 if (nLength > 0) {
152 byte[] padding = new byte [nLength];
153 padding [0] = 0x80;
154 return padding;
156 return null;
159 /* F, G and H are basic MD4 functions. */
160 private uint F (uint x, uint y, uint z)
162 return (uint) (((x) & (y)) | ((~x) & (z)));
165 private uint G (uint x, uint y, uint z)
167 return (uint) (((x) & (y)) | ((x) & (z)) | ((y) & (z)));
170 private uint H (uint x, uint y, uint z)
172 return (uint) ((x) ^ (y) ^ (z));
175 /* ROTATE_LEFT rotates x left n bits. */
176 private uint ROL (uint x, byte n)
178 return (uint) (((x) << (n)) | ((x) >> (32-(n))));
181 /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
182 /* Rotation is separate from addition to prevent recomputation */
183 private void FF (ref uint a, uint b, uint c, uint d, uint x, byte s)
185 a += F (b, c, d) + x;
186 a = ROL (a, s);
189 private void GG (ref uint a, uint b, uint c, uint d, uint x, byte s)
191 a += G (b, c, d) + x + 0x5a827999;
192 a = ROL (a, s);
195 private void HH (ref uint a, uint b, uint c, uint d, uint x, byte s)
197 a += H (b, c, d) + x + 0x6ed9eba1;
198 a = ROL (a, s);
201 private void Encode (byte[] output, uint[] input)
203 for (int i = 0, j = 0; j < output.Length; i++, j += 4) {
204 output [j] = (byte)(input [i]);
205 output [j+1] = (byte)(input [i] >> 8);
206 output [j+2] = (byte)(input [i] >> 16);
207 output [j+3] = (byte)(input [i] >> 24);
211 private void Decode (uint[] output, byte[] input, int index)
213 for (int i = 0, j = index; i < output.Length; i++, j += 4) {
214 output [i] = (uint) ((input [j]) | (input [j+1] << 8) | (input [j+2] << 16) | (input [j+3] << 24));
218 private void MD4Transform (uint[] state, byte[] block, int index)
220 uint a = state [0];
221 uint b = state [1];
222 uint c = state [2];
223 uint d = state [3];
225 Decode (x, block, index);
227 /* Round 1 */
228 FF (ref a, b, c, d, x[ 0], S11); /* 1 */
229 FF (ref d, a, b, c, x[ 1], S12); /* 2 */
230 FF (ref c, d, a, b, x[ 2], S13); /* 3 */
231 FF (ref b, c, d, a, x[ 3], S14); /* 4 */
232 FF (ref a, b, c, d, x[ 4], S11); /* 5 */
233 FF (ref d, a, b, c, x[ 5], S12); /* 6 */
234 FF (ref c, d, a, b, x[ 6], S13); /* 7 */
235 FF (ref b, c, d, a, x[ 7], S14); /* 8 */
236 FF (ref a, b, c, d, x[ 8], S11); /* 9 */
237 FF (ref d, a, b, c, x[ 9], S12); /* 10 */
238 FF (ref c, d, a, b, x[10], S13); /* 11 */
239 FF (ref b, c, d, a, x[11], S14); /* 12 */
240 FF (ref a, b, c, d, x[12], S11); /* 13 */
241 FF (ref d, a, b, c, x[13], S12); /* 14 */
242 FF (ref c, d, a, b, x[14], S13); /* 15 */
243 FF (ref b, c, d, a, x[15], S14); /* 16 */
245 /* Round 2 */
246 GG (ref a, b, c, d, x[ 0], S21); /* 17 */
247 GG (ref d, a, b, c, x[ 4], S22); /* 18 */
248 GG (ref c, d, a, b, x[ 8], S23); /* 19 */
249 GG (ref b, c, d, a, x[12], S24); /* 20 */
250 GG (ref a, b, c, d, x[ 1], S21); /* 21 */
251 GG (ref d, a, b, c, x[ 5], S22); /* 22 */
252 GG (ref c, d, a, b, x[ 9], S23); /* 23 */
253 GG (ref b, c, d, a, x[13], S24); /* 24 */
254 GG (ref a, b, c, d, x[ 2], S21); /* 25 */
255 GG (ref d, a, b, c, x[ 6], S22); /* 26 */
256 GG (ref c, d, a, b, x[10], S23); /* 27 */
257 GG (ref b, c, d, a, x[14], S24); /* 28 */
258 GG (ref a, b, c, d, x[ 3], S21); /* 29 */
259 GG (ref d, a, b, c, x[ 7], S22); /* 30 */
260 GG (ref c, d, a, b, x[11], S23); /* 31 */
261 GG (ref b, c, d, a, x[15], S24); /* 32 */
263 HH (ref a, b, c, d, x[ 0], S31); /* 33 */
264 HH (ref d, a, b, c, x[ 8], S32); /* 34 */
265 HH (ref c, d, a, b, x[ 4], S33); /* 35 */
266 HH (ref b, c, d, a, x[12], S34); /* 36 */
267 HH (ref a, b, c, d, x[ 2], S31); /* 37 */
268 HH (ref d, a, b, c, x[10], S32); /* 38 */
269 HH (ref c, d, a, b, x[ 6], S33); /* 39 */
270 HH (ref b, c, d, a, x[14], S34); /* 40 */
271 HH (ref a, b, c, d, x[ 1], S31); /* 41 */
272 HH (ref d, a, b, c, x[ 9], S32); /* 42 */
273 HH (ref c, d, a, b, x[ 5], S33); /* 43 */
274 HH (ref b, c, d, a, x[13], S34); /* 44 */
275 HH (ref a, b, c, d, x[ 3], S31); /* 45 */
276 HH (ref d, a, b, c, x[11], S32); /* 46 */
277 HH (ref c, d, a, b, x[ 7], S33); /* 47 */
278 HH (ref b, c, d, a, x[15], S34); /* 48 */
280 state [0] += a;
281 state [1] += b;
282 state [2] += c;
283 state [3] += d;
288 #endif