Example #1
0
        /// <summary>
        /// Try get algorithm from mechanism.
        /// </summary>
        /// <param name="mechanism">Algorithm mechanism.</param>
        /// <param name="algorithm">Algorithm.</param>
        /// <returns></returns>
        public static bool TryGetAlgorithm(string mechanism, out IAsymmetricAlgorithm algorithm)
        {
            mechanism = mechanism.Replace('_', '-').ToUpperInvariant();
            switch (mechanism)
            {
            case "DSA": algorithm = DSA; return(true);

            case "ECDSA": algorithm = ECDSA; return(true);

            case "ECGOST3410":
            case "ECGOST3410-2001":
            case "ECGOST-3410":
            case "ECGOST-3410-2001": algorithm = ECGOST3410; return(true);

            case "ED25519": algorithm = new Ed25519(); return(true);

            case "ED448": algorithm = new Ed448(); return(true);

            case "ELGAMAL": algorithm = (IAsymmetricAlgorithm)ElGamal; return(true);

            case "GOST3410":
            case "GOST3410-94":
            case "GOST-3410":
            case "GOST-3410-94": algorithm = GOST3410; return(true);

            case "RSA": algorithm = (IAsymmetricAlgorithm)RSA; return(true);

            case "SM2": algorithm = SM2; return(true);

            default: algorithm = null; return(false);
            }
        }
