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 
EVP_AEAD_key_length(const EVP_AEAD * aead)26 size_t EVP_AEAD_key_length(const EVP_AEAD *aead) { return aead->key_len; }
27 
EVP_AEAD_nonce_length(const EVP_AEAD * aead)28 size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead) { return aead->nonce_len; }
29 
EVP_AEAD_max_overhead(const EVP_AEAD * aead)30 size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead) { return aead->overhead; }
31 
EVP_AEAD_max_tag_len(const EVP_AEAD * aead)32 size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead) { return aead->max_tag_len; }
33 
EVP_AEAD_CTX_zero(EVP_AEAD_CTX * ctx)34 void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx) {
35   OPENSSL_memset(ctx, 0, sizeof(EVP_AEAD_CTX));
36 }
37 
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)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 
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)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 
EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX * ctx)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|. */
check_alias(const uint8_t * in,size_t in_len,const uint8_t * out,size_t out_len)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 
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)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   OPENSSL_memset(out, 0, max_out_len);
120   *out_len = 0;
121   return 0;
122 }
123 
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)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   OPENSSL_memset(out, 0, max_out_len);
143   *out_len = 0;
144   return 0;
145 }
146 
EVP_AEAD_CTX_aead(const EVP_AEAD_CTX * ctx)147 const EVP_AEAD *EVP_AEAD_CTX_aead(const EVP_AEAD_CTX *ctx) { return ctx->aead; }
148 
EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX * ctx,const uint8_t ** out_iv,size_t * out_len)149 int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
150                         size_t *out_len) {
151   if (ctx->aead->get_iv == NULL) {
152     return 0;
153   }
154 
155   return ctx->aead->get_iv(ctx, out_iv, out_len);
156 }
157