Author: spouliot
Date: 2005-03-07 09:46:30 -0500 (Mon, 07 Mar 2005)
New Revision: 41524

Modified:
   trunk/mcs/class/corlib/System.Security.Cryptography/ChangeLog
   
trunk/mcs/class/corlib/System.Security.Cryptography/MD5CryptoServiceProvider.cs
   
trunk/mcs/class/corlib/System.Security.Cryptography/SHA1CryptoServiceProvider.cs
   trunk/mcs/class/corlib/System.Security.Cryptography/SHA256Managed.cs
Log:
2005-03-07  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * MD5CryptoServiceProvider.cs: Fixed #73404 to return right results
        if the data length is bigger than 2^32 bits.
        * SHA1CryptoServiceProvider.cs: Fixed #73404 to return right results
        if the data length is bigger than 2^32 bits.
        * SHA256Managed.cs: Fixed #73404 to return right results if the data
        length is bigger than 2^32 bits.



Modified: trunk/mcs/class/corlib/System.Security.Cryptography/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Security.Cryptography/ChangeLog       
2005-03-07 14:39:59 UTC (rev 41523)
+++ trunk/mcs/class/corlib/System.Security.Cryptography/ChangeLog       
2005-03-07 14:46:30 UTC (rev 41524)
@@ -1,3 +1,12 @@
+2005-03-07  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * MD5CryptoServiceProvider.cs: Fixed #73404 to return right results
+       if the data length is bigger than 2^32 bits.
+       * SHA1CryptoServiceProvider.cs: Fixed #73404 to return right results
+       if the data length is bigger than 2^32 bits.
+       * SHA256Managed.cs: Fixed #73404 to return right results if the data
+       length is bigger than 2^32 bits.
+
 2005-03-03  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * RNGCryptoServiceProvider.cs: Added a new call in the static ctor

Modified: 
trunk/mcs/class/corlib/System.Security.Cryptography/MD5CryptoServiceProvider.cs
===================================================================
--- 
trunk/mcs/class/corlib/System.Security.Cryptography/MD5CryptoServiceProvider.cs 
    2005-03-07 14:39:59 UTC (rev 41523)
+++ 
trunk/mcs/class/corlib/System.Security.Cryptography/MD5CryptoServiceProvider.cs 
    2005-03-07 14:46:30 UTC (rev 41524)
@@ -6,12 +6,8 @@
 //     Sebastien Pouliot ([EMAIL PROTECTED])
 //
 // Copyright 2001 by Matthew S. Ford.
-// (C) 2004 Novell (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
-//
 // 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
@@ -43,7 +39,7 @@
                private const int HASH_SIZE_BYTES  =  16;
                private uint[] _H;
                private uint[] buff;
-               private uint count;
+               private ulong count;
                private byte[] _ProcessingBuffer;   // Used to start data when 
passed less than a block worth.
                private int _ProcessingBufferCount; // Counts how much data we 
have stored that still needs processed.
        
