Blame SOURCES/libsodium-1.0.18/test/default/box8.c

ca16be
ca16be
#define TEST_NAME "box8"
ca16be
#include "cmptest.h"
ca16be
ca16be
static unsigned char alicesk[crypto_box_SECRETKEYBYTES];
ca16be
static unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
ca16be
static unsigned char bobsk[crypto_box_SECRETKEYBYTES];
ca16be
static unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
ca16be
static unsigned char n[crypto_box_NONCEBYTES];
ca16be
ca16be
int
ca16be
main(void)
ca16be
{
ca16be
    unsigned char *m;
ca16be
    unsigned char *c;
ca16be
    unsigned char *m2;
ca16be
    size_t         mlen;
ca16be
    size_t         mlen_max = 1000;
ca16be
    size_t         i;
ca16be
    int            faults;
ca16be
    int            ret;
ca16be
ca16be
    m  = (unsigned char *) sodium_malloc(mlen_max);
ca16be
    c  = (unsigned char *) sodium_malloc(mlen_max);
ca16be
    m2 = (unsigned char *) sodium_malloc(mlen_max);
ca16be
    crypto_box_keypair(alicepk, alicesk);
ca16be
    crypto_box_keypair(bobpk, bobsk);
ca16be
    for (mlen = 0; mlen + crypto_box_ZEROBYTES <= mlen_max; mlen++) {
ca16be
        randombytes_buf(n, crypto_box_NONCEBYTES);
ca16be
        randombytes_buf(m + crypto_box_ZEROBYTES, mlen);
ca16be
        ret = crypto_box(c, m, mlen + crypto_box_ZEROBYTES, n, bobpk, alicesk);
ca16be
        assert(ret == 0);
ca16be
#ifdef BROWSER_TESTS
ca16be
        faults = 1;
ca16be
#else
ca16be
        faults = 5;
ca16be
#endif
ca16be
        while (faults > 0) {
ca16be
            c[rand() % (mlen + crypto_box_ZEROBYTES)] = rand();
ca16be
            if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,
ca16be
                                bobsk) == 0) {
ca16be
                for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {
ca16be
                    if (m2[i] != m[i]) {
ca16be
                        printf("forgery\n");
ca16be
                        return 100;
ca16be
                    }
ca16be
                }
ca16be
            } else {
ca16be
                faults--;
ca16be
            }
ca16be
        }
ca16be
    }
ca16be
    sodium_free(m);
ca16be
    sodium_free(c);
ca16be
    sodium_free(m2);
ca16be
ca16be
    return 0;
ca16be
}