示例#1
0
        public static byte[] HmacData(TpmAlgId hashAlgId, byte[] key, byte[] dataToHash)
        {
#if TSS_USE_BCRYPT
            string algName = Native.BCryptHashAlgName(hashAlgId);
            if (string.IsNullOrEmpty(algName))
            {
                Globs.Throw <ArgumentException>("HmacData(): Unsupported hash algorithm " + hashAlgId);
                return(null);
            }

            var alg    = new BCryptAlgorithm(algName, Native.BCRYPT_ALG_HANDLE_HMAC);
            var digest = alg.HmacData(key, dataToHash);
            alg.Close();
            return(digest);
#else
            switch (hashAlgId)
            {
            case TpmAlgId.Sha1:
                using (var h = new HMACSHA1(key))
                {
                    return(h.ComputeHash(dataToHash));
                }

            case TpmAlgId.Sha256:
                using (var h2 = new HMACSHA256(key))
                {
                    return(h2.ComputeHash(dataToHash));
                }

            case TpmAlgId.Sha384:
                using (var h3 = new HMACSHA384(key))
                {
                    return(h3.ComputeHash(dataToHash));
                }

            case TpmAlgId.Sha512:
                using (var h4 = new HMACSHA512(key))
                {
                    return(h4.ComputeHash(dataToHash));
                }

            default:
                Globs.Throw <ArgumentException>("HmacData(): Unsupported hash algorithm " + hashAlgId);
                return(null);
            }
#endif // !TSS_USE_BCRYPT
        }
示例#2
0
        public static byte[] Mac(TpmAlgId symAlg, TpmAlgId macScheme, byte[] key, byte[] data)
        {
            if (symAlg != TpmAlgId.Aes)
            {
                Globs.Throw <ArgumentException>("CryptoLib.Mac(): Unsupported symmetric algorithm" + symAlg);
                return(null);
            }
            if (macScheme != TpmAlgId.Cmac)
            {
                Globs.Throw <ArgumentException>("CryptoLib.Mac(): Unsupported MAC scheme " + macScheme);
                return(null);
            }

#if TSS_USE_BCRYPT
            var alg    = new BCryptAlgorithm(Native.BCRYPT_AES_CMAC_ALGORITHM);
            var digest = alg.HmacData(key, data);
            alg.Close();
            return(digest);
#else
            Globs.Throw <ArgumentException>("Mac(): .Net Crypto API does not support symmetric cipher based MAC." +
                                            "Complile TSS.Net with BCrypt enabled.");
            return(null);
#endif // !TSS_USE_BCRYPT
        }
示例#3
0
        public static byte[] HmacData(TpmAlgId hashAlgId, byte[] key, byte[] dataToHash)
        {
#if TSS_USE_BCRYPT
            string algName = Native.BCryptHashAlgName(hashAlgId);
            if (string.IsNullOrEmpty(algName))
            {
                Globs.Throw<ArgumentException>("HmacData(): Unsupported hash algorithm " + hashAlgId);
                return null;
            }

            var alg = new BCryptAlgorithm(algName, Native.BCRYPT_ALG_HANDLE_HMAC);
            var digest = alg.HmacData(key, dataToHash);
            alg.Close();
            return digest;
#else
            switch (hashAlgId)
            {
                case TpmAlgId.Sha1:
                    using (var h = new HMACSHA1(key))
                    {
                        return h.ComputeHash(dataToHash);
                    }
                case TpmAlgId.Sha256:
                    using (var h2 = new HMACSHA256(key))
                    {
                        return h2.ComputeHash(dataToHash);
                    }
                case TpmAlgId.Sha384:
                    using (var h3 = new HMACSHA384(key))
                    {
                        return h3.ComputeHash(dataToHash);
                    }
                case TpmAlgId.Sha512:
                    using (var h4 = new HMACSHA512(key))
                    {
                        return h4.ComputeHash(dataToHash);
                    }
                default:
                    Globs.Throw<ArgumentException>("HmacData(): Unsupported hash algorithm " + hashAlgId);
                    return null;
            }
#endif // !TSS_USE_BCRYPT
        }