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