Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

301 строка
11 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 <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. #include "../internal.h"
  23. #define POLY1305_TAG_LEN 16
  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_length(poly1305_state *poly1305, size_t data_len) {
  56. uint8_t length_bytes[8];
  57. unsigned i;
  58. for (i = 0; i < sizeof(length_bytes); i++) {
  59. length_bytes[i] = data_len;
  60. data_len >>= 8;
  61. }
  62. CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
  63. }
  64. typedef void (*aead_poly1305_update)(poly1305_state *ctx, const uint8_t *ad,
  65. size_t ad_len, const uint8_t *ciphertext,
  66. size_t ciphertext_len);
  67. /* aead_poly1305 fills |tag| with the authentication tag for the given
  68. * inputs, using |update| to control the order and format that the inputs are
  69. * signed/authenticated. */
  70. static void aead_poly1305(aead_poly1305_update update,
  71. uint8_t tag[POLY1305_TAG_LEN],
  72. const struct aead_chacha20_poly1305_ctx *c20_ctx,
  73. const uint8_t nonce[12], const uint8_t *ad,
  74. size_t ad_len, const uint8_t *ciphertext,
  75. size_t ciphertext_len) {
  76. alignas(16) uint8_t poly1305_key[32];
  77. memset(poly1305_key, 0, sizeof(poly1305_key));
  78. CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
  79. c20_ctx->key, nonce, 0);
  80. poly1305_state ctx;
  81. CRYPTO_poly1305_init(&ctx, poly1305_key);
  82. update(&ctx, ad, ad_len, ciphertext, ciphertext_len);
  83. CRYPTO_poly1305_finish(&ctx, tag);
  84. }
  85. static int seal_impl(aead_poly1305_update poly1305_update,
  86. const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  87. size_t max_out_len, const uint8_t nonce[12],
  88. const uint8_t *in, size_t in_len, const uint8_t *ad,
  89. size_t ad_len) {
  90. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  91. const uint64_t in_len_64 = in_len;
  92. /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  93. * individual operations that work on more than 256GB at a time.
  94. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  95. * 32-bits and this produces a warning because it's always false.
  96. * Casting to uint64_t inside the conditional is not sufficient to stop
  97. * the warning. */
  98. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  99. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  100. return 0;
  101. }
  102. if (in_len + c20_ctx->tag_len < in_len) {
  103. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  104. return 0;
  105. }
  106. if (max_out_len < in_len + c20_ctx->tag_len) {
  107. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  108. return 0;
  109. }
  110. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  111. alignas(16) uint8_t tag[POLY1305_TAG_LEN];
  112. aead_poly1305(poly1305_update, tag, c20_ctx, nonce, ad, ad_len, out, in_len);
  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 open_impl(aead_poly1305_update poly1305_update,
  118. const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
  119. size_t max_out_len, const uint8_t nonce[12],
  120. const uint8_t *in, size_t in_len, const uint8_t *ad,
  121. size_t ad_len) {
  122. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  123. size_t plaintext_len;
  124. const uint64_t in_len_64 = in_len;
  125. if (in_len < c20_ctx->tag_len) {
  126. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  127. return 0;
  128. }
  129. /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  130. * individual operations that work on more than 256GB at a time.
  131. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  132. * 32-bits and this produces a warning because it's always false.
  133. * Casting to uint64_t inside the conditional is not sufficient to stop
  134. * the warning. */
  135. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  136. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  137. return 0;
  138. }
  139. plaintext_len = in_len - c20_ctx->tag_len;
  140. alignas(16) uint8_t tag[POLY1305_TAG_LEN];
  141. aead_poly1305(poly1305_update, tag, c20_ctx, nonce, ad, ad_len, in,
  142. plaintext_len);
  143. if (CRYPTO_memcmp(tag, in + plaintext_len, c20_ctx->tag_len) != 0) {
  144. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  145. return 0;
  146. }
  147. CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1);
  148. *out_len = plaintext_len;
  149. return 1;
  150. }
  151. static void poly1305_update_padded_16(poly1305_state *poly1305,
  152. const uint8_t *data, size_t data_len) {
  153. static const uint8_t padding[16] = { 0 }; /* Padding is all zeros. */
  154. CRYPTO_poly1305_update(poly1305, data, data_len);
  155. if (data_len % 16 != 0) {
  156. CRYPTO_poly1305_update(poly1305, padding, sizeof(padding) - (data_len % 16));
  157. }
  158. }
  159. static void poly1305_update(poly1305_state *ctx, const uint8_t *ad,
  160. size_t ad_len, const uint8_t *ciphertext,
  161. size_t ciphertext_len) {
  162. poly1305_update_padded_16(ctx, ad, ad_len);
  163. poly1305_update_padded_16(ctx, ciphertext, ciphertext_len);
  164. poly1305_update_length(ctx, ad_len);
  165. poly1305_update_length(ctx, ciphertext_len);
  166. }
  167. static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  168. size_t *out_len, size_t max_out_len,
  169. const uint8_t *nonce, size_t nonce_len,
  170. const uint8_t *in, size_t in_len,
  171. const uint8_t *ad, size_t ad_len) {
  172. if (nonce_len != 12) {
  173. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  174. return 0;
  175. }
  176. return seal_impl(poly1305_update, ctx, out, out_len, max_out_len, nonce, in,
  177. in_len, ad, ad_len);
  178. }
  179. static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  180. size_t *out_len, size_t max_out_len,
  181. const uint8_t *nonce, size_t nonce_len,
  182. const uint8_t *in, size_t in_len,
  183. const uint8_t *ad, size_t ad_len) {
  184. if (nonce_len != 12) {
  185. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  186. return 0;
  187. }
  188. return open_impl(poly1305_update, ctx, out, out_len, max_out_len, nonce, in,
  189. in_len, ad, ad_len);
  190. }
  191. static const EVP_AEAD aead_chacha20_poly1305 = {
  192. 32, /* key len */
  193. 12, /* nonce len */
  194. POLY1305_TAG_LEN, /* overhead */
  195. POLY1305_TAG_LEN, /* max tag length */
  196. aead_chacha20_poly1305_init,
  197. NULL, /* init_with_direction */
  198. aead_chacha20_poly1305_cleanup,
  199. aead_chacha20_poly1305_seal,
  200. aead_chacha20_poly1305_open,
  201. NULL, /* get_iv */
  202. };
  203. const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
  204. return &aead_chacha20_poly1305;
  205. }
  206. static void poly1305_update_old(poly1305_state *ctx, const uint8_t *ad,
  207. size_t ad_len, const uint8_t *ciphertext,
  208. size_t ciphertext_len) {
  209. CRYPTO_poly1305_update(ctx, ad, ad_len);
  210. poly1305_update_length(ctx, ad_len);
  211. CRYPTO_poly1305_update(ctx, ciphertext, ciphertext_len);
  212. poly1305_update_length(ctx, ciphertext_len);
  213. }
  214. static int aead_chacha20_poly1305_old_seal(
  215. const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len,
  216. const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len,
  217. const uint8_t *ad, size_t ad_len) {
  218. if (nonce_len != 8) {
  219. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  220. return 0;
  221. }
  222. uint8_t nonce_96[12];
  223. memset(nonce_96, 0, 4);
  224. memcpy(nonce_96 + 4, nonce, 8);
  225. return seal_impl(poly1305_update_old, ctx, out, out_len, max_out_len,
  226. nonce_96, in, in_len, ad, ad_len);
  227. }
  228. static int aead_chacha20_poly1305_old_open(
  229. const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len,
  230. const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len,
  231. const uint8_t *ad, size_t ad_len) {
  232. if (nonce_len != 8) {
  233. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  234. return 0;
  235. }
  236. uint8_t nonce_96[12];
  237. memset(nonce_96, 0, 4);
  238. memcpy(nonce_96 + 4, nonce, 8);
  239. return open_impl(poly1305_update_old, ctx, out, out_len, max_out_len,
  240. nonce_96, in, in_len, ad, ad_len);
  241. }
  242. static const EVP_AEAD aead_chacha20_poly1305_old = {
  243. 32, /* key len */
  244. 8, /* nonce len */
  245. POLY1305_TAG_LEN, /* overhead */
  246. POLY1305_TAG_LEN, /* max tag length */
  247. aead_chacha20_poly1305_init,
  248. NULL, /* init_with_direction */
  249. aead_chacha20_poly1305_cleanup,
  250. aead_chacha20_poly1305_old_seal,
  251. aead_chacha20_poly1305_old_open,
  252. NULL, /* get_iv */
  253. };
  254. const EVP_AEAD *EVP_aead_chacha20_poly1305_old(void) {
  255. return &aead_chacha20_poly1305_old;
  256. }