chacha20poly1305.core.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2016-2023 DeNA Co., Ltd., Kazuho Oku, Lars Eggert, Christian
  3. Huitema, Fastly
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to
  7. * deal in the Software without restriction, including without limitation the
  8. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. * sell copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include <stddef.h>
  24. #include "picotls.h"
  25. #define CHACHA20POLY1305_BLOCKSIZE 64
  26. struct chacha20poly1305_context_t {
  27. ptls_aead_context_t super;
  28. ptls_cipher_context_t *chacha;
  29. uint8_t static_iv[PTLS_CHACHA20POLY1305_IV_SIZE];
  30. size_t aadlen;
  31. size_t textlen;
  32. void (*poly1305_init)(struct chacha20poly1305_context_t *, const void *);
  33. void (*poly1305_update)(struct chacha20poly1305_context_t *, const void *, size_t);
  34. void (*poly1305_finish)(struct chacha20poly1305_context_t *, void *);
  35. };
  36. static void chacha20poly1305_write_u64(uint8_t *buf, uint64_t v)
  37. {
  38. *buf++ = v & 0xff;
  39. *buf++ = (v >> 8) & 0xff;
  40. *buf++ = (v >> 16) & 0xff;
  41. *buf++ = (v >> 24) & 0xff;
  42. *buf++ = (v >> 32) & 0xff;
  43. *buf++ = (v >> 40) & 0xff;
  44. *buf++ = (v >> 48) & 0xff;
  45. *buf = (v >> 56) & 0xff;
  46. }
  47. static void chacha20poly1305_encrypt_pad(struct chacha20poly1305_context_t *ctx, size_t n)
  48. {
  49. static const uint8_t zeros[16] = {0};
  50. if (n % 16 != 0)
  51. ctx->poly1305_update(ctx, zeros, 16 - (n % 16));
  52. }
  53. static void chacha20poly1305_finalize(struct chacha20poly1305_context_t *ctx, uint8_t *tag)
  54. {
  55. uint8_t lenbuf[16];
  56. chacha20poly1305_encrypt_pad(ctx, ctx->textlen);
  57. chacha20poly1305_write_u64(lenbuf, ctx->aadlen);
  58. chacha20poly1305_write_u64(lenbuf + 8, ctx->textlen);
  59. ctx->poly1305_update(ctx, lenbuf, sizeof(lenbuf));
  60. ctx->poly1305_finish(ctx, tag);
  61. }
  62. static void chacha20poly1305_dispose_crypto(ptls_aead_context_t *_ctx)
  63. {
  64. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  65. ptls_cipher_free(ctx->chacha);
  66. }
  67. static void chacha20poly1305_init(ptls_aead_context_t *_ctx, uint64_t seq, const void *aad, size_t aadlen)
  68. {
  69. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  70. uint8_t tmpbuf[CHACHA20POLY1305_BLOCKSIZE];
  71. /* init chacha */
  72. memset(tmpbuf, 0, 16 - PTLS_CHACHA20POLY1305_IV_SIZE);
  73. ptls_aead__build_iv(ctx->super.algo, tmpbuf + 16 - PTLS_CHACHA20POLY1305_IV_SIZE, ctx->static_iv, seq);
  74. ptls_cipher_init(ctx->chacha, tmpbuf);
  75. /* init poly1305 */
  76. memset(tmpbuf, 0, sizeof(tmpbuf));
  77. ptls_cipher_encrypt(ctx->chacha, tmpbuf, tmpbuf, CHACHA20POLY1305_BLOCKSIZE);
  78. ctx->poly1305_init(ctx, tmpbuf);
  79. ptls_clear_memory(tmpbuf, sizeof(tmpbuf));
  80. /* aad */
  81. if (aadlen != 0) {
  82. ctx->poly1305_update(ctx, aad, aadlen);
  83. chacha20poly1305_encrypt_pad(ctx, aadlen);
  84. }
  85. ctx->aadlen = aadlen;
  86. ctx->textlen = 0;
  87. }
  88. static size_t chacha20poly1305_encrypt_update(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen)
  89. {
  90. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  91. ptls_cipher_encrypt(ctx->chacha, output, input, inlen);
  92. ctx->poly1305_update(ctx, output, inlen);
  93. ctx->textlen += inlen;
  94. return inlen;
  95. }
  96. static size_t chacha20poly1305_encrypt_final(ptls_aead_context_t *_ctx, void *output)
  97. {
  98. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  99. chacha20poly1305_finalize(ctx, output);
  100. return PTLS_CHACHA20POLY1305_TAG_SIZE;
  101. }
  102. static size_t chacha20poly1305_decrypt(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen, uint64_t seq,
  103. const void *aad, size_t aadlen)
  104. {
  105. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  106. uint8_t tag[PTLS_CHACHA20POLY1305_TAG_SIZE];
  107. size_t ret;
  108. if (inlen < sizeof(tag))
  109. return SIZE_MAX;
  110. chacha20poly1305_init(&ctx->super, seq, aad, aadlen);
  111. ctx->poly1305_update(ctx, input, inlen - sizeof(tag));
  112. ctx->textlen = inlen - sizeof(tag);
  113. chacha20poly1305_finalize(ctx, tag);
  114. if (ptls_mem_equal(tag, (const uint8_t *)input + inlen - sizeof(tag), sizeof(tag))) {
  115. ptls_cipher_encrypt(ctx->chacha, output, input, inlen - sizeof(tag));
  116. ret = inlen - sizeof(tag);
  117. } else {
  118. ret = SIZE_MAX;
  119. }
  120. ptls_clear_memory(tag, sizeof(tag));
  121. return ret;
  122. }
  123. static void chacha20poly1305_get_iv(ptls_aead_context_t *_ctx, void *iv)
  124. {
  125. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  126. memcpy(iv, ctx->static_iv, sizeof(ctx->static_iv));
  127. }
  128. static void chacha20poly1305_set_iv(ptls_aead_context_t *_ctx, const void *iv)
  129. {
  130. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  131. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  132. }
  133. static int chacha20poly1305_setup_crypto(ptls_aead_context_t *_ctx, int is_enc, const void *key, const void *iv,
  134. ptls_cipher_algorithm_t *chacha,
  135. void (*poly1305_init)(struct chacha20poly1305_context_t *, const void *),
  136. void (*poly1305_update)(struct chacha20poly1305_context_t *, const void *, size_t),
  137. void (*poly1305_finish)(struct chacha20poly1305_context_t *, void *))
  138. {
  139. struct chacha20poly1305_context_t *ctx = (struct chacha20poly1305_context_t *)_ctx;
  140. ctx->super.dispose_crypto = chacha20poly1305_dispose_crypto;
  141. ctx->super.do_get_iv = chacha20poly1305_get_iv;
  142. ctx->super.do_set_iv = chacha20poly1305_set_iv;
  143. if (is_enc) {
  144. ctx->super.do_encrypt_init = chacha20poly1305_init;
  145. ctx->super.do_encrypt_update = chacha20poly1305_encrypt_update;
  146. ctx->super.do_encrypt_final = chacha20poly1305_encrypt_final;
  147. ctx->super.do_encrypt = ptls_aead__do_encrypt;
  148. ctx->super.do_encrypt_v = ptls_aead__do_encrypt_v;
  149. ctx->super.do_decrypt = NULL;
  150. } else {
  151. ctx->super.do_encrypt_init = NULL;
  152. ctx->super.do_encrypt_update = NULL;
  153. ctx->super.do_encrypt_final = NULL;
  154. ctx->super.do_encrypt = NULL;
  155. ctx->super.do_encrypt_v = NULL;
  156. ctx->super.do_decrypt = chacha20poly1305_decrypt;
  157. }
  158. if ((ctx->chacha = ptls_cipher_new(chacha, is_enc, key)) == NULL)
  159. return PTLS_ERROR_LIBRARY;
  160. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  161. ctx->poly1305_init = poly1305_init;
  162. ctx->poly1305_update = poly1305_update;
  163. ctx->poly1305_finish = poly1305_finish;
  164. return 0;
  165. }