summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2015-10-02 11:43:25 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2015-10-02 11:43:25 +0700
commit39aff2f1764dd0c79eb39091410d419d79776ac9 (patch)
treef36c3be4a832a3766bdf06eb8490b3bc894e0160
parentAdd NIST OIDs and DigestUtilities entries for SHA3 (diff)
downloadBouncyCastle.NET-ed25519-39aff2f1764dd0c79eb39091410d419d79776ac9.tar.xz
Port of recent ISO trailer updates from Java
-rw-r--r--crypto/crypto.csproj5
-rw-r--r--crypto/src/crypto/signers/Iso9796d2PssSigner.cs50
-rw-r--r--crypto/src/crypto/signers/Iso9796d2Signer.cs61
-rw-r--r--crypto/src/crypto/signers/IsoTrailers.cs57
-rw-r--r--crypto/src/crypto/signers/X931Signer.cs63
5 files changed, 134 insertions, 102 deletions
diff --git a/crypto/crypto.csproj b/crypto/crypto.csproj
index 488886e6c..b094843f2 100644
--- a/crypto/crypto.csproj
+++ b/crypto/crypto.csproj
@@ -4324,6 +4324,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "src\crypto\signers\IsoTrailers.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "src\crypto\signers\PSSSigner.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
diff --git a/crypto/src/crypto/signers/Iso9796d2PssSigner.cs b/crypto/src/crypto/signers/Iso9796d2PssSigner.cs
index 1486656bd..fb117c19d 100644
--- a/crypto/src/crypto/signers/Iso9796d2PssSigner.cs
+++ b/crypto/src/crypto/signers/Iso9796d2PssSigner.cs
@@ -26,29 +26,23 @@ namespace Org.BouncyCastle.Crypto.Signers
             return recoveredMessage;
         }
 
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerImplicit = 0xBC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerRipeMD160 = 0x31CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerRipeMD128 = 0x32CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha1 = 0x33CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha256 = 0x34CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha512 = 0x35CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha384 = 0x36CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerWhirlpool = 0x37CC;
 
-        private static readonly IDictionary trailerMap = Platform.CreateHashtable();
-
-        static Iso9796d2PssSigner()
-        {
-            trailerMap.Add("RIPEMD128", TrailerRipeMD128);
-            trailerMap.Add("RIPEMD160", TrailerRipeMD160);
-            trailerMap.Add("SHA-1", TrailerSha1);
-            trailerMap.Add("SHA-256", TrailerSha256);
-            trailerMap.Add("SHA-384", TrailerSha384);
-            trailerMap.Add("SHA-512", TrailerSha512);
-
-            trailerMap.Add("Whirlpool", TrailerWhirlpool);
-        }
-
         private IDigest digest;
         private IAsymmetricBlockCipher cipher;
 
@@ -71,8 +65,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         private int preTLength;
 
         /// <summary>
-        /// Generate a signer for the with either implicit or explicit trailers
-        /// for ISO9796-2, scheme 2 or 3.
+        /// Generate a signer with either implicit or explicit trailers for ISO9796-2, scheme 2 or 3.
         /// </summary>
         /// <param name="cipher">base cipher to use for signature creation/verification</param>
         /// <param name="digest">digest to use.</param>
@@ -91,15 +84,15 @@ namespace Org.BouncyCastle.Crypto.Signers
 
             if (isImplicit)
             {
-                trailer = TrailerImplicit;
+                trailer = IsoTrailers.TRAILER_IMPLICIT;
+            }
+            else if (IsoTrailers.NoTrailerAvailable(digest))
+            {
+                throw new ArgumentException("no valid trailer", "digest");
             }
             else
             {
-                string digestAlg = digest.AlgorithmName;
-                if (!trailerMap.Contains(digestAlg))
-                    throw new ArgumentException("no valid trailer for digest");
-
-                trailer = (int)trailerMap[digestAlg];
+                trailer = IsoTrailers.GetTrailer(digest);
             }
         }
 
@@ -180,7 +173,7 @@ namespace Org.BouncyCastle.Crypto.Signers
 
             block = new byte[(keyBits + 7) / 8];
 
-            if (trailer == TrailerImplicit)
+            if (trailer == IsoTrailers.TRAILER_IMPLICIT)
             {
                 mBuf = new byte[block.Length - digest.GetDigestSize() - saltLength - 1 - 1];
             }
