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>
|
|
|
|
|
2017-06-12 22:53:51 +01:00
|
|
|
#include <assert.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/cipher.h>
|
|
|
|
#include <openssl/err.h>
|
2017-05-05 20:05:25 +01:00
|
|
|
#include <openssl/mem.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
#include "internal.h"
|
2017-05-03 21:23:37 +01:00
|
|
|
#include "../../internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
size_t EVP_AEAD_key_length(const EVP_AEAD *aead) { return aead->key_len; }
|
|
|
|
|
|
|
|
size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) { return aead->nonce_len; }
|
|
|
|
|
|
|
|
size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) { return aead->overhead; }
|
|
|
|
|
|
|
|
size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) { return aead->max_tag_len; }
|
|
|
|
|
2015-08-21 23:05:11 +01:00
|
|
|
void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx) {
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(ctx, 0, sizeof(EVP_AEAD_CTX));
|
2015-08-21 23:05:11 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 20:05:25 +01:00
|
|
|
EVP_AEAD_CTX *EVP_AEAD_CTX_new(const EVP_AEAD *aead, const uint8_t *key,
|
|
|
|
size_t key_len, size_t tag_len) {
|
|
|
|
EVP_AEAD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_AEAD_CTX));
|
|
|
|
EVP_AEAD_CTX_zero(ctx);
|
|
|
|
|
|
|
|
if (EVP_AEAD_CTX_init(ctx, aead, key, key_len, tag_len, NULL)) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_AEAD_CTX_free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx) {
|
|
|
|
EVP_AEAD_CTX_cleanup(ctx);
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
|
|
|
|
const uint8_t *key, size_t key_len, size_t tag_len,
|
|
|
|
ENGINE *impl) {
|
2015-02-28 08:59:33 +00:00
|
|
|
if (!aead->init) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_DIRECTION_SET);
|
2015-05-11 22:28:12 +01:00
|
|
|
ctx->aead = NULL;
|
2015-02-28 08:59:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return EVP_AEAD_CTX_init_with_direction(ctx, aead, key, key_len, tag_len,
|
|
|
|
evp_aead_open);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_AEAD_CTX_init_with_direction(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
|
|
|
|
const uint8_t *key, size_t key_len,
|
|
|
|
size_t tag_len,
|
|
|
|
enum evp_aead_direction_t dir) {
|
2014-06-20 20:00:00 +01:00
|
|
|
if (key_len != aead->key_len) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_KEY_SIZE);
|
2015-05-11 22:28:12 +01:00
|
|
|
ctx->aead = NULL;
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-11 22:28:12 +01:00
|
|
|
|
|
|
|
ctx->aead = aead;
|
|
|
|
|
|
|
|
int ok;
|
2015-02-28 08:59:33 +00:00
|
|
|
if (aead->init) {
|
2015-05-11 22:28:12 +01:00
|
|
|
ok = aead->init(ctx, key, key_len, tag_len);
|
2015-02-28 08:59:33 +00:00
|
|
|
} else {
|
2015-05-11 22:28:12 +01:00
|
|
|
ok = aead->init_with_direction(ctx, key, key_len, tag_len, dir);
|
2015-02-28 08:59:33 +00:00
|
|
|
}
|
2015-05-11 22:28:12 +01:00
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
ctx->aead = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) {
|
|
|
|
if (ctx->aead == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ctx->aead->cleanup(ctx);
|
|
|
|
ctx->aead = NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-08 23:31:42 +01:00
|
|
|
/* check_alias returns 1 if |out| is compatible with |in| and 0 otherwise. If
|
|
|
|
* |in| and |out| alias, we require that |in| == |out|. */
|
|
|
|
static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out,
|
|
|
|
size_t out_len) {
|
|
|
|
if (!buffers_alias(in, in_len, out, out_len)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-06-08 23:31:42 +01:00
|
|
|
|
|
|
|
return in == out;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_AEAD_CTX_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) {
|
2017-06-06 20:29:48 +01:00
|
|
|
if (in_len + ctx->aead->overhead < in_len /* overflow */) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
if (max_out_len < in_len) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2016-06-08 23:31:42 +01:00
|
|
|
if (!check_alias(in, in_len, out, max_out_len)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
size_t out_tag_len;
|
|
|
|
if (ctx->aead->seal_scatter(ctx, out, out + in_len, &out_tag_len,
|
|
|
|
max_out_len - in_len, nonce, nonce_len, in,
|
|
|
|
in_len, ad, ad_len)) {
|
|
|
|
*out_len = in_len + out_tag_len;
|
2014-06-20 20:00:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* In the event of an error, clear the output buffer so that a caller
|
|
|
|
* that doesn't check the return value doesn't send raw data. */
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(out, 0, max_out_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
*out_len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
int EVP_AEAD_CTX_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 *ad,
|
|
|
|
size_t ad_len) {
|
|
|
|
// |in| and |out| may alias exactly, |out_tag| may not alias.
|
|
|
|
if (!check_alias(in, in_len, out, in_len) ||
|
|
|
|
buffers_alias(out, in_len, out_tag, max_out_tag_len) ||
|
|
|
|
buffers_alias(in, in_len, out_tag, max_out_tag_len)) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->aead->seal_scatter(ctx, out, out_tag, out_tag_len, max_out_tag_len,
|
|
|
|
nonce, nonce_len, in, in_len, ad, ad_len)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* In the event of an error, clear the output buffer so that a caller
|
|
|
|
* that doesn't check the return value doesn't send raw data. */
|
|
|
|
OPENSSL_memset(out, 0, in_len);
|
|
|
|
OPENSSL_memset(out_tag, 0, max_out_tag_len);
|
|
|
|
*out_tag_len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
int EVP_AEAD_CTX_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) {
|
2016-06-08 23:31:42 +01:00
|
|
|
if (!check_alias(in, in_len, out, max_out_len)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
if (ctx->aead->open) {
|
|
|
|
if (!ctx->aead->open(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
|
|
|
|
in_len, ad, ad_len)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AEADs that use the default implementation of open() must set |tag_len| at
|
|
|
|
// initialization time.
|
|
|
|
assert(ctx->tag_len);
|
|
|
|
|
|
|
|
if (in_len < ctx->tag_len) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t plaintext_len = in_len - ctx->tag_len;
|
|
|
|
if (max_out_len < plaintext_len) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (EVP_AEAD_CTX_open_gather(ctx, out, nonce, nonce_len, in, plaintext_len,
|
|
|
|
in + plaintext_len, ctx->tag_len, ad, ad_len)) {
|
|
|
|
*out_len = plaintext_len;
|
2014-06-20 20:00:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* In the event of an error, clear the output buffer so that a caller
|
|
|
|
* that doesn't check the return value doesn't try and process bad
|
|
|
|
* data. */
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(out, 0, max_out_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
*out_len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-20 23:32:52 +00:00
|
|
|
|
2017-06-06 20:29:48 +01:00
|
|
|
int EVP_AEAD_CTX_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) {
|
|
|
|
if (!check_alias(in, in_len, out, in_len)) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx->aead->open_gather) {
|
|
|
|
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_CTRL_NOT_IMPLEMENTED);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->aead->open_gather(ctx, out, nonce, nonce_len, in, in_len, in_tag,
|
|
|
|
in_tag_len, ad, ad_len)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
/* In the event of an error, clear the output buffer so that a caller
|
|
|
|
* that doesn't check the return value doesn't try and process bad
|
|
|
|
* data. */
|
|
|
|
OPENSSL_memset(out, 0, in_len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-17 00:19:16 +01:00
|
|
|
const EVP_AEAD *EVP_AEAD_CTX_aead(const EVP_AEAD_CTX *ctx) { return ctx->aead; }
|
|
|
|
|
2015-11-04 02:36:10 +00:00
|
|
|
int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
|
|
|
|
size_t *out_len) {
|
|
|
|
if (ctx->aead->get_iv == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx->aead->get_iv(ctx, out_iv, out_len);
|
|
|
|
}
|