Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

221 wiersze
7.5 KiB

  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 <openssl/chacha.h>
  16. #include <openssl/cipher.h>
  17. #include <openssl/err.h>
  18. #include <openssl/mem.h>
  19. #include <openssl/poly1305.h>
  20. #include "internal.h"
  21. #define POLY1305_TAG_LEN 16
  22. #define CHACHA20_NONCE_LEN 8
  23. struct aead_chacha20_poly1305_ctx {
  24. unsigned char key[32];
  25. unsigned char tag_len;
  26. };
  27. static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  28. size_t key_len, size_t tag_len) {
  29. struct aead_chacha20_poly1305_ctx *c20_ctx;
  30. if (tag_len == 0) {
  31. tag_len = POLY1305_TAG_LEN;
  32. }
  33. if (tag_len > POLY1305_TAG_LEN) {
  34. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_init, CIPHER_R_TOO_LARGE);
  35. return 0;
  36. }
  37. if (key_len != sizeof(c20_ctx->key)) {
  38. return 0; /* internal error - EVP_AEAD_CTX_init should catch this. */
  39. }
  40. c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
  41. if (c20_ctx == NULL) {
  42. return 0;
  43. }
  44. memcpy(c20_ctx->key, key, key_len);
  45. c20_ctx->tag_len = tag_len;
  46. ctx->aead_state = c20_ctx;
  47. return 1;
  48. }
  49. static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
  50. struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  51. OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key));
  52. OPENSSL_free(c20_ctx);
  53. }
  54. static void poly1305_update_with_length(poly1305_state *poly1305,
  55. const uint8_t *data, size_t data_len) {
  56. size_t j = data_len;
  57. uint8_t length_bytes[8];
  58. unsigned i;
  59. for (i = 0; i < sizeof(length_bytes); i++) {
  60. length_bytes[i] = j;
  61. j >>= 8;
  62. }
  63. CRYPTO_poly1305_update(poly1305, data, data_len);
  64. CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
  65. }
  66. #if __arm__
  67. #define ALIGNED __attribute__((aligned(16)))
  68. #else
  69. #define ALIGNED
  70. #endif
  71. static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  72. size_t *out_len, size_t max_out_len,
  73. const uint8_t *nonce, size_t nonce_len,
  74. const uint8_t *in, size_t in_len,
  75. const uint8_t *ad, size_t ad_len) {
  76. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  77. uint8_t poly1305_key[32] ALIGNED;
  78. poly1305_state poly1305;
  79. const uint64_t in_len_64 = in_len;
  80. /* The underlying ChaCha implementation may not overflow the block
  81. * counter into the second counter word. Therefore we disallow
  82. * individual operations that work on more than 256GB at a time.
  83. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  84. * 32-bits and this produces a warning because it's always false.
  85. * Casting to uint64_t inside the conditional is not sufficient to stop
  86. * the warning. */
  87. if (in_len_64 >= (1ull << 32) * 64 - 64) {
  88. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_seal, CIPHER_R_TOO_LARGE);
  89. return 0;
  90. }
  91. if (in_len + c20_ctx->tag_len < in_len) {
  92. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_seal, CIPHER_R_TOO_LARGE);
  93. return 0;
  94. }
  95. if (max_out_len < in_len + c20_ctx->tag_len) {
  96. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_seal,
  97. CIPHER_R_BUFFER_TOO_SMALL);
  98. return 0;
  99. }
  100. if (nonce_len != CHACHA20_NONCE_LEN) {
  101. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_seal, 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. if (c20_ctx->tag_len != POLY1305_TAG_LEN) {
  112. uint8_t tag[POLY1305_TAG_LEN];
  113. CRYPTO_poly1305_finish(&poly1305, tag);
  114. memcpy(out + in_len, tag, c20_ctx->tag_len);
  115. *out_len = in_len + c20_ctx->tag_len;
  116. return 1;
  117. }
  118. CRYPTO_poly1305_finish(&poly1305, out + in_len);
  119. *out_len = in_len + c20_ctx->tag_len;
  120. return 1;
  121. }
  122. static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  123. size_t *out_len, size_t max_out_len,
  124. const uint8_t *nonce, size_t nonce_len,
  125. const uint8_t *in, size_t in_len,
  126. const uint8_t *ad, size_t ad_len) {
  127. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  128. uint8_t mac[POLY1305_TAG_LEN];
  129. uint8_t poly1305_key[32] ALIGNED;
  130. size_t plaintext_len;
  131. poly1305_state poly1305;
  132. const uint64_t in_len_64 = in_len;
  133. if (in_len < c20_ctx->tag_len) {
  134. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_open, CIPHER_R_BAD_DECRYPT);
  135. return 0;
  136. }
  137. /* The underlying ChaCha implementation may not overflow the block
  138. * counter into the second counter word. Therefore we disallow
  139. * individual operations that work on more than 256GB at a time.
  140. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  141. * 32-bits and this produces a warning because it's always false.
  142. * Casting to uint64_t inside the conditional is not sufficient to stop
  143. * the warning. */
  144. if (in_len_64 >= (1ull << 32) * 64 - 64) {
  145. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_open, CIPHER_R_TOO_LARGE);
  146. return 0;
  147. }
  148. if (nonce_len != CHACHA20_NONCE_LEN) {
  149. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_open, CIPHER_R_IV_TOO_LARGE);
  150. return 0;
  151. }
  152. plaintext_len = in_len - c20_ctx->tag_len;
  153. if (max_out_len < plaintext_len) {
  154. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_open,
  155. CIPHER_R_BUFFER_TOO_SMALL);
  156. return 0;
  157. }
  158. memset(poly1305_key, 0, sizeof(poly1305_key));
  159. CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
  160. c20_ctx->key, nonce, 0);
  161. CRYPTO_poly1305_init(&poly1305, poly1305_key);
  162. poly1305_update_with_length(&poly1305, ad, ad_len);
  163. poly1305_update_with_length(&poly1305, in, plaintext_len);
  164. CRYPTO_poly1305_finish(&poly1305, mac);
  165. if (CRYPTO_memcmp(mac, in + plaintext_len, c20_ctx->tag_len) != 0) {
  166. OPENSSL_PUT_ERROR(CIPHER, aead_chacha20_poly1305_open, CIPHER_R_BAD_DECRYPT);
  167. return 0;
  168. }
  169. CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1);
  170. *out_len = plaintext_len;
  171. return 1;
  172. }
  173. static const EVP_AEAD aead_chacha20_poly1305 = {
  174. 32, /* key len */
  175. CHACHA20_NONCE_LEN, /* nonce len */
  176. POLY1305_TAG_LEN, /* overhead */
  177. POLY1305_TAG_LEN, /* max tag length */
  178. aead_chacha20_poly1305_init, aead_chacha20_poly1305_cleanup,
  179. aead_chacha20_poly1305_seal, aead_chacha20_poly1305_open,
  180. };
  181. const EVP_AEAD *EVP_aead_chacha20_poly1305() { return &aead_chacha20_poly1305; }