aboutsummaryrefslogtreecommitdiff
path: root/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/tests/LibNoscryptTests.cs
blob: b8d96236eaf8d2296745b10609ccb5e776a5c15e (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using Microsoft.VisualStudio.TestTools.UnitTesting;

using VNLib.Hashing;
using VNLib.Utils.Memory;
using VNLib.Utils.Cryptography.Noscrypt.Random;
using VNLib.Utils.Cryptography.Noscrypt.Singatures;

namespace VNLib.Utils.Cryptography.Noscrypt.Tests
{
    [TestClass()]
    public class LibNoscryptTests : IDisposable
    {
        //Keys generated using npx noskey package
        const string TestPrivateKeyHex = "98c642360e7163a66cee5d9a842b252345b6f3f3e21bd3b7635d5e6c20c7ea36";
        const string TestPublicKeyHex = "0db15182c4ad3418b4fbab75304be7ade9cfa430a21c1c5320c9298f54ea5406";

        const string TestPrivateKeyHex2 = "3032cb8da355f9e72c9a94bbabae80ca99d3a38de1aed094b432a9fe3432e1f2";
        const string TestPublicKeyHex2 = "421181660af5d39eb95e48a0a66c41ae393ba94ffeca94703ef81afbed724e5a";

#nullable disable
        private NoscryptLibrary _testLib;
#nullable enable

        [TestInitialize]
        public void Initialize()
        {
            _testLib = NoscryptLibrary.LoadDefault();
        }

        [TestMethod()]
        public void InitializeTest()
        {
            //Init new context and interface
            using NCContext context = _testLib.Initialize(MemoryUtil.Shared, NCFallbackRandom.Shared);
        }

        [TestMethod()]
        public void ValidateSecretKeyTest()
        {
            //Random context seed
            ReadOnlySpan<byte> secretKey = RandomHash.GetRandomBytes(32);
            Span<byte> publicKey = stackalloc byte[32];

            using NCContext context = _testLib.Initialize(MemoryUtil.Shared, NCFallbackRandom.Shared);

            //validate the secret key
            Assert.IsTrue(NCKeyUtil.ValidateSecretKey(context, in NCKeyUtil.AsSecretKey(secretKey)));

            //Generate the public key
            NCKeyUtil.GetPublicKey(
                context,
                in NCKeyUtil.AsSecretKey(secretKey),
                ref NCKeyUtil.AsPublicKey(publicKey)
            );

            //Make sure the does not contain all zeros
            Assert.IsTrue(publicKey.ToArray().Any(b => b != 0));
        }

        [TestMethod()]
        public void TestGetPublicKey()
        {
            using NCContext context = _testLib.Initialize(MemoryUtil.Shared, NCFallbackRandom.Shared);

            //Test known key 1
            TestKnownKeys(
                context,
                Convert.FromHexString(TestPrivateKeyHex),
                Convert.FromHexString(TestPublicKeyHex)
            );

            //Test known key 2
            TestKnownKeys(
                context,
                Convert.FromHexString(TestPrivateKeyHex2),
                Convert.FromHexString(TestPublicKeyHex2)
            );


            static void TestKnownKeys(NCContext context, ReadOnlySpan<byte> knownSec, ReadOnlySpan<byte> kownPub)
            {
                NCPublicKey pubKey;

                //Invoke test function
                NCKeyUtil.GetPublicKey(
                    context,
                    in NCKeyUtil.AsSecretKey(knownSec),
                    ref pubKey
                );

                //Make sure known key matches the generated key
                Assert.IsTrue(pubKey.AsSpan().SequenceEqual(kownPub));
            }
        }

        //Test argument validations
        [TestMethod()]
        public void TestPublicApiArgValidations()
        {
            using NCContext context = _testLib.Initialize(MemoryUtil.Shared, NCFallbackRandom.Shared);

            byte[] bin16 = new byte[16];
            byte[] bin32 = new byte[32];
            byte[] bin64 = new byte[64];
            NCSecretKey secKey = default;
            NCPublicKey pubKey = default;

            //noThrow (its a bad sec key but it should not throw)
            NCKeyUtil.ValidateSecretKey(context, in secKey);
            Assert.ThrowsException<ArgumentNullException>(() => NCKeyUtil.ValidateSecretKey(null!, ref NCSecretKey.NullRef));
            Assert.ThrowsException<ArgumentNullException>(() => NCKeyUtil.ValidateSecretKey(context, ref NCSecretKey.NullRef));

            //public key
            Assert.ThrowsException<ArgumentNullException>(() => NCKeyUtil.GetPublicKey(null!, in secKey, ref pubKey));
            Assert.ThrowsException<ArgumentNullException>(() => NCKeyUtil.GetPublicKey(context, ref NCSecretKey.NullRef, ref pubKey));
            Assert.ThrowsException<ArgumentNullException>(() => NCKeyUtil.GetPublicKey(context, in secKey, ref NCPublicKey.NullRef));

            /*
             *       VERIFY DATA
             */
            //Null context 
            Assert.ThrowsException<ArgumentNullException>(() =>
                NCSignatureUtil.VerifyData(null!, ref pubKey, bin32, bin64)
            );

            //Null pubkey
            Assert.ThrowsException<ArgumentNullException>(() =>
                NCSignatureUtil.VerifyData(context, ref NCPublicKey.NullRef, bin32, bin64)
            );

            //No data buffer
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.VerifyData(context, ref pubKey, [], bin64)
            );

            //No signature
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.VerifyData(context, ref pubKey, bin32, [])
            );

            //Signature too small
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
               NCSignatureUtil.VerifyData(context, ref pubKey, bin32, bin32)
           );

            /*
             *      SIGN DATA
             */

            //Null context
            Assert.ThrowsException<ArgumentNullException>(() =>
                NCSignatureUtil.SignData(null!, ref secKey, bin32, bin32, bin64)
            );

            //Null secret key
            Assert.ThrowsException<ArgumentNullException>(() =>
                NCSignatureUtil.SignData(context, ref NCSecretKey.NullRef, bin32, bin32, bin64)
            );

            //No entropy
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.SignData(context, ref secKey, [], bin32, bin64)
            );

            //No data
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.SignData(context, ref secKey, bin32, [], bin64)
            );

            //No signature
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.SignData(context, ref secKey, bin32, bin32, [])
            );

            //Signature too small
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.SignData(context, ref secKey, bin32, bin32, bin32)
            );  

            //Entropy too small
            Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
                NCSignatureUtil.SignData(context, ref secKey, bin16, bin32, bin32)
            );

            /*
             * Cipher api
             */

            NoscryptSigner signer = new(context, NCFallbackRandom.Shared);

          
        }

        void IDisposable.Dispose()
        {
            _testLib.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}