| 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 |
| 23 | #include "../internal.h" |
| 24 |
| 25 | #if defined(__cplusplus) |
| 26 | extern "C" { |
| 27 | #endif |
| 28 |
| 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 |*out_len| >= |mac_size|, satisfying |
| 39 | // |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 | // header: the 13-byte, TLS record header. |
| 86 | // data: the record data itself |
| 87 | // data_size: the secret, reported length of the data once the padding and MAC |
| 88 | // have been removed. |
| 89 | // data_plus_mac_plus_padding_size: the public length of the whole |
| 90 | // record, including padding. |
| 91 | // |
| 92 | // On entry: by virtue of having been through one of the remove_padding |
| 93 | // functions, above, we know that data_plus_mac_size is large enough to contain |
| 94 | // a padding byte and MAC. (If the padding was invalid, it might contain the |
| 95 | // padding too. ) |
| 96 | int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out, |
| 97 | size_t *md_out_size, const uint8_t header[13], |
| 98 | const uint8_t *data, size_t data_size, |
| 99 | size_t data_plus_mac_plus_padding_size, |
| 100 | const uint8_t *mac_secret, |
| 101 | unsigned mac_secret_length); |
| 102 |
| 103 | #define POLY1305_TAG_LEN 16 |
| 104 |
| 105 | // For convenience (the x86_64 calling convention allows only six parameters in |
| 106 | // registers), the final parameter for the assembly functions is both an input |
| 107 | // and output parameter. |
| 108 | union chacha20_poly1305_open_data { |
| 109 | struct { |
| 110 | alignas(16) uint8_t key[32]; |
| 111 | uint32_t counter; |
| 112 | uint8_t nonce[12]; |
| 113 | } in; |
| 114 | struct { |
| 115 | uint8_t tag[POLY1305_TAG_LEN]; |
| 116 | } out; |
| 117 | }; |
| 118 |
| 119 | union chacha20_poly1305_seal_data { |
| 120 | struct { |
| 121 | alignas(16) uint8_t key[32]; |
| 122 | uint32_t counter; |
| 123 | uint8_t nonce[12]; |
| 124 | const uint8_t *extra_ciphertext; |
| 125 | size_t extra_ciphertext_len; |
| 126 | } in; |
| 127 | struct { |
| 128 | uint8_t tag[POLY1305_TAG_LEN]; |
| 129 | } out; |
| 130 | }; |
| 131 |
| 132 | #if (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \ |
| 133 | !defined(OPENSSL_NO_ASM) |
| 134 |
| 135 | static_assert(sizeof(union chacha20_poly1305_open_data) == 48, |
| 136 | "wrong chacha20_poly1305_open_data size"); |
| 137 | static_assert(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8, |
| 138 | "wrong chacha20_poly1305_seal_data size"); |
| 139 |
| 140 | inline int chacha20_poly1305_asm_capable(void) { |
| 141 | #if defined(OPENSSL_X86_64) |
| 142 | return CRYPTO_is_SSE4_1_capable(); |
| 143 | #elif defined(OPENSSL_AARCH64) |
| 144 | return CRYPTO_is_NEON_capable(); |
| 145 | #endif |
| 146 | } |
| 147 |
| 148 | // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It decrypts |
| 149 | // |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|. |
| 150 | // Additional input parameters are passed in |aead_data->in|. On exit, it will |
| 151 | // write calculated tag value to |aead_data->out.tag|, which the caller must |
| 152 | // check. |
| 153 | #if defined(OPENSSL_X86_64) |
| 154 | extern void chacha20_poly1305_open_sse41( |
| 155 | uint8_t *out_plaintext, const uint8_t *ciphertext, size_t plaintext_len, |
| 156 | const uint8_t *ad, size_t ad_len, union chacha20_poly1305_open_data *data); |
| 157 | extern void chacha20_poly1305_open_avx2( |
| 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 | inline void chacha20_poly1305_open(uint8_t *out_plaintext, |
| 161 | const uint8_t *ciphertext, |
| 162 | size_t plaintext_len, const uint8_t *ad, |
| 163 | size_t ad_len, |
| 164 | union chacha20_poly1305_open_data *data) { |
| 165 | if (CRYPTO_is_AVX2_capable() && CRYPTO_is_BMI2_capable()) { |
| 166 | chacha20_poly1305_open_avx2(out_plaintext, ciphertext, plaintext_len, ad, |
| 167 | ad_len, data); |
| 168 | } else { |
| 169 | chacha20_poly1305_open_sse41(out_plaintext, ciphertext, plaintext_len, ad, |
| 170 | ad_len, data); |
| 171 | } |
| 172 | } |
| 173 | #else |
| 174 | extern void chacha20_poly1305_open(uint8_t *out_plaintext, |
| 175 | const uint8_t *ciphertext, |
| 176 | size_t plaintext_len, const uint8_t *ad, |
| 177 | size_t ad_len, |
| 178 | union chacha20_poly1305_open_data *data); |
| 179 | #endif |
| 180 |
| 181 | // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It encrypts |
| 182 | // |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|. |
| 183 | // Additional input parameters are passed in |aead_data->in|. The calculated tag |
| 184 | // value is over the computed ciphertext concatenated with |extra_ciphertext| |
| 185 | // and written to |aead_data->out.tag|. |
| 186 | #if defined(OPENSSL_X86_64) |
| 187 | extern void chacha20_poly1305_seal_sse41( |
| 188 | uint8_t *out_ciphertext, const uint8_t *plaintext, size_t plaintext_len, |
| 189 | const uint8_t *ad, size_t ad_len, union chacha20_poly1305_seal_data *data); |
| 190 | extern void chacha20_poly1305_seal_avx2( |
| 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 | inline void chacha20_poly1305_seal(uint8_t *out_ciphertext, |
| 194 | const uint8_t *plaintext, |
| 195 | size_t plaintext_len, const uint8_t *ad, |
| 196 | size_t ad_len, |
| 197 | union chacha20_poly1305_seal_data *data) { |
| 198 | if (CRYPTO_is_AVX2_capable() && CRYPTO_is_BMI2_capable()) { |
| 199 | chacha20_poly1305_seal_avx2(out_ciphertext, plaintext, plaintext_len, ad, |
| 200 | ad_len, data); |
| 201 | } else { |
| 202 | chacha20_poly1305_seal_sse41(out_ciphertext, plaintext, plaintext_len, ad, |
| 203 | ad_len, data); |
| 204 | } |
| 205 | } |
| 206 | #else |
| 207 | extern void chacha20_poly1305_seal(uint8_t *out_ciphertext, |
| 208 | const uint8_t *plaintext, |
| 209 | size_t plaintext_len, const uint8_t *ad, |
| 210 | size_t ad_len, |
| 211 | union chacha20_poly1305_seal_data *data); |
| 212 | #endif |
| 213 |
| 214 | #else |
| 215 |
| 216 | inline int chacha20_poly1305_asm_capable(void) { return 0; } |
| 217 |
| 218 | inline void chacha20_poly1305_open(uint8_t *out_plaintext, |
| 219 | const uint8_t *ciphertext, |
| 220 | size_t plaintext_len, const uint8_t *ad, |
| 221 | size_t ad_len, |
| 222 | union chacha20_poly1305_open_data *data) { |
| 223 | abort(); |
| 224 | } |
| 225 |
| 226 | inline void chacha20_poly1305_seal(uint8_t *out_ciphertext, |
| 227 | const uint8_t *plaintext, |
| 228 | size_t plaintext_len, const uint8_t *ad, |
| 229 | size_t ad_len, |
| 230 | union chacha20_poly1305_seal_data *data) { |
| 231 | abort(); |
| 232 | } |
| 233 | #endif |
| 234 |
| 235 |
| 236 | #if defined(__cplusplus) |
| 237 | } // extern C |
| 238 | #endif |
| 239 |
| 240 | #endif // OPENSSL_HEADER_CRYPTO_CIPHER_INTERNAL_H |
| 241 |