Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f869741
uadk_prov: fix some cleancode issues and bugs for rsa
Oct 21, 2024
9af16cb
uadk_provider: add input pointer length check for digest and cipher
Oct 21, 2024
481401e
uadk_provider: add openssl ffc library function
Oct 18, 2024
453eeca
uadk_provider: reconstructing dh implementation
Oct 21, 2024
7144a90
uadk_provider: code cleanup for provider bio
Oct 18, 2024
44d9fc6
uadk_provider: add length check for input pointer
Oct 18, 2024
6b557e7
uadk_provider: add a timeout exit condition to the loop
Oct 18, 2024
f6c1d65
uadk_provider: code cleanup for uadk_provider and uadk_engine
Oct 18, 2024
9532695
uadk_provider: add aes alg for uadk_provider in openssl3
Oct 18, 2024
70d3477
uadk_engine: optimized engine update process
Oct 18, 2024
575a8bd
uadk_provider: fix cipher issue when input len is 0 in decrypto update
Oct 18, 2024
e686ca5
uadk_provider: fix the set ctx param function for ivlen and pad
Oct 18, 2024
9b2a0c6
uadk_provider: fix the key and IV verification methods for cipher init
Oct 18, 2024
8fa00d1
uadk_provider: fix the switch condition issue of soft digest in engine
Oct 18, 2024
d943ed0
uadk_provider: extract hardware initialization functions to separate …
Oct 18, 2024
d54ba19
uadk_provider: modify poll loop exit condition
Oct 18, 2024
f1d953f
uadk_engine_ecx: merge some similar code logic
Oct 18, 2024
68fa37a
uadk_engine: fixup pubkey size used in reverse operation
Oct 18, 2024
e6db0a6
uadk_provider: add aes cts alg to uadk_provider in openssl3
Oct 21, 2024
7ae294a
uadk_provider: bugfix cipher decryption issue
Oct 18, 2024
d976478
uadk_engine: remove update iv for cipher
lin755 Oct 21, 2024
6933f86
uadk_provider_rsa: unify function definitions with macro
Oct 18, 2024
68bb3c3
uadk_provider_pkey: fixup functions with the same name
Oct 18, 2024
2a04559
uadk_prov: fix data type conversion errors
Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ uadk_provider_la_SOURCES=uadk_prov_init.c uadk_async.c uadk_utils.c \
uadk_prov_digest.c uadk_prov_cipher.c \
uadk_prov_rsa.c uadk_prov_dh.c \
uadk_prov_bio.c uadk_prov_der_writer.c uadk_prov_packet.c \
uadk_prov_pkey.c uadk_prov_sm2.c
uadk_prov_pkey.c uadk_prov_sm2.c \
uadk_prov_ffc.c