@@ -247,11 +240,10 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 int sigTrail = ((block[block.Length - 2] & 0xFF) << 8) | (block[block.Length - 1] & 0xFF);
 
-                string digestAlg = digest.AlgorithmName;
-                if (!trailerMap.Contains(digestAlg))
+                if (IsoTrailers.NoTrailerAvailable(digest))
                     throw new ArgumentException("unrecognised hash in signature");
 
-                if (sigTrail != (int)trailerMap[digestAlg])
+                if (sigTrail != IsoTrailers.GetTrailer(digest))
                     throw new InvalidOperationException("signer initialised with wrong digest for trailer " + sigTrail);
 
                 tLength = 2;
@@ -395,7 +387,7 @@ namespace Org.BouncyCastle.Crypto.Signers
             digest.DoFinal(hash, 0);
 
             int tLength = 2;
-            if (trailer == TrailerImplicit)
+            if (trailer == IsoTrailers.TRAILER_IMPLICIT)
             {
                 tLength = 1;
             }
@@ -415,9 +407,9 @@ namespace Org.BouncyCastle.Crypto.Signers
 
             Array.Copy(hash, 0, block, block.Length - hLen - tLength, hLen);
 
-            if (trailer == TrailerImplicit)
+            if (trailer == IsoTrailers.TRAILER_IMPLICIT)
             {
-                block[block.Length - 1] = (byte)TrailerImplicit;
+                block[block.Length - 1] = (byte)IsoTrailers.TRAILER_IMPLICIT;
             }
             else
             {
diff --git a/crypto/src/crypto/signers/Iso9796d2Signer.cs b/crypto/src/crypto/signers/Iso9796d2Signer.cs
index 4bb4d17a6..b90ed8f0b 100644
--- a/crypto/src/crypto/signers/Iso9796d2Signer.cs
+++ b/crypto/src/crypto/signers/Iso9796d2Signer.cs
@@ -21,30 +21,23 @@ namespace Org.BouncyCastle.Crypto.Signers
             return recoveredMessage;
         }
 
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerImplicit = 0xBC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerRipeMD160 = 0x31CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerRipeMD128 = 0x32CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha1 = 0x33CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha256 = 0x34CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha512 = 0x35CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerSha384 = 0x36CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
         public const int TrailerWhirlpool = 0x37CC;
 
-        private static IDictionary trailerMap = Platform.CreateHashtable();
-
-        static Iso9796d2Signer()
-        {
-            trailerMap.Add("RIPEMD128", TrailerRipeMD128);
-            trailerMap.Add("RIPEMD160", TrailerRipeMD160);
-
-            trailerMap.Add("SHA-1", TrailerSha1);
-            trailerMap.Add("SHA-256", TrailerSha256);
-            trailerMap.Add("SHA-384", TrailerSha384);
-            trailerMap.Add("SHA-512", TrailerSha512);
-
-            trailerMap.Add("Whirlpool", TrailerWhirlpool);
-        }
-
         private IDigest digest;
         private IAsymmetricBlockCipher cipher;
 
@@ -60,8 +53,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         private byte[] preBlock;
 
         /// <summary>
-        /// Generate a signer for the with either implicit or explicit trailers
-        /// for ISO9796-2.
+        /// Generate a signer with either implicit or explicit trailers for ISO9796-2.
         /// </summary>
         /// <param name="cipher">base cipher to use for signature creation/verification</param>
         /// <param name="digest">digest to use.</param>
@@ -76,20 +68,15 @@ namespace Org.BouncyCastle.Crypto.Signers
 
             if (isImplicit)
             {
-                trailer = TrailerImplicit;
+                trailer = IsoTrailers.TRAILER_IMPLICIT;
+            }
+            else if (IsoTrailers.NoTrailerAvailable(digest))
+            {
+                throw new ArgumentException("no valid trailer", "digest");
             }
             else
             {
-                string digestName = digest.AlgorithmName;
-
-                if (trailerMap.Contains(digestName))
-                {
-                    trailer = (int)trailerMap[digest.AlgorithmName];
-                }
-                else
-                {
-                    throw new System.ArgumentException("no valid trailer for digest");
-                }
+                trailer = IsoTrailers.GetTrailer(digest);
             }
         }
 
@@ -119,7 +106,7 @@ namespace Org.BouncyCastle.Crypto.Signers
             keyBits = kParam.Modulus.BitLength;
 
             block = new byte[(keyBits + 7) / 8];
