Пример #1
0
        /// <summary>
        /// Takes in a hash function, and nonce provider, and optional entropy provider (only used for 'random' nonce)
        /// </summary>
        /// <param name="hashFunction"></param>
        /// <param name="nonceProviderTypes"></param>
        /// <param name="entropyProvider"></param>
        /// <returns></returns>
        public IDsaEcc GetInstanceForSignatures(HashFunction hashFunction, NonceProviderTypes nonceProviderTypes, IEntropyProvider entropyProvider = null)
        {
            var sha           = _shaFactory.GetShaInstance(hashFunction);
            var hmac          = _hmacFactory.GetHmacInstance(hashFunction);
            var nonceProvider = _nonceProviderFactory.GetNonceProvider(nonceProviderTypes, hmac, entropyProvider);

            return(new EccDsa(sha, nonceProvider));
        }
Пример #2
0
        private IDrbg GetHashImplementation(DrbgParameters drbgParameters, IEntropyProvider iEntropyProvider)
        {
            var hashFunction = GetHashFunction(drbgParameters.Mode);
            var sha          = _shaFactory.GetShaInstance(hashFunction);

            return(new DrbgHash(iEntropyProvider, sha, drbgParameters));
        }
Пример #3
0
        public ISsh GetSshInstance(HashFunction hash, Cipher cipher)
        {
            var sha         = _shaFactory.GetShaInstance(hash);
            var lengthTuple = GetIvKeyLengthFromCipher(cipher);

            return(new Ssh(sha, lengthTuple.ivLength, lengthTuple.keyLength));
        }
Пример #4
0
        public KdfHmac(IHmacFactory hmacFactory, IShaFactory shaFactory, KdaOneStepAuxFunction auxFunction, bool useCounter)
        {
            UseCounter = useCounter;

            HashFunction hashFunction = null;

            switch (auxFunction)
            {
            case KdaOneStepAuxFunction.HMAC_SHA1:
                hashFunction = new HashFunction(ModeValues.SHA1, DigestSizes.d160);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA2_D224:
                hashFunction = new HashFunction(ModeValues.SHA2, DigestSizes.d224);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA2_D256:
                hashFunction = new HashFunction(ModeValues.SHA2, DigestSizes.d256);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA2_D384:
                hashFunction = new HashFunction(ModeValues.SHA2, DigestSizes.d384);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA2_D512:
                hashFunction = new HashFunction(ModeValues.SHA2, DigestSizes.d512);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA2_D512_T224:
                hashFunction = new HashFunction(ModeValues.SHA2, DigestSizes.d512t224);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA2_D512_T256:
                hashFunction = new HashFunction(ModeValues.SHA2, DigestSizes.d512t256);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA3_D224:
                hashFunction = new HashFunction(ModeValues.SHA3, DigestSizes.d224);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA3_D256:
                hashFunction = new HashFunction(ModeValues.SHA3, DigestSizes.d256);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA3_D384:
                hashFunction = new HashFunction(ModeValues.SHA3, DigestSizes.d384);
                break;

            case KdaOneStepAuxFunction.HMAC_SHA3_D512:
                hashFunction = new HashFunction(ModeValues.SHA3, DigestSizes.d512);
                break;

            default:
                throw new ArgumentException(nameof(auxFunction));
            }

            _hmac = hmacFactory.GetHmacInstance(hashFunction);
            _sha  = shaFactory.GetShaInstance(hashFunction);
        }
Пример #5
0
        public void ShouldEncryptDecryptToSameValue(string label, KeyPair rsaKeyPair, HashFunction hashFunction)
        {
            var numberOfTimes = 512; // Arbitrary

            var hash = _shaFactory.GetShaInstance(hashFunction);

            _subject = new RsaOaep(hash, new Mgf(hash), new Rsa(new RsaVisitor()), _entropyProviderFactory);

            var key = new BitString("CAFECAFEFACEFACE");

            for (var i = 0; i < numberOfTimes; i++)
            {
                var secretEncrypted = _subject.Encrypt(rsaKeyPair.PubKey, key, null);
                var secretDecrypted = _subject.Decrypt(rsaKeyPair, secretEncrypted.SharedSecretZ, null);

                Assert.AreEqual(key, secretDecrypted.SharedSecretZ);
            }
        }
Пример #6
0
        public IMaskFunction GetMaskInstance(PssMaskTypes maskType, HashFunction hashFunction = null)
        {
            switch (maskType)
            {
            case PssMaskTypes.MGF1:
                var shaMgf = _shaFactory.GetShaInstance(hashFunction);
                return(new Mgf1Mask(shaMgf));

            case PssMaskTypes.SHAKE128:
                var shake128 = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHAKE, DigestSizes.d128));
                return(new ShakeMask(shake128));

            case PssMaskTypes.SHAKE256:
                var shake256 = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHAKE, DigestSizes.d256));
                return(new ShakeMask(shake256));

            default:
                throw new ArgumentException($"Invalid {nameof(maskType)} provided");
            }
        }
Пример #7
0
        public void TestCasesFromGhIssues(string label, ModeValues mode, DigestSizes digestSize, int keyLen, string zzHex, string otherInfoHex, string expectedHex)
        {
            var zz        = new BitString(zzHex);
            var otherInfo = new BitString(otherInfoHex);

            var hashFunction = new HashFunction(mode, digestSize);
            var sha          = _factory.GetShaInstance(hashFunction);
            var subject      = new AnsiX942Concat(sha);

            var param = new ConcatAns942Parameters
            {
                Zz        = zz,
                KeyLen    = keyLen,
                OtherInfo = otherInfo
            };

            var result = subject.DeriveKey(param);

            Assert.True(result.Success);
            Assert.AreEqual(new BitString(expectedHex).ToHex(), result.DerivedKey.ToHex());
        }
Пример #8
0
        public IAnsiX942 GetInstance(AnsiX942Types type, HashFunction hashFunction)
        {
            var sha = _shaFactory.GetShaInstance(hashFunction);

            switch (type)
            {
            case AnsiX942Types.Concat:
                return(new AnsiX942Concat(sha));

            case AnsiX942Types.Der:
                return(new AnsiX942Der(sha));

            default:
                throw new Exception("No ANSI x9.42 KDF type specified");
            }
        }
Пример #9
0
        public IIkeV1 GetIkeV1Instance(AuthenticationMethods authMethods, HashFunction hash)
        {
            var sha  = _shaFactory.GetShaInstance(hash);
            var hmac = _hmacFactory.GetHmacInstance(hash);

            switch (authMethods)
            {
            case AuthenticationMethods.Dsa:
                return(new DsaIkeV1(hmac));

            case AuthenticationMethods.Pke:
                return(new PkeIkeV1(hmac, sha));

            case AuthenticationMethods.Psk:
                return(new PskIkeV1(hmac));

            default:
                throw new ArgumentException("No such authentication mode");
            }
        }
Пример #10
0
        public void IsoIec9797Tests(ModeValues mode, DigestSizes digestSize, int macLength, string ascii, int count, string keyHex, string expectedHex)
        {
            var sha = _shaFactory.GetShaInstance(new HashFunction(mode, digestSize));

            _subject = new NativeHmac(sha);
            var       key = new BitString(keyHex);
            BitString message;

            if (count == 0)
            {
                message = new BitString(Encoding.ASCII.GetBytes(ascii));
            }
            else
            {
                message = new BitString(Encoding.ASCII.GetBytes(ascii), count * 8);
            }

            var result = _subject.Generate(key, message, macLength);

            var expected = new BitString(expectedHex, macLength);

            Assert.AreEqual(expected.ToHex(), result.Mac.ToHex());
        }
Пример #11
0
        public void ShouldKdfCorrectly(
            string label,
            ModeValues modeValue,
            DigestSizes digestSize,
            int kdfSize,
            BigInteger z,
            BitString otherInfo,
            BitString expectedDerivedKeyingMaterial
            )
        {
            var sha = _shaFactory.GetShaInstance(new HashFunction(modeValue, digestSize));

            _subject = new KdfSha(sha, true);

            var result = _subject.DeriveKey(
                new BitString(z),
                kdfSize,
                otherInfo,
                null
                );

            Assert.That(result.Success);
            Assert.AreEqual(expectedDerivedKeyingMaterial.ToHex(), result.DerivedKey.ToHex());
        }
Пример #12
0
        public IHmac GetHmacInstance(HashFunction hashFunction)
        {
            var sha = _iShaFactory.GetShaInstance(hashFunction);

            return(new NativeHmac(sha));
        }
        private ISha GetSha(string hash)
        {
            var mapping = AlgorithmSpecificationToDomainMapping.GetMappingFromAlgorithm(hash);

            return(_shaFactory.GetShaInstance(new HashFunction(mapping.shaMode, mapping.shaDigestSize)));
        }
