| 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 |
|
| 15 |
#include <openssl/aead.h> |
| 16 |
|
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include <openssl/cipher.h> |
| 20 |
#include <openssl/err.h> |
| 21 |
|
| 22 |
#include "internal.h" |
| 23 |
#include "../internal.h" |
| 24 |
|
| 25 |
|
| 26 |
size_t EVP_AEAD_key_length(const EVP_AEAD *aead) { return aead->key_len; } |
| 27 |
|
| 28 |
size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) { return aead->nonce_len; } |
| 29 |
|
| 30 |
size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) { return aead->overhead; } |
| 31 |
|
| 32 |
size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) { return aead->max_tag_len; } |
| 33 |
|
| 34 |
void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx) { |
| 35 |
memset(ctx, 0, sizeof(EVP_AEAD_CTX)); |
| 36 |
} |
| 37 |
|
| 38 |
int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, |
| 39 |
const uint8_t *key, size_t key_len, size_t tag_len, |
| 40 |
ENGINE *impl) { |
| 41 |
if (!aead->init) { |
| 42 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_DIRECTION_SET); |
| 43 |
ctx->aead = NULL; |
| 44 |
return 0; |
| 45 |
} |
| 46 |
return EVP_AEAD_CTX_init_with_direction(ctx, aead, key, key_len, tag_len, |
| 47 |
evp_aead_open); |
| 48 |
} |
| 49 |
|
| 50 |
int EVP_AEAD_CTX_init_with_direction(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, |
| 51 |
const uint8_t *key, size_t key_len, |
| 52 |
size_t tag_len, |
| 53 |
enum evp_aead_direction_t dir) { |
| 54 |
if (key_len != aead->key_len) { |
| 55 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_KEY_SIZE); |
| 56 |
ctx->aead = NULL; |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
ctx->aead = aead; |
| 61 |
|
| 62 |
int ok; |
| 63 |
if (aead->init) { |
| 64 |
ok = aead->init(ctx, key, key_len, tag_len); |
| 65 |
} else { |
| 66 |
ok = aead->init_with_direction(ctx, key, key_len, tag_len, dir); |
| 67 |
} |
| 68 |
|
| 69 |
if (!ok) { |
| 70 |
ctx->aead = NULL; |
| 71 |
} |
| 72 |
|
| 73 |
return ok; |
| 74 |
} |
| 75 |
|
| 76 |
void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) { |
| 77 |
if (ctx->aead == NULL) { |
| 78 |
return; |
| 79 |
} |
| 80 |
ctx->aead->cleanup(ctx); |
| 81 |
ctx->aead = NULL; |
| 82 |
} |
| 83 |
|
| 84 |
/* check_alias returns 1 if |out| is compatible with |in| and 0 otherwise. If |
| 85 |
* |in| and |out| alias, we require that |in| == |out|. */ |
| 86 |
static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out, |
| 87 |
size_t out_len) { |
| 88 |
if (!buffers_alias(in, in_len, out, out_len)) { |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
return in == out; |
| 93 |
} |
| 94 |
|
| 95 |
int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 96 |
size_t max_out_len, const uint8_t *nonce, |
| 97 |
size_t nonce_len, const uint8_t *in, size_t in_len, |
| 98 |
const uint8_t *ad, size_t ad_len) { |
| 99 |
size_t possible_out_len = in_len + ctx->aead->overhead; |
| 100 |
|
| 101 |
if (possible_out_len < in_len /* overflow */) { |
| 102 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 103 |
goto error; |
| 104 |
} |
| 105 |
|
| 106 |
if (!check_alias(in, in_len, out, max_out_len)) { |
| 107 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT); |
| 108 |
goto error; |
| 109 |
} |
| 110 |
|
| 111 |
if (ctx->aead->seal(ctx, out, out_len, max_out_len, nonce, nonce_len, in, |
| 112 |
in_len, ad, ad_len)) { |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
error: |
| 117 |
/* In the event of an error, clear the output buffer so that a caller |
| 118 |
* that doesn't check the return value doesn't send raw data. */ |
| 119 |
memset(out, 0, max_out_len); |
| 120 |
*out_len = 0; |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 125 |
size_t max_out_len, const uint8_t *nonce, |
| 126 |
size_t nonce_len, const uint8_t *in, size_t in_len, |
| 127 |
const uint8_t *ad, size_t ad_len) { |
| 128 |
if (!check_alias(in, in_len, out, max_out_len)) { |
| 129 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT); |
| 130 |
goto error; |
| 131 |
} |
| 132 |
|
| 133 |
if (ctx->aead->open(ctx, out, out_len, max_out_len, nonce, nonce_len, in, |
| 134 |
in_len, ad, ad_len)) { |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
error: |
| 139 |
/* In the event of an error, clear the output buffer so that a caller |
| 140 |
* that doesn't check the return value doesn't try and process bad |
| 141 |
* data. */ |
| 142 |
memset(out, 0, max_out_len); |
| 143 |
*out_len = 0; |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
int EVP_AEAD_CTX_get_rc4_state(const EVP_AEAD_CTX *ctx, const RC4_KEY **out_key) { |
| 148 |
if (ctx->aead->get_rc4_state == NULL) { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
return ctx->aead->get_rc4_state(ctx, out_key); |
| 153 |
} |
| 154 |
|
| 155 |
int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv, |
| 156 |
size_t *out_len) { |
| 157 |
if (ctx->aead->get_iv == NULL) { |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
return ctx->aead->get_iv(ctx, out_iv, out_len); |
| 162 |
} |