Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / cipher / aead.c
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
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   ctx->aead = aead;
37   if (key_len != aead->key_len) {
38     OPENSSL_PUT_ERROR(CIPHER, EVP_AEAD_CTX_init, CIPHER_R_UNSUPPORTED_KEY_SIZE);
39     return 0;
40   }
41   return aead->init(ctx, key, key_len, tag_len);
42 }
43
44 void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx) {
45   if (ctx->aead == NULL) {
46     return;
47   }
48   ctx->aead->cleanup(ctx);
49   ctx->aead = NULL;
50 }
51
52 /* check_alias returns 0 if |out| points within the buffer determined by |in|
53  * and |in_len| and 1 otherwise.
54  *
55  * When processing, there's only an issue if |out| points within in[:in_len]
56  * and isn't equal to |in|. If that's the case then writing the output will
57  * stomp input that hasn't been read yet.
58  *
59  * This function checks for that case. */
60 static int check_alias(const uint8_t *in, size_t in_len, const uint8_t *out) {
61   if (out <= in) {
62     return 1;
63   } else if (in + in_len <= out) {
64     return 1;
65   }
66   return 0;
67 }
68
69 int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
70                       size_t max_out_len, const uint8_t *nonce,
71                       size_t nonce_len, const uint8_t *in, size_t in_len,
72                       const uint8_t *ad, size_t ad_len) {
73   size_t possible_out_len = in_len + ctx->aead->overhead;
74
75   if (possible_out_len < in_len /* overflow */) {
76     OPENSSL_PUT_ERROR(CIPHER, EVP_AEAD_CTX_seal, CIPHER_R_TOO_LARGE);
77     goto error;
78   }
79
80   if (!check_alias(in, in_len, out)) {
81     OPENSSL_PUT_ERROR(CIPHER, EVP_AEAD_CTX_seal, CIPHER_R_OUTPUT_ALIASES_INPUT);
82     goto error;
83   }
84
85   if (ctx->aead->seal(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
86                       in_len, ad, ad_len)) {
87     return 1;
88   }
89
90 error:
91   /* In the event of an error, clear the output buffer so that a caller
92    * that doesn't check the return value doesn't send raw data. */
93   memset(out, 0, max_out_len);
94   *out_len = 0;
95   return 0;
96 }
97
98 int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
99                       size_t max_out_len, const uint8_t *nonce,
100                       size_t nonce_len, const uint8_t *in, size_t in_len,
101                       const uint8_t *ad, size_t ad_len) {
102   if (!check_alias(in, in_len, out)) {
103     OPENSSL_PUT_ERROR(CIPHER, EVP_AEAD_CTX_open, CIPHER_R_OUTPUT_ALIASES_INPUT);
104     goto error;
105   }
106
107   if (ctx->aead->open(ctx, out, out_len, max_out_len, nonce, nonce_len, in,
108                       in_len, ad, ad_len)) {
109     return 1;
110   }
111
112 error:
113   /* In the event of an error, clear the output buffer so that a caller
114    * that doesn't check the return value doesn't try and process bad
115    * data. */
116   memset(out, 0, max_out_len);
117   *out_len = 0;
118   return 0;
119 }