2019-08-26 15:05:08 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2019-08-26 15:05:08 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
#ifndef OSSL_PROV_CIPHERCOMMON_H
|
|
|
|
# define OSSL_PROV_CIPHERCOMMON_H
|
|
|
|
# pragma once
|
2019-08-26 15:05:08 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# include <openssl/params.h>
|
|
|
|
# include <openssl/core_dispatch.h>
|
|
|
|
# include <openssl/core_names.h>
|
|
|
|
# include <openssl/evp.h>
|
|
|
|
# include "internal/cryptlib.h"
|
|
|
|
# include "crypto/modes.h"
|
2019-08-26 15:05:08 +08:00
|
|
|
|
2022-08-15 12:49:17 +08:00
|
|
|
# define MAXCHUNK ((size_t)1 << 30)
|
2022-02-10 02:30:25 +08:00
|
|
|
# define MAXBITCHUNK ((size_t)1 << (sizeof(size_t) * 8 - 4))
|
2019-08-26 15:05:08 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define GENERIC_BLOCK_SIZE 16
|
|
|
|
# define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */
|
|
|
|
# define IV_STATE_BUFFERED 1 /* iv has been copied to the iv buffer */
|
|
|
|
# define IV_STATE_COPIED 2 /* iv has been copied from the iv buffer */
|
|
|
|
# define IV_STATE_FINISHED 3 /* the iv has been used - so don't reuse it */
|
|
|
|
|
|
|
|
# define PROV_CIPHER_FUNC(type, name, args) typedef type (* OSSL_##name##_fn)args
|
2019-08-26 15:05:08 +08:00
|
|
|
|
|
|
|
typedef struct prov_cipher_hw_st PROV_CIPHER_HW;
|
|
|
|
typedef struct prov_cipher_ctx_st PROV_CIPHER_CTX;
|
|
|
|
|
|
|
|
typedef int (PROV_CIPHER_HW_FN)(PROV_CIPHER_CTX *dat, unsigned char *out,
|
|
|
|
const unsigned char *in, size_t len);
|
|
|
|
|
2020-12-17 14:39:57 +08:00
|
|
|
/* Internal flags that can be queried */
|
2022-02-10 02:30:25 +08:00
|
|
|
# define PROV_CIPHER_FLAG_AEAD 0x0001
|
|
|
|
# define PROV_CIPHER_FLAG_CUSTOM_IV 0x0002
|
|
|
|
# define PROV_CIPHER_FLAG_CTS 0x0004
|
|
|
|
# define PROV_CIPHER_FLAG_TLS1_MULTIBLOCK 0x0008
|
|
|
|
# define PROV_CIPHER_FLAG_RAND_KEY 0x0010
|
2020-12-17 14:39:57 +08:00
|
|
|
/* Internal flags that are only used within the provider */
|
2022-02-10 02:30:25 +08:00
|
|
|
# define PROV_CIPHER_FLAG_VARIABLE_LENGTH 0x0100
|
|
|
|
# define PROV_CIPHER_FLAG_INVERSE_CIPHER 0x0200
|
2020-12-17 14:39:57 +08:00
|
|
|
|
2019-08-26 15:05:08 +08:00
|
|
|
struct prov_cipher_ctx_st {
|
2022-05-09 19:42:39 +08:00
|
|
|
/* place buffer at the beginning for memory alignment */
|
|
|
|
/* The original value of the iv */
|
|
|
|
unsigned char oiv[GENERIC_BLOCK_SIZE];
|
|
|
|
/* Buffer of partial blocks processed via update calls */
|
|
|
|
unsigned char buf[GENERIC_BLOCK_SIZE];
|
|
|
|
unsigned char iv[GENERIC_BLOCK_SIZE];
|
|
|
|
|
2019-08-26 15:05:08 +08:00
|
|
|
block128_f block;
|
|
|
|
union {
|
|
|
|
cbc128_f cbc;
|
|
|
|
ctr128_f ctr;
|
Optimize AES-ECB mode in OpenSSL for both aarch64 and aarch32
Aes-ecb mode can be optimized by inverleaving cipher operation on
several blocks and loop unrolling. Interleaving needs one ideal
unrolling factor, here we adopt the same factor with aes-cbc,
which is described as below:
If blocks number > 5, select 5 blocks as one iteration,every
loop, decrease the blocks number by 5.
If 3 < left blocks < 5 select 3 blocks as one iteration, every
loop, decrease the block number by 3.
If left blocks < 3, treat them as tail blocks.
Detailed implementation will have a little adjustment for squeezing
code space.
With this way, for small size such as 16 bytes, the performance is
similar as before, but for big size such as 16k bytes, the performance
improves a lot, even reaches to 100%, for some arches such as A57,
the improvement even exceeds 100%. The following table will list the
encryption performance data on aarch64, take a72 and a57 as examples.
Performance value takes the unit of cycles per byte, takes the format
as comparision of values. List them as below:
A72:
Before optimization After optimization Improve
evp-aes-128-ecb@16 17.26538237 16.82663866 2.61%
evp-aes-128-ecb@64 5.50528499 5.222637557 5.41%
evp-aes-128-ecb@256 2.632700213 1.908442892 37.95%
evp-aes-128-ecb@1024 1.876102047 1.078018868 74.03%
evp-aes-128-ecb@8192 1.6550392 0.853982929 93.80%
evp-aes-128-ecb@16384 1.636871283 0.847623957 93.11%
evp-aes-192-ecb@16 17.73104961 17.09692468 3.71%
evp-aes-192-ecb@64 5.78984398 5.418545192 6.85%
evp-aes-192-ecb@256 2.872005308 2.081815274 37.96%
evp-aes-192-ecb@1024 2.083226672 1.25095642 66.53%
evp-aes-192-ecb@8192 1.831992057 0.995916251 83.95%
evp-aes-192-ecb@16384 1.821590009 0.993820525 83.29%
evp-aes-256-ecb@16 18.0606306 17.96963317 0.51%
evp-aes-256-ecb@64 6.19651997 5.762465812 7.53%
evp-aes-256-ecb@256 3.176991394 2.24642538 41.42%
evp-aes-256-ecb@1024 2.385991919 1.396018192 70.91%
evp-aes-256-ecb@8192 2.147862636 1.142222597 88.04%
evp-aes-256-ecb@16384 2.131361787 1.135944617 87.63%
A57:
Before optimization After optimization Improve
evp-aes-128-ecb@16 18.61045121 18.36456218 1.34%
evp-aes-128-ecb@64 6.438628994 5.467959461 17.75%
evp-aes-128-ecb@256 2.957452881 1.97238604 49.94%
evp-aes-128-ecb@1024 2.117096219 1.099665054 92.52%
evp-aes-128-ecb@8192 1.868385973 0.837440804 123.11%
evp-aes-128-ecb@16384 1.853078526 0.822420027 125.32%
evp-aes-192-ecb@16 19.07021756 18.50018552 3.08%
evp-aes-192-ecb@64 6.672351486 5.696088921 17.14%
evp-aes-192-ecb@256 3.260427769 2.131449916 52.97%
evp-aes-192-ecb@1024 2.410522832 1.250529718 92.76%
evp-aes-192-ecb@8192 2.17921605 0.973225504 123.92%
evp-aes-192-ecb@16384 2.162250997 0.95919871 125.42%
evp-aes-256-ecb@16 19.3008384 19.12743654 0.91%
evp-aes-256-ecb@64 6.992950658 5.92149541 18.09%
evp-aes-256-ecb@256 3.576361743 2.287619504 56.34%
evp-aes-256-ecb@1024 2.726671027 1.381267599 97.40%
evp-aes-256-ecb@8192 2.493583657 1.110959913 124.45%
evp-aes-256-ecb@16384 2.473916816 1.099967073 124.91%
Change-Id: Iccd23d972e0d52d22dc093f4c208f69c9d5a0ca7
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/10518)
2019-11-07 10:36:45 +08:00
|
|
|
ecb128_f ecb;
|
2019-08-26 15:05:08 +08:00
|
|
|
} stream;
|
|
|
|
|
2019-09-05 09:23:57 +08:00
|
|
|
unsigned int mode;
|
2019-10-08 07:19:10 +08:00
|
|
|
size_t keylen; /* key size (in bytes) */
|
2019-09-05 09:23:57 +08:00
|
|
|
size_t ivlen;
|
|
|
|
size_t blocksize;
|
2019-10-08 07:19:10 +08:00
|
|
|
size_t bufsz; /* Number of bytes in buf */
|
2020-06-08 12:33:27 +08:00
|
|
|
unsigned int cts_mode; /* Use to set the type for CTS modes */
|
2019-10-08 07:19:10 +08:00
|
|
|
unsigned int pad : 1; /* Whether padding should be used or not */
|
|
|
|
unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
|
|
|
|
unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */
|
2020-06-08 12:33:27 +08:00
|
|
|
unsigned int updated : 1; /* Set to 1 during update for one shot ciphers */
|
2020-12-17 14:39:57 +08:00
|
|
|
unsigned int variable_keylength : 1;
|
|
|
|
unsigned int inverse_cipher : 1; /* set to 1 to use inverse cipher */
|
|
|
|
unsigned int use_bits : 1; /* Set to 0 for cfb1 to use bits instead of bytes */
|
2019-09-05 09:23:57 +08:00
|
|
|
|
2020-05-28 00:20:18 +08:00
|
|
|
unsigned int tlsversion; /* If TLS padding is in use the TLS version number */
|
|
|
|
unsigned char *tlsmac; /* tls MAC extracted from the last record */
|
|
|
|
int alloced; /*
|
|
|
|
* Whether the tlsmac data has been allocated or
|
|
|
|
* points into the user buffer.
|
|
|
|
*/
|
|
|
|
size_t tlsmacsize; /* Size of the TLS MAC */
|
2020-11-11 19:07:12 +08:00
|
|
|
int removetlspad; /* Whether TLS padding should be removed or not */
|
|
|
|
size_t removetlsfixed; /*
|
2020-06-23 21:34:45 +08:00
|
|
|
* Length of the fixed size data to remove when
|
2020-11-11 19:07:12 +08:00
|
|
|
* processing TLS data (equals mac size plus
|
2020-06-23 21:34:45 +08:00
|
|
|
* IV size if applicable)
|
|
|
|
*/
|
2020-05-28 00:20:18 +08:00
|
|
|
|
2019-08-26 15:05:08 +08:00
|
|
|
/*
|
|
|
|
* num contains the number of bytes of |iv| which are valid for modes that
|
|
|
|
* manage partial blocks themselves.
|
|
|
|
*/
|
2019-09-05 09:23:57 +08:00
|
|
|
unsigned int num;
|
2019-08-26 15:05:08 +08:00
|
|
|
const PROV_CIPHER_HW *hw; /* hardware specific functions */
|
|
|
|
const void *ks; /* Pointer to algorithm specific key data */
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2019-08-26 15:05:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct prov_cipher_hw_st {
|
|
|
|
int (*init)(PROV_CIPHER_CTX *dat, const uint8_t *key, size_t keylen);
|
|
|
|
PROV_CIPHER_HW_FN *cipher;
|
2019-11-18 11:13:05 +08:00
|
|
|
void (*copyctx)(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src);
|
2019-08-26 15:05:08 +08:00
|
|
|
};
|
|
|
|
|
2020-09-29 15:40:26 +08:00
|
|
|
void ossl_cipher_generic_reset_ctx(PROV_CIPHER_CTX *ctx);
|
|
|
|
OSSL_FUNC_cipher_encrypt_init_fn ossl_cipher_generic_einit;
|
|
|
|
OSSL_FUNC_cipher_decrypt_init_fn ossl_cipher_generic_dinit;
|
|
|
|
OSSL_FUNC_cipher_update_fn ossl_cipher_generic_block_update;
|
|
|
|
OSSL_FUNC_cipher_final_fn ossl_cipher_generic_block_final;
|
|
|
|
OSSL_FUNC_cipher_update_fn ossl_cipher_generic_stream_update;
|
|
|
|
OSSL_FUNC_cipher_final_fn ossl_cipher_generic_stream_final;
|
|
|
|
OSSL_FUNC_cipher_cipher_fn ossl_cipher_generic_cipher;
|
|
|
|
OSSL_FUNC_cipher_get_ctx_params_fn ossl_cipher_generic_get_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_generic_set_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_gettable_params_fn ossl_cipher_generic_gettable_params;
|
|
|
|
OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_generic_gettable_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_generic_settable_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_set_ctx_params_fn ossl_cipher_var_keylen_set_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_var_keylen_settable_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_gettable_ctx_params_fn ossl_cipher_aead_gettable_ctx_params;
|
|
|
|
OSSL_FUNC_cipher_settable_ctx_params_fn ossl_cipher_aead_settable_ctx_params;
|
|
|
|
|
|
|
|
int ossl_cipher_generic_get_params(OSSL_PARAM params[], unsigned int md,
|
2020-12-17 14:39:57 +08:00
|
|
|
uint64_t flags,
|
|
|
|
size_t kbits, size_t blkbits, size_t ivbits);
|
2020-09-29 15:40:26 +08:00
|
|
|
void ossl_cipher_generic_initkey(void *vctx, size_t kbits, size_t blkbits,
|
2020-12-17 14:39:57 +08:00
|
|
|
size_t ivbits, unsigned int mode,
|
|
|
|
uint64_t flags,
|
|
|
|
const PROV_CIPHER_HW *hw, void *provctx);
|
2019-08-26 15:05:08 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits,\
|
2019-10-08 14:42:28 +08:00
|
|
|
blkbits, ivbits, typ) \
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
|
|
|
(void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
|
2020-09-29 15:40:26 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
|
|
|
|
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
|
|
|
(void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_get_ctx_params }, \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_set_ctx_params }, \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_gettable_params }, \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
|
2019-08-26 15:05:08 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_settable_ctx_params }, \
|
2023-04-19 22:08:22 +08:00
|
|
|
OSSL_DISPATCH_END \
|
2019-08-26 15:05:08 +08:00
|
|
|
};
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, \
|
2019-11-15 00:05:19 +08:00
|
|
|
kbits, blkbits, ivbits, typ) \
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \
|
2019-11-15 00:05:19 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
|
|
|
(void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
|
2020-09-29 15:40:26 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\
|
|
|
|
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\
|
|
|
|
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
|
|
|
|
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
|
2019-11-15 00:05:19 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
|
|
|
(void (*)(void)) alg##_##kbits##_##lcmode##_get_params }, \
|
|
|
|
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_get_ctx_params }, \
|
2019-11-15 00:05:19 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \
|
2019-11-15 00:05:19 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_gettable_params }, \
|
2019-11-15 00:05:19 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
|
2019-11-15 00:05:19 +08:00
|
|
|
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
|
2020-09-29 15:40:26 +08:00
|
|
|
(void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \
|
2023-04-19 22:08:22 +08:00
|
|
|
OSSL_DISPATCH_END \
|
2019-11-15 00:05:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, \
|
2019-11-15 00:05:19 +08:00
|
|
|
kbits, blkbits, ivbits, typ) \
|
2020-09-29 15:40:26 +08:00
|
|
|
static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
|
2019-10-08 14:42:28 +08:00
|
|
|
static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
|
|
|
|
{ \
|
2020-09-29 15:40:26 +08:00
|
|
|
return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
|
|
|
|
flags, kbits, blkbits, ivbits); \
|
2019-10-08 14:42:28 +08:00
|
|
|
} \
|
2020-09-29 15:40:26 +08:00
|
|
|
static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx; \
|
2019-10-08 14:42:28 +08:00
|
|
|
static void * alg##_##kbits##_##lcmode##_newctx(void *provctx) \
|
|
|
|
{ \
|
2020-09-08 10:56:34 +08:00
|
|
|
PROV_##UCALG##_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx))\
|
|
|
|
: NULL; \
|
2019-10-08 14:42:28 +08:00
|
|
|
if (ctx != NULL) { \
|
2020-09-29 15:40:26 +08:00
|
|
|
ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, \
|
|
|
|
EVP_CIPH_##UCMODE##_MODE, flags, \
|
|
|
|
ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
|
|
|
|
provctx); \
|
2019-10-08 14:42:28 +08:00
|
|
|
} \
|
|
|
|
return ctx; \
|
|
|
|
} \
|
2019-11-15 00:05:19 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_generic_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
|
2019-11-15 00:05:19 +08:00
|
|
|
blkbits, ivbits, typ) \
|
|
|
|
IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \
|
|
|
|
blkbits, ivbits, typ) \
|
2019-10-08 14:42:28 +08:00
|
|
|
IMPLEMENT_generic_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \
|
|
|
|
blkbits, ivbits, typ)
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_var_keylen_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits, \
|
2019-11-15 00:05:19 +08:00
|
|
|
blkbits, ivbits, typ) \
|
|
|
|
IMPLEMENT_generic_cipher_genfn(alg, UCALG, lcmode, UCMODE, flags, kbits, \
|
|
|
|
blkbits, ivbits, typ) \
|
|
|
|
IMPLEMENT_var_keylen_cipher_func(alg, UCALG, lcmode, UCMODE, flags, kbits, \
|
|
|
|
blkbits, ivbits, typ)
|
|
|
|
|
2020-09-29 15:40:26 +08:00
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cbc;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ecb;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ofb128;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb128;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb8;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_cfb1;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_generic_ctr;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cbc;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb8;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_cfb128;
|
|
|
|
PROV_CIPHER_HW_FN ossl_cipher_hw_chunked_ofb128;
|
2022-02-10 02:30:25 +08:00
|
|
|
# define ossl_cipher_hw_chunked_ecb ossl_cipher_hw_generic_ecb
|
|
|
|
# define ossl_cipher_hw_chunked_ctr ossl_cipher_hw_generic_ctr
|
|
|
|
# define ossl_cipher_hw_chunked_cfb1 ossl_cipher_hw_generic_cfb1
|
2019-08-26 15:05:08 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_CIPHER_HW_OFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
|
2019-09-15 18:06:28 +08:00
|
|
|
static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
|
|
|
|
unsigned char *out, \
|
|
|
|
const unsigned char *in, size_t len) \
|
|
|
|
{ \
|
|
|
|
int num = ctx->num; \
|
|
|
|
KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
|
|
|
|
\
|
|
|
|
while (len >= MAXCHUNK) { \
|
|
|
|
FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, &num); \
|
|
|
|
len -= MAXCHUNK; \
|
|
|
|
in += MAXCHUNK; \
|
|
|
|
out += MAXCHUNK; \
|
|
|
|
} \
|
|
|
|
if (len > 0) { \
|
|
|
|
FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, &num); \
|
|
|
|
} \
|
|
|
|
ctx->num = num; \
|
|
|
|
return 1; \
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_CIPHER_HW_ECB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
|
2019-09-15 18:06:28 +08:00
|
|
|
static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
|
|
|
|
unsigned char *out, \
|
|
|
|
const unsigned char *in, size_t len) \
|
|
|
|
{ \
|
|
|
|
size_t i, bl = ctx->blocksize; \
|
|
|
|
KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
|
|
|
|
\
|
|
|
|
if (len < bl) \
|
|
|
|
return 1; \
|
|
|
|
for (i = 0, len -= bl; i <= len; i += bl) \
|
|
|
|
FUNC_PREFIX##_encrypt(in + i, out + i, key, ctx->enc); \
|
|
|
|
return 1; \
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_CIPHER_HW_CBC(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
|
2019-09-15 18:06:28 +08:00
|
|
|
static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
|
|
|
|
unsigned char *out, \
|
|
|
|
const unsigned char *in, size_t len) \
|
|
|
|
{ \
|
|
|
|
KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
|
|
|
|
\
|
|
|
|
while (len >= MAXCHUNK) { \
|
|
|
|
FUNC_PREFIX##_encrypt(in, out, MAXCHUNK, key, ctx->iv, ctx->enc); \
|
|
|
|
len -= MAXCHUNK; \
|
|
|
|
in += MAXCHUNK; \
|
|
|
|
out += MAXCHUNK; \
|
|
|
|
} \
|
|
|
|
if (len > 0) \
|
|
|
|
FUNC_PREFIX##_encrypt(in, out, (long)len, key, ctx->iv, ctx->enc); \
|
|
|
|
return 1; \
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_CIPHER_HW_CFB(MODE, NAME, CTX_NAME, KEY_NAME, FUNC_PREFIX) \
|
2019-09-15 18:06:28 +08:00
|
|
|
static int cipher_hw_##NAME##_##MODE##_cipher(PROV_CIPHER_CTX *ctx, \
|
|
|
|
unsigned char *out, \
|
|
|
|
const unsigned char *in, size_t len) \
|
|
|
|
{ \
|
|
|
|
size_t chunk = MAXCHUNK; \
|
|
|
|
KEY_NAME *key = &(((CTX_NAME *)ctx)->ks.ks); \
|
|
|
|
int num = ctx->num; \
|
|
|
|
\
|
|
|
|
if (len < chunk) \
|
|
|
|
chunk = len; \
|
|
|
|
while (len > 0 && len >= chunk) { \
|
|
|
|
FUNC_PREFIX##_encrypt(in, out, (long)chunk, key, ctx->iv, &num, \
|
|
|
|
ctx->enc); \
|
|
|
|
len -= chunk; \
|
|
|
|
in += chunk; \
|
|
|
|
out += chunk; \
|
|
|
|
if (len < chunk) \
|
|
|
|
chunk = len; \
|
|
|
|
} \
|
|
|
|
ctx->num = num; \
|
|
|
|
return 1; \
|
|
|
|
}
|
2019-09-19 18:10:25 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define IMPLEMENT_CIPHER_HW_COPYCTX(name, CTX_TYPE) \
|
2019-11-18 11:13:05 +08:00
|
|
|
static void name(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src) \
|
|
|
|
{ \
|
|
|
|
CTX_TYPE *sctx = (CTX_TYPE *)src; \
|
|
|
|
CTX_TYPE *dctx = (CTX_TYPE *)dst; \
|
|
|
|
\
|
|
|
|
*dctx = *sctx; \
|
|
|
|
dst->ks = &dctx->ks.ks; \
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(name) \
|
2019-10-03 14:05:49 +08:00
|
|
|
static const OSSL_PARAM name##_known_gettable_ctx_params[] = { \
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), \
|
|
|
|
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), \
|
|
|
|
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
|
|
|
|
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL), \
|
Support cipher provider "iv state"
Some modes (e.g., CBC and OFB) update the effective IV with each
block-cipher invocation, making the "IV" stored in the (historically)
EVP_CIPHER_CTX or (current) PROV_CIPHER_CTX distinct from the initial
IV passed in at cipher initialization time. The latter is stored in
the "oiv" (original IV) field, and has historically been accessible
via the EVP_CIPHER_CTX_original_iv() API. The "effective IV" has
also historically been accessible, via both EVP_CIPHER_CTX_iv()
and EVP_CIPHER_CTX_iv_noconst(), the latter of which allows for
*write* access to the internal cipher state. This is particularly
problematic given that provider-internal cipher state need not, in
general, even be accessible from the same address space as libcrypto,
so these APIs are not sustainable in the long term. However, it still
remains necessary to provide access to the contents of the "IV state"
(e.g., when serializing cipher state for in-kernel TLS); a subsequent
reinitialization of a cipher context using the "IV state" as the
input IV will be able to resume processing of data in a compatible
manner.
This problem was introduced in commit
089cb623be76b88a1eea6fcd135101037661bbc3, which effectively caused
all IV queries to return the "original IV", removing access to the
current IV state of the cipher.
These functions for accessing the (even the "original") IV had remained
undocumented for quite some time, presumably due to unease about
exposing the internals of the cipher state in such a manner.
Note that this also as a side effect "fixes" some "bugs" where things
had been referring to the 'iv' field that should have been using the
'oiv' field. It also fixes the EVP_CTRL_GET_IV cipher control,
which was clearly intended to expose the non-original IV, for
use exporting the cipher state into the kernel for kTLS.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/12233)
2020-06-20 04:50:22 +08:00
|
|
|
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0), \
|
2021-01-14 22:19:46 +08:00
|
|
|
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
|
2019-10-03 14:05:49 +08:00
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(name) \
|
2019-10-03 14:05:49 +08:00
|
|
|
OSSL_PARAM_END \
|
|
|
|
}; \
|
2021-02-23 09:48:35 +08:00
|
|
|
const OSSL_PARAM * name##_gettable_ctx_params(ossl_unused void *cctx, \
|
|
|
|
ossl_unused void *provctx) \
|
2019-10-03 14:05:49 +08:00
|
|
|
{ \
|
|
|
|
return name##_known_gettable_ctx_params; \
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:30:25 +08:00
|
|
|
# define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(name) \
|
2019-10-03 14:05:49 +08:00
|
|
|
static const OSSL_PARAM name##_known_settable_ctx_params[] = { \
|
|
|
|
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_PADDING, NULL), \
|
|
|
|
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
|
2022-02-10 02:30:25 +08:00
|
|
|
# define CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(name) \
|
2019-10-03 14:05:49 +08:00
|
|
|
OSSL_PARAM_END \
|
|
|
|
}; \
|
2021-02-23 09:48:35 +08:00
|
|
|
const OSSL_PARAM * name##_settable_ctx_params(ossl_unused void *cctx, \
|
|
|
|
ossl_unused void *provctx) \
|
2019-10-03 14:05:49 +08:00
|
|
|
{ \
|
|
|
|
return name##_known_settable_ctx_params; \
|
|
|
|
}
|
|
|
|
|
2020-09-29 15:40:26 +08:00
|
|
|
int ossl_cipher_generic_initiv(PROV_CIPHER_CTX *ctx, const unsigned char *iv,
|
|
|
|
size_t ivlen);
|
2019-10-08 07:19:10 +08:00
|
|
|
|
Fix external symbols in the provider cipher implementations.
Partial fix for #12964
This add ossl_ names for the following symbols.
chacha20_dinit, chacha20_einit, chacha20_initctx,
ccm_cipher, ccm_dinit, ccm_einit, ccm_generic_auth_decrypt, ccm_generic_auth_encrypt,
ccm_generic_gettag, ccm_generic_setaad, ccm_generic_setiv, ccm_get_ctx_params,
ccm_initctx, ccm_set_ctx_params, ccm_stream_final, ccm_stream_update
gcm_aad_update, gcm_cipher, gcm_cipher_final, gcm_cipher_update
gcm_dinit, gcm_einit, gcm_get_ctx_params, gcm_initctx, gcm_one_shot
gcm_set_ctx_params, gcm_setiv, gcm_stream_final, gcm_stream_update
tdes_dinit, tdes_dupctx, tdes_einit, tdes_freectx
tdes_get_ctx_params, tdes_gettable_ctx_params, tdes_newctx
PROV_CIPHER_HW_des_*,
padblock, unpadblock, tlsunpadblock, fillblock, trailingdata
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14209)
2021-02-17 15:54:29 +08:00
|
|
|
size_t ossl_cipher_fillblock(unsigned char *buf, size_t *buflen,
|
|
|
|
size_t blocksize,
|
|
|
|
const unsigned char **in, size_t *inlen);
|
|
|
|
int ossl_cipher_trailingdata(unsigned char *buf, size_t *buflen,
|
|
|
|
size_t blocksize,
|
|
|
|
const unsigned char **in, size_t *inlen);
|
2022-02-10 02:30:25 +08:00
|
|
|
|
|
|
|
#endif
|