11558Srgrimes
21558Srgrimes#define TEST_NAME "secretbox7"
31558Srgrimes#include "cmptest.h"
41558Srgrimes
51558Srgrimesstatic unsigned char k[crypto_secretbox_KEYBYTES];
61558Srgrimesstatic unsigned char n[crypto_secretbox_NONCEBYTES];
71558Srgrimesstatic unsigned char m[10000];
81558Srgrimesstatic unsigned char c[10000];
91558Srgrimesstatic unsigned char m2[10000];
101558Srgrimes
111558Srgrimesint
121558Srgrimesmain(void)
131558Srgrimes{
141558Srgrimes    size_t mlen;
151558Srgrimes    size_t i;
161558Srgrimes
171558Srgrimes    for (mlen = 0; mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;
181558Srgrimes         ++mlen) {
191558Srgrimes        crypto_secretbox_keygen(k);
201558Srgrimes        randombytes_buf(n, crypto_secretbox_NONCEBYTES);
211558Srgrimes        randombytes_buf(m + crypto_secretbox_ZEROBYTES, mlen);
221558Srgrimes        crypto_secretbox(c, m, mlen + crypto_secretbox_ZEROBYTES, n, k);
231558Srgrimes        if (crypto_secretbox_open(m2, c, mlen + crypto_secretbox_ZEROBYTES, n,
241558Srgrimes                                  k) == 0) {
251558Srgrimes            for (i = 0; i < mlen + crypto_secretbox_ZEROBYTES; ++i) {
261558Srgrimes                if (m2[i] != m[i]) {
271558Srgrimes                    printf("bad decryption\n");
281558Srgrimes                    break;
291558Srgrimes                }
30114589Sobrien            }
311558Srgrimes        } else {
3238040Scharnier            printf("ciphertext fails verification\n");
331558Srgrimes        }
341558Srgrimes    }
351558Srgrimes    return 0;
361558Srgrimes}
371558Srgrimes