Você não pode selecionar mais de 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.
 
 
 
 
 
 

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