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