Example #2
0
        private static byte[] AsymmetricEncrypt([NotNull] IAsymmetricAlgorithm asymmetric, [NotNull] byte[] data, RSASettings settings = null)
        {
            if (data.Length == 0)
            {
                return(null);
            }

            if (settings is { UseExpiration : true })
Example #3
0
        /// <inheritdoc />
        /// <exception cref="ArgumentException" />
        /// <exception cref="SignatureVerificationException" />
        public void Validate(string decodedPayload, IAsymmetricAlgorithm alg, byte[] bytesToSign, byte[] decodedSignature)
        {
            var ex = GetValidationException(alg, decodedPayload, bytesToSign, decodedSignature);

            if (ex is object)
            {
                throw ex;
            }
        }
Example #4
0
        private Exception GetValidationException(IAsymmetricAlgorithm alg, string payloadJson, byte[] bytesToSign, byte[] decodedSignature)
        {
            if (!alg.Verify(bytesToSign, decodedSignature))
            {
                return(new SignatureVerificationException("The signature is invalid according to the validation procedure."));
            }

            return(GetValidationException(payloadJson));
        }
Example #5
0
 private static IAsymmetricAlgorithm EnsureAlgorithm(IAsymmetricAlgorithm asymmetricAlgorithm)
 {
     if (asymmetricAlgorithm is null)
     {
         return((IAsymmetricAlgorithm)AsymmetricAlgorithmHelper.RSA);
     }
     else if (asymmetricAlgorithm.Mechanism != "RSA")
     {
         throw new System.Security.Cryptography.CryptographicException("Requires RSA asymmetric algorithm.");
     }
     else
     {
         return(asymmetricAlgorithm);
     }
 }
Example #6
0
 private static IAsymmetricAlgorithm EnsureAlgorithm(IAsymmetricAlgorithm asymmetricAlgorithm)
 {
     if (asymmetricAlgorithm is null)
     {
         return(AsymmetricAlgorithmHelper.SM2);
     }
     else if (asymmetricAlgorithm.Mechanism != "SM2")
     {
         throw new CryptographicException("Requires SM2 asymmetric algorithm.");
     }
     else
     {
         return(asymmetricAlgorithm);
     }
 }
Example #7
0
        public static byte[] AsymmetricEncrypt([NotNull] X509Certificate2 certificate, [NotNull] byte[] data, RSASettings settings = null)
        {
            if (data.Length == 0)
            {
                return(null);
            }
            IAsymmetricAlgorithm asymmetric = null;

            try
            {
                System.Security.Cryptography.RSA algorithm = certificate.GetPublicEncryptor <System.Security.Cryptography.RSA>();
                asymmetric = new RSAAlgorithm <System.Security.Cryptography.RSA>(algorithm);
                return(AsymmetricEncrypt(asymmetric, data, settings));
            }
            finally
            {
                ObjectHelper.Dispose(ref asymmetric);
            }
        }
Example #8
0
        public static byte[] AsymmetricEncrypt([NotNull] string publicKeyXml, [NotNull] byte[] data, RSASettings settings = null)
        {
            if (publicKeyXml.Length == 0)
            {
                throw new ArgumentNullException(nameof(publicKeyXml));
            }
            if (data.Length == 0)
            {
                return(null);
            }

            IAsymmetricAlgorithm asymmetric = null;

            try
            {
                asymmetric = CreateAsymmetricAlgorithm(settings);
                asymmetric.FromXmlString(publicKeyXml);
                return(AsymmetricEncrypt(asymmetric, data, settings));
            }
            finally
            {
                ObjectHelper.Dispose(ref asymmetric);
            }
        }
Example #9
0
 /// <summary>
 /// RSA/ISO9796-2.
 /// </summary>
 /// <param name="hashAlgorithm">Hash algorithm.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public RSAandISO9796_2(IHashAlgorithm hashAlgorithm, IAsymmetricAlgorithm asymmetricAlgorithm)
     : base(string.Format(CultureInfo.InvariantCulture, "{0}withISO9796-2", hashAlgorithm.Mechanism), EnsureAlgorithm(asymmetricAlgorithm))
 {
     _hashAlgorithm = hashAlgorithm;
 }
Example #10
0
 /// <summary>
 /// GOST3410.
 /// </summary>
 /// <param name="hashAlgorithm">Hash algorithm.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public GOST3410(IHashAlgorithm hashAlgorithm, IAsymmetricAlgorithm asymmetricAlgorithm)
     : base(string.Format(CultureInfo.InvariantCulture, "{0}withGOST3410", hashAlgorithm.Mechanism), EnsureAlgorithm(asymmetricAlgorithm))
 {
     _hashAlgorithm = hashAlgorithm;
 }
Example #11
0
 /// <summary>
 /// RSA.
 /// <para/>Legal signature hash Algorithm:
 /// <see cref="MD2"/>,<see cref="MD4"/>,<see cref="MD5"/>,
 /// <see cref="SHA1"/>,<see cref="SHA224"/>,<see cref="SHA256"/>,<see cref="SHA384"/>,<see cref="SHA512"/>,
 /// <see cref="RIPEMD128"/>,<see cref="RIPEMD160"/>,<see cref="RIPEMD256"/>.
 /// </summary>
 /// <param name="hashAlgorithm">Hash algorithm.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public RSA(IHashAlgorithm hashAlgorithm, IAsymmetricAlgorithm asymmetricAlgorithm)
     : base(string.Format(CultureInfo.InvariantCulture, "{0}withRSA", hashAlgorithm.Mechanism), EnsureAlgorithm(asymmetricAlgorithm))
 {
     _hashAlgorithm = hashAlgorithm;
     _identifier    = GetAlgorithmIdentifier(base.Mechanism, _finder);
 }
Example #12
0
 /// <summary>
 /// PLAIN-ECDSA.
 /// </summary>
 /// <param name="hashAlgorithm">Hash algorithm.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public PLAIN_ECDSA(IHashAlgorithm hashAlgorithm, IAsymmetricAlgorithm asymmetricAlgorithm)
     : base(string.Format(CultureInfo.InvariantCulture, "{0}withPLAIN-ECDSA", hashAlgorithm.Mechanism), EnsureAlgorithm(asymmetricAlgorithm))
 {
     _hashAlgorithm = hashAlgorithm;
 }
Example #13
0
 /// <summary>
 /// RSAandMGF1.
 /// </summary>
 /// <param name="hashAlgorithm">Hash algorithm.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public RSAandMGF1(IHashAlgorithm hashAlgorithm, IAsymmetricAlgorithm asymmetricAlgorithm)
     : base(string.Format(CultureInfo.InvariantCulture, "{0}withRSAandMGF1", hashAlgorithm.Mechanism), EnsureAlgorithm(asymmetricAlgorithm))
 {
     _hashAlgorithMgf = hashAlgorithm;
 }
Example #14
0
 /// <summary>
 /// Ed25519.
 /// </summary>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public Ed25519(IAsymmetricAlgorithm asymmetricAlgorithm) : base("Ed25519", EnsureAlgorithm(asymmetricAlgorithm))
 {
 }
Example #15
0
 /// <summary>
 /// Ed25519ctx.
 /// <para/>Uses context byte[0] by default.
 /// </summary>
 /// <param name="context">Context.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public Ed25519ctx(byte[] context, IAsymmetricAlgorithm asymmetricAlgorithm) : base("Ed25519ctx", EnsureAlgorithm(asymmetricAlgorithm))
 {
     _context = context;
 }
Example #16
0
 private protected SignatureAlgorithm(string mechanism, IAsymmetricAlgorithm asymmetricAlgorithm)
 {
     this.Mechanism       = mechanism;
     _asymmetricAlgorithm = asymmetricAlgorithm;
     this.X509            = GetX509Oid(mechanism);
 }
Example #17
0
 /// <summary>
 /// Ed448ph.
 /// <para/>Uses context byte[0] by default.
 /// </summary>
 /// <param name="context">Context.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public Ed448ph(byte[] context, IAsymmetricAlgorithm asymmetricAlgorithm) : base("Ed448ph", EnsureAlgorithm(asymmetricAlgorithm))
 {
     _context = context;
 }
Example #18
0
 public InternalAsymmetricAlgorithmTests()
 {
     _algorithm = CoreExtensionBuilderHelper.CurrentServices.GetRequiredService <IAsymmetricAlgorithm>();
 }
Example #19
0
 /// <inheritdoc />
 /// <exception cref="ArgumentException" />
 public bool TryValidate(string payloadJson, IAsymmetricAlgorithm alg, byte[] bytesToSign, byte[] decodedSignature, out Exception ex)
 {
     ex = GetValidationException(alg, payloadJson, bytesToSign, decodedSignature);
     return(ex is null);
 }
Example #20
0
 /// <summary>
 /// RSA/X9.31.
 /// <para/>Legal key size is more than or equal to 512 bits (64 bits increments).
 /// <para/>Uses key size 2048 bits, certainty 25 by default.
 /// <para/>Legal signature hash Algorithm:
 /// <see cref="SHA1"/>,<see cref="SHA224"/>,<see cref="SHA256"/>,<see cref="SHA384"/>,<see cref="SHA512"/>,
 /// <see cref="RIPEMD128"/>,<see cref="RIPEMD160"/>,
 /// <see cref="SHA512T"/>/224,<see cref="SHA512T"/>/256,
 /// <see cref="Whirlpool"/>.
 /// </summary>
 /// <param name="hashAlgorithm">Hash algorithm.</param>
 /// <param name="asymmetricAlgorithm">Asymmetric algorithm. To provide function generate key pair, this argument is not required.</param>
 public RSAandX931(IHashAlgorithm hashAlgorithm, IAsymmetricAlgorithm asymmetricAlgorithm)
     : base(string.Format(CultureInfo.InvariantCulture, "{0}withRSA/X9.31", hashAlgorithm.Mechanism), asymmetricAlgorithm)
 {
     _hashAlgorithm = hashAlgorithm;
 }