Re: [PATCH 2/3] crypto: cavium: Remove the individual encrypt/decrypt function for each algorithm

2017-04-24 Thread Herbert Xu
On Fri, Apr 21, 2017 at 11:16:05AM +, George Cherian wrote:
>
> -int cvm_aes_encrypt_cbc(struct ablkcipher_request *req)
> +static inline u32 cvm_cipher_type(const char *name)
>  {
> - return cvm_enc_dec(req, true, AES_CBC);
> -}
>  
> -int cvm_aes_decrypt_cbc(struct ablkcipher_request *req)
> -{
> - return cvm_enc_dec(req, false, AES_CBC);
> + const struct cvm_cipher *cipher = cvm_cipher_table;
> +
> + while (cipher->name) {
> + if (!strcmp(cipher->name, name))
> + break;
> + cipher++;
> + }
> +
> + return cipher->value;
>  }

That's rather unwieldy.  It's usually easier to embed the cipher
type into the algo structure and then get to it via the alg object.

Cheers,
-- 
Email: Herbert Xu 
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt


[PATCH 2/3] crypto: cavium: Remove the individual encrypt/decrypt function for each algorithm

2017-04-20 Thread George Cherian
Remove the individual encrypt/decrypt function for easch algorithm.
This is in prepration of adding more crypto algorithms supported by
hardware. While at that simplify create_ctx_hdr/create_input_list
function interfaces.

Signed-off-by: George Cherian 
---
 drivers/crypto/cavium/cpt/cptvf_algs.c | 133 +
 drivers/crypto/cavium/cpt/cptvf_algs.h |   7 ++
 2 files changed, 77 insertions(+), 63 deletions(-)

diff --git a/drivers/crypto/cavium/cpt/cptvf_algs.c 
b/drivers/crypto/cavium/cpt/cptvf_algs.c
index cc853f9..c365fc6 100644
--- a/drivers/crypto/cavium/cpt/cptvf_algs.c
+++ b/drivers/crypto/cavium/cpt/cptvf_algs.c
@@ -98,7 +98,6 @@ static inline void update_output_data(struct cpt_request_info 
*req_info,
 }
 
 static inline u32 create_ctx_hdr(struct ablkcipher_request *req, u32 enc,
-u32 cipher_type, u32 aes_key_type,
 u32 *argcnt)
 {
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
@@ -124,11 +123,11 @@ static inline u32 create_ctx_hdr(struct 
ablkcipher_request *req, u32 enc,
req_info->req.param1 = req->nbytes; /* Encryption Data length */
req_info->req.param2 = 0; /*Auth data length */
 
-   fctx->enc.enc_ctrl.e.enc_cipher = cipher_type;
-   fctx->enc.enc_ctrl.e.aes_key = aes_key_type;
+   fctx->enc.enc_ctrl.e.enc_cipher = ctx->cipher_type;
+   fctx->enc.enc_ctrl.e.aes_key = ctx->key_type;
fctx->enc.enc_ctrl.e.iv_source = FROM_DPTR;
 
-   if (cipher_type == AES_XTS)
+   if (ctx->cipher_type == AES_XTS)
memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len * 2);
else
memcpy(fctx->enc.encr_key, ctx->enc_key, ctx->key_len);
@@ -154,14 +153,13 @@ static inline u32 create_ctx_hdr(struct 
ablkcipher_request *req, u32 enc,
 }
 
 static inline u32 create_input_list(struct ablkcipher_request  *req, u32 enc,
-   u32 cipher_type, u32 aes_key_type,
u32 enc_iv_len)
 {
struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
struct cpt_request_info *req_info = >cpt_req;
u32 argcnt =  0;
 
-   create_ctx_hdr(req, enc, cipher_type, aes_key_type, );
+   create_ctx_hdr(req, enc, );
update_input_iv(req_info, req->info, enc_iv_len, );
update_input_data(req_info, req->src, req->nbytes, );
req_info->incnt = argcnt;
@@ -177,7 +175,6 @@ static inline void store_cb_info(struct ablkcipher_request 
*req,
 }
 
 static inline void create_output_list(struct ablkcipher_request *req,
- u32 cipher_type,
  u32 enc_iv_len)
 {
struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
@@ -197,12 +194,9 @@ static inline void create_output_list(struct 
ablkcipher_request *req,
req_info->outcnt = argcnt;
 }
 
-static inline int cvm_enc_dec(struct ablkcipher_request *req, u32 enc,
- u32 cipher_type)
+static inline int cvm_enc_dec(struct ablkcipher_request *req, u32 enc)
 {
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
-   struct cvm_enc_ctx *ctx = crypto_ablkcipher_ctx(tfm);
-   u32 key_type = AES_128_BIT;
struct cvm_req_ctx *rctx = ablkcipher_request_ctx(req);
u32 enc_iv_len = crypto_ablkcipher_ivsize(tfm);
struct fc_context *fctx = >fctx;
@@ -210,36 +204,10 @@ static inline int cvm_enc_dec(struct ablkcipher_request 
*req, u32 enc,
void *cdev = NULL;
int status;
 
-   switch (ctx->key_len) {
-   case 16:
-   key_type = AES_128_BIT;
-   break;
-   case 24:
-   key_type = AES_192_BIT;
-   break;
-   case 32:
-   if (cipher_type == AES_XTS)
-   key_type = AES_128_BIT;
-   else
-   key_type = AES_256_BIT;
-   break;
-   case 64:
-   if (cipher_type == AES_XTS)
-   key_type = AES_256_BIT;
-   else
-   return -EINVAL;
-   break;
-   default:
-   return -EINVAL;
-   }
-
-   if (cipher_type == DES3_CBC)
-   key_type = 0;
-
memset(req_info, 0, sizeof(struct cpt_request_info));
memset(fctx, 0, sizeof(struct fc_context));
-   create_input_list(req, enc, cipher_type, key_type, enc_iv_len);
-   create_output_list(req, cipher_type, enc_iv_len);
+   create_input_list(req, enc, enc_iv_len);
+   create_output_list(req, enc_iv_len);
store_cb_info(req, req_info);
cdev = dev_handle.cdev[smp_processor_id()];
status = cptvf_do_request(cdev, req_info);
@@ -254,34 +222,41 @@ static inline int cvm_enc_dec(struct ablkcipher_request 
*req, u32 enc,
return -EINPROGRESS;
 }
 
-int cvm_des3_encrypt_cbc(struct