18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API for algorithms (i.e., low-level API).
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#ifndef _CRYPTO_ALGAPI_H
88c2ecf20Sopenharmony_ci#define _CRYPTO_ALGAPI_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/crypto.h>
118c2ecf20Sopenharmony_ci#include <linux/list.h>
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Maximum values for blocksize and alignmask, used to allocate
178c2ecf20Sopenharmony_ci * static buffers that are big enough for any combination of
188c2ecf20Sopenharmony_ci * algs and architectures. Ciphers have a lower maximum size.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci#define MAX_ALGAPI_BLOCKSIZE		160
218c2ecf20Sopenharmony_ci#define MAX_ALGAPI_ALIGNMASK		63
228c2ecf20Sopenharmony_ci#define MAX_CIPHER_BLOCKSIZE		16
238c2ecf20Sopenharmony_ci#define MAX_CIPHER_ALIGNMASK		15
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistruct crypto_aead;
268c2ecf20Sopenharmony_cistruct crypto_instance;
278c2ecf20Sopenharmony_cistruct module;
288c2ecf20Sopenharmony_cistruct rtattr;
298c2ecf20Sopenharmony_cistruct seq_file;
308c2ecf20Sopenharmony_cistruct sk_buff;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct crypto_type {
338c2ecf20Sopenharmony_ci	unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
348c2ecf20Sopenharmony_ci	unsigned int (*extsize)(struct crypto_alg *alg);
358c2ecf20Sopenharmony_ci	int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
368c2ecf20Sopenharmony_ci	int (*init_tfm)(struct crypto_tfm *tfm);
378c2ecf20Sopenharmony_ci	void (*show)(struct seq_file *m, struct crypto_alg *alg);
388c2ecf20Sopenharmony_ci	int (*report)(struct sk_buff *skb, struct crypto_alg *alg);
398c2ecf20Sopenharmony_ci	void (*free)(struct crypto_instance *inst);
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	unsigned int type;
428c2ecf20Sopenharmony_ci	unsigned int maskclear;
438c2ecf20Sopenharmony_ci	unsigned int maskset;
448c2ecf20Sopenharmony_ci	unsigned int tfmsize;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistruct crypto_instance {
488c2ecf20Sopenharmony_ci	struct crypto_alg alg;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	struct crypto_template *tmpl;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	union {
538c2ecf20Sopenharmony_ci		/* Node in list of instances after registration. */
548c2ecf20Sopenharmony_ci		struct hlist_node list;
558c2ecf20Sopenharmony_ci		/* List of attached spawns before registration. */
568c2ecf20Sopenharmony_ci		struct crypto_spawn *spawns;
578c2ecf20Sopenharmony_ci	};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	struct work_struct free_work;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	void *__ctx[] CRYPTO_MINALIGN_ATTR;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct crypto_template {
658c2ecf20Sopenharmony_ci	struct list_head list;
668c2ecf20Sopenharmony_ci	struct hlist_head instances;
678c2ecf20Sopenharmony_ci	struct module *module;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	char name[CRYPTO_MAX_ALG_NAME];
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct crypto_spawn {
758c2ecf20Sopenharmony_ci	struct list_head list;
768c2ecf20Sopenharmony_ci	struct crypto_alg *alg;
778c2ecf20Sopenharmony_ci	union {
788c2ecf20Sopenharmony_ci		/* Back pointer to instance after registration.*/
798c2ecf20Sopenharmony_ci		struct crypto_instance *inst;
808c2ecf20Sopenharmony_ci		/* Spawn list pointer prior to registration. */
818c2ecf20Sopenharmony_ci		struct crypto_spawn *next;
828c2ecf20Sopenharmony_ci	};
838c2ecf20Sopenharmony_ci	const struct crypto_type *frontend;
848c2ecf20Sopenharmony_ci	u32 mask;
858c2ecf20Sopenharmony_ci	bool dead;
868c2ecf20Sopenharmony_ci	bool registered;
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistruct crypto_queue {
908c2ecf20Sopenharmony_ci	struct list_head list;
918c2ecf20Sopenharmony_ci	struct list_head *backlog;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	unsigned int qlen;
948c2ecf20Sopenharmony_ci	unsigned int max_qlen;
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistruct scatter_walk {
988c2ecf20Sopenharmony_ci	struct scatterlist *sg;
998c2ecf20Sopenharmony_ci	unsigned int offset;
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_civoid crypto_mod_put(struct crypto_alg *alg);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint crypto_register_template(struct crypto_template *tmpl);
1058c2ecf20Sopenharmony_ciint crypto_register_templates(struct crypto_template *tmpls, int count);
1068c2ecf20Sopenharmony_civoid crypto_unregister_template(struct crypto_template *tmpl);
1078c2ecf20Sopenharmony_civoid crypto_unregister_templates(struct crypto_template *tmpls, int count);
1088c2ecf20Sopenharmony_cistruct crypto_template *crypto_lookup_template(const char *name);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciint crypto_register_instance(struct crypto_template *tmpl,
1118c2ecf20Sopenharmony_ci			     struct crypto_instance *inst);
1128c2ecf20Sopenharmony_civoid crypto_unregister_instance(struct crypto_instance *inst);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciint crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
1158c2ecf20Sopenharmony_ci		      const char *name, u32 type, u32 mask);
1168c2ecf20Sopenharmony_civoid crypto_drop_spawn(struct crypto_spawn *spawn);
1178c2ecf20Sopenharmony_cistruct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
1188c2ecf20Sopenharmony_ci				    u32 mask);
1198c2ecf20Sopenharmony_civoid *crypto_spawn_tfm2(struct crypto_spawn *spawn);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistruct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
1228c2ecf20Sopenharmony_ciint crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret);
1238c2ecf20Sopenharmony_ciconst char *crypto_attr_alg_name(struct rtattr *rta);
1248c2ecf20Sopenharmony_ciint crypto_attr_u32(struct rtattr *rta, u32 *num);
1258c2ecf20Sopenharmony_ciint crypto_inst_setname(struct crypto_instance *inst, const char *name,
1268c2ecf20Sopenharmony_ci			struct crypto_alg *alg);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_civoid crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
1298c2ecf20Sopenharmony_ciint crypto_enqueue_request(struct crypto_queue *queue,
1308c2ecf20Sopenharmony_ci			   struct crypto_async_request *request);
1318c2ecf20Sopenharmony_civoid crypto_enqueue_request_head(struct crypto_queue *queue,
1328c2ecf20Sopenharmony_ci				 struct crypto_async_request *request);
1338c2ecf20Sopenharmony_cistruct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
1348c2ecf20Sopenharmony_cistatic inline unsigned int crypto_queue_len(struct crypto_queue *queue)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	return queue->qlen;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_civoid crypto_inc(u8 *a, unsigned int size);
1408c2ecf20Sopenharmony_civoid __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int size);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic inline void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1458c2ecf20Sopenharmony_ci	    __builtin_constant_p(size) &&
1468c2ecf20Sopenharmony_ci	    (size % sizeof(unsigned long)) == 0) {
1478c2ecf20Sopenharmony_ci		unsigned long *d = (unsigned long *)dst;
1488c2ecf20Sopenharmony_ci		unsigned long *s = (unsigned long *)src;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		while (size > 0) {
1518c2ecf20Sopenharmony_ci			*d++ ^= *s++;
1528c2ecf20Sopenharmony_ci			size -= sizeof(unsigned long);
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci	} else {
1558c2ecf20Sopenharmony_ci		__crypto_xor(dst, dst, src, size);
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic inline void crypto_xor_cpy(u8 *dst, const u8 *src1, const u8 *src2,
1608c2ecf20Sopenharmony_ci				  unsigned int size)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1638c2ecf20Sopenharmony_ci	    __builtin_constant_p(size) &&
1648c2ecf20Sopenharmony_ci	    (size % sizeof(unsigned long)) == 0) {
1658c2ecf20Sopenharmony_ci		unsigned long *d = (unsigned long *)dst;
1668c2ecf20Sopenharmony_ci		unsigned long *s1 = (unsigned long *)src1;
1678c2ecf20Sopenharmony_ci		unsigned long *s2 = (unsigned long *)src2;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		while (size > 0) {
1708c2ecf20Sopenharmony_ci			*d++ = *s1++ ^ *s2++;
1718c2ecf20Sopenharmony_ci			size -= sizeof(unsigned long);
1728c2ecf20Sopenharmony_ci		}
1738c2ecf20Sopenharmony_ci	} else {
1748c2ecf20Sopenharmony_ci		__crypto_xor(dst, src1, src2, size);
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	return PTR_ALIGN(crypto_tfm_ctx(tfm),
1818c2ecf20Sopenharmony_ci			 crypto_tfm_alg_alignmask(tfm) + 1);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic inline struct crypto_instance *crypto_tfm_alg_instance(
1858c2ecf20Sopenharmony_ci	struct crypto_tfm *tfm)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return container_of(tfm->__crt_alg, struct crypto_instance, alg);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic inline void *crypto_instance_ctx(struct crypto_instance *inst)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	return inst->__ctx;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistruct crypto_cipher_spawn {
1968c2ecf20Sopenharmony_ci	struct crypto_spawn base;
1978c2ecf20Sopenharmony_ci};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic inline int crypto_grab_cipher(struct crypto_cipher_spawn *spawn,
2008c2ecf20Sopenharmony_ci				     struct crypto_instance *inst,
2018c2ecf20Sopenharmony_ci				     const char *name, u32 type, u32 mask)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	type &= ~CRYPTO_ALG_TYPE_MASK;
2048c2ecf20Sopenharmony_ci	type |= CRYPTO_ALG_TYPE_CIPHER;
2058c2ecf20Sopenharmony_ci	mask |= CRYPTO_ALG_TYPE_MASK;
2068c2ecf20Sopenharmony_ci	return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic inline void crypto_drop_cipher(struct crypto_cipher_spawn *spawn)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	crypto_drop_spawn(&spawn->base);
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline struct crypto_alg *crypto_spawn_cipher_alg(
2158c2ecf20Sopenharmony_ci	struct crypto_cipher_spawn *spawn)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	return spawn->base.alg;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic inline struct crypto_cipher *crypto_spawn_cipher(
2218c2ecf20Sopenharmony_ci	struct crypto_cipher_spawn *spawn)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	u32 type = CRYPTO_ALG_TYPE_CIPHER;
2248c2ecf20Sopenharmony_ci	u32 mask = CRYPTO_ALG_TYPE_MASK;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	return __crypto_cipher_cast(crypto_spawn_tfm(&spawn->base, type, mask));
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistatic inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic inline struct crypto_async_request *crypto_get_backlog(
2358c2ecf20Sopenharmony_ci	struct crypto_queue *queue)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	return queue->backlog == &queue->list ? NULL :
2388c2ecf20Sopenharmony_ci	       container_of(queue->backlog, struct crypto_async_request, list);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic inline u32 crypto_requires_off(struct crypto_attr_type *algt, u32 off)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	return (algt->type ^ off) & algt->mask & off;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/*
2478c2ecf20Sopenharmony_ci * When an algorithm uses another algorithm (e.g., if it's an instance of a
2488c2ecf20Sopenharmony_ci * template), these are the flags that should always be set on the "outer"
2498c2ecf20Sopenharmony_ci * algorithm if any "inner" algorithm has them set.
2508c2ecf20Sopenharmony_ci */
2518c2ecf20Sopenharmony_ci#define CRYPTO_ALG_INHERITED_FLAGS	\
2528c2ecf20Sopenharmony_ci	(CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK |	\
2538c2ecf20Sopenharmony_ci	 CRYPTO_ALG_ALLOCATES_MEMORY)
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/*
2568c2ecf20Sopenharmony_ci * Given the type and mask that specify the flags restrictions on a template
2578c2ecf20Sopenharmony_ci * instance being created, return the mask that should be passed to
2588c2ecf20Sopenharmony_ci * crypto_grab_*() (along with type=0) to honor any request the user made to
2598c2ecf20Sopenharmony_ci * have any of the CRYPTO_ALG_INHERITED_FLAGS clear.
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_cistatic inline u32 crypto_algt_inherited_mask(struct crypto_attr_type *algt)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	return crypto_requires_off(algt, CRYPTO_ALG_INHERITED_FLAGS);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cinoinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci/**
2698c2ecf20Sopenharmony_ci * crypto_memneq - Compare two areas of memory without leaking
2708c2ecf20Sopenharmony_ci *		   timing information.
2718c2ecf20Sopenharmony_ci *
2728c2ecf20Sopenharmony_ci * @a: One area of memory
2738c2ecf20Sopenharmony_ci * @b: Another area of memory
2748c2ecf20Sopenharmony_ci * @size: The size of the area.
2758c2ecf20Sopenharmony_ci *
2768c2ecf20Sopenharmony_ci * Returns 0 when data is equal, 1 otherwise.
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_cistatic inline int crypto_memneq(const void *a, const void *b, size_t size)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ciint crypto_register_notifier(struct notifier_block *nb);
2848c2ecf20Sopenharmony_ciint crypto_unregister_notifier(struct notifier_block *nb);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci/* Crypto notification events. */
2878c2ecf20Sopenharmony_cienum {
2888c2ecf20Sopenharmony_ci	CRYPTO_MSG_ALG_REQUEST,
2898c2ecf20Sopenharmony_ci	CRYPTO_MSG_ALG_REGISTER,
2908c2ecf20Sopenharmony_ci	CRYPTO_MSG_ALG_LOADED,
2918c2ecf20Sopenharmony_ci};
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci#endif	/* _CRYPTO_ALGAPI_H */
294