diff mbox series

[v7,02/11] crypto: akcipher - check the presence of callback before the call

Message ID 20190301175918.29694-3-vt@altlinux.org (mailing list archive)
State Changes Requested
Delegated to: Herbert Xu
Headers show
Series crypto: add EC-RDSA (GOST 34.10) algorithm | expand

Commit Message

Vitaly Chikunov March 1, 2019, 5:59 p.m. UTC
Because with introduction of EC-RDSA and change in workings of RSA in
regard to sign/verify, akcipher could have not all callbacks defined,
check the presence of callbacks before calling them to increase
robustness.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
---
 include/crypto/akcipher.h | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

Comments

Herbert Xu March 21, 2019, 11:19 a.m. UTC | #1
On Fri, Mar 01, 2019 at 08:59:09PM +0300, Vitaly Chikunov wrote:
> Because with introduction of EC-RDSA and change in workings of RSA in
> regard to sign/verify, akcipher could have not all callbacks defined,
> check the presence of callbacks before calling them to increase
> robustness.
> 
> Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
> ---
>  include/crypto/akcipher.h | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)
> 
> diff --git a/include/crypto/akcipher.h b/include/crypto/akcipher.h
> index 2d690494568c..f537fad1989f 100644
> --- a/include/crypto/akcipher.h
> +++ b/include/crypto/akcipher.h
> @@ -268,7 +268,10 @@ static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
>  {
>  	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
>  
> -	return alg->max_size(tfm);
> +	if (alg->max_size)
> +		return alg->max_size(tfm);
> +	else
> +		return 0;
>  }

Please do these checks at registration time instead and provide
default implementations as needed.

Thanks,
Vitaly Chikunov March 21, 2019, 11:42 a.m. UTC | #2
Herbert,

On Thu, Mar 21, 2019 at 07:19:18PM +0800, Herbert Xu wrote:
> On Fri, Mar 01, 2019 at 08:59:09PM +0300, Vitaly Chikunov wrote:
> > Because with introduction of EC-RDSA and change in workings of RSA in
> > regard to sign/verify, akcipher could have not all callbacks defined,
> > check the presence of callbacks before calling them to increase
> > robustness.
> > 
> > Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
> > ---
> >  include/crypto/akcipher.h | 25 ++++++++++++++++---------
> >  1 file changed, 16 insertions(+), 9 deletions(-)
> > 
> > diff --git a/include/crypto/akcipher.h b/include/crypto/akcipher.h
> > index 2d690494568c..f537fad1989f 100644
> > --- a/include/crypto/akcipher.h
> > +++ b/include/crypto/akcipher.h
> > @@ -268,7 +268,10 @@ static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
> >  {
> >  	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
> >  
> > -	return alg->max_size(tfm);
> > +	if (alg->max_size)
> > +		return alg->max_size(tfm);
> > +	else
> > +		return 0;
> >  }
> 
> Please do these checks at registration time instead and provide
> default implementations as needed.

I guess patch "PATCH v7 01/11] KEYS: report to keyctl only actually
supported key ops" should also be removed and will infer anything from
presence of the callbacks.


Thanks,


> 
> Thanks,
> -- 
> Email: Herbert Xu <herbert@gondor.apana.org.au>
> Home Page: http://gondor.apana.org.au/~herbert/
> PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu March 21, 2019, 12:11 p.m. UTC | #3
On Thu, Mar 21, 2019 at 02:42:10PM +0300, Vitaly Chikunov wrote:
>
> I guess patch "PATCH v7 01/11] KEYS: report to keyctl only actually
> supported key ops" should also be removed and will infer anything from
> presence of the callbacks.

Indeed.  Especially if this is exposed to user-space then you
need to be careful changing the user-space API by clearing the
verify/sign bits for raw RSA.

Cheers,
diff mbox series

Patch

diff --git a/include/crypto/akcipher.h b/include/crypto/akcipher.h
index 2d690494568c..f537fad1989f 100644
--- a/include/crypto/akcipher.h
+++ b/include/crypto/akcipher.h
@@ -268,7 +268,10 @@  static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
 {
 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
 
-	return alg->max_size(tfm);
+	if (alg->max_size)
+		return alg->max_size(tfm);
+	else
+		return 0;
 }
 
 /**
@@ -287,10 +290,11 @@  static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
 	struct crypto_alg *calg = tfm->base.__crt_alg;
 	unsigned int src_len = req->src_len;
-	int ret;
+	int ret = -ENOSYS;
 
 	crypto_stats_get(calg);
-	ret = alg->encrypt(req);
+	if (alg->encrypt)
+		ret = alg->encrypt(req);
 	crypto_stats_akcipher_encrypt(src_len, ret, calg);
 	return ret;
 }
@@ -311,10 +315,11 @@  static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
 	struct crypto_alg *calg = tfm->base.__crt_alg;
 	unsigned int src_len = req->src_len;
-	int ret;
+	int ret = -ENOSYS;
 
 	crypto_stats_get(calg);
-	ret = alg->decrypt(req);
+	if (alg->decrypt)
+		ret = alg->decrypt(req);
 	crypto_stats_akcipher_decrypt(src_len, ret, calg);
 	return ret;
 }
@@ -334,10 +339,11 @@  static inline int crypto_akcipher_sign(struct akcipher_request *req)
 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
 	struct crypto_alg *calg = tfm->base.__crt_alg;
-	int ret;
+	int ret = -ENOSYS;
 
 	crypto_stats_get(calg);
-	ret = alg->sign(req);
+	if (alg->sign)
+		ret = alg->sign(req);
 	crypto_stats_akcipher_sign(ret, calg);
 	return ret;
 }
@@ -357,10 +363,11 @@  static inline int crypto_akcipher_verify(struct akcipher_request *req)
 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
 	struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
 	struct crypto_alg *calg = tfm->base.__crt_alg;
-	int ret;
+	int ret = -ENOSYS;
 
 	crypto_stats_get(calg);
-	ret = alg->verify(req);
+	if (alg->verify)
+		ret = alg->verify(req);
 	crypto_stats_akcipher_verify(ret, calg);
 	return ret;
 }