xref: /linux/include/crypto/internal/akcipher.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
23c339ab8STadeusz Struk /*
33c339ab8STadeusz Struk  * Public Key Encryption
43c339ab8STadeusz Struk  *
53c339ab8STadeusz Struk  * Copyright (c) 2015, Intel Corporation
63c339ab8STadeusz Struk  * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
73c339ab8STadeusz Struk  */
83c339ab8STadeusz Struk #ifndef _CRYPTO_AKCIPHER_INT_H
93c339ab8STadeusz Struk #define _CRYPTO_AKCIPHER_INT_H
103c339ab8STadeusz Struk #include <crypto/akcipher.h>
1128a4618aSAndrzej Zaborowski #include <crypto/algapi.h>
1228a4618aSAndrzej Zaborowski 
1328a4618aSAndrzej Zaborowski struct akcipher_instance {
1428a4618aSAndrzej Zaborowski 	void (*free)(struct akcipher_instance *inst);
1528a4618aSAndrzej Zaborowski 	union {
1628a4618aSAndrzej Zaborowski 		struct {
1728a4618aSAndrzej Zaborowski 			char head[offsetof(struct akcipher_alg, base)];
1828a4618aSAndrzej Zaborowski 			struct crypto_instance base;
1928a4618aSAndrzej Zaborowski 		} s;
2028a4618aSAndrzej Zaborowski 		struct akcipher_alg alg;
2128a4618aSAndrzej Zaborowski 	};
2228a4618aSAndrzej Zaborowski };
2328a4618aSAndrzej Zaborowski 
2428a4618aSAndrzej Zaborowski struct crypto_akcipher_spawn {
2528a4618aSAndrzej Zaborowski 	struct crypto_spawn base;
2628a4618aSAndrzej Zaborowski };
273c339ab8STadeusz Struk 
283c339ab8STadeusz Struk /*
293c339ab8STadeusz Struk  * Transform internal helpers.
303c339ab8STadeusz Struk  */
akcipher_request_ctx(struct akcipher_request * req)313c339ab8STadeusz Struk static inline void *akcipher_request_ctx(struct akcipher_request *req)
323c339ab8STadeusz Struk {
333c339ab8STadeusz Struk 	return req->__ctx;
343c339ab8STadeusz Struk }
353c339ab8STadeusz Struk 
akcipher_request_ctx_dma(struct akcipher_request * req)364ac33776SHerbert Xu static inline void *akcipher_request_ctx_dma(struct akcipher_request *req)
374ac33776SHerbert Xu {
384ac33776SHerbert Xu 	unsigned int align = crypto_dma_align();
394ac33776SHerbert Xu 
404ac33776SHerbert Xu 	if (align <= crypto_tfm_ctx_alignment())
414ac33776SHerbert Xu 		align = 1;
424ac33776SHerbert Xu 
434ac33776SHerbert Xu 	return PTR_ALIGN(akcipher_request_ctx(req), align);
444ac33776SHerbert Xu }
454ac33776SHerbert Xu 
akcipher_set_reqsize(struct crypto_akcipher * akcipher,unsigned int reqsize)46333706b8SGary R Hook static inline void akcipher_set_reqsize(struct crypto_akcipher *akcipher,
47333706b8SGary R Hook 					unsigned int reqsize)
48333706b8SGary R Hook {
493e71e5b0SHerbert Xu 	akcipher->reqsize = reqsize;
50333706b8SGary R Hook }
51333706b8SGary R Hook 
akcipher_set_reqsize_dma(struct crypto_akcipher * akcipher,unsigned int reqsize)524ac33776SHerbert Xu static inline void akcipher_set_reqsize_dma(struct crypto_akcipher *akcipher,
534ac33776SHerbert Xu 					    unsigned int reqsize)
544ac33776SHerbert Xu {
554ac33776SHerbert Xu 	reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
564ac33776SHerbert Xu 	akcipher->reqsize = reqsize;
574ac33776SHerbert Xu }
584ac33776SHerbert Xu 
akcipher_tfm_ctx(struct crypto_akcipher * tfm)593c339ab8STadeusz Struk static inline void *akcipher_tfm_ctx(struct crypto_akcipher *tfm)
603c339ab8STadeusz Struk {
614ac33776SHerbert Xu 	return crypto_tfm_ctx(&tfm->base);
624ac33776SHerbert Xu }
634ac33776SHerbert Xu 
akcipher_tfm_ctx_dma(struct crypto_akcipher * tfm)644ac33776SHerbert Xu static inline void *akcipher_tfm_ctx_dma(struct crypto_akcipher *tfm)
654ac33776SHerbert Xu {
664ac33776SHerbert Xu 	return crypto_tfm_ctx_dma(&tfm->base);
673c339ab8STadeusz Struk }
683c339ab8STadeusz Struk 
akcipher_request_complete(struct akcipher_request * req,int err)693c339ab8STadeusz Struk static inline void akcipher_request_complete(struct akcipher_request *req,
703c339ab8STadeusz Struk 					     int err)
713c339ab8STadeusz Struk {
72*700d5078SHerbert Xu 	crypto_request_complete(&req->base, err);
733c339ab8STadeusz Struk }
743c339ab8STadeusz Struk 
akcipher_alg_name(struct crypto_akcipher * tfm)753c339ab8STadeusz Struk static inline const char *akcipher_alg_name(struct crypto_akcipher *tfm)
763c339ab8STadeusz Struk {
773c339ab8STadeusz Struk 	return crypto_akcipher_tfm(tfm)->__crt_alg->cra_name;
783c339ab8STadeusz Struk }
793c339ab8STadeusz Struk 
akcipher_crypto_instance(struct akcipher_instance * inst)8028a4618aSAndrzej Zaborowski static inline struct crypto_instance *akcipher_crypto_instance(
8128a4618aSAndrzej Zaborowski 		struct akcipher_instance *inst)
8228a4618aSAndrzej Zaborowski {
8328a4618aSAndrzej Zaborowski 	return container_of(&inst->alg.base, struct crypto_instance, alg);
8428a4618aSAndrzej Zaborowski }
8528a4618aSAndrzej Zaborowski 
akcipher_instance(struct crypto_instance * inst)8628a4618aSAndrzej Zaborowski static inline struct akcipher_instance *akcipher_instance(
8728a4618aSAndrzej Zaborowski 		struct crypto_instance *inst)
8828a4618aSAndrzej Zaborowski {
8928a4618aSAndrzej Zaborowski 	return container_of(&inst->alg, struct akcipher_instance, alg.base);
9028a4618aSAndrzej Zaborowski }
9128a4618aSAndrzej Zaborowski 
akcipher_alg_instance(struct crypto_akcipher * akcipher)9228a4618aSAndrzej Zaborowski static inline struct akcipher_instance *akcipher_alg_instance(
9328a4618aSAndrzej Zaborowski 		struct crypto_akcipher *akcipher)
9428a4618aSAndrzej Zaborowski {
9528a4618aSAndrzej Zaborowski 	return akcipher_instance(crypto_tfm_alg_instance(&akcipher->base));
9628a4618aSAndrzej Zaborowski }
9728a4618aSAndrzej Zaborowski 
akcipher_instance_ctx(struct akcipher_instance * inst)9828a4618aSAndrzej Zaborowski static inline void *akcipher_instance_ctx(struct akcipher_instance *inst)
9928a4618aSAndrzej Zaborowski {
10028a4618aSAndrzej Zaborowski 	return crypto_instance_ctx(akcipher_crypto_instance(inst));
10128a4618aSAndrzej Zaborowski }
10228a4618aSAndrzej Zaborowski 
10373bed26fSEric Biggers int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
10473bed26fSEric Biggers 			 struct crypto_instance *inst,
10573bed26fSEric Biggers 			 const char *name, u32 type, u32 mask);
10628a4618aSAndrzej Zaborowski 
crypto_spawn_akcipher(struct crypto_akcipher_spawn * spawn)10728a4618aSAndrzej Zaborowski static inline struct crypto_akcipher *crypto_spawn_akcipher(
10828a4618aSAndrzej Zaborowski 		struct crypto_akcipher_spawn *spawn)
10928a4618aSAndrzej Zaborowski {
11028a4618aSAndrzej Zaborowski 	return crypto_spawn_tfm2(&spawn->base);
11128a4618aSAndrzej Zaborowski }
11228a4618aSAndrzej Zaborowski 
crypto_drop_akcipher(struct crypto_akcipher_spawn * spawn)11328a4618aSAndrzej Zaborowski static inline void crypto_drop_akcipher(struct crypto_akcipher_spawn *spawn)
11428a4618aSAndrzej Zaborowski {
11528a4618aSAndrzej Zaborowski 	crypto_drop_spawn(&spawn->base);
11628a4618aSAndrzej Zaborowski }
11728a4618aSAndrzej Zaborowski 
crypto_spawn_akcipher_alg(struct crypto_akcipher_spawn * spawn)11828a4618aSAndrzej Zaborowski static inline struct akcipher_alg *crypto_spawn_akcipher_alg(
11928a4618aSAndrzej Zaborowski 		struct crypto_akcipher_spawn *spawn)
12028a4618aSAndrzej Zaborowski {
12128a4618aSAndrzej Zaborowski 	return container_of(spawn->base.alg, struct akcipher_alg, base);
12228a4618aSAndrzej Zaborowski }
12328a4618aSAndrzej Zaborowski 
1243c339ab8STadeusz Struk /**
1253c339ab8STadeusz Struk  * crypto_register_akcipher() -- Register public key algorithm
1263c339ab8STadeusz Struk  *
1273c339ab8STadeusz Struk  * Function registers an implementation of a public key verify algorithm
1283c339ab8STadeusz Struk  *
1293c339ab8STadeusz Struk  * @alg:	algorithm definition
1303c339ab8STadeusz Struk  *
1313c339ab8STadeusz Struk  * Return: zero on success; error code in case of error
1323c339ab8STadeusz Struk  */
1333c339ab8STadeusz Struk int crypto_register_akcipher(struct akcipher_alg *alg);
1343c339ab8STadeusz Struk 
1353c339ab8STadeusz Struk /**
1363c339ab8STadeusz Struk  * crypto_unregister_akcipher() -- Unregister public key algorithm
1373c339ab8STadeusz Struk  *
1383c339ab8STadeusz Struk  * Function unregisters an implementation of a public key verify algorithm
1393c339ab8STadeusz Struk  *
1403c339ab8STadeusz Struk  * @alg:	algorithm definition
1413c339ab8STadeusz Struk  */
1423c339ab8STadeusz Struk void crypto_unregister_akcipher(struct akcipher_alg *alg);
14328a4618aSAndrzej Zaborowski 
14428a4618aSAndrzej Zaborowski /**
14528a4618aSAndrzej Zaborowski  * akcipher_register_instance() -- Unregister public key template instance
14628a4618aSAndrzej Zaborowski  *
14728a4618aSAndrzej Zaborowski  * Function registers an implementation of an asymmetric key algorithm
14828a4618aSAndrzej Zaborowski  * created from a template
14928a4618aSAndrzej Zaborowski  *
15028a4618aSAndrzej Zaborowski  * @tmpl:	the template from which the algorithm was created
15128a4618aSAndrzej Zaborowski  * @inst:	the template instance
15228a4618aSAndrzej Zaborowski  */
15328a4618aSAndrzej Zaborowski int akcipher_register_instance(struct crypto_template *tmpl,
15428a4618aSAndrzej Zaborowski 		struct akcipher_instance *inst);
1553c339ab8STadeusz Struk #endif
156