Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

e_chacha20poly1305.c 7.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/aead.h>
  15. #include <string.h>
  16. #include <openssl/chacha.h>
  17. #include <openssl/cipher.h>
  18. #include <openssl/err.h>
  19. #include <openssl/mem.h>
  20. #include <openssl/poly1305.h>
  21. #include "internal.h"
  22. #define POLY1305_TAG_LEN 16
  23. #define CHACHA20_NONCE_LEN 8
  24. struct aead_chacha20_poly1305_ctx {
  25. unsigned char key[32];
  26. unsigned char tag_len;
  27. };
  28. static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  29. size_t key_len, size_t tag_len) {
  30. struct aead_chacha20_poly1305_ctx *c20_ctx;
  31. if (tag_len == 0) {
  32. tag_len = POLY1305_TAG_LEN;
  33. }
  34. if (tag_len > POLY1305_TAG_LEN) {
  35. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  36. return 0;
  37. }
  38. if (key_len != sizeof(c20_ctx->key)) {
  39. return 0; /* internal error - EVP_AEAD_CTX_init should catch this. */
  40. }
  41. c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
  42. if (c20_ctx == NULL) {
  43. return 0;
  44. }
  45. memcpy(c20_ctx->key, key, key_len);
  46. c20_ctx->tag_len = tag_len;
  47. ctx->aead_state = c20_ctx;
  48. return 1;
  49. }
  50. static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
  51. struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  52. OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key));
  53. OPENSSL_free(c20_ctx);
  54. }
  55. static void poly1305_update_with_length(poly1305_state *poly1305,
  56. const uint8_t *data, size_t data_len) {
  57. size_t j = data_len;
  58. uint8_t length_bytes[8];
  59. unsigned i;
  60. for (i = 0; i < sizeof(length_bytes); i++) {
  61. length_bytes[i] = j;
  62. j >>= 8;
  63. }
  64. CRYPTO_poly1305_update(poly1305, data, data_len);
  65. CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
  66. }
  67. #if defined(__arm__)
  68. #define ALIGNED __attribute__((aligned(16)))
  69. #else
  70. #define ALIGNED
  71. #endif
  72. static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  73. size_t *out_len, size_t max_out_len,
  74. const uint8_t *nonce, size_t nonce_len,
  75. const uint8_t *in, size_t in_len,
  76. const uint8_t *ad, size_t ad_len) {
  77. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  78. uint8_t poly1305_key[32] ALIGNED;
  79. poly1305_state poly1305;
  80. const uint64_t in_len_64 = in_len;
  81. /* The underlying ChaCha implementation may not overflow the block
  82. * counter into the second counter word. Therefore we disallow
  83. * individual operations that work on more than 256GB at a time.
  84. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  85. * 32-bits and this produces a warning because it's always false.
  86. * Casting to uint64_t inside the conditional is not sufficient to stop
  87. * the warning. */
  88. if (in_len_64 >= (1ull << 32) * 64 - 64) {
  89. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  90. return 0;
  91. }
  92. if (in_len + c20_ctx->tag_len < in_len) {
  93. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  94. return 0;
  95. }
  96. if (max_out_len < in_len + c20_ctx->tag_len) {
  97. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  98. return 0;
  99. }
  100. if (nonce_len != CHACHA20_NONCE_LEN) {
  101. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE);
  102. return 0;
  103. }
  104. memset(poly1305_key, 0, sizeof(poly1305_key));
  105. CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
  106. c20_ctx->key, nonce, 0);
  107. CRYPTO_poly1305_init(&poly1305, poly1305_key);
  108. poly1305_update_with_length(&poly1305, ad, ad_len);
  109. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  110. poly1305_update_with_length(&poly1305, out, in_len);
  111. uint8_t tag[POLY1305_TAG_LEN] ALIGNED;
  112. CRYPTO_poly1305_finish(&poly1305, tag);
  113. memcpy(out + in_len, tag, c20_ctx->tag_len);
  114. *out_len = in_len + c20_ctx->tag_len;
  115. return 1;
  116. }
  117. static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  118. size_t *out_len, size_t max_out_len,
  119. const uint8_t *nonce, size_t nonce_len,
  120. const uint8_t *in, size_t in_len,
  121. const uint8_t *ad, size_t ad_len) {
  122. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  123. uint8_t mac[POLY1305_TAG_LEN];
  124. uint8_t poly1305_key[32] ALIGNED;
  125. size_t plaintext_len;
  126. poly1305_state poly1305;
  127. const uint64_t in_len_64 = in_len;
  128. if (in_len < c20_ctx->tag_len) {
  129. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  130. return 0;
  131. }
  132. /* The underlying ChaCha implementation may not overflow the block
  133. * counter into the second counter word. Therefore we disallow
  134. * individual operations that work on more than 256GB at a time.
  135. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  136. * 32-bits and this produces a warning because it's always false.
  137. * Casting to uint64_t inside the conditional is not sufficient to stop
  138. * the warning. */
  139. if (in_len_64 >= (1ull << 32) * 64 - 64) {
  140. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  141. return 0;
  142. }
  143. if (nonce_len != CHACHA20_NONCE_LEN) {
  144. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE);
  145. return 0;
  146. }
  147. plaintext_len = in_len - c20_ctx->tag_len;
  148. if (max_out_len < plaintext_len) {
  149. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  150. return 0;
  151. }
  152. memset(poly1305_key, 0, sizeof(poly1305_key));
  153. CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
  154. c20_ctx->key, nonce, 0);
  155. CRYPTO_poly1305_init(&poly1305, poly1305_key);
  156. poly1305_update_with_length(&poly1305, ad, ad_len);
  157. poly1305_update_with_length(&poly1305, in, plaintext_len);
  158. CRYPTO_poly1305_finish(&poly1305, mac);
  159. if (CRYPTO_memcmp(mac, in + plaintext_len, c20_ctx->tag_len) != 0) {
  160. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  161. return 0;
  162. }
  163. CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1);
  164. *out_len = plaintext_len;
  165. return 1;
  166. }
  167. static const EVP_AEAD aead_chacha20_poly1305 = {
  168. 32, /* key len */
  169. CHACHA20_NONCE_LEN, /* nonce len */
  170. POLY1305_TAG_LEN, /* overhead */
  171. POLY1305_TAG_LEN, /* max tag length */
  172. aead_chacha20_poly1305_init,
  173. NULL, /* init_with_direction */
  174. aead_chacha20_poly1305_cleanup,
  175. aead_chacha20_poly1305_seal,
  176. aead_chacha20_poly1305_open,
  177. NULL, /* get_rc4_state */
  178. };
  179. const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
  180. return &aead_chacha20_poly1305;
  181. }