1 /* 2 * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (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 #include <openssl/params.h> 11 #include <openssl/core_dispatch.h> 12 #include <openssl/core_names.h> 13 #include <openssl/evp.h> 14 #include "internal/cryptlib.h" 15 #include "crypto/modes.h" 16 17 # define MAXCHUNK ((size_t)1 << 30) 18 # define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4)) 19 20 #define GENERIC_BLOCK_SIZE 16 21 #define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */ 22 #define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */ 23 #define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */ 24 #define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */ 25 26 #define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args 27 28 typedef struct prov_cipher_hw_st PROV_CIPHER_HW; 29 typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX; 30 31 typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out, 32 const unsigned char *in, size_t len); 33 34 /* Internal flags that can be queried */ 35 #define PROV_CIPHER_FLAG_AEAD 0x0001 36 #define PROV_CIPHER_FLAG_CUSTOM_IV 0x0002 37 #define PROV_CIPHER_FLAG_CTS 0x0004 38 #define PROV_CIPHER_FLAG_TLS1_MULTIBLOCK 0x0008 39 #define PROV_CIPHER_FLAG_RAND_KEY 0x0010 40 /* Internal flags that are only used within the provider */ 41 #define PROV_CIPHER_FLAG_VARIABLE_LENGTH 0x0100 42 #define PROV_CIPHER_FLAG_INVERSE_CIPHER 0x0200 43 44 struct prov_cipher_ctx_st { 45 /* place buffer at the beginning for memory alignment */ 46 /* The original value of the iv */ 47 unsigned char oiv[GENERIC_BLOCK_SIZE]; 48 /* Buffer of partial blocks processed via update calls */ 49 unsigned char buf[GENERIC_BLOCK_SIZE]; 50 unsigned char iv[GENERIC_BLOCK_SIZE]; 51 52 block128_f block; 53 union { 54 cbc128_f cbc; 55 ctr128_f ctr; 56 ecb128_f ecb; 57 } stream; 58 59 unsigned int mode; 60 size_t keylen; /* key size (in bytes) */ 61 size_t ivlen; 62 size_t blocksize; 63 size_t bufsz; /* Number of bytes in buf */ 64 unsigned int cts_mode; /* Use to set the type for CTS modes */ 65 unsigned int pad : 1; /* Whether padding should be used or not */ 66 unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */ 67 unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */ 68 unsigned int key_set : 1; /* Set when key is set on the context */ 69 unsigned int updated : 1; /* Set to 1 during update for one shot ciphers */ 70 unsigned int variable_keylength : 1; 71 unsigned int inverse_cipher : 1; /* set to 1 to use inverse cipher */ 72 unsigned int use_bits : 1; /* Set to 0 for cfb1 to use bits instead of bytes */ 73 74 unsigned int tlsversion; /* If TLS padding is in use the TLS version number */ 75 unsigned char *tlsmac; /* tls MAC extracted from the last record */ 76 int alloced; /* 77 * Whether the tlsmac data has been allocated or 78 * points into the user buffer. 79 */ 80 size_t tlsmacsize; /* Size of the TLS MAC */ 81 int removetlspad; /* Whether TLS padding should be removed or not */ 82 size_t removetlsfixed; /* 83 * Length of the fixed size data to remove when 84 * processing TLS data (equals mac size plus 85 * IV size if applicable) 86 */ 87 88 /* 89 * num contains the number of bytes of |iv| which are valid for modes that 90 * manage partial blocks themselves. 91 */ 92 unsigned int num; 93 const PROV_CIPHER_HW *hw; /* hardware specific functions */ 94 const void *ks; /* Pointer to algorithm specific key data */ 95 OSSL_LIB_CTX *libctx; 96 }; 97 98 struct prov_cipher_hw_st { 99 int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen); 100 PROV_CIPHER_HW_FN *cipher; 101 void (*copyctx)(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src); 102 }; 103 104 void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx); 105 OSSL_FUNC_cipher_encrypt_init_fn ossl_cipher_generic_einit; 106 OSSL_FUNC_cipher_decrypt_init_fn ossl_cipher_generic_dinit; 107 OSSL_FUNC_cipher_update_fn ossl_cipher_generic_block_update; 108 OSSL_FUNC_cipher_final_fn ossl_cipher_generic_block_final; 109 OSSL_FUNC_cipher_update_fn ossl_cipher_generic_stream_update; 110 OSSL_FUNC_cipher_final_fn ossl_cipher_generic_stream_final; 111 OSSL_FUNC_cipher_cipher_fn ossl_cipher_generic_cipher; 112 OSSL_FUNC_cipher_get_ctx_params_fn ossl_cipher_generic_get_ctx_params; 113 OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_generic_set_ctx_params; 114 OSSL_FUNC_cipher_gettable_params_fn ossl_cipher_generic_gettable_params; 115 OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_generic_gettable_ctx_params; 116 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_generic_settable_ctx_params; 117 OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_var_keylen_set_ctx_params; 118 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_var_keylen_settable_ctx_params; 119 OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_aead_gettable_ctx_params; 120 OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_aead_settable_ctx_params; 121 122 int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md, 123 uint64_t flags, 124 size_t kbits, size_t blkbits, size_t ivbits); 125 void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits, 126 size_t ivbits, unsigned int mode, 127 uint64_t flags, 128 const PROV_CIPHER_HW *hw, void *provctx); 129 130 #define IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,\ 131 blkbits, ivbits, typ) \ 132 const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \ 133 { OSSL_FUNC_CIPHER_NEWCTX, \ 134 (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ 135 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ 136 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ 137 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \ 138 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \ 139 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\ 140 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \ 141 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ 142 { OSSL_FUNC_CIPHER_GET_PARAMS, \ 143 (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \ 144 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ 145 (void (*)(void))ossl_cipher_generic_get_ctx_params }, \ 146 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ 147 (void (*)(void))ossl_cipher_generic_set_ctx_params }, \ 148 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ 149 (void (*)(void))ossl_cipher_generic_gettable_params }, \ 150 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ 151 (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \ 152 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ 153 (void (*)(void))ossl_cipher_generic_settable_ctx_params }, \ 154 { 0, NULL } \ 155 }; 156 157 #define IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, \ 158 kbits, blkbits, ivbits, typ) \ 159 const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \ 160 { OSSL_FUNC_CIPHER_NEWCTX, \ 161 (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ 162 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ 163 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ 164 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\ 165 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\ 166 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\ 167 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \ 168 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ 169 { OSSL_FUNC_CIPHER_GET_PARAMS, \ 170 (void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \ 171 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ 172 (void (*)(void))ossl_cipher_generic_get_ctx_params }, \ 173 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ 174 (void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \ 175 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ 176 (void (*)(void))ossl_cipher_generic_gettable_params }, \ 177 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ 178 (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \ 179 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ 180 (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \ 181 { 0, NULL } \ 182 }; 183 184 185 #define IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, \ 186 kbits, blkbits, ivbits, typ) \ 187 static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \ 188 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \ 189 { \ 190 return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \ 191 flags, kbits, blkbits, ivbits); \ 192 } \ 193 static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \ 194 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \ 195 { \ 196 PROV_##UCALG##_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx))\ 197 : NULL; \ 198 if (ctx != NULL) { \ 199 ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \ 200 EVP_CIPH_##UCMODE##_MODE, flags, \ 201 ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\ 202 provctx); \ 203 } \ 204 return ctx; \ 205 } \ 206 207 #define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ 208 blkbits, ivbits, typ) \ 209 IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \ 210 blkbits, ivbits, typ) \ 211 IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \ 212 blkbits, ivbits, typ) 213 214 #define IMPLEMENT_var_keylen_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \ 215 blkbits, ivbits, typ) \ 216 IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \ 217 blkbits, ivbits, typ) \ 218 IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \ 219 blkbits, ivbits, typ) 220 221 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cbc; 222 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ecb; 223 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ofb128; 224 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb128; 225 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb8; 226 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb1; 227 PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ctr; 228 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cbc; 229 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb8; 230 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb128; 231 PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_ofb128; 232 #define ossl_cipher_hw_chunked_ecb ossl_cipher_hw_generic_ecb 233 #define ossl_cipher_hw_chunked_ctr ossl_cipher_hw_generic_ctr 234 #define ossl_cipher_hw_chunked_cfb1 ossl_cipher_hw_generic_cfb1 235 236 #define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ 237 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ 238 unsigned char *out, \ 239 const unsigned char *in, size_t len) \ 240 { \ 241 int num = ctx->num; \ 242 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ 243 \ 244 while (len >= MAXCHUNK) { \ 245 FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \ 246 len -= MAXCHUNK; \ 247 in += MAXCHUNK; \ 248 out += MAXCHUNK; \ 249 } \ 250 if (len > 0) { \ 251 FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \ 252 } \ 253 ctx->num = num; \ 254 return 1; \ 255 } 256 257 #define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ 258 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ 259 unsigned char *out, \ 260 const unsigned char *in, size_t len) \ 261 { \ 262 size_t i, bl = ctx->blocksize; \ 263 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ 264 \ 265 if (len < bl) \ 266 return 1; \ 267 for (i = 0, len -= bl; i <= len; i += bl) \ 268 FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \ 269 return 1; \ 270 } 271 272 #define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ 273 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ 274 unsigned char *out, \ 275 const unsigned char *in, size_t len) \ 276 { \ 277 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ 278 \ 279 while (len >= MAXCHUNK) { \ 280 FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \ 281 len -= MAXCHUNK; \ 282 in += MAXCHUNK; \ 283 out += MAXCHUNK; \ 284 } \ 285 if (len > 0) \ 286 FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \ 287 return 1; \ 288 } 289 290 #define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \ 291 static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \ 292 unsigned char *out, \ 293 const unsigned char *in, size_t len) \ 294 { \ 295 size_t chunk = MAXCHUNK; \ 296 KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \ 297 int num = ctx->num; \ 298 \ 299 if (len < chunk) \ 300 chunk = len; \ 301 while (len > 0 && len >= chunk) { \ 302 FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \ 303 ctx->enc); \ 304 len -= chunk; \ 305 in += chunk; \ 306 out += chunk; \ 307 if (len < chunk) \ 308 chunk = len; \ 309 } \ 310 ctx->num = num; \ 311 return 1; \ 312 } 313 314 #define IMPLEMENT_CIPHER_HW_COPYCTX(name, CTX_TYPE) \ 315 static void name(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src) \ 316 { \ 317 CTX_TYPE *sctx = (CTX_TYPE *)src; \ 318 CTX_TYPE *dctx = (CTX_TYPE *)dst; \ 319 \ 320 *dctx = *sctx; \ 321 dst->ks = &dctx->ks.ks; \ 322 } 323 324 #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \ 325 static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \ 326 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \ 327 OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \ 328 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \ 329 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \ 330 OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), \ 331 OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0), 332 333 #define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \ 334 OSSL_PARAM_END \ 335 }; \ 336 const OSSL_PARAM * name##_gettable_ctx_params(ossl_unused void *cctx, \ 337 ossl_unused void *provctx) \ 338 { \ 339 return name##_known_gettable_ctx_params; \ 340 } 341 342 #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \ 343 static const OSSL_PARAM name##_known_settable_ctx_params[] = { \ 344 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \ 345 OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), 346 #define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \ 347 OSSL_PARAM_END \ 348 }; \ 349 const OSSL_PARAM * name##_settable_ctx_params(ossl_unused void *cctx, \ 350 ossl_unused void *provctx) \ 351 { \ 352 return name##_known_settable_ctx_params; \ 353 } 354 355 int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv, 356 size_t ivlen); 357 358 size_t ossl_cipher_fillblock(unsigned char *buf, size_t *buflen, 359 size_t blocksize, 360 const unsigned char **in, size_t *inlen); 361 int ossl_cipher_trailingdata(unsigned char *buf, size_t *buflen, 362 size_t blocksize, 363 const unsigned char **in, size_t *inlen); 364