Skip to content

Commit

Permalink
Merge pull request #144 from dorssel/span
Browse files Browse the repository at this point in the history
Prepare for Span
  • Loading branch information
dorssel authored Jan 8, 2025
2 parents d3fffb5 + d060188 commit 3467710
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/linters/vs-spell-exclusion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ dotnet
Encryptor
inliner
IntelliSense
netstandard
NIST
xorend
8 changes: 6 additions & 2 deletions AesExtra/AesExtra.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2022 Frans van Dorsselaer
Expand All @@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<RootNamespace>Dorssel.Security.Cryptography</RootNamespace>
<AssemblyName>Dorssel.Security.Cryptography.AesExtra</AssemblyName>

Expand All @@ -21,4 +21,8 @@ SPDX-License-Identifier: MIT
<None Include="..\README.md" Pack="true" PackagePath="" Visible="false" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Bcl.Memory" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion AesExtra/AesSiv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ byte[] S2V(byte[][] associatedData, byte[] plaintext)
_ = Cmac.TransformBlock(plaintext, 0, plaintext.Length - BLOCKSIZE, null, 0);
_ = Cmac.TransformBlock(D, 0, BLOCKSIZE, null, 0);
_ = Cmac.TransformFinalBlock([], 0, 0);
return Cmac.Hash;
return Cmac.Hash!;
}
else
{
Expand Down
10 changes: 7 additions & 3 deletions AesExtra/CryptographicOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
//
// SPDX-License-Identifier: MIT

#if NETSTANDARD2_0

using System.Runtime.CompilerServices;

namespace Dorssel.Security.Cryptography;

/// <summary>
/// This is a backport of .NET 9. Since this library is for .NET Standard 2.0 it uses <see cref="byte"/>[] instead of Span.
/// This is a backport of .NET 9.
/// <para/>
/// See:
/// <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptographicOperations.cs"/>
/// </summary>
static class CryptographicOperations
{
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static bool FixedTimeEquals(byte[] left, byte[] right)
public static bool FixedTimeEquals(ReadOnlySpan<byte> left, ReadOnlySpan<byte> right)
{
// NoOptimization because we want this method to be exactly as non-short-circuiting
// as written.
Expand All @@ -40,7 +42,7 @@ public static bool FixedTimeEquals(byte[] left, byte[] right)
}

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void ZeroMemory(byte[] buffer)
public static void ZeroMemory(Span<byte> buffer)
{
// NoOptimize to prevent the optimizer from deciding this call is unnecessary
// NoInlining to prevent the inliner from forgetting that the method was no-optimize
Expand All @@ -50,3 +52,5 @@ public static void ZeroMemory(byte[] buffer)
}
}
}

#endif
2 changes: 2 additions & 0 deletions AesExtra/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@

[assembly: SuppressMessage("Security", "CA5358:Review cipher mode usage with cryptography experts", Justification = "Done :)")]
[assembly: SuppressMessage("Security", "CA5401:Do not use CreateEncryptor with non-default IV", Justification = "We only use ECB, which does not use an IV")] // DevSkim: ignore DS187371
[assembly: SuppressMessage("Maintainability", "CA1512:Use ArgumentOutOfRangeException throw helper", Justification = "Not available in netstandard2.0.")]
[assembly: SuppressMessage("Maintainability", "CA1513:Use ObjectDisposedException throw helper", Justification = "Not available in netstandard2.0.")]
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ SPDX-License-Identifier: MIT
<ItemGroup>
<!-- all -->
<PackageVersion Include="Dorssel.GitVersion.MsBuild" Version="1.1.0" />
<!-- netstandard2.0 -->
<PackageVersion Include="Microsoft.Bcl.Memory" Version="9.0.0" />
<!-- UnitTests -->
<PackageVersion Include="Moq" Version="4.20.72" />
</ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ SPDX-License-Identifier: MIT
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AesExtra\AesExtra.csproj" />
<ProjectReference Include="..\AesExtra\AesExtra.csproj">
<SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
</ProjectReference>
</ItemGroup>

</Project>

0 comments on commit 3467710

Please sign in to comment.