Пример #14
0
        public IKdfOneStep GetInstance(KdaOneStepAuxFunction auxFunction, bool useCounter)
        {
            switch (auxFunction)
            {
            case KdaOneStepAuxFunction.SHA1:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160)), useCounter));

            case KdaOneStepAuxFunction.SHA2_D224:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d224)), useCounter));

            case KdaOneStepAuxFunction.SHA2_D256:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256)), useCounter));

            case KdaOneStepAuxFunction.SHA2_D384:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d384)), useCounter));

            case KdaOneStepAuxFunction.SHA2_D512:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512)), useCounter));

            case KdaOneStepAuxFunction.SHA2_D512_T224:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t224)), useCounter));

            case KdaOneStepAuxFunction.SHA2_D512_T256:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t256)), useCounter));

            case KdaOneStepAuxFunction.SHA3_D224:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d224)), useCounter));

            case KdaOneStepAuxFunction.SHA3_D256:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d256)), useCounter));

            case KdaOneStepAuxFunction.SHA3_D384:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d384)), useCounter));

            case KdaOneStepAuxFunction.SHA3_D512:
                return(new KdfSha(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d512)), useCounter));

            case KdaOneStepAuxFunction.HMAC_SHA1:
            case KdaOneStepAuxFunction.HMAC_SHA2_D224:
            case KdaOneStepAuxFunction.HMAC_SHA2_D256:
            case KdaOneStepAuxFunction.HMAC_SHA2_D384:
            case KdaOneStepAuxFunction.HMAC_SHA2_D512:
            case KdaOneStepAuxFunction.HMAC_SHA2_D512_T224:
            case KdaOneStepAuxFunction.HMAC_SHA2_D512_T256:
            case KdaOneStepAuxFunction.HMAC_SHA3_D224:
            case KdaOneStepAuxFunction.HMAC_SHA3_D256:
            case KdaOneStepAuxFunction.HMAC_SHA3_D384:
            case KdaOneStepAuxFunction.HMAC_SHA3_D512:
                return(new KdfHmac(_hmacFactory, _shaFactory, auxFunction, useCounter));

            case KdaOneStepAuxFunction.KMAC_128:
                return(new KdfKmac(_kmacFactory, 256, useCounter));

            case KdaOneStepAuxFunction.KMAC_256:
                return(new KdfKmac(_kmacFactory, 512, useCounter));

            default:
                throw new ArgumentException(nameof(auxFunction));
            }
        }
Пример #15
0
        public IRsaOaep Get(KasHashAlg hashAlg)
        {
            ISha sha = null;

            switch (hashAlg)
            {
            case KasHashAlg.SHA1:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160));
                break;

            case KasHashAlg.SHA2_D224:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d224));
                break;

            case KasHashAlg.SHA2_D256:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256));
                break;

            case KasHashAlg.SHA2_D384:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d384));
                break;

            case KasHashAlg.SHA2_D512:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512));
                break;

            case KasHashAlg.SHA2_D512_T224:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t224));
                break;

            case KasHashAlg.SHA2_D512_T256:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t256));
                break;

            case KasHashAlg.SHA3_D224:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d224));
                break;

            case KasHashAlg.SHA3_D256:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d256));
                break;

            case KasHashAlg.SHA3_D384:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d384));
                break;

            case KasHashAlg.SHA3_D512:
                sha = _shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d512));
                break;
            }

            return(new RsaOaep(sha, new Mgf(sha), _rsa, _entropyFactory));
        }
Пример #16
0
 public IDsaFfc GetInstance(HashFunction hashFunction, EntropyProviderTypes entropyType = EntropyProviderTypes.Random)
 {
     return(new FfcDsa(_shaFactory.GetShaInstance(hashFunction), entropyType));
 }
Пример #17
0
 public ISnmp GetInstance()
 {
     return(new Snmp(_shaFactory.GetShaInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160))));
 }
Пример #18
0
        public IAnsiX963 GetInstance(HashFunction hashFunction)
        {
            var sha = _shaFactory.GetShaInstance(hashFunction);

            return(new AnsiX963(sha));
        }
Пример #19
0
        public ITlsKdf_v1_3 GetInstance(HashFunctions hashFunction)
        {
            var hf = ShaAttributes.GetHashFunctionFromEnum(hashFunction);

            return(new TlsKdfv13(_hkdfFactory.GetKdf(hf), _shaFactory.GetShaInstance(hf), hf.OutputLen));
        }