Combines two similar functions into one function to simplify the codes.
Signed-off-by: Qi Tao taoqi10@huawei.com --- drv/isa_ce_sm4.c | 70 +++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 42 deletions(-)
diff --git a/drv/isa_ce_sm4.c b/drv/isa_ce_sm4.c index ceaefeb..6b2ea88 100644 --- a/drv/isa_ce_sm4.c +++ b/drv/isa_ce_sm4.c @@ -224,69 +224,45 @@ static void sm4_set_decrypt_key(const __u8 *userKey, struct SM4_KEY *key) sm4_v8_set_decrypt_key(userKey, key); }
-static void sm4_cfb_encrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey_enc) +static void sm4_cfb_crypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey, const int enc) { unsigned char keydata[SM4_BLOCK_SIZE]; const unsigned char *src = msg->in; unsigned char *dst = msg->out; __u32 nbytes = msg->in_bytes; - __u32 blocks; - __u32 bbytes; + __u32 blocks, bbytes; __u32 i = 0;
blocks = SM4_BYTES2BLKS(nbytes); if (blocks) { - sm4_v8_cfb_encrypt_blocks(src, dst, blocks, rkey_enc, msg->iv); + if (enc == SM4_ENCRYPT) + sm4_v8_cfb_encrypt_blocks(src, dst, blocks, rkey, msg->iv); + else + sm4_v8_cfb_decrypt_blocks(src, dst, blocks, rkey, msg->iv); + bbytes = blocks * SM4_BLOCK_SIZE; dst += bbytes; src += bbytes; nbytes -= bbytes; }
- if (nbytes > 0) { - sm4_v8_crypt_block(msg->iv, keydata, rkey_enc); - while (nbytes > 0) { - *dst++ = *src++ ^ keydata[i++]; - nbytes--; - } + if (nbytes == 0) + return;
- /* store new IV */ + sm4_v8_crypt_block(msg->iv, keydata, rkey); + while (nbytes > 0) { + *dst++ = *src++ ^ keydata[i++]; + nbytes--; + } + + /* store new IV */ + if (enc == SM4_ENCRYPT) { if (msg->out_bytes >= msg->iv_bytes) memcpy(msg->iv, msg->out + msg->out_bytes - msg->iv_bytes, msg->iv_bytes); else memcpy(msg->iv, msg->out, msg->out_bytes); - } -} - -static void sm4_cfb_decrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey_dec) -{ - __u32 nbytes = msg->in_bytes; - const unsigned char *src = msg->in; - unsigned char *dst = msg->out; - __u32 blocks; - __u32 bbytes; - - blocks = SM4_BYTES2BLKS(nbytes); - if (blocks) { - sm4_v8_cfb_decrypt_blocks(src, dst, blocks, rkey_dec, msg->iv); - bbytes = blocks * SM4_BLOCK_SIZE; - dst += bbytes; - src += bbytes; - nbytes -= bbytes; - } - - if (nbytes > 0) { - unsigned char keydata[SM4_BLOCK_SIZE]; - __u32 i = 0; - - sm4_v8_crypt_block(msg->iv, keydata, rkey_dec); - while (nbytes > 0) { - *dst++ = *src++ ^ keydata[i++]; - nbytes--; - } - - /* store new IV */ + } else { if (msg->in_bytes >= msg->iv_bytes) memcpy(msg->iv, msg->in + msg->in_bytes - msg->iv_bytes, msg->iv_bytes); @@ -295,6 +271,16 @@ static void sm4_cfb_decrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rke } }
+static void sm4_cfb_encrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey_enc) +{ + sm4_cfb_crypt(msg, rkey_enc, SM4_ENCRYPT); +} + +static void sm4_cfb_decrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey_dec) +{ + sm4_cfb_crypt(msg, rkey_dec, SM4_DECRYPT); +} + static int sm4_xts_encrypt(struct wd_cipher_msg *msg, const struct SM4_KEY *rkey) { struct SM4_KEY rkey2;