Exemplo n.º 1
0
        public void ShouldMultiExpandSameResult()
        {
            var fixedInfo = new BitString(128);
            var salt      = new BitString(128);
            var z         = new BitString(128);
            var hmacAlg   = HashFunctions.Sha2_d256;
            var l         = 256;

            var hkdfParam = new KdfParameterHkdf()
            {
                L       = l,
                Salt    = salt,
                Z       = z,
                HmacAlg = hmacAlg
            };
            var multiExpandHkdfParam = new KdfMultiExpansionParameterHkdf()
            {
                Salt                = salt,
                Z                   = z,
                HmacAlg             = hmacAlg,
                IterationParameters = new List <KdfMultiExpansionIterationParameter>()
                {
                    new KdfMultiExpansionIterationParameter(l, fixedInfo),
                    new KdfMultiExpansionIterationParameter(l, fixedInfo),
                }
            };

            var hkdfResult            = _kdfVisitor.Kdf(hkdfParam, fixedInfo);
            var multiExpandHkdfResult = _kdfMultiExpansionVisitor.Kdf(multiExpandHkdfParam);

            Assert.True(multiExpandHkdfResult.Results.Count == 2);
            Assert.AreEqual(hkdfResult.DerivedKey, multiExpandHkdfResult.Results[0].DerivedKey);
            Assert.AreEqual(hkdfResult.DerivedKey, multiExpandHkdfResult.Results[1].DerivedKey);
        }
Exemplo n.º 2
0
        public KdfResult Kdf(KdfParameterOneStep param, BitString fixedInfo)
        {
            var result      = _kdfVisitor.Kdf(param, fixedInfo);
            var dkmBytesLen = result.DerivedKey.BitLength.CeilingDivide(BitString.BITSINBYTE);

            // Modify a random byte within DKM
            result.DerivedKey[_random.GetRandomInt(0, dkmBytesLen)] += 2;

            return(result);
        }
Exemplo n.º 3
0
        public void ShouldKdf()
        {
            var twoStepParam = new KdfParameterTwoStep()
            {
                L               = 256,
                Salt            = new BitString(128),
                Z               = new BitString(128),
                CounterLen      = 8,
                CounterLocation = CounterLocations.AfterFixedData,
                KdfMode         = KdfModes.Counter,
                MacMode         = MacModes.HMAC_SHA224,
            };

            var dkm = _kdfVisitor.Kdf(twoStepParam, new BitString(128));

            Assert.AreEqual("EB9436CDC0C6FBC168A3BDE32929C104C2E4F4C1DEA2CA3485A7799E49870E0C", dkm.DerivedKey.ToHex());
        }
Exemplo n.º 4
0
        public KdfResult Kdf(KdfParameterOneStep param, BitString fixedInfo)
        {
            var zBytesLen = param.Z.BitLength.CeilingDivide(BitString.BITSINBYTE);

            var modifiedParam = new KdfParameterOneStep()
            {
                L                  = param.L,
                Salt               = param.Salt,
                Z                  = param.Z.GetDeepCopy(),
                AuxFunction        = param.AuxFunction,
                FixedInfoPattern   = param.FixedInfoPattern,
                FixedInputEncoding = param.FixedInputEncoding
            };

            // Modify a random byte within Z
            modifiedParam.Z[_random.GetRandomInt(0, zBytesLen)] += 2;

            return(_kdfVisitor.Kdf(modifiedParam, fixedInfo));
        }
Exemplo n.º 5
0
        public void ShouldKdfWithEntropyBits()
        {
            var fixedInfo = new FixedInfoFactory(new FixedInfoStrategyFactory()).Get()
                            .Get(new FixedInfoParameter()
            {
                FixedInfoPattern = "entropyBits[96]",
                EntropyBits      = new BitString("1EE48EE4593B28D592D6ABFD"),
                L        = 256,
                Encoding = FixedInfoEncoding.Concatenation,
            });
            var result = _kdfVisitor.Kdf(new KdfParameterOneStep()
            {
                L           = 256,
                Z           = new BitString("A641EE6887A5C679A6398B398DC14394B8BE1750A38616A80A5C3D3A59DDB302"),
                AuxFunction = KdaOneStepAuxFunction.SHA2_D256
            }, fixedInfo);

            var expectedDkm = new BitString("C56E3F1ADA02DB5511C7941839A68A4CAFD7B3F3BBFDBAAD430F49619FFEDF28");

            Assert.AreEqual(expectedDkm.ToHex(), result.DerivedKey.ToHex());
        }
Exemplo n.º 6
0
 public KdfResult AcceptKdf(IKdfVisitor visitor, BitString fixedInfo)
 {
     return(visitor.Kdf(this, fixedInfo));
 }