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