Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / boringssl / crypto / cipher_extra / e_ssl3.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 <assert.h>
16 #include <limits.h>
17 #include <string.h>
18
19 #include <openssl/aead.h>
20 #include <openssl/cipher.h>
21 #include <openssl/err.h>
22 #include <openssl/hmac.h>
23 #include <openssl/md5.h>
24 #include <openssl/mem.h>
25 #include <openssl/sha.h>
26
27 #include "internal.h"
28 #include "../internal.h"
29 #include "../fipsmodule/cipher/internal.h"
30
31
32 typedef struct {
33   EVP_CIPHER_CTX cipher_ctx;
34   EVP_MD_CTX md_ctx;
35 } AEAD_SSL3_CTX;
36
37 static int ssl3_mac(AEAD_SSL3_CTX *ssl3_ctx, uint8_t *out, unsigned *out_len,
38                     const uint8_t *ad, size_t ad_len, const uint8_t *in,
39                     size_t in_len) {
40   size_t md_size = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
41   size_t pad_len = (md_size == 20) ? 40 : 48;
42
43   // To allow for CBC mode which changes cipher length, |ad| doesn't include the
44   // length for legacy ciphers.
45   uint8_t ad_extra[2];
46   ad_extra[0] = (uint8_t)(in_len >> 8);
47   ad_extra[1] = (uint8_t)(in_len & 0xff);
48
49   EVP_MD_CTX md_ctx;
50   EVP_MD_CTX_init(&md_ctx);
51
52   uint8_t pad[48];
53   uint8_t tmp[EVP_MAX_MD_SIZE];
54   OPENSSL_memset(pad, 0x36, pad_len);
55   if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) ||
56       !EVP_DigestUpdate(&md_ctx, pad, pad_len) ||
57       !EVP_DigestUpdate(&md_ctx, ad, ad_len) ||
58       !EVP_DigestUpdate(&md_ctx, ad_extra, sizeof(ad_extra)) ||
59       !EVP_DigestUpdate(&md_ctx, in, in_len) ||
60       !EVP_DigestFinal_ex(&md_ctx, tmp, NULL)) {
61     EVP_MD_CTX_cleanup(&md_ctx);
62     return 0;
63   }
64
65   OPENSSL_memset(pad, 0x5c, pad_len);
66   if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) ||
67       !EVP_DigestUpdate(&md_ctx, pad, pad_len) ||
68       !EVP_DigestUpdate(&md_ctx, tmp, md_size) ||
69       !EVP_DigestFinal_ex(&md_ctx, out, out_len)) {
70     EVP_MD_CTX_cleanup(&md_ctx);
71     return 0;
72   }
73   EVP_MD_CTX_cleanup(&md_ctx);
74   return 1;
75 }
76
77 static void aead_ssl3_cleanup(EVP_AEAD_CTX *ctx) {
78   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
79   EVP_CIPHER_CTX_cleanup(&ssl3_ctx->cipher_ctx);
80   EVP_MD_CTX_cleanup(&ssl3_ctx->md_ctx);
81   OPENSSL_free(ssl3_ctx);
82   ctx->aead_state = NULL;
83 }
84
85 static int aead_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
86                           size_t tag_len, enum evp_aead_direction_t dir,
87                           const EVP_CIPHER *cipher, const EVP_MD *md) {
88   if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
89       tag_len != EVP_MD_size(md)) {
90     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
91     return 0;
92   }
93
94   if (key_len != EVP_AEAD_key_length(ctx->aead)) {
95     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
96     return 0;
97   }
98
99   size_t mac_key_len = EVP_MD_size(md);
100   size_t enc_key_len = EVP_CIPHER_key_length(cipher);
101   assert(mac_key_len + enc_key_len + EVP_CIPHER_iv_length(cipher) == key_len);
102
103   AEAD_SSL3_CTX *ssl3_ctx = OPENSSL_malloc(sizeof(AEAD_SSL3_CTX));
104   if (ssl3_ctx == NULL) {
105     OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
106     return 0;
107   }
108   EVP_CIPHER_CTX_init(&ssl3_ctx->cipher_ctx);
109   EVP_MD_CTX_init(&ssl3_ctx->md_ctx);
110
111   ctx->aead_state = ssl3_ctx;
112   if (!EVP_CipherInit_ex(&ssl3_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
113                          &key[mac_key_len + enc_key_len],
114                          dir == evp_aead_seal) ||
115       !EVP_DigestInit_ex(&ssl3_ctx->md_ctx, md, NULL) ||
116       !EVP_DigestUpdate(&ssl3_ctx->md_ctx, key, mac_key_len)) {
117     aead_ssl3_cleanup(ctx);
118     ctx->aead_state = NULL;
119     return 0;
120   }
121   EVP_CIPHER_CTX_set_padding(&ssl3_ctx->cipher_ctx, 0);
122
123   return 1;
124 }
125
126 static size_t aead_ssl3_tag_len(const EVP_AEAD_CTX *ctx, const size_t in_len,
127                                 const size_t extra_in_len) {
128   assert(extra_in_len == 0);
129   const AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX*)ctx->aead_state;
130
131   const size_t digest_len = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
132   if (EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) != EVP_CIPH_CBC_MODE) {
133     // The NULL cipher.
134     return digest_len;
135   }
136
137   const size_t block_size = EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx);
138   // An overflow of |in_len + digest_len| doesn't affect the result mod
139   // |block_size|, provided that |block_size| is a smaller power of two.
140   assert(block_size != 0 && (block_size & (block_size - 1)) == 0);
141   const size_t pad_len = block_size - ((in_len + digest_len) % block_size);
142   return digest_len + pad_len;
143 }
144
145 static int aead_ssl3_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
146                                   uint8_t *out_tag, size_t *out_tag_len,
147                                   const size_t max_out_tag_len,
148                                   const uint8_t *nonce, const size_t nonce_len,
149                                   const uint8_t *in, const size_t in_len,
150                                   const uint8_t *extra_in,
151                                   const size_t extra_in_len, const uint8_t *ad,
152                                   const size_t ad_len) {
153   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
154
155   if (!ssl3_ctx->cipher_ctx.encrypt) {
156     // Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction.
157     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
158     return 0;
159   }
160
161   if (in_len > INT_MAX) {
162     // EVP_CIPHER takes int as input.
163     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
164     return 0;
165   }
166
167   if (max_out_tag_len < aead_ssl3_tag_len(ctx, in_len, extra_in_len)) {
168     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
169     return 0;
170   }
171
172   if (nonce_len != 0) {
173     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE);
174     return 0;
175   }
176
177   if (ad_len != 11 - 2 /* length bytes */) {
178     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
179     return 0;
180   }
181
182   // Compute the MAC. This must be first in case the operation is being done
183   // in-place.
184   uint8_t mac[EVP_MAX_MD_SIZE];
185   unsigned mac_len;
186   if (!ssl3_mac(ssl3_ctx, mac, &mac_len, ad, ad_len, in, in_len)) {
187     return 0;
188   }
189
190   // Encrypt the input.
191   int len;
192   if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in,
193                          (int)in_len)) {
194     return 0;
195   }
196
197   const size_t block_size = EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx);
198
199   // Feed the MAC into the cipher in two steps. First complete the final partial
200   // block from encrypting the input and split the result between |out| and
201   // |out_tag|. Then encrypt the remainder.
202
203   size_t early_mac_len = (block_size - (in_len % block_size)) % block_size;
204   if (early_mac_len != 0) {
205     assert(len + block_size - early_mac_len == in_len);
206     uint8_t buf[EVP_MAX_BLOCK_LENGTH];
207     int buf_len;
208     if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, buf, &buf_len, mac,
209                            (int)early_mac_len)) {
210       return 0;
211     }
212     assert(buf_len == (int)block_size);
213     OPENSSL_memcpy(out + len, buf, block_size - early_mac_len);
214     OPENSSL_memcpy(out_tag, buf + block_size - early_mac_len, early_mac_len);
215   }
216   size_t tag_len = early_mac_len;
217
218   if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out_tag + tag_len, &len,
219                          mac + tag_len, mac_len - tag_len)) {
220     return 0;
221   }
222   tag_len += len;
223
224   if (block_size > 1) {
225     assert(block_size <= 256);
226     assert(EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
227
228     // Compute padding and feed that into the cipher.
229     uint8_t padding[256];
230     size_t padding_len = block_size - ((in_len + mac_len) % block_size);
231     OPENSSL_memset(padding, 0, padding_len - 1);
232     padding[padding_len - 1] = padding_len - 1;
233     if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out_tag + tag_len, &len, padding,
234                            (int)padding_len)) {
235       return 0;
236     }
237     tag_len += len;
238   }
239
240   if (!EVP_EncryptFinal_ex(&ssl3_ctx->cipher_ctx, out_tag + tag_len, &len)) {
241     return 0;
242   }
243   tag_len += len;
244   assert(tag_len == aead_ssl3_tag_len(ctx, in_len, extra_in_len));
245
246   *out_tag_len = tag_len;
247   return 1;
248 }
249
250 static int aead_ssl3_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
251                          size_t *out_len, size_t max_out_len,
252                          const uint8_t *nonce, size_t nonce_len,
253                          const uint8_t *in, size_t in_len,
254                          const uint8_t *ad, size_t ad_len) {
255   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
256
257   if (ssl3_ctx->cipher_ctx.encrypt) {
258     // Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction.
259     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
260     return 0;
261   }
262
263   size_t mac_len = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
264   if (in_len < mac_len) {
265     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
266     return 0;
267   }
268
269   if (max_out_len < in_len) {
270     // This requires that the caller provide space for the MAC, even though it
271     // will always be removed on return.
272     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
273     return 0;
274   }
275
276   if (nonce_len != 0) {
277     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
278     return 0;
279   }
280
281   if (ad_len != 11 - 2 /* length bytes */) {
282     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
283     return 0;
284   }
285
286   if (in_len > INT_MAX) {
287     // EVP_CIPHER takes int as input.
288     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
289     return 0;
290   }
291
292   // Decrypt to get the plaintext + MAC + padding.
293   size_t total = 0;
294   int len;
295   if (!EVP_DecryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
296     return 0;
297   }
298   total += len;
299   if (!EVP_DecryptFinal_ex(&ssl3_ctx->cipher_ctx, out + total, &len)) {
300     return 0;
301   }
302   total += len;
303   assert(total == in_len);
304
305   // Remove CBC padding and MAC. This would normally be timing-sensitive, but
306   // SSLv3 CBC ciphers are already broken. Support will be removed eventually.
307   // https://www.openssl.org/~bodo/ssl-poodle.pdf
308   size_t data_len;
309   if (EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
310     unsigned padding_length = out[total - 1];
311     if (total < padding_length + 1 + mac_len) {
312       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
313       return 0;
314     }
315     // The padding must be minimal.
316     if (padding_length + 1 > EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx)) {
317       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
318       return 0;
319     }
320     data_len = total - padding_length - 1 - mac_len;
321   } else {
322     data_len = total - mac_len;
323   }
324
325   // Compute the MAC and compare against the one in the record.
326   uint8_t mac[EVP_MAX_MD_SIZE];
327   if (!ssl3_mac(ssl3_ctx, mac, NULL, ad, ad_len, out, data_len)) {
328     return 0;
329   }
330   if (CRYPTO_memcmp(&out[data_len], mac, mac_len) != 0) {
331     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
332     return 0;
333   }
334
335   *out_len = data_len;
336   return 1;
337 }
338
339 static int aead_ssl3_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
340                             size_t *out_iv_len) {
341   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
342   const size_t iv_len = EVP_CIPHER_CTX_iv_length(&ssl3_ctx->cipher_ctx);
343   if (iv_len <= 1) {
344     return 0;
345   }
346
347   *out_iv = ssl3_ctx->cipher_ctx.iv;
348   *out_iv_len = iv_len;
349   return 1;
350 }
351
352 static int aead_aes_128_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
353                                            size_t key_len, size_t tag_len,
354                                            enum evp_aead_direction_t dir) {
355   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
356                         EVP_sha1());
357 }
358
359 static int aead_aes_256_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
360                                            size_t key_len, size_t tag_len,
361                                            enum evp_aead_direction_t dir) {
362   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
363                         EVP_sha1());
364 }
365 static int aead_des_ede3_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx,
366                                             const uint8_t *key, size_t key_len,
367                                             size_t tag_len,
368                                             enum evp_aead_direction_t dir) {
369   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
370                         EVP_sha1());
371 }
372
373 static int aead_null_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
374                                     size_t key_len, size_t tag_len,
375                                     enum evp_aead_direction_t dir) {
376   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
377                         EVP_sha1());
378 }
379
380 static const EVP_AEAD aead_aes_128_cbc_sha1_ssl3 = {
381     SHA_DIGEST_LENGTH + 16 + 16,  // key len (SHA1 + AES128 + IV)
382     0,                            // nonce len
383     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
384     SHA_DIGEST_LENGTH,            // max tag length
385     0,                            // seal_scatter_supports_extra_in
386
387     NULL,  // init
388     aead_aes_128_cbc_sha1_ssl3_init,
389     aead_ssl3_cleanup,
390     aead_ssl3_open,
391     aead_ssl3_seal_scatter,
392     NULL,  // open_gather
393     aead_ssl3_get_iv,
394     aead_ssl3_tag_len,
395 };
396
397 static const EVP_AEAD aead_aes_256_cbc_sha1_ssl3 = {
398     SHA_DIGEST_LENGTH + 32 + 16,  // key len (SHA1 + AES256 + IV)
399     0,                            // nonce len
400     16 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
401     SHA_DIGEST_LENGTH,            // max tag length
402     0,                            // seal_scatter_supports_extra_in
403
404     NULL,  // init
405     aead_aes_256_cbc_sha1_ssl3_init,
406     aead_ssl3_cleanup,
407     aead_ssl3_open,
408     aead_ssl3_seal_scatter,
409     NULL,  // open_gather
410     aead_ssl3_get_iv,
411     aead_ssl3_tag_len,
412 };
413
414 static const EVP_AEAD aead_des_ede3_cbc_sha1_ssl3 = {
415     SHA_DIGEST_LENGTH + 24 + 8,  // key len (SHA1 + 3DES + IV)
416     0,                           // nonce len
417     8 + SHA_DIGEST_LENGTH,       // overhead (padding + SHA1)
418     SHA_DIGEST_LENGTH,           // max tag length
419     0,                           // seal_scatter_supports_extra_in
420
421     NULL,  // init
422     aead_des_ede3_cbc_sha1_ssl3_init,
423     aead_ssl3_cleanup,
424     aead_ssl3_open,
425     aead_ssl3_seal_scatter,
426     NULL,  // open_gather
427     aead_ssl3_get_iv,
428     aead_ssl3_tag_len,
429 };
430
431 static const EVP_AEAD aead_null_sha1_ssl3 = {
432     SHA_DIGEST_LENGTH,  // key len
433     0,                  // nonce len
434     SHA_DIGEST_LENGTH,  // overhead (SHA1)
435     SHA_DIGEST_LENGTH,  // max tag length
436     0,                  // seal_scatter_supports_extra_in
437
438     NULL,  // init
439     aead_null_sha1_ssl3_init,
440     aead_ssl3_cleanup,
441     aead_ssl3_open,
442     aead_ssl3_seal_scatter,
443     NULL,  // open_gather
444     NULL,  // get_iv
445     aead_ssl3_tag_len,
446 };
447
448 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void) {
449   return &aead_aes_128_cbc_sha1_ssl3;
450 }
451
452 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void) {
453   return &aead_aes_256_cbc_sha1_ssl3;
454 }
455
456 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void) {
457   return &aead_des_ede3_cbc_sha1_ssl3;
458 }
459
460 const EVP_AEAD *EVP_aead_null_sha1_ssl3(void) { return &aead_null_sha1_ssl3; }