2 * Shared glue code for 128bit block ciphers
5 #ifndef _CRYPTO_GLUE_HELPER_H
6 #define _CRYPTO_GLUE_HELPER_H
8 #include <linux/kernel.h>
9 #include <linux/crypto.h>
11 #include <crypto/b128ops.h>
13 typedef void (*common_glue_func_t)(void *ctx, u8 *dst, const u8 *src);
14 typedef void (*common_glue_cbc_func_t)(void *ctx, u128 *dst, const u128 *src);
15 typedef void (*common_glue_ctr_func_t)(void *ctx, u128 *dst, const u128 *src,
18 #define GLUE_FUNC_CAST(fn) ((common_glue_func_t)(fn))
19 #define GLUE_CBC_FUNC_CAST(fn) ((common_glue_cbc_func_t)(fn))
20 #define GLUE_CTR_FUNC_CAST(fn) ((common_glue_ctr_func_t)(fn))
22 struct common_glue_func_entry {
23 unsigned int num_blocks; /* number of blocks that @fn will process */
25 common_glue_func_t ecb;
26 common_glue_cbc_func_t cbc;
27 common_glue_ctr_func_t ctr;
31 struct common_glue_ctx {
32 unsigned int num_funcs;
33 int fpu_blocks_limit; /* -1 means fpu not needed at all */
36 * First funcs entry must have largest num_blocks and last funcs entry
37 * must have num_blocks == 1!
39 struct common_glue_func_entry funcs[];
42 static inline bool glue_fpu_begin(unsigned int bsize, int fpu_blocks_limit,
43 struct blkcipher_desc *desc,
44 bool fpu_enabled, unsigned int nbytes)
46 if (likely(fpu_blocks_limit < 0))
53 * Vector-registers are only used when chunk to be processed is large
54 * enough, so do not enable FPU until it is necessary.
56 if (nbytes < bsize * (unsigned int)fpu_blocks_limit)
60 /* prevent sleeping if FPU is in use */
61 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
68 static inline void glue_fpu_end(bool fpu_enabled)
74 static inline void le128_to_be128(be128 *dst, const le128 *src)
76 dst->a = cpu_to_be64(le64_to_cpu(src->a));
77 dst->b = cpu_to_be64(le64_to_cpu(src->b));
80 static inline void be128_to_le128(le128 *dst, const be128 *src)
82 dst->a = cpu_to_le64(be64_to_cpu(src->a));
83 dst->b = cpu_to_le64(be64_to_cpu(src->b));
86 static inline void le128_inc(le128 *i)
88 u64 a = le64_to_cpu(i->a);
89 u64 b = le64_to_cpu(i->b);
95 i->a = cpu_to_le64(a);
96 i->b = cpu_to_le64(b);
99 extern int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
100 struct blkcipher_desc *desc,
101 struct scatterlist *dst,
102 struct scatterlist *src, unsigned int nbytes);
104 extern int glue_cbc_encrypt_128bit(const common_glue_func_t fn,
105 struct blkcipher_desc *desc,
106 struct scatterlist *dst,
107 struct scatterlist *src,
108 unsigned int nbytes);
110 extern int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
111 struct blkcipher_desc *desc,
112 struct scatterlist *dst,
113 struct scatterlist *src,
114 unsigned int nbytes);
116 extern int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
117 struct blkcipher_desc *desc,
118 struct scatterlist *dst,
119 struct scatterlist *src, unsigned int nbytes);
121 #endif /* _CRYPTO_GLUE_HELPER_H */