public KdfResult Kdf(KdfParameterTwoStep 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); }
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()); }
public KdfResult Kdf(KdfParameterTwoStep param, BitString fixedInfo) { var zBytesLen = param.Z.BitLength.CeilingDivide(BitString.BITSINBYTE); var modifiedParam = new KdfParameterTwoStep() { L = param.L, Salt = param.Salt, Z = param.Z.GetDeepCopy(), FixedInfoPattern = param.FixedInfoPattern, FixedInputEncoding = param.FixedInputEncoding, MacMode = param.MacMode, KdfMode = param.KdfMode, Iv = param.Iv, CounterLocation = param.CounterLocation, CounterLen = param.CounterLen }; // Modify a random byte within Z modifiedParam.Z[_random.GetRandomInt(0, zBytesLen)] += 2; return(_kdfVisitor.Kdf(modifiedParam, fixedInfo)); }
public KdfResult Kdf(KdfParameterTwoStep param, BitString fixedInfo) { IMac randomnessExtractionMac = null; MacModes keyExpansionMacMode = param.MacMode; switch (param.MacMode) { case MacModes.CMAC_AES128: randomnessExtractionMac = _cmacFactory.GetCmacInstance(CmacTypes.AES128); break; case MacModes.CMAC_AES192: randomnessExtractionMac = _cmacFactory.GetCmacInstance(CmacTypes.AES192); keyExpansionMacMode = MacModes.CMAC_AES128; break; case MacModes.CMAC_AES256: randomnessExtractionMac = _cmacFactory.GetCmacInstance(CmacTypes.AES256); keyExpansionMacMode = MacModes.CMAC_AES128; break; case MacModes.HMAC_SHA1: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160)); break; case MacModes.HMAC_SHA224: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d224)); break; case MacModes.HMAC_SHA256: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256)); break; case MacModes.HMAC_SHA384: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d384)); break; case MacModes.HMAC_SHA512: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512)); break; case MacModes.HMAC_SHA_d512t224: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t224)); break; case MacModes.HMAC_SHA_d512t256: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t256)); break; case MacModes.HMAC_SHA3_224: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d224)); break; case MacModes.HMAC_SHA3_256: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d256)); break; case MacModes.HMAC_SHA3_384: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d384)); break; case MacModes.HMAC_SHA3_512: randomnessExtractionMac = _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d512)); break; default: throw new ArgumentException($"Invalid {nameof(MacModes)} provided to KdfVisitor."); } // Randomness extraction (step one) var randomnessExtraction = randomnessExtractionMac.Generate(param.Salt, param.Z); // Key Expansion (step two) var kdf = _kdfTwoStepFactory.GetKdfInstance( param.KdfMode, keyExpansionMacMode, param.CounterLocation, param.CounterLen); return(new KdfResult(kdf.DeriveKey(randomnessExtraction.Mac, fixedInfo, param.L, param.Iv, 0).DerivedKey)); }