+
+// disable deprecation for sprintf and fopen
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/time.h>
+#include <inttypes.h>
+#include <stddef.h>
+#include <string.h>
+#include <stdlib.h>
+#include "grain128aead_32p.h"
+
+#define KAT_SUCCESS 0
+#define KAT_FILE_OPEN_ERROR -1
+#define KAT_DATA_ERROR -3
+#define KAT_CRYPTO_FAILURE -4
+
+//#define MAX_MESSAGE_LENGTH 512*512
+#define MAX_ASSOCIATED_DATA_LENGTH 32
+
+
+double TimeStart()
+{
+ struct timeval tstart;
+ gettimeofday(&tstart,0);
+ return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
+}
+
+double TimeStop(double t)
+{
+ struct timeval tend;
+
+ gettimeofday(&tend,0);
+ t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
+ return (t);
+}
+
+
+
+int crypto_aead_encrypt(
+ unsigned char *c, unsigned long long *clen,
+ const unsigned char *m, unsigned long long mlen,
+ const unsigned char *ad, unsigned long long adlen,
+ const unsigned char *nsec,
+ const unsigned char *npub,
+ const unsigned char *k);
+int crypto_aead_decrypt(
+ unsigned char *m, unsigned long long *mlen,
+ unsigned char *nsec,
+ const unsigned char *c, unsigned long long clen,
+ const unsigned char *ad, unsigned long long adlen,
+ const unsigned char *npub,
+ const unsigned char *k);
+
+
+void init_buffer(unsigned char *buffer, unsigned long long numbytes);
+
+void print_bstr(const char *label, const unsigned char *data, unsigned long long length);
+
+int generate_test_vectors();
+
+int main(int argc, char** argv)
+{
+
+ int size_buf=1;
+ int nb_test=1;
+
+
+ for(int i=1; i<argc; i++){
+ if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
+ if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
+
+ }
+#define CRYPTO_ABYTES 32
+#define CRYPTO_KEYBYTES 16
+#define CRYPTO_NPUBBYTES 16
+
+
+ size_buf=size_buf*size_buf;
+
+
+ unsigned char key[CRYPTO_KEYBYTES];
+ unsigned char nonce[CRYPTO_NPUBBYTES];
+ unsigned char msg[size_buf];
+ unsigned char msg2[size_buf];
+ unsigned char ad[32];
+ unsigned char ct[size_buf + CRYPTO_ABYTES];
+ unsigned long long clen, mlen2;
+ int count = 1;
+ int func_ret, ret_val = KAT_SUCCESS;
+
+ init_buffer(key, sizeof(key));
+ init_buffer(nonce, sizeof(nonce));
+ init_buffer(msg, sizeof(msg));
+ init_buffer(ad, sizeof(ad));
+
+
+ int mlen=size_buf;
+ //printf("size head %d\n",CRYPTO_ABYTES);
+
+
+ // for (unsigned long long adlen = 0; adlen <= MAX_ASSOCIATED_DATA_LENGTH; adlen++) {
+ int adlen=10;
+ /*
+ printf("Count = %d\n", count++);
+
+ print_bstr("Key = ", key, CRYPTO_KEYBYTES);
+
+ print_bstr("Nonce = ", nonce, CRYPTO_NPUBBYTES);
+
+ //print_bstr("PT = ", msg, mlen);
+
+ print_bstr("AD = ", ad, adlen);
+ */
+
+ double time_encrypt=0;
+ double t=TimeStart();
+
+ for(int i=0;i<nb_test;i++) {
+ crypto_aead_encrypt(ct, &clen, msg, mlen, ad, adlen, NULL, nonce, key);
+ }
+
+ time_encrypt+=TimeStop(t);
+ printf("%e\t",(double)size_buf*nb_test/time_encrypt);
+
+
+ /*if ((func_ret = crypto_aead_encrypt(ct, &clen, msg, mlen, ad, adlen, NULL, nonce, key)) != 0) {
+ printf("crypto_aead_encrypt returned <%d>\n", func_ret);
+ ret_val = KAT_CRYPTO_FAILURE;
+ }*/
+
+ //print_bstr("CT = ", ct, clen);
+
+ // printf("\n");
+
+
+ double time_decrypt=0;
+ t=TimeStart();
+
+ for(int i=0;i<nb_test;i++) {
+ crypto_aead_decrypt(msg2, &mlen2, NULL, ct, clen, ad, adlen, nonce, key);
+ }
+
+ time_decrypt+=TimeStop(t);
+ printf("%e\t",(double)size_buf*nb_test/time_decrypt);
+ /*
+
+ if ((func_ret = crypto_aead_decrypt(msg2, &mlen2, NULL, ct, clen, ad, adlen, nonce, key)) != 0) {
+ printf("crypto_aead_decrypt returned <%d>\n", func_ret);
+ ret_val = KAT_CRYPTO_FAILURE;
+
+ }
+ */
+ if (mlen != mlen2) {
+ printf("crypto_aead_decrypt returned bad 'mlen': Got <%llu>, expected <%llu>\n", mlen2, mlen);
+ ret_val = KAT_CRYPTO_FAILURE;
+ }
+
+ if (memcmp(msg, msg2, mlen)) {
+ printf("crypto_aead_decrypt did not recover the plaintext\n");
+ ret_val = KAT_CRYPTO_FAILURE;
+ }
+
+ // print_bstr("MSG = ", msg, mlen);
+ // print_bstr("MSG2 = ", msg2, mlen);
+
+ return ret_val;
+}
+
+
+void print_bstr(const char *label, const unsigned char *data, unsigned long long length)
+{
+ printf( "%s", label);
+
+ for (unsigned long long i = 0; i < length; i++)
+ printf("%02X", data[i]);
+
+ printf("\n");
+}
+
+void init_buffer(unsigned char *buffer, unsigned long long numbytes)
+{
+ for (unsigned long long i = 0; i < numbytes; i++)
+ buffer[i] = (unsigned char)i;
+}