|
| 1 | +// |
| 2 | +// System.Security.Cryptography.SHA1CryptoServiceProvider.cs |
| 3 | +// |
| 4 | +// Authors: |
| 5 | +// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu) |
| 6 | +// Sebastien Pouliot (sebastien@ximian.com) |
| 7 | +// |
| 8 | +// Copyright 2001 by Matthew S. Ford. |
| 9 | +// Copyright (C) 2004, 2005, 2008 Novell, Inc (http://www.novell.com) |
| 10 | +// |
| 11 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 12 | +// a copy of this software and associated documentation files (the |
| 13 | +// "Software"), to deal in the Software without restriction, including |
| 14 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 15 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 16 | +// permit persons to whom the Software is furnished to do so, subject to |
| 17 | +// the following conditions: |
| 18 | +// |
| 19 | +// The above copyright notice and this permission notice shall be |
| 20 | +// included in all copies or substantial portions of the Software. |
| 21 | +// |
| 22 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 23 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 24 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 25 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 26 | +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 27 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 28 | +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | +// |
| 30 | + |
| 31 | +// Note: |
| 32 | +// The MS Framework includes two (almost) identical class for SHA1. |
| 33 | +// SHA1Managed is a 100% managed implementation. |
| 34 | +// SHA1CryptoServiceProvider (this file) is a wrapper on CryptoAPI. |
| 35 | +// Mono must provide those two class for binary compatibility. |
| 36 | +// In our case both class are wrappers around a managed internal class SHA1Internal. |
| 37 | + |
| 38 | +using System; |
| 39 | +using System.Runtime.InteropServices; |
| 40 | +using System.Security.Cryptography; |
| 41 | + |
| 42 | +namespace Mono.Security.Cryptography { |
| 43 | + |
| 44 | + internal class SHA1Internal { |
| 45 | + private const int BLOCK_SIZE_BYTES = 64; |
| 46 | + private uint[] _H; // these are my chaining variables |
| 47 | + private ulong count; |
| 48 | + private byte[] _ProcessingBuffer; // Used to start data when passed less than a block worth. |
| 49 | + private int _ProcessingBufferCount; // Counts how much data we have stored that still needs processed. |
| 50 | + private uint[] buff; |
| 51 | + |
| 52 | + public SHA1Internal() { |
| 53 | + _H = new uint[5]; |
| 54 | + _ProcessingBuffer = new byte[BLOCK_SIZE_BYTES]; |
| 55 | + buff = new uint[80]; |
| 56 | + |
| 57 | + Initialize(); |
| 58 | + } |
| 59 | + |
| 60 | + public void HashCore(byte[] rgb, int ibStart, int cbSize) { |
| 61 | + int i; |
| 62 | + |
| 63 | + if (_ProcessingBufferCount != 0) { |
| 64 | + if (cbSize < (BLOCK_SIZE_BYTES - _ProcessingBufferCount)) { |
| 65 | + System.Buffer.BlockCopy(rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, cbSize); |
| 66 | + _ProcessingBufferCount += cbSize; |
| 67 | + return; |
| 68 | + } else { |
| 69 | + i = (BLOCK_SIZE_BYTES - _ProcessingBufferCount); |
| 70 | + System.Buffer.BlockCopy(rgb, ibStart, _ProcessingBuffer, _ProcessingBufferCount, i); |
| 71 | + ProcessBlock(_ProcessingBuffer, 0); |
| 72 | + _ProcessingBufferCount = 0; |
| 73 | + ibStart += i; |
| 74 | + cbSize -= i; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (i = 0; i < cbSize - cbSize % BLOCK_SIZE_BYTES; i += BLOCK_SIZE_BYTES) { |
| 79 | + ProcessBlock(rgb, (uint)(ibStart + i)); |
| 80 | + } |
| 81 | + |
| 82 | + if (cbSize % BLOCK_SIZE_BYTES != 0) { |
| 83 | + System.Buffer.BlockCopy(rgb, cbSize - cbSize % BLOCK_SIZE_BYTES + ibStart, _ProcessingBuffer, 0, cbSize % BLOCK_SIZE_BYTES); |
| 84 | + _ProcessingBufferCount = cbSize % BLOCK_SIZE_BYTES; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public byte[] HashFinal() { |
| 89 | + byte[] hash = new byte[20]; |
| 90 | + |
| 91 | + ProcessFinalBlock(_ProcessingBuffer, 0, _ProcessingBufferCount); |
| 92 | + |
| 93 | + for (int i = 0; i < 5; i++) { |
| 94 | + for (int j = 0; j < 4; j++) { |
| 95 | + hash[i * 4 + j] = (byte)(_H[i] >> (8 * (3 - j))); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return hash; |
| 100 | + } |
| 101 | + |
| 102 | + public void Initialize() { |
| 103 | + count = 0; |
| 104 | + _ProcessingBufferCount = 0; |
| 105 | + |
| 106 | + _H[0] = 0x67452301; |
| 107 | + _H[1] = 0xefcdab89; |
| 108 | + _H[2] = 0x98badcfe; |
| 109 | + _H[3] = 0x10325476; |
| 110 | + _H[4] = 0xC3D2E1F0; |
| 111 | + } |
| 112 | + |
| 113 | + private void ProcessBlock(byte[] inputBuffer, uint inputOffset) { |
| 114 | + uint a, b, c, d, e; |
| 115 | + |
| 116 | + count += BLOCK_SIZE_BYTES; |
| 117 | + |
| 118 | + // abc removal would not work on the fields |
| 119 | + uint[] _H = this._H; |
| 120 | + uint[] buff = this.buff; |
| 121 | + InitialiseBuff(buff, inputBuffer, inputOffset); |
| 122 | + FillBuff(buff); |
| 123 | + |
| 124 | + a = _H[0]; |
| 125 | + b = _H[1]; |
| 126 | + c = _H[2]; |
| 127 | + d = _H[3]; |
| 128 | + e = _H[4]; |
| 129 | + |
| 130 | + // This function was unrolled because it seems to be doubling our performance with current compiler/VM. |
| 131 | + // Possibly roll up if this changes. |
| 132 | + |
| 133 | + // ---- Round 1 -------- |
| 134 | + int i = 0; |
| 135 | + while (i < 20) { |
| 136 | + e += ((a << 5) | (a >> 27)) + (((c ^ d) & b) ^ d) + 0x5A827999 + buff[i]; |
| 137 | + b = (b << 30) | (b >> 2); |
| 138 | + |
| 139 | + d += ((e << 5) | (e >> 27)) + (((b ^ c) & a) ^ c) + 0x5A827999 + buff[i + 1]; |
| 140 | + a = (a << 30) | (a >> 2); |
| 141 | + |
| 142 | + c += ((d << 5) | (d >> 27)) + (((a ^ b) & e) ^ b) + 0x5A827999 + buff[i + 2]; |
| 143 | + e = (e << 30) | (e >> 2); |
| 144 | + |
| 145 | + b += ((c << 5) | (c >> 27)) + (((e ^ a) & d) ^ a) + 0x5A827999 + buff[i + 3]; |
| 146 | + d = (d << 30) | (d >> 2); |
| 147 | + |
| 148 | + a += ((b << 5) | (b >> 27)) + (((d ^ e) & c) ^ e) + 0x5A827999 + buff[i + 4]; |
| 149 | + c = (c << 30) | (c >> 2); |
| 150 | + i += 5; |
| 151 | + } |
| 152 | + |
| 153 | + // ---- Round 2 -------- |
| 154 | + while (i < 40) { |
| 155 | + e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0x6ED9EBA1 + buff[i]; |
| 156 | + b = (b << 30) | (b >> 2); |
| 157 | + |
| 158 | + d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0x6ED9EBA1 + buff[i + 1]; |
| 159 | + a = (a << 30) | (a >> 2); |
| 160 | + |
| 161 | + c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0x6ED9EBA1 + buff[i + 2]; |
| 162 | + e = (e << 30) | (e >> 2); |
| 163 | + |
| 164 | + b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0x6ED9EBA1 + buff[i + 3]; |
| 165 | + d = (d << 30) | (d >> 2); |
| 166 | + |
| 167 | + a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0x6ED9EBA1 + buff[i + 4]; |
| 168 | + c = (c << 30) | (c >> 2); |
| 169 | + i += 5; |
| 170 | + } |
| 171 | + |
| 172 | + // ---- Round 3 -------- |
| 173 | + while (i < 60) { |
| 174 | + e += ((a << 5) | (a >> 27)) + ((b & c) | (b & d) | (c & d)) + 0x8F1BBCDC + buff[i]; |
| 175 | + b = (b << 30) | (b >> 2); |
| 176 | + |
| 177 | + d += ((e << 5) | (e >> 27)) + ((a & b) | (a & c) | (b & c)) + 0x8F1BBCDC + buff[i + 1]; |
| 178 | + a = (a << 30) | (a >> 2); |
| 179 | + |
| 180 | + c += ((d << 5) | (d >> 27)) + ((e & a) | (e & b) | (a & b)) + 0x8F1BBCDC + buff[i + 2]; |
| 181 | + e = (e << 30) | (e >> 2); |
| 182 | + |
| 183 | + b += ((c << 5) | (c >> 27)) + ((d & e) | (d & a) | (e & a)) + 0x8F1BBCDC + buff[i + 3]; |
| 184 | + d = (d << 30) | (d >> 2); |
| 185 | + |
| 186 | + a += ((b << 5) | (b >> 27)) + ((c & d) | (c & e) | (d & e)) + 0x8F1BBCDC + buff[i + 4]; |
| 187 | + c = (c << 30) | (c >> 2); |
| 188 | + i += 5; |
| 189 | + } |
| 190 | + |
| 191 | + // ---- Round 4 -------- |
| 192 | + while (i < 80) { |
| 193 | + e += ((a << 5) | (a >> 27)) + (b ^ c ^ d) + 0xCA62C1D6 + buff[i]; |
| 194 | + b = (b << 30) | (b >> 2); |
| 195 | + |
| 196 | + d += ((e << 5) | (e >> 27)) + (a ^ b ^ c) + 0xCA62C1D6 + buff[i + 1]; |
| 197 | + a = (a << 30) | (a >> 2); |
| 198 | + |
| 199 | + c += ((d << 5) | (d >> 27)) + (e ^ a ^ b) + 0xCA62C1D6 + buff[i + 2]; |
| 200 | + e = (e << 30) | (e >> 2); |
| 201 | + |
| 202 | + b += ((c << 5) | (c >> 27)) + (d ^ e ^ a) + 0xCA62C1D6 + buff[i + 3]; |
| 203 | + d = (d << 30) | (d >> 2); |
| 204 | + |
| 205 | + a += ((b << 5) | (b >> 27)) + (c ^ d ^ e) + 0xCA62C1D6 + buff[i + 4]; |
| 206 | + c = (c << 30) | (c >> 2); |
| 207 | + i += 5; |
| 208 | + } |
| 209 | + |
| 210 | + _H[0] += a; |
| 211 | + _H[1] += b; |
| 212 | + _H[2] += c; |
| 213 | + _H[3] += d; |
| 214 | + _H[4] += e; |
| 215 | + } |
| 216 | + |
| 217 | + private static void InitialiseBuff(uint[] buff, byte[] input, uint inputOffset) { |
| 218 | + buff[0] = (uint)((input[inputOffset + 0] << 24) | (input[inputOffset + 1] << 16) | (input[inputOffset + 2] << 8) | (input[inputOffset + 3])); |
| 219 | + buff[1] = (uint)((input[inputOffset + 4] << 24) | (input[inputOffset + 5] << 16) | (input[inputOffset + 6] << 8) | (input[inputOffset + 7])); |
| 220 | + buff[2] = (uint)((input[inputOffset + 8] << 24) | (input[inputOffset + 9] << 16) | (input[inputOffset + 10] << 8) | (input[inputOffset + 11])); |
| 221 | + buff[3] = (uint)((input[inputOffset + 12] << 24) | (input[inputOffset + 13] << 16) | (input[inputOffset + 14] << 8) | (input[inputOffset + 15])); |
| 222 | + buff[4] = (uint)((input[inputOffset + 16] << 24) | (input[inputOffset + 17] << 16) | (input[inputOffset + 18] << 8) | (input[inputOffset + 19])); |
| 223 | + buff[5] = (uint)((input[inputOffset + 20] << 24) | (input[inputOffset + 21] << 16) | (input[inputOffset + 22] << 8) | (input[inputOffset + 23])); |
| 224 | + buff[6] = (uint)((input[inputOffset + 24] << 24) | (input[inputOffset + 25] << 16) | (input[inputOffset + 26] << 8) | (input[inputOffset + 27])); |
| 225 | + buff[7] = (uint)((input[inputOffset + 28] << 24) | (input[inputOffset + 29] << 16) | (input[inputOffset + 30] << 8) | (input[inputOffset + 31])); |
| 226 | + buff[8] = (uint)((input[inputOffset + 32] << 24) | (input[inputOffset + 33] << 16) | (input[inputOffset + 34] << 8) | (input[inputOffset + 35])); |
| 227 | + buff[9] = (uint)((input[inputOffset + 36] << 24) | (input[inputOffset + 37] << 16) | (input[inputOffset + 38] << 8) | (input[inputOffset + 39])); |
| 228 | + buff[10] = (uint)((input[inputOffset + 40] << 24) | (input[inputOffset + 41] << 16) | (input[inputOffset + 42] << 8) | (input[inputOffset + 43])); |
| 229 | + buff[11] = (uint)((input[inputOffset + 44] << 24) | (input[inputOffset + 45] << 16) | (input[inputOffset + 46] << 8) | (input[inputOffset + 47])); |
| 230 | + buff[12] = (uint)((input[inputOffset + 48] << 24) | (input[inputOffset + 49] << 16) | (input[inputOffset + 50] << 8) | (input[inputOffset + 51])); |
| 231 | + buff[13] = (uint)((input[inputOffset + 52] << 24) | (input[inputOffset + 53] << 16) | (input[inputOffset + 54] << 8) | (input[inputOffset + 55])); |
| 232 | + buff[14] = (uint)((input[inputOffset + 56] << 24) | (input[inputOffset + 57] << 16) | (input[inputOffset + 58] << 8) | (input[inputOffset + 59])); |
| 233 | + buff[15] = (uint)((input[inputOffset + 60] << 24) | (input[inputOffset + 61] << 16) | (input[inputOffset + 62] << 8) | (input[inputOffset + 63])); |
| 234 | + } |
| 235 | + |
| 236 | + private static void FillBuff(uint[] buff) { |
| 237 | + uint val; |
| 238 | + for (int i = 16; i < 80; i += 8) { |
| 239 | + val = buff[i - 3] ^ buff[i - 8] ^ buff[i - 14] ^ buff[i - 16]; |
| 240 | + buff[i] = (val << 1) | (val >> 31); |
| 241 | + |
| 242 | + val = buff[i - 2] ^ buff[i - 7] ^ buff[i - 13] ^ buff[i - 15]; |
| 243 | + buff[i + 1] = (val << 1) | (val >> 31); |
| 244 | + |
| 245 | + val = buff[i - 1] ^ buff[i - 6] ^ buff[i - 12] ^ buff[i - 14]; |
| 246 | + buff[i + 2] = (val << 1) | (val >> 31); |
| 247 | + |
| 248 | + val = buff[i + 0] ^ buff[i - 5] ^ buff[i - 11] ^ buff[i - 13]; |
| 249 | + buff[i + 3] = (val << 1) | (val >> 31); |
| 250 | + |
| 251 | + val = buff[i + 1] ^ buff[i - 4] ^ buff[i - 10] ^ buff[i - 12]; |
| 252 | + buff[i + 4] = (val << 1) | (val >> 31); |
| 253 | + |
| 254 | + val = buff[i + 2] ^ buff[i - 3] ^ buff[i - 9] ^ buff[i - 11]; |
| 255 | + buff[i + 5] = (val << 1) | (val >> 31); |
| 256 | + |
| 257 | + val = buff[i + 3] ^ buff[i - 2] ^ buff[i - 8] ^ buff[i - 10]; |
| 258 | + buff[i + 6] = (val << 1) | (val >> 31); |
| 259 | + |
| 260 | + val = buff[i + 4] ^ buff[i - 1] ^ buff[i - 7] ^ buff[i - 9]; |
| 261 | + buff[i + 7] = (val << 1) | (val >> 31); |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + private void ProcessFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { |
| 266 | + ulong total = count + (ulong)inputCount; |
| 267 | + int paddingSize = (56 - (int)(total % BLOCK_SIZE_BYTES)); |
| 268 | + |
| 269 | + if (paddingSize < 1) |
| 270 | + paddingSize += BLOCK_SIZE_BYTES; |
| 271 | + |
| 272 | + int length = inputCount + paddingSize + 8; |
| 273 | + byte[] fooBuffer = (length == 64) ? _ProcessingBuffer : new byte[length]; |
| 274 | + |
| 275 | + for (int i = 0; i < inputCount; i++) { |
| 276 | + fooBuffer[i] = inputBuffer[i + inputOffset]; |
| 277 | + } |
| 278 | + |
| 279 | + fooBuffer[inputCount] = 0x80; |
| 280 | + for (int i = inputCount + 1; i < inputCount + paddingSize; i++) { |
| 281 | + fooBuffer[i] = 0x00; |
| 282 | + } |
| 283 | + |
| 284 | + // I deal in bytes. The algorithm deals in bits. |
| 285 | + ulong size = total << 3; |
| 286 | + AddLength(size, fooBuffer, inputCount + paddingSize); |
| 287 | + ProcessBlock(fooBuffer, 0); |
| 288 | + |
| 289 | + if (length == 128) |
| 290 | + ProcessBlock(fooBuffer, 64); |
| 291 | + } |
| 292 | + |
| 293 | + internal void AddLength(ulong length, byte[] buffer, int position) { |
| 294 | + buffer[position++] = (byte)(length >> 56); |
| 295 | + buffer[position++] = (byte)(length >> 48); |
| 296 | + buffer[position++] = (byte)(length >> 40); |
| 297 | + buffer[position++] = (byte)(length >> 32); |
| 298 | + buffer[position++] = (byte)(length >> 24); |
| 299 | + buffer[position++] = (byte)(length >> 16); |
| 300 | + buffer[position++] = (byte)(length >> 8); |
| 301 | + buffer[position] = (byte)(length); |
| 302 | + } |
| 303 | + |
| 304 | + public SHA1Internal Clone() { |
| 305 | + return new SHA1Internal { |
| 306 | + _H = (uint[])_H.Clone(), |
| 307 | + count = count, |
| 308 | + _ProcessingBuffer = (byte[])_ProcessingBuffer.Clone(), |
| 309 | + _ProcessingBufferCount = _ProcessingBufferCount, |
| 310 | + buff = (uint[])buff.Clone() |
| 311 | + }; |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + [ComVisible(true)] |
| 316 | + internal sealed class SHA1CryptoServiceProvider : SHA1, ICloneable { |
| 317 | + private SHA1Internal sha; |
| 318 | + |
| 319 | + public SHA1CryptoServiceProvider() { |
| 320 | + sha = new SHA1Internal(); |
| 321 | + } |
| 322 | + |
| 323 | + ~SHA1CryptoServiceProvider() { |
| 324 | + Dispose(false); |
| 325 | + } |
| 326 | + |
| 327 | + protected override void Dispose(bool disposing) { |
| 328 | + // nothing new to do (managed implementation) |
| 329 | + base.Dispose(disposing); |
| 330 | + } |
| 331 | + |
| 332 | + protected override void HashCore(byte[] rgb, int ibStart, int cbSize) { |
| 333 | + State = 1; |
| 334 | + sha.HashCore(rgb, ibStart, cbSize); |
| 335 | + } |
| 336 | + |
| 337 | + protected override byte[] HashFinal() { |
| 338 | + State = 0; |
| 339 | + return sha.HashFinal(); |
| 340 | + } |
| 341 | + |
| 342 | + public override void Initialize() { |
| 343 | + sha.Initialize(); |
| 344 | + } |
| 345 | + |
| 346 | + object ICloneable.Clone() { |
| 347 | + return new SHA1CryptoServiceProvider() { |
| 348 | + sha = sha.Clone() |
| 349 | + }; |
| 350 | + } |
| 351 | + } |
| 352 | +} |
0 commit comments