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