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>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/mem.h>
|
|
|
|
#include <openssl/poly1305.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2016-01-18 06:12:57 +00:00
|
|
|
#include "../internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define POLY1305_TAG_LEN 16
|
|
|
|
|
|
|
|
struct aead_chacha20_poly1305_ctx {
|
|
|
|
unsigned char key[32];
|
|
|
|
unsigned char tag_len;
|
|
|
|
};
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
|
|
|
|
!defined(OPENSSL_WINDOWS)
|
|
|
|
static const int kHaveAsm = 1;
|
|
|
|
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It
|
|
|
|
// decrypts |plaintext_len| bytes from |ciphertext| and writes them to
|
|
|
|
// |out_plaintext|. On entry, |aead_data| must contain the final 48 bytes of
|
|
|
|
// the initial ChaCha20 block, i.e. the key, followed by four zeros, followed
|
|
|
|
// by the nonce. On exit, it will contain the calculated tag value, 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,
|
|
|
|
size_t ad_len, uint8_t *aead_data);
|
2017-01-19 23:12:44 +00:00
|
|
|
|
|
|
|
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It
|
|
|
|
// encrypts |plaintext_len| bytes from |plaintext| and writes them to
|
|
|
|
// |out_ciphertext|. On entry, |aead_data| must contain the final 48 bytes of
|
|
|
|
// the initial ChaCha20 block, i.e. the key, followed by four zeros, followed
|
|
|
|
// by the nonce. On exit, it will contain the calculated tag value, which the
|
|
|
|
// caller must append to the ciphertext.
|
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,
|
|
|
|
size_t ad_len, uint8_t *aead_data);
|
2017-01-19 23:12:44 +00:00
|
|
|
#else
|
|
|
|
static const int kHaveAsm = 0;
|
|
|
|
|
|
|
|
static void chacha20_poly1305_open(uint8_t *out_plaintext,
|
|
|
|
const uint8_t *ciphertext,
|
|
|
|
size_t plaintext_len, const uint8_t *ad,
|
|
|
|
size_t ad_len, uint8_t *aead_data) {}
|
|
|
|
|
|
|
|
static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
|
|
|
const uint8_t *plaintext,
|
|
|
|
size_t plaintext_len, const uint8_t *ad,
|
|
|
|
size_t ad_len, uint8_t *aead_data) {}
|
|
|
|
#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) {
|
|
|
|
struct aead_chacha20_poly1305_ctx *c20_ctx;
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
return 0; /* internal error - EVP_AEAD_CTX_init should catch this. */
|
|
|
|
}
|
|
|
|
|
|
|
|
c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
|
|
|
|
if (c20_ctx == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(c20_ctx->key, key, key_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
c20_ctx->tag_len = tag_len;
|
|
|
|
ctx->aead_state = c20_ctx;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
|
|
|
|
struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
|
|
|
|
OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key));
|
|
|
|
OPENSSL_free(c20_ctx);
|
|
|
|
}
|
|
|
|
|
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-01-19 23:12:44 +00:00
|
|
|
static void poly1305_update_padded_16(poly1305_state *poly1305,
|
|
|
|
const uint8_t *data, size_t data_len) {
|
|
|
|
static const uint8_t padding[16] = { 0 }; /* Padding is all zeros. */
|
|
|
|
|
|
|
|
CRYPTO_poly1305_update(poly1305, data, data_len);
|
|
|
|
if (data_len % 16 != 0) {
|
|
|
|
CRYPTO_poly1305_update(poly1305, padding,
|
|
|
|
sizeof(padding) - (data_len % 16));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calc_tag fills |tag| with the authentication tag for the given inputs. */
|
|
|
|
static void calc_tag(uint8_t tag[POLY1305_TAG_LEN],
|
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx,
|
|
|
|
const uint8_t nonce[12], const uint8_t *ad, size_t ad_len,
|
|
|
|
const uint8_t *ciphertext, size_t ciphertext_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));
|
2015-10-03 22:38:58 +01:00
|
|
|
CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
|
|
|
|
c20_ctx->key, nonce, 0);
|
2017-01-19 23:12:44 +00:00
|
|
|
|
2015-10-03 22:38:58 +01:00
|
|
|
poly1305_state ctx;
|
|
|
|
CRYPTO_poly1305_init(&ctx, poly1305_key);
|
2017-01-19 23:12:44 +00:00
|
|
|
poly1305_update_padded_16(&ctx, ad, ad_len);
|
|
|
|
poly1305_update_padded_16(&ctx, ciphertext, ciphertext_len);
|
|
|
|
poly1305_update_length(&ctx, ad_len);
|
|
|
|
poly1305_update_length(&ctx, ciphertext_len);
|
2015-10-03 22:38:58 +01:00
|
|
|
CRYPTO_poly1305_finish(&ctx, tag);
|
|
|
|
}
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
|
|
|
size_t *out_len, size_t max_out_len,
|
|
|
|
const uint8_t *nonce, size_t nonce_len,
|
|
|
|
const uint8_t *in, size_t in_len,
|
|
|
|
const uint8_t *ad, size_t ad_len) {
|
2015-10-03 22:38:58 +01:00
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
|
2014-06-20 20:00:00 +01:00
|
|
|
const uint64_t in_len_64 = in_len;
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
if (nonce_len != 12) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:13:36 +01:00
|
|
|
/* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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. */
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_len + c20_ctx->tag_len < in_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 (max_out_len < in_len + c20_ctx->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-01-19 23:12:44 +00:00
|
|
|
alignas(16) uint8_t tag[48];
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
if (kHaveAsm) {
|
|
|
|
OPENSSL_memcpy(tag, c20_ctx->key, 32);
|
|
|
|
OPENSSL_memset(tag + 32, 0, 4);
|
|
|
|
OPENSSL_memcpy(tag + 32 + 4, nonce, 12);
|
|
|
|
chacha20_poly1305_seal(out, in, in_len, ad, ad_len, tag);
|
|
|
|
} else {
|
|
|
|
CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
|
|
|
|
calc_tag(tag, c20_ctx, nonce, ad, ad_len, out, in_len);
|
|
|
|
}
|
2015-10-03 22:38:58 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(out + in_len, tag, c20_ctx->tag_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
*out_len = in_len + c20_ctx->tag_len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
|
|
|
size_t *out_len, size_t max_out_len,
|
|
|
|
const uint8_t *nonce, size_t nonce_len,
|
|
|
|
const uint8_t *in, size_t in_len,
|
|
|
|
const uint8_t *ad, size_t ad_len) {
|
2014-06-20 20:00:00 +01:00
|
|
|
const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
|
|
|
|
size_t plaintext_len;
|
|
|
|
const uint64_t in_len_64 = in_len;
|
|
|
|
|
2017-01-19 23:12:44 +00:00
|
|
|
if (nonce_len != 12) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
if (in_len < c20_ctx->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;
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:13:36 +01:00
|
|
|
/* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
|
2014-06-20 20:00:00 +01:00
|
|
|
* 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. */
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-03 22:38:58 +01:00
|
|
|
plaintext_len = in_len - c20_ctx->tag_len;
|
2017-01-19 23:12:44 +00:00
|
|
|
alignas(16) uint8_t tag[48];
|
|
|
|
|
|
|
|
if (kHaveAsm) {
|
|
|
|
OPENSSL_memcpy(tag, c20_ctx->key, 32);
|
|
|
|
OPENSSL_memset(tag + 32, 0, 4);
|
|
|
|
OPENSSL_memcpy(tag + 32 + 4, nonce, 12);
|
|
|
|
chacha20_poly1305_open(out, in, plaintext_len, ad, ad_len, tag);
|
|
|
|
} else {
|
|
|
|
calc_tag(tag, c20_ctx, nonce, ad, ad_len, in, plaintext_len);
|
|
|
|
CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1);
|
|
|
|
}
|
|
|
|
|
2015-10-03 22:38:58 +01:00
|
|
|
if (CRYPTO_memcmp(tag, in + plaintext_len, c20_ctx->tag_len) != 0) {
|
|
|
|
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
|
|
|
*out_len = plaintext_len;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
static const EVP_AEAD aead_chacha20_poly1305 = {
|
|
|
|
32, /* key len */
|
2015-10-29 17:19:56 +00:00
|
|
|
12, /* nonce len */
|
2014-06-20 20:00:00 +01:00
|
|
|
POLY1305_TAG_LEN, /* overhead */
|
|
|
|
POLY1305_TAG_LEN, /* max tag length */
|
2015-02-28 08:59:33 +00:00
|
|
|
aead_chacha20_poly1305_init,
|
|
|
|
NULL, /* init_with_direction */
|
|
|
|
aead_chacha20_poly1305_cleanup,
|
|
|
|
aead_chacha20_poly1305_seal,
|
|
|
|
aead_chacha20_poly1305_open,
|
2015-11-04 02:36:10 +00:00
|
|
|
NULL, /* get_iv */
|
2014-06-20 20:00:00 +01:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|