summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/crypto/digests/SkeinEngine.cs22
-rw-r--r--crypto/src/crypto/engines/ThreefishEngine.cs78
2 files changed, 18 insertions, 82 deletions
diff --git a/crypto/src/crypto/digests/SkeinEngine.cs b/crypto/src/crypto/digests/SkeinEngine.cs
index 2f38115d2..a36ac8fe7 100644
--- a/crypto/src/crypto/digests/SkeinEngine.cs
+++ b/crypto/src/crypto/digests/SkeinEngine.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 
 using Org.BouncyCastle.Crypto.Engines;
 using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Crypto.Utilities;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.Collections;
 
@@ -75,7 +76,7 @@ namespace Org.BouncyCastle.Crypto.Digests
                 bytes[5] = 0;
 
                 // 8..15 = output length
-                ThreefishEngine.WordToBytes((ulong)outputSizeBits, bytes, 8);
+                Pack.UInt64_To_LE((ulong)outputSizeBits, bytes, 8);
             }
 
             public byte[] Bytes
@@ -441,10 +442,7 @@ namespace Org.BouncyCastle.Crypto.Digests
             private void ProcessBlock(ulong[] output)
             {
                 engine.threefish.Init(true, engine.chain, tweak.GetWords());
-                for (int i = 0; i < message.Length; i++)
-                {
-                    message[i] = ThreefishEngine.BytesToWord(currentBlock, i * 8);
-                }
+                Pack.LE_To_UInt64(currentBlock, 0, message);
 
                 engine.threefish.ProcessBlock(message, output);
 
@@ -465,7 +463,6 @@ namespace Org.BouncyCastle.Crypto.Digests
                 tweak.Final = true;
                 ProcessBlock(output);
             }
