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); }
public NoKeyConfirmationHmac( INoKeyConfirmationMacDataCreator macDataCreator, INoKeyConfirmationParameters noKeyConfirmationParameters, IHmac algo) : base(macDataCreator, noKeyConfirmationParameters) { Algo = algo; }
public KeyConfirmationHmac( IKeyConfirmationMacDataCreator macDataCreator, IKeyConfirmationParameters keyConfirmationParameters, IHmac iHmac) : base(macDataCreator, keyConfirmationParameters) { _iHmac = iHmac; _keyConfirmationParameters = keyConfirmationParameters; }
public IEccNonceProvider GetNonceProvider(NonceProviderTypes nonceTypes, IHmac hmac, IEntropyProvider entropyProvider) { switch (nonceTypes) { case NonceProviderTypes.Deterministic: return(new DeterministicNonceProvider(hmac)); case NonceProviderTypes.Random: return(new RandomNonceProvider(entropyProvider)); } throw new ArgumentException("Invalid nonce type provided"); }
/// <summary> /// Compute a Hmac hash for all the properties tagged with /// `HmacAttribute` /// </summary> /// <remarks>Uses reflection and a naive cache to figure out what /// properties to include in the hash.</remarks> /// <param name="obj"></param> /// <param name="secret">uses type name by default</param> /// <returns></returns> public static string ComputeHash(this IHmac obj, string secret = null) { var type = obj.GetType(); var keys = HmacProperties .Ensure(type, FindHmacProperties) .Select(m => m.Invoke(obj, null)); var hash = ComputeHash(secret ?? type.FullName, keys.ToArray()); if (obj.Hash == null) { obj.Hash = hash; } return(hash); }
/// <summary> /// Recompute the hash and check it /// </summary> /// <param name="obj"></param> /// <param name="secret">uses type name by default</param> /// <returns></returns> public static bool ValidateHash(this IHmac obj, string secret = null) { if (obj == null) { throw new ArgumentNullException("obj"); } if (string.IsNullOrEmpty(obj.Hash)) { throw new ArgumentOutOfRangeException("obj.Hash", "the object mush have a hash already"); } return(obj.Hash == ComputeHash(obj, secret)); }
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()); }
private BitString HmacPrf(IHmac hmac, BitString secret, BitString seed, int outputLength) { var hmacLen = hmac.OutputLength; var numBlocks = outputLength.CeilingDivide(hmacLen); var A = seed.GetDeepCopy(); var output = new BitString(0); for (var i = 0; i < numBlocks; i++) { A = hmac.Generate(secret, A).Mac; output = output.ConcatenateBits(hmac.Generate(secret, A.ConcatenateBits(seed)).Mac); } return(output); }
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()); }
public PskIkeV1(IHmac hmac) : base(hmac) { }
public TlsKdfv12(IHmac hmac) { _hmac = hmac; }
public DsaIkeV1(IHmac hmac) : base(hmac) { }
public TlsKdfv12_ExtendedMasterSecret(IHmac hmac) : base(hmac) { }
public DrbgHmac(IEntropyProvider entropyProvider, IHmac hmac, DrbgParameters drbgParameters) : base(entropyProvider, drbgParameters) { _hmac = hmac; HashAttributes = DrbgAttributesHelper.GetHashDrbgAttributes(drbgParameters.Mode); }
protected IkeV1Base(IHmac hmac) { Hmac = hmac; }
public Tpm(IHmac hmac) { _hmac = hmac; }
public TlsKdfv10v11(IHmac shaHmac, IHmac md5Hmac) { _shaHmac = shaHmac; _md5Hmac = md5Hmac; }
public PkeIkeV1(IHmac hmac, ISha sha) : base(hmac) { _sha = sha; }
public IkeV2(IHmac hmac) { _hmac = hmac; }
public Hkdf(IHmac hmac) { _hmac = hmac; }
private readonly BitString _01bits = new BitString("01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"); // Maximum possible length (512 bits) public DeterministicNonceProvider(IHmac hmac) { _hmac = hmac; }
public PbKdf(IHmac hmac) { _hmac = hmac; }