@@ -432,46 +428,45 @@
                
                private void ProcessFinalBlock (byte[] inputBuffer, int 
inputOffset, int inputCount) 
                {
-                       byte[] fooBuffer;
-                       int paddingSize;
-                       int i;
-                       uint size;
+                       ulong total = count + (ulong)inputCount;
+                       int paddingSize = (int)(56 - total % BLOCK_SIZE_BYTES);
 
-                       paddingSize = (int)(56 - (inputCount + count) % 
BLOCK_SIZE_BYTES);
-
                        if (paddingSize < 1)
                                paddingSize += BLOCK_SIZE_BYTES;
 
-                       fooBuffer = new byte[inputCount+paddingSize+8];
+                       byte[] fooBuffer = new byte [inputCount+paddingSize+8];
 
-                       for (i=0; i<inputCount; i++) {
+                       for (int i=0; i<inputCount; i++) {
                                fooBuffer[i] = inputBuffer[i+inputOffset];
                        }
 
                        fooBuffer[inputCount] = 0x80;
-                       for (i=inputCount+1; i<inputCount+paddingSize; i++) {
+                       for (int i=inputCount+1; i<inputCount+paddingSize; i++) 
{
                                fooBuffer[i] = 0x00;
                        }
 
-                       size = (uint)(count+inputCount);
-                       size *= 8;
-                       fooBuffer[inputCount+paddingSize]   = (byte)(size);
-                       fooBuffer[inputCount+paddingSize+1] = (byte)((size) >>  
8);
-                       fooBuffer[inputCount+paddingSize+2] = (byte)((size) >> 
16);
-                       fooBuffer[inputCount+paddingSize+3] = (byte)((size) >> 
24);
+                       // I deal in bytes. The algorithm deals in bits.
+                       ulong size = total << 3;
+                       AddLength (size, fooBuffer, inputCount+paddingSize);
+                       ProcessBlock (fooBuffer, 0);
 
-                       fooBuffer[inputCount+paddingSize+4] = 0x00;
-                       fooBuffer[inputCount+paddingSize+5] = 0x00;
-                       fooBuffer[inputCount+paddingSize+6] = 0x00;
-                       fooBuffer[inputCount+paddingSize+7] = 0x00;
-
-                       ProcessBlock(fooBuffer, 0);
-
                        if (inputCount+paddingSize+8 == 128) {
                                ProcessBlock(fooBuffer, 64);
                        }
                }
-               
+
+               internal void AddLength (ulong length, byte[] buffer, int 
position)
+               {
+                       buffer [position++] = (byte)(length);
+                       buffer [position++] = (byte)(length >>  8);
+                       buffer [position++] = (byte)(length >> 16);
+                       buffer [position++] = (byte)(length >> 24);
+                       buffer [position++] = (byte)(length >> 32);
+                       buffer [position++] = (byte)(length >> 40);
+                       buffer [position++] = (byte)(length >> 48);
+                       buffer [position]   = (byte)(length >> 56);
+               }
+
                private readonly static uint[] K = {
                        0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
                        0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 

Modified: 
trunk/mcs/class/corlib/System.Security.Cryptography/SHA1CryptoServiceProvider.cs
===================================================================
--- 
trunk/mcs/class/corlib/System.Security.Cryptography/SHA1CryptoServiceProvider.cs
    2005-03-07 14:39:59 UTC (rev 41523)
+++ 
trunk/mcs/class/corlib/System.Security.Cryptography/SHA1CryptoServiceProvider.cs
    2005-03-07 14:46:30 UTC (rev 41524)
@@ -42,7 +42,7 @@
                private const int BLOCK_SIZE_BYTES =  64;
                private const int HASH_SIZE_BYTES  =  20;
                private uint[] _H;  // these are my chaining variables
-               private uint count;
+               private ulong count;
                private byte[] _ProcessingBuffer;   // Used to start data when 
passed less than a block worth.
                private int _ProcessingBufferCount; // Counts how much data we 
have stored that still needs processed.
                private uint[] buff;
@@ -399,42 +399,44 @@
        
                private void ProcessFinalBlock (byte[] inputBuffer, int 
inputOffset, int inputCount) 
                {
-                       int i;
-                       int paddingSize = (int)(56 - (inputCount + count) % 
BLOCK_SIZE_BYTES);
+                       ulong total = count + (ulong)inputCount;
+                       int paddingSize = (56 - (int)(total % 
BLOCK_SIZE_BYTES));
 
                        if (paddingSize < 1)
                                paddingSize += BLOCK_SIZE_BYTES;
 
                        byte[] fooBuffer = new byte[inputCount+paddingSize+8];
 
-                       for (i=0; i<inputCount; i++) {
+                       for (int i=0; i<inputCount; i++) {
                                fooBuffer[i] = inputBuffer[i+inputOffset];
                        }
 
                        fooBuffer[inputCount] = 0x80;
-                       for (i=inputCount+1; i<inputCount+paddingSize; i++) {
+                       for (int i=inputCount+1; i<inputCount+paddingSize; i++) 
{
                                fooBuffer[i] = 0x00;
                        }
 
                        // I deal in bytes. The algorithm deals in bits.
-                       uint size = (uint)((count+inputCount) << 3);
+                       ulong size = total << 3;
+                       AddLength (size, fooBuffer, inputCount+paddingSize);
+                       ProcessBlock (fooBuffer, 0);
 
-                       fooBuffer[inputCount+paddingSize]   = 0x00;
-                       fooBuffer[inputCount+paddingSize+1] = 0x00;
-                       fooBuffer[inputCount+paddingSize+2] = 0x00;
-                       fooBuffer[inputCount+paddingSize+3] = 0x00;
-
-                       fooBuffer[inputCount+paddingSize+4] = (byte)((size) >> 
24);
-                       fooBuffer[inputCount+paddingSize+5] = (byte)((size) >> 
16);
-                       fooBuffer[inputCount+paddingSize+6] = (byte)((size) >>  
8);
-                       fooBuffer[inputCount+paddingSize+7] = (byte)((size) >>  
0);
-
-                       ProcessBlock(fooBuffer, 0);
-
                        if (inputCount+paddingSize+8 == 128) {
                                ProcessBlock(fooBuffer, 64);
                        }
                }
+
+               internal void AddLength (ulong length, byte[] buffer, int 
position)
+               {
+                       buffer [position++] = (byte)(length >> 56);
+                       buffer [position++] = (byte)(length >> 48);
+                       buffer [position++] = (byte)(length >> 40);
+                       buffer [position++] = (byte)(length >> 32);
+                       buffer [position++] = (byte)(length >> 24);
+                       buffer [position++] = (byte)(length >> 16);
+                       buffer [position++] = (byte)(length >>  8);
+                       buffer [position]   = (byte)(length);
+               }
        }
 
        public sealed class SHA1CryptoServiceProvider : SHA1 {

Modified: trunk/mcs/class/corlib/System.Security.Cryptography/SHA256Managed.cs
===================================================================
--- trunk/mcs/class/corlib/System.Security.Cryptography/SHA256Managed.cs        
2005-03-07 14:39:59 UTC (rev 41523)
+++ trunk/mcs/class/corlib/System.Security.Cryptography/SHA256Managed.cs        
2005-03-07 14:46:30 UTC (rev 41524)
@@ -35,7 +35,7 @@
                private const int HASH_SIZE_BYTES  =  32;
                private uint[] _H;
                private uint[] K;
-               private uint count;
+               private ulong count;
                private byte[] _ProcessingBuffer;   // Used to start data when 
passed less than a block worth.
                private int _ProcessingBufferCount; // Counts how much data we 
have stored that still needs processed.
                private uint[] buff;
@@ -203,46 +203,44 @@
        
                private void ProcessFinalBlock (byte[] inputBuffer, int 
inputOffset, int inputCount) 
                {
-                       byte[] fooBuffer;
-                       int paddingSize;
-                       int i;
-                       uint size;
+                       ulong total = count + (ulong)inputCount;
+                       int paddingSize = (56 - (int)(total % 
BLOCK_SIZE_BYTES));
 
-                       paddingSize = (int)(56 - (inputCount + count) % 
BLOCK_SIZE_BYTES);
-
                        if (paddingSize < 1)
                                paddingSize += BLOCK_SIZE_BYTES;
 
-                       fooBuffer = new byte[inputCount+paddingSize+8];
+                       byte[] fooBuffer = new byte[inputCount+paddingSize+8];
 
-                       for (i=0; i<inputCount; i++) {
+                       for (int i=0; i<inputCount; i++) {
                                fooBuffer[i] = inputBuffer[i+inputOffset];
                        }
 
                        fooBuffer[inputCount] = 0x80;
-                       for (i=inputCount+1; i<inputCount+paddingSize; i++) {
+                       for (int i=inputCount+1; i<inputCount+paddingSize; i++) 
{
                                fooBuffer[i] = 0x00;
                        }
 
-                       size = (uint)(count+inputCount);
-                       size *= 8;
+                       // I deal in bytes. The algorithm deals in bits.
+                       ulong size = total << 3;
+                       AddLength (size, fooBuffer, inputCount+paddingSize);
+                       ProcessBlock (fooBuffer, 0);
 
-                       fooBuffer[inputCount+paddingSize]   = 0x00;
-                       fooBuffer[inputCount+paddingSize+1] = 0x00;
-                       fooBuffer[inputCount+paddingSize+2] = 0x00;
-                       fooBuffer[inputCount+paddingSize+3] = 0x00;
-
-                       fooBuffer[inputCount+paddingSize+4] = (byte)((size) >> 
24);
-                       fooBuffer[inputCount+paddingSize+5] = (byte)((size) >> 
16);
-                       fooBuffer[inputCount+paddingSize+6] = (byte)((size) >>  
8);
-                       fooBuffer[inputCount+paddingSize+7] = (byte)((size) >>  
0);
-
-                       ProcessBlock(fooBuffer, 0);
-
                        if (inputCount+paddingSize+8 == 128) {
                                ProcessBlock(fooBuffer, 64);
                        }
                }
+
+               internal void AddLength (ulong length, byte[] buffer, int 
position)
+               {
+                       buffer [position++] = (byte)(length >> 56);
+                       buffer [position++] = (byte)(length >> 48);
+                       buffer [position++] = (byte)(length >> 40);
+                       buffer [position++] = (byte)(length >> 32);
+                       buffer [position++] = (byte)(length >> 24);
+                       buffer [position++] = (byte)(length >> 16);
+                       buffer [position++] = (byte)(length >>  8);
+                       buffer [position]   = (byte)(length);
+               }
        }
 }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to