23e09efd2aa61dbbb245086ca90e6f7e89215ded
[linux-2.6-microblaze.git] / arch / x86 / include / asm / crypto / glue_helper.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Shared glue code for 128bit block ciphers
4  */
5
6 #ifndef _CRYPTO_GLUE_HELPER_H
7 #define _CRYPTO_GLUE_HELPER_H
8
9 #include <crypto/internal/skcipher.h>
10 #include <linux/kernel.h>
11 #include <asm/fpu/api.h>
12
13 typedef void (*common_glue_func_t)(const void *ctx, u8 *dst, const u8 *src);
14 typedef void (*common_glue_cbc_func_t)(const void *ctx, u8 *dst, const u8 *src);
15
16 struct common_glue_func_entry {
17         unsigned int num_blocks; /* number of blocks that @fn will process */
18         union {
19                 common_glue_func_t ecb;
20                 common_glue_cbc_func_t cbc;
21         } fn_u;
22 };
23
24 struct common_glue_ctx {
25         unsigned int num_funcs;
26         int fpu_blocks_limit; /* -1 means fpu not needed at all */
27
28         /*
29          * First funcs entry must have largest num_blocks and last funcs entry
30          * must have num_blocks == 1!
31          */
32         struct common_glue_func_entry funcs[];
33 };
34
35 static inline bool glue_fpu_begin(unsigned int bsize, int fpu_blocks_limit,
36                                   struct skcipher_walk *walk,
37                                   bool fpu_enabled, unsigned int nbytes)
38 {
39         if (likely(fpu_blocks_limit < 0))
40                 return false;
41
42         if (fpu_enabled)
43                 return true;
44
45         /*
46          * Vector-registers are only used when chunk to be processed is large
47          * enough, so do not enable FPU until it is necessary.
48          */
49         if (nbytes < bsize * (unsigned int)fpu_blocks_limit)
50                 return false;
51
52         /* prevent sleeping if FPU is in use */
53         skcipher_walk_atomise(walk);
54
55         kernel_fpu_begin();
56         return true;
57 }
58
59 static inline void glue_fpu_end(bool fpu_enabled)
60 {
61         if (fpu_enabled)
62                 kernel_fpu_end();
63 }
64
65 extern int glue_ecb_req_128bit(const struct common_glue_ctx *gctx,
66                                struct skcipher_request *req);
67
68 extern int glue_cbc_encrypt_req_128bit(const common_glue_func_t fn,
69                                        struct skcipher_request *req);
70
71 extern int glue_cbc_decrypt_req_128bit(const struct common_glue_ctx *gctx,
72                                        struct skcipher_request *req);
73
74 #endif /* _CRYPTO_GLUE_HELPER_H */