cipher.h (26700B)
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_CIPHER_H 16 #define OPENSSL_HEADER_CIPHER_H 17 18 #include <openssl/base.h> // IWYU pragma: export 19 20 #if defined(__cplusplus) 21 extern "C" { 22 #endif 23 24 25 // Ciphers. 26 27 28 // Cipher primitives. 29 // 30 // The following functions return |EVP_CIPHER| objects that implement the named 31 // cipher algorithm. 32 33 OPENSSL_EXPORT const EVP_CIPHER *EVP_rc4(void); 34 35 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_cbc(void); 36 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ecb(void); 37 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede(void); 38 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3(void); 39 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede_cbc(void); 40 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3_cbc(void); 41 42 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_ecb(void); 43 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cbc(void); 44 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_ctr(void); 45 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_ofb(void); 46 47 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_ecb(void); 48 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cbc(void); 49 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_ctr(void); 50 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_ofb(void); 51 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_xts(void); 52 53 // EVP_enc_null returns a 'cipher' that passes plaintext through as 54 // ciphertext. 55 OPENSSL_EXPORT const EVP_CIPHER *EVP_enc_null(void); 56 57 // EVP_rc2_cbc returns a cipher that implements 128-bit RC2 in CBC mode. 58 OPENSSL_EXPORT const EVP_CIPHER *EVP_rc2_cbc(void); 59 60 // EVP_rc2_40_cbc returns a cipher that implements 40-bit RC2 in CBC mode. This 61 // is obviously very, very weak and is included only in order to read PKCS#12 62 // files, which often encrypt the certificate chain using this cipher. It is 63 // deliberately not exported. 64 const EVP_CIPHER *EVP_rc2_40_cbc(void); 65 66 // EVP_get_cipherbynid returns the cipher corresponding to the given NID, or 67 // NULL if no such cipher is known. Note using this function links almost every 68 // cipher implemented by BoringSSL into the binary, whether the caller uses them 69 // or not. Size-conscious callers, such as client software, should not use this 70 // function. 71 OPENSSL_EXPORT const EVP_CIPHER *EVP_get_cipherbynid(int nid); 72 73 74 // Cipher context allocation. 75 // 76 // An |EVP_CIPHER_CTX| represents the state of an encryption or decryption in 77 // progress. 78 79 // EVP_CIPHER_CTX_init initialises an, already allocated, |EVP_CIPHER_CTX|. 80 OPENSSL_EXPORT void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx); 81 82 // EVP_CIPHER_CTX_new allocates a fresh |EVP_CIPHER_CTX|, calls 83 // |EVP_CIPHER_CTX_init| and returns it, or NULL on allocation failure. 84 OPENSSL_EXPORT EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void); 85 86 // EVP_CIPHER_CTX_cleanup frees any memory referenced by |ctx|. It returns 87 // one. 88 OPENSSL_EXPORT int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *ctx); 89 90 // EVP_CIPHER_CTX_free calls |EVP_CIPHER_CTX_cleanup| on |ctx| and then frees 91 // |ctx| itself. 92 OPENSSL_EXPORT void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx); 93 94 // EVP_CIPHER_CTX_copy sets |out| to be a duplicate of the current state of 95 // |in|. The |out| argument must have been previously initialised. 96 OPENSSL_EXPORT int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, 97 const EVP_CIPHER_CTX *in); 98 99 // EVP_CIPHER_CTX_reset calls |EVP_CIPHER_CTX_cleanup| followed by 100 // |EVP_CIPHER_CTX_init| and returns one. 101 OPENSSL_EXPORT int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx); 102 103 104 // Cipher context configuration. 105 106 // EVP_CipherInit_ex configures |ctx| for a fresh encryption (or decryption, if 107 // |enc| is zero) operation using |cipher|. If |ctx| has been previously 108 // configured with a cipher then |cipher|, |key| and |iv| may be |NULL| and 109 // |enc| may be -1 to reuse the previous values. The operation will use |key| 110 // as the key and |iv| as the IV (if any). These should have the correct 111 // lengths given by |EVP_CIPHER_key_length| and |EVP_CIPHER_iv_length|. It 112 // returns one on success and zero on error. 113 OPENSSL_EXPORT int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, 114 const EVP_CIPHER *cipher, ENGINE *engine, 115 const uint8_t *key, const uint8_t *iv, 116 int enc); 117 118 // EVP_EncryptInit_ex calls |EVP_CipherInit_ex| with |enc| equal to one. 119 OPENSSL_EXPORT int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, 120 const EVP_CIPHER *cipher, ENGINE *impl, 121 const uint8_t *key, const uint8_t *iv); 122 123 // EVP_DecryptInit_ex calls |EVP_CipherInit_ex| with |enc| equal to zero. 124 OPENSSL_EXPORT int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, 125 const EVP_CIPHER *cipher, ENGINE *impl, 126 const uint8_t *key, const uint8_t *iv); 127 128 129 // Cipher operations. 130 131 // EVP_EncryptUpdate encrypts |in_len| bytes from |in| to |out|. The number 132 // of output bytes may be up to |in_len| plus the block length minus one and 133 // |out| must have sufficient space. The number of bytes actually output is 134 // written to |*out_len|. It returns one on success and zero otherwise. 135 // 136 // If |ctx| is an AEAD cipher, e.g. |EVP_aes_128_gcm|, and |out| is NULL, this 137 // function instead adds |in_len| bytes from |in| to the AAD and sets |*out_len| 138 // to |in_len|. The AAD must be fully specified in this way before this function 139 // is used to encrypt plaintext. 140 OPENSSL_EXPORT int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, 141 int *out_len, const uint8_t *in, 142 int in_len); 143 144 // EVP_EncryptFinal_ex writes at most a block of ciphertext to |out| and sets 145 // |*out_len| to the number of bytes written. If padding is enabled (the 146 // default) then standard padding is applied to create the final block. If 147 // padding is disabled (with |EVP_CIPHER_CTX_set_padding|) then any partial 148 // block remaining will cause an error. The function returns one on success and 149 // zero otherwise. 150 OPENSSL_EXPORT int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, 151 int *out_len); 152 153 // EVP_DecryptUpdate decrypts |in_len| bytes from |in| to |out|. The number of 154 // output bytes may be up to |in_len| plus the block length minus one and |out| 155 // must have sufficient space. The number of bytes actually output is written 156 // to |*out_len|. It returns one on success and zero otherwise. 157 // 158 // If |ctx| is an AEAD cipher, e.g. |EVP_aes_128_gcm|, and |out| is NULL, this 159 // function instead adds |in_len| bytes from |in| to the AAD and sets |*out_len| 160 // to |in_len|. The AAD must be fully specified in this way before this function 161 // is used to decrypt ciphertext. 162 OPENSSL_EXPORT int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, 163 int *out_len, const uint8_t *in, 164 int in_len); 165 166 // EVP_DecryptFinal_ex writes at most a block of ciphertext to |out| and sets 167 // |*out_len| to the number of bytes written. If padding is enabled (the 168 // default) then padding is removed from the final block. 169 // 170 // WARNING: it is unsafe to call this function with unauthenticated 171 // ciphertext if padding is enabled. 172 OPENSSL_EXPORT int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, 173 int *out_len); 174 175 // EVP_CipherUpdate calls either |EVP_EncryptUpdate| or |EVP_DecryptUpdate| 176 // depending on how |ctx| has been setup. 177 OPENSSL_EXPORT int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, 178 int *out_len, const uint8_t *in, 179 int in_len); 180 181 // EVP_CipherFinal_ex calls either |EVP_EncryptFinal_ex| or 182 // |EVP_DecryptFinal_ex| depending on how |ctx| has been setup. 183 OPENSSL_EXPORT int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, 184 int *out_len); 185 186 187 // Cipher context accessors. 188 189 // EVP_CIPHER_CTX_cipher returns the |EVP_CIPHER| underlying |ctx|, or NULL if 190 // none has been set. 191 OPENSSL_EXPORT const EVP_CIPHER *EVP_CIPHER_CTX_cipher( 192 const EVP_CIPHER_CTX *ctx); 193 194 // EVP_CIPHER_CTX_nid returns a NID identifying the |EVP_CIPHER| underlying 195 // |ctx| (e.g. |NID_aes_128_gcm|). It will crash if no cipher has been 196 // configured. 197 OPENSSL_EXPORT int EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx); 198 199 // EVP_CIPHER_CTX_encrypting returns one if |ctx| is configured for encryption 200 // and zero otherwise. 201 OPENSSL_EXPORT int EVP_CIPHER_CTX_encrypting(const EVP_CIPHER_CTX *ctx); 202 203 // EVP_CIPHER_CTX_block_size returns the block size, in bytes, of the cipher 204 // underlying |ctx|, or one if the cipher is a stream cipher. It will crash if 205 // no cipher has been configured. 206 OPENSSL_EXPORT unsigned EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx); 207 208 // EVP_CIPHER_CTX_key_length returns the key size, in bytes, of the cipher 209 // underlying |ctx| or zero if no cipher has been configured. 210 OPENSSL_EXPORT unsigned EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx); 211 212 // EVP_CIPHER_CTX_iv_length returns the IV size, in bytes, of the cipher 213 // underlying |ctx|. It will crash if no cipher has been configured. 214 OPENSSL_EXPORT unsigned EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx); 215 216 // EVP_CIPHER_CTX_get_app_data returns the opaque, application data pointer for 217 // |ctx|, or NULL if none has been set. 218 OPENSSL_EXPORT void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx); 219 220 // EVP_CIPHER_CTX_set_app_data sets the opaque, application data pointer for 221 // |ctx| to |data|. 222 OPENSSL_EXPORT void EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, 223 void *data); 224 225 // EVP_CIPHER_CTX_flags returns a value which is the OR of zero or more 226 // |EVP_CIPH_*| flags. It will crash if no cipher has been configured. 227 OPENSSL_EXPORT uint32_t EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx); 228 229 // EVP_CIPHER_CTX_mode returns one of the |EVP_CIPH_*| cipher mode values 230 // enumerated below. It will crash if no cipher has been configured. 231 OPENSSL_EXPORT uint32_t EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx); 232 233 // EVP_CIPHER_CTX_ctrl is an |ioctl| like function. The |command| argument 234 // should be one of the |EVP_CTRL_*| values. The |arg| and |ptr| arguments are 235 // specific to the command in question. 236 OPENSSL_EXPORT int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int command, 237 int arg, void *ptr); 238 239 // EVP_CIPHER_CTX_set_padding sets whether padding is enabled for |ctx| and 240 // returns one. Pass a non-zero |pad| to enable padding (the default) or zero 241 // to disable. 242 OPENSSL_EXPORT int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad); 243 244 // EVP_CIPHER_CTX_set_key_length sets the key length for |ctx|. This is only 245 // valid for ciphers that can take a variable length key. It returns one on 246 // success and zero on error. 247 OPENSSL_EXPORT int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *ctx, 248 unsigned key_len); 249 250 251 // Cipher accessors. 252 253 // EVP_CIPHER_nid returns a NID identifying |cipher|. (For example, 254 // |NID_aes_128_gcm|.) 255 OPENSSL_EXPORT int EVP_CIPHER_nid(const EVP_CIPHER *cipher); 256 257 // EVP_CIPHER_block_size returns the block size, in bytes, for |cipher|, or one 258 // if |cipher| is a stream cipher. 259 OPENSSL_EXPORT unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher); 260 261 // EVP_CIPHER_key_length returns the key size, in bytes, for |cipher|. If 262 // |cipher| can take a variable key length then this function returns the 263 // default key length and |EVP_CIPHER_flags| will return a value with 264 // |EVP_CIPH_VARIABLE_LENGTH| set. 265 OPENSSL_EXPORT unsigned EVP_CIPHER_key_length(const EVP_CIPHER *cipher); 266 267 // EVP_CIPHER_iv_length returns the IV size, in bytes, of |cipher|, or zero if 268 // |cipher| doesn't take an IV. 269 OPENSSL_EXPORT unsigned EVP_CIPHER_iv_length(const EVP_CIPHER *cipher); 270 271 // EVP_CIPHER_flags returns a value which is the OR of zero or more 272 // |EVP_CIPH_*| flags. 273 OPENSSL_EXPORT uint32_t EVP_CIPHER_flags(const EVP_CIPHER *cipher); 274 275 // EVP_CIPHER_mode returns one of the cipher mode values enumerated below. 276 OPENSSL_EXPORT uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher); 277 278 279 // Key derivation. 280 281 // EVP_BytesToKey generates a key and IV for the cipher |type| by iterating 282 // |md| |count| times using |data| and an optional |salt|, writing the result to 283 // |key| and |iv|. If not NULL, the |key| and |iv| buffers must have enough 284 // space to hold a key and IV for |type|, as returned by |EVP_CIPHER_key_length| 285 // and |EVP_CIPHER_iv_length|. This function returns the length of the key 286 // (without the IV) on success or zero on error. 287 // 288 // If |salt| is NULL, the empty string is used as the salt. Salt lengths other 289 // than 0 and 8 are not supported by this function. Either of |key| or |iv| may 290 // be NULL to skip that output. 291 // 292 // When the total data derived is less than the size of |md|, this function 293 // implements PBKDF1 from RFC 8018. Otherwise, it generalizes PBKDF1 by 294 // computing prepending the previous output to |data| and re-running PBKDF1 for 295 // further output. 296 // 297 // This function is provided for compatibility with legacy uses of PBKDF1. New 298 // applications should use a more modern algorithm, such as |EVP_PBE_scrypt|. 299 OPENSSL_EXPORT int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, 300 const uint8_t salt[8], const uint8_t *data, 301 size_t data_len, unsigned count, uint8_t *key, 302 uint8_t *iv); 303 304 305 // Cipher modes (for |EVP_CIPHER_mode|). 306 307 #define EVP_CIPH_STREAM_CIPHER 0x0 308 #define EVP_CIPH_ECB_MODE 0x1 309 #define EVP_CIPH_CBC_MODE 0x2 310 #define EVP_CIPH_CFB_MODE 0x3 311 #define EVP_CIPH_OFB_MODE 0x4 312 #define EVP_CIPH_CTR_MODE 0x5 313 #define EVP_CIPH_GCM_MODE 0x6 314 #define EVP_CIPH_XTS_MODE 0x7 315 316 // The following values are never returned from |EVP_CIPHER_mode| and are 317 // included only to make it easier to compile code with BoringSSL. 318 #define EVP_CIPH_CCM_MODE 0x8 319 #define EVP_CIPH_OCB_MODE 0x9 320 #define EVP_CIPH_WRAP_MODE 0xa 321 322 323 // Cipher flags (for |EVP_CIPHER_flags|). 324 325 // EVP_CIPH_VARIABLE_LENGTH indicates that the cipher takes a variable length 326 // key. 327 #define EVP_CIPH_VARIABLE_LENGTH 0x40 328 329 // EVP_CIPH_ALWAYS_CALL_INIT indicates that the |init| function for the cipher 330 // should always be called when initialising a new operation, even if the key 331 // is NULL to indicate that the same key is being used. 332 #define EVP_CIPH_ALWAYS_CALL_INIT 0x80 333 334 // EVP_CIPH_CUSTOM_IV indicates that the cipher manages the IV itself rather 335 // than keeping it in the |iv| member of |EVP_CIPHER_CTX|. 336 #define EVP_CIPH_CUSTOM_IV 0x100 337 338 // EVP_CIPH_CTRL_INIT indicates that EVP_CTRL_INIT should be used when 339 // initialising an |EVP_CIPHER_CTX|. 340 #define EVP_CIPH_CTRL_INIT 0x200 341 342 // EVP_CIPH_FLAG_CUSTOM_CIPHER indicates that the cipher manages blocking 343 // itself. This causes EVP_(En|De)crypt_ex to be simple wrapper functions. 344 #define EVP_CIPH_FLAG_CUSTOM_CIPHER 0x400 345 346 // EVP_CIPH_FLAG_AEAD_CIPHER specifies that the cipher is an AEAD. This is an 347 // older version of the proper AEAD interface. See aead.h for the current 348 // one. 349 #define EVP_CIPH_FLAG_AEAD_CIPHER 0x800 350 351 // EVP_CIPH_CUSTOM_COPY indicates that the |ctrl| callback should be called 352 // with |EVP_CTRL_COPY| at the end of normal |EVP_CIPHER_CTX_copy| 353 // processing. 354 #define EVP_CIPH_CUSTOM_COPY 0x1000 355 356 // EVP_CIPH_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS 357 // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms 358 // (it's up the the caller to use the FIPS module in a fashion compliant with 359 // their needs). Thus this exists only to allow code to compile. 360 #define EVP_CIPH_FLAG_NON_FIPS_ALLOW 0 361 362 363 // Deprecated functions 364 365 // EVP_CipherInit acts like EVP_CipherInit_ex except that |EVP_CIPHER_CTX_init| 366 // is called on |cipher| first, if |cipher| is not NULL. 367 OPENSSL_EXPORT int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, 368 const uint8_t *key, const uint8_t *iv, 369 int enc); 370 371 // EVP_EncryptInit calls |EVP_CipherInit| with |enc| equal to one. 372 OPENSSL_EXPORT int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, 373 const EVP_CIPHER *cipher, const uint8_t *key, 374 const uint8_t *iv); 375 376 // EVP_DecryptInit calls |EVP_CipherInit| with |enc| equal to zero. 377 OPENSSL_EXPORT int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, 378 const EVP_CIPHER *cipher, const uint8_t *key, 379 const uint8_t *iv); 380 381 // EVP_CipherFinal calls |EVP_CipherFinal_ex|. 382 OPENSSL_EXPORT int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, 383 int *out_len); 384 385 // EVP_EncryptFinal calls |EVP_EncryptFinal_ex|. 386 OPENSSL_EXPORT int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, 387 int *out_len); 388 389 // EVP_DecryptFinal calls |EVP_DecryptFinal_ex|. 390 OPENSSL_EXPORT int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, uint8_t *out, 391 int *out_len); 392 393 // EVP_Cipher historically exposed an internal implementation detail of |ctx| 394 // and should not be used. Use |EVP_CipherUpdate| and |EVP_CipherFinal_ex| 395 // instead. 396 // 397 // If |ctx|'s cipher does not have the |EVP_CIPH_FLAG_CUSTOM_CIPHER| flag, it 398 // encrypts or decrypts |in_len| bytes from |in| and writes the resulting 399 // |in_len| bytes to |out|. It returns one on success and zero on error. 400 // |in_len| must be a multiple of the cipher's block size, or the behavior is 401 // undefined. 402 // 403 // TODO(davidben): Rather than being undefined (it'll often round the length up 404 // and likely read past the buffer), just fail the operation. 405 // 406 // If |ctx|'s cipher has the |EVP_CIPH_FLAG_CUSTOM_CIPHER| flag, it runs in one 407 // of two modes: If |in| is non-NULL, it behaves like |EVP_CipherUpdate|. If 408 // |in| is NULL, it behaves like |EVP_CipherFinal_ex|. In both cases, it returns 409 // |*out_len| on success and -1 on error. 410 // 411 // WARNING: The two possible calling conventions of this function signal errors 412 // incompatibly. In the first, zero indicates an error. In the second, zero 413 // indicates success with zero bytes of output. 414 OPENSSL_EXPORT int EVP_Cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, 415 const uint8_t *in, size_t in_len); 416 417 // EVP_add_cipher_alias does nothing and returns one. 418 OPENSSL_EXPORT int EVP_add_cipher_alias(const char *a, const char *b); 419 420 // EVP_get_cipherbyname returns an |EVP_CIPHER| given a human readable name in 421 // |name|, or NULL if the name is unknown. Note using this function links almost 422 // every cipher implemented by BoringSSL into the binary, not just the ones the 423 // caller requests. Size-conscious callers, such as client software, should not 424 // use this function. 425 OPENSSL_EXPORT const EVP_CIPHER *EVP_get_cipherbyname(const char *name); 426 427 // These AEADs are deprecated AES-GCM implementations that set 428 // |EVP_CIPH_FLAG_CUSTOM_CIPHER|. Use |EVP_aead_aes_128_gcm| and 429 // |EVP_aead_aes_256_gcm| instead. 430 // 431 // WARNING: Although these APIs allow streaming an individual AES-GCM operation, 432 // this is not secure. Until calling |EVP_DecryptFinal_ex|, the tag has not yet 433 // been checked and output released by |EVP_DecryptUpdate| is unauthenticated 434 // and easily manipulated by attackers. Callers must buffer the output and may 435 // not act on it until the entire operation is complete. 436 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_gcm(void); 437 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_gcm(void); 438 439 // These are deprecated, 192-bit version of AES. 440 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ecb(void); 441 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_cbc(void); 442 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ctr(void); 443 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_gcm(void); 444 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ofb(void); 445 446 // EVP_des_ede3_ecb is an alias for |EVP_des_ede3|. Use the former instead. 447 OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3_ecb(void); 448 449 // EVP_aes_128_cfb128 is only available in decrepit. 450 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb128(void); 451 452 // EVP_aes_128_cfb is an alias for |EVP_aes_128_cfb128| and is only available in 453 // decrepit. 454 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb(void); 455 456 // EVP_aes_192_cfb128 is only available in decrepit. 457 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_cfb128(void); 458 459 // EVP_aes_192_cfb is an alias for |EVP_aes_192_cfb128| and is only available in 460 // decrepit. 461 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_cfb(void); 462 463 // EVP_aes_256_cfb128 is only available in decrepit. 464 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cfb128(void); 465 466 // EVP_aes_256_cfb is an alias for |EVP_aes_256_cfb128| and is only available in 467 // decrepit. 468 OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_256_cfb(void); 469 470 // EVP_bf_ecb is Blowfish in ECB mode and is only available in decrepit. 471 OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_ecb(void); 472 473 // EVP_bf_cbc is Blowfish in CBC mode and is only available in decrepit. 474 OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_cbc(void); 475 476 // EVP_bf_cfb is Blowfish in 64-bit CFB mode and is only available in decrepit. 477 OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_cfb(void); 478 479 // EVP_cast5_ecb is CAST5 in ECB mode and is only available in decrepit. 480 OPENSSL_EXPORT const EVP_CIPHER *EVP_cast5_ecb(void); 481 482 // EVP_cast5_cbc is CAST5 in CBC mode and is only available in decrepit. 483 OPENSSL_EXPORT const EVP_CIPHER *EVP_cast5_cbc(void); 484 485 // The following flags do nothing and are included only to make it easier to 486 // compile code with BoringSSL. 487 #define EVP_CIPHER_CTX_FLAG_WRAP_ALLOW 0 488 489 // EVP_CIPHER_CTX_set_flags does nothing. 490 OPENSSL_EXPORT void EVP_CIPHER_CTX_set_flags(const EVP_CIPHER_CTX *ctx, 491 uint32_t flags); 492 493 494 // Private functions. 495 496 // EVP_CIPH_NO_PADDING disables padding in block ciphers. 497 #define EVP_CIPH_NO_PADDING 0x800 498 499 // The following are |EVP_CIPHER_CTX_ctrl| commands. 500 #define EVP_CTRL_INIT 0x0 501 #define EVP_CTRL_SET_KEY_LENGTH 0x1 502 #define EVP_CTRL_GET_RC2_KEY_BITS 0x2 503 #define EVP_CTRL_SET_RC2_KEY_BITS 0x3 504 #define EVP_CTRL_GET_RC5_ROUNDS 0x4 505 #define EVP_CTRL_SET_RC5_ROUNDS 0x5 506 #define EVP_CTRL_RAND_KEY 0x6 507 #define EVP_CTRL_PBE_PRF_NID 0x7 508 #define EVP_CTRL_COPY 0x8 509 #define EVP_CTRL_AEAD_SET_IVLEN 0x9 510 #define EVP_CTRL_AEAD_GET_TAG 0x10 511 #define EVP_CTRL_AEAD_SET_TAG 0x11 512 #define EVP_CTRL_AEAD_SET_IV_FIXED 0x12 513 #define EVP_CTRL_GCM_IV_GEN 0x13 514 #define EVP_CTRL_AEAD_SET_MAC_KEY 0x17 515 // EVP_CTRL_GCM_SET_IV_INV sets the GCM invocation field, decrypt only 516 #define EVP_CTRL_GCM_SET_IV_INV 0x18 517 #define EVP_CTRL_GET_IVLEN 0x19 518 519 // The following constants are unused. 520 #define EVP_GCM_TLS_FIXED_IV_LEN 4 521 #define EVP_GCM_TLS_EXPLICIT_IV_LEN 8 522 #define EVP_GCM_TLS_TAG_LEN 16 523 524 // The following are legacy aliases for AEAD |EVP_CIPHER_CTX_ctrl| values. 525 #define EVP_CTRL_GCM_SET_IVLEN EVP_CTRL_AEAD_SET_IVLEN 526 #define EVP_CTRL_GCM_GET_TAG EVP_CTRL_AEAD_GET_TAG 527 #define EVP_CTRL_GCM_SET_TAG EVP_CTRL_AEAD_SET_TAG 528 #define EVP_CTRL_GCM_SET_IV_FIXED EVP_CTRL_AEAD_SET_IV_FIXED 529 530 #define EVP_MAX_KEY_LENGTH 64 531 #define EVP_MAX_IV_LENGTH 16 532 #define EVP_MAX_BLOCK_LENGTH 32 533 534 struct evp_cipher_ctx_st { 535 // cipher contains the underlying cipher for this context. 536 const EVP_CIPHER *cipher; 537 538 // app_data is a pointer to opaque, user data. 539 void *app_data; // application stuff 540 541 // cipher_data points to the |cipher| specific state. 542 void *cipher_data; 543 544 // key_len contains the length of the key, which may differ from 545 // |cipher->key_len| if the cipher can take a variable key length. 546 unsigned key_len; 547 548 // encrypt is one if encrypting and zero if decrypting. 549 int encrypt; 550 551 // flags contains the OR of zero or more |EVP_CIPH_*| flags, above. 552 uint32_t flags; 553 554 // oiv contains the original IV value. 555 uint8_t oiv[EVP_MAX_IV_LENGTH]; 556 557 // iv contains the current IV value, which may have been updated. 558 uint8_t iv[EVP_MAX_IV_LENGTH]; 559 560 // buf contains a partial block which is used by, for example, CTR mode to 561 // store unused keystream bytes. 562 uint8_t buf[EVP_MAX_BLOCK_LENGTH]; 563 564 // buf_len contains the number of bytes of a partial block contained in 565 // |buf|. 566 int buf_len; 567 568 // num contains the number of bytes of |iv| which are valid for modes that 569 // manage partial blocks themselves. 570 unsigned num; 571 572 // final_used is non-zero if the |final| buffer contains plaintext. 573 int final_used; 574 575 uint8_t final[EVP_MAX_BLOCK_LENGTH]; // possible final block 576 577 // Has this structure been rendered unusable by a failure. 578 int poisoned; 579 } /* EVP_CIPHER_CTX */; 580 581 typedef struct evp_cipher_info_st { 582 const EVP_CIPHER *cipher; 583 unsigned char iv[EVP_MAX_IV_LENGTH]; 584 } EVP_CIPHER_INFO; 585 586 587 #if defined(__cplusplus) 588 } // extern C 589 590 #if !defined(BORINGSSL_NO_CXX) 591 extern "C++" { 592 593 BSSL_NAMESPACE_BEGIN 594 595 BORINGSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free) 596 597 using ScopedEVP_CIPHER_CTX = 598 internal::StackAllocated<EVP_CIPHER_CTX, int, EVP_CIPHER_CTX_init, 599 EVP_CIPHER_CTX_cleanup>; 600 601 BSSL_NAMESPACE_END 602 603 } // extern C++ 604 #endif 605 606 #endif 607 608 #define CIPHER_R_AES_KEY_SETUP_FAILED 100 609 #define CIPHER_R_BAD_DECRYPT 101 610 #define CIPHER_R_BAD_KEY_LENGTH 102 611 #define CIPHER_R_BUFFER_TOO_SMALL 103 612 #define CIPHER_R_CTRL_NOT_IMPLEMENTED 104 613 #define CIPHER_R_CTRL_OPERATION_NOT_IMPLEMENTED 105 614 #define CIPHER_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH 106 615 #define CIPHER_R_INITIALIZATION_ERROR 107 616 #define CIPHER_R_INPUT_NOT_INITIALIZED 108 617 #define CIPHER_R_INVALID_AD_SIZE 109 618 #define CIPHER_R_INVALID_KEY_LENGTH 110 619 #define CIPHER_R_INVALID_NONCE_SIZE 111 620 #define CIPHER_R_INVALID_OPERATION 112 621 #define CIPHER_R_IV_TOO_LARGE 113 622 #define CIPHER_R_NO_CIPHER_SET 114 623 #define CIPHER_R_OUTPUT_ALIASES_INPUT 115 624 #define CIPHER_R_TAG_TOO_LARGE 116 625 #define CIPHER_R_TOO_LARGE 117 626 #define CIPHER_R_UNSUPPORTED_AD_SIZE 118 627 #define CIPHER_R_UNSUPPORTED_INPUT_SIZE 119 628 #define CIPHER_R_UNSUPPORTED_KEY_SIZE 120 629 #define CIPHER_R_UNSUPPORTED_NONCE_SIZE 121 630 #define CIPHER_R_UNSUPPORTED_TAG_SIZE 122 631 #define CIPHER_R_WRONG_FINAL_BLOCK_LENGTH 123 632 #define CIPHER_R_NO_DIRECTION_SET 124 633 #define CIPHER_R_INVALID_NONCE 125 634 635 #endif // OPENSSL_HEADER_CIPHER_H