libsodium/test/default/secretbox_easy2.c
Frank Denis 44f7a9f3cb Allow the authentication tag to be stored separately from the message.
Encrypting in-place and storing the tag separately is a very common need.
Instead of forcing people to do their own cuisine, let's provide simple
variants of the _easy interfaces to do that.
2014-06-26 15:18:39 -07:00

33 lines
962 B
C

#include <stdio.h>
#include <string.h>
#define TEST_NAME "secretbox_easy2"
#include "cmptest.h"
unsigned char m[10000];
unsigned char m2[10000];
unsigned char c[crypto_secretbox_MACBYTES + 10000];
unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char k[crypto_secretbox_KEYBYTES];
unsigned char mac[crypto_secretbox_MACBYTES];
int main(void)
{
unsigned long long mlen;
randombytes_buf(k, sizeof k);
mlen = (unsigned long long) randombytes_uniform((uint32_t) sizeof m);
randombytes_buf(m, mlen);
randombytes_buf(nonce, sizeof nonce);
crypto_secretbox_easy(c, m, mlen, nonce, k);
crypto_secretbox_open_easy(m2, c, mlen + crypto_secretbox_MACBYTES,
nonce, k);
printf("%d\n", memcmp(m, m2, mlen));
crypto_secretbox_easy_detached(c, mac, m, mlen, nonce, k);
crypto_secretbox_open_easy_detached(m2, c, mac, mlen, nonce, k);
printf("%d\n", memcmp(m, m2, mlen));
return 0;
}