| 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_INTERNAL_H |
| 5 |
#define OPENSSL_HEADER_CIPHER_INTERNAL_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include <openssl/aead.h> |
| 10 |
#include <openssl/aes.h> |
| 11 |
#include <openssl/bytestring.h> |
| 12 |
|
| 13 |
#include "../../internal.h" |
| 14 |
#include "../modes/internal.h" |
| 15 |
|
| 16 |
#if defined(__cplusplus) |
| 17 |
extern "C" { |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
// EVP_CIPH_MODE_MASK contains the bits of |flags| that represent the mode. |
| 22 |
#define EVP_CIPH_MODE_MASK 0x3f |
| 23 |
|
| 24 |
// Set of EVP_AEAD->aead_id identifiers, zero is reserved as the "unknown" |
| 25 |
// value since it is the default for a structure. Implementations of the same |
| 26 |
// algorithms should use the same identifier. For example, machine-optimised |
| 27 |
// assembly versions should use the same identifier as their C counterparts. |
| 28 |
#define AEAD_UNKNOWN_ID 0 |
| 29 |
#define AEAD_AES_128_CTR_HMAC_SHA256_ID 1 |
| 30 |
#define AEAD_AES_256_CTR_HMAC_SHA256_ID 2 |
| 31 |
#define AEAD_AES_128_GCM_SIV_ID 3 |
| 32 |
#define AEAD_AES_256_GCM_SIV_ID 4 |
| 33 |
#define AEAD_CHACHA20_POLY1305_ID 5 |
| 34 |
#define AEAD_XCHACHA20_POLY1305_ID 6 |
| 35 |
#define AEAD_AES_128_CBC_SHA1_TLS_ID 7 |
| 36 |
#define AEAD_AES_128_CBC_SHA1_TLS_IMPLICIT_IV_ID 8 |
| 37 |
#define AEAD_AES_256_CBC_SHA1_TLS_ID 9 |
| 38 |
#define AEAD_AES_256_CBC_SHA1_TLS_IMPLICIT_IV_ID 10 |
| 39 |
#define AEAD_AES_128_CBC_SHA256_TLS_ID 11 |
| 40 |
#define AEAD_AES_128_CBC_SHA256_TLS_IMPLICIT_IV_ID 12 |
| 41 |
#define AEAD_DES_EDE3_CBC_SHA1_TLS_ID 13 |
| 42 |
#define AEAD_DES_EDE3_CBC_SHA1_TLS_IMPLICIT_IV_ID 14 |
| 43 |
#define AEAD_NULL_SHA1_TLS_ID 15 |
| 44 |
#define AEAD_AES_128_GCM_ID 16 |
| 45 |
#define AEAD_AES_192_GCM_ID 17 |
| 46 |
#define AEAD_AES_256_GCM_ID 18 |
| 47 |
#define AEAD_AES_128_GCM_RANDNONCE_ID 19 |
| 48 |
#define AEAD_AES_256_GCM_RANDNONCE_ID 20 |
| 49 |
#define AEAD_AES_128_GCM_TLS12_ID 21 |
| 50 |
#define AEAD_AES_256_GCM_TLS12_ID 22 |
| 51 |
#define AEAD_AES_128_GCM_TLS13_ID 23 |
| 52 |
#define AEAD_AES_256_GCM_TLS13_ID 24 |
| 53 |
#define AEAD_AES_128_CCM_BLUETOOTH_ID 25 |
| 54 |
#define AEAD_AES_128_CCM_BLUETOOTH_8_ID 26 |
| 55 |
#define AEAD_AES_128_CCM_MATTER_ID 27 |
| 56 |
#define AEAD_AES_256_CBC_SHA384_TLS_ID 28 |
| 57 |
#define AEAD_MAX_ID 28 |
| 58 |
|
| 59 |
// EVP_AEAD represents a specific AEAD algorithm. |
| 60 |
struct evp_aead_st { |
| 61 |
uint8_t key_len; |
| 62 |
uint8_t nonce_len; |
| 63 |
uint8_t overhead; |
| 64 |
uint8_t max_tag_len; |
| 65 |
uint16_t aead_id; |
| 66 |
int seal_scatter_supports_extra_in; |
| 67 |
|
| 68 |
// init initialises an |EVP_AEAD_CTX|. If this call returns zero then |
| 69 |
// |cleanup| will not be called for that context. |
| 70 |
int (*init)(EVP_AEAD_CTX *, const uint8_t *key, size_t key_len, |
| 71 |
size_t tag_len); |
| 72 |
int (*init_with_direction)(EVP_AEAD_CTX *, const uint8_t *key, size_t key_len, |
| 73 |
size_t tag_len, enum evp_aead_direction_t dir); |
| 74 |
void (*cleanup)(EVP_AEAD_CTX *); |
| 75 |
|
| 76 |
int (*open)(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 77 |
size_t max_out_len, const uint8_t *nonce, size_t nonce_len, |
| 78 |
const uint8_t *in, size_t in_len, const uint8_t *ad, |
| 79 |
size_t ad_len); |
| 80 |
|
| 81 |
int (*seal_scatter)(const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, |
| 82 |
size_t *out_tag_len, size_t max_out_tag_len, |
| 83 |
const uint8_t *nonce, size_t nonce_len, const uint8_t *in, |
| 84 |
size_t in_len, const uint8_t *extra_in, |
| 85 |
size_t extra_in_len, const uint8_t *ad, size_t ad_len); |
| 86 |
|
| 87 |
int (*open_gather)(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 88 |
const uint8_t *nonce, size_t nonce_len, const uint8_t *in, |
| 89 |
size_t in_len, const uint8_t *in_tag, size_t in_tag_len, |
| 90 |
const uint8_t *ad, size_t ad_len); |
| 91 |
|
| 92 |
int (*get_iv)(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv, |
| 93 |
size_t *out_len); |
| 94 |
|
| 95 |
size_t (*tag_len)(const EVP_AEAD_CTX *ctx, size_t in_Len, |
| 96 |
size_t extra_in_len); |
| 97 |
|
| 98 |
int (*serialize_state)(const EVP_AEAD_CTX *ctx, CBB *cbb); |
| 99 |
|
| 100 |
int (*deserialize_state)(const EVP_AEAD_CTX *ctx, CBS *cbs); |
| 101 |
}; |
| 102 |
|
| 103 |
struct evp_cipher_st { |
| 104 |
// type contains a NID identifying the cipher. (e.g. NID_aes_128_gcm.) |
| 105 |
int nid; |
| 106 |
|
| 107 |
// block_size contains the block size, in bytes, of the cipher, or 1 for a |
| 108 |
// stream cipher. |
| 109 |
unsigned block_size; |
| 110 |
|
| 111 |
// key_len contains the key size, in bytes, for the cipher. If the cipher |
| 112 |
// takes a variable key size then this contains the default size. |
| 113 |
unsigned key_len; |
| 114 |
|
| 115 |
// iv_len contains the IV size, in bytes, or zero if inapplicable. |
| 116 |
unsigned iv_len; |
| 117 |
|
| 118 |
// ctx_size contains the size, in bytes, of the per-key context for this |
| 119 |
// cipher. |
| 120 |
unsigned ctx_size; |
| 121 |
|
| 122 |
// flags contains the OR of a number of flags. See |EVP_CIPH_*|. |
| 123 |
uint32_t flags; |
| 124 |
|
| 125 |
int (*init)(EVP_CIPHER_CTX *ctx, const uint8_t *key, const uint8_t *iv, |
| 126 |
int enc); |
| 127 |
|
| 128 |
int (*cipher)(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, |
| 129 |
size_t inl); |
| 130 |
|
| 131 |
// cleanup, if non-NULL, releases memory associated with the context. It is |
| 132 |
// called if |EVP_CTRL_INIT| succeeds. Note that |init| may not have been |
| 133 |
// called at this point. |
| 134 |
void (*cleanup)(EVP_CIPHER_CTX *); |
| 135 |
|
| 136 |
int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr); |
| 137 |
}; |
| 138 |
|
| 139 |
// aes_ctr_set_key initialises |*aes_key| using |key_bytes| bytes from |key|, |
| 140 |
// where |key_bytes| must either be 16, 24 or 32. If not NULL, |*out_block| is |
| 141 |
// set to a function that encrypts single blocks. If not NULL, |*gcm_key| is |
| 142 |
// initialised to do GHASH with the given key. It returns a function for |
| 143 |
// optimised CTR-mode, or NULL if CTR-mode should be built using |*out_block|. |
| 144 |
ctr128_f aes_ctr_set_key(AES_KEY *aes_key, GCM128_KEY *gcm_key, |
| 145 |
block128_f *out_block, const uint8_t *key, |
| 146 |
size_t key_bytes); |
| 147 |
|
| 148 |
|
| 149 |
// EXPERIMENTAL functions for use in the TLS Transfer function. See |
| 150 |
// |SSL_to_bytes| for more details. |
| 151 |
|
| 152 |
// EVP_AEAD_CTX_serialize_state serializes the state of |ctx|, |
| 153 |
// and writes it to |cbb|. The serialized bytes contains only the subset of data |
| 154 |
// necessary to restore the state of an |EVP_AEAD_CTX| after initializing a new |
| 155 |
// instance using |EVP_AEAD_CTX_init|. Function returns 1 on success or zero for |
| 156 |
// an error. |
| 157 |
// |
| 158 |
// EvpAeadCtxStateSerializationVersion ::= INTEGER {v1 (1)} |
| 159 |
// |
| 160 |
// EvpAeadCtxState ::= SEQUENCE { |
| 161 |
// serializationVersion EvpAeadCtxStateSerializationVersion, |
| 162 |
// evpAeadCipherIdentifier INTEGER, |
| 163 |
// state OCTET STRING |
| 164 |
// } |
| 165 |
OPENSSL_EXPORT int EVP_AEAD_CTX_serialize_state(const EVP_AEAD_CTX *ctx, |
| 166 |
CBB *cbb); |
| 167 |
|
| 168 |
// EVP_AEAD_CTX_deserialize_state deserializes the state |
| 169 |
// contained in |cbs|, configures the |ctx| to match. The deserialized bytes |
| 170 |
// contains only the subset of data necessary to restore the state of an |
| 171 |
// |EVP_AEAD_CTX| after initializing a new instance using |EVP_AEAD_CTX_init|. |
| 172 |
// The function returns 1 on success or zero for an error. |
| 173 |
OPENSSL_EXPORT int EVP_AEAD_CTX_deserialize_state(const EVP_AEAD_CTX *ctx, |
| 174 |
CBS *cbs); |
| 175 |
|
| 176 |
OPENSSL_EXPORT uint16_t EVP_AEAD_CTX_get_aead_id(const EVP_AEAD_CTX *ctx); |
| 177 |
|
| 178 |
#if defined(__cplusplus) |
| 179 |
} // extern C |
| 180 |
#endif |
| 181 |
|
| 182 |
#endif // OPENSSL_HEADER_CIPHER_INTERNAL_H |