uadk_provider_la_LDFLAGS=-module -version-number $(VERSION)
uadk_provider_la_LIBADD=$(WD_LIBS) -lpthread
Expand Down
6 changes: 4 additions & 2 deletions src/uadk_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ static void *async_poll_process_func(void *args)
{
struct async_poll_task *task;
struct async_op *op;
int ret, idx;
int ret, idx, empty_num;

while (uadk_e_get_async_poll_state()) {
if (sem_wait(&poll_queue.full_sem)) {
Expand All @@ -342,7 +342,9 @@ static void *async_poll_process_func(void *args)

task = async_get_queue_task();
if (!task) {
(void)sem_post(&poll_queue.full_sem);
(void)sem_getvalue(&poll_queue.empty_sem, &empty_num);
if (empty_num != ASYNC_QUEUE_TASK_NUM)
(void)sem_post(&poll_queue.full_sem);
usleep(1);
continue;
}
Expand Down
114 changes: 29 additions & 85 deletions src/uadk_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
#define CTX_ASYNC_ENC 2
#define CTX_ASYNC_DEC 3
#define CTX_NUM 4
#define CTR_128BIT_COUNTER 16
#define CTR_MODE_LEN_SHIFT 4
#define BYTE_BITS 8
#define IV_LEN 16
#define ENV_ENABLED 1
#define MAX_KEY_LEN 64
Expand Down Expand Up @@ -74,7 +71,6 @@ struct cipher_info {
int nid;
enum wd_cipher_alg alg;
enum wd_cipher_mode mode;
__u32 out_bytes;
};

static EVP_CIPHER *uadk_aes_128_cbc;
Expand Down Expand Up @@ -130,30 +126,30 @@ static struct sw_cipher_t sec_ciphers_sw_table[] = {
};

static struct cipher_info cipher_info_table[] = {
{ NID_aes_128_ecb, WD_CIPHER_AES, WD_CIPHER_ECB, 16},
{ NID_aes_192_ecb, WD_CIPHER_AES, WD_CIPHER_ECB, 16},
{ NID_aes_256_ecb, WD_CIPHER_AES, WD_CIPHER_ECB, 16},
{ NID_aes_128_cbc, WD_CIPHER_AES, WD_CIPHER_CBC, 16},
{ NID_aes_192_cbc, WD_CIPHER_AES, WD_CIPHER_CBC, 64},
{ NID_aes_256_cbc, WD_CIPHER_AES, WD_CIPHER_CBC, 64},
{ NID_aes_128_xts, WD_CIPHER_AES, WD_CIPHER_XTS, 32},
{ NID_aes_256_xts, WD_CIPHER_AES, WD_CIPHER_XTS, 512},
{ NID_sm4_cbc, WD_CIPHER_SM4, WD_CIPHER_CBC, 16},
{ NID_des_ede3_cbc, WD_CIPHER_3DES, WD_CIPHER_CBC, 16},
{ NID_des_ede3_ecb, WD_CIPHER_3DES, WD_CIPHER_ECB, 16},
{ NID_aes_128_ctr, WD_CIPHER_AES, WD_CIPHER_CTR, 64},
{ NID_aes_192_ctr, WD_CIPHER_AES, WD_CIPHER_CTR, 64},
{ NID_aes_256_ctr, WD_CIPHER_AES, WD_CIPHER_CTR, 64},
{ NID_aes_128_ofb128, WD_CIPHER_AES, WD_CIPHER_OFB, 16},
{ NID_aes_192_ofb128, WD_CIPHER_AES, WD_CIPHER_OFB, 16},
{ NID_aes_256_ofb128, WD_CIPHER_AES, WD_CIPHER_OFB, 16},
{ NID_aes_128_cfb128, WD_CIPHER_AES, WD_CIPHER_CFB, 16},
{ NID_aes_192_cfb128, WD_CIPHER_AES, WD_CIPHER_CFB, 16},
{ NID_aes_256_cfb128, WD_CIPHER_AES, WD_CIPHER_CFB, 16},
{ NID_sm4_ofb128, WD_CIPHER_SM4, WD_CIPHER_OFB, 16},
{ NID_sm4_cfb128, WD_CIPHER_SM4, WD_CIPHER_CFB, 16},
{ NID_sm4_ecb, WD_CIPHER_SM4, WD_CIPHER_ECB, 16},
{ NID_sm4_ctr, WD_CIPHER_SM4, WD_CIPHER_CTR, 16},
{ NID_aes_128_ecb, WD_CIPHER_AES, WD_CIPHER_ECB},
{ NID_aes_192_ecb, WD_CIPHER_AES, WD_CIPHER_ECB},
{ NID_aes_256_ecb, WD_CIPHER_AES, WD_CIPHER_ECB},
{ NID_aes_128_cbc, WD_CIPHER_AES, WD_CIPHER_CBC},
{ NID_aes_192_cbc, WD_CIPHER_AES, WD_CIPHER_CBC},
{ NID_aes_256_cbc, WD_CIPHER_AES, WD_CIPHER_CBC},
{ NID_aes_128_xts, WD_CIPHER_AES, WD_CIPHER_XTS},
{ NID_aes_256_xts, WD_CIPHER_AES, WD_CIPHER_XTS},
{ NID_sm4_cbc, WD_CIPHER_SM4, WD_CIPHER_CBC},
{ NID_des_ede3_cbc, WD_CIPHER_3DES, WD_CIPHER_CBC},
{ NID_des_ede3_ecb, WD_CIPHER_3DES, WD_CIPHER_ECB},
{ NID_aes_128_ctr, WD_CIPHER_AES, WD_CIPHER_CTR},
{ NID_aes_192_ctr, WD_CIPHER_AES, WD_CIPHER_CTR},
{ NID_aes_256_ctr, WD_CIPHER_AES, WD_CIPHER_CTR},
{ NID_aes_128_ofb128, WD_CIPHER_AES, WD_CIPHER_OFB},
{ NID_aes_192_ofb128, WD_CIPHER_AES, WD_CIPHER_OFB},
{ NID_aes_256_ofb128, WD_CIPHER_AES, WD_CIPHER_OFB},
{ NID_aes_128_cfb128, WD_CIPHER_AES, WD_CIPHER_CFB},
{ NID_aes_192_cfb128, WD_CIPHER_AES, WD_CIPHER_CFB},
{ NID_aes_256_cfb128, WD_CIPHER_AES, WD_CIPHER_CFB},
{ NID_sm4_ofb128, WD_CIPHER_SM4, WD_CIPHER_OFB},
{ NID_sm4_cfb128, WD_CIPHER_SM4, WD_CIPHER_CFB},
{ NID_sm4_ecb, WD_CIPHER_SM4, WD_CIPHER_ECB},
{ NID_sm4_ctr, WD_CIPHER_SM4, WD_CIPHER_CTR},
};

static const EVP_CIPHER *sec_ciphers_get_cipher_sw_impl(int n_id)
Expand Down Expand Up @@ -362,7 +358,7 @@ static int uadk_e_cipher_env_poll(void *ctx)

do {
ret = wd_cipher_poll(expt, &recv);
if (ret < 0 || recv == expt)
if (ret < 0 || recv >= expt)
return ret;
rx_cnt++;
} while (rx_cnt < ENGINE_ENV_RECV_MAX_CNT);
Expand Down Expand Up @@ -484,11 +480,10 @@ static int uadk_e_init_cipher(void)
}

static void cipher_priv_ctx_setup(struct cipher_priv_ctx *priv,
enum wd_cipher_alg alg, enum wd_cipher_mode mode, __u32 out_bytes)
enum wd_cipher_alg alg, enum wd_cipher_mode mode)
{
priv->setup.alg = alg;
priv->setup.mode = mode;
priv->req.out_bytes = out_bytes;
}

static int uadk_e_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
Expand Down Expand Up @@ -519,7 +514,7 @@ static int uadk_e_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
for (i = 0; i < cipher_counts; i++) {
if (nid == cipher_info_table[i].nid) {
cipher_priv_ctx_setup(priv, cipher_info_table[i].alg,
cipher_info_table[i].mode, cipher_info_table[i].out_bytes);
cipher_info_table[i].mode);
break;
}
}
Expand Down Expand Up @@ -576,57 +571,6 @@ static void *uadk_e_cipher_cb(struct wd_cipher_req *req, void *data)
return NULL;
}

