e_chacha20poly1305.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <openssl/type_check.h>
  23. #include "../fipsmodule/cipher/internal.h"
  24. #include "../internal.h"
  25. #define POLY1305_TAG_LEN 16
  26. struct aead_chacha20_poly1305_ctx {
  27. uint8_t key[32];
  28. };
  29. // For convenience (the x86_64 calling convention allows only six parameters in
  30. // registers), the final parameter for the assembly functions is both an input
  31. // and output parameter.
  32. union open_data {
  33. struct {
  34. alignas(16) uint8_t key[32];
  35. uint32_t counter;
  36. uint8_t nonce[12];
  37. } in;
  38. struct {
  39. uint8_t tag[POLY1305_TAG_LEN];
  40. } out;
  41. };
  42. union seal_data {
  43. struct {
  44. alignas(16) uint8_t key[32];
  45. uint32_t counter;
  46. uint8_t nonce[12];
  47. const uint8_t *extra_ciphertext;
  48. size_t extra_ciphertext_len;
  49. } in;
  50. struct {
  51. uint8_t tag[POLY1305_TAG_LEN];
  52. } out;
  53. };
  54. #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
  55. !defined(OPENSSL_WINDOWS)
  56. static int asm_capable(void) {
  57. const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
  58. return sse41_capable;
  59. }
  60. OPENSSL_COMPILE_ASSERT(sizeof(union open_data) == 48, wrong_open_data_size);
  61. OPENSSL_COMPILE_ASSERT(sizeof(union seal_data) == 48 + 8 + 8,
  62. wrong_seal_data_size);
  63. // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
  64. // |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
  65. // Additional input parameters are passed in |aead_data->in|. On exit, it will
  66. // write calculated tag value to |aead_data->out.tag|, which the caller must
  67. // check.
  68. extern void chacha20_poly1305_open(uint8_t *out_plaintext,
  69. const uint8_t *ciphertext,
  70. size_t plaintext_len, const uint8_t *ad,
  71. size_t ad_len, union open_data *aead_data);
  72. // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It encrypts
  73. // |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
  74. // Additional input parameters are passed in |aead_data->in|. The calculated tag
  75. // value is over the computed ciphertext concatenated with |extra_ciphertext|
  76. // and written to |aead_data->out.tag|.
  77. extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
  78. const uint8_t *plaintext,
  79. size_t plaintext_len, const uint8_t *ad,
  80. size_t ad_len, union seal_data *aead_data);
  81. #else
  82. static int asm_capable(void) { return 0; }
  83. static void chacha20_poly1305_open(uint8_t *out_plaintext,
  84. const uint8_t *ciphertext,
  85. size_t plaintext_len, const uint8_t *ad,
  86. size_t ad_len, union open_data *aead_data) {}
  87. static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
  88. const uint8_t *plaintext,
  89. size_t plaintext_len, const uint8_t *ad,
  90. size_t ad_len, union seal_data *aead_data) {}
  91. #endif
  92. static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  93. size_t key_len, size_t tag_len) {
  94. struct aead_chacha20_poly1305_ctx *c20_ctx;
  95. if (tag_len == 0) {
  96. tag_len = POLY1305_TAG_LEN;
  97. }
  98. if (tag_len > POLY1305_TAG_LEN) {
  99. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  100. return 0;
  101. }
  102. if (key_len != sizeof(c20_ctx->key)) {
  103. return 0; // internal error - EVP_AEAD_CTX_init should catch this.
  104. }
  105. c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
  106. if (c20_ctx == NULL) {
  107. return 0;
  108. }
  109. OPENSSL_memcpy(c20_ctx->key, key, key_len);
  110. ctx->aead_state = c20_ctx;
  111. ctx->tag_len = tag_len;
  112. return 1;
  113. }
  114. static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
  115. OPENSSL_free(ctx->aead_state);
  116. }
  117. static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) {
  118. uint8_t length_bytes[8];
  119. for (unsigned i = 0; i < sizeof(length_bytes); i++) {
  120. length_bytes[i] = data_len;
  121. data_len >>= 8;
  122. }
  123. CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
  124. }
  125. // calc_tag fills |tag| with the authentication tag for the given inputs.
  126. static void calc_tag(uint8_t tag[POLY1305_TAG_LEN],
  127. const struct aead_chacha20_poly1305_ctx *c20_ctx,
  128. const uint8_t nonce[12], const uint8_t *ad, size_t ad_len,
  129. const uint8_t *ciphertext, size_t ciphertext_len,
  130. const uint8_t *ciphertext_extra,
  131. size_t ciphertext_extra_len) {
  132. alignas(16) uint8_t poly1305_key[32];
  133. OPENSSL_memset(poly1305_key, 0, sizeof(poly1305_key));
  134. CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
  135. c20_ctx->key, nonce, 0);
  136. static const uint8_t padding[16] = { 0 }; // Padding is all zeros.
  137. poly1305_state ctx;
  138. CRYPTO_poly1305_init(&ctx, poly1305_key);
  139. CRYPTO_poly1305_update(&ctx, ad, ad_len);
  140. if (ad_len % 16 != 0) {
  141. CRYPTO_poly1305_update(&ctx, padding, sizeof(padding) - (ad_len % 16));
  142. }
  143. CRYPTO_poly1305_update(&ctx, ciphertext, ciphertext_len);
  144. CRYPTO_poly1305_update(&ctx, ciphertext_extra, ciphertext_extra_len);
  145. const size_t ciphertext_total = ciphertext_len + ciphertext_extra_len;
  146. if (ciphertext_total % 16 != 0) {
  147. CRYPTO_poly1305_update(&ctx, padding,
  148. sizeof(padding) - (ciphertext_total % 16));
  149. }
  150. poly1305_update_length(&ctx, ad_len);
  151. poly1305_update_length(&ctx, ciphertext_total);
  152. CRYPTO_poly1305_finish(&ctx, tag);
  153. }
  154. static int aead_chacha20_poly1305_seal_scatter(
  155. const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
  156. size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
  157. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
  158. size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
  159. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  160. if (extra_in_len + ctx->tag_len < ctx->tag_len) {
  161. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  162. return 0;
  163. }
  164. if (max_out_tag_len < ctx->tag_len + extra_in_len) {
  165. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  166. return 0;
  167. }
  168. if (nonce_len != 12) {
  169. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  170. return 0;
  171. }
  172. // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  173. // individual operations that work on more than 256GB at a time.
  174. // |in_len_64| is needed because, on 32-bit platforms, size_t is only
  175. // 32-bits and this produces a warning because it's always false.
  176. // Casting to uint64_t inside the conditional is not sufficient to stop
  177. // the warning.
  178. const uint64_t in_len_64 = in_len;
  179. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  180. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  181. return 0;
  182. }
  183. if (max_out_tag_len < ctx->tag_len) {
  184. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  185. return 0;
  186. }
  187. // The the extra input is given, it is expected to be very short and so is
  188. // encrypted byte-by-byte first.
  189. if (extra_in_len) {
  190. static const size_t kChaChaBlockSize = 64;
  191. uint32_t block_counter = 1 + (in_len / kChaChaBlockSize);
  192. size_t offset = in_len % kChaChaBlockSize;
  193. uint8_t block[64 /* kChaChaBlockSize */];
  194. for (size_t done = 0; done < extra_in_len; block_counter++) {
  195. memset(block, 0, sizeof(block));
  196. CRYPTO_chacha_20(block, block, sizeof(block), c20_ctx->key, nonce,
  197. block_counter);
  198. for (size_t i = offset; i < sizeof(block) && done < extra_in_len;
  199. i++, done++) {
  200. out_tag[done] = extra_in[done] ^ block[i];
  201. }
  202. offset = 0;
  203. }
  204. }
  205. union seal_data data;
  206. if (asm_capable()) {
  207. OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
  208. data.in.counter = 0;
  209. OPENSSL_memcpy(data.in.nonce, nonce, 12);
  210. data.in.extra_ciphertext = out_tag;
  211. data.in.extra_ciphertext_len = extra_in_len;
  212. chacha20_poly1305_seal(out, in, in_len, ad, ad_len, &data);
  213. } else {
  214. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  215. calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, out, in_len, out_tag,
  216. extra_in_len);
  217. }
  218. OPENSSL_memcpy(out_tag + extra_in_len, data.out.tag, ctx->tag_len);
  219. *out_tag_len = extra_in_len + ctx->tag_len;
  220. return 1;
  221. }
  222. static int aead_chacha20_poly1305_open_gather(
  223. const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
  224. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
  225. size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
  226. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  227. if (nonce_len != 12) {
  228. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  229. return 0;
  230. }
  231. if (in_tag_len != ctx->tag_len) {
  232. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  233. return 0;
  234. }
  235. // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  236. // individual operations that work on more than 256GB at a time.
  237. // |in_len_64| is needed because, on 32-bit platforms, size_t is only
  238. // 32-bits and this produces a warning because it's always false.
  239. // Casting to uint64_t inside the conditional is not sufficient to stop
  240. // the warning.
  241. const uint64_t in_len_64 = in_len;
  242. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  243. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  244. return 0;
  245. }
  246. union open_data data;
  247. if (asm_capable()) {
  248. OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
  249. data.in.counter = 0;
  250. OPENSSL_memcpy(data.in.nonce, nonce, 12);
  251. chacha20_poly1305_open(out, in, in_len, ad, ad_len, &data);
  252. } else {
  253. calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, in, in_len, NULL, 0);
  254. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  255. }
  256. if (CRYPTO_memcmp(data.out.tag, in_tag, ctx->tag_len) != 0) {
  257. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  258. return 0;
  259. }
  260. return 1;
  261. }
  262. static const EVP_AEAD aead_chacha20_poly1305 = {
  263. 32, // key len
  264. 12, // nonce len
  265. POLY1305_TAG_LEN, // overhead
  266. POLY1305_TAG_LEN, // max tag length
  267. 1, // seal_scatter_supports_extra_in
  268. aead_chacha20_poly1305_init,
  269. NULL, // init_with_direction
  270. aead_chacha20_poly1305_cleanup,
  271. NULL /* open */,
  272. aead_chacha20_poly1305_seal_scatter,
  273. aead_chacha20_poly1305_open_gather,
  274. NULL, // get_iv
  275. NULL, // tag_len
  276. };
  277. const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
  278. return &aead_chacha20_poly1305;
  279. }