2 * Cryptographic API for algorithms (i.e., low-level API).
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
12 #ifndef _CRYPTO_ALGAPI_H
13 #define _CRYPTO_ALGAPI_H
15 #include <linux/crypto.h>
16 #include <linux/device.h>
17 #include <linux/list.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
22 struct crypto_instance;
29 unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
30 unsigned int (*extsize)(struct crypto_alg *alg);
31 int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
32 int (*init_tfm)(struct crypto_tfm *tfm);
33 void (*show)(struct seq_file *m, struct crypto_alg *alg);
34 struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask);
35 void (*free)(struct crypto_instance *inst);
38 unsigned int maskclear;
43 struct crypto_instance {
44 struct crypto_alg alg;
46 struct crypto_template *tmpl;
47 struct hlist_node list;
49 void *__ctx[] CRYPTO_MINALIGN_ATTR;
52 struct crypto_template {
53 struct list_head list;
54 struct hlist_head instances;
55 struct module *module;
57 struct crypto_instance *(*alloc)(struct rtattr **tb);
58 void (*free)(struct crypto_instance *inst);
59 int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
61 char name[CRYPTO_MAX_ALG_NAME];
65 struct scatterlist *sg;
69 struct blkcipher_walk {
82 struct scatter_walk in;
85 struct scatter_walk out;
94 unsigned int walk_blocksize;
95 unsigned int cipher_blocksize;
96 unsigned int alignmask;
99 extern const struct crypto_type crypto_blkcipher_type;
101 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
102 int crypto_check_attr_type(struct rtattr **tb, u32 type);
103 const char *crypto_attr_alg_name(struct rtattr *rta);
104 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
105 const struct crypto_type *frontend,
108 static inline struct crypto_alg *crypto_attr_alg(struct rtattr *rta,
111 return crypto_attr_alg2(rta, NULL, type, mask);
114 int crypto_attr_u32(struct rtattr *rta, u32 *num);
116 /* These functions require the input/output to be aligned as u32. */
117 void crypto_inc(u8 *a, unsigned int size);
118 void crypto_xor(u8 *dst, const u8 *src, unsigned int size);
120 int blkcipher_walk_done(struct blkcipher_desc *desc,
121 struct blkcipher_walk *walk, int err);
122 int blkcipher_walk_virt(struct blkcipher_desc *desc,
123 struct blkcipher_walk *walk);
124 int blkcipher_walk_phys(struct blkcipher_desc *desc,
125 struct blkcipher_walk *walk);
126 int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
127 struct blkcipher_walk *walk,
128 unsigned int blocksize);
129 int blkcipher_aead_walk_virt_block(struct blkcipher_desc *desc,
130 struct blkcipher_walk *walk,
131 struct crypto_aead *tfm,
132 unsigned int blocksize);
134 static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
136 return PTR_ALIGN(crypto_tfm_ctx(tfm),
137 crypto_tfm_alg_alignmask(tfm) + 1);
140 static inline struct crypto_instance *crypto_tfm_alg_instance(
141 struct crypto_tfm *tfm)
143 return container_of(tfm->__crt_alg, struct crypto_instance, alg);
146 static inline void *crypto_instance_ctx(struct crypto_instance *inst)
151 static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
153 return crypto_tfm_ctx(&tfm->base);
156 static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
158 return crypto_tfm_ctx_aligned(&tfm->base);
161 static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
163 return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
166 static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
167 struct scatterlist *dst,
168 struct scatterlist *src,
173 walk->total = nbytes;
176 static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
179 return crypto_attr_alg(tb[1], type, mask);
182 static inline int crypto_requires_sync(u32 type, u32 mask)
184 return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
187 noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
190 * crypto_memneq - Compare two areas of memory without leaking
191 * timing information.
193 * @a: One area of memory
194 * @b: Another area of memory
195 * @size: The size of the area.
197 * Returns 0 when data is equal, 1 otherwise.
199 static inline int crypto_memneq(const void *a, const void *b, size_t size)
201 return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
204 static inline void crypto_yield(u32 flags)
206 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
207 if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
212 #endif /* _CRYPTO_ALGAPI_H */