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 93e68b0
Show file tree
Hide file tree
Showing 22 changed files with 1,073 additions and 3,803 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/managed.yml
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
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
4 changes: 2 additions & 2 deletions license.txt
@@ -1,4 +1,4 @@
Copyright (c) 2021, Alexandre Mutel
Copyright (c) 2021-2024, Alexandre Mutel
All rights reserved.

Redistribution and use in source and binary forms, with or without modification
Expand All @@ -25,7 +25,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
> Monocypher.NET is a simple managed wrapper around https://github.com/LoupVaillant/Monocypher
> which comes with the following license:

Copyright (c) 2017-2020, Loup Vaillant
Copyright (c) 2017-2024, Loup Vaillant
Copyright (c) 2017-2019, Michael Savage
Copyright (c) 2017-2020, Fabio Scotoni
All rights reserved.
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -4,7 +4,7 @@

Monocypher.NET is a managed wrapper around [Monocypher](https://github.com/LoupVaillant/Monocypher) cryptographic library.

> The current _native_ version of Monocypher used by Monocypher.NET is `3.1.2`
> The current _native_ version of Monocypher used by Monocypher.NET is `4.0.2`
## Features

- Provides the entire native Monocypher API in an efficient 1-to-1 mapping:
Expand Down Expand Up @@ -46,7 +46,7 @@ RNGCryptoServiceProvider.Fill(key);
Span<byte> nonce = stackalloc byte[24];
RNGCryptoServiceProvider.Fill(nonce);

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

// mac contains the authenticated code
// cipherText contains the encrypted message
Expand Down Expand Up @@ -92,7 +92,7 @@ That being said, if you are building an IoT project using the C Monocypher and y

## How to Build?

You need to install the [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0). Then from the root folder:
You need to install the [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0). Then from the root folder:

```console
$ dotnet build src -c Release
Expand Down
6 changes: 3 additions & 3 deletions src/Monocypher.CodeGen/Monocypher.CodeGen.csproj
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
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
@@ -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
18 changes: 9 additions & 9 deletions src/Monocypher.Tests/Tests.cs
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Text;
using NUnit.Framework;
Expand All @@ -13,24 +14,23 @@ 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");
}

[Test]
public void TestChaCha20()
{
Span<byte> ciptherText = stackalloc byte[64];
Span<byte> ciptherText = stackalloc byte[32];
Span<byte> key = new byte[32]
{
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());
Span<byte> expected = new byte[64]
crypto_chacha20_h(ciptherText, key, new Byte16().AsReadOnlySpan());

Span<byte> expected = new byte[32]
{
0x5a, 0x3c, 0x5c, 0xd2, 0x20, 0x8b, 0x75, 0x91, 0x66, 0xbd, 0x25, 0xa8, 0x45, 0xc8, 0xf3, 0xf8, 0x24, 0xe0, 0xdb, 0x9c, 0xfa, 0x1f, 0xb0, 0x14, 0x7d, 0x90, 0xbc, 0xf4, 0xaf, 0x8c, 0xd3, 0x79, 0xf0, 0xbf, 0x31, 0x2b, 0x04,
0xd2, 0xa1, 0xbd, 0x48, 0xd3, 0x50, 0x17, 0xc7, 0x1f, 0x29, 0x52, 0x83, 0x71, 0xba, 0x8c, 0x95, 0xe5, 0xa4, 0x0c, 0x33, 0x59, 0x01, 0x20, 0x65, 0x8c, 0x15, 0x5a
0xf5, 0xc3, 0xeb, 0x70, 0xb2, 0x26, 0x55, 0x5e, 0x34, 0x90, 0xc3, 0x2e, 0xd1, 0x62, 0xd3, 0x8d, 0x83, 0x71, 0xba, 0x8c, 0x95, 0xe5, 0xa4, 0x0c, 0x33, 0x59, 0x01, 0x20, 0x65, 0x8c, 0x15, 0x5a
};

Console.WriteLine(ciptherText.ToHexBytes());
Expand All @@ -54,7 +54,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 +69,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
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
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

0 comments on commit 93e68b0

Please sign in to comment.