public unsafe KeySchedule(IConnectionStateTls13 state, EphemeralBufferPoolWindows pool, ReadableBuffer resumptionSecret) { _pool = pool; _stateData = pool.Rent(); _state = state; _hashSize = CryptoProvider.HashProvider.HashSize(CipherSuite.HashType); _stateData.Memory.TryGetPointer(out _secret); _clientHandshakeTrafficSecret = ((byte *)_secret) + _hashSize; _serverHandshakeTrafficSecret = _clientHandshakeTrafficSecret + _hashSize; _masterSecret = _serverHandshakeTrafficSecret + _hashSize; _clientApplicationTrafficSecret = _masterSecret + _hashSize; _serverApplicationTrafficSecret = _clientApplicationTrafficSecret + _hashSize; void *resumptionPointer = null; int secretLength = 0; if (resumptionSecret.Length > 0) { var stackSecret = stackalloc byte[resumptionSecret.Length]; resumptionSecret.CopyTo(new Span <byte>(stackSecret, resumptionSecret.Length)); secretLength = resumptionSecret.Length; resumptionPointer = stackSecret; } HkdfFunctions.HkdfExtract(CryptoProvider.HashProvider, CipherSuite.HashType, null, 0, resumptionPointer, secretLength, _secret, _hashSize); }
public unsafe void Hkdf(string input) { var lines = input.Split('\n').Select(l => l.Trim().Split('=')).ToDictionary(val => val[0], val => val[1]); HashType hashType = (HashType)Enum.Parse(typeof(HashType), lines["Hash"], true); var ikm = StringToByteArray(lines["IKM"]); var salt = string.IsNullOrEmpty(lines["salt"]) ? new byte[0] : StringToByteArray(lines["salt"]); var info = string.IsNullOrEmpty(lines["info"]) ? new byte[0] : StringToByteArray(lines["info"]); var prk = StringToByteArray(lines["PRK"]); var okm = StringToByteArray(lines["OKM"]); var provider = new HashProvider(); var prkResult = new byte[provider.HashSize(hashType)]; var okmResult = new byte[okm.Length]; fixed(byte *iPtr = ikm) fixed(byte *sPtr = salt) fixed(byte *prPtr = prkResult) { HkdfFunctions.HkdfExtract(provider, hashType, sPtr, salt.Length, iPtr, ikm.Length, prPtr, prkResult.Length); HkdfFunctions.HkdfExpand(provider, hashType, prPtr, prkResult.Length, new Span <byte>(info), new Span <byte>(okmResult)); } Assert.Equal <byte>(prkResult, prk); for (int i = 0; i < okmResult.Length; i++) { Assert.Equal(okmResult[i], okm[i]); } }
public unsafe void GenerateResumptionSecret() { var hash = stackalloc byte[_hashSize]; _state.HandshakeHash.InterimHash(hash, _hashSize); _resumptionSecret = HkdfFunctions.ResumptionSecret(CryptoProvider.HashProvider, CipherSuite.HashType, _masterSecret, new Span <byte>(hash, _hashSize)); }
public void GenerateEarlyTrafficKey(ref IBulkCipherInstance earlyDataKey) { var hash = stackalloc byte[_hashSize]; _state.HandshakeHash.InterimHash(hash, _hashSize); var hashSpan = new Span <byte>(hash, _hashSize); HkdfFunctions.ClientEarlyTrafficSecret(CryptoProvider.HashProvider, CipherSuite.HashType, _secret, hashSpan, new Span <byte>(_clientHandshakeTrafficSecret, _hashSize)); earlyDataKey = GetKey(_clientHandshakeTrafficSecret, _hashSize); }
public unsafe void SetDheDerivedValue(IKeyshareInstance keyShare) { if (keyShare != null) { keyShare.DeriveSecret(CryptoProvider.HashProvider, CipherSuite.HashType, _secret, _hashSize, _secret, _hashSize); } else { HkdfFunctions.HkdfExtract(CryptoProvider.HashProvider, CipherSuite.HashType, null, 0, _secret, _hashSize, _secret, _hashSize); } }
private unsafe IBulkCipherInstance GetKey(byte *secret, int secretLength) { var newKey = CryptoProvider.CipherProvider.GetCipherKey(CipherSuite.BulkCipherType); var key = stackalloc byte[newKey.KeyLength]; var keySpan = new Span <byte>(key, newKey.KeyLength); var iv = stackalloc byte[newKey.IVLength]; var ivSpan = new Span <byte>(iv, newKey.IVLength); HkdfFunctions.HkdfExpandLabel(CryptoProvider.HashProvider, CipherSuite.HashType , secret, secretLength, Tls1_3Consts.TrafficKey, new Span <byte>(), keySpan); newKey.SetKey(keySpan); HkdfFunctions.HkdfExpandLabel(CryptoProvider.HashProvider, CipherSuite.HashType , secret, secretLength, Tls1_3Consts.TrafficIv, new Span <byte>(), ivSpan); newKey.SetIV(ivSpan); return(newKey); }
public byte[] GenerateClientFinishedKey() { return(HkdfFunctions.FinishedKey(CryptoProvider.HashProvider, CipherSuite.HashType, _clientHandshakeTrafficSecret)); }
public unsafe void GenerateMasterSecret(Span <byte> hash) { HkdfFunctions.HkdfExtract(CryptoProvider.HashProvider, CipherSuite.HashType, _secret, _hashSize, null, 0, _masterSecret, _hashSize); HkdfFunctions.ClientServerApplicationTrafficSecret(CryptoProvider.HashProvider, CipherSuite.HashType, _masterSecret, hash, new Span <byte>(_clientApplicationTrafficSecret, _hashSize), new Span <byte>(_serverApplicationTrafficSecret, _hashSize)); }
public unsafe void GenerateHandshakeTrafficSecrets(Span <byte> hash) { HkdfFunctions.ServerHandshakeTrafficSecret(CryptoProvider.HashProvider, CipherSuite.HashType, _secret, hash, new Span <byte>(_serverHandshakeTrafficSecret, _hashSize)); HkdfFunctions.ClientHandshakeTrafficSecret(CryptoProvider.HashProvider, CipherSuite.HashType, _secret, hash, new Span <byte>(_clientHandshakeTrafficSecret, _hashSize)); }