]> git.sesse.net Git - bcachefs-tools-debian/blob - include/crypto/skcipher.h
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / include / crypto / skcipher.h
1 /*
2  * Symmetric key ciphers.
3  * 
4  * Copyright (c) 2007-2015 Herbert Xu <herbert@gondor.apana.org.au>
5  *
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) 
9  * any later version.
10  *
11  */
12
13 #ifndef _CRYPTO_SKCIPHER_H
14 #define _CRYPTO_SKCIPHER_H
15
16 #include <linux/crypto.h>
17
18 struct crypto_skcipher;
19 struct skcipher_request;
20
21 struct skcipher_alg {
22         struct crypto_alg base;
23 };
24
25 int crypto_register_skcipher(struct skcipher_alg *alg);
26
27 struct crypto_skcipher {
28         int (*setkey)(struct crypto_skcipher *tfm, const u8 *key,
29                       unsigned int keylen);
30         int (*encrypt)(struct skcipher_request *req);
31         int (*decrypt)(struct skcipher_request *req);
32
33         unsigned                ivsize;
34         unsigned                keysize;
35
36         struct crypto_tfm       base;
37 };
38
39 struct crypto_sync_skcipher {
40         struct crypto_skcipher base;
41 };
42
43 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
44                                               u32 type, u32 mask);
45
46 static inline struct crypto_sync_skcipher *
47 crypto_alloc_sync_skcipher(const char *alg_name, u32 type, u32 mask)
48 {
49         return (void *) crypto_alloc_skcipher(alg_name, type, mask);
50 }
51
52 static inline void crypto_free_skcipher(struct crypto_skcipher *tfm)
53 {
54         kfree(tfm);
55 }
56
57 static inline void crypto_free_sync_skcipher(struct crypto_sync_skcipher *tfm)
58 {
59         crypto_free_skcipher(&tfm->base);
60 }
61
62 struct skcipher_request {
63         unsigned                cryptlen;
64         u8                      *iv;
65
66         struct scatterlist      *src;
67         struct scatterlist      *dst;
68
69         struct crypto_tfm       *tfm;
70 };
71
72 #define MAX_SYNC_SKCIPHER_REQSIZE      384
73 #define SYNC_SKCIPHER_REQUEST_ON_STACK(name, tfm) \
74         char __##name##_desc[sizeof(struct skcipher_request) + \
75                              MAX_SYNC_SKCIPHER_REQSIZE + \
76                              (!(sizeof((struct crypto_sync_skcipher *)1 == \
77                                        (typeof(tfm))1))) \
78                             ] CRYPTO_MINALIGN_ATTR; \
79         struct skcipher_request *name = (void *)__##name##_desc
80
81 static inline int crypto_skcipher_setkey(struct crypto_skcipher *tfm,
82                                          const u8 *key, unsigned int keylen)
83 {
84         return tfm->setkey(tfm, key, keylen);
85 }
86
87 static inline struct crypto_skcipher *crypto_skcipher_reqtfm(
88         struct skcipher_request *req)
89 {
90         return container_of(req->tfm, struct crypto_skcipher, base);
91 }
92
93 static inline int crypto_skcipher_encrypt(struct skcipher_request *req)
94 {
95         return crypto_skcipher_reqtfm(req)->encrypt(req);
96 }
97
98 static inline int crypto_skcipher_decrypt(struct skcipher_request *req)
99 {
100         return crypto_skcipher_reqtfm(req)->decrypt(req);
101 }
102
103 static inline void skcipher_request_set_tfm(struct skcipher_request *req,
104                                             struct crypto_skcipher *tfm)
105 {
106         req->tfm = &tfm->base;
107 }
108
109 static inline void skcipher_request_set_sync_tfm(struct skcipher_request *req,
110                                             struct crypto_sync_skcipher *tfm)
111 {
112         skcipher_request_set_tfm(req, &tfm->base);
113 }
114
115 static inline void skcipher_request_set_crypt(
116         struct skcipher_request *req,
117         struct scatterlist *src, struct scatterlist *dst,
118         unsigned int cryptlen, void *iv)
119 {
120         req->src        = src;
121         req->dst        = dst;
122         req->cryptlen   = cryptlen;
123         req->iv         = iv;
124 }
125
126 #endif  /* _CRYPTO_SKCIPHER_H */