Skip to content

Commit

Permalink
Update to Monocypher 4.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Feb 3, 2024
1 parent 380711a commit 1942462
Show file tree
Hide file tree
Showing 11 changed files with 1,064 additions and 3,793 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/managed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: true
fetch-depth: 0

- name: Install .NET 6.0
uses: actions/setup-dotnet@v1
- name: Install .NET 8.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'

- name: Build, Test, Pack, Publish
shell: bash
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v3
with:
submodules: recursive
- name: Setup Linux
Expand Down
2 changes: 1 addition & 1 deletion ext/Monocypher
Submodule Monocypher updated 207 files
6 changes: 3 additions & 3 deletions src/Monocypher.CodeGen/Monocypher.CodeGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<!-- Workaround for issue https://github.com/microsoft/ClangSharp/issues/129 -->
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' AND '$(PackAsTool)' != 'true'">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.42" />
<PackageReference Include="CppAst.CodeGen" Version="0.8.2" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.58" />
<PackageReference Include="CppAst.CodeGen" Version="0.11.3" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Monocypher.CodeGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void GeneratePInvoke()
{
e => e.Map<CppField>("crypto_blake2b_vtable").Discard(),
e => e.Map<CppField>("crypto_sha512_vtable").Discard(),
e => e.Map<CppField>("crypto_argon2_no_extras").Discard(),
}
};
csOptions.Plugins.Insert(0, new FixedArrayTypeConverter());
Expand Down
14 changes: 10 additions & 4 deletions src/Monocypher.Tests/Monocypher.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' AND '$(PackAsTool)' != 'true'">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<Content Include="..\Monocypher\runtimes\$(RuntimeIdentifier)\native\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Using Include="NUnit.Framework" />
<Using Include="NUnit.Framework.Legacy.ClassicAssert" Alias="Assert" />
<Using Include="NUnit.Framework.Legacy.CollectionAssert" Alias="CollectionAssert" />
<Using Include="NUnit.Framework.Legacy.StringAssert" Alias="StringAssert" />
<Using Include="NUnit.Framework.Legacy.DirectoryAssert" Alias="DirectoryAssert" />
<Using Include="NUnit.Framework.Legacy.FileAssert" Alias="FileAssert" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions src/Monocypher.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Text;
using NUnit.Framework;
Expand All @@ -13,7 +14,7 @@ public class Tests
public void SimpleBlake2Init()
{
crypto_blake2b_ctx ctx = default;
crypto_blake2b_init(ref ctx);
crypto_blake2b_init(ref ctx, 256);
Assert.AreNotEqual(0, (int)ctx.hash_size.Value, "Invalid hash_size returned from blake2b_init");
}

Expand All @@ -25,7 +26,7 @@ public void TestChaCha20()
{
0xee, 0xc7, 0xb5, 0x3d, 0x05, 0xd9, 0xcc, 0x81, 0x61, 0x84, 0xc4, 0x9f, 0x65, 0x2f, 0x37, 0x04, 0x70, 0xa4, 0x52, 0x22, 0xa5, 0xc7, 0x4d, 0xa4, 0x2e, 0x7f, 0x09, 0xd6, 0x86, 0x2d, 0x6d, 0xd0,
};
crypto_chacha20(ciptherText, key, new Byte8().AsReadOnlySpan());
crypto_chacha20_h(ciptherText, key, new Byte8().AsReadOnlySpan());

Span<byte> expected = new byte[64]
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public void CryptoLockUnlock()
Span<byte> nonce = stackalloc byte[24];
RandomNumberGenerator.Fill(nonce);

crypto_lock(mac, cipherText, key, nonce, inputText);
crypto_aead_lock(cipherText, mac, key, nonce, ReadOnlySpan<byte>.Empty, inputText);

var builder = new StringBuilder();

Expand All @@ -69,7 +70,7 @@ public void CryptoLockUnlock()

// Verify that we get the same output from unlock
Span<byte> outputText = stackalloc byte[16];
crypto_unlock(outputText, key, nonce, mac, cipherText);
crypto_aead_unlock(outputText, mac, key, nonce, ReadOnlySpan<byte>.Empty, cipherText);

Assert.True(outputText.SequenceEqual(inputText), "crypto_unlock failed. Spans are different");
}
Expand Down
127 changes: 0 additions & 127 deletions src/Monocypher/Monocypher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,133 +508,6 @@ public override string ToString()
}
}

/// <summary>
///
/// These functions provide an interface for the Chacha20 encryption primitive.
/// <br/>
///
/// Chacha20 is a low-level primitive. Consider using authenticated encryption,
/// implemented by <see cref="crypto_lock"/>.
/// <br/>
///
/// This overrides considers plain_text input as if it was composed of all of input zero.
///
/// </summary>
/// <param name="key">A 32-byte secret key.</param>
/// <param name="nonce">A 8-byte buffer. An 8-byte or 24-byte number, used only once with any given key. It does
/// not need to be secret or random, but it does have to be unique. Repeating
/// a nonce with the same key reveals the XOR of two different messages, which
/// allows decryption. 24-byte nonces can be selected at random. 8-byte nonces
/// cannot. They are too small, and the same
/// nonce may be selected twice by accident. See
/// intro(3monocypher) for advice about
/// generating random numbers (use the operating system's random number
/// generator).</param>
/// <param name="cipher_text">The encrypted message.</param>
public static unsafe void crypto_chacha20(Span<byte> cipher_text, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce)
{
ExpectSize32(nameof(key), key.Length);
ExpectSize8(nameof(nonce), nonce.Length);
fixed (void* cipher_text_ptr = cipher_text)
crypto_chacha20(new IntPtr(cipher_text_ptr), IntPtr.Zero, (Monocypher.size_t)cipher_text.Length, in key.AsByte32(), in nonce.AsByte8());
}


