• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #ifndef OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
11 #define OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
12 
13 #include <assert.h>
14 #include <stdlib.h>
15 
16 #include <openssl/base.h>
17 
18 #include "../internal.h"
19 
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
23 
24 
25 // EVP_tls_cbc_get_padding determines the padding from the decrypted, TLS, CBC
26 // record in |in|. This decrypted record should not include any "decrypted"
27 // explicit IV. If the record is publicly invalid, it returns zero. Otherwise,
28 // it returns one and sets |*out_padding_ok| to all ones (0xfff..f) if the
29 // padding is valid and zero otherwise. It then sets |*out_len| to the length
30 // with the padding removed or |in_len| if invalid.
31 //
32 // If the function returns one, it runs in time independent of the contents of
33 // |in|. It is also guaranteed that |*out_len| >= |mac_size|, satisfying
34 // |EVP_tls_cbc_copy_mac|'s precondition.
35 int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
36                                const uint8_t *in, size_t in_len,
37                                size_t block_size, size_t mac_size);
38 
39 // EVP_tls_cbc_copy_mac copies |md_size| bytes from the end of the first
40 // |in_len| bytes of |in| to |out| in constant time (independent of the concrete
41 // value of |in_len|, which may vary within a 256-byte window). |in| must point
42 // to a buffer of |orig_len| bytes.
43 //
44 // On entry:
45 //   orig_len >= in_len >= md_size
46 //   md_size <= EVP_MAX_MD_SIZE
47 void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
48                           size_t in_len, size_t orig_len);
49 
50 // EVP_tls_cbc_record_digest_supported returns 1 iff |md| is a hash function
51 // which EVP_tls_cbc_digest_record supports.
52 int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
53 
54 // EVP_sha1_final_with_secret_suffix computes the result of hashing |len| bytes
55 // from |in| to |ctx| and writes the resulting hash to |out|. |len| is treated
56 // as secret and must be at most |max_len|, which is treated as public. |in|
57 // must point to a buffer of at least |max_len| bytes. It returns one on success
58 // and zero if inputs are too long.
59 //
60 // This function is exported for unit tests.
61 OPENSSL_EXPORT int EVP_sha1_final_with_secret_suffix(
62     SHA_CTX *ctx, uint8_t out[SHA_DIGEST_LENGTH], const uint8_t *in, size_t len,
63     size_t max_len);
64 
65 // EVP_sha256_final_with_secret_suffix acts like
66 // |EVP_sha1_final_with_secret_suffix|, but for SHA-256.
67 //
68 // This function is exported for unit tests.
69 OPENSSL_EXPORT int EVP_sha256_final_with_secret_suffix(
70     SHA256_CTX *ctx, uint8_t out[SHA256_DIGEST_LENGTH], const uint8_t *in,
71     size_t len, size_t max_len);
72 
73 // EVP_tls_cbc_digest_record computes the MAC of a decrypted, padded TLS
74 // record.
75 //
76 //   md: the hash function used in the HMAC.
77 //     EVP_tls_cbc_record_digest_supported must return true for this hash.
78 //   md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
79 //   md_out_size: the number of output bytes is written here.
80 //   header: the 13-byte, TLS record header.
81 //   data: the record data itself
82 //   data_size: the secret, reported length of the data once the padding and MAC
83 //     have been removed.
84 //   data_plus_mac_plus_padding_size: the public length of the whole
85 //     record, including padding.
86 //
87 // On entry: by virtue of having been through one of the remove_padding
88 // functions, above, we know that data_plus_mac_size is large enough to contain
89 // a padding byte and MAC. (If the padding was invalid, it might contain the
90 // padding too. )
91 int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
92                               size_t *md_out_size, const uint8_t header[13],
93                               const uint8_t *data, size_t data_size,
94                               size_t data_plus_mac_plus_padding_size,
95                               const uint8_t *mac_secret,
96                               unsigned mac_secret_length);
97 
98 #define POLY1305_TAG_LEN 16
99 
100 // For convenience (the x86_64 calling convention allows only six parameters in
101 // registers), the final parameter for the assembly functions is both an input
102 // and output parameter.
103 union chacha20_poly1305_open_data {
104   struct {
105     alignas(16) uint8_t key[32];
106     uint32_t counter;
107     uint8_t nonce[12];
108   } in;
109   struct {
110     uint8_t tag[POLY1305_TAG_LEN];
111   } out;
112 };
113 
114 union chacha20_poly1305_seal_data {
115   struct {
116     alignas(16) uint8_t key[32];
117     uint32_t counter;
118     uint8_t nonce[12];
119     const uint8_t *extra_ciphertext;
120     size_t extra_ciphertext_len;
121   } in;
122   struct {
123     uint8_t tag[POLY1305_TAG_LEN];
124   } out;
125 };
126 
127 #if (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) &&  \
128     !defined(OPENSSL_NO_ASM)
129 
130 static_assert(sizeof(union chacha20_poly1305_open_data) == 48,
131               "wrong chacha20_poly1305_open_data size");
132 static_assert(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8,
133               "wrong chacha20_poly1305_seal_data size");
134 
chacha20_poly1305_asm_capable(void)135 inline int chacha20_poly1305_asm_capable(void) {
136 #if defined(OPENSSL_X86_64)
137   return CRYPTO_is_SSE4_1_capable();
138 #elif defined(OPENSSL_AARCH64)
139   return CRYPTO_is_NEON_capable();
140 #endif
141 }
142 
143 // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It decrypts
144 // |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
145 // Additional input parameters are passed in |aead_data->in|. On exit, it will
146 // write calculated tag value to |aead_data->out.tag|, which the caller must
147 // check.
148 #if defined(OPENSSL_X86_64)
149 extern void chacha20_poly1305_open_nohw(
150     uint8_t *out_plaintext, const uint8_t *ciphertext, size_t plaintext_len,
151     const uint8_t *ad, size_t ad_len, union chacha20_poly1305_open_data *data);
152 extern void chacha20_poly1305_open_avx2(
153     uint8_t *out_plaintext, const uint8_t *ciphertext, size_t plaintext_len,
154     const uint8_t *ad, size_t ad_len, union chacha20_poly1305_open_data *data);
chacha20_poly1305_open(uint8_t * out_plaintext,const uint8_t * ciphertext,size_t plaintext_len,const uint8_t * ad,size_t ad_len,union chacha20_poly1305_open_data * data)155 inline void chacha20_poly1305_open(uint8_t *out_plaintext,
156                                    const uint8_t *ciphertext,
157                                    size_t plaintext_len, const uint8_t *ad,
158                                    size_t ad_len,
159                                    union chacha20_poly1305_open_data *data) {
160   if (CRYPTO_is_AVX2_capable() && CRYPTO_is_BMI2_capable()) {
161     chacha20_poly1305_open_avx2(out_plaintext, ciphertext, plaintext_len, ad,
162                                 ad_len, data);
163   } else {
164     chacha20_poly1305_open_nohw(out_plaintext, ciphertext, plaintext_len, ad,
165                                 ad_len, data);
166   }
167 }
168 #else
169 extern void chacha20_poly1305_open(uint8_t *out_plaintext,
170                                    const uint8_t *ciphertext,
171                                    size_t plaintext_len, const uint8_t *ad,
172                                    size_t ad_len,
173                                    union chacha20_poly1305_open_data *data);
174 #endif
175 
176 // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It encrypts
177 // |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
178 // Additional input parameters are passed in |aead_data->in|. The calculated tag
179 // value is over the computed ciphertext concatenated with |extra_ciphertext|
180 // and written to |aead_data->out.tag|.
181 #if defined(OPENSSL_X86_64)
182 extern void chacha20_poly1305_seal_nohw(
183     uint8_t *out_ciphertext, const uint8_t *plaintext, size_t plaintext_len,
184     const uint8_t *ad, size_t ad_len, union chacha20_poly1305_seal_data *data);
185 extern void chacha20_poly1305_seal_avx2(
186     uint8_t *out_ciphertext, const uint8_t *plaintext, size_t plaintext_len,
187     const uint8_t *ad, size_t ad_len, union chacha20_poly1305_seal_data *data);
chacha20_poly1305_seal(uint8_t * out_ciphertext,const uint8_t * plaintext,size_t plaintext_len,const uint8_t * ad,size_t ad_len,union chacha20_poly1305_seal_data * data)188 inline void chacha20_poly1305_seal(uint8_t *out_ciphertext,
189                                    const uint8_t *plaintext,
190                                    size_t plaintext_len, const uint8_t *ad,
191                                    size_t ad_len,
192                                    union chacha20_poly1305_seal_data *data) {
193   if (CRYPTO_is_AVX2_capable() && CRYPTO_is_BMI2_capable()) {
194     chacha20_poly1305_seal_avx2(out_ciphertext, plaintext, plaintext_len, ad,
195                                 ad_len, data);
196   } else {
197     chacha20_poly1305_seal_nohw(out_ciphertext, plaintext, plaintext_len, ad,
198                                 ad_len, data);
199   }
200 }
201 #else
202 extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
203                                    const uint8_t *plaintext,
204                                    size_t plaintext_len, const uint8_t *ad,
205                                    size_t ad_len,
206                                    union chacha20_poly1305_seal_data *data);
207 #endif
208 
209 #else
210 
chacha20_poly1305_asm_capable(void)211 inline int chacha20_poly1305_asm_capable(void) { return 0; }
212 
chacha20_poly1305_open(uint8_t * out_plaintext,const uint8_t * ciphertext,size_t plaintext_len,const uint8_t * ad,size_t ad_len,union chacha20_poly1305_open_data * data)213 inline void chacha20_poly1305_open(uint8_t *out_plaintext,
214                                    const uint8_t *ciphertext,
215                                    size_t plaintext_len, const uint8_t *ad,
216                                    size_t ad_len,
217                                    union chacha20_poly1305_open_data *data) {
218   abort();
219 }
220 
chacha20_poly1305_seal(uint8_t * out_ciphertext,const uint8_t * plaintext,size_t plaintext_len,const uint8_t * ad,size_t ad_len,union chacha20_poly1305_seal_data * data)221 inline void chacha20_poly1305_seal(uint8_t *out_ciphertext,
222                                    const uint8_t *plaintext,
223                                    size_t plaintext_len, const uint8_t *ad,
224                                    size_t ad_len,
225                                    union chacha20_poly1305_seal_data *data) {
226   abort();
227 }
228 #endif
229 
230 
231 #if defined(__cplusplus)
232 }  // extern C
233 #endif
234 
235 #endif  // OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
236