demo.c 1.66 KB
Newer Older
Martin Schläffer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <stdio.h>
#include <string.h>

#include "api.h"
#if defined(CRYPTO_AEAD)
#include "crypto_aead.h"
#elif defined(CRYPTO_HASH)
#include "crypto_hash.h"
#endif

void print(unsigned char c, unsigned char* x, unsigned long long xlen) {
  unsigned long long i;
  printf("%c[%d]=", c, (int)xlen);
  for (i = 0; i < xlen; ++i) printf("%02x", x[i]);
  printf("\n");
}

int main() {
  int result = 0;
#if defined(CRYPTO_AEAD)
  unsigned long long alen = 0;
  unsigned long long mlen = 0;
  unsigned long long clen = CRYPTO_ABYTES;
  unsigned char a[] = "ASCON";
  unsigned char m[] = "ascon";
  unsigned char c[strlen((const char*)m) + CRYPTO_ABYTES];
  unsigned char nsec[CRYPTO_NSECBYTES];
  unsigned char npub[CRYPTO_NPUBBYTES] = {0};
  unsigned char k[CRYPTO_KEYBYTES] = {0};
  alen = strlen((const char*)a);
  mlen = strlen((const char*)m);
  print('k', k, CRYPTO_KEYBYTES);
  printf(" ");
  print('n', npub, CRYPTO_NPUBBYTES);
  printf("\n");
  print('a', a, alen);
  printf(" ");
  print('m', m, mlen);
  printf(" -> ");
  result |= crypto_aead_encrypt(c, &clen, m, mlen, a, alen, nsec, npub, k);
  print('c', c, clen - CRYPTO_ABYTES);
  printf(" ");
  print('t', c + clen - CRYPTO_ABYTES, CRYPTO_ABYTES);
  printf(" -> ");
  result |= crypto_aead_decrypt(m, &mlen, nsec, c, clen, a, alen, npub, k);
  print('a', a, alen);
  printf(" ");
  print('m', m, mlen);
  printf("\n");
#elif defined(CRYPTO_HASH)
  unsigned long long mlen = 0;
  unsigned char m[] = "ascon";
  unsigned char h[CRYPTO_BYTES] = {0};
  mlen = strlen((const char*)m);
  print('m', m, mlen);
  printf(" -> ");
  result |= crypto_hash(h, m, mlen);
  print('h', h, CRYPTO_BYTES);
  printf("\n");
#endif
  return result;
}