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