box7.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #define TEST_NAME "box7"
  2. #include "cmptest.h"
  3. static unsigned char alicesk[crypto_box_SECRETKEYBYTES];
  4. static unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
  5. static unsigned char bobsk[crypto_box_SECRETKEYBYTES];
  6. static unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
  7. static unsigned char n[crypto_box_NONCEBYTES];
  8. int
  9. main(void)
  10. {
  11. unsigned char *m;
  12. unsigned char *c;
  13. unsigned char *m2;
  14. size_t mlen;
  15. size_t mlen_max = 1000;
  16. size_t i;
  17. int ret;
  18. m = (unsigned char *) sodium_malloc(mlen_max);
  19. c = (unsigned char *) sodium_malloc(mlen_max);
  20. m2 = (unsigned char *) sodium_malloc(mlen_max);
  21. memset(m, 0, crypto_box_ZEROBYTES);
  22. crypto_box_keypair(alicepk, alicesk);
  23. crypto_box_keypair(bobpk, bobsk);
  24. for (mlen = 0; mlen + crypto_box_ZEROBYTES <= mlen_max; mlen++) {
  25. randombytes_buf(n, crypto_box_NONCEBYTES);
  26. randombytes_buf(m + crypto_box_ZEROBYTES, mlen);
  27. ret = crypto_box(c, m, mlen + crypto_box_ZEROBYTES, n, bobpk, alicesk);
  28. assert(ret == 0);
  29. if (crypto_box_open(m2, c, mlen + crypto_box_ZEROBYTES, n, alicepk,
  30. bobsk) == 0) {
  31. for (i = 0; i < mlen + crypto_box_ZEROBYTES; ++i) {
  32. if (m2[i] != m[i]) {
  33. printf("bad decryption\n");
  34. break;
  35. }
  36. }
  37. } else {
  38. printf("ciphertext fails verification\n");
  39. }
  40. }
  41. sodium_free(m);
  42. sodium_free(c);
  43. sodium_free(m2);
  44. return 0;
  45. }