aboutsummaryrefslogtreecommitdiff
path: root/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NCSignatureUtil.cs
blob: 2755cebe0f9805a7647735d7c9af39c6d3c0e435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (C) 2024 Vaughn Nugent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

using System;
using System.Runtime.InteropServices;

using VNLib.Utils.Extensions;
using VNLib.Utils.Cryptography.Noscrypt.@internal;
using static VNLib.Utils.Cryptography.Noscrypt.NoscryptLibrary;

using NCResult = System.Int64;

namespace VNLib.Utils.Cryptography.Noscrypt.Singatures
{

    /// <summary>
    /// Contains utility methods for signing and verifying data using the noscrypt library
    /// </summary>
    public unsafe static class NCSignatureUtil
    {
        /// <summary>
        /// Signs the data using the supplied secret key and 
        /// entropy pointer
        /// </summary>
        /// <param name="context">The initialized context memory to pass to the library</param>
        /// <param name="secretKey">A reference to a structure containing the private key data</param>
        /// <param name="random32">A pointer to a 32 byte buffer containing high entropy random data</param>
        /// <param name="data">A pointer to a buffer containing the data to sign</param>
        /// <param name="dataSize">The size of the data buffer in bytes</param>
        /// <param name="sig64">A pointer to a 64 byte buffer to write signature data to</param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void SignData(
            NCContext context,
            ref readonly NCSecretKey secretKey,
            ref readonly byte random32,
            ref readonly byte data,
            uint dataSize,
            ref byte sig64
        )
        {
            Check(context);

            fixed (NCSecretKey* pSecKey = &secretKey)
            fixed (byte* pData = &data, pSig = &sig64, pRandom = &random32)
            {
                NCResult result = GetTable(context).NCSignData(
                    ctx: context.DangerousGetHandle(),
                    sk: pSecKey,
                    random32: pRandom,
                    data: pData,
                    dataSize,
                    sig64: pSig
                );

                NCUtil.CheckResult<FunctionTable.NCSignDataDelegate>(result, raiseOnFailure: true);
            }
        }

        /// <summary>
        /// Verifies signed data against the supplied public key
        /// </summary>
        /// <param name="context">The initialized context memory to pass to the library</param>
        /// <param name="publicKey">A reference to a structure containing the public key data</param>
        /// <param name="data">A pointer to a buffer containing the data to verify</param>
        /// <param name="dataSize">The size of the data buffer in bytes</param>
        /// <param name="sig64">A pointer to a 64 byte buffer to read signature data from</param>
        /// <returns>True if the signature was signed by the supplied public key, false otherwise</returns>
        public static bool VerifyData(
            NCContext context,
            ref readonly NCPublicKey publicKey,
            ref readonly byte data,
            uint dataSize,
            ref readonly byte sig64
        )
        {
            Check(context);

            fixed (NCPublicKey* pPubKey = &publicKey)
            fixed (byte* pData = &data, pSig = &sig64)
            {
                NCResult result = GetTable(context).NCVerifyData(
                    context.DangerousGetHandle(),
                    pk: pPubKey,
                    data: pData,
                    dataSize,
                    sig64: pSig
                );

                NCUtil.CheckResult<FunctionTable.NCVerifyDataDelegate>(result, false);

                return result == NC_SUCCESS;
            }
        }

        /// <summary>
        /// Signs the data using the supplied secret key and 
        /// entropy pointer
        /// </summary>
        /// <param name="context">The initialized context memory to pass to the library</param>
        /// <param name="secretKey">A reference to a structure containing the private key data</param>
        /// <param name="random32">A pointer to a 32 byte buffer containing high entropy random data</param>
        /// <param name="data">A pointer to a buffer containing the data to sign</param>
        /// <param name="signatureBuffer">A pointer to a 64 byte buffer to write signature data to</param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void SignData(
            NCContext context,
            ref readonly NCSecretKey secretKey,
            ReadOnlySpan<byte> random32,
            ReadOnlySpan<byte> data,
            Span<byte> signatureBuffer
        )
        {
            ArgumentOutOfRangeException.ThrowIfLessThan(signatureBuffer.Length, NC_SIGNATURE_SIZE, nameof(signatureBuffer));
            ArgumentOutOfRangeException.ThrowIfLessThan(random32.Length, 32, nameof(random32));
            ArgumentOutOfRangeException.ThrowIfZero(data.Length, nameof(data));

            SignData(
                context,
                secretKey: in secretKey,
                random32: in MemoryMarshal.GetReference(random32),
                data: in MemoryMarshal.GetReference(data),
                dataSize: (uint)data.Length,
                sig64: ref MemoryMarshal.GetReference(signatureBuffer)
            );
        }

        /// <summary>
        /// Verifies signed data against the supplied public key
        /// </summary>
        /// <param name="context">The initialized context memory to pass to the library</param>
        /// <param name="publicKey">A reference to a structure containing the public key data</param>
        /// <param name="data">A pointer to a buffer containing the data to verify</param>
        /// <param name="signatureBuffer">A pointer to a 64 byte buffer to read signature data from</param>
        /// <returns>True if the signature was signed by the supplied public key, false otherwise</returns>
        public static bool VerifyData(
            NCContext context,
            ref readonly NCPublicKey publicKey,
            ReadOnlySpan<byte> data,
            ReadOnlySpan<byte> signatureBuffer
        )
        {
            ArgumentOutOfRangeException.ThrowIfLessThan(signatureBuffer.Length, NC_SIGNATURE_SIZE, nameof(signatureBuffer));
            ArgumentOutOfRangeException.ThrowIfZero(data.Length, nameof(data));

            return VerifyData(
                context,
                publicKey: in publicKey,
                data: in MemoryMarshal.GetReference(data),
                dataSize: (uint)data.Length,
                sig64: ref MemoryMarshal.GetReference(signatureBuffer)
            );
        }

        private static void Check(NCContext? context)
        {
            ArgumentNullException.ThrowIfNull(context);
            context.ThrowIfClosed();
        }

        private static ref readonly FunctionTable GetTable(NCContext ctx)
            => ref ctx.Library.Functions;
    }
}