e_chacha20poly1305.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/cpu.h>
  19. #include <openssl/err.h>
  20. #include <openssl/mem.h>
  21. #include <openssl/poly1305.h>
  22. #include "internal.h"
  23. #include "../internal.h"
  24. #define POLY1305_TAG_LEN 16
  25. struct aead_chacha20_poly1305_ctx {
  26. unsigned char key[32];
  27. unsigned char tag_len;
  28. };
  29. #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
  30. !defined(OPENSSL_WINDOWS)
  31. static int asm_capable(void) {
  32. const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
  33. return sse41_capable;
  34. }
  35. // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It
  36. // decrypts |plaintext_len| bytes from |ciphertext| and writes them to
  37. // |out_plaintext|. On entry, |aead_data| must contain the final 48 bytes of
  38. // the initial ChaCha20 block, i.e. the key, followed by four zeros, followed
  39. // by the nonce. On exit, it will contain the calculated tag value, which the
  40. // caller must check.
  41. extern void chacha20_poly1305_open(uint8_t *out_plaintext,
  42. const uint8_t *ciphertext,
  43. size_t plaintext_len, const uint8_t *ad,
  44. size_t ad_len, uint8_t *aead_data);
  45. // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It
  46. // encrypts |plaintext_len| bytes from |plaintext| and writes them to
  47. // |out_ciphertext|. On entry, |aead_data| must contain the final 48 bytes of
  48. // the initial ChaCha20 block, i.e. the key, followed by four zeros, followed
  49. // by the nonce. On exit, it will contain the calculated tag value, which the
  50. // caller must append to the ciphertext.
  51. extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
  52. const uint8_t *plaintext,
  53. size_t plaintext_len, const uint8_t *ad,
  54. size_t ad_len, uint8_t *aead_data);
  55. #else
  56. static int asm_capable(void) {
  57. return 0;
  58. }
  59. static void chacha20_poly1305_open(uint8_t *out_plaintext,
  60. const uint8_t *ciphertext,
  61. size_t plaintext_len, const uint8_t *ad,
  62. size_t ad_len, uint8_t *aead_data) {}
  63. static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
  64. const uint8_t *plaintext,
  65. size_t plaintext_len, const uint8_t *ad,
  66. size_t ad_len, uint8_t *aead_data) {}
  67. #endif
  68. static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  69. size_t key_len, size_t tag_len) {
  70. struct aead_chacha20_poly1305_ctx *c20_ctx;
  71. if (tag_len == 0) {
  72. tag_len = POLY1305_TAG_LEN;
  73. }
  74. if (tag_len > POLY1305_TAG_LEN) {
  75. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  76. return 0;
  77. }
  78. if (key_len != sizeof(c20_ctx->key)) {
  79. return 0; /* internal error - EVP_AEAD_CTX_init should catch this. */
  80. }
  81. c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
  82. if (c20_ctx == NULL) {
  83. return 0;
  84. }
  85. OPENSSL_memcpy(c20_ctx->key, key, key_len);
  86. c20_ctx->tag_len = tag_len;
  87. ctx->aead_state = c20_ctx;
  88. return 1;
  89. }
  90. static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
  91. struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  92. OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key));
  93. OPENSSL_free(c20_ctx);
  94. }
  95. static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) {
  96. uint8_t length_bytes[8];
  97. for (unsigned i = 0; i < sizeof(length_bytes); i++) {
  98. length_bytes[i] = data_len;
  99. data_len >>= 8;
  100. }
  101. CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
  102. }
  103. static void poly1305_update_padded_16(poly1305_state *poly1305,
  104. const uint8_t *data, size_t data_len) {
  105. static const uint8_t padding[16] = { 0 }; /* Padding is all zeros. */
  106. CRYPTO_poly1305_update(poly1305, data, data_len);
  107. if (data_len % 16 != 0) {
  108. CRYPTO_poly1305_update(poly1305, padding,
  109. sizeof(padding) - (data_len % 16));
  110. }
  111. }
  112. /* calc_tag fills |tag| with the authentication tag for the given inputs. */
  113. static void calc_tag(uint8_t tag[POLY1305_TAG_LEN],
  114. const struct aead_chacha20_poly1305_ctx *c20_ctx,
  115. const uint8_t nonce[12], const uint8_t *ad, size_t ad_len,
  116. const uint8_t *ciphertext, size_t ciphertext_len) {
  117. alignas(16) uint8_t poly1305_key[32];
  118. OPENSSL_memset(poly1305_key, 0, sizeof(poly1305_key));
  119. CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
  120. c20_ctx->key, nonce, 0);
  121. poly1305_state ctx;
  122. CRYPTO_poly1305_init(&ctx, poly1305_key);
  123. poly1305_update_padded_16(&ctx, ad, ad_len);
  124. poly1305_update_padded_16(&ctx, ciphertext, ciphertext_len);
  125. poly1305_update_length(&ctx, ad_len);
  126. poly1305_update_length(&ctx, ciphertext_len);
  127. CRYPTO_poly1305_finish(&ctx, tag);
  128. }
  129. static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
  130. size_t *out_len, size_t max_out_len,
  131. const uint8_t *nonce, size_t nonce_len,
  132. const uint8_t *in, size_t in_len,
  133. const uint8_t *ad, size_t ad_len) {
  134. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  135. const uint64_t in_len_64 = in_len;
  136. if (nonce_len != 12) {
  137. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  138. return 0;
  139. }
  140. /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  141. * individual operations that work on more than 256GB at a time.
  142. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  143. * 32-bits and this produces a warning because it's always false.
  144. * Casting to uint64_t inside the conditional is not sufficient to stop
  145. * the warning. */
  146. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  147. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  148. return 0;
  149. }
  150. if (in_len + c20_ctx->tag_len < in_len) {
  151. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  152. return 0;
  153. }
  154. if (max_out_len < in_len + c20_ctx->tag_len) {
  155. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  156. return 0;
  157. }
  158. alignas(16) uint8_t tag[48];
  159. if (asm_capable()) {
  160. OPENSSL_memcpy(tag, c20_ctx->key, 32);
  161. OPENSSL_memset(tag + 32, 0, 4);
  162. OPENSSL_memcpy(tag + 32 + 4, nonce, 12);
  163. chacha20_poly1305_seal(out, in, in_len, ad, ad_len, tag);
  164. } else {
  165. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  166. calc_tag(tag, c20_ctx, nonce, ad, ad_len, out, in_len);
  167. }
  168. OPENSSL_memcpy(out + in_len, tag, c20_ctx->tag_len);
  169. *out_len = in_len + c20_ctx->tag_len;
  170. return 1;
  171. }
  172. static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
  173. size_t *out_len, size_t max_out_len,
  174. const uint8_t *nonce, size_t nonce_len,
  175. const uint8_t *in, size_t in_len,
  176. const uint8_t *ad, size_t ad_len) {
  177. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  178. size_t plaintext_len;
  179. const uint64_t in_len_64 = in_len;
  180. if (nonce_len != 12) {
  181. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  182. return 0;
  183. }
  184. if (in_len < c20_ctx->tag_len) {
  185. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  186. return 0;
  187. }
  188. /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  189. * individual operations that work on more than 256GB at a time.
  190. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  191. * 32-bits and this produces a warning because it's always false.
  192. * Casting to uint64_t inside the conditional is not sufficient to stop
  193. * the warning. */
  194. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  195. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  196. return 0;
  197. }
  198. plaintext_len = in_len - c20_ctx->tag_len;
  199. alignas(16) uint8_t tag[48];
  200. if (asm_capable()) {
  201. OPENSSL_memcpy(tag, c20_ctx->key, 32);
  202. OPENSSL_memset(tag + 32, 0, 4);
  203. OPENSSL_memcpy(tag + 32 + 4, nonce, 12);
  204. chacha20_poly1305_open(out, in, plaintext_len, ad, ad_len, tag);
  205. } else {
  206. calc_tag(tag, c20_ctx, nonce, ad, ad_len, in, plaintext_len);
  207. CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1);
  208. }
  209. if (CRYPTO_memcmp(tag, in + plaintext_len, c20_ctx->tag_len) != 0) {
  210. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  211. return 0;
  212. }
  213. *out_len = plaintext_len;
  214. return 1;
  215. }
  216. static const EVP_AEAD aead_chacha20_poly1305 = {
  217. 32, /* key len */
  218. 12, /* nonce len */
  219. POLY1305_TAG_LEN, /* overhead */
  220. POLY1305_TAG_LEN, /* max tag length */
  221. aead_chacha20_poly1305_init,
  222. NULL, /* init_with_direction */
  223. aead_chacha20_poly1305_cleanup,
  224. aead_chacha20_poly1305_seal,
  225. aead_chacha20_poly1305_open,
  226. NULL, /* get_iv */
  227. };
  228. const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
  229. return &aead_chacha20_poly1305;
  230. }