/* Increment counter (128-bit int) by c */
static void ctr_iv_inc(uint8_t *counter, __u32 c)
{
uint32_t n = CTR_128BIT_COUNTER;
uint8_t *counter1 = counter;
__u32 c_value = c;

/*
* Since the counter has been increased 1 by the hardware,
* so the c need to decrease 1.
*/
c_value -= 1;
do {
--n;
c_value += counter1[n];
counter1[n] = (uint8_t)c_value;
c_value >>= BYTE_BITS;
} while (n);
}

static void uadk_cipher_update_priv_ctx(struct cipher_priv_ctx *priv)
{
__u16 iv_bytes = priv->req.iv_bytes;
int offset = priv->req.in_bytes - iv_bytes;
unsigned char K[IV_LEN] = {0};
__u32 i;

switch (priv->setup.mode) {
case WD_CIPHER_CFB:
case WD_CIPHER_CBC:
if (priv->req.op_type == WD_CIPHER_ENCRYPTION)
memcpy(priv->iv, priv->req.dst + offset, iv_bytes);
else
memcpy(priv->iv, priv->req.src + offset, iv_bytes);

break;
case WD_CIPHER_OFB:
for (i = 0; i < IV_LEN; i++) {
K[i] = *((unsigned char *)priv->req.src + offset + i) ^
*((unsigned char *)priv->req.dst + offset + i);
}
memcpy(priv->iv, K, iv_bytes);
break;
case WD_CIPHER_CTR:
ctr_iv_inc(priv->iv, priv->req.in_bytes >> CTR_MODE_LEN_SHIFT);
break;
default:
break;
}
}

static int do_cipher_sync(struct cipher_priv_ctx *priv)
{
int ret;
Expand Down Expand Up @@ -742,7 +686,7 @@ static void uadk_e_ctx_init(EVP_CIPHER_CTX *ctx, struct cipher_priv_ctx *priv)
for (i = 0; i < cipher_counts; i++) {
if (nid == cipher_info_table[i].nid) {
cipher_priv_ctx_setup(priv, cipher_info_table[i].alg,
cipher_info_table[i].mode, cipher_info_table[i].out_bytes);
cipher_info_table[i].mode);
break;
}
}
Expand Down Expand Up @@ -782,6 +726,7 @@ static int uadk_e_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,

