1 /* Copyright (c) 2014, Google Inc.
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.
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. */
15 #include <openssl/aead.h>
19 #include <openssl/chacha.h>
20 #include <openssl/cipher.h>
21 #include <openssl/cpu.h>
22 #include <openssl/err.h>
23 #include <openssl/mem.h>
24 #include <openssl/poly1305.h>
25 #include <openssl/type_check.h>
27 #include "../fipsmodule/cipher/internal.h"
28 #include "../internal.h"
31 #define POLY1305_TAG_LEN 16
33 struct aead_chacha20_poly1305_ctx {
37 // For convenience (the x86_64 calling convention allows only six parameters in
38 // registers), the final parameter for the assembly functions is both an input
39 // and output parameter.
42 alignas(16) uint8_t key[32];
47 uint8_t tag[POLY1305_TAG_LEN];
53 alignas(16) uint8_t key[32];
56 const uint8_t *extra_ciphertext;
57 size_t extra_ciphertext_len;
60 uint8_t tag[POLY1305_TAG_LEN];
64 #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
65 !defined(OPENSSL_WINDOWS)
66 static int asm_capable(void) {
67 const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
71 OPENSSL_COMPILE_ASSERT(sizeof(union open_data) == 48, wrong_open_data_size);
72 OPENSSL_COMPILE_ASSERT(sizeof(union seal_data) == 48 + 8 + 8,
73 wrong_seal_data_size);
75 // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
76 // |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
77 // Additional input parameters are passed in |aead_data->in|. On exit, it will
78 // write calculated tag value to |aead_data->out.tag|, which the caller must
80 extern void chacha20_poly1305_open(uint8_t *out_plaintext,
81 const uint8_t *ciphertext,
82 size_t plaintext_len, const uint8_t *ad,
83 size_t ad_len, union open_data *aead_data);
85 // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It encrypts
86 // |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
87 // Additional input parameters are passed in |aead_data->in|. The calculated tag
88 // value is over the computed ciphertext concatenated with |extra_ciphertext|
89 // and written to |aead_data->out.tag|.
90 extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
91 const uint8_t *plaintext,
92 size_t plaintext_len, const uint8_t *ad,
93 size_t ad_len, union seal_data *aead_data);
95 static int asm_capable(void) { return 0; }
98 static void chacha20_poly1305_open(uint8_t *out_plaintext,
99 const uint8_t *ciphertext,
100 size_t plaintext_len, const uint8_t *ad,
101 size_t ad_len, union open_data *aead_data) {}
103 static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
104 const uint8_t *plaintext,
105 size_t plaintext_len, const uint8_t *ad,
106 size_t ad_len, union seal_data *aead_data) {}
109 static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
110 size_t key_len, size_t tag_len) {
111 struct aead_chacha20_poly1305_ctx *c20_ctx;
114 tag_len = POLY1305_TAG_LEN;
117 if (tag_len > POLY1305_TAG_LEN) {
118 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
122 if (key_len != sizeof(c20_ctx->key)) {
123 return 0; // internal error - EVP_AEAD_CTX_init should catch this.
126 c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
127 if (c20_ctx == NULL) {
131 OPENSSL_memcpy(c20_ctx->key, key, key_len);
132 ctx->aead_state = c20_ctx;
133 ctx->tag_len = tag_len;
138 static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
139 OPENSSL_free(ctx->aead_state);
142 static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) {
143 uint8_t length_bytes[8];
145 for (unsigned i = 0; i < sizeof(length_bytes); i++) {
146 length_bytes[i] = data_len;
150 CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
153 // calc_tag fills |tag| with the authentication tag for the given inputs.
154 static void calc_tag(uint8_t tag[POLY1305_TAG_LEN],
155 const struct aead_chacha20_poly1305_ctx *c20_ctx,
156 const uint8_t nonce[12], const uint8_t *ad, size_t ad_len,
157 const uint8_t *ciphertext, size_t ciphertext_len,
158 const uint8_t *ciphertext_extra,
159 size_t ciphertext_extra_len) {
160 alignas(16) uint8_t poly1305_key[32];
161 OPENSSL_memset(poly1305_key, 0, sizeof(poly1305_key));
162 CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
163 c20_ctx->key, nonce, 0);
165 static const uint8_t padding[16] = { 0 }; // Padding is all zeros.
167 CRYPTO_poly1305_init(&ctx, poly1305_key);
168 CRYPTO_poly1305_update(&ctx, ad, ad_len);
169 if (ad_len % 16 != 0) {
170 CRYPTO_poly1305_update(&ctx, padding, sizeof(padding) - (ad_len % 16));
172 CRYPTO_poly1305_update(&ctx, ciphertext, ciphertext_len);
173 CRYPTO_poly1305_update(&ctx, ciphertext_extra, ciphertext_extra_len);
174 const size_t ciphertext_total = ciphertext_len + ciphertext_extra_len;
175 if (ciphertext_total % 16 != 0) {
176 CRYPTO_poly1305_update(&ctx, padding,
177 sizeof(padding) - (ciphertext_total % 16));
179 poly1305_update_length(&ctx, ad_len);
180 poly1305_update_length(&ctx, ciphertext_total);
181 CRYPTO_poly1305_finish(&ctx, tag);
184 static int aead_chacha20_poly1305_seal_scatter(
185 const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
186 size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
187 size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
188 size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
189 const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
191 if (extra_in_len + ctx->tag_len < ctx->tag_len) {
192 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
195 if (max_out_tag_len < ctx->tag_len + extra_in_len) {
196 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
199 if (nonce_len != 12) {
200 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
204 // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
205 // individual operations that work on more than 256GB at a time.
206 // |in_len_64| is needed because, on 32-bit platforms, size_t is only
207 // 32-bits and this produces a warning because it's always false.
208 // Casting to uint64_t inside the conditional is not sufficient to stop
210 const uint64_t in_len_64 = in_len;
211 if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
212 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
216 if (max_out_tag_len < ctx->tag_len) {
217 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
221 // The the extra input is given, it is expected to be very short and so is
222 // encrypted byte-by-byte first.
224 static const size_t kChaChaBlockSize = 64;
225 uint32_t block_counter = 1 + (in_len / kChaChaBlockSize);
226 size_t offset = in_len % kChaChaBlockSize;
227 uint8_t block[64 /* kChaChaBlockSize */];
229 for (size_t done = 0; done < extra_in_len; block_counter++) {
230 memset(block, 0, sizeof(block));
231 CRYPTO_chacha_20(block, block, sizeof(block), c20_ctx->key, nonce,
233 for (size_t i = offset; i < sizeof(block) && done < extra_in_len;
235 out_tag[done] = extra_in[done] ^ block[i];
241 union seal_data data;
243 OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
245 OPENSSL_memcpy(data.in.nonce, nonce, 12);
246 data.in.extra_ciphertext = out_tag;
247 data.in.extra_ciphertext_len = extra_in_len;
248 chacha20_poly1305_seal(out, in, in_len, ad, ad_len, &data);
250 CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
251 calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, out, in_len, out_tag,
255 OPENSSL_memcpy(out_tag + extra_in_len, data.out.tag, ctx->tag_len);
256 *out_tag_len = extra_in_len + ctx->tag_len;
260 static int aead_chacha20_poly1305_open_gather(
261 const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
262 size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
263 size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
264 const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
266 if (nonce_len != 12) {
267 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
271 if (in_tag_len != ctx->tag_len) {
272 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
276 // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
277 // individual operations that work on more than 256GB at a time.
278 // |in_len_64| is needed because, on 32-bit platforms, size_t is only
279 // 32-bits and this produces a warning because it's always false.
280 // Casting to uint64_t inside the conditional is not sufficient to stop
282 const uint64_t in_len_64 = in_len;
283 if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
284 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
288 union open_data data;
290 OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
292 OPENSSL_memcpy(data.in.nonce, nonce, 12);
293 chacha20_poly1305_open(out, in, in_len, ad, ad_len, &data);
295 calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, in, in_len, NULL, 0);
296 CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
299 if (CRYPTO_memcmp(data.out.tag, in_tag, ctx->tag_len) != 0) {
300 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
307 static const EVP_AEAD aead_chacha20_poly1305 = {
310 POLY1305_TAG_LEN, // overhead
311 POLY1305_TAG_LEN, // max tag length
312 1, // seal_scatter_supports_extra_in
314 aead_chacha20_poly1305_init,
315 NULL, // init_with_direction
316 aead_chacha20_poly1305_cleanup,
318 aead_chacha20_poly1305_seal_scatter,
319 aead_chacha20_poly1305_open_gather,
324 const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
325 return &aead_chacha20_poly1305;