summary refs log tree commit diff
path: root/crypto/src/pkcs
diff options
context:
space:
mode:
authorMegan Woods <megan@flygfisk.com>2019-01-14 00:17:24 +1100
committerMegan Woods <megan@flygfisk.com>2019-01-14 00:17:24 +1100
commit785d36daf1d125b3fba16e1d92719e2a0f67698e (patch)
tree2dfb561f0be1a55b87f135997d5d3c1e552c05da /crypto/src/pkcs
parentFix some comments (diff)
downloadBouncyCastle.NET-ed25519-785d36daf1d125b3fba16e1d92719e2a0f67698e.tar.xz
Added ECGOST3410_2012Signer
Updated encoding of SubjectPublicKeyInfo and PrivateKeyInfo
Diffstat (limited to 'crypto/src/pkcs')
-rw-r--r--crypto/src/pkcs/PrivateKeyInfoFactory.cs44
1 files changed, 43 insertions, 1 deletions
diff --git a/crypto/src/pkcs/PrivateKeyInfoFactory.cs b/crypto/src/pkcs/PrivateKeyInfoFactory.cs
index 3036dc8b6..75a56983a 100644
--- a/crypto/src/pkcs/PrivateKeyInfoFactory.cs
+++ b/crypto/src/pkcs/PrivateKeyInfoFactory.cs
@@ -5,6 +5,7 @@ using Org.BouncyCastle.Asn1.CryptoPro;
 using Org.BouncyCastle.Asn1.EdEC;
 using Org.BouncyCastle.Asn1.Oiw;
 using Org.BouncyCastle.Asn1.Pkcs;
+using Org.BouncyCastle.Asn1.Rosstandart;
 using Org.BouncyCastle.Asn1.Sec;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Asn1.X9;
@@ -117,10 +118,35 @@ namespace Org.BouncyCastle.Pkcs
 
             if (privateKey is ECPrivateKeyParameters)
             {
-                ECPrivateKeyParameters priv = (ECPrivateKeyParameters)privateKey;
+                ECPrivateKeyParameters priv = (ECPrivateKeyParameters) privateKey;                 
                 DerBitString publicKey = new DerBitString(ECKeyPairGenerator.GetCorrespondingPublicKey(priv).Q.GetEncoded(false));
 
                 ECDomainParameters dp = priv.Parameters;
+
+                // ECGOST3410
+                if (dp is ECGOST3410Parameters)
+                {
+                    ECGOST3410Parameters domainParameters = (ECGOST3410Parameters) dp;
+
+                    Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
+                        (domainParameters).PublicKeyParamSet,
+                        (domainParameters).DigestParamSet,
+                        (domainParameters).EncryptionParamSet);
+
+                    bool is512 = priv.D.BitLength > 256;
+                    DerObjectIdentifier identifier = (is512) ?
+                        RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512 :
+                        RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
+                    int size = (is512) ? 64 : 32;
+
+                    byte[] encKey = new byte[size];
+
+                    ExtractBytes(encKey, size, 0, priv.D);
+
+                    return new PrivateKeyInfo(new AlgorithmIdentifier(identifier, gostParams), new DerOctetString(encKey));
+                } 
+
+
                 int orderBitLength = dp.N.BitLength;
 
                 AlgorithmIdentifier algID;
@@ -245,5 +271,21 @@ namespace Org.BouncyCastle.Pkcs
 
             return PrivateKeyInfo.GetInstance(keyBytes);
         }
+
+        private static void ExtractBytes(byte[] encKey, int size, int offSet, BigInteger bI)
+        {
+            byte[] val = bI.ToByteArray();
+            if (val.Length < size)
+            {
+                byte[] tmp = new byte[size];
+                Array.Copy(val, 0, tmp, tmp.Length - val.Length, val.Length);
+                val = tmp;
+            }
+
+            for (int i = 0; i != size; i++)
+            {
+                encKey[offSet + i] = val[val.Length - 1 - i];
+            }
+        }
     }
 }