1. Use macros to replace repeated calculations. 2. Remove duplicate initialization of variable. 3. Use "__u32" to replace "unsigned int". 4. Fix format alignment and comment formatting.
Signed-off-by: Qi Tao taoqi10@huawei.com --- drv/isa_ce_sm4.c | 34 ++++++++++++++++------------------ v1/wd.c | 3 ++- v1/wd_rng.c | 4 ++-- 3 files changed, 20 insertions(+), 21 deletions(-)
diff --git a/drv/isa_ce_sm4.c b/drv/isa_ce_sm4.c index 4aa62f1..0e19818 100644 --- a/drv/isa_ce_sm4.c +++ b/drv/isa_ce_sm4.c @@ -18,10 +18,10 @@ #define SM4_ENCRYPT 1 #define SM4_DECRYPT 0 #define MSG_Q_DEPTH 1024 -#define INCREASE_BITS 96 -#define BYTE_BITS 8 +#define INCREASE_BYTES 12 #define SM4_BLOCK_SIZE 16 #define MAX_BLOCK_NUM (1U << 28) +#define CTR96_SHIFT_BITS 8 #define SM4_BYTES2BLKS(nbytes) ((nbytes) >> 4) #define SM4_KEY_SIZE 16
@@ -49,14 +49,14 @@ static void isa_ce_exit(struct wd_alg_driver *drv) /* increment upper 96 bits of 128-bit counter by 1 */ static void ctr96_inc(__u8 *counter) { - __u32 n = INCREASE_BITS / BYTE_BITS; + __u32 n = INCREASE_BYTES; __u32 c = 1;
do { --n; c += counter[n]; counter[n] = (__u8)c; - c >>= BYTE_BITS; + c >>= CTR96_SHIFT_BITS; } while (n); }
@@ -74,7 +74,7 @@ static void sm4_v8_ctr32_encrypt(__u8 *in, __u8 *out, n = (n + 1) % SM4_BLOCK_SIZE; }
- ctr32 = GETU32(iv + INCREASE_BITS / BYTE_BITS); + ctr32 = GETU32(iv + INCREASE_BYTES); while (len >= SM4_BLOCK_SIZE) { blocks = len / SM4_BLOCK_SIZE; /* @@ -97,7 +97,7 @@ static void sm4_v8_ctr32_encrypt(__u8 *in, __u8 *out, } sm4_v8_ctr32_encrypt_blocks(in, out, blocks, key, iv); /* (*ctr) does not update iv, caller does: */ - PUTU32(iv + INCREASE_BITS / BYTE_BITS, ctr32); + PUTU32(iv + INCREASE_BYTES, ctr32); /* ... overflow was detected, propagate carry. */ if (ctr32 == 0) ctr96_inc(iv); @@ -107,10 +107,9 @@ static void sm4_v8_ctr32_encrypt(__u8 *in, __u8 *out, in += offset; } if (len) { - memset(ecount_buf, 0, SM4_BLOCK_SIZE); sm4_v8_ctr32_encrypt_blocks(ecount_buf, ecount_buf, 1, key, iv); ++ctr32; - PUTU32(iv + INCREASE_BITS / BYTE_BITS, ctr32); + PUTU32(iv + INCREASE_BYTES, ctr32); if (ctr32 == 0) ctr96_inc(iv); while (len--) { @@ -233,11 +232,13 @@ static void sm4_set_decrypt_key(const __u8 *userKey, struct SM4_KEY *key)
static void sm4_cfb_encrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey_enc) { - unsigned int nbytes = msg->in_bytes; + unsigned char keydata[SM4_BLOCK_SIZE]; const unsigned char *src = msg->in; unsigned char *dst = msg->out; - unsigned int blocks; - unsigned int bbytes; + __u32 nbytes = msg->in_bytes; + __u32 blocks; + __u32 bbytes; + __u32 i = 0;
blocks = SM4_BYTES2BLKS(nbytes); if (blocks) { @@ -249,9 +250,6 @@ static void sm4_cfb_encrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rke }
if (nbytes > 0) { - unsigned char keydata[SM4_BLOCK_SIZE]; - unsigned int i = 0; - sm4_v8_crypt_block(msg->iv, keydata, rkey_enc); while (nbytes > 0) { *dst++ = *src++ ^ keydata[i++]; @@ -269,11 +267,11 @@ static void sm4_cfb_encrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rke
static void sm4_cfb_decrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey_dec) { - unsigned int nbytes = msg->in_bytes; + __u32 nbytes = msg->in_bytes; const unsigned char *src = msg->in; unsigned char *dst = msg->out; - unsigned int blocks; - unsigned int bbytes; + __u32 blocks; + __u32 bbytes;
blocks = SM4_BYTES2BLKS(nbytes); if (blocks) { @@ -286,7 +284,7 @@ static void sm4_cfb_decrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rke
if (nbytes > 0) { unsigned char keydata[SM4_BLOCK_SIZE]; - unsigned int i = 0; + __u32 i = 0;
sm4_v8_crypt_block(msg->iv, keydata, rkey_dec); while (nbytes > 0) { diff --git a/v1/wd.c b/v1/wd.c index 26e7af3..4286bbe 100644 --- a/v1/wd.c +++ b/v1/wd.c @@ -88,7 +88,8 @@ static int get_raw_attr(const char *dev_root, const char *attr, if (ptrRet == NULL) return -WD_ENODEV;
- /* The attr_file = "/sys/class/uacce/xxx" + /* + * The attr_file = "/sys/class/uacce/xxx" * It's the Internal Definition File Node */ fd = open(attr_path, O_RDONLY, 0); diff --git a/v1/wd_rng.c b/v1/wd_rng.c index 24a4b7a..7a89cd1 100644 --- a/v1/wd_rng.c +++ b/v1/wd_rng.c @@ -57,7 +57,7 @@ static int wcrypto_setup_qinfo(struct wcrypto_rng_ctx_setup *setup, WD_ERR("algorithm mismatch!\n"); return ret; } - qinfo = q->qinfo; + qinfo = q->qinfo; /* lock at ctx creating */ wd_spinlock(&qinfo->qlock); if (qinfo->ctx_num >= WD_MAX_CTX_NUM) { @@ -120,7 +120,7 @@ void *wcrypto_create_rng_ctx(struct wd_queue *q, return ctx;
free_ctx_id: - qinfo = q->qinfo; + qinfo = q->qinfo; wd_spinlock(&qinfo->qlock); qinfo->ctx_num--; wd_free_id(qinfo->ctx_id, WD_MAX_CTX_NUM, ctx_id, WD_MAX_CTX_NUM);