xref: /freebsd/sys/contrib/libsodium/test/default/secretbox_easy2.c (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "secretbox_easy2"
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 *m;
9*0ac341f1SConrad Meyer     unsigned char *m2;
10*0ac341f1SConrad Meyer     unsigned char *c;
11*0ac341f1SConrad Meyer     unsigned char *nonce;
12*0ac341f1SConrad Meyer     unsigned char *k;
13*0ac341f1SConrad Meyer     unsigned char *mac;
14*0ac341f1SConrad Meyer     size_t         mlen;
15*0ac341f1SConrad Meyer     size_t         i;
16*0ac341f1SConrad Meyer 
17*0ac341f1SConrad Meyer     mlen  = (size_t) randombytes_uniform((uint32_t) 10000) + 1U;
18*0ac341f1SConrad Meyer     m     = (unsigned char *) sodium_malloc(mlen);
19*0ac341f1SConrad Meyer     m2    = (unsigned char *) sodium_malloc(mlen);
20*0ac341f1SConrad Meyer     c     = (unsigned char *) sodium_malloc(crypto_secretbox_MACBYTES + mlen);
21*0ac341f1SConrad Meyer     nonce = (unsigned char *) sodium_malloc(crypto_secretbox_NONCEBYTES);
22*0ac341f1SConrad Meyer     k     = (unsigned char *) sodium_malloc(crypto_secretbox_KEYBYTES);
23*0ac341f1SConrad Meyer     mac   = (unsigned char *) sodium_malloc(crypto_secretbox_MACBYTES);
24*0ac341f1SConrad Meyer     crypto_secretbox_keygen(k);
25*0ac341f1SConrad Meyer     randombytes_buf(m, mlen);
26*0ac341f1SConrad Meyer     randombytes_buf(nonce, crypto_secretbox_NONCEBYTES);
27*0ac341f1SConrad Meyer     crypto_secretbox_easy(c, m, (unsigned long long) mlen, nonce, k);
28*0ac341f1SConrad Meyer     if (crypto_secretbox_open_easy(
29*0ac341f1SConrad Meyer             m2, c, (unsigned long long) mlen + crypto_secretbox_MACBYTES, nonce,
30*0ac341f1SConrad Meyer             k) != 0) {
31*0ac341f1SConrad Meyer         printf("crypto_secretbox_open_easy() failed\n");
32*0ac341f1SConrad Meyer     }
33*0ac341f1SConrad Meyer     printf("%d\n", memcmp(m, m2, mlen));
34*0ac341f1SConrad Meyer 
35*0ac341f1SConrad Meyer     for (i = 0; i < mlen + crypto_secretbox_MACBYTES - 1; i++) {
36*0ac341f1SConrad Meyer         if (crypto_secretbox_open_easy(m2, c, (unsigned long long) i, nonce,
37*0ac341f1SConrad Meyer                                        k) == 0) {
38*0ac341f1SConrad Meyer             printf("short open() should have failed\n");
39*0ac341f1SConrad Meyer             return 1;
40*0ac341f1SConrad Meyer         }
41*0ac341f1SConrad Meyer     }
42*0ac341f1SConrad Meyer     crypto_secretbox_detached(c, mac, m, (unsigned long long) mlen, nonce, k);
43*0ac341f1SConrad Meyer     if (crypto_secretbox_open_detached(NULL, c, mac, (unsigned long long) mlen,
44*0ac341f1SConrad Meyer                                        nonce, k) != 0) {
45*0ac341f1SConrad Meyer         printf("crypto_secretbox_open_detached() with a NULL message pointer failed\n");
46*0ac341f1SConrad Meyer     }
47*0ac341f1SConrad Meyer     if (crypto_secretbox_open_detached(m2, c, mac, (unsigned long long) mlen,
48*0ac341f1SConrad Meyer                                        nonce, k) != 0) {
49*0ac341f1SConrad Meyer         printf("crypto_secretbox_open_detached() failed\n");
50*0ac341f1SConrad Meyer     }
51*0ac341f1SConrad Meyer     printf("%d\n", memcmp(m, m2, mlen));
52*0ac341f1SConrad Meyer 
53*0ac341f1SConrad Meyer     memcpy(c, m, mlen);
54*0ac341f1SConrad Meyer     crypto_secretbox_easy(c, c, (unsigned long long) mlen, nonce, k);
55*0ac341f1SConrad Meyer     printf("%d\n", memcmp(m, c, mlen) == 0);
56*0ac341f1SConrad Meyer     printf("%d\n", memcmp(m, c + crypto_secretbox_MACBYTES, mlen) == 0);
57*0ac341f1SConrad Meyer     if (crypto_secretbox_open_easy(
58*0ac341f1SConrad Meyer             c, c, (unsigned long long) mlen + crypto_secretbox_MACBYTES, nonce,
59*0ac341f1SConrad Meyer             k) != 0) {
60*0ac341f1SConrad Meyer         printf("crypto_secretbox_open_easy() failed\n");
61*0ac341f1SConrad Meyer     }
62*0ac341f1SConrad Meyer     printf("%d\n", memcmp(m, c, mlen));
63*0ac341f1SConrad Meyer 
64*0ac341f1SConrad Meyer     sodium_free(m);
65*0ac341f1SConrad Meyer     sodium_free(m2);
66*0ac341f1SConrad Meyer     sodium_free(c);
67*0ac341f1SConrad Meyer     sodium_free(nonce);
68*0ac341f1SConrad Meyer     sodium_free(k);
69*0ac341f1SConrad Meyer     sodium_free(mac);
70*0ac341f1SConrad Meyer 
71*0ac341f1SConrad Meyer     return 0;
72*0ac341f1SConrad Meyer }
73