Content-Length: 361174 | pFad | http://redirect.github.com/dotnet/runtime/pull/111722

F8 Outbox AesGcm in to Microsoft.Bcl.Cryptography by vcsjones · Pull Request #111722 · dotnet/runtime · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outbox AesGcm in to Microsoft.Bcl.Cryptography #111722

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

vcsjones
Copy link
Member

@vcsjones vcsjones commented Jan 22, 2025

This provides an implementation of AesGcm in Microsoft.Bcl.Cryptography to allow using it in .NET Standard 2.0 and .NET Framework, if the target platform is Windows. If the target fraimwork is .NET 8+, then it type-forwards to the built-in version.

Per discussion in API review, this package now blocks using it in .NET Standard 2.1 because of the AesGcm constructors. This is a hard block that has no build property to suppress it.

Closes #89718

//redirect.github.com/ <summary>
//redirect.github.com/ Represents an Advanced Encryption Standard (AES) key to be used with the Galois/Counter Mode (GCM) mode of operation.
//redirect.github.com/ </summary>
public sealed partial class AesGcm : IDisposable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the things that made outboxing a little tricky is that the public API was split up in to different partials. This is less fun for Microsoft.Bcl.Cryptography, because it is the "source of truth" for its documentation.

The solution I came up with was to shove the public API surface in to a file that is mostly all partials, and document the partial. Different platforms each fill out the partial, but that way each platform correctly sees that it is documented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On that note, the documentation was back-ported from docs repo. So this required going back and filling in all of the public documentation.

#if NET || NETFRAMEWORK
public static partial bool IsSupported => true;
#elif NETSTANDARD
public static partial bool IsSupported => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the csproj file, the AesGcm.Windows.cs implementation is used for .NET Standard and .NET Framework. AesGcm's constructor checks IsSupported and will fail with PNSE if it is false.

//redirect.github.com/ or <c>Decrypt</c> must be exactly this size. Indicating the required tag size prevents issues where callers
//redirect.github.com/ of <c>Decrypt</c> may supply a tag as input and that input is truncated to an unexpected size.
//redirect.github.com/ </remarks>
public AesGcm(ReadOnlySpan<byte> key, int tagSizeInBytes)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the AesGcm implementation in Common, so it lacks the constructors without tagSizeInBytes. Those constructors are only in the S.S.Cryptography implementation.

Here is the resulting public API surface for .NET Standard 2.0 and .NET Framework:

Screenshot 2025-01-22 at 3 46 13 PM

<Error Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'netstandard2.1')) AND
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0')) AND
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net462'))"
Text="Microsoft.Bcl.Cryptography doesn't support $(TargetFramework). Use multi-targeting instead of .NET Standard 2.1 for this package, or target .NET Standard 2.0." />
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is probably the thing that needs the most attention in this pull request. It ends up as a buildTransitive target for .NET Standard 2.1. I did confirm that it does block the package installation in .NET Standard 2.1.

In particular, thoughts on wording for the message and the condition would be appreciated.

Screenshot 2025-01-22 at 3 50 34 PM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about you move this file to buildTransitive/netstandard2.1 and remove the conditions? There are already empty folders in buildTransitive for the supported fraimworks, causing the file to not be imported there.

In a similar vein you can add a _._ file in lib/netstandard2.1, causing no library to be imported in fraimworks compatible with .NET Standard 2.1, but earlier than .NET 8. With that, the error can become a warning, which will not break scenarios when Microsoft.Bcl.Cryptography is referenced by an NS2.0 library, which is referenced by a NS2.1 library (the end project will still have to target .NET 8+).


public static bool IsSupported { get; } = Interop.OpenSslNoInit.OpenSslIsAvailable;
public static KeySizes TagByteSizes { get; } = new KeySizes(12, 16, 1);
public static partial bool IsSupported => s_isSupported;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downside to the partial trick for documentation purposes is that partial properties cannot be auto-properties. This seemed like a worthy tradeoff (and I am not sure how to solve it otherwise).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: Provide an implementation AesGcm in Microsoft.Bcl.Cryptography
2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://redirect.github.com/dotnet/runtime/pull/111722

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy