You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

  1. #define TEST_NAME "secretbox7"
  2. #include "cmptest.h"
  3. unsigned char k[crypto_secretbox_KEYBYTES];
  4. unsigned char n[crypto_secretbox_NONCEBYTES];
  5. unsigned char m[10000];
  6. unsigned char c[10000];
  7. unsigned char m2[10000];
  8. int main(void)
  9. {
  10. size_t mlen;
  11. size_t i;
  12. for (mlen = 0; mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;
  13. ++mlen) {
  14. randombytes_buf(k, crypto_secretbox_KEYBYTES);
  15. randombytes_buf(n, crypto_secretbox_NONCEBYTES);
  16. randombytes_buf(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,
  19. k) == 0) {
  20. for (i = 0; i < mlen + crypto_secretbox_ZEROBYTES; ++i) {
  21. if (m2[i] != m[i]) {
  22. printf("bad decryption\n");
  23. break;
  24. }
  25. }
  26. } else {
  27. printf("ciphertext fails verification\n");
  28. }
  29. }
  30. return 0;
  31. }