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.

128 lines
4.6 KiB

  1. #define TEST_NAME "aead_chacha20poly1305"
  2. #include "cmptest.h"
  3. static unsigned char firstkey[crypto_aead_chacha20poly1305_KEYBYTES]
  4. = { 0x42, 0x90, 0xbc, 0xb1, 0x54, 0x17, 0x35, 0x31, 0xf3, 0x14, 0xaf,
  5. 0x57, 0xf3, 0xbe, 0x3b, 0x50, 0x06, 0xda, 0x37, 0x1e, 0xce, 0x27,
  6. 0x2a, 0xfa, 0x1b, 0x5d, 0xbd, 0xd1, 0x10, 0x0a, 0x10, 0x07 };
  7. static unsigned char m[10U]
  8. = { 0x86, 0xd0, 0x99, 0x74, 0x84, 0x0b, 0xde, 0xd2, 0xa5, 0xca };
  9. static unsigned char nonce[crypto_aead_chacha20poly1305_NPUBBYTES]
  10. = { 0xcd, 0x7c, 0xf6, 0x7b, 0xe3, 0x9c, 0x79, 0x4a };
  11. static unsigned char ad[10U]
  12. = { 0x87, 0xe2, 0x29, 0xd4, 0x50, 0x08, 0x45, 0xa0, 0x79, 0xc0 };
  13. static unsigned char c[10U + crypto_aead_chacha20poly1305_ABYTES];
  14. int main(void)
  15. {
  16. unsigned char m2[10U];
  17. unsigned long long clen;
  18. unsigned long long m2len;
  19. size_t i;
  20. crypto_aead_chacha20poly1305_encrypt(c, &clen, m, sizeof m, ad, sizeof ad,
  21. NULL, nonce, firstkey);
  22. if (clen != sizeof m + crypto_aead_chacha20poly1305_abytes()) {
  23. printf("clen is not properly set\n");
  24. }
  25. for (i = 0U; i < sizeof c; ++i) {
  26. printf(",0x%02x", (unsigned int)c[i]);
  27. if (i % 8 == 7) {
  28. printf("\n");
  29. }
  30. }
  31. printf("\n");
  32. if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, sizeof c, ad,
  33. sizeof ad, nonce, firstkey) != 0) {
  34. printf("crypto_aead_chacha20poly1305_decrypt() failed\n");
  35. }
  36. if (m2len != sizeof c - crypto_aead_chacha20poly1305_abytes()) {
  37. printf("m2len is not properly set\n");
  38. }
  39. if (memcmp(m, m2, sizeof m) != 0) {
  40. printf("m != m2\n");
  41. }
  42. for (i = 0U; i < sizeof c; i++) {
  43. c[i] ^= (i + 1U);
  44. if (crypto_aead_chacha20poly1305_decrypt(m2, NULL, NULL, c, sizeof c,
  45. ad, sizeof ad, nonce, firstkey)
  46. == 0 || memcmp(m, m2, sizeof m) == 0) {
  47. printf("message can be forged\n");
  48. }
  49. c[i] ^= (i + 1U);
  50. }
  51. crypto_aead_chacha20poly1305_encrypt(c, &clen, m, sizeof m, NULL, 0U, NULL,
  52. nonce, firstkey);
  53. if (clen != sizeof m + crypto_aead_chacha20poly1305_abytes()) {
  54. printf("clen is not properly set (adlen=0)\n");
  55. }
  56. for (i = 0U; i < sizeof c; ++i) {
  57. printf(",0x%02x", (unsigned int)c[i]);
  58. if (i % 8 == 7) {
  59. printf("\n");
  60. }
  61. }
  62. printf("\n");
  63. if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, sizeof c,
  64. NULL, 0U, nonce, firstkey) != 0) {
  65. printf("crypto_aead_chacha20poly1305_decrypt() failed (adlen=0)\n");
  66. }
  67. if (m2len != sizeof c - crypto_aead_chacha20poly1305_abytes()) {
  68. printf("m2len is not properly set (adlen=0)\n");
  69. }
  70. if (memcmp(m, m2, sizeof m) != 0) {
  71. printf("m != m2 (adlen=0)\n");
  72. }
  73. if (crypto_aead_chacha20poly1305_decrypt(
  74. m2, &m2len, NULL, c, crypto_aead_chacha20poly1305_ABYTES / 2, NULL,
  75. 0U, nonce, firstkey) != -1) {
  76. printf("crypto_aead_chacha20poly1305_decrypt() worked with a short "
  77. "ciphertext\n");
  78. }
  79. if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U,
  80. nonce, firstkey) != -1) {
  81. printf("crypto_aead_chacha20poly1305_decrypt() worked with an empty "
  82. "ciphertext\n");
  83. }
  84. memcpy(c, m, sizeof m);
  85. crypto_aead_chacha20poly1305_encrypt(c, &clen, c, sizeof m, NULL, 0U, NULL,
  86. nonce, firstkey);
  87. if (clen != sizeof m + crypto_aead_chacha20poly1305_abytes()) {
  88. printf("clen is not properly set (adlen=0)\n");
  89. }
  90. for (i = 0U; i < sizeof c; ++i) {
  91. printf(",0x%02x", (unsigned int)c[i]);
  92. if (i % 8 == 7) {
  93. printf("\n");
  94. }
  95. }
  96. printf("\n");
  97. if (crypto_aead_chacha20poly1305_decrypt(c, &m2len, NULL, c, sizeof c,
  98. NULL, 0U, nonce, firstkey) != 0) {
  99. printf("crypto_aead_chacha20poly1305_decrypt() failed (adlen=0)\n");
  100. }
  101. if (m2len != sizeof c - crypto_aead_chacha20poly1305_abytes()) {
  102. printf("m2len is not properly set (adlen=0)\n");
  103. }
  104. if (memcmp(m, c, sizeof m) != 0) {
  105. printf("m != c (adlen=0)\n");
  106. }
  107. assert(crypto_aead_chacha20poly1305_keybytes() > 0U);
  108. assert(crypto_aead_chacha20poly1305_npubbytes() > 0U);
  109. assert(crypto_aead_chacha20poly1305_nsecbytes() == 0U);
  110. return 0;
  111. }