-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/9.0] Use OpenSSL 3's HKDF if it is available (#107085)
* Split out managed implementation * Misc. cleanup * Introduce a PAL * Start a Unix implementation * Get DeriveKey working with OpenSSL * Extract and Expand individual calls * Fix build * Fix tests on Azure Linux * Use defined consts rather than magic strings --------- Co-authored-by: Kevin Jones <kevin@vcsjones.com>
- Loading branch information
1 parent
7cf8f4a
commit 996ce3b
Showing
14 changed files
with
633 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HKDF.Managed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Security.Cryptography | ||
{ | ||
public static partial class HKDF | ||
{ | ||
private static void Extract( | ||
HashAlgorithmName hashAlgorithmName, | ||
int hashLength, | ||
ReadOnlySpan<byte> ikm, | ||
ReadOnlySpan<byte> salt, | ||
Span<byte> prk) | ||
{ | ||
HKDFManagedImplementation.Extract(hashAlgorithmName, hashLength, ikm, salt, prk); | ||
} | ||
|
||
private static void Expand( | ||
HashAlgorithmName hashAlgorithmName, | ||
int hashLength, | ||
ReadOnlySpan<byte> prk, | ||
Span<byte> output, | ||
ReadOnlySpan<byte> info) | ||
{ | ||
HKDFManagedImplementation.Expand(hashAlgorithmName, hashLength, prk, output, info); | ||
} | ||
|
||
private static void DeriveKeyCore( | ||
HashAlgorithmName hashAlgorithmName, | ||
int hashLength, | ||
ReadOnlySpan<byte> ikm, | ||
Span<byte> output, | ||
ReadOnlySpan<byte> salt, | ||
ReadOnlySpan<byte> info) | ||
{ | ||
HKDFManagedImplementation.DeriveKey(hashAlgorithmName, hashLength, ikm, output, salt, info); | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HKDF.OpenSsl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
|
||
namespace System.Security.Cryptography | ||
{ | ||
public static partial class HKDF | ||
{ | ||
private static readonly bool s_hasOpenSslImplementation = Interop.Crypto.EvpKdfAlgs.Hkdf is not null; | ||
|
||
private static void Extract( | ||
HashAlgorithmName hashAlgorithmName, | ||
int hashLength, | ||
ReadOnlySpan<byte> ikm, | ||
ReadOnlySpan<byte> salt, | ||
Span<byte> prk) | ||
{ | ||
if (s_hasOpenSslImplementation) | ||
{ | ||
Debug.Assert(Interop.Crypto.EvpKdfAlgs.Hkdf is not null); | ||
Debug.Assert(hashAlgorithmName.Name is not null); | ||
|
||
Interop.Crypto.HkdfExtract(Interop.Crypto.EvpKdfAlgs.Hkdf, ikm, hashAlgorithmName.Name, salt, prk); | ||
} | ||
else | ||
{ | ||
HKDFManagedImplementation.Extract(hashAlgorithmName, hashLength, ikm, salt, prk); | ||
} | ||
} | ||
|
||
private static void Expand( | ||
HashAlgorithmName hashAlgorithmName, | ||
int hashLength, | ||
ReadOnlySpan<byte> prk, | ||
Span<byte> output, | ||
ReadOnlySpan<byte> info) | ||
{ | ||
if (s_hasOpenSslImplementation) | ||
{ | ||
Debug.Assert(Interop.Crypto.EvpKdfAlgs.Hkdf is not null); | ||
Debug.Assert(hashAlgorithmName.Name is not null); | ||
|
||
Interop.Crypto.HkdfExpand(Interop.Crypto.EvpKdfAlgs.Hkdf, prk, hashAlgorithmName.Name, info, output); | ||
} | ||
else | ||
{ | ||
HKDFManagedImplementation.Expand(hashAlgorithmName, hashLength, prk, output, info); | ||
} | ||
} | ||
|
||
private static void DeriveKeyCore( | ||
HashAlgorithmName hashAlgorithmName, | ||
int hashLength, | ||
ReadOnlySpan<byte> ikm, | ||
Span<byte> output, | ||
ReadOnlySpan<byte> salt, | ||
ReadOnlySpan<byte> info) | ||
{ | ||
if (s_hasOpenSslImplementation) | ||
{ | ||
Debug.Assert(Interop.Crypto.EvpKdfAlgs.Hkdf is not null); | ||
Debug.Assert(hashAlgorithmName.Name is not null); | ||
|
||
Interop.Crypto.HkdfDeriveKey( | ||
Interop.Crypto.EvpKdfAlgs.Hkdf, | ||
ikm, | ||
hashAlgorithmName.Name, | ||
salt, | ||
info, | ||
output); | ||
} | ||
else | ||
{ | ||
HKDFManagedImplementation.DeriveKey(hashAlgorithmName, hashLength, ikm, output, salt, info); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.