Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/cipher_extra/internal.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
5 #define OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
6
7 #include <stdlib.h>
8
9 #include <openssl/base.h>
10 #include <openssl/cipher.h>
11 #include <openssl/type_check.h>
12
13 #include "../internal.h"
14 #include "../fipsmodule/cpucap/internal.h"
15
16 #if defined(__cplusplus)
17 extern "C" {
18 #endif
19
20 OPENSSL_EXPORT int x86_64_assembly_implementation_FOR_TESTING(void);
21
22 #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
23 !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX) && !defined(AWSLC_FIPS)
24 #define AES_CBC_HMAC_SHA_STITCH
25 // TLS1_1_VERSION is also defined in ssl.h.
26 #define TLS1_1_VERSION 0x0302
27 #define NO_PAYLOAD_LENGTH ((size_t)-1)
28 #define HMAC_KEY_SIZE 64
29 #endif
30
31 // EVP_tls_cbc_get_padding determines the padding from the decrypted, TLS, CBC
32 // record in |in|. This decrypted record should not include any "decrypted"
33 // explicit IV. If the record is publicly invalid, it returns zero. Otherwise,
34 // it returns one and sets |*out_padding_ok| to all ones (0xfff..f) if the
35 // padding is valid and zero otherwise. It then sets |*out_len| to the length
36 // with the padding removed or |in_len| if invalid.
37 //
38 // If the function returns one, it runs in time independent of the contents of
39 // |in|. It is also guaranteed that |*out_len| >= |mac_size|, satisfying
40 // |EVP_tls_cbc_copy_mac|'s precondition.
41 int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
42 const uint8_t *in, size_t in_len,
43 size_t block_size, size_t mac_size);
44
45 // EVP_tls_cbc_copy_mac copies |md_size| bytes from the end of the first
46 // |in_len| bytes of |in| to |out| in constant time (independent of the concrete
47 // value of |in_len|, which may vary within a 256-byte window). |in| must point
48 // to a buffer of |orig_len| bytes.
49 //
50 // On entry:
51 // orig_len >= in_len >= md_size
52 // md_size <= EVP_MAX_MD_SIZE
53 void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
54 size_t in_len, size_t orig_len);
55
56 // EVP_tls_cbc_record_digest_supported returns 1 iff |md| is a hash function
57 // which EVP_tls_cbc_digest_record supports.
58 int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
59
60 // EVP_final_with_secret_suffix_sha1, EVP_final_with_secret_suffix_sha256, and
61 // EVP_final_with_secret_suffix_sha384 compute the result of hashing |len|
62 // bytes from |in| to |ctx| and write the resulting hash to |out|.
63 // |len| is treated as secret and must be at most |max_len|, which is treated
64 // as public. |in| must point to a buffer of at least |max_len| bytes.
65 // It returns one on success and zero if inputs are too long.
66 //
67 // The functions are exported for unit tests.
68 OPENSSL_EXPORT int EVP_final_with_secret_suffix_sha1(
69 SHA_CTX *ctx, uint8_t out[SHA_DIGEST_LENGTH], const uint8_t *in, size_t len,
70 size_t max_len);
71
72 OPENSSL_EXPORT int EVP_final_with_secret_suffix_sha256(
73 SHA256_CTX *ctx, uint8_t out[SHA256_DIGEST_LENGTH], const uint8_t *in,
74 size_t len, size_t max_len);
75
76 OPENSSL_EXPORT int EVP_final_with_secret_suffix_sha384(
77 SHA512_CTX *ctx, uint8_t out[SHA384_DIGEST_LENGTH], const uint8_t *in,
78 size_t len, size_t max_len);
79
80 // EVP_tls_cbc_digest_record computes the MAC of a decrypted, padded TLS
81 // record.
82 //
83 // md: the hash function used in the HMAC.
84 // EVP_tls_cbc_record_digest_supported must return true for this hash.
85 // md_out: the digest output. At most EVP_MAX_MD_SIZE bytes will be written.
86 // md_out_size: the number of output bytes is written here.
87 // header: the 13-byte, TLS record header.
88 // data: the record data itself
89 // data_size: the secret, reported length of the data once the padding and MAC
90 // have been removed.
91 // data_plus_mac_plus_padding_size: the public length of the whole
92 // record, including padding.
93 //
94 // On entry: by virtue of having been through one of the remove_padding
95 // functions, above, we know that data_plus_mac_size is large enough to contain
96 // a padding byte and MAC. (If the padding was invalid, it might contain the
97 // padding too. )
98 int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
99 size_t *md_out_size, const uint8_t header[13],
100 const uint8_t *data, size_t data_size,
101 size_t data_plus_mac_plus_padding_size,
102 const uint8_t *mac_secret,
103 unsigned mac_secret_length);
104
105 #define POLY1305_TAG_LEN 16
106
107 // For convenience (the x86_64 calling convention allows only six parameters in
108 // registers), the final parameter for the assembly functions is both an input
109 // and output parameter.
110 union chacha20_poly1305_open_data {
111 struct {
112 alignas(16) uint8_t key[32];
113 uint32_t counter;
114 uint8_t nonce[12];
115 } in;
116 struct {
117 uint8_t tag[POLY1305_TAG_LEN];
118 } out;
119 };
120
121 union chacha20_poly1305_seal_data {
122 struct {
123 alignas(16) uint8_t key[32];
124 uint32_t counter;
125 uint8_t nonce[12];
126 const uint8_t *extra_ciphertext;
127 size_t extra_ciphertext_len;
128 } in;
129 struct {
130 uint8_t tag[POLY1305_TAG_LEN];
131 } out;
132 };
133
134 #if (defined(OPENSSL_X86_64) || defined(OPENSSL_AARCH64)) && \
135 !defined(OPENSSL_NO_ASM)
136
137 OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_open_data) == 48,
138 _wrong_chacha20_poly1305_open_data_size)
139 OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8,
140 _wrong_chacha20_poly1305_seal_data_size)
141
142 OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) {
143 #if defined(OPENSSL_X86_64)
144 return CRYPTO_is_SSE4_1_capable();
145 #elif defined(OPENSSL_AARCH64)
146 return CRYPTO_is_NEON_capable();
147 #endif
148 }
149
150 // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It decrypts
151 // |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
152 // Additional input parameters are passed in |aead_data->in|. On exit, it will
153 // write calculated tag value to |aead_data->out.tag|, which the caller must
154 // check.
155 extern 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
161 // chacha20_poly1305_open is defined in chacha20_poly1305_*.pl. It encrypts
162 // |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
163 // Additional input parameters are passed in |aead_data->in|. The calculated tag
164 // value is over the computed ciphertext concatenated with |extra_ciphertext|
165 // and written to |aead_data->out.tag|.
166 extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
167 const uint8_t *plaintext,
168 size_t plaintext_len, const uint8_t *ad,
169 size_t ad_len,
170 union chacha20_poly1305_seal_data *data);
171 #else
172
173 OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) { return 0; }
174
175 OPENSSL_INLINE void chacha20_poly1305_open(uint8_t *out_plaintext,
176 const uint8_t *ciphertext,
177 size_t plaintext_len, const uint8_t *ad,
178 size_t ad_len,
179 union chacha20_poly1305_open_data *data) {
180 abort();
181 }
182
183 OPENSSL_INLINE void chacha20_poly1305_seal(uint8_t *out_ciphertext,
184 const uint8_t *plaintext,
185 size_t plaintext_len, const uint8_t *ad,
186 size_t ad_len,
187 union chacha20_poly1305_seal_data *data) {
188 abort();
189 }
190 #endif
191
192
193 #if defined(__cplusplus)
194 } // extern C
195 #endif
196
197 #endif // OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H