Path: blob/main/sys/contrib/libsodium/test/default/box8.c
48378 views
1#define TEST_NAME "box8"2#include "cmptest.h"34static unsigned char alicesk[crypto_box_SECRETKEYBYTES];5static unsigned char alicepk[crypto_box_PUBLICKEYBYTES];6static unsigned char bobsk[crypto_box_SECRETKEYBYTES];7static unsigned char bobpk[crypto_box_PUBLICKEYBYTES];8static unsigned char n[crypto_box_NONCEBYTES];910int11main(void)12{13unsigned char *m;14unsigned char *c;15unsigned char *m2;16size_t mlen;17size_t mlen_max = 1000;18size_t i;19int faults;20int ret;2122m = (unsigned char *) sodium_malloc(mlen_max);23c = (unsigned char *) sodium_malloc(mlen_max);24m2 = (unsigned char *) sodium_malloc(mlen_max);25crypto_box_keypair(alicepk, alicesk);26crypto_box_keypair(bobpk, bobsk);27for (mlen = 0; mlen + crypto_box_ZEROBYTES <= mlen_max; mlen++) {28randombytes_buf(n, crypto_box_NONCEBYTES);29randombytes_buf(m + crypto_box_ZEROBYTES, mlen);30ret = crypto_box(c, m, mlen + crypto_box_ZEROBYTES, n, bobpk, alicesk);31assert(ret == 0);32#ifdef BROWSER_TESTS33faults = 1;34#else35faults = 5;36#endif37while (faults > 0) {38c[rand() % (mlen + crypto_box_ZEROBYTES)] = rand();39if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,40bobsk) == 0) {41for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {42if (m2[i] != m[i]) {43printf("forgery\n");44return 100;45}46}47} else {48faults--;49}50}51}52sodium_free(m);53sodium_free(c);54sodium_free(m2);5556return 0;57}585960