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.

59 lines
2.0 KiB

  1. #define TEST_NAME "box_easy2"
  2. #include "cmptest.h"
  3. unsigned char m[10000];
  4. unsigned char m2[10000];
  5. unsigned char c[crypto_box_MACBYTES + 10000];
  6. unsigned char nonce[crypto_box_NONCEBYTES];
  7. unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
  8. unsigned char alicesk[crypto_box_SECRETKEYBYTES];
  9. unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
  10. unsigned char bobsk[crypto_box_SECRETKEYBYTES];
  11. unsigned char mac[crypto_box_MACBYTES];
  12. int main(void)
  13. {
  14. size_t mlen;
  15. size_t i;
  16. crypto_box_keypair(alicepk, alicesk);
  17. crypto_box_keypair(bobpk, bobsk);
  18. mlen = (size_t) randombytes_uniform((uint32_t)sizeof m);
  19. randombytes_buf(m, mlen);
  20. randombytes_buf(nonce, sizeof nonce);
  21. crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
  22. if (crypto_box_open_easy(m2, c,
  23. (unsigned long long) mlen + crypto_box_MACBYTES,
  24. nonce, alicepk, bobsk) != 0) {
  25. printf("open() failed");
  26. return 1;
  27. }
  28. printf("%d\n", memcmp(m, m2, mlen));
  29. for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
  30. if (crypto_box_open_easy(m2, c, (unsigned long long) i,
  31. nonce, alicepk, bobsk) == 0) {
  32. printf("short open() should have failed");
  33. return 1;
  34. }
  35. }
  36. memcpy(c, m, mlen);
  37. crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
  38. printf("%d\n", memcmp(m, c, mlen) == 0);
  39. printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
  40. if (crypto_box_open_easy(c, c,
  41. (unsigned long long) mlen + crypto_box_MACBYTES,
  42. nonce, alicepk, bobsk) != 0) {
  43. printf("crypto_box_open_easy() failed\n");
  44. }
  45. crypto_box_detached(c, mac, m, (unsigned long long) mlen,
  46. nonce, bobsk, alicepk);
  47. crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen,
  48. nonce, alicepk, bobsk);
  49. printf("%d\n", memcmp(m, m2, mlen));
  50. return 0;
  51. }