/// <summary>
 /// Creates parameters for the CKM_RSA_PKCS_PSS mechanism
 /// </summary>
 /// <param name='hashAlg'>Hash algorithm used in the PSS encoding (CKM)</param>
 /// <param name='mgf'>Mask generation function to use on the encoded block (CKG)</param>
 /// <param name='len'>Length, in bytes, of the salt value used in the PSS encoding</param>
 /// <returns>Parameters for the CKM_RSA_PKCS_PSS mechanism</returns>
 public ICkRsaPkcsPssParams CreateCkRsaPkcsPssParams(ulong hashAlg, ulong mgf, ulong len)
 {
     return _factory.CreateCkRsaPkcsPssParams(hashAlg, mgf, len);
 }
Example #2
0
        /// <summary>
        /// Creates parameters for CKM_RSA_PKCS_PSS mechanism
        /// </summary>
        /// <param name="mechanismParamsFactory">Factory for creation of IMechanismParams instances</param>
        /// <param name="hash">Hash value</param>
        /// <param name="hashAlgorithm">Hash algorithm</param>
        /// <returns>Parameters for CKM_RSA_PKCS_PSS mechanism or null</returns>
        private static ICkRsaPkcsPssParams CreateCkRsaPkcsPssParams(IMechanismParamsFactory mechanismParamsFactory, byte[] hash, HashAlgorithmName hashAlgorithm)
        {
            if (hash == null || hash.Length == 0)
            {
                throw new ArgumentNullException(nameof(hash));
            }

            ICkRsaPkcsPssParams pssParams = null;

            if (hashAlgorithm == HashAlgorithmName.SHA1)
            {
                if (hash.Length != 20)
                {
                    throw new ArgumentException("Invalid lenght of hash value");
                }

                pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams(
                    hashAlg: (ulong)CKM.CKM_SHA_1,
                    mgf: (ulong)CKG.CKG_MGF1_SHA1,
                    len: (ulong)hash.Length
                    );
            }
            else if (hashAlgorithm == HashAlgorithmName.SHA256)
            {
                if (hash.Length != 32)
                {
                    throw new ArgumentException("Invalid lenght of hash value");
                }

                pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams(
                    hashAlg: (ulong)CKM.CKM_SHA256,
                    mgf: (ulong)CKG.CKG_MGF1_SHA256,
                    len: (ulong)hash.Length
                    );
            }
            else if (hashAlgorithm == HashAlgorithmName.SHA384)
            {
                if (hash.Length != 48)
                {
                    throw new ArgumentException("Invalid lenght of hash value");
                }

                pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams(
                    hashAlg: (ulong)CKM.CKM_SHA384,
                    mgf: (ulong)CKG.CKG_MGF1_SHA384,
                    len: (ulong)hash.Length
                    );
            }
            else if (hashAlgorithm == HashAlgorithmName.SHA512)
            {
                if (hash.Length != 64)
                {
                    throw new ArgumentException("Invalid lenght of hash value");
                }

                pssParams = mechanismParamsFactory.CreateCkRsaPkcsPssParams(
                    hashAlg: (ulong)CKM.CKM_SHA512,
                    mgf: (ulong)CKG.CKG_MGF1_SHA512,
                    len: (ulong)hash.Length
                    );
            }

            return(pssParams);
        }