~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/lskcipher.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /crypto/lskcipher.c (Architecture i386) and /crypto/lskcipher.c (Architecture alpha)


  1 // SPDX-License-Identifier: GPL-2.0-or-later        1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*                                                  2 /*
  3  * Linear symmetric key cipher operations.          3  * Linear symmetric key cipher operations.
  4  *                                                  4  *
  5  * Generic encrypt/decrypt wrapper for ciphers      5  * Generic encrypt/decrypt wrapper for ciphers.
  6  *                                                  6  *
  7  * Copyright (c) 2023 Herbert Xu <herbert@gond      7  * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
  8  */                                                 8  */
  9                                                     9 
 10 #include <linux/cryptouser.h>                      10 #include <linux/cryptouser.h>
 11 #include <linux/err.h>                             11 #include <linux/err.h>
 12 #include <linux/export.h>                          12 #include <linux/export.h>
 13 #include <linux/kernel.h>                          13 #include <linux/kernel.h>
 14 #include <linux/seq_file.h>                        14 #include <linux/seq_file.h>
 15 #include <linux/slab.h>                            15 #include <linux/slab.h>
 16 #include <linux/string.h>                          16 #include <linux/string.h>
 17 #include <net/netlink.h>                           17 #include <net/netlink.h>
 18 #include "skcipher.h"                              18 #include "skcipher.h"
 19                                                    19 
 20 static inline struct crypto_lskcipher *__crypt     20 static inline struct crypto_lskcipher *__crypto_lskcipher_cast(
 21         struct crypto_tfm *tfm)                    21         struct crypto_tfm *tfm)
 22 {                                                  22 {
 23         return container_of(tfm, struct crypto     23         return container_of(tfm, struct crypto_lskcipher, base);
 24 }                                                  24 }
 25                                                    25 
 26 static inline struct lskcipher_alg *__crypto_l     26 static inline struct lskcipher_alg *__crypto_lskcipher_alg(
 27         struct crypto_alg *alg)                    27         struct crypto_alg *alg)
 28 {                                                  28 {
 29         return container_of(alg, struct lskcip     29         return container_of(alg, struct lskcipher_alg, co.base);
 30 }                                                  30 }
 31                                                    31 
 32 static int lskcipher_setkey_unaligned(struct c     32 static int lskcipher_setkey_unaligned(struct crypto_lskcipher *tfm,
 33                                       const u8     33                                       const u8 *key, unsigned int keylen)
 34 {                                                  34 {
 35         unsigned long alignmask = crypto_lskci     35         unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
 36         struct lskcipher_alg *cipher = crypto_     36         struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm);
 37         u8 *buffer, *alignbuffer;                  37         u8 *buffer, *alignbuffer;
 38         unsigned long absize;                      38         unsigned long absize;
 39         int ret;                                   39         int ret;
 40                                                    40 
 41         absize = keylen + alignmask;               41         absize = keylen + alignmask;
 42         buffer = kmalloc(absize, GFP_ATOMIC);      42         buffer = kmalloc(absize, GFP_ATOMIC);
 43         if (!buffer)                               43         if (!buffer)
 44                 return -ENOMEM;                    44                 return -ENOMEM;
 45                                                    45 
 46         alignbuffer = (u8 *)ALIGN((unsigned lo     46         alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
 47         memcpy(alignbuffer, key, keylen);          47         memcpy(alignbuffer, key, keylen);
 48         ret = cipher->setkey(tfm, alignbuffer,     48         ret = cipher->setkey(tfm, alignbuffer, keylen);
 49         kfree_sensitive(buffer);                   49         kfree_sensitive(buffer);
 50         return ret;                                50         return ret;
 51 }                                                  51 }
 52                                                    52 
 53 int crypto_lskcipher_setkey(struct crypto_lskc     53 int crypto_lskcipher_setkey(struct crypto_lskcipher *tfm, const u8 *key,
 54                             unsigned int keyle     54                             unsigned int keylen)
 55 {                                                  55 {
 56         unsigned long alignmask = crypto_lskci     56         unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
 57         struct lskcipher_alg *cipher = crypto_     57         struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm);
 58                                                    58 
 59         if (keylen < cipher->co.min_keysize ||     59         if (keylen < cipher->co.min_keysize || keylen > cipher->co.max_keysize)
 60                 return -EINVAL;                    60                 return -EINVAL;
 61                                                    61 
 62         if ((unsigned long)key & alignmask)        62         if ((unsigned long)key & alignmask)
 63                 return lskcipher_setkey_unalig     63                 return lskcipher_setkey_unaligned(tfm, key, keylen);
 64         else                                       64         else
 65                 return cipher->setkey(tfm, key     65                 return cipher->setkey(tfm, key, keylen);
 66 }                                                  66 }
 67 EXPORT_SYMBOL_GPL(crypto_lskcipher_setkey);        67 EXPORT_SYMBOL_GPL(crypto_lskcipher_setkey);
 68                                                    68 
 69 static int crypto_lskcipher_crypt_unaligned(       69 static int crypto_lskcipher_crypt_unaligned(
 70         struct crypto_lskcipher *tfm, const u8     70         struct crypto_lskcipher *tfm, const u8 *src, u8 *dst, unsigned len,
 71         u8 *iv, int (*crypt)(struct crypto_lsk     71         u8 *iv, int (*crypt)(struct crypto_lskcipher *tfm, const u8 *src,
 72                              u8 *dst, unsigned     72                              u8 *dst, unsigned len, u8 *iv, u32 flags))
 73 {                                                  73 {
 74         unsigned statesize = crypto_lskcipher_     74         unsigned statesize = crypto_lskcipher_statesize(tfm);
 75         unsigned ivsize = crypto_lskcipher_ivs     75         unsigned ivsize = crypto_lskcipher_ivsize(tfm);
 76         unsigned bs = crypto_lskcipher_blocksi     76         unsigned bs = crypto_lskcipher_blocksize(tfm);
 77         unsigned cs = crypto_lskcipher_chunksi     77         unsigned cs = crypto_lskcipher_chunksize(tfm);
 78         int err;                                   78         int err;
 79         u8 *tiv;                                   79         u8 *tiv;
 80         u8 *p;                                     80         u8 *p;
 81                                                    81 
 82         BUILD_BUG_ON(MAX_CIPHER_BLOCKSIZE > PA     82         BUILD_BUG_ON(MAX_CIPHER_BLOCKSIZE > PAGE_SIZE ||
 83                      MAX_CIPHER_ALIGNMASK >= P     83                      MAX_CIPHER_ALIGNMASK >= PAGE_SIZE);
 84                                                    84 
 85         tiv = kmalloc(PAGE_SIZE, GFP_ATOMIC);      85         tiv = kmalloc(PAGE_SIZE, GFP_ATOMIC);
 86         if (!tiv)                                  86         if (!tiv)
 87                 return -ENOMEM;                    87                 return -ENOMEM;
 88                                                    88 
 89         memcpy(tiv, iv, ivsize + statesize);       89         memcpy(tiv, iv, ivsize + statesize);
 90                                                    90 
 91         p = kmalloc(PAGE_SIZE, GFP_ATOMIC);        91         p = kmalloc(PAGE_SIZE, GFP_ATOMIC);
 92         err = -ENOMEM;                             92         err = -ENOMEM;
 93         if (!p)                                    93         if (!p)
 94                 goto out;                          94                 goto out;
 95                                                    95 
 96         while (len >= bs) {                        96         while (len >= bs) {
 97                 unsigned chunk = min((unsigned     97                 unsigned chunk = min((unsigned)PAGE_SIZE, len);
 98                 int err;                           98                 int err;
 99                                                    99 
100                 if (chunk > cs)                   100                 if (chunk > cs)
101                         chunk &= ~(cs - 1);       101                         chunk &= ~(cs - 1);
102                                                   102 
103                 memcpy(p, src, chunk);            103                 memcpy(p, src, chunk);
104                 err = crypt(tfm, p, p, chunk,     104                 err = crypt(tfm, p, p, chunk, tiv, CRYPTO_LSKCIPHER_FLAG_FINAL);
105                 if (err)                          105                 if (err)
106                         goto out;                 106                         goto out;
107                                                   107 
108                 memcpy(dst, p, chunk);            108                 memcpy(dst, p, chunk);
109                 src += chunk;                     109                 src += chunk;
110                 dst += chunk;                     110                 dst += chunk;
111                 len -= chunk;                     111                 len -= chunk;
112         }                                         112         }
113                                                   113 
114         err = len ? -EINVAL : 0;                  114         err = len ? -EINVAL : 0;
115                                                   115 
116 out:                                              116 out:
117         memcpy(iv, tiv, ivsize + statesize);      117         memcpy(iv, tiv, ivsize + statesize);
118         kfree_sensitive(p);                       118         kfree_sensitive(p);
119         kfree_sensitive(tiv);                     119         kfree_sensitive(tiv);
120         return err;                               120         return err;
121 }                                                 121 }
122                                                   122 
123 static int crypto_lskcipher_crypt(struct crypt    123 static int crypto_lskcipher_crypt(struct crypto_lskcipher *tfm, const u8 *src,
124                                   u8 *dst, uns    124                                   u8 *dst, unsigned len, u8 *iv,
125                                   int (*crypt)    125                                   int (*crypt)(struct crypto_lskcipher *tfm,
126                                                   126                                                const u8 *src, u8 *dst,
127                                                   127                                                unsigned len, u8 *iv,
128                                                   128                                                u32 flags))
129 {                                                 129 {
130         unsigned long alignmask = crypto_lskci    130         unsigned long alignmask = crypto_lskcipher_alignmask(tfm);
131                                                   131 
132         if (((unsigned long)src | (unsigned lo    132         if (((unsigned long)src | (unsigned long)dst | (unsigned long)iv) &
133             alignmask)                            133             alignmask)
134                 return crypto_lskcipher_crypt_    134                 return crypto_lskcipher_crypt_unaligned(tfm, src, dst, len, iv,
135                                                   135                                                         crypt);
136                                                   136 
137         return crypt(tfm, src, dst, len, iv, C    137         return crypt(tfm, src, dst, len, iv, CRYPTO_LSKCIPHER_FLAG_FINAL);
138 }                                                 138 }
139                                                   139 
140 int crypto_lskcipher_encrypt(struct crypto_lsk    140 int crypto_lskcipher_encrypt(struct crypto_lskcipher *tfm, const u8 *src,
141                              u8 *dst, unsigned    141                              u8 *dst, unsigned len, u8 *iv)
142 {                                                 142 {
143         struct lskcipher_alg *alg = crypto_lsk    143         struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
144                                                   144 
145         return crypto_lskcipher_crypt(tfm, src    145         return crypto_lskcipher_crypt(tfm, src, dst, len, iv, alg->encrypt);
146 }                                                 146 }
147 EXPORT_SYMBOL_GPL(crypto_lskcipher_encrypt);      147 EXPORT_SYMBOL_GPL(crypto_lskcipher_encrypt);
148                                                   148 
149 int crypto_lskcipher_decrypt(struct crypto_lsk    149 int crypto_lskcipher_decrypt(struct crypto_lskcipher *tfm, const u8 *src,
150                              u8 *dst, unsigned    150                              u8 *dst, unsigned len, u8 *iv)
151 {                                                 151 {
152         struct lskcipher_alg *alg = crypto_lsk    152         struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm);
153                                                   153 
154         return crypto_lskcipher_crypt(tfm, src    154         return crypto_lskcipher_crypt(tfm, src, dst, len, iv, alg->decrypt);
155 }                                                 155 }
156 EXPORT_SYMBOL_GPL(crypto_lskcipher_decrypt);      156 EXPORT_SYMBOL_GPL(crypto_lskcipher_decrypt);
157                                                   157 
158 static int crypto_lskcipher_crypt_sg(struct sk    158 static int crypto_lskcipher_crypt_sg(struct skcipher_request *req,
159                                      int (*cry    159                                      int (*crypt)(struct crypto_lskcipher *tfm,
160                                                   160                                                   const u8 *src, u8 *dst,
161                                                   161                                                   unsigned len, u8 *ivs,
162                                                   162                                                   u32 flags))
163 {                                                 163 {
164         struct crypto_skcipher *skcipher = cry    164         struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
165         struct crypto_lskcipher **ctx = crypto    165         struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher);
166         u8 *ivs = skcipher_request_ctx(req);      166         u8 *ivs = skcipher_request_ctx(req);
167         struct crypto_lskcipher *tfm = *ctx;      167         struct crypto_lskcipher *tfm = *ctx;
168         struct skcipher_walk walk;                168         struct skcipher_walk walk;
169         unsigned ivsize;                          169         unsigned ivsize;
170         u32 flags;                                170         u32 flags;
171         int err;                                  171         int err;
172                                                   172 
173         ivsize = crypto_lskcipher_ivsize(tfm);    173         ivsize = crypto_lskcipher_ivsize(tfm);
174         ivs = PTR_ALIGN(ivs, crypto_skcipher_a    174         ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(skcipher) + 1);
175         memcpy(ivs, req->iv, ivsize);             175         memcpy(ivs, req->iv, ivsize);
176                                                   176 
177         flags = req->base.flags & CRYPTO_TFM_R    177         flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP;
178                                                   178 
179         if (req->base.flags & CRYPTO_SKCIPHER_    179         if (req->base.flags & CRYPTO_SKCIPHER_REQ_CONT)
180                 flags |= CRYPTO_LSKCIPHER_FLAG    180                 flags |= CRYPTO_LSKCIPHER_FLAG_CONT;
181                                                   181 
182         if (!(req->base.flags & CRYPTO_SKCIPHE    182         if (!(req->base.flags & CRYPTO_SKCIPHER_REQ_NOTFINAL))
183                 flags |= CRYPTO_LSKCIPHER_FLAG    183                 flags |= CRYPTO_LSKCIPHER_FLAG_FINAL;
184                                                   184 
185         err = skcipher_walk_virt(&walk, req, f    185         err = skcipher_walk_virt(&walk, req, false);
186                                                   186 
187         while (walk.nbytes) {                     187         while (walk.nbytes) {
188                 err = crypt(tfm, walk.src.virt    188                 err = crypt(tfm, walk.src.virt.addr, walk.dst.virt.addr,
189                             walk.nbytes, ivs,     189                             walk.nbytes, ivs,
190                             flags & ~(walk.nby    190                             flags & ~(walk.nbytes == walk.total ?
191                             0 : CRYPTO_LSKCIPH    191                             0 : CRYPTO_LSKCIPHER_FLAG_FINAL));
192                 err = skcipher_walk_done(&walk    192                 err = skcipher_walk_done(&walk, err);
193                 flags |= CRYPTO_LSKCIPHER_FLAG    193                 flags |= CRYPTO_LSKCIPHER_FLAG_CONT;
194         }                                         194         }
195                                                   195 
196         memcpy(req->iv, ivs, ivsize);             196         memcpy(req->iv, ivs, ivsize);
197                                                   197 
198         return err;                               198         return err;
199 }                                                 199 }
200                                                   200 
201 int crypto_lskcipher_encrypt_sg(struct skciphe    201 int crypto_lskcipher_encrypt_sg(struct skcipher_request *req)
202 {                                                 202 {
203         struct crypto_skcipher *skcipher = cry    203         struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
204         struct crypto_lskcipher **ctx = crypto    204         struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher);
205         struct lskcipher_alg *alg = crypto_lsk    205         struct lskcipher_alg *alg = crypto_lskcipher_alg(*ctx);
206                                                   206 
207         return crypto_lskcipher_crypt_sg(req,     207         return crypto_lskcipher_crypt_sg(req, alg->encrypt);
208 }                                                 208 }
209                                                   209 
210 int crypto_lskcipher_decrypt_sg(struct skciphe    210 int crypto_lskcipher_decrypt_sg(struct skcipher_request *req)
211 {                                                 211 {
212         struct crypto_skcipher *skcipher = cry    212         struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
213         struct crypto_lskcipher **ctx = crypto    213         struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher);
214         struct lskcipher_alg *alg = crypto_lsk    214         struct lskcipher_alg *alg = crypto_lskcipher_alg(*ctx);
215                                                   215 
216         return crypto_lskcipher_crypt_sg(req,     216         return crypto_lskcipher_crypt_sg(req, alg->decrypt);
217 }                                                 217 }
218                                                   218 
219 static void crypto_lskcipher_exit_tfm(struct c    219 static void crypto_lskcipher_exit_tfm(struct crypto_tfm *tfm)
220 {                                                 220 {
221         struct crypto_lskcipher *skcipher = __    221         struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm);
222         struct lskcipher_alg *alg = crypto_lsk    222         struct lskcipher_alg *alg = crypto_lskcipher_alg(skcipher);
223                                                   223 
224         alg->exit(skcipher);                      224         alg->exit(skcipher);
225 }                                                 225 }
226                                                   226 
227 static int crypto_lskcipher_init_tfm(struct cr    227 static int crypto_lskcipher_init_tfm(struct crypto_tfm *tfm)
228 {                                                 228 {
229         struct crypto_lskcipher *skcipher = __    229         struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm);
230         struct lskcipher_alg *alg = crypto_lsk    230         struct lskcipher_alg *alg = crypto_lskcipher_alg(skcipher);
231                                                   231 
232         if (alg->exit)                            232         if (alg->exit)
233                 skcipher->base.exit = crypto_l    233                 skcipher->base.exit = crypto_lskcipher_exit_tfm;
234                                                   234 
235         if (alg->init)                            235         if (alg->init)
236                 return alg->init(skcipher);       236                 return alg->init(skcipher);
237                                                   237 
238         return 0;                                 238         return 0;
239 }                                                 239 }
240                                                   240 
241 static void crypto_lskcipher_free_instance(str    241 static void crypto_lskcipher_free_instance(struct crypto_instance *inst)
242 {                                                 242 {
243         struct lskcipher_instance *skcipher =     243         struct lskcipher_instance *skcipher =
244                 container_of(inst, struct lskc    244                 container_of(inst, struct lskcipher_instance, s.base);
245                                                   245 
246         skcipher->free(skcipher);                 246         skcipher->free(skcipher);
247 }                                                 247 }
248                                                   248 
249 static void __maybe_unused crypto_lskcipher_sh    249 static void __maybe_unused crypto_lskcipher_show(
250         struct seq_file *m, struct crypto_alg     250         struct seq_file *m, struct crypto_alg *alg)
251 {                                                 251 {
252         struct lskcipher_alg *skcipher = __cry    252         struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
253                                                   253 
254         seq_printf(m, "type         : lskciphe    254         seq_printf(m, "type         : lskcipher\n");
255         seq_printf(m, "blocksize    : %u\n", a    255         seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
256         seq_printf(m, "min keysize  : %u\n", s    256         seq_printf(m, "min keysize  : %u\n", skcipher->co.min_keysize);
257         seq_printf(m, "max keysize  : %u\n", s    257         seq_printf(m, "max keysize  : %u\n", skcipher->co.max_keysize);
258         seq_printf(m, "ivsize       : %u\n", s    258         seq_printf(m, "ivsize       : %u\n", skcipher->co.ivsize);
259         seq_printf(m, "chunksize    : %u\n", s    259         seq_printf(m, "chunksize    : %u\n", skcipher->co.chunksize);
260         seq_printf(m, "statesize    : %u\n", s    260         seq_printf(m, "statesize    : %u\n", skcipher->co.statesize);
261 }                                                 261 }
262                                                   262 
263 static int __maybe_unused crypto_lskcipher_rep    263 static int __maybe_unused crypto_lskcipher_report(
264         struct sk_buff *skb, struct crypto_alg    264         struct sk_buff *skb, struct crypto_alg *alg)
265 {                                                 265 {
266         struct lskcipher_alg *skcipher = __cry    266         struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
267         struct crypto_report_blkcipher rblkcip    267         struct crypto_report_blkcipher rblkcipher;
268                                                   268 
269         memset(&rblkcipher, 0, sizeof(rblkciph    269         memset(&rblkcipher, 0, sizeof(rblkcipher));
270                                                   270 
271         strscpy(rblkcipher.type, "lskcipher",     271         strscpy(rblkcipher.type, "lskcipher", sizeof(rblkcipher.type));
272         strscpy(rblkcipher.geniv, "<none>", si    272         strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
273                                                   273 
274         rblkcipher.blocksize = alg->cra_blocks    274         rblkcipher.blocksize = alg->cra_blocksize;
275         rblkcipher.min_keysize = skcipher->co.    275         rblkcipher.min_keysize = skcipher->co.min_keysize;
276         rblkcipher.max_keysize = skcipher->co.    276         rblkcipher.max_keysize = skcipher->co.max_keysize;
277         rblkcipher.ivsize = skcipher->co.ivsiz    277         rblkcipher.ivsize = skcipher->co.ivsize;
278                                                   278 
279         return nla_put(skb, CRYPTOCFGA_REPORT_    279         return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
280                        sizeof(rblkcipher), &rb    280                        sizeof(rblkcipher), &rblkcipher);
281 }                                                 281 }
282                                                   282 
283 static const struct crypto_type crypto_lskciph    283 static const struct crypto_type crypto_lskcipher_type = {
284         .extsize = crypto_alg_extsize,            284         .extsize = crypto_alg_extsize,
285         .init_tfm = crypto_lskcipher_init_tfm,    285         .init_tfm = crypto_lskcipher_init_tfm,
286         .free = crypto_lskcipher_free_instance    286         .free = crypto_lskcipher_free_instance,
287 #ifdef CONFIG_PROC_FS                             287 #ifdef CONFIG_PROC_FS
288         .show = crypto_lskcipher_show,            288         .show = crypto_lskcipher_show,
289 #endif                                            289 #endif
290 #if IS_ENABLED(CONFIG_CRYPTO_USER)                290 #if IS_ENABLED(CONFIG_CRYPTO_USER)
291         .report = crypto_lskcipher_report,        291         .report = crypto_lskcipher_report,
292 #endif                                            292 #endif
293         .maskclear = ~CRYPTO_ALG_TYPE_MASK,       293         .maskclear = ~CRYPTO_ALG_TYPE_MASK,
294         .maskset = CRYPTO_ALG_TYPE_MASK,          294         .maskset = CRYPTO_ALG_TYPE_MASK,
295         .type = CRYPTO_ALG_TYPE_LSKCIPHER,        295         .type = CRYPTO_ALG_TYPE_LSKCIPHER,
296         .tfmsize = offsetof(struct crypto_lskc    296         .tfmsize = offsetof(struct crypto_lskcipher, base),
297 };                                                297 };
298                                                   298 
299 static void crypto_lskcipher_exit_tfm_sg(struc    299 static void crypto_lskcipher_exit_tfm_sg(struct crypto_tfm *tfm)
300 {                                                 300 {
301         struct crypto_lskcipher **ctx = crypto    301         struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm);
302                                                   302 
303         crypto_free_lskcipher(*ctx);              303         crypto_free_lskcipher(*ctx);
304 }                                                 304 }
305                                                   305 
306 int crypto_init_lskcipher_ops_sg(struct crypto    306 int crypto_init_lskcipher_ops_sg(struct crypto_tfm *tfm)
307 {                                                 307 {
308         struct crypto_lskcipher **ctx = crypto    308         struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm);
309         struct crypto_alg *calg = tfm->__crt_a    309         struct crypto_alg *calg = tfm->__crt_alg;
310         struct crypto_lskcipher *skcipher;        310         struct crypto_lskcipher *skcipher;
311                                                   311 
312         if (!crypto_mod_get(calg))                312         if (!crypto_mod_get(calg))
313                 return -EAGAIN;                   313                 return -EAGAIN;
314                                                   314 
315         skcipher = crypto_create_tfm(calg, &cr    315         skcipher = crypto_create_tfm(calg, &crypto_lskcipher_type);
316         if (IS_ERR(skcipher)) {                   316         if (IS_ERR(skcipher)) {
317                 crypto_mod_put(calg);             317                 crypto_mod_put(calg);
318                 return PTR_ERR(skcipher);         318                 return PTR_ERR(skcipher);
319         }                                         319         }
320                                                   320 
321         *ctx = skcipher;                          321         *ctx = skcipher;
322         tfm->exit = crypto_lskcipher_exit_tfm_    322         tfm->exit = crypto_lskcipher_exit_tfm_sg;
323                                                   323 
324         return 0;                                 324         return 0;
325 }                                                 325 }
326                                                   326 
327 int crypto_grab_lskcipher(struct crypto_lskcip    327 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
328                           struct crypto_instan    328                           struct crypto_instance *inst,
329                           const char *name, u3    329                           const char *name, u32 type, u32 mask)
330 {                                                 330 {
331         spawn->base.frontend = &crypto_lskciph    331         spawn->base.frontend = &crypto_lskcipher_type;
332         return crypto_grab_spawn(&spawn->base,    332         return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
333 }                                                 333 }
334 EXPORT_SYMBOL_GPL(crypto_grab_lskcipher);         334 EXPORT_SYMBOL_GPL(crypto_grab_lskcipher);
335                                                   335 
336 struct crypto_lskcipher *crypto_alloc_lskciphe    336 struct crypto_lskcipher *crypto_alloc_lskcipher(const char *alg_name,
337                                                   337                                                 u32 type, u32 mask)
338 {                                                 338 {
339         return crypto_alloc_tfm(alg_name, &cry    339         return crypto_alloc_tfm(alg_name, &crypto_lskcipher_type, type, mask);
340 }                                                 340 }
341 EXPORT_SYMBOL_GPL(crypto_alloc_lskcipher);        341 EXPORT_SYMBOL_GPL(crypto_alloc_lskcipher);
342                                                   342 
343 static int lskcipher_prepare_alg(struct lskcip    343 static int lskcipher_prepare_alg(struct lskcipher_alg *alg)
344 {                                                 344 {
345         struct crypto_alg *base = &alg->co.bas    345         struct crypto_alg *base = &alg->co.base;
346         int err;                                  346         int err;
347                                                   347 
348         err = skcipher_prepare_alg_common(&alg    348         err = skcipher_prepare_alg_common(&alg->co);
349         if (err)                                  349         if (err)
350                 return err;                       350                 return err;
351                                                   351 
352         if (alg->co.chunksize & (alg->co.chunk    352         if (alg->co.chunksize & (alg->co.chunksize - 1))
353                 return -EINVAL;                   353                 return -EINVAL;
354                                                   354 
355         base->cra_type = &crypto_lskcipher_typ    355         base->cra_type = &crypto_lskcipher_type;
356         base->cra_flags |= CRYPTO_ALG_TYPE_LSK    356         base->cra_flags |= CRYPTO_ALG_TYPE_LSKCIPHER;
357                                                   357 
358         return 0;                                 358         return 0;
359 }                                                 359 }
360                                                   360 
361 int crypto_register_lskcipher(struct lskcipher    361 int crypto_register_lskcipher(struct lskcipher_alg *alg)
362 {                                                 362 {
363         struct crypto_alg *base = &alg->co.bas    363         struct crypto_alg *base = &alg->co.base;
364         int err;                                  364         int err;
365                                                   365 
366         err = lskcipher_prepare_alg(alg);         366         err = lskcipher_prepare_alg(alg);
367         if (err)                                  367         if (err)
368                 return err;                       368                 return err;
369                                                   369 
370         return crypto_register_alg(base);         370         return crypto_register_alg(base);
371 }                                                 371 }
372 EXPORT_SYMBOL_GPL(crypto_register_lskcipher);     372 EXPORT_SYMBOL_GPL(crypto_register_lskcipher);
373                                                   373 
374 void crypto_unregister_lskcipher(struct lskcip    374 void crypto_unregister_lskcipher(struct lskcipher_alg *alg)
375 {                                                 375 {
376         crypto_unregister_alg(&alg->co.base);     376         crypto_unregister_alg(&alg->co.base);
377 }                                                 377 }
378 EXPORT_SYMBOL_GPL(crypto_unregister_lskcipher)    378 EXPORT_SYMBOL_GPL(crypto_unregister_lskcipher);
379                                                   379 
380 int crypto_register_lskciphers(struct lskciphe    380 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count)
381 {                                                 381 {
382         int i, ret;                               382         int i, ret;
383                                                   383 
384         for (i = 0; i < count; i++) {             384         for (i = 0; i < count; i++) {
385                 ret = crypto_register_lskciphe    385                 ret = crypto_register_lskcipher(&algs[i]);
386                 if (ret)                          386                 if (ret)
387                         goto err;                 387                         goto err;
388         }                                         388         }
389                                                   389 
390         return 0;                                 390         return 0;
391                                                   391 
392 err:                                              392 err:
393         for (--i; i >= 0; --i)                    393         for (--i; i >= 0; --i)
394                 crypto_unregister_lskcipher(&a    394                 crypto_unregister_lskcipher(&algs[i]);
395                                                   395 
396         return ret;                               396         return ret;
397 }                                                 397 }
398 EXPORT_SYMBOL_GPL(crypto_register_lskciphers);    398 EXPORT_SYMBOL_GPL(crypto_register_lskciphers);
399                                                   399 
400 void crypto_unregister_lskciphers(struct lskci    400 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count)
401 {                                                 401 {
402         int i;                                    402         int i;
403                                                   403 
404         for (i = count - 1; i >= 0; --i)          404         for (i = count - 1; i >= 0; --i)
405                 crypto_unregister_lskcipher(&a    405                 crypto_unregister_lskcipher(&algs[i]);
406 }                                                 406 }
407 EXPORT_SYMBOL_GPL(crypto_unregister_lskciphers    407 EXPORT_SYMBOL_GPL(crypto_unregister_lskciphers);
408                                                   408 
409 int lskcipher_register_instance(struct crypto_    409 int lskcipher_register_instance(struct crypto_template *tmpl,
410                                 struct lskciph    410                                 struct lskcipher_instance *inst)
411 {                                                 411 {
412         int err;                                  412         int err;
413                                                   413 
414         if (WARN_ON(!inst->free))                 414         if (WARN_ON(!inst->free))
415                 return -EINVAL;                   415                 return -EINVAL;
416                                                   416 
417         err = lskcipher_prepare_alg(&inst->alg    417         err = lskcipher_prepare_alg(&inst->alg);
418         if (err)                                  418         if (err)
419                 return err;                       419                 return err;
420                                                   420 
421         return crypto_register_instance(tmpl,     421         return crypto_register_instance(tmpl, lskcipher_crypto_instance(inst));
422 }                                                 422 }
423 EXPORT_SYMBOL_GPL(lskcipher_register_instance)    423 EXPORT_SYMBOL_GPL(lskcipher_register_instance);
424                                                   424 
425 static int lskcipher_setkey_simple(struct cryp    425 static int lskcipher_setkey_simple(struct crypto_lskcipher *tfm, const u8 *key,
426                                    unsigned in    426                                    unsigned int keylen)
427 {                                                 427 {
428         struct crypto_lskcipher *cipher = lskc    428         struct crypto_lskcipher *cipher = lskcipher_cipher_simple(tfm);
429                                                   429 
430         crypto_lskcipher_clear_flags(cipher, C    430         crypto_lskcipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
431         crypto_lskcipher_set_flags(cipher, cry    431         crypto_lskcipher_set_flags(cipher, crypto_lskcipher_get_flags(tfm) &
432                                    CRYPTO_TFM_    432                                    CRYPTO_TFM_REQ_MASK);
433         return crypto_lskcipher_setkey(cipher,    433         return crypto_lskcipher_setkey(cipher, key, keylen);
434 }                                                 434 }
435                                                   435 
436 static int lskcipher_init_tfm_simple(struct cr    436 static int lskcipher_init_tfm_simple(struct crypto_lskcipher *tfm)
437 {                                                 437 {
438         struct lskcipher_instance *inst = lskc    438         struct lskcipher_instance *inst = lskcipher_alg_instance(tfm);
439         struct crypto_lskcipher **ctx = crypto    439         struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
440         struct crypto_lskcipher_spawn *spawn;     440         struct crypto_lskcipher_spawn *spawn;
441         struct crypto_lskcipher *cipher;          441         struct crypto_lskcipher *cipher;
442                                                   442 
443         spawn = lskcipher_instance_ctx(inst);     443         spawn = lskcipher_instance_ctx(inst);
444         cipher = crypto_spawn_lskcipher(spawn)    444         cipher = crypto_spawn_lskcipher(spawn);
445         if (IS_ERR(cipher))                       445         if (IS_ERR(cipher))
446                 return PTR_ERR(cipher);           446                 return PTR_ERR(cipher);
447                                                   447 
448         *ctx = cipher;                            448         *ctx = cipher;
449         return 0;                                 449         return 0;
450 }                                                 450 }
451                                                   451 
452 static void lskcipher_exit_tfm_simple(struct c    452 static void lskcipher_exit_tfm_simple(struct crypto_lskcipher *tfm)
453 {                                                 453 {
454         struct crypto_lskcipher **ctx = crypto    454         struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
455                                                   455 
456         crypto_free_lskcipher(*ctx);              456         crypto_free_lskcipher(*ctx);
457 }                                                 457 }
458                                                   458 
459 static void lskcipher_free_instance_simple(str    459 static void lskcipher_free_instance_simple(struct lskcipher_instance *inst)
460 {                                                 460 {
461         crypto_drop_lskcipher(lskcipher_instan    461         crypto_drop_lskcipher(lskcipher_instance_ctx(inst));
462         kfree(inst);                              462         kfree(inst);
463 }                                                 463 }
464                                                   464 
465 /**                                               465 /**
466  * lskcipher_alloc_instance_simple - allocate     466  * lskcipher_alloc_instance_simple - allocate instance of simple block cipher
467  *                                                467  *
468  * Allocate an lskcipher_instance for a simple    468  * Allocate an lskcipher_instance for a simple block cipher mode of operation,
469  * e.g. cbc or ecb.  The instance context will    469  * e.g. cbc or ecb.  The instance context will have just a single crypto_spawn,
470  * that for the underlying cipher.  The {min,m    470  * that for the underlying cipher.  The {min,max}_keysize, ivsize, blocksize,
471  * alignmask, and priority are set from the un    471  * alignmask, and priority are set from the underlying cipher but can be
472  * overridden if needed.  The tfm context defa    472  * overridden if needed.  The tfm context defaults to
473  * struct crypto_lskcipher *, and default ->se    473  * struct crypto_lskcipher *, and default ->setkey(), ->init(), and
474  * ->exit() methods are installed.                474  * ->exit() methods are installed.
475  *                                                475  *
476  * @tmpl: the template being instantiated         476  * @tmpl: the template being instantiated
477  * @tb: the template parameters                   477  * @tb: the template parameters
478  *                                                478  *
479  * Return: a pointer to the new instance, or a    479  * Return: a pointer to the new instance, or an ERR_PTR().  The caller still
480  *         needs to register the instance.        480  *         needs to register the instance.
481  */                                               481  */
482 struct lskcipher_instance *lskcipher_alloc_ins    482 struct lskcipher_instance *lskcipher_alloc_instance_simple(
483         struct crypto_template *tmpl, struct r    483         struct crypto_template *tmpl, struct rtattr **tb)
484 {                                                 484 {
485         u32 mask;                                 485         u32 mask;
486         struct lskcipher_instance *inst;          486         struct lskcipher_instance *inst;
487         struct crypto_lskcipher_spawn *spawn;     487         struct crypto_lskcipher_spawn *spawn;
488         char ecb_name[CRYPTO_MAX_ALG_NAME];       488         char ecb_name[CRYPTO_MAX_ALG_NAME];
489         struct lskcipher_alg *cipher_alg;         489         struct lskcipher_alg *cipher_alg;
490         const char *cipher_name;                  490         const char *cipher_name;
491         int err;                                  491         int err;
492                                                   492 
493         err = crypto_check_attr_type(tb, CRYPT    493         err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_LSKCIPHER, &mask);
494         if (err)                                  494         if (err)
495                 return ERR_PTR(err);              495                 return ERR_PTR(err);
496                                                   496 
497         cipher_name = crypto_attr_alg_name(tb[    497         cipher_name = crypto_attr_alg_name(tb[1]);
498         if (IS_ERR(cipher_name))                  498         if (IS_ERR(cipher_name))
499                 return ERR_CAST(cipher_name);     499                 return ERR_CAST(cipher_name);
500                                                   500 
501         inst = kzalloc(sizeof(*inst) + sizeof(    501         inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
502         if (!inst)                                502         if (!inst)
503                 return ERR_PTR(-ENOMEM);          503                 return ERR_PTR(-ENOMEM);
504                                                   504 
505         spawn = lskcipher_instance_ctx(inst);     505         spawn = lskcipher_instance_ctx(inst);
506         err = crypto_grab_lskcipher(spawn,        506         err = crypto_grab_lskcipher(spawn,
507                                     lskcipher_    507                                     lskcipher_crypto_instance(inst),
508                                     cipher_nam    508                                     cipher_name, 0, mask);
509                                                   509 
510         ecb_name[0] = 0;                          510         ecb_name[0] = 0;
511         if (err == -ENOENT && !!memcmp(tmpl->n    511         if (err == -ENOENT && !!memcmp(tmpl->name, "ecb", 4)) {
512                 err = -ENAMETOOLONG;              512                 err = -ENAMETOOLONG;
513                 if (snprintf(ecb_name, CRYPTO_    513                 if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
514                              cipher_name) >= C    514                              cipher_name) >= CRYPTO_MAX_ALG_NAME)
515                         goto err_free_inst;       515                         goto err_free_inst;
516                                                   516 
517                 err = crypto_grab_lskcipher(sp    517                 err = crypto_grab_lskcipher(spawn,
518                                             ls    518                                             lskcipher_crypto_instance(inst),
519                                             ec    519                                             ecb_name, 0, mask);
520         }                                         520         }
521                                                   521 
522         if (err)                                  522         if (err)
523                 goto err_free_inst;               523                 goto err_free_inst;
524                                                   524 
525         cipher_alg = crypto_lskcipher_spawn_al    525         cipher_alg = crypto_lskcipher_spawn_alg(spawn);
526                                                   526 
527         err = crypto_inst_setname(lskcipher_cr    527         err = crypto_inst_setname(lskcipher_crypto_instance(inst), tmpl->name,
528                                   &cipher_alg-    528                                   &cipher_alg->co.base);
529         if (err)                                  529         if (err)
530                 goto err_free_inst;               530                 goto err_free_inst;
531                                                   531 
532         if (ecb_name[0]) {                        532         if (ecb_name[0]) {
533                 int len;                          533                 int len;
534                                                   534 
535                 err = -EINVAL;                    535                 err = -EINVAL;
536                 len = strscpy(ecb_name, &ciphe    536                 len = strscpy(ecb_name, &cipher_alg->co.base.cra_name[4],
537                               sizeof(ecb_name)    537                               sizeof(ecb_name));
538                 if (len < 2)                      538                 if (len < 2)
539                         goto err_free_inst;       539                         goto err_free_inst;
540                                                   540 
541                 if (ecb_name[len - 1] != ')')     541                 if (ecb_name[len - 1] != ')')
542                         goto err_free_inst;       542                         goto err_free_inst;
543                                                   543 
544                 ecb_name[len - 1] = 0;            544                 ecb_name[len - 1] = 0;
545                                                   545 
546                 err = -ENAMETOOLONG;              546                 err = -ENAMETOOLONG;
547                 if (snprintf(inst->alg.co.base    547                 if (snprintf(inst->alg.co.base.cra_name, CRYPTO_MAX_ALG_NAME,
548                              "%s(%s)", tmpl->n    548                              "%s(%s)", tmpl->name, ecb_name) >=
549                     CRYPTO_MAX_ALG_NAME)          549                     CRYPTO_MAX_ALG_NAME)
550                         goto err_free_inst;       550                         goto err_free_inst;
551                                                   551 
552                 if (strcmp(ecb_name, cipher_na    552                 if (strcmp(ecb_name, cipher_name) &&
553                     snprintf(inst->alg.co.base    553                     snprintf(inst->alg.co.base.cra_driver_name,
554                              CRYPTO_MAX_ALG_NA    554                              CRYPTO_MAX_ALG_NAME,
555                              "%s(%s)", tmpl->n    555                              "%s(%s)", tmpl->name, cipher_name) >=
556                     CRYPTO_MAX_ALG_NAME)          556                     CRYPTO_MAX_ALG_NAME)
557                         goto err_free_inst;       557                         goto err_free_inst;
558         } else {                                  558         } else {
559                 /* Don't allow nesting. */        559                 /* Don't allow nesting. */
560                 err = -ELOOP;                     560                 err = -ELOOP;
561                 if ((cipher_alg->co.base.cra_f    561                 if ((cipher_alg->co.base.cra_flags & CRYPTO_ALG_INSTANCE))
562                         goto err_free_inst;       562                         goto err_free_inst;
563         }                                         563         }
564                                                   564 
565         err = -EINVAL;                            565         err = -EINVAL;
566         if (cipher_alg->co.ivsize)                566         if (cipher_alg->co.ivsize)
567                 goto err_free_inst;               567                 goto err_free_inst;
568                                                   568 
569         inst->free = lskcipher_free_instance_s    569         inst->free = lskcipher_free_instance_simple;
570                                                   570 
571         /* Default algorithm properties, can b    571         /* Default algorithm properties, can be overridden */
572         inst->alg.co.base.cra_blocksize = ciph    572         inst->alg.co.base.cra_blocksize = cipher_alg->co.base.cra_blocksize;
573         inst->alg.co.base.cra_alignmask = ciph    573         inst->alg.co.base.cra_alignmask = cipher_alg->co.base.cra_alignmask;
574         inst->alg.co.base.cra_priority = ciphe    574         inst->alg.co.base.cra_priority = cipher_alg->co.base.cra_priority;
575         inst->alg.co.min_keysize = cipher_alg-    575         inst->alg.co.min_keysize = cipher_alg->co.min_keysize;
576         inst->alg.co.max_keysize = cipher_alg-    576         inst->alg.co.max_keysize = cipher_alg->co.max_keysize;
577         inst->alg.co.ivsize = cipher_alg->co.b    577         inst->alg.co.ivsize = cipher_alg->co.base.cra_blocksize;
578         inst->alg.co.statesize = cipher_alg->c    578         inst->alg.co.statesize = cipher_alg->co.statesize;
579                                                   579 
580         /* Use struct crypto_lskcipher * by de    580         /* Use struct crypto_lskcipher * by default, can be overridden */
581         inst->alg.co.base.cra_ctxsize = sizeof    581         inst->alg.co.base.cra_ctxsize = sizeof(struct crypto_lskcipher *);
582         inst->alg.setkey = lskcipher_setkey_si    582         inst->alg.setkey = lskcipher_setkey_simple;
583         inst->alg.init = lskcipher_init_tfm_si    583         inst->alg.init = lskcipher_init_tfm_simple;
584         inst->alg.exit = lskcipher_exit_tfm_si    584         inst->alg.exit = lskcipher_exit_tfm_simple;
585                                                   585 
586         return inst;                              586         return inst;
587                                                   587 
588 err_free_inst:                                    588 err_free_inst:
589         lskcipher_free_instance_simple(inst);     589         lskcipher_free_instance_simple(inst);
590         return ERR_PTR(err);                      590         return ERR_PTR(err);
591 }                                                 591 }
592 EXPORT_SYMBOL_GPL(lskcipher_alloc_instance_sim    592 EXPORT_SYMBOL_GPL(lskcipher_alloc_instance_simple);
593                                                   593 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php