Esempio n. 1
0
        public override void ImportParameters(RSAParameters parameters)
        {
            if (parameters.Exponent == null || parameters.Modulus == null)
            {
                throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidRsaParameters));
            }
            bool publicOnly = parameters.P == null || parameters.Q == null;

            //
            // We need to build a key blob structured as follows:
            //     BCRYPT_RSAKEY_BLOB   header
            //     byte[cbPublicExp]    publicExponent      - Exponent
            //     byte[cbModulus]      modulus             - Modulus
            //     -- Private only --
            //     byte[cbPrime1]       prime1              - P
            //     byte[cbPrime2]       prime2              - Q
            //

            int blobSize = Marshal.SizeOf(typeof(BCryptNative.BCRYPT_RSAKEY_BLOB)) +
                           parameters.Exponent.Length +
                           parameters.Modulus.Length;

            if (!publicOnly)
            {
                blobSize += parameters.P.Length +
                            parameters.Q.Length;
            }

            byte[] rsaBlob = new byte[blobSize];
            unsafe
            {
                fixed(byte *pRsaBlob = rsaBlob)
                {
                    // Build the header
                    BCryptNative.BCRYPT_RSAKEY_BLOB *pBcryptBlob = (BCryptNative.BCRYPT_RSAKEY_BLOB *)pRsaBlob;
                    pBcryptBlob->Magic = publicOnly ? BCryptNative.KeyBlobMagicNumber.RsaPublic :
                                         BCryptNative.KeyBlobMagicNumber.RsaPrivate;

                    pBcryptBlob->BitLength = parameters.Modulus.Length * 8;

                    pBcryptBlob->cbPublicExp = parameters.Exponent.Length;
                    pBcryptBlob->cbModulus   = parameters.Modulus.Length;

                    if (!publicOnly)
                    {
                        pBcryptBlob->cbPrime1 = parameters.P.Length;
                        pBcryptBlob->cbPrime2 = parameters.Q.Length;
                    }

                    int offset = Marshal.SizeOf(typeof(BCryptNative.BCRYPT_RSAKEY_BLOB));

                    // Copy the exponent
                    Buffer.BlockCopy(parameters.Exponent, 0, rsaBlob, offset, parameters.Exponent.Length);
                    offset += parameters.Exponent.Length;

                    // Copy the modulus
                    Buffer.BlockCopy(parameters.Modulus, 0, rsaBlob, offset, parameters.Modulus.Length);
                    offset += parameters.Modulus.Length;

                    if (!publicOnly)
                    {
                        // Copy P
                        Buffer.BlockCopy(parameters.P, 0, rsaBlob, offset, parameters.P.Length);
                        offset += parameters.P.Length;

                        // Copy Q
                        Buffer.BlockCopy(parameters.Q, 0, rsaBlob, offset, parameters.Q.Length);
                        offset += parameters.Q.Length;
                    }
                }
            }
            Key = CngKey.Import(rsaBlob, publicOnly ? s_rsaPublicBlob : s_rsaPrivateBlob);
        }
Esempio n. 2
0
 internal static Exception HashAlgorithmNameNullOrEmpty()
 {
     return(new ArgumentException(SR.GetString(SR.Cryptography_HashAlgorithmNameNullOrEmpty), "hashAlgorithm"));
 }
Esempio n. 3
0
 private static Exception DerivedClassMustOverride()
 {
     return(new NotImplementedException(SR.GetString(SR.NotSupported_SubclassOverride)));
 }
Esempio n. 4
0
 /// <summary>
 /// When overridden in a derived class, generates a new public/private keypair for the specified curve.
 /// </summary>
 /// <param name="curve">The curve to use.</param>
 public virtual void GenerateKey(ECCurve curve)
 {
     throw new NotSupportedException(SR.GetString(SR.NotSupported_SubclassOverride));
 }
Esempio n. 5
0
 /// <summary>
 /// When overridden in a derived class, imports the specified ECParameters.
 /// </summary>
 /// <param name="parameters">The curve parameters.</param>
 public virtual void ImportParameters(ECParameters parameters)
 {
     throw new NotSupportedException(SR.GetString(SR.NotSupported_SubclassOverride));
 }
Esempio n. 6
0
 /// <summary>
 /// When overridden in a derived class, exports the explicit ECParameters for an ECCurve.
 /// </summary>
 /// <param name="includePrivateParameters">true to include private parameters, otherwise, false.</param>
 /// <returns>The ECParameters representing the point on the curve for this key, using the explicit curve format.</returns>
 public virtual ECParameters ExportExplicitParameters(bool includePrivateParameters)
 {
     throw new NotSupportedException(SR.GetString(SR.NotSupported_SubclassOverride));
 }