From 34b4da8b6a3b86d6af80180e88093a506796836b Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Wed, 13 Jan 2021 00:39:21 -0800 Subject: [PATCH] Add ExpandSecret variant that takes label as a byte span --- Hazel/Dtls/PrfSha256.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Hazel/Dtls/PrfSha256.cs b/Hazel/Dtls/PrfSha256.cs index 683e55f..276169a 100644 --- a/Hazel/Dtls/PrfSha256.cs +++ b/Hazel/Dtls/PrfSha256.cs @@ -17,14 +17,24 @@ namespace Hazel.Dtls /// Seed for expansion (treated as a salt) public static void ExpandSecret(ByteSpan output, ByteSpan key, string label, ByteSpan initialSeed) { - ByteSpan writer = output; - - ///TODO(mendsley): Look into pre-calculating all of these ByteSpan labelAsBytes = Encoding.ASCII.GetBytes(label); + ExpandSecret(output, key, labelAsBytes, initialSeed); + } + + /// + /// Expand a secret key + /// + /// Output span. Length determines how much data to generate + /// Original key to expand + /// Label (treated as a salt) + /// Seed for expansion (treated as a salt) + public static void ExpandSecret(ByteSpan output, ByteSpan key, ByteSpan label, ByteSpan initialSeed) + { + ByteSpan writer = output; - byte[] roundSeed = new byte[labelAsBytes.Length + initialSeed.Length]; - labelAsBytes.CopyTo(roundSeed); - initialSeed.CopyTo(roundSeed, labelAsBytes.Length)); + byte[] roundSeed = new byte[label.Length + initialSeed.Length]; + label.CopyTo(roundSeed); + initialSeed.CopyTo(roundSeed, label.Length)); byte[] hashA = roundSeed; -- 2.39.5