priv->req.src = (unsigned char *)in;
priv->req.in_bytes = inlen;
priv->req.out_bytes = inlen;
priv->req.dst = out;
priv->req.out_buf_bytes = inlen;

Expand Down Expand Up @@ -813,7 +758,6 @@ static int uadk_e_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
if (!ret)
goto out_notify;
}
uadk_cipher_update_priv_ctx(priv);

return 1;

Expand Down
41 changes: 28 additions & 13 deletions src/uadk_digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ static uint32_t sec_digest_get_sw_threshold(int n_id)
return digest_pkt_threshold_table[i].threshold;
} while (++i < threshold_table_size);

fprintf(stderr, "nid %d not found in digest threshold table", n_id);
fprintf(stderr, "nid %d not found in digest threshold table.\n", n_id);
return 0;
}

Expand Down Expand Up @@ -384,8 +384,8 @@ static __u32 sched_single_pick_next_ctx(handle_t sched_ctx,
{
if (sched_mode)
return CTX_ASYNC;
else
return CTX_SYNC;

return CTX_SYNC;
}

static int sched_single_poll_policy(handle_t h_sched_ctx,
Expand Down Expand Up @@ -684,14 +684,19 @@ static int digest_update_inner(EVP_MD_CTX *ctx, const void *data, size_t data_le
digest_set_msg_state(priv, false);

do {
if (left_len == data_len) {
/*
* If there is data in the buffer, it will be filled and processed. Otherwise, it
* will be processed according to the UADK package len(16M-512Byte). Finally the
* remaining data less than the size of the buffer will be stored in the buffer.
*/
if (priv->last_update_bufflen != 0) {
processing_len = DIGEST_BLOCK_SIZE - priv->last_update_bufflen;
uadk_memcpy(priv->data + priv->last_update_bufflen, tmpdata,
processing_len);

priv->last_update_bufflen = DIGEST_BLOCK_SIZE;
priv->req.in_bytes = DIGEST_BLOCK_SIZE;
priv->req.in = priv->data;
priv->last_update_bufflen = 0;
} else {
if (left_len > BUF_LEN)
processing_len = BUF_LEN;
Expand Down Expand Up @@ -723,14 +728,15 @@ static int digest_update_inner(EVP_MD_CTX *ctx, const void *data, size_t data_le

return 1;
do_soft_digest:
if (priv->state == SEC_DIGEST_FIRST_UPDATING
&& priv->data
&& priv->last_update_bufflen != 0) {
if (priv->state == SEC_DIGEST_FIRST_UPDATING) {
priv->switch_flag = UADK_DO_SOFT;
(void)digest_soft_init(priv);
ret = digest_soft_update(priv, priv->data, priv->last_update_bufflen);
if (ret != 1)
return ret;
/* filling buf has been executed */
if (processing_len < DIGEST_BLOCK_SIZE) {
ret = digest_soft_update(priv, priv->data, DIGEST_BLOCK_SIZE);
if (ret != 1)
return ret;
}

return digest_soft_update(priv, tmpdata, left_len);
}
Expand All @@ -749,6 +755,11 @@ static int uadk_e_digest_update(EVP_MD_CTX *ctx, const void *data, size_t data_l
return 0;
}

if (unlikely(data_len && !data)) {
fprintf(stderr, "data to be digest is NULL.\n");
return 0;
}

if (unlikely(priv->switch_flag == UADK_DO_SOFT))
goto soft_update;

Expand Down Expand Up @@ -877,6 +888,11 @@ static int uadk_e_digest_final(EVP_MD_CTX *ctx, unsigned char *digest)
return 0;
}

if (unlikely(!digest)) {
fprintf(stderr, "the output buffer is NULL.\n");
return 0;
}

digest_set_msg_state(priv, true);
priv->req.in = priv->data;
priv->req.out = priv->out;
Expand Down Expand Up @@ -952,8 +968,7 @@ static int uadk_e_digest_cleanup(EVP_MD_CTX *ctx)
priv->sess = 0;
}

if (priv->soft_ctx)
digest_soft_cleanup(priv);
digest_soft_cleanup(priv);

return 1;
}
Expand Down
Loading