Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Cryptographic API for algorithms (i.e., low-level API).
0004  *
0005  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
0006  */
0007 #ifndef _CRYPTO_ALGAPI_H
0008 #define _CRYPTO_ALGAPI_H
0009 
0010 #include <linux/align.h>
0011 #include <linux/crypto.h>
0012 #include <linux/kconfig.h>
0013 #include <linux/list.h>
0014 #include <linux/types.h>
0015 
0016 #include <asm/unaligned.h>
0017 
0018 /*
0019  * Maximum values for blocksize and alignmask, used to allocate
0020  * static buffers that are big enough for any combination of
0021  * algs and architectures. Ciphers have a lower maximum size.
0022  */
0023 #define MAX_ALGAPI_BLOCKSIZE        160
0024 #define MAX_ALGAPI_ALIGNMASK        63
0025 #define MAX_CIPHER_BLOCKSIZE        16
0026 #define MAX_CIPHER_ALIGNMASK        15
0027 
0028 struct crypto_aead;
0029 struct crypto_instance;
0030 struct module;
0031 struct notifier_block;
0032 struct rtattr;
0033 struct seq_file;
0034 struct sk_buff;
0035 
0036 struct crypto_type {
0037     unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
0038     unsigned int (*extsize)(struct crypto_alg *alg);
0039     int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
0040     int (*init_tfm)(struct crypto_tfm *tfm);
0041     void (*show)(struct seq_file *m, struct crypto_alg *alg);
0042     int (*report)(struct sk_buff *skb, struct crypto_alg *alg);
0043     void (*free)(struct crypto_instance *inst);
0044 
0045     unsigned int type;
0046     unsigned int maskclear;
0047     unsigned int maskset;
0048     unsigned int tfmsize;
0049 };
0050 
0051 struct crypto_instance {
0052     struct crypto_alg alg;
0053 
0054     struct crypto_template *tmpl;
0055 
0056     union {
0057         /* Node in list of instances after registration. */
0058         struct hlist_node list;
0059         /* List of attached spawns before registration. */
0060         struct crypto_spawn *spawns;
0061     };
0062 
0063     void *__ctx[] CRYPTO_MINALIGN_ATTR;
0064 };
0065 
0066 struct crypto_template {
0067     struct list_head list;
0068     struct hlist_head instances;
0069     struct module *module;
0070 
0071     int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
0072 
0073     char name[CRYPTO_MAX_ALG_NAME];
0074 };
0075 
0076 struct crypto_spawn {
0077     struct list_head list;
0078     struct crypto_alg *alg;
0079     union {
0080         /* Back pointer to instance after registration.*/
0081         struct crypto_instance *inst;
0082         /* Spawn list pointer prior to registration. */
0083         struct crypto_spawn *next;
0084     };
0085     const struct crypto_type *frontend;
0086     u32 mask;
0087     bool dead;
0088     bool registered;
0089 };
0090 
0091 struct crypto_queue {
0092     struct list_head list;
0093     struct list_head *backlog;
0094 
0095     unsigned int qlen;
0096     unsigned int max_qlen;
0097 };
0098 
0099 struct scatter_walk {
0100     struct scatterlist *sg;
0101     unsigned int offset;
0102 };
0103 
0104 struct crypto_attr_alg {
0105     char name[CRYPTO_MAX_ALG_NAME];
0106 };
0107 
0108 struct crypto_attr_type {
0109     u32 type;
0110     u32 mask;
0111 };
0112 
0113 void crypto_mod_put(struct crypto_alg *alg);
0114 
0115 int crypto_register_template(struct crypto_template *tmpl);
0116 int crypto_register_templates(struct crypto_template *tmpls, int count);
0117 void crypto_unregister_template(struct crypto_template *tmpl);
0118 void crypto_unregister_templates(struct crypto_template *tmpls, int count);
0119 struct crypto_template *crypto_lookup_template(const char *name);
0120 
0121 int crypto_register_instance(struct crypto_template *tmpl,
0122                  struct crypto_instance *inst);
0123 void crypto_unregister_instance(struct crypto_instance *inst);
0124 
0125 int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
0126               const char *name, u32 type, u32 mask);
0127 void crypto_drop_spawn(struct crypto_spawn *spawn);
0128 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
0129                     u32 mask);
0130 void *crypto_spawn_tfm2(struct crypto_spawn *spawn);
0131 
0132 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
0133 int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret);
0134 const char *crypto_attr_alg_name(struct rtattr *rta);
0135 int crypto_inst_setname(struct crypto_instance *inst, const char *name,
0136             struct crypto_alg *alg);
0137 
0138 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
0139 int crypto_enqueue_request(struct crypto_queue *queue,
0140                struct crypto_async_request *request);
0141 void crypto_enqueue_request_head(struct crypto_queue *queue,
0142                  struct crypto_async_request *request);
0143 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
0144 static inline unsigned int crypto_queue_len(struct crypto_queue *queue)
0145 {
0146     return queue->qlen;
0147 }
0148 
0149 void crypto_inc(u8 *a, unsigned int size);
0150 void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int size);
0151 
0152 static inline void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
0153 {
0154     if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
0155         __builtin_constant_p(size) &&
0156         (size % sizeof(unsigned long)) == 0) {
0157         unsigned long *d = (unsigned long *)dst;
0158         unsigned long *s = (unsigned long *)src;
0159         unsigned long l;
0160 
0161         while (size > 0) {
0162             l = get_unaligned(d) ^ get_unaligned(s++);
0163             put_unaligned(l, d++);
0164             size -= sizeof(unsigned long);
0165         }
0166     } else {
0167         __crypto_xor(dst, dst, src, size);
0168     }
0169 }
0170 
0171 static inline void crypto_xor_cpy(u8 *dst, const u8 *src1, const u8 *src2,
0172                   unsigned int size)
0173 {
0174     if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
0175         __builtin_constant_p(size) &&
0176         (size % sizeof(unsigned long)) == 0) {
0177         unsigned long *d = (unsigned long *)dst;
0178         unsigned long *s1 = (unsigned long *)src1;
0179         unsigned long *s2 = (unsigned long *)src2;
0180         unsigned long l;
0181 
0182         while (size > 0) {
0183             l = get_unaligned(s1++) ^ get_unaligned(s2++);
0184             put_unaligned(l, d++);
0185             size -= sizeof(unsigned long);
0186         }
0187     } else {
0188         __crypto_xor(dst, src1, src2, size);
0189     }
0190 }
0191 
0192 static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
0193 {
0194     return PTR_ALIGN(crypto_tfm_ctx(tfm),
0195              crypto_tfm_alg_alignmask(tfm) + 1);
0196 }
0197 
0198 static inline struct crypto_instance *crypto_tfm_alg_instance(
0199     struct crypto_tfm *tfm)
0200 {
0201     return container_of(tfm->__crt_alg, struct crypto_instance, alg);
0202 }
0203 
0204 static inline void *crypto_instance_ctx(struct crypto_instance *inst)
0205 {
0206     return inst->__ctx;
0207 }
0208 
0209 static inline struct crypto_async_request *crypto_get_backlog(
0210     struct crypto_queue *queue)
0211 {
0212     return queue->backlog == &queue->list ? NULL :
0213            container_of(queue->backlog, struct crypto_async_request, list);
0214 }
0215 
0216 static inline u32 crypto_requires_off(struct crypto_attr_type *algt, u32 off)
0217 {
0218     return (algt->type ^ off) & algt->mask & off;
0219 }
0220 
0221 /*
0222  * When an algorithm uses another algorithm (e.g., if it's an instance of a
0223  * template), these are the flags that should always be set on the "outer"
0224  * algorithm if any "inner" algorithm has them set.
0225  */
0226 #define CRYPTO_ALG_INHERITED_FLAGS  \
0227     (CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK |  \
0228      CRYPTO_ALG_ALLOCATES_MEMORY)
0229 
0230 /*
0231  * Given the type and mask that specify the flags restrictions on a template
0232  * instance being created, return the mask that should be passed to
0233  * crypto_grab_*() (along with type=0) to honor any request the user made to
0234  * have any of the CRYPTO_ALG_INHERITED_FLAGS clear.
0235  */
0236 static inline u32 crypto_algt_inherited_mask(struct crypto_attr_type *algt)
0237 {
0238     return crypto_requires_off(algt, CRYPTO_ALG_INHERITED_FLAGS);
0239 }
0240 
0241 noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
0242 
0243 /**
0244  * crypto_memneq - Compare two areas of memory without leaking
0245  *         timing information.
0246  *
0247  * @a: One area of memory
0248  * @b: Another area of memory
0249  * @size: The size of the area.
0250  *
0251  * Returns 0 when data is equal, 1 otherwise.
0252  */
0253 static inline int crypto_memneq(const void *a, const void *b, size_t size)
0254 {
0255     return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
0256 }
0257 
0258 int crypto_register_notifier(struct notifier_block *nb);
0259 int crypto_unregister_notifier(struct notifier_block *nb);
0260 
0261 /* Crypto notification events. */
0262 enum {
0263     CRYPTO_MSG_ALG_REQUEST,
0264     CRYPTO_MSG_ALG_REGISTER,
0265     CRYPTO_MSG_ALG_LOADED,
0266 };
0267 
0268 #endif  /* _CRYPTO_ALGAPI_H */