xref: /freebsd/sys/contrib/libsodium/test/default/verify1.c (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "verify1"
3*0ac341f1SConrad Meyer #include "cmptest.h"
4*0ac341f1SConrad Meyer 
5*0ac341f1SConrad Meyer int
main(void)6*0ac341f1SConrad Meyer main(void)
7*0ac341f1SConrad Meyer {
8*0ac341f1SConrad Meyer     unsigned char *v16, *v16x;
9*0ac341f1SConrad Meyer     unsigned char *v32, *v32x;
10*0ac341f1SConrad Meyer     unsigned char *v64, *v64x;
11*0ac341f1SConrad Meyer     uint32_t       r;
12*0ac341f1SConrad Meyer     uint8_t        o;
13*0ac341f1SConrad Meyer     int            i;
14*0ac341f1SConrad Meyer 
15*0ac341f1SConrad Meyer     v16  = (unsigned char *) sodium_malloc(16);
16*0ac341f1SConrad Meyer     v16x = (unsigned char *) sodium_malloc(16);
17*0ac341f1SConrad Meyer     v32  = (unsigned char *) sodium_malloc(32);
18*0ac341f1SConrad Meyer     v32x = (unsigned char *) sodium_malloc(32);
19*0ac341f1SConrad Meyer     v64  = (unsigned char *) sodium_malloc(64);
20*0ac341f1SConrad Meyer     v64x = (unsigned char *) sodium_malloc(64);
21*0ac341f1SConrad Meyer     for (i = 0; i < 10000; i++) {
22*0ac341f1SConrad Meyer         randombytes_buf(v16, 16);
23*0ac341f1SConrad Meyer         randombytes_buf(v32, 32);
24*0ac341f1SConrad Meyer         randombytes_buf(v64, 64);
25*0ac341f1SConrad Meyer 
26*0ac341f1SConrad Meyer         memcpy(v16x, v16, 16);
27*0ac341f1SConrad Meyer         memcpy(v32x, v32, 32);
28*0ac341f1SConrad Meyer         memcpy(v64x, v64, 64);
29*0ac341f1SConrad Meyer 
30*0ac341f1SConrad Meyer         if (crypto_verify_16(v16, v16x) != 0 ||
31*0ac341f1SConrad Meyer             crypto_verify_32(v32, v32x) != 0 ||
32*0ac341f1SConrad Meyer             crypto_verify_64(v64, v64x) != 0 ||
33*0ac341f1SConrad Meyer             sodium_memcmp(v16, v16x, 16) != 0 ||
34*0ac341f1SConrad Meyer             sodium_memcmp(v32, v32x, 32) != 0 ||
35*0ac341f1SConrad Meyer             sodium_memcmp(v64, v64x, 64) != 0) {
36*0ac341f1SConrad Meyer             printf("Failed\n");
37*0ac341f1SConrad Meyer         }
38*0ac341f1SConrad Meyer     }
39*0ac341f1SConrad Meyer     printf("OK\n");
40*0ac341f1SConrad Meyer 
41*0ac341f1SConrad Meyer     for (i = 0; i < 100000; i++) {
42*0ac341f1SConrad Meyer         r = randombytes_random();
43*0ac341f1SConrad Meyer         o = (uint8_t) randombytes_random();
44*0ac341f1SConrad Meyer         if (o == 0) {
45*0ac341f1SConrad Meyer             continue;
46*0ac341f1SConrad Meyer         }
47*0ac341f1SConrad Meyer         v16x[r & 15U] ^= o;
48*0ac341f1SConrad Meyer         v32x[r & 31U] ^= o;
49*0ac341f1SConrad Meyer         v64x[r & 63U] ^= o;
50*0ac341f1SConrad Meyer         if (crypto_verify_16(v16, v16x) != -1 ||
51*0ac341f1SConrad Meyer             crypto_verify_32(v32, v32x) != -1 ||
52*0ac341f1SConrad Meyer             crypto_verify_64(v64, v64x) != -1 ||
53*0ac341f1SConrad Meyer             sodium_memcmp(v16, v16x, 16) != -1 ||
54*0ac341f1SConrad Meyer             sodium_memcmp(v32, v32x, 32) != -1 ||
55*0ac341f1SConrad Meyer             sodium_memcmp(v64, v64x, 64) != -1) {
56*0ac341f1SConrad Meyer             printf("Failed\n");
57*0ac341f1SConrad Meyer         }
58*0ac341f1SConrad Meyer         v16x[r & 15U] ^= o;
59*0ac341f1SConrad Meyer         v32x[r & 31U] ^= o;
60*0ac341f1SConrad Meyer         v64x[r & 63U] ^= o;
61*0ac341f1SConrad Meyer     }
62*0ac341f1SConrad Meyer     printf("OK\n");
63*0ac341f1SConrad Meyer 
64*0ac341f1SConrad Meyer     assert(crypto_verify_16_bytes() == 16U);
65*0ac341f1SConrad Meyer     assert(crypto_verify_32_bytes() == 32U);
66*0ac341f1SConrad Meyer     assert(crypto_verify_64_bytes() == 64U);
67*0ac341f1SConrad Meyer 
68*0ac341f1SConrad Meyer     sodium_free(v16);
69*0ac341f1SConrad Meyer     sodium_free(v16x);
70*0ac341f1SConrad Meyer     sodium_free(v32);
71*0ac341f1SConrad Meyer     sodium_free(v32x);
72*0ac341f1SConrad Meyer     sodium_free(v64);
73*0ac341f1SConrad Meyer     sodium_free(v64x);
74*0ac341f1SConrad Meyer 
75*0ac341f1SConrad Meyer     return 0;
76*0ac341f1SConrad Meyer }
77