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