2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <openssl/aead.h>
|
|
|
|
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/chacha.h>
|
|
|
|
#include <openssl/cipher.h>
|
2017-02-03 16:23:18 +00:00
|
|
|
#include <openssl/cpu.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/mem.h>
|
|
|
|
#include <openssl/poly1305.h>
|
2017-08-01 20:02:26 +01:00
|
|
|
#include <openssl/type_check.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-05-03 21:23:37 +01:00
|
|
|
#include "../fipsmodule/cipher/internal.h"
|
2016-01-18 06:12:57 +00:00
|
|
|
#include "../internal.h"
|
2018-08-08 01:04:54 +01:00
|
|
|
#include "../chacha/internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define POLY1305_TAG_LEN 16
|
|
|
|
|
|
|
|
struct aead_chacha20_poly1305_ctx {
|
2017-08-01 20:02:26 +01:00
|
|
|
uint8_t key[32];
|
|
|
|
};
|
|
|
|
|
2018-10-16 20:11:51 +01:00
|
|
|
OPENSSL_COMPILE_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
|
|
|
|
sizeof(struct aead_chacha20_poly1305_ctx),
|
|
|
|
AEAD_state_too_small);
|
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
|
|
OPENSSL_COMPILE_ASSERT(alignof(union evp_aead_ctx_st_state) >=
|
|
|
|
alignof(struct aead_chacha20_poly1305_ctx),
|
|
|
|
AEAD_state_insufficient_alignment);
|
|
|
|
#endif
|
|
|
|
|
2017-08-01 20:02:26 +01:00
|
|
|
// For convenience (the x86_64 calling convention allows only six parameters in
|
|
|
|
// registers), the final parameter for the assembly functions is both an input
|
|
|
|
// and output parameter.
|
|
|
|
union open_data {
|
|
|
|
struct {
|
|
|
|
alignas(16) uint8_t key[32];
|
|
|
|
uint32_t counter;
|
|
|
|
uint8_t nonce[12];
|
|
|
|
} in;
|
|
|
|
struct {
|
|
|
|
uint8_t tag[POLY1305_TAG_LEN];
|
|
|
|
} out;
|
|
|
|
};
|
|
|
|
|
|
|
|
union seal_data {
|
|
|
|
struct {
|
|
|
|
alignas(16) uint8_t key[32];
|
|
|
|
uint32_t counter;
|
|
|
|
uint8_t nonce[12];
|
|
|
|
const uint8_t *extra_ciphertext;
|
|
|
|
size_t extra_ciphertext_len;
|
|
|
|
} in;
|
|
|
|
struct {
|
|
|
|
uint8_t tag[POLY1305_TAG_LEN];
|
|
|
|
} out;
|
2014-06-20 20:00:00 +01:00
|
|
|
};
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
|
|
|
|
!defined(OPENSSL_WINDOWS)
|
2017-02-03 16:23:18 +00:00
|
|
|
static int asm_capable(void) {
|
|
|
|
const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
|
|
|
|
return sse41_capable;
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:02:26 +01:00
|
|
|
OPENSSL_COMPILE_ASSERT(sizeof(union open_data) == 48, wrong_open_data_size);
|
|
|
|
OPENSSL_COMPILE_ASSERT(sizeof(union seal_data) == 48 + 8 + 8,
|
|
|
|
wrong_seal_data_size);
|
|
|
|
|
|
|
|
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
|
|
|
|
// |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
|
|
|
|
// Additional input parameters are passed in |aead_data->in|. On exit, it will
|
|
|
|
// write calculated tag value to |aead_data->out.tag|, which the caller must
|
|
|
|
// check.
|
2017-01-26 22:00:47 +00:00
|
|
|
extern void chacha20_poly1305_open(uint8_t *out_plaintext,
|
|
|
|
const uint8_t *ciphertext,
|
|
|
|
size_t plaintext_len, const uint8_t *ad,
|
2017-08-01 20:02:26 +01:00
|
|
|
size_t ad_len, union open_data *aead_data);
|
|
|
|
|
|
|
|
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It encrypts
|
|
|
|
// |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
|
|
|
|
// Additional input parameters are passed in |aead_data->in|. The calculated tag
|
|
|
|
// value is over the computed ciphertext concatenated with |extra_ciphertext|
|
|
|
|
// and written to |aead_data->out.tag|.
|
2017-01-26 22:00:47 +00:00
|
|
|
extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
|
|
|
const uint8_t *plaintext,
|
|
|
|
size_t plaintext_len, const uint8_t *ad,
|
2017-08-01 20:02:26 +01:00
|
|
|
size_t ad_len, union seal_data *aead_data);
|
2017-01-19 23:12:44 +00:00
|
|
|
#else
|
2017-08-01 20:02:26 +01:00
|
|
|
static int asm_capable(void) { return 0; }
|
2017-02-03 16:23:18 +00:00
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
|
|
|
|
static void chacha20_poly1305_open(uint8_t *out_plaintext,
|
|
|
|
const uint8_t *ciphertext,
|
|
|
|
size_t plaintext_len, const uint8_t *ad,
|
2017-08-01 20:02:26 +01:00
|
|
|
size_t ad_len, union open_data *aead_data) {}
|
2017-01-19 23:12:44 +00:00
|
|
|
|
|
|
|
static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
|
|
|
const uint8_t *plaintext,
|
|
|
|
size_t plaintext_len, const uint8_t *ad,
|
2017-08-01 20:02:26 +01:00
|
|
|
size_t ad_len, union seal_data *aead_data) {}
|
2017-01-19 23:12:44 +00:00
|
|
|
#endif
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
|
|
|
size_t key_len, size_t tag_len) {
|
2018-10-16 20:11:51 +01:00
|
|
|
struct aead_chacha20_poly1305_ctx *c20_ctx =
|
|
|
|
(struct aead_chacha20_poly1305_ctx *)&ctx->state;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (tag_len == 0) {
|
|
|
|
tag_len = POLY1305_TAG_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag_len > POLY1305_TAG_LEN) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_len != sizeof(c20_ctx->key)) {
|
2017-08-18 19:06:02 +01:00
|
|
|
return 0; // internal error - EVP_AEAD_CTX_init should catch this.
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(c20_ctx->key, key, key_len);
|
2017-06-06 20:29:48 +01:00
|
|
|
ctx->tag_len = tag_len;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-16 20:11:51 +01:00
|
|
|
static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-03 22:38:58 +01:00
|
|
|
static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) {
|
2014-06-20 20:00:00 +01:00
|
|
|
uint8_t length_bytes[8];
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
for (unsigned i = 0; i < sizeof(length_bytes); i++) {
|
2015-10-03 22:38:58 +01:00
|
|
|
length_bytes[i] = data_len;
|
|
|
|
data_len >>= 8;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// calc_tag fills |tag| with the authentication tag for the given inputs.
|
2018-08-08 01:04:54 +01:00
|
|
|
static void calc_tag(uint8_t tag[POLY1305_TAG_LEN], const uint8_t *key,
|
2017-01-19 23:12:44 +00:00
|
|
|
const uint8_t nonce[12], const uint8_t *ad, size_t ad_len,
|
2017-06-28 21:33:08 +01:00
|
|
|
const uint8_t *ciphertext, size_t ciphertext_len,
|
|
|
|
const uint8_t *ciphertext_extra,
|
|
|
|
size_t ciphertext_extra_len) {
|
2016-01-18 06:12:57 +00:00
|
|
|
alignas(16) uint8_t poly1305_key[32];
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(poly1305_key, 0, sizeof(poly1305_key));
|
2018-08-08 01:04:54 +01:00
|
|
|
CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key), key, nonce,
|
|
|
|
0);
|
2017-01-19 23:12:44 +00:00
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
static const uint8_t padding[16] = { 0 }; // Padding is all zeros.
|
2015-10-03 22:38:58 +01:00
|
|
|
poly1305_state ctx;
|
|
|
|
CRYPTO_poly1305_init(&ctx, poly1305_key);
|
2017-06-28 21:33:08 +01:00
|
|
|
CRYPTO_poly1305_update(&ctx, ad, ad_len);
|
|
|
|
if (ad_len % 16 != 0) {
|
|
|
|
CRYPTO_poly1305_update(&ctx, padding, sizeof(padding) - (ad_len % 16));
|
|
|
|
}
|
|
|
|
CRYPTO_poly1305_update(&ctx, ciphertext, ciphertext_len);
|
|
|
|
CRYPTO_poly1305_update(&ctx, ciphertext_extra, ciphertext_extra_len);
|
|
|
|
const size_t ciphertext_total = ciphertext_len + ciphertext_extra_len;
|
|
|
|
if (ciphertext_total % 16 != 0) {
|
|
|
|
CRYPTO_poly1305_update(&ctx, padding,
|
|
|
|
sizeof(padding) - (ciphertext_total % 16));
|
|
|
|
}
|
2017-01-19 23:12:44 +00:00
|
|
|
poly1305_update_length(&ctx, ad_len);
|
2017-06-28 21:33:08 +01:00
|
|
|
poly1305_update_length(&ctx, ciphertext_total);
|
2015-10-03 22:38:58 +01:00
|
|
|
CRYPTO_poly1305_finish(&ctx, tag);
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
static int chacha20_poly1305_seal_scatter(
|
|
|
|
const uint8_t *key, uint8_t *out, uint8_t *out_tag,
|
2017-06-06 20:29:48 +01:00
|
|
|
size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
|
2017-06-23 22:49:22 +01:00
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
|
2018-08-08 01:04:54 +01:00
|
|
|
size_t extra_in_len, const uint8_t *ad, size_t ad_len, size_t tag_len) {
|
|
|
|
if (extra_in_len + tag_len < tag_len) {
|
2017-06-28 21:33:08 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-08 01:04:54 +01:00
|
|
|
if (max_out_tag_len < tag_len + extra_in_len) {
|
2017-06-28 21:33:08 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-01-19 23:12:44 +00:00
|
|
|
if (nonce_len != 12) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
|
|
|
|
// individual operations that work on more than 256GB at a time.
|
|
|
|
// |in_len_64| is needed because, on 32-bit platforms, size_t is only
|
|
|
|
// 32-bits and this produces a warning because it's always false.
|
|
|
|
// Casting to uint64_t inside the conditional is not sufficient to stop
|
|
|
|
// the warning.
|
2017-06-06 20:29:48 +01:00
|
|
|
const uint64_t in_len_64 = in_len;
|
2016-03-18 22:53:29 +00:00
|
|
|
if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
if (max_out_tag_len < tag_len) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// The the extra input is given, it is expected to be very short and so is
|
|
|
|
// encrypted byte-by-byte first.
|
2017-06-28 21:33:08 +01:00
|
|
|
if (extra_in_len) {
|
|
|
|
static const size_t kChaChaBlockSize = 64;
|
|
|
|
uint32_t block_counter = 1 + (in_len / kChaChaBlockSize);
|
|
|
|
size_t offset = in_len % kChaChaBlockSize;
|
|
|
|
uint8_t block[64 /* kChaChaBlockSize */];
|
|
|
|
|
|
|
|
for (size_t done = 0; done < extra_in_len; block_counter++) {
|
|
|
|
memset(block, 0, sizeof(block));
|
2018-08-08 01:04:54 +01:00
|
|
|
CRYPTO_chacha_20(block, block, sizeof(block), key, nonce,
|
2017-06-28 21:33:08 +01:00
|
|
|
block_counter);
|
|
|
|
for (size_t i = offset; i < sizeof(block) && done < extra_in_len;
|
|
|
|
i++, done++) {
|
|
|
|
out_tag[done] = extra_in[done] ^ block[i];
|
|
|
|
}
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:02:26 +01:00
|
|
|
union seal_data data;
|
2017-02-03 16:23:18 +00:00
|
|
|
if (asm_capable()) {
|
2018-08-08 01:04:54 +01:00
|
|
|
OPENSSL_memcpy(data.in.key, key, 32);
|
2017-08-01 20:02:26 +01:00
|
|
|
data.in.counter = 0;
|
|
|
|
OPENSSL_memcpy(data.in.nonce, nonce, 12);
|
|
|
|
data.in.extra_ciphertext = out_tag;
|
|
|
|
data.in.extra_ciphertext_len = extra_in_len;
|
|
|
|
chacha20_poly1305_seal(out, in, in_len, ad, ad_len, &data);
|
2017-01-19 23:12:44 +00:00
|
|
|
} else {
|
2018-08-08 01:04:54 +01:00
|
|
|
CRYPTO_chacha_20(out, in, in_len, key, nonce, 1);
|
|
|
|
calc_tag(data.out.tag, key, nonce, ad, ad_len, out, in_len, out_tag,
|
2017-08-01 20:02:26 +01:00
|
|
|
extra_in_len);
|
2017-01-19 23:12:44 +00:00
|
|
|
}
|
2015-10-03 22:38:58 +01:00
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
OPENSSL_memcpy(out_tag + extra_in_len, data.out.tag, tag_len);
|
|
|
|
*out_tag_len = extra_in_len + tag_len;
|
2014-06-20 20:00:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
static int aead_chacha20_poly1305_seal_scatter(
|
|
|
|
const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
|
|
|
|
size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
|
|
|
|
size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
|
2018-10-16 20:11:51 +01:00
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx =
|
|
|
|
(struct aead_chacha20_poly1305_ctx *)&ctx->state;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
return chacha20_poly1305_seal_scatter(
|
|
|
|
c20_ctx->key, out, out_tag, out_tag_len, max_out_tag_len, nonce,
|
|
|
|
nonce_len, in, in_len, extra_in, extra_in_len, ad, ad_len, ctx->tag_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_xchacha20_poly1305_seal_scatter(
|
|
|
|
const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
|
|
|
|
size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
|
|
|
|
size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
|
2018-10-16 20:11:51 +01:00
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx =
|
|
|
|
(struct aead_chacha20_poly1305_ctx *)&ctx->state;
|
2018-08-08 01:04:54 +01:00
|
|
|
|
|
|
|
if (nonce_len != 24) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
alignas(4) uint8_t derived_key[32];
|
|
|
|
alignas(4) uint8_t derived_nonce[12];
|
|
|
|
CRYPTO_hchacha20(derived_key, c20_ctx->key, nonce);
|
|
|
|
OPENSSL_memset(derived_nonce, 0, 4);
|
|
|
|
OPENSSL_memcpy(&derived_nonce[4], &nonce[16], 8);
|
|
|
|
|
|
|
|
return chacha20_poly1305_seal_scatter(
|
|
|
|
derived_key, out, out_tag, out_tag_len, max_out_tag_len,
|
|
|
|
derived_nonce, sizeof(derived_nonce), in, in_len, extra_in, extra_in_len,
|
|
|
|
ad, ad_len, ctx->tag_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chacha20_poly1305_open_gather(
|
|
|
|
const uint8_t *key, uint8_t *out, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
|
|
|
|
size_t in_tag_len, const uint8_t *ad, size_t ad_len, size_t tag_len) {
|
2017-01-19 23:12:44 +00:00
|
|
|
if (nonce_len != 12) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
if (in_tag_len != tag_len) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
|
|
|
|
// individual operations that work on more than 256GB at a time.
|
|
|
|
// |in_len_64| is needed because, on 32-bit platforms, size_t is only
|
|
|
|
// 32-bits and this produces a warning because it's always false.
|
|
|
|
// Casting to uint64_t inside the conditional is not sufficient to stop
|
|
|
|
// the warning.
|
2017-06-06 20:29:48 +01:00
|
|
|
const uint64_t in_len_64 = in_len;
|
2016-03-18 22:53:29 +00:00
|
|
|
if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:02:26 +01:00
|
|
|
union open_data data;
|
2017-02-03 16:23:18 +00:00
|
|
|
if (asm_capable()) {
|
2018-08-08 01:04:54 +01:00
|
|
|
OPENSSL_memcpy(data.in.key, key, 32);
|
2017-08-01 20:02:26 +01:00
|
|
|
data.in.counter = 0;
|
|
|
|
OPENSSL_memcpy(data.in.nonce, nonce, 12);
|
|
|
|
chacha20_poly1305_open(out, in, in_len, ad, ad_len, &data);
|
2017-01-19 23:12:44 +00:00
|
|
|
} else {
|
2018-08-08 01:04:54 +01:00
|
|
|
calc_tag(data.out.tag, key, nonce, ad, ad_len, in, in_len, NULL, 0);
|
|
|
|
CRYPTO_chacha_20(out, in, in_len, key, nonce, 1);
|
2017-01-19 23:12:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
if (CRYPTO_memcmp(data.out.tag, in_tag, tag_len) != 0) {
|
2015-10-03 22:38:58 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-03 22:38:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
static int aead_chacha20_poly1305_open_gather(
|
|
|
|
const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
|
|
|
|
size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
|
2018-10-16 20:11:51 +01:00
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx =
|
|
|
|
(struct aead_chacha20_poly1305_ctx *)&ctx->state;
|
2018-08-08 01:04:54 +01:00
|
|
|
|
|
|
|
return chacha20_poly1305_open_gather(c20_ctx->key, out, nonce, nonce_len, in,
|
|
|
|
in_len, in_tag, in_tag_len, ad, ad_len,
|
|
|
|
ctx->tag_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int aead_xchacha20_poly1305_open_gather(
|
|
|
|
const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
|
|
|
|
size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
|
|
|
|
size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
|
2018-10-16 20:11:51 +01:00
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx =
|
|
|
|
(struct aead_chacha20_poly1305_ctx *)&ctx->state;
|
2018-08-08 01:04:54 +01:00
|
|
|
|
|
|
|
if (nonce_len != 24) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
alignas(4) uint8_t derived_key[32];
|
|
|
|
alignas(4) uint8_t derived_nonce[12];
|
|
|
|
CRYPTO_hchacha20(derived_key, c20_ctx->key, nonce);
|
|
|
|
OPENSSL_memset(derived_nonce, 0, 4);
|
|
|
|
OPENSSL_memcpy(&derived_nonce[4], &nonce[16], 8);
|
|
|
|
|
|
|
|
return chacha20_poly1305_open_gather(
|
|
|
|
derived_key, out, derived_nonce, sizeof(derived_nonce), in, in_len,
|
|
|
|
in_tag, in_tag_len, ad, ad_len, ctx->tag_len);
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static const EVP_AEAD aead_chacha20_poly1305 = {
|
2017-08-18 19:06:02 +01:00
|
|
|
32, // key len
|
|
|
|
12, // nonce len
|
|
|
|
POLY1305_TAG_LEN, // overhead
|
|
|
|
POLY1305_TAG_LEN, // max tag length
|
|
|
|
1, // seal_scatter_supports_extra_in
|
2017-06-23 22:49:22 +01:00
|
|
|
|
2015-02-28 08:59:33 +00:00
|
|
|
aead_chacha20_poly1305_init,
|
2017-08-18 19:06:02 +01:00
|
|
|
NULL, // init_with_direction
|
2015-02-28 08:59:33 +00:00
|
|
|
aead_chacha20_poly1305_cleanup,
|
2017-06-06 20:29:48 +01:00
|
|
|
NULL /* open */,
|
|
|
|
aead_chacha20_poly1305_seal_scatter,
|
|
|
|
aead_chacha20_poly1305_open_gather,
|
2017-08-18 19:06:02 +01:00
|
|
|
NULL, // get_iv
|
|
|
|
NULL, // tag_len
|
2014-06-20 20:00:00 +01:00
|
|
|
};
|
|
|
|
|
2018-08-08 01:04:54 +01:00
|
|
|
static const EVP_AEAD aead_xchacha20_poly1305 = {
|
|
|
|
32, // key len
|
|
|
|
24, // nonce len
|
|
|
|
POLY1305_TAG_LEN, // overhead
|
|
|
|
POLY1305_TAG_LEN, // max tag length
|
|
|
|
1, // seal_scatter_supports_extra_in
|
|
|
|
|
|
|
|
aead_chacha20_poly1305_init,
|
|
|
|
NULL, // init_with_direction
|
|
|
|
aead_chacha20_poly1305_cleanup,
|
|
|
|
NULL /* open */,
|
|
|
|
aead_xchacha20_poly1305_seal_scatter,
|
|
|
|
aead_xchacha20_poly1305_open_gather,
|
|
|
|
NULL, // get_iv
|
|
|
|
NULL, // tag_len
|
|
|
|
};
|
|
|
|
|
2015-11-30 23:48:18 +00:00
|
|
|
const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
|
2014-08-20 21:24:00 +01:00
|
|
|
return &aead_chacha20_poly1305;
|
|
|
|
}
|
2018-08-08 01:04:54 +01:00
|
|
|
|
|
|
|
const EVP_AEAD *EVP_aead_xchacha20_poly1305(void) {
|
|
|
|
return &aead_xchacha20_poly1305;
|
|
|
|
}
|