-            if (trailer == TrailerImplicit)
+            if (trailer == IsoTrailers.TRAILER_IMPLICIT)
             {
                 mBuf = new byte[block.Length - digest.GetDigestSize() - 2];
             }
@@ -195,10 +182,10 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 int sigTrail = ((block[block.Length - 2] & 0xFF) << 8) | (block[block.Length - 1] & 0xFF);
 
-                string digestName = digest.AlgorithmName;
-                if (!trailerMap.Contains(digestName))
+                if (IsoTrailers.NoTrailerAvailable(digest))
                     throw new ArgumentException("unrecognised hash in signature");
-                if (sigTrail != (int)trailerMap[digestName])
+
+                if (sigTrail != IsoTrailers.GetTrailer(digest))
                     throw new InvalidOperationException("signer initialised with wrong digest for trailer " + sigTrail);
 
                 delta = 2;
@@ -319,12 +306,12 @@ namespace Org.BouncyCastle.Crypto.Signers
             int t = 0;
             int delta = 0;
 
-            if (trailer == TrailerImplicit)
+            if (trailer == IsoTrailers.TRAILER_IMPLICIT)
             {
                 t = 8;
                 delta = block.Length - digSize - 1;
                 digest.DoFinal(block, delta);
-                block[block.Length - 1] = (byte) TrailerImplicit;
+                block[block.Length - 1] = (byte)IsoTrailers.TRAILER_IMPLICIT;
             }
             else
             {
@@ -424,10 +411,10 @@ namespace Org.BouncyCastle.Crypto.Signers
             {
                 int sigTrail = ((block[block.Length - 2] & 0xFF) << 8) | (block[block.Length - 1] & 0xFF);
 
-                string digestName = digest.AlgorithmName;
-                if (!trailerMap.Contains(digestName))
+                if (IsoTrailers.NoTrailerAvailable(digest))
                     throw new ArgumentException("unrecognised hash in signature");
-                if (sigTrail != (int)trailerMap[digestName])
+
+                if (sigTrail != IsoTrailers.GetTrailer(digest))
                     throw new InvalidOperationException("signer initialised with wrong digest for trailer " + sigTrail);
 
                 delta = 2;
diff --git a/crypto/src/crypto/signers/IsoTrailers.cs b/crypto/src/crypto/signers/IsoTrailers.cs
new file mode 100644
index 000000000..497ffaf78
--- /dev/null
+++ b/crypto/src/crypto/signers/IsoTrailers.cs
@@ -0,0 +1,57 @@
+using System;
+using System.Collections;
+
+using Org.BouncyCastle.Crypto.Digests;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Collections;
+
+namespace Org.BouncyCastle.Crypto.Signers
+{
+    public class IsoTrailers
+    {
+        public const int TRAILER_IMPLICIT    = 0xBC;
+        public const int TRAILER_RIPEMD160   = 0x31CC;
+        public const int TRAILER_RIPEMD128   = 0x32CC;
+        public const int TRAILER_SHA1        = 0x33CC;
+        public const int TRAILER_SHA256      = 0x34CC;
+        public const int TRAILER_SHA512      = 0x35CC;
+        public const int TRAILER_SHA384      = 0x36CC;
+        public const int TRAILER_WHIRLPOOL   = 0x37CC;
+        public const int TRAILER_SHA224      = 0x38CC;
+        public const int TRAILER_SHA512_224  = 0x39CC;
+        public const int TRAILER_SHA512_256  = 0x40CC;
+
+        private static IDictionary CreateTrailerMap()
+        {
+            IDictionary trailers = Platform.CreateHashtable();
+
+            trailers.Add("RIPEMD128", TRAILER_RIPEMD128);
+            trailers.Add("RIPEMD160", TRAILER_RIPEMD160);
+
+            trailers.Add("SHA-1", TRAILER_SHA1);
+            trailers.Add("SHA-224", TRAILER_SHA224);
+            trailers.Add("SHA-256", TRAILER_SHA256);
+            trailers.Add("SHA-384", TRAILER_SHA384);
+            trailers.Add("SHA-512", TRAILER_SHA512);
+            trailers.Add("SHA-512/224", TRAILER_SHA512_224);
+            trailers.Add("SHA-512/256", TRAILER_SHA512_256);
+
+            trailers.Add("Whirlpool", TRAILER_WHIRLPOOL);
+
+            return CollectionUtilities.ReadOnly(trailers);
+        }
+
+        // IDictionary is (string -> Int32)
+        private static readonly IDictionary trailerMap = CreateTrailerMap();
+
+        public static int GetTrailer(IDigest digest)
+        {
+            return (int)trailerMap[digest.AlgorithmName];
+        }
+
+        public static bool NoTrailerAvailable(IDigest digest)
+        {
+            return !trailerMap.Contains(digest.AlgorithmName);
+        }
+    }
+}
diff --git a/crypto/src/crypto/signers/X931Signer.cs b/crypto/src/crypto/signers/X931Signer.cs
index 89f512b78..36483fe17 100644
--- a/crypto/src/crypto/signers/X931Signer.cs
+++ b/crypto/src/crypto/signers/X931Signer.cs
@@ -20,31 +20,24 @@ namespace Org.BouncyCastle.Crypto.Signers
     public class X931Signer
         :   ISigner
     {
-        public const int TRAILER_IMPLICIT    = 0xBC;
-        public const int TRAILER_RIPEMD160   = 0x31CC;
-        public const int TRAILER_RIPEMD128   = 0x32CC;
-        public const int TRAILER_SHA1        = 0x33CC;
-        public const int TRAILER_SHA256      = 0x34CC;
-        public const int TRAILER_SHA512      = 0x35CC;
-        public const int TRAILER_SHA384      = 0x36CC;
-        public const int TRAILER_WHIRLPOOL   = 0x37CC;
-        public const int TRAILER_SHA224      = 0x38CC;
-
-        private static readonly IDictionary trailerMap = Platform.CreateHashtable();
-
-        static X931Signer()
-        {
-            trailerMap.Add("RIPEMD128", TRAILER_RIPEMD128);
-            trailerMap.Add("RIPEMD160", TRAILER_RIPEMD160);
-
-            trailerMap.Add("SHA-1", TRAILER_SHA1);
-            trailerMap.Add("SHA-224", TRAILER_SHA224);
-            trailerMap.Add("SHA-256", TRAILER_SHA256);
-            trailerMap.Add("SHA-384", TRAILER_SHA384);
-            trailerMap.Add("SHA-512", TRAILER_SHA512);
-
-            trailerMap.Add("Whirlpool", TRAILER_WHIRLPOOL);
-        }
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_IMPLICIT = 0xBC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_RIPEMD160 = 0x31CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_RIPEMD128 = 0x32CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_SHA1 = 0x33CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_SHA256 = 0x34CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_SHA512 = 0x35CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_SHA384 = 0x36CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_WHIRLPOOL = 0x37CC;
+        [Obsolete("Use 'IsoTrailers' instead")]
+        public const int TRAILER_SHA224 = 0x38CC;
 
         private IDigest                     digest;
         private IAsymmetricBlockCipher      cipher;
@@ -55,8 +48,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         private byte[]      block;
 
         /**
-         * Generate a signer for the with either implicit or explicit trailers
-         * for ISO9796-2.
+         * Generate a signer with either implicit or explicit trailers for X9.31.
          *
          * @param cipher base cipher to use for signature creation/verification
          * @param digest digest to use.
@@ -69,15 +61,15 @@ namespace Org.BouncyCastle.Crypto.Signers
 
             if (isImplicit)
             {
-                trailer = TRAILER_IMPLICIT;
+                trailer = IsoTrailers.TRAILER_IMPLICIT;
+            }
+            else if (IsoTrailers.NoTrailerAvailable(digest))
+            {
+                throw new ArgumentException("no valid trailer", "digest");
             }
             else
             {
-                string name = digest.AlgorithmName;
-                if (!trailerMap.Contains(name))
-                    throw new ArgumentException("no valid trailer", "digest");
-
-                trailer = (int)trailerMap[name];
+                trailer = IsoTrailers.GetTrailer(digest);
             }
         }
 
@@ -161,12 +153,11 @@ namespace Org.BouncyCastle.Crypto.Signers
             int digSize = digest.GetDigestSize();
 
             int delta;
-
-            if (trailer == TRAILER_IMPLICIT)
+            if (trailer == IsoTrailers.TRAILER_IMPLICIT)
             {
                 delta = block.Length - digSize - 1;
                 digest.DoFinal(block, delta);
-                block[block.Length - 1] = (byte)TRAILER_IMPLICIT;
+                block[block.Length - 1] = (byte)IsoTrailers.TRAILER_IMPLICIT;
             }
             else
             {