aead_xchacha20poly1305.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #define TEST_NAME "aead_xchacha20poly1305"
  2. #include "cmptest.h"
  3. static int
  4. tv(void)
  5. {
  6. #undef MLEN
  7. #define MLEN 114U
  8. #undef ADLEN
  9. #define ADLEN 12U
  10. #undef CLEN
  11. #define CLEN (MLEN + crypto_aead_xchacha20poly1305_ietf_ABYTES)
  12. static const unsigned char firstkey[crypto_aead_xchacha20poly1305_ietf_KEYBYTES]
  13. = {
  14. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  16. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  17. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  18. };
  19. #undef MESSAGE
  20. #define MESSAGE "Ladies and Gentlemen of the class of '99: If I could offer you " \
  21. "only one tip for the future, sunscreen would be it."
  22. unsigned char *m = (unsigned char *) sodium_malloc(MLEN);
  23. static const unsigned char nonce[crypto_aead_xchacha20poly1305_ietf_NPUBBYTES]
  24. = { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  25. 0x48, 0x49, 0x4a, 0x4b };
  26. static const unsigned char ad[ADLEN]
  27. = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 };
  28. unsigned char *c = (unsigned char *) sodium_malloc(CLEN);
  29. unsigned char *detached_c = (unsigned char *) sodium_malloc(MLEN);
  30. unsigned char *key2 = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
  31. unsigned char *mac = (unsigned char *) sodium_malloc(crypto_aead_xchacha20poly1305_ietf_ABYTES);
  32. unsigned char *m2 = (unsigned char *) sodium_malloc(MLEN);
  33. unsigned long long found_clen;
  34. unsigned long long found_maclen;
  35. unsigned long long m2len;
  36. size_t i;
  37. assert(sizeof MESSAGE - 1U == MLEN);
  38. memcpy(m, MESSAGE, MLEN);
  39. crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
  40. ad, ADLEN,
  41. NULL, nonce, firstkey);
  42. if (found_clen != MLEN + crypto_aead_xchacha20poly1305_ietf_abytes()) {
  43. printf("found_clen is not properly set\n");
  44. }
  45. for (i = 0U; i < CLEN; ++i) {
  46. printf(",0x%02x", (unsigned int) c[i]);
  47. if (i % 8 == 7) {
  48. printf("\n");
  49. }
  50. }
  51. printf("\n");
  52. crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c,
  53. mac, &found_maclen,
  54. m, MLEN,
  55. ad, ADLEN,
  56. NULL, nonce, firstkey);
  57. if (found_maclen != crypto_aead_xchacha20poly1305_ietf_abytes()) {
  58. printf("found_maclen is not properly set\n");
  59. }
  60. if (memcmp(detached_c, c, MLEN) != 0) {
  61. printf("detached ciphertext is bogus\n");
  62. }
  63. if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN, ad,
  64. ADLEN, nonce, firstkey) != 0) {
  65. printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed\n");
  66. }
  67. if (m2len != MLEN) {
  68. printf("m2len is not properly set\n");
  69. }
  70. if (memcmp(m, m2, MLEN) != 0) {
  71. printf("m != m2\n");
  72. }
  73. memset(m2, 0, m2len);
  74. if (crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, NULL,
  75. c, MLEN, mac,
  76. ad, ADLEN,
  77. nonce, firstkey) != 0) {
  78. printf("crypto_aead_xchacha20poly1305_ietf_decrypt_detached() failed\n");
  79. }
  80. if (memcmp(m, m2, MLEN) != 0) {
  81. printf("detached m != m2\n");
  82. }
  83. for (i = 0U; i < CLEN; i++) {
  84. c[i] ^= (i + 1U);
  85. if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, NULL, NULL, c, CLEN,
  86. ad, ADLEN, nonce, firstkey)
  87. == 0 || memcmp(m, m2, MLEN) == 0) {
  88. printf("message can be forged\n");
  89. }
  90. c[i] ^= (i + 1U);
  91. }
  92. crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, m, MLEN,
  93. NULL, 0U, NULL, nonce, firstkey);
  94. if (found_clen != CLEN) {
  95. printf("clen is not properly set (adlen=0)\n");
  96. }
  97. for (i = 0U; i < CLEN; ++i) {
  98. printf(",0x%02x", (unsigned int) c[i]);
  99. if (i % 8 == 7) {
  100. printf("\n");
  101. }
  102. }
  103. printf("\n");
  104. if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, CLEN,
  105. NULL, 0U, nonce, firstkey) != 0) {
  106. printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
  107. }
  108. if (m2len != MLEN) {
  109. printf("m2len is not properly set (adlen=0)\n");
  110. }
  111. if (memcmp(m, m2, MLEN) != 0) {
  112. printf("m != m2 (adlen=0)\n");
  113. }
  114. m2len = 1;
  115. if (crypto_aead_xchacha20poly1305_ietf_decrypt(
  116. m2, &m2len, NULL, NULL,
  117. randombytes_uniform(crypto_aead_xchacha20poly1305_ietf_ABYTES),
  118. NULL, 0U, nonce, firstkey) != -1) {
  119. printf("crypto_aead_xchacha20poly1305_ietf_decrypt() worked with a short "
  120. "ciphertext\n");
  121. }
  122. if (m2len != 0) {
  123. printf("Message length should have been set to zero after a failure\n");
  124. }
  125. m2len = 1;
  126. if (crypto_aead_xchacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U,
  127. nonce, firstkey) != -1) {
  128. printf("crypto_aead_xchacha20poly1305_ietf_decrypt() worked with an empty "
  129. "ciphertext\n");
  130. }
  131. if (m2len != 0) {
  132. printf("Message length should have been set to zero after a failure\n");
  133. }
  134. memcpy(c, m, MLEN);
  135. crypto_aead_xchacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN,
  136. NULL, 0U, NULL, nonce, firstkey);
  137. if (found_clen != CLEN) {
  138. printf("clen is not properly set (adlen=0)\n");
  139. }
  140. for (i = 0U; i < CLEN; ++i) {
  141. printf(",0x%02x", (unsigned int) c[i]);
  142. if (i % 8 == 7) {
  143. printf("\n");
  144. }
  145. }
  146. printf("\n");
  147. if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
  148. NULL, 0U, nonce, firstkey) != 0) {
  149. printf("crypto_aead_xchacha20poly1305_ietf_decrypt() failed (adlen=0)\n");
  150. }
  151. if (m2len != MLEN) {
  152. printf("m2len is not properly set (adlen=0)\n");
  153. }
  154. if (memcmp(m, c, MLEN) != 0) {
  155. printf("m != c (adlen=0)\n");
  156. }
  157. crypto_aead_xchacha20poly1305_ietf_keygen(key2);
  158. if (crypto_aead_xchacha20poly1305_ietf_decrypt(c, &m2len, NULL, c, CLEN,
  159. NULL, 0U, nonce, key2) == 0) {
  160. printf("crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed\n");
  161. }
  162. sodium_free(c);
  163. sodium_free(detached_c);
  164. sodium_free(key2);
  165. sodium_free(mac);
  166. sodium_free(m2);
  167. sodium_free(m);
  168. assert(crypto_aead_xchacha20poly1305_ietf_keybytes() == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
  169. assert(crypto_aead_xchacha20poly1305_ietf_npubbytes() == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
  170. assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == 0U);
  171. assert(crypto_aead_xchacha20poly1305_ietf_nsecbytes() == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);
  172. assert(crypto_aead_xchacha20poly1305_ietf_messagebytes_max() == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
  173. assert(crypto_aead_xchacha20poly1305_IETF_KEYBYTES == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
  174. assert(crypto_aead_xchacha20poly1305_IETF_NSECBYTES == crypto_aead_xchacha20poly1305_ietf_NSECBYTES);
  175. assert(crypto_aead_xchacha20poly1305_IETF_NPUBBYTES == crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
  176. assert(crypto_aead_xchacha20poly1305_IETF_ABYTES == crypto_aead_xchacha20poly1305_ietf_ABYTES);
  177. assert(crypto_aead_xchacha20poly1305_IETF_MESSAGEBYTES_MAX == crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
  178. return 0;
  179. }
  180. int
  181. main(void)
  182. {
  183. tv();
  184. return 0;
  185. }