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.

53 lines
1.9 KiB

  1. #define TEST_NAME "secretbox_easy2"
  2. #include "cmptest.h"
  3. unsigned char m[10000];
  4. unsigned char m2[10000];
  5. unsigned char c[crypto_secretbox_MACBYTES + 10000];
  6. unsigned char nonce[crypto_secretbox_NONCEBYTES];
  7. unsigned char k[crypto_secretbox_KEYBYTES];
  8. unsigned char mac[crypto_secretbox_MACBYTES];
  9. int main(void)
  10. {
  11. size_t mlen;
  12. size_t i;
  13. randombytes_buf(k, sizeof k);
  14. mlen = (size_t) randombytes_uniform((uint32_t) sizeof m);
  15. randombytes_buf(m, (unsigned long long) mlen);
  16. randombytes_buf(nonce, sizeof nonce);
  17. crypto_secretbox_easy(c, m, (unsigned long long) mlen, nonce, k);
  18. crypto_secretbox_open_easy(m2, c,
  19. (unsigned long long) mlen + crypto_secretbox_MACBYTES,
  20. nonce, k);
  21. printf("%d\n", memcmp(m, m2, mlen));
  22. for (i = 0; i < mlen + crypto_secretbox_MACBYTES - 1; i++) {
  23. if (crypto_secretbox_open_easy(m2, c, (unsigned long long) i,
  24. nonce, k) == 0) {
  25. printf("short open() should have failed\n");
  26. return 1;
  27. }
  28. }
  29. crypto_secretbox_detached(c, mac, m, (unsigned long long) mlen, nonce, k);
  30. if (crypto_secretbox_open_detached(m2, c, mac, (unsigned long long) mlen,
  31. nonce, k) != 0) {
  32. printf("crypto_secretbox_open_detached() failed\n");
  33. }
  34. printf("%d\n", memcmp(m, m2, mlen));
  35. memcpy(c, m, mlen);
  36. crypto_secretbox_easy(c, c, (unsigned long long) mlen, nonce, k);
  37. printf("%d\n", memcmp(m, c, mlen) == 0);
  38. printf("%d\n", memcmp(m, c + crypto_secretbox_MACBYTES, mlen) == 0);
  39. if (crypto_secretbox_open_easy(c, c,
  40. (unsigned long long) mlen + crypto_secretbox_MACBYTES,
  41. nonce, k) != 0) {
  42. printf("crypto_secretbox_open_easy() failed\n");
  43. }
  44. printf("%d\n", memcmp(m, c, mlen));
  45. return 0;
  46. }