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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* Copyright (c) 2024 Vaughn Nugent
*
* Package: noscrypt
* File: test.c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with NativeHeapApi. If not, see http://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <noscrypt.h>
#include <mbedtls/sha256.h>
#include <mbedtls/platform_util.h>
#ifdef _NC_IS_WINDOWS
#define IS_WINDOWS
#endif
#ifdef IS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wincrypt.h>
#endif
#ifdef IS_WINDOWS
/*Asserts that an internal test condition is true, otherwise aborts the test process*/
#define TASSERT(x) if(!(x)) { printf("ERROR! Internal test assumption failed: %s.\n Aborting tests...\n", #x); ExitProcess(1); }
#else
/*Asserts that an internal test condition is true, otherwise aborts the test process*/
#define TASSERT(x) if(!(x)) { printf("Internal assumption failed: %s\n", #x); exit(1); }
#endif
/*Prints a string literal to the console*/
#define PRINTL(x) printf(x); printf("\n");
#define ENSURE(x) if(!(x)) { printf("Assumption failed!\n"); return 1; }
#define TEST(x, expected) printf("\tTesting %s\n", #x); if(((long)x) != ((long)expected)) \
{ printf("FAILED: Expected %ld but got %ld @ callsite %s. Line: %d \n", ((long)expected), ((long)x), #x, __LINE__); return 1; }
#ifdef IS_WINDOWS
#define ZERO_FILL(x, size) SecureZeroMemory(x, size)
#else
#define ZERO_FILL(x, size) memset(x, 0, size)
#endif
//Pre-computed constants for argument errors
#define ARG_ERROR_POS_0 E_NULL_PTR
#define ARG_ERROR_POS_1 NCResultWithArgPosition(E_NULL_PTR, 0x01)
#define ARG_ERROR_POS_2 NCResultWithArgPosition(E_NULL_PTR, 0x02)
#define ARG_ERROR_POS_3 NCResultWithArgPosition(E_NULL_PTR, 0x03)
#define ARG_ERROR_POS_4 NCResultWithArgPosition(E_NULL_PTR, 0x04)
#define ARG_ERROR_POS_5 NCResultWithArgPosition(E_NULL_PTR, 0x05)
#define ARG_ERROR_POS_6 NCResultWithArgPosition(E_NULL_PTR, 0x06)
#define ARG_RAMGE_ERROR_POS_0 E_ARGUMENT_OUT_OF_RANGE
#define ARG_RAMGE_ERROR_POS_1 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x01)
#define ARG_RAMGE_ERROR_POS_2 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x02)
#define ARG_RAMGE_ERROR_POS_3 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x03)
#define ARG_RAMGE_ERROR_POS_4 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x04)
#define ARG_RAMGE_ERROR_POS_5 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x05)
#define ARG_RAMGE_ERROR_POS_6 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x06)
#define ARG_INVALID_ERROR_POS_0 E_INVALID_ARG
#define ARG_INVALID_ERROR_POS_1 NCResultWithArgPosition(E_INVALID_ARG, 0x01)
#define ARG_INVALID_ERROR_POS_2 NCResultWithArgPosition(E_INVALID_ARG, 0x02)
#define ARG_INVALID_ERROR_POS_3 NCResultWithArgPosition(E_INVALID_ARG, 0x03)
#define ARG_INVALID_ERROR_POS_4 NCResultWithArgPosition(E_INVALID_ARG, 0x04)
#define ARG_INVALID_ERROR_POS_5 NCResultWithArgPosition(E_INVALID_ARG, 0x05)
#define ARG_INVALID_ERROR_POS_6 NCResultWithArgPosition(E_INVALID_ARG, 0x06)
static void FillRandomData(void* pbBuffer, size_t length);
static int TestEcdsa(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKey);
static int InitKepair(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKey);
#ifndef NC_INPUT_VALIDATION_OFF
static int TestPublicApiArgumentValidation(void);
#endif
static const uint8_t zero32[32] = { 0 };
static const uint8_t zero64[64] = { 0 };
int main(void)
{
NCContext ctx;
uint8_t ctxRandom[32];
NCSecretKey secKey;
NCPublicKey pubKey;
PRINTL("Begining basic noscrypt tests\n")
FillRandomData(ctxRandom, 32);
//Context struct size should aways match the size of the struct returned by NCGetContextStructSize
TEST(NCGetContextStructSize(), sizeof(NCContext))
TEST(NCInitContext(&ctx, ctxRandom), NC_SUCCESS)
if (InitKepair(&ctx, &secKey, &pubKey) != 0)
{
return 1;
}
if (TestEcdsa(&ctx, &secKey, &pubKey) != 0)
{
return 1;
}
#ifndef NC_INPUT_VALIDATION_OFF
if(TestPublicApiArgumentValidation() != 0)
{
return 1;
}
#endif
PRINTL("ECDSA tests passed\n")
TEST(NCDestroyContext(&ctx), NC_SUCCESS)
return 0;
}
static void _sha256(const uint8_t* data, size_t length, uint8_t digest[32])
{
mbedtls_sha256_context sha256;
mbedtls_sha256_init(&sha256);
TASSERT(0 == mbedtls_sha256_starts(&sha256, 0))
TASSERT(0 == mbedtls_sha256_update(&sha256, data, length))
TASSERT(0 == mbedtls_sha256_finish(&sha256, digest))
mbedtls_sha256_free(&sha256);
}
static const char* message = "Test message to sign";
static int InitKepair(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKey)
{
PRINTL("TEST: Keypair\n")
//Get random private key
FillRandomData(secKey, sizeof(NCSecretKey));
//Ensure not empty
ENSURE(memcmp(zero32, secKey, 32) != 0);
//Ensure the key is valid, result should be 1 on success
TEST(NCValidateSecretKey(context, secKey), 1);
//Generate a public key from the secret key
TEST(NCGetPublicKey(context, secKey, pubKey), NC_SUCCESS);
PRINTL("\nPASSED: Keypair tests completed\n")
return 0;
}
static int TestEcdsa(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKey)
{
uint8_t digestToSign[32];
uint8_t sigEntropy[32];
uint8_t invalidSig[64];
PRINTL("TEST: Ecdsa\n")
//Init a new secret key with random data
FillRandomData(invalidSig, sizeof(invalidSig));
FillRandomData(sigEntropy, sizeof(sigEntropy));
//compute sha256 of the test string
_sha256((uint8_t*)message, strlen(message), digestToSign);
//Sign and verify sig64
{
uint8_t sig[64];
TEST(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig), NC_SUCCESS);
TEST(NCVerifyDigest(context, pubKey, digestToSign, sig), NC_SUCCESS);
}
//Sign and verify raw data
{
uint8_t sig[64];
TEST(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen(message), sig), NC_SUCCESS);
TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen(message), sig), NC_SUCCESS);
}
//ensure the signature is the same for signing data and sig64
{
uint8_t sig1[64];
uint8_t sig2[64];
//Ensure operations succeed but dont print them as test cases
ENSURE(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen(message), sig1) == NC_SUCCESS);
ENSURE(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig2) == NC_SUCCESS);
//Perform test
TEST(memcmp(sig1, sig2, 64), 0);
}
//Try signing data then veriyfing the sig64
{
uint8_t sig[64];
ENSURE(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen(message), sig) == NC_SUCCESS);
TEST(NCVerifyDigest(context, pubKey, digestToSign, sig), NC_SUCCESS);
//Now invert test, zero signature to ensure its overwritten
ZERO_FILL(sig, sizeof(sig));
ENSURE(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig) == NC_SUCCESS);
TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen(message), sig), NC_SUCCESS);
}
//test verification of invalid signature
{
TEST(NCVerifyDigest(context, pubKey, digestToSign, invalidSig), E_INVALID_ARG);
}
PRINTL("\nPASSED: Ecdsa tests completed\n")
return 0;
}
#ifndef NC_INPUT_VALIDATION_OFF
static int TestPublicApiArgumentValidation(void)
{
NCContext ctx;
uint8_t ctxRandom[32];
uint8_t sig64[64];
NCSecretKey secKey;
NCPublicKey pubKey;
NCCryptoData cryptoData;
uint8_t hmacKeyOut[NC_HMAC_KEY_SIZE];
PRINTL("TEST: Public API argument validation tests\n")
FillRandomData(ctxRandom, 32);
//Test null context
TEST(NCInitContext(NULL, ctxRandom), ARG_ERROR_POS_0)
TEST(NCInitContext(&ctx, NULL), ARG_ERROR_POS_1)
//Test null context
TEST(NCDestroyContext(NULL), ARG_ERROR_POS_0)
//reinit
TEST(NCReInitContext(NULL, ctxRandom), ARG_ERROR_POS_0)
TEST(NCReInitContext(&ctx, NULL), ARG_ERROR_POS_1)
//Test null secret key
TEST(NCGetPublicKey(&ctx, NULL, &pubKey), ARG_ERROR_POS_1)
TEST(NCGetPublicKey(&ctx, &secKey, NULL), ARG_ERROR_POS_2)
//Test null secret key
TEST(NCValidateSecretKey(NULL, &secKey), ARG_ERROR_POS_0)
TEST(NCValidateSecretKey(&ctx, NULL), ARG_ERROR_POS_1)
//Verify sig64 args test
TEST(NCVerifyDigest(NULL, &pubKey, zero32, sig64), ARG_ERROR_POS_0)
TEST(NCVerifyDigest(&ctx, NULL, zero32, sig64), ARG_ERROR_POS_1)
TEST(NCVerifyDigest(&ctx, &pubKey, NULL, sig64), ARG_ERROR_POS_2)
TEST(NCVerifyDigest(&ctx, &pubKey, zero32, NULL), ARG_ERROR_POS_3)
//Test verify data args
TEST(NCVerifyData(NULL, &pubKey, zero32, 32, sig64), ARG_ERROR_POS_0)
TEST(NCVerifyData(&ctx, NULL, zero32, 32, sig64), ARG_ERROR_POS_1)
TEST(NCVerifyData(&ctx, &pubKey, NULL, 32, sig64), ARG_ERROR_POS_2)
TEST(NCVerifyData(&ctx, &pubKey, zero32, 0, sig64), ARG_RAMGE_ERROR_POS_3)
TEST(NCVerifyData(&ctx, &pubKey, zero32, 32, NULL), ARG_ERROR_POS_4)
//Test null sign data args
TEST(NCSignData(NULL, &secKey, zero32, zero32, 32, sig64), ARG_ERROR_POS_0)
TEST(NCSignData(&ctx, NULL, zero32, zero32, 32, sig64), ARG_ERROR_POS_1)
TEST(NCSignData(&ctx, &secKey, NULL, zero32, 32, sig64), ARG_ERROR_POS_2)
TEST(NCSignData(&ctx, &secKey, zero32, NULL, 32, sig64), ARG_ERROR_POS_3)
TEST(NCSignData(&ctx, &secKey, zero32, zero32, 0, sig64), ARG_RAMGE_ERROR_POS_4)
TEST(NCSignData(&ctx, &secKey, zero32, zero32, 32, NULL), ARG_ERROR_POS_5)
//Test null sign digest args
TEST(NCSignDigest(NULL, &secKey, zero32, zero32, sig64), ARG_ERROR_POS_0)
TEST(NCSignDigest(&ctx, NULL, zero32, zero32, sig64), ARG_ERROR_POS_1)
TEST(NCSignDigest(&ctx, &secKey, NULL, zero32, sig64), ARG_ERROR_POS_2)
TEST(NCSignDigest(&ctx, &secKey, zero32, NULL, sig64), ARG_ERROR_POS_3)
TEST(NCSignDigest(&ctx, &secKey, zero32, zero32, NULL), ARG_ERROR_POS_4)
//Encrypt
cryptoData.dataSize = 32;
cryptoData.inputData = zero32;
cryptoData.outputData = sig64;
FillRandomData(&cryptoData.nonce, 32);
TEST(NCEncrypt(NULL, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_ERROR_POS_0)
TEST(NCEncrypt(&ctx, NULL, &pubKey, hmacKeyOut, &cryptoData), ARG_ERROR_POS_1)
TEST(NCEncrypt(&ctx, &secKey, NULL, hmacKeyOut, &cryptoData), ARG_ERROR_POS_2)
TEST(NCEncrypt(&ctx, &secKey, &pubKey, NULL, &cryptoData), ARG_ERROR_POS_3)
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, NULL), ARG_ERROR_POS_4)
//Test invalid data size
cryptoData.dataSize = 0;
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_RAMGE_ERROR_POS_4)
//Test null input data
cryptoData.dataSize = 32;
cryptoData.inputData = NULL;
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_INVALID_ERROR_POS_4)
//Test null output data
cryptoData.inputData = zero32;
cryptoData.outputData = NULL;
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_INVALID_ERROR_POS_4)
//Decrypt
cryptoData.dataSize = 32;
cryptoData.inputData = zero32;
cryptoData.outputData = sig64;
TEST(NCDecrypt(NULL, &secKey, &pubKey, &cryptoData), ARG_ERROR_POS_0)
TEST(NCDecrypt(&ctx, NULL, &pubKey, &cryptoData), ARG_ERROR_POS_1)
TEST(NCDecrypt(&ctx, &secKey, NULL, &cryptoData), ARG_ERROR_POS_2)
TEST(NCDecrypt(&ctx, &secKey, &pubKey, NULL), ARG_ERROR_POS_3)
//Test invalid data size
cryptoData.dataSize = 0;
TEST(NCDecrypt(&ctx, &secKey, &pubKey, &cryptoData), ARG_RAMGE_ERROR_POS_3)
//Test null input data
cryptoData.dataSize = 32;
cryptoData.inputData = NULL;
TEST(NCDecrypt(&ctx, &secKey, &pubKey, &cryptoData), ARG_INVALID_ERROR_POS_3)
//Test null output data
cryptoData.inputData = zero32;
cryptoData.outputData = NULL;
TEST(NCDecrypt(&ctx, &secKey, &pubKey, &cryptoData), ARG_INVALID_ERROR_POS_3)
{
uint8_t hmacDataOut[NC_ENCRYPTION_MAC_SIZE];
TEST(NCComputeMac(NULL, hmacKeyOut, zero32, 32, hmacDataOut), ARG_ERROR_POS_0)
TEST(NCComputeMac(&ctx, NULL, zero32, 32, hmacDataOut), ARG_ERROR_POS_1)
TEST(NCComputeMac(&ctx, hmacKeyOut, NULL, 32, hmacDataOut), ARG_ERROR_POS_2)
TEST(NCComputeMac(&ctx, hmacKeyOut, zero32, 0, hmacDataOut), ARG_RAMGE_ERROR_POS_3)
TEST(NCComputeMac(&ctx, hmacKeyOut, zero32, 32, NULL), ARG_ERROR_POS_4)
}
PRINTL("\nPASSED: Public API argument validation tests completed\n")
return 0;
}
#endif
static void FillRandomData(void* pbBuffer, size_t length)
{
#ifdef IS_WINDOWS
HCRYPTPROV hCryptProv;
TASSERT(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0));
TASSERT(CryptGenRandom(hCryptProv, (DWORD)length, pbBuffer))
TASSERT(CryptReleaseContext(hCryptProv, 0));
#else
FILE* f = fopen("/dev/urandom", "rb");
TASSERT(f != NULL);
TASSERT(fread(pbBuffer, 1, length, f) == length);
fclose(f);
#endif
}
|