Example #1
0
        public void ShouldVerifyRandomlyGeneratedSignatures(ModeValues mode, DigestSizes digest, Curve curveEnum)
        {
            var nonces = new List <BigInteger>();

            var hashFunction = new HashFunction(mode, digest);
            var shaFactory   = new NativeShaFactory();
            var sha          = shaFactory.GetShaInstance(hashFunction);
            var hmacFactory  = new HmacFactory(shaFactory);
            var hmac         = hmacFactory.GetHmacInstance(hashFunction);

            var subject = new EccDsa(sha, new DeterministicNonceProvider(hmac), EntropyProviderTypes.Random);

            var curveFactory = new EccCurveFactory();
            var curve        = curveFactory.GetCurve(curveEnum);
            var domainParams = new EccDomainParameters(curve);
            var key          = subject.GenerateKeyPair(domainParams).KeyPair;

            var rand = new Random800_90();

            for (var i = 0; i < 100; i++)
            {
                var message = rand.GetRandomBitString(1024);

                var signature = subject.Sign(domainParams, key, message).Signature;
                var verify    = subject.Verify(domainParams, key, message, signature);

                nonces.Add(signature.R);

                Assert.IsTrue(verify.Success, verify.ErrorMessage);
            }

            // Check nonces for uniqueness
            Assert.AreEqual(nonces.Count, nonces.Distinct().Count(), "Repeated nonce detected");
        }
Example #2
0
        public void ShouldReturnCorrectMac(ModeValues modeValue, DigestSizes digestSize,
                                           int keySize, int tagLength,
                                           BitString serverId, BitString iutId,
                                           BitString serverPublicKey, BitString iutPublicKey,
                                           BitString derivedKeyingMaterial,
                                           BitString expectedMacData, BitString expectedTag)
        {
            var hmac = _hmacFactory.GetHmacInstance(new HashFunction(modeValue, digestSize));

            var p = new KeyConfirmationParameters(
                KeyAgreementRole.InitiatorPartyU,
                KeyConfirmationRole.Provider,
                KeyConfirmationDirection.Bilateral,
                KeyAgreementMacType.CmacAes, // note this doesn't matter for the scope of this test
                keySize,
                tagLength,
                iutId,
                serverId,
                iutPublicKey,
                serverPublicKey,
                derivedKeyingMaterial
                );

            _subject = new KeyConfirmationHmac(new KeyConfirmationMacDataCreator(), p, hmac);

            var result = _subject.ComputeMac();

            Assert.That(result.Success);
            Assert.AreEqual(expectedMacData.ToHex(), result.MacData.ToHex(), nameof(expectedMacData));
            Assert.AreEqual(expectedTag.ToHex(), result.Mac.ToHex(), nameof(expectedTag));
        }
Example #3
0
        public void ShouldHmacCorrectlySpotChecks(ModeValues mode, DigestSizes digestSize, int macLen, string msgHex, string keyHex, string expectedHex)
        {
            var hmacFactory = new HmacFactory(new NativeShaFactory());
            var hmac        = hmacFactory.GetHmacInstance(new HashFunction(mode, digestSize));

            var msg      = new BitString(msgHex);
            var key      = new BitString(keyHex);
            var expected = new BitString(expectedHex);

            var result = hmac.Generate(key, msg, macLen);

            Assert.That(result.Success);
            Assert.AreEqual(expected.ToHex(), result.Mac.ToHex());
        }
Example #4
0
        public void ShouldHmacCorrectly(string label, ModeValues mode, DigestSizes digestSize, int keyByteSize, int additionToIndexInKey, BitString expectedHmac, int macLength)
        {
            var hashFunction = new HashFunction(mode, digestSize);
            var factory      = new HmacFactory(new NativeShaFactory());

            _subject = factory.GetHmacInstance(hashFunction);

            var key     = GenKey(keyByteSize, additionToIndexInKey);
            var message = GetBitStringFromString(label);

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

            Assert.AreEqual(expectedHmac.ToHex(), result.Mac.ToHex());
        }
Example #5
0
        public void ShouldReturnHmacInstance()
        {
            var result = _subject.GetHmacInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160));

            Assert.IsInstanceOf(typeof(NativeHmac), result);
        }