summary refs log tree commit diff
path: root/crypto/src/x509
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/x509
parentFix some comments (diff)
downloadBouncyCastle.NET-ed25519-785d36daf1d125b3fba16e1d92719e2a0f67698e.tar.xz
Added ECGOST3410_2012Signer
Updated encoding of SubjectPublicKeyInfo and PrivateKeyInfo
Diffstat (limited to 'crypto/src/x509')
-rw-r--r--crypto/src/x509/SubjectPublicKeyInfoFactory.cs66
1 files changed, 65 insertions, 1 deletions
diff --git a/crypto/src/x509/SubjectPublicKeyInfoFactory.cs b/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
index 2fa8b7a28..234bcff34 100644
--- a/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
+++ b/crypto/src/x509/SubjectPublicKeyInfoFactory.cs
@@ -6,12 +6,14 @@ 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.X509;
 using Org.BouncyCastle.Asn1.X9;
 using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Math.EC;
 using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
 
 namespace Org.BouncyCastle.X509
 {
@@ -91,9 +93,54 @@ namespace Org.BouncyCastle.X509
             } // End of RSA.
 
             if (publicKey is ECPublicKeyParameters)
-            {
+            {            
+               
                 ECPublicKeyParameters _key = (ECPublicKeyParameters) publicKey;
 
+
+                if (_key.Parameters is ECGOST3410Parameters)
+                {
+                    ECGOST3410Parameters gostParams = (ECGOST3410Parameters)_key.Parameters;
+
+                    BigInteger bX = _key.Q.AffineXCoord.ToBigInteger();
+                    BigInteger bY = _key.Q.AffineYCoord.ToBigInteger();
+                    bool is512 = (bX.BitLength > 256);
+
+                    Gost3410PublicKeyAlgParameters parameters = new Gost3410PublicKeyAlgParameters(
+                        gostParams.PublicKeyParamSet,
+                        gostParams.DigestParamSet,
+                        gostParams.EncryptionParamSet);
+
+                    int encKeySize;
+                    int offset;
+                    DerObjectIdentifier algIdentifier;
+                    if (is512)
+                    {
+                        encKeySize = 128;
+                        offset = 64;
+                        algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
+                    }
+                    else
+                    {
+                        encKeySize = 64;
+                        offset = 32;
+                        algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
+                    }
+
+                    byte[] encKey = new byte[encKeySize];
+               
+                    ExtractBytes(encKey, encKeySize / 2, 0, bX);
+                    ExtractBytes(encKey, encKeySize / 2, offset, bY);
+                  
+                    return new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, parameters), new DerOctetString(encKey));
+                   
+
+                } // End of ECGOST3410_2012
+
+
+
+
+
                 if (_key.AlgorithmName == "ECGOST3410")
                 {
                     if (_key.PublicKeyParamSet == null)
@@ -209,5 +256,22 @@ namespace Org.BouncyCastle.X509
                 encKey[offset + i] = val[val.Length - 1 - i];
             }
         }
+
+
+        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];
+            }
+        }
     }
 }