chacha20.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <stdlib.h>
  23. #include "bitops.h"
  24. #include "handy.h"
  25. #include "poly1305.h"
  26. #include "salsa20.h"
  27. #include "sha2.h"
  28. #include "picotls.h"
  29. #include "picotls/minicrypto.h"
  30. #include "../chacha20poly1305.core.h"
  31. struct chacha20_context_t {
  32. ptls_cipher_context_t super;
  33. cf_chacha20_ctx chacha;
  34. uint8_t key[PTLS_CHACHA20_KEY_SIZE];
  35. };
  36. static void chacha20_dispose(ptls_cipher_context_t *_ctx)
  37. {
  38. struct chacha20_context_t *ctx = (struct chacha20_context_t *)_ctx;
  39. ptls_clear_memory(ctx, sizeof(*ctx));
  40. }
  41. static void chacha20_init(ptls_cipher_context_t *_ctx, const void *iv)
  42. {
  43. struct chacha20_context_t *ctx = (struct chacha20_context_t *)_ctx;
  44. ctx->chacha.nblock = 0;
  45. ctx->chacha.ncounter = 0;
  46. memcpy(ctx->chacha.nonce, iv, sizeof ctx->chacha.nonce);
  47. }
  48. static void chacha20_transform(ptls_cipher_context_t *_ctx, void *output, const void *input, size_t len)
  49. {
  50. struct chacha20_context_t *ctx = (struct chacha20_context_t *)_ctx;
  51. cf_chacha20_cipher(&ctx->chacha, input, output, len);
  52. }
  53. static int chacha20_setup_crypto(ptls_cipher_context_t *_ctx, int is_enc, const void *key)
  54. {
  55. struct chacha20_context_t *ctx = (struct chacha20_context_t *)_ctx;
  56. ctx->super.do_dispose = chacha20_dispose;
  57. ctx->super.do_init = chacha20_init;
  58. ctx->super.do_transform = chacha20_transform;
  59. cf_chacha20_init(&ctx->chacha, key, PTLS_CHACHA20_KEY_SIZE, (const uint8_t *)"01234567" /* not used */);
  60. return 0;
  61. }
  62. struct cifra_chacha20poly1305_context_t {
  63. struct chacha20poly1305_context_t super;
  64. cf_poly1305 poly;
  65. };
  66. static void cifra_poly1305_init(struct chacha20poly1305_context_t *_ctx, const void *rs)
  67. {
  68. struct cifra_chacha20poly1305_context_t *ctx = (struct cifra_chacha20poly1305_context_t *)_ctx;
  69. cf_poly1305_init(&ctx->poly, rs, (const uint8_t *)rs + 16);
  70. }
  71. static void cifra_poly1305_update(struct chacha20poly1305_context_t *_ctx, const void *input, size_t len)
  72. {
  73. struct cifra_chacha20poly1305_context_t *ctx = (struct cifra_chacha20poly1305_context_t *)_ctx;
  74. cf_poly1305_update(&ctx->poly, input, len);
  75. }
  76. static void cifra_poly1305_finish(struct chacha20poly1305_context_t *_ctx, void *tag)
  77. {
  78. struct cifra_chacha20poly1305_context_t *ctx = (struct cifra_chacha20poly1305_context_t *)_ctx;
  79. cf_poly1305_finish(&ctx->poly, tag);
  80. }
  81. static int cifra_chacha20poly1305_setup_crypto(ptls_aead_context_t *ctx, int is_enc, const void *key, const void *iv)
  82. {
  83. return chacha20poly1305_setup_crypto(ctx, is_enc, key, iv, &ptls_minicrypto_chacha20, cifra_poly1305_init,
  84. cifra_poly1305_update, cifra_poly1305_finish);
  85. }
  86. ptls_cipher_algorithm_t ptls_minicrypto_chacha20 = {
  87. "CHACHA20", PTLS_CHACHA20_KEY_SIZE, 1 /* block size */, PTLS_CHACHA20_IV_SIZE, sizeof(struct chacha20_context_t),
  88. chacha20_setup_crypto};
  89. ptls_aead_algorithm_t ptls_minicrypto_chacha20poly1305 = {
  90. "CHACHA20-POLY1305",
  91. PTLS_CHACHA20POLY1305_CONFIDENTIALITY_LIMIT,
  92. PTLS_CHACHA20POLY1305_INTEGRITY_LIMIT,
  93. &ptls_minicrypto_chacha20,
  94. NULL,
  95. PTLS_CHACHA20_KEY_SIZE,
  96. PTLS_CHACHA20POLY1305_IV_SIZE,
  97. PTLS_CHACHA20POLY1305_TAG_SIZE,
  98. {PTLS_TLS12_CHACHAPOLY_FIXED_IV_SIZE, PTLS_TLS12_CHACHAPOLY_RECORD_IV_SIZE},
  99. 0,
  100. 0,
  101. sizeof(struct cifra_chacha20poly1305_context_t),
  102. cifra_chacha20poly1305_setup_crypto};
  103. ptls_cipher_suite_t ptls_minicrypto_chacha20poly1305sha256 = {.id = PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256,
  104. .name = PTLS_CIPHER_SUITE_NAME_CHACHA20_POLY1305_SHA256,
  105. .aead = &ptls_minicrypto_chacha20poly1305,
  106. .hash = &ptls_minicrypto_sha256};