box_easy2.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #define TEST_NAME "box_easy2"
  2. #include "cmptest.h"
  3. static const unsigned char small_order_p[crypto_box_PUBLICKEYBYTES] = {
  4. 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
  5. 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
  6. 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00
  7. };
  8. int
  9. main(void)
  10. {
  11. unsigned char *alicepk;
  12. unsigned char *alicesk;
  13. unsigned char *bobpk;
  14. unsigned char *bobsk;
  15. unsigned char *mac;
  16. unsigned char *nonce;
  17. unsigned char *k1;
  18. unsigned char *k2;
  19. unsigned char *m;
  20. unsigned char *m2;
  21. unsigned char *c;
  22. size_t mlen;
  23. size_t i;
  24. size_t m_size;
  25. size_t m2_size;
  26. size_t c_size;
  27. int ret;
  28. m2_size = m_size = 7U + randombytes_uniform(1000);
  29. c_size = crypto_box_MACBYTES + m_size;
  30. m = (unsigned char *) sodium_malloc(m_size);
  31. m2 = (unsigned char *) sodium_malloc(m2_size);
  32. c = (unsigned char *) sodium_malloc(c_size);
  33. alicepk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
  34. alicesk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
  35. bobpk = (unsigned char *) sodium_malloc(crypto_box_PUBLICKEYBYTES);
  36. bobsk = (unsigned char *) sodium_malloc(crypto_box_SECRETKEYBYTES);
  37. mac = (unsigned char *) sodium_malloc(crypto_box_MACBYTES);
  38. nonce = (unsigned char *) sodium_malloc(crypto_box_NONCEBYTES);
  39. k1 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
  40. k2 = (unsigned char *) sodium_malloc(crypto_box_BEFORENMBYTES);
  41. crypto_box_keypair(alicepk, alicesk);
  42. crypto_box_keypair(bobpk, bobsk);
  43. mlen = (size_t) randombytes_uniform((uint32_t) m_size) + 1U;
  44. randombytes_buf(m, mlen);
  45. randombytes_buf(nonce, crypto_box_NONCEBYTES);
  46. ret = crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
  47. assert(ret == 0);
  48. if (crypto_box_open_easy(m2, c,
  49. (unsigned long long) mlen + crypto_box_MACBYTES,
  50. nonce, alicepk, bobsk) != 0) {
  51. printf("open() failed");
  52. return 1;
  53. }
  54. printf("%d\n", memcmp(m, m2, mlen));
  55. for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
  56. if (crypto_box_open_easy(m2, c, (unsigned long long) i, nonce, alicepk,
  57. bobsk) == 0) {
  58. printf("short open() should have failed");
  59. return 1;
  60. }
  61. }
  62. memcpy(c, m, mlen);
  63. ret =
  64. crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
  65. assert(ret == 0);
  66. printf("%d\n", memcmp(m, c, mlen) == 0);
  67. printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
  68. if (crypto_box_open_easy(c, c,
  69. (unsigned long long) mlen + crypto_box_MACBYTES,
  70. nonce, alicepk, bobsk) != 0) {
  71. printf("crypto_box_open_easy() failed\n");
  72. }
  73. ret = crypto_box_beforenm(k1, small_order_p, bobsk);
  74. assert(ret == -1);
  75. ret = crypto_box_beforenm(k2, small_order_p, alicesk);
  76. assert(ret == -1);
  77. ret = crypto_box_beforenm(k1, alicepk, bobsk);
  78. assert(ret == 0);
  79. ret = crypto_box_beforenm(k2, bobpk, alicesk);
  80. assert(ret == 0);
  81. memset(m2, 0, m2_size);
  82. if (crypto_box_easy_afternm(c, m, 0, nonce, k1) != 0) {
  83. printf(
  84. "crypto_box_easy_afternm() with a null ciphertext should have "
  85. "worked\n");
  86. }
  87. crypto_box_easy_afternm(c, m, (unsigned long long) mlen, nonce, k1);
  88. if (crypto_box_open_easy_afternm(
  89. m2, c, (unsigned long long) mlen + crypto_box_MACBYTES, nonce,
  90. k2) != 0) {
  91. printf("crypto_box_open_easy_afternm() failed\n");
  92. }
  93. printf("%d\n", memcmp(m, m2, mlen));
  94. if (crypto_box_open_easy_afternm(m2, c, crypto_box_MACBYTES - 1U, nonce,
  95. k2) == 0) {
  96. printf(
  97. "crypto_box_open_easy_afternm() with a huge ciphertext should have "
  98. "failed\n");
  99. }
  100. memset(m2, 0, m2_size);
  101. ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen, nonce,
  102. small_order_p, bobsk);
  103. assert(ret == -1);
  104. ret = crypto_box_detached(c, mac, m, (unsigned long long) mlen, nonce,
  105. alicepk, bobsk);
  106. assert(ret == 0);
  107. if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen, nonce,
  108. small_order_p, alicesk) != -1) {
  109. printf("crypto_box_open_detached() with a weak key passed\n");
  110. }
  111. if (crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen, nonce,
  112. bobpk, alicesk) != 0) {
  113. printf("crypto_box_open_detached() failed\n");
  114. }
  115. printf("%d\n", memcmp(m, m2, mlen));
  116. memset(m2, 0, m2_size);
  117. crypto_box_detached_afternm(c, mac, m, (unsigned long long) mlen, nonce,
  118. k1);
  119. if (crypto_box_open_detached_afternm(m2, c, mac, (unsigned long long) mlen,
  120. nonce, k2) != 0) {
  121. printf("crypto_box_open_detached_afternm() failed\n");
  122. }
  123. printf("%d\n", memcmp(m, m2, mlen));
  124. sodium_free(alicepk);
  125. sodium_free(alicesk);
  126. sodium_free(bobpk);
  127. sodium_free(bobsk);
  128. sodium_free(mac);
  129. sodium_free(nonce);
  130. sodium_free(k1);
  131. sodium_free(k2);
  132. sodium_free(m);
  133. sodium_free(m2);
  134. sodium_free(c);
  135. printf("OK\n");
  136. return 0;
  137. }