62680775d1896f6ca9411e29c53b8cb31b831ff6
[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 #include <crypto/b128ops.h>
13
14 typedef void (*common_glue_func_t)(const void *ctx, u8 *dst, const u8 *src);
15 typedef void (*common_glue_cbc_func_t)(const void *ctx, u8 *dst, const u8 *src);
16 typedef void (*common_glue_ctr_func_t)(const void *ctx, u8 *dst, const u8 *src,
17                                        le128 *iv);
18
19 struct common_glue_func_entry {
20         unsigned int num_blocks; /* number of blocks that @fn will process */
21         union {
22                 common_glue_func_t ecb;
23                 common_glue_cbc_func_t cbc;
24                 common_glue_ctr_func_t ctr;
25         } fn_u;
26 };
27
28 struct common_glue_ctx {
29         unsigned int num_funcs;
30         int fpu_blocks_limit; /* -1 means fpu not needed at all */
31
32         /*
33          * First funcs entry must have largest num_blocks and last funcs entry
34          * must have num_blocks == 1!
35          */
36         struct common_glue_func_entry funcs[];
37 };
38
39 static inline bool glue_fpu_begin(unsigned int bsize, int fpu_blocks_limit,
40                                   struct skcipher_walk *walk,
41                                   bool fpu_enabled, unsigned int nbytes)
42 {
43         if (likely(fpu_blocks_limit < 0))
44                 return false;
45
46         if (fpu_enabled)
47                 return true;
48
49         /*
50          * Vector-registers are only used when chunk to be processed is large
51          * enough, so do not enable FPU until it is necessary.
52          */
53         if (nbytes < bsize * (unsigned int)fpu_blocks_limit)
54                 return false;
55
56         /* prevent sleeping if FPU is in use */
57         skcipher_walk_atomise(walk);
58
59         kernel_fpu_begin();
60         return true;
61 }
62
63 static inline void glue_fpu_end(bool fpu_enabled)
64 {
65         if (fpu_enabled)
66                 kernel_fpu_end();
67 }
68
69 static inline void le128_to_be128(be128 *dst, const le128 *src)
70 {
71         dst->a = cpu_to_be64(le64_to_cpu(src->a));
72         dst->b = cpu_to_be64(le64_to_cpu(src->b));
73 }
74
75 static inline void be128_to_le128(le128 *dst, const be128 *src)
76 {
77         dst->a = cpu_to_le64(be64_to_cpu(src->a));
78         dst->b = cpu_to_le64(be64_to_cpu(src->b));
79 }
80
81 static inline void le128_inc(le128 *i)
82 {
83         u64 a = le64_to_cpu(i->a);
84         u64 b = le64_to_cpu(i->b);
85
86         b++;
87         if (!b)
88                 a++;
89
90         i->a = cpu_to_le64(a);
91         i->b = cpu_to_le64(b);
92 }
93
94 extern int glue_ecb_req_128bit(const struct common_glue_ctx *gctx,
95                                struct skcipher_request *req);
96
97 extern int glue_cbc_encrypt_req_128bit(const common_glue_func_t fn,
98                                        struct skcipher_request *req);
99
100 extern int glue_cbc_decrypt_req_128bit(const struct common_glue_ctx *gctx,
101                                        struct skcipher_request *req);
102
103 extern int glue_ctr_req_128bit(const struct common_glue_ctx *gctx,
104                                struct skcipher_request *req);
105
106 #endif /* _CRYPTO_GLUE_HELPER_H */