secretbox7.c 874 B

1234567891011121314151617181920212223242526272829303132
  1. #include <stdio.h>
  2. #include "crypto_secretbox.h"
  3. #include "randombytes.h"
  4. unsigned char k[crypto_secretbox_KEYBYTES];
  5. unsigned char n[crypto_secretbox_NONCEBYTES];
  6. unsigned char m[10000];
  7. unsigned char c[10000];
  8. unsigned char m2[10000];
  9. int main()
  10. {
  11. int mlen;
  12. int i;
  13. for (mlen = 0;mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;++mlen) {
  14. randombytes(k,crypto_secretbox_KEYBYTES);
  15. randombytes(n,crypto_secretbox_NONCEBYTES);
  16. randombytes(m + crypto_secretbox_ZEROBYTES,mlen);
  17. crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k);
  18. if (crypto_secretbox_open(m2,c,mlen + crypto_secretbox_ZEROBYTES,n,k) == 0) {
  19. for (i = 0;i < mlen + crypto_secretbox_ZEROBYTES;++i)
  20. if (m2[i] != m[i]) {
  21. printf("bad decryption\n");
  22. break;
  23. }
  24. } else {
  25. printf("ciphertext fails verification\n");
  26. }
  27. }
  28. return 0;
  29. }