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