You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

329 rivejä
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/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. struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  116. OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key));
  117. OPENSSL_free(c20_ctx);
  118. }
  119. static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) {
  120. uint8_t length_bytes[8];
  121. for (unsigned i = 0; i < sizeof(length_bytes); i++) {
  122. length_bytes[i] = data_len;
  123. data_len >>= 8;
  124. }
  125. CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
  126. }
  127. /* calc_tag fills |tag| with the authentication tag for the given inputs. */
  128. static void calc_tag(uint8_t tag[POLY1305_TAG_LEN],
  129. const struct aead_chacha20_poly1305_ctx *c20_ctx,
  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),
  137. c20_ctx->key, nonce, 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 aead_chacha20_poly1305_seal_scatter(
  157. const EVP_AEAD_CTX *ctx, 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) {
  161. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  162. if (extra_in_len + ctx->tag_len < ctx->tag_len) {
  163. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  164. return 0;
  165. }
  166. if (max_out_tag_len < ctx->tag_len + extra_in_len) {
  167. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  168. return 0;
  169. }
  170. if (nonce_len != 12) {
  171. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  172. return 0;
  173. }
  174. /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  175. * individual operations that work on more than 256GB at a time.
  176. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  177. * 32-bits and this produces a warning because it's always false.
  178. * Casting to uint64_t inside the conditional is not sufficient to stop
  179. * the warning. */
  180. const uint64_t in_len_64 = in_len;
  181. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  182. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  183. return 0;
  184. }
  185. if (max_out_tag_len < ctx->tag_len) {
  186. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  187. return 0;
  188. }
  189. /* The the extra input is given, it is expected to be very short and so is
  190. * encrypted byte-by-byte first. */
  191. if (extra_in_len) {
  192. static const size_t kChaChaBlockSize = 64;
  193. uint32_t block_counter = 1 + (in_len / kChaChaBlockSize);
  194. size_t offset = in_len % kChaChaBlockSize;
  195. uint8_t block[64 /* kChaChaBlockSize */];
  196. for (size_t done = 0; done < extra_in_len; block_counter++) {
  197. memset(block, 0, sizeof(block));
  198. CRYPTO_chacha_20(block, block, sizeof(block), c20_ctx->key, nonce,
  199. block_counter);
  200. for (size_t i = offset; i < sizeof(block) && done < extra_in_len;
  201. i++, done++) {
  202. out_tag[done] = extra_in[done] ^ block[i];
  203. }
  204. offset = 0;
  205. }
  206. }
  207. union seal_data data;
  208. if (asm_capable()) {
  209. OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
  210. data.in.counter = 0;
  211. OPENSSL_memcpy(data.in.nonce, nonce, 12);
  212. data.in.extra_ciphertext = out_tag;
  213. data.in.extra_ciphertext_len = extra_in_len;
  214. chacha20_poly1305_seal(out, in, in_len, ad, ad_len, &data);
  215. } else {
  216. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  217. calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, out, in_len, out_tag,
  218. extra_in_len);
  219. }
  220. OPENSSL_memcpy(out_tag + extra_in_len, data.out.tag, ctx->tag_len);
  221. *out_tag_len = extra_in_len + ctx->tag_len;
  222. return 1;
  223. }
  224. static int aead_chacha20_poly1305_open_gather(
  225. const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
  226. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
  227. size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
  228. const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
  229. if (nonce_len != 12) {
  230. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  231. return 0;
  232. }
  233. if (in_tag_len != ctx->tag_len) {
  234. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  235. return 0;
  236. }
  237. /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
  238. * individual operations that work on more than 256GB at a time.
  239. * |in_len_64| is needed because, on 32-bit platforms, size_t is only
  240. * 32-bits and this produces a warning because it's always false.
  241. * Casting to uint64_t inside the conditional is not sufficient to stop
  242. * the warning. */
  243. const uint64_t in_len_64 = in_len;
  244. if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
  245. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  246. return 0;
  247. }
  248. union open_data data;
  249. if (asm_capable()) {
  250. OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
  251. data.in.counter = 0;
  252. OPENSSL_memcpy(data.in.nonce, nonce, 12);
  253. chacha20_poly1305_open(out, in, in_len, ad, ad_len, &data);
  254. } else {
  255. calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, in, in_len, NULL, 0);
  256. CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
  257. }
  258. if (CRYPTO_memcmp(data.out.tag, in_tag, ctx->tag_len) != 0) {
  259. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. static const EVP_AEAD aead_chacha20_poly1305 = {
  265. 32, /* key len */
  266. 12, /* nonce len */
  267. POLY1305_TAG_LEN, /* overhead */
  268. POLY1305_TAG_LEN, /* max tag length */
  269. 1, /* seal_scatter_supports_extra_in */
  270. aead_chacha20_poly1305_init,
  271. NULL, /* init_with_direction */
  272. aead_chacha20_poly1305_cleanup,
  273. NULL /* open */,
  274. aead_chacha20_poly1305_seal_scatter,
  275. aead_chacha20_poly1305_open_gather,
  276. NULL, /* get_iv */
  277. NULL, /* tag_len */
  278. };
  279. const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
  280. return &aead_chacha20_poly1305;
  281. }