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