| 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 |
| 24 |
| 25 | size_t EVP_AEAD_key_length(const EVP_AEAD *aead) { return aead->key_len; } |
| 26 |
| 27 | size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) { return aead->nonce_len; } |
| 28 |
| 29 | size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) { return aead->overhead; } |
| 30 |
| 31 | size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) { return aead->max_tag_len; } |
| 32 |
| 33 | int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, |
| 34 | const uint8_t *key, size_t key_len, size_t tag_len, |
| 35 | ENGINE *impl) { |
| 36 | if (!aead->init) { |
| 37 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_NO_DIRECTION_SET); |
| 38 | ctx->aead = NULL; |
| 39 | return 0; |
| 40 | } |
| 41 | return EVP_AEAD_CTX_init_with_direction(ctx, aead, key, key_len, tag_len, |
| 42 | evp_aead_open); |
| 43 | } |
| 44 |
| 45 | int EVP_AEAD_CTX_init_with_direction(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, |
| 46 | const uint8_t *key, size_t key_len, |
| 47 | size_t tag_len, |
| 48 | enum evp_aead_direction_t dir) { |
| 49 | if (key_len != aead->key_len) { |
| 50 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_KEY_SIZE); |
| 51 | ctx->aead = NULL; |
| 52 | return 0; |
| 53 | } |
| 54 |
| 55 | ctx->aead = aead; |
| 56 |
| 57 | int ok; |
| 58 | if (aead->init) { |
| 59 | ok = aead->init(ctx, key, key_len, tag_len); |
| 60 | } else { |
| 61 | ok = aead->init_with_direction(ctx, key, key_len, tag_len, dir); |
| 62 | } |
| 63 |
| 64 | if (!ok) { |
| 65 | ctx->aead = NULL; |
| 66 | } |
| 67 |
| 68 | return ok; |
| 69 | } |
| 70 |
| 71 | void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) { |
| 72 | if (ctx->aead == NULL) { |
| 73 | return; |
| 74 | } |
| 75 | ctx->aead->cleanup(ctx); |
| 76 | ctx->aead = NULL; |
| 77 | } |
| 78 |
| 79 | /* check_alias returns 0 if |out| points within the buffer determined by |in| |
| 80 | * and |in_len| and 1 otherwise. |
| 81 | * |
| 82 | * When processing, there's only an issue if |out| points within in[:in_len] |
| 83 | * and isn't equal to |in|. If that's the case then writing the output will |
| 84 | * stomp input that hasn't been read yet. |
| 85 | * |
| 86 | * This function checks for that case. */ |
| 87 | static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out) { |
| 88 | if (out <= in) { |
| 89 | return 1; |
| 90 | } else if (in + in_len <= out) { |
| 91 | return 1; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 |
| 96 | int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 97 | size_t max_out_len, const uint8_t *nonce, |
| 98 | size_t nonce_len, const uint8_t *in, size_t in_len, |
| 99 | const uint8_t *ad, size_t ad_len) { |
| 100 | size_t possible_out_len = in_len + ctx->aead->overhead; |
| 101 |
| 102 | if (possible_out_len < in_len /* overflow */) { |
| 103 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 104 | goto error; |
| 105 | } |
| 106 |
| 107 | if (!check_alias(in, in_len, out)) { |
| 108 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT); |
| 109 | goto error; |
| 110 | } |
| 111 |
| 112 | if (ctx->aead->seal(ctx, out, out_len, max_out_len, nonce, nonce_len, in, |
| 113 | in_len, ad, ad_len)) { |
| 114 | return 1; |
| 115 | } |
| 116 |
| 117 | error: |
| 118 | /* In the event of an error, clear the output buffer so that a caller |
| 119 | * that doesn't check the return value doesn't send raw data. */ |
| 120 | memset(out, 0, max_out_len); |
| 121 | *out_len = 0; |
| 122 | return 0; |
| 123 | } |
| 124 |
| 125 | int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 126 | size_t max_out_len, const uint8_t *nonce, |
| 127 | size_t nonce_len, const uint8_t *in, size_t in_len, |
| 128 | const uint8_t *ad, size_t ad_len) { |
| 129 | if (!check_alias(in, in_len, out)) { |
| 130 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_OUTPUT_ALIASES_INPUT); |
| 131 | goto error; |
| 132 | } |
| 133 |
| 134 | if (ctx->aead->open(ctx, out, out_len, max_out_len, nonce, nonce_len, in, |
| 135 | in_len, ad, ad_len)) { |
| 136 | return 1; |
| 137 | } |
| 138 |
| 139 | error: |
| 140 | /* In the event of an error, clear the output buffer so that a caller |
| 141 | * that doesn't check the return value doesn't try and process bad |
| 142 | * data. */ |
| 143 | memset(out, 0, max_out_len); |
| 144 | *out_len = 0; |
| 145 | return 0; |
| 146 | } |
| 147 |
| 148 | int EVP_AEAD_CTX_get_rc4_state(const EVP_AEAD_CTX *ctx, const RC4_KEY **out_key) { |
| 149 | if (ctx->aead->get_rc4_state == NULL) { |
| 150 | return 0; |
| 151 | } |
| 152 |
| 153 | return ctx->aead->get_rc4_state(ctx, out_key); |
| 154 | } |
| 155 |