-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial skeleton code for C++ Implementation
* Add libcask native dll project to sln * Add stub implementation and P/Invoke interop to it to tests * Revert back to CRLF on Windows, there are too many issues with VS putting snippets with CRLF into files and making them have mixed line endings * Use MSBuild magic so that we 1. Have a good experience in VS on Windows for C++ 2. Don't regress the C# dev experience x-plat for C# This currently works only on Windows x64 and only with full VS, but everything is set up there to the point where you can drop a breakpoint in .cpp and hit it from the Test Explorer. More work will be needed to match this experience for linux, mac, or even VS Code on Windows.
- Loading branch information
Showing
16 changed files
with
592 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
# Use unix line endings always, even on Windows | ||
* text=auto eol=lf | ||
|
||
# Exceptions to above for files that VS saves with CRLF always | ||
*.sln eol=crlf | ||
*.lutconfig eol=crlf | ||
# Normalize line endings to LF in repo, checkout CRLF on Windows | ||
* text=auto | ||
|
||
# Allow comments in JSON in GitHub rendering | ||
*.json linguist-language=JSON-with-Comments |
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
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 |
---|---|---|
@@ -1,9 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>net8.0;net472</TargetFrameworks> | ||
<OSIsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</OSIsWindows> | ||
<OSIsX64 Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">true</OSIsX64> | ||
<MSBuildIsNETFramework Condition="'$(MSBuildRuntimeType)' == 'full'">true</MSBuildIsNETFramework> | ||
<BuildCpp Condition="'$(OSIsWindows)' == 'true' and '$(MSBuildIsNETFramework)' == 'true' and '$(OSIsX64)' == 'true'">true</BuildCpp> | ||
<!-- TODO: Our custom C++ build logic is breaking fast-up-to-date for this project. --> | ||
<DisableFastUpToDateCheck>True</DisableFastUpToDateCheck> | ||
<EnableUnmanagedDebugging>true</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Cask\Cask.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition="'$(BuildCpp)' == 'true'"> | ||
<CppProjectReference Include="..\..\libcask\libcask.vcxproj" Properties="Platform=x64" /> | ||
<Content Include="$(ArtifactsPath)\bin\libcask\$(Configuration.ToLowerInvariant())_x64\libcask.dll" CopyToOutputDirectory="PreserveNewest" Visible="false" /> | ||
</ItemGroup> | ||
|
||
<Target Name="BuildCppProjectReference" BeforeTargets="DispatchToInnerBuilds"> | ||
<MSBuild Projects="@(CppProjectReference)" /> | ||
</Target> | ||
|
||
<Target Name="CleanCppProjectReference" BeforeTargets="Clean" Condition="'$(TargetFramework)' == ''"> | ||
<MSBuild Projects="@(CppProjectReference)" Targets="Clean" /> | ||
</Target> | ||
|
||
</Project> |
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,139 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
using Xunit; | ||
|
||
|
||
using static System.Runtime.InteropServices.UnmanagedType; | ||
|
||
[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)] | ||
|
||
namespace CommonAnnotatedSecurityKeys.Tests; | ||
|
||
// WIP: These tests are disabled because the C++ implementation is stubbed out. | ||
// To enable them, flip the return value of IsSupportedTestClass in | ||
// TestFilter.cs. | ||
|
||
|
||
// CA2101: Specify marshaling for P/Invoke string arguments | ||
// Supppressed due to false positives: https://github.com/dotnet/roslyn-analyzers/issues/7502 | ||
#pragma warning disable CA2101 | ||
|
||
// SYSLIB1054: Use 'LibraryImportAttribute' instead of 'DllImportAttribute' /o | ||
// generate P/Invoke marshalling code at compile time This is very cool but | ||
// would require additional work to switch between LibraryImport and DllImport | ||
// based on target framework. | ||
#pragma warning disable SYSLIB1054 | ||
|
||
public class CppCaskTests : CaskTestsBase | ||
{ | ||
public CppCaskTests() : base(new Implementation()) | ||
{ | ||
} | ||
|
||
private sealed class Implementation : ICask | ||
{ | ||
public bool CompareHash(string candidateHash, | ||
byte[] derivationInput, | ||
string secret, | ||
int secretEntropyInBytes = 32) | ||
{ | ||
return NativeMethods.Cask_CompareHash(candidateHash, derivationInput, derivationInput.Length, secret, secretEntropyInBytes); | ||
} | ||
|
||
public string GenerateHash(byte[] derivationInput, | ||
string secret, | ||
int secretEntropyInBytes = 32) | ||
{ | ||
int size = NativeMethods.Cask_GenerateHash(derivationInput, derivationInput.Length, secret, secretEntropyInBytes, null, 0); | ||
byte[] bytes = new byte[size]; | ||
size = NativeMethods.Cask_GenerateHash(derivationInput, derivationInput.Length, secret, secretEntropyInBytes, bytes, size); | ||
Assert.True(size == bytes.Length, "Cask_GenerateKey did not use as many bytes as it said it would."); | ||
return Encoding.UTF8.GetString(bytes, 0, size - 1); // - 1 to remove null terminator | ||
} | ||
|
||
public string GenerateKey(string providerSignature, | ||
string allocatorCode, | ||
string? reserved = null, | ||
int secretEntropyInBytes = 32) | ||
{ | ||
int size = NativeMethods.Cask_GenerateKey(providerSignature, allocatorCode, reserved, secretEntropyInBytes, null, 0); | ||
byte[] bytes = new byte[size]; | ||
size = NativeMethods.Cask_GenerateKey(providerSignature, allocatorCode, reserved, secretEntropyInBytes, bytes, size); | ||
Assert.True(size == bytes.Length, "Cask_GenerateKey did not use as many bytes as it said it would."); | ||
return Encoding.UTF8.GetString(bytes, 0, size - 1); // -1 to remove null terminator | ||
} | ||
|
||
public bool IsCask(string keyOrHash) | ||
{ | ||
return NativeMethods.Cask_IsCask(keyOrHash); | ||
} | ||
|
||
public bool IsCaskBytes(byte[] bytes) | ||
{ | ||
return NativeMethods.Cask_IsCaskBytes(bytes, bytes.Length); | ||
} | ||
|
||
Mock ICask.MockFillRandom(FillRandomAction fillRandom) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
Mock ICask.MockUtcNow(UtcNowFunc getUtcNow) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private static class NativeMethods | ||
{ | ||
[DllImport("libcask")] | ||
[return: MarshalAs(I1)] | ||
public static extern bool Cask_IsCask( | ||
[MarshalAs(LPUTF8Str)] string keyOrHash); | ||
|
||
[DllImport("libcask")] | ||
[return: MarshalAs(I1)] | ||
public static extern bool Cask_IsCaskBytes( | ||
byte[] keyOrHash, | ||
int length); | ||
|
||
[DllImport("libcask")] | ||
[return: MarshalAs(I1)] | ||
public static extern bool Cask_CompareHash( | ||
[MarshalAs(LPUTF8Str)] | ||
string candidateHash, | ||
byte[] derivationInput, | ||
int derivationInputLength, | ||
[MarshalAs(LPUTF8Str)] string secret, | ||
int secretEntropyInBytes); | ||
|
||
[DllImport("libcask")] | ||
public static extern int Cask_GenerateKey( | ||
[MarshalAs(LPUTF8Str)] | ||
string providerSignature, | ||
[MarshalAs(LPUTF8Str)] | ||
string allocatorCode, | ||
[MarshalAs(LPUTF8Str)] | ||
string? providerData, | ||
int secretEntropyInBytes, | ||
byte[]? output, | ||
int outputCapacity); | ||
|
||
[DllImport("libcask")] | ||
public static extern int Cask_GenerateHash( | ||
byte[] derivationInput, | ||
int derivationInputLength, | ||
[MarshalAs(LPUTF8Str)] | ||
string secret, | ||
int secretEntropyInBytes, | ||
byte[]? output, | ||
int outputCapacity); | ||
} | ||
} | ||
} | ||
|
||
#pragma warning restore CA2101 | ||
#pragma warning restore SYSLIB1054 |
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,8 @@ | ||
{ | ||
"profiles": { | ||
"Cask.Tests": { | ||
"commandName": "Project", | ||
"nativeDebugging": true | ||
} | ||
} | ||
} |
Oops, something went wrong.