/// <summary>
///
/// These functions provide an interface for the Chacha20 encryption primitive.
/// <br/>
///
/// Chacha20 is a low-level primitive. Consider using authenticated encryption,
/// implemented by <see cref="crypto_lock"/>.
/// <br/>
///
/// This overrides considers plain_text input as if it was composed of all of input zero.
///
/// </summary>
/// <param name="key">A 32-byte secret key.</param>
/// <param name="nonce">A 24-byte buffer. An 8-byte or 24-byte number, used only once with any given key. It does
/// not need to be secret or random, but it does have to be unique. Repeating
/// a nonce with the same key reveals the XOR of two different messages, which
/// allows decryption. 24-byte nonces can be selected at random. 8-byte nonces
/// cannot. They are too small, and the same
/// nonce may be selected twice by accident. See
/// intro(3monocypher) for advice about
/// generating random numbers (use the operating system's random number
/// generator).</param>
/// <param name="cipher_text">The encrypted message.</param>
public static unsafe void crypto_xchacha20(Span<byte> cipher_text, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce)
{
ExpectSize32(nameof(key), key.Length);
ExpectSize24(nameof(nonce), nonce.Length);
fixed (void* cipher_text_ptr = cipher_text)
crypto_xchacha20(new IntPtr(cipher_text_ptr), IntPtr.Zero, (Monocypher.size_t)cipher_text.Length, in key.AsByte32(), in nonce.AsByte24());
}

/// <summary>
///
/// These functions provide an interface for the Chacha20 encryption primitive.
/// <br/>
///
/// Chacha20 is a low-level primitive. Consider using authenticated encryption,
/// implemented by <see cref="crypto_lock"/>.
/// <br/>
///
/// This overrides considers plain_text input as if it was composed of all of input zero.
///
/// </summary>
/// <param name="key">A 32-byte secret key.</param>
/// <param name="nonce">A 8-byte buffer. An 8-byte or 24-byte number, used only once with any given key. It does
/// not need to be secret or random, but it does have to be unique. Repeating
/// a nonce with the same key reveals the XOR of two different messages, which
/// allows decryption. 24-byte nonces can be selected at random. 8-byte nonces
/// cannot. They are too small, and the same
/// nonce may be selected twice by accident. See
/// intro(3monocypher) for advice about
/// generating random numbers (use the operating system's random number
/// generator).</param>
/// <param name="cipher_text">The encrypted message.</param>
/// <param name="ctr">The number of 64-byte blocks since the beginning of the stream.</param>
public static unsafe ulong crypto_chacha20_ctr(Span<byte> cipher_text, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ulong ctr)
{
ExpectSize32(nameof(key), key.Length);
ExpectSize8(nameof(nonce), nonce.Length);
fixed (void* cipher_text_ptr = cipher_text)
return crypto_chacha20_ctr(new IntPtr(cipher_text_ptr), IntPtr.Zero, (Monocypher.size_t)cipher_text.Length, in key.AsByte32(), in nonce.AsByte8(), ctr);
}

/// <summary>
///
/// These functions provide an interface for the Chacha20 encryption primitive.
/// <br/>
///
/// Chacha20 is a low-level primitive. Consider using authenticated encryption,
/// implemented by <see cref="crypto_lock"/>.
/// <br/>
///
/// This overrides considers plain_text input as if it was composed of all of input zero.
///
/// </summary>
/// <param name="key">A 32-byte secret key.</param>
/// <param name="nonce">A 24-byte buffer. An 8-byte or 24-byte number, used only once with any given key. It does
/// not need to be secret or random, but it does have to be unique. Repeating
/// a nonce with the same key reveals the XOR of two different messages, which
/// allows decryption. 24-byte nonces can be selected at random. 8-byte nonces
/// cannot. They are too small, and the same
/// nonce may be selected twice by accident. See
/// intro(3monocypher) for advice about
/// generating random numbers (use the operating system's random number
/// generator).</param>
/// <param name="cipher_text">The encrypted message.</param>
/// <param name="ctr">The number of 64-byte blocks since the beginning of the stream.</param>
public static unsafe ulong crypto_xchacha20_ctr(Span<byte> cipher_text, ReadOnlySpan<byte> key, ReadOnlySpan<byte> nonce, ulong ctr)
{
ExpectSize32(nameof(key), key.Length);
ExpectSize24(nameof(nonce), nonce.Length);
fixed (void* cipher_text_ptr = cipher_text)
return crypto_xchacha20_ctr(new IntPtr(cipher_text_ptr), IntPtr.Zero, (Monocypher.size_t)cipher_text.Length, in key.AsByte32(), in nonce.AsByte24(), ctr);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static char ByteHighToHex(byte value) => HexBytes[(value >> 4) & 0x0F];
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
10 changes: 5 additions & 5 deletions src/Monocypher/Monocypher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<ItemGroup>
<None Include="../../img/monocypher_dotnet.png" Pack="true" PackagePath="" />
<None Include="../../readme.md" Pack="true" PackagePath="/"/>
<None Include="../../readme.md" Pack="true" PackagePath="/" />
</ItemGroup>

<ItemGroup>
Expand All @@ -36,18 +36,18 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="3.1.0">
<PackageReference Include="MinVer" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

<!--Add support for sourcelink-->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<Target Name="PatchVersion" AfterTargets="MinVer">
Expand Down
Loading

0 comments on commit 1942462

Please sign in to comment.