-
         }
 
         /**
@@ -776,31 +773,28 @@ namespace Org.BouncyCastle.Crypto.Digests
         private void Output(ulong outputSequence, byte[] outBytes, int outOff, int outputBytes)
         {
             byte[] currentBytes = new byte[8];
-            ThreefishEngine.WordToBytes(outputSequence, currentBytes, 0);
+            Pack.UInt64_To_LE(outputSequence, currentBytes, 0);
 
-            // Output is a sequence of UBI invocations all of which use and preserve the pre-output
-            // state
+            // Output is a sequence of UBI invocations all of which use and preserve the pre-output state
             ulong[] outputWords = new ulong[chain.Length];
             UbiInit(PARAM_TYPE_OUTPUT);
             this.ubi.Update(currentBytes, 0, currentBytes.Length, outputWords);
             ubi.DoFinal(outputWords);
 
-            int wordsRequired = ((outputBytes + 8 - 1) / 8);
+            int wordsRequired = (outputBytes + 8 - 1) / 8;
             for (int i = 0; i < wordsRequired; i++)
             {
                 int toWrite = System.Math.Min(8, outputBytes - (i * 8));
                 if (toWrite == 8)
                 {
-                    ThreefishEngine.WordToBytes(outputWords[i], outBytes, outOff + (i * 8));
+                    Pack.UInt64_To_LE(outputWords[i], outBytes, outOff + (i * 8));
                 }
                 else
                 {
-                    ThreefishEngine.WordToBytes(outputWords[i], currentBytes, 0);
+                    Pack.UInt64_To_LE(outputWords[i], currentBytes, 0);
                     Array.Copy(currentBytes, 0, outBytes, outOff + (i * 8), toWrite);
                 }
             }
         }
-
     }
 }
-
diff --git a/crypto/src/crypto/engines/ThreefishEngine.cs b/crypto/src/crypto/engines/ThreefishEngine.cs
index eade3cc72..c5aee5395 100644
--- a/crypto/src/crypto/engines/ThreefishEngine.cs
+++ b/crypto/src/crypto/engines/ThreefishEngine.cs
@@ -135,18 +135,17 @@ namespace Org.BouncyCastle.Crypto.Engines
 
 			switch (blocksizeBits)
 			{
-				case BLOCKSIZE_256:
+			case BLOCKSIZE_256:
 				cipher = new Threefish256Cipher(kw, t);
 				break;
-				case BLOCKSIZE_512:
+			case BLOCKSIZE_512:
 				cipher = new Threefish512Cipher(kw, t);
 				break;
-				case BLOCKSIZE_1024:
+			case BLOCKSIZE_1024:
 				cipher = new Threefish1024Cipher(kw, t);
 				break;
-				default:
-				throw new ArgumentException(
-					"Invalid blocksize - Threefish is defined with block size of 256, 512, or 1024 bits");
+			default:
+				throw new ArgumentException("Invalid blocksize - Threefish is defined with block size of 256, 512, or 1024 bits");
 			}
 		}
 
@@ -189,10 +188,7 @@ namespace Org.BouncyCastle.Crypto.Engines
 					                            + " bytes)");
 				}
 				keyWords = new ulong[blocksizeWords];
-				for (int i = 0; i < keyWords.Length; i++)
-				{
-					keyWords[i] = BytesToWord(keyBytes, i * 8);
-				}
+				Pack.LE_To_UInt64(keyBytes, 0, keyWords);
 			}
 			if (tweakBytes != null)
 			{
@@ -200,7 +196,8 @@ namespace Org.BouncyCastle.Crypto.Engines
 				{
 					throw new ArgumentException("Threefish tweak must be " + TWEAK_SIZE_BYTES + " bytes");
 				}
-				tweakWords = new ulong[]{BytesToWord(tweakBytes, 0), BytesToWord(tweakBytes, 8)};
+				tweakWords = new ulong[2];
+				Pack.LE_To_UInt64(tweakBytes, 0, tweakWords);
 			}
 			Init(forEncryption, keyWords, tweakWords);
 		}
@@ -298,16 +295,9 @@ namespace Org.BouncyCastle.Crypto.Engines
 				throw new DataLengthException("Input buffer too short");
 			}
 
-			for (int i = 0; i < blocksizeBytes; i += 8)
-			{
-				currentBlock[i >> 3] = BytesToWord(inBytes, inOff + i);
-			}
+			Pack.LE_To_UInt64(inBytes, inOff, currentBlock);
 			ProcessBlock(this.currentBlock, this.currentBlock);
-			for (int i = 0; i < blocksizeBytes; i += 8)
-			{
-				WordToBytes(this.currentBlock[i >> 3], outBytes, outOff + i);
-			}
-
+			Pack.UInt64_To_LE(currentBlock, outBytes, outOff);
 			return blocksizeBytes;
 		}
 
@@ -347,54 +337,6 @@ namespace Org.BouncyCastle.Crypto.Engines
 			return blocksizeWords;
 		}
 
-		/// <summary>
-		/// Read a single 64 bit word from input in LSB first order.
-		/// </summary>
-		internal static ulong BytesToWord(byte[] bytes, int off)
-		{
-			if ((off + 8) > bytes.Length)
-			{
-				// Help the JIT avoid index checks
-				throw new ArgumentException();
-			}
-
-			ulong word = 0;
-			int index = off;
-
-			word = (bytes[index++] & 0xffUL);
-			word |= (bytes[index++] & 0xffUL) << 8;
-			word |= (bytes[index++] & 0xffUL) << 16;
-			word |= (bytes[index++] & 0xffUL) << 24;
-			word |= (bytes[index++] & 0xffUL) << 32;
-			word |= (bytes[index++] & 0xffUL) << 40;
-			word |= (bytes[index++] & 0xffUL) << 48;
-			word |= (bytes[index++] & 0xffUL) << 56;
-
-			return word;
-		}
-
-		/// <summary>
-		/// Write a 64 bit word to output in LSB first order.
-		/// </summary>
-		internal static void WordToBytes(ulong word, byte[] bytes, int off)
-		{
-			if ((off + 8) > bytes.Length)
-			{
-				// Help the JIT avoid index checks
-				throw new ArgumentException();
-			}
-			int index = off;
-
-			bytes[index++] = (byte)word;
-			bytes[index++] = (byte)(word >> 8);
-			bytes[index++] = (byte)(word >> 16);
-			bytes[index++] = (byte)(word >> 24);
-			bytes[index++] = (byte)(word >> 32);
-			bytes[index++] = (byte)(word >> 40);
-			bytes[index++] = (byte)(word >> 48);
-			bytes[index++] = (byte)(word >> 56);
-		}
-
 		/**
 	     * Rotate left + xor part of the mix operation.
 	     */