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

TOMOYO Linux Cross Reference
Linux/crypto/pcbc.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ 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.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /crypto/pcbc.c (Version linux-6.11.5) and /crypto/pcbc.c (Version linux-4.13.16)


  1 // SPDX-License-Identifier: GPL-2.0-or-later   << 
  2 /*                                                  1 /*
  3  * PCBC: Propagating Cipher Block Chaining mod      2  * PCBC: Propagating Cipher Block Chaining mode
  4  *                                                  3  *
  5  * Copyright (C) 2006 Red Hat, Inc. All Rights      4  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
  6  * Written by David Howells (dhowells@redhat.c      5  * Written by David Howells (dhowells@redhat.com)
  7  *                                                  6  *
  8  * Derived from cbc.c                               7  * Derived from cbc.c
  9  * - Copyright (c) 2006 Herbert Xu <herbert@go      8  * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
                                                   >>   9  *
                                                   >>  10  * This program is free software; you can redistribute it and/or modify it
                                                   >>  11  * under the terms of the GNU General Public License as published by the Free
                                                   >>  12  * Software Foundation; either version 2 of the License, or (at your option)
                                                   >>  13  * any later version.
                                                   >>  14  *
 10  */                                                15  */
 11                                                    16 
 12 #include <crypto/algapi.h>                     << 
 13 #include <crypto/internal/cipher.h>            << 
 14 #include <crypto/internal/skcipher.h>              17 #include <crypto/internal/skcipher.h>
 15 #include <linux/err.h>                             18 #include <linux/err.h>
 16 #include <linux/init.h>                            19 #include <linux/init.h>
 17 #include <linux/kernel.h>                          20 #include <linux/kernel.h>
 18 #include <linux/module.h>                          21 #include <linux/module.h>
                                                   >>  22 #include <linux/slab.h>
                                                   >>  23 #include <linux/compiler.h>
                                                   >>  24 
                                                   >>  25 struct crypto_pcbc_ctx {
                                                   >>  26         struct crypto_cipher *child;
                                                   >>  27 };
                                                   >>  28 
                                                   >>  29 static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
                                                   >>  30                               unsigned int keylen)
                                                   >>  31 {
                                                   >>  32         struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
                                                   >>  33         struct crypto_cipher *child = ctx->child;
                                                   >>  34         int err;
                                                   >>  35 
                                                   >>  36         crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
                                                   >>  37         crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
                                                   >>  38                                        CRYPTO_TFM_REQ_MASK);
                                                   >>  39         err = crypto_cipher_setkey(child, key, keylen);
                                                   >>  40         crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
                                                   >>  41                                           CRYPTO_TFM_RES_MASK);
                                                   >>  42         return err;
                                                   >>  43 }
 19                                                    44 
 20 static int crypto_pcbc_encrypt_segment(struct      45 static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
 21                                        struct      46                                        struct skcipher_walk *walk,
 22                                        struct      47                                        struct crypto_cipher *tfm)
 23 {                                                  48 {
 24         int bsize = crypto_cipher_blocksize(tf     49         int bsize = crypto_cipher_blocksize(tfm);
 25         unsigned int nbytes = walk->nbytes;        50         unsigned int nbytes = walk->nbytes;
 26         u8 *src = walk->src.virt.addr;             51         u8 *src = walk->src.virt.addr;
 27         u8 *dst = walk->dst.virt.addr;             52         u8 *dst = walk->dst.virt.addr;
 28         u8 * const iv = walk->iv;              !!  53         u8 *iv = walk->iv;
 29                                                    54 
 30         do {                                       55         do {
 31                 crypto_xor(iv, src, bsize);        56                 crypto_xor(iv, src, bsize);
 32                 crypto_cipher_encrypt_one(tfm,     57                 crypto_cipher_encrypt_one(tfm, dst, iv);
 33                 crypto_xor_cpy(iv, dst, src, b !!  58                 memcpy(iv, dst, bsize);
                                                   >>  59                 crypto_xor(iv, src, bsize);
 34                                                    60 
 35                 src += bsize;                      61                 src += bsize;
 36                 dst += bsize;                      62                 dst += bsize;
 37         } while ((nbytes -= bsize) >= bsize);      63         } while ((nbytes -= bsize) >= bsize);
 38                                                    64 
 39         return nbytes;                             65         return nbytes;
 40 }                                                  66 }
 41                                                    67 
 42 static int crypto_pcbc_encrypt_inplace(struct      68 static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
 43                                        struct      69                                        struct skcipher_walk *walk,
 44                                        struct      70                                        struct crypto_cipher *tfm)
 45 {                                                  71 {
 46         int bsize = crypto_cipher_blocksize(tf     72         int bsize = crypto_cipher_blocksize(tfm);
 47         unsigned int nbytes = walk->nbytes;        73         unsigned int nbytes = walk->nbytes;
 48         u8 *src = walk->src.virt.addr;             74         u8 *src = walk->src.virt.addr;
 49         u8 * const iv = walk->iv;              !!  75         u8 *iv = walk->iv;
 50         u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];       !!  76         u8 tmpbuf[bsize];
 51                                                    77 
 52         do {                                       78         do {
 53                 memcpy(tmpbuf, src, bsize);        79                 memcpy(tmpbuf, src, bsize);
 54                 crypto_xor(iv, src, bsize);        80                 crypto_xor(iv, src, bsize);
 55                 crypto_cipher_encrypt_one(tfm,     81                 crypto_cipher_encrypt_one(tfm, src, iv);
 56                 crypto_xor_cpy(iv, tmpbuf, src !!  82                 memcpy(iv, tmpbuf, bsize);
                                                   >>  83                 crypto_xor(iv, src, bsize);
 57                                                    84 
 58                 src += bsize;                      85                 src += bsize;
 59         } while ((nbytes -= bsize) >= bsize);      86         } while ((nbytes -= bsize) >= bsize);
 60                                                    87 
                                                   >>  88         memcpy(walk->iv, iv, bsize);
                                                   >>  89 
 61         return nbytes;                             90         return nbytes;
 62 }                                                  91 }
 63                                                    92 
 64 static int crypto_pcbc_encrypt(struct skcipher     93 static int crypto_pcbc_encrypt(struct skcipher_request *req)
 65 {                                                  94 {
 66         struct crypto_skcipher *tfm = crypto_s     95         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 67         struct crypto_cipher *cipher = skciphe !!  96         struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
                                                   >>  97         struct crypto_cipher *child = ctx->child;
 68         struct skcipher_walk walk;                 98         struct skcipher_walk walk;
 69         unsigned int nbytes;                       99         unsigned int nbytes;
 70         int err;                                  100         int err;
 71                                                   101 
 72         err = skcipher_walk_virt(&walk, req, f    102         err = skcipher_walk_virt(&walk, req, false);
 73                                                   103 
 74         while (walk.nbytes) {                  !! 104         while ((nbytes = walk.nbytes)) {
 75                 if (walk.src.virt.addr == walk    105                 if (walk.src.virt.addr == walk.dst.virt.addr)
 76                         nbytes = crypto_pcbc_e    106                         nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
 77                                                !! 107                                                              child);
 78                 else                              108                 else
 79                         nbytes = crypto_pcbc_e    109                         nbytes = crypto_pcbc_encrypt_segment(req, &walk,
 80                                                !! 110                                                              child);
 81                 err = skcipher_walk_done(&walk    111                 err = skcipher_walk_done(&walk, nbytes);
 82         }                                         112         }
 83                                                   113 
 84         return err;                               114         return err;
 85 }                                                 115 }
 86                                                   116 
 87 static int crypto_pcbc_decrypt_segment(struct     117 static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
 88                                        struct     118                                        struct skcipher_walk *walk,
 89                                        struct     119                                        struct crypto_cipher *tfm)
 90 {                                                 120 {
 91         int bsize = crypto_cipher_blocksize(tf    121         int bsize = crypto_cipher_blocksize(tfm);
 92         unsigned int nbytes = walk->nbytes;       122         unsigned int nbytes = walk->nbytes;
 93         u8 *src = walk->src.virt.addr;            123         u8 *src = walk->src.virt.addr;
 94         u8 *dst = walk->dst.virt.addr;            124         u8 *dst = walk->dst.virt.addr;
 95         u8 * const iv = walk->iv;              !! 125         u8 *iv = walk->iv;
 96                                                   126 
 97         do {                                      127         do {
 98                 crypto_cipher_decrypt_one(tfm,    128                 crypto_cipher_decrypt_one(tfm, dst, src);
 99                 crypto_xor(dst, iv, bsize);       129                 crypto_xor(dst, iv, bsize);
100                 crypto_xor_cpy(iv, dst, src, b !! 130                 memcpy(iv, src, bsize);
                                                   >> 131                 crypto_xor(iv, dst, bsize);
101                                                   132 
102                 src += bsize;                     133                 src += bsize;
103                 dst += bsize;                     134                 dst += bsize;
104         } while ((nbytes -= bsize) >= bsize);     135         } while ((nbytes -= bsize) >= bsize);
105                                                   136 
                                                   >> 137         memcpy(walk->iv, iv, bsize);
                                                   >> 138 
106         return nbytes;                            139         return nbytes;
107 }                                                 140 }
108                                                   141 
109 static int crypto_pcbc_decrypt_inplace(struct     142 static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
110                                        struct     143                                        struct skcipher_walk *walk,
111                                        struct     144                                        struct crypto_cipher *tfm)
112 {                                                 145 {
113         int bsize = crypto_cipher_blocksize(tf    146         int bsize = crypto_cipher_blocksize(tfm);
114         unsigned int nbytes = walk->nbytes;       147         unsigned int nbytes = walk->nbytes;
115         u8 *src = walk->src.virt.addr;            148         u8 *src = walk->src.virt.addr;
116         u8 * const iv = walk->iv;              !! 149         u8 *iv = walk->iv;
117         u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __alig !! 150         u8 tmpbuf[bsize] __aligned(__alignof__(u32));
118                                                   151 
119         do {                                      152         do {
120                 memcpy(tmpbuf, src, bsize);       153                 memcpy(tmpbuf, src, bsize);
121                 crypto_cipher_decrypt_one(tfm,    154                 crypto_cipher_decrypt_one(tfm, src, src);
122                 crypto_xor(src, iv, bsize);       155                 crypto_xor(src, iv, bsize);
123                 crypto_xor_cpy(iv, src, tmpbuf !! 156                 memcpy(iv, tmpbuf, bsize);
                                                   >> 157                 crypto_xor(iv, src, bsize);
124                                                   158 
125                 src += bsize;                     159                 src += bsize;
126         } while ((nbytes -= bsize) >= bsize);     160         } while ((nbytes -= bsize) >= bsize);
127                                                   161 
                                                   >> 162         memcpy(walk->iv, iv, bsize);
                                                   >> 163 
128         return nbytes;                            164         return nbytes;
129 }                                                 165 }
130                                                   166 
131 static int crypto_pcbc_decrypt(struct skcipher    167 static int crypto_pcbc_decrypt(struct skcipher_request *req)
132 {                                                 168 {
133         struct crypto_skcipher *tfm = crypto_s    169         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
134         struct crypto_cipher *cipher = skciphe !! 170         struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
                                                   >> 171         struct crypto_cipher *child = ctx->child;
135         struct skcipher_walk walk;                172         struct skcipher_walk walk;
136         unsigned int nbytes;                      173         unsigned int nbytes;
137         int err;                                  174         int err;
138                                                   175 
139         err = skcipher_walk_virt(&walk, req, f    176         err = skcipher_walk_virt(&walk, req, false);
140                                                   177 
141         while (walk.nbytes) {                  !! 178         while ((nbytes = walk.nbytes)) {
142                 if (walk.src.virt.addr == walk    179                 if (walk.src.virt.addr == walk.dst.virt.addr)
143                         nbytes = crypto_pcbc_d    180                         nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
144                                                !! 181                                                              child);
145                 else                              182                 else
146                         nbytes = crypto_pcbc_d    183                         nbytes = crypto_pcbc_decrypt_segment(req, &walk,
147                                                !! 184                                                              child);
148                 err = skcipher_walk_done(&walk    185                 err = skcipher_walk_done(&walk, nbytes);
149         }                                         186         }
150                                                   187 
151         return err;                               188         return err;
152 }                                                 189 }
153                                                   190 
                                                   >> 191 static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
                                                   >> 192 {
                                                   >> 193         struct skcipher_instance *inst = skcipher_alg_instance(tfm);
                                                   >> 194         struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
                                                   >> 195         struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
                                                   >> 196         struct crypto_cipher *cipher;
                                                   >> 197 
                                                   >> 198         cipher = crypto_spawn_cipher(spawn);
                                                   >> 199         if (IS_ERR(cipher))
                                                   >> 200                 return PTR_ERR(cipher);
                                                   >> 201 
                                                   >> 202         ctx->child = cipher;
                                                   >> 203         return 0;
                                                   >> 204 }
                                                   >> 205 
                                                   >> 206 static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
                                                   >> 207 {
                                                   >> 208         struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
                                                   >> 209 
                                                   >> 210         crypto_free_cipher(ctx->child);
                                                   >> 211 }
                                                   >> 212 
                                                   >> 213 static void crypto_pcbc_free(struct skcipher_instance *inst)
                                                   >> 214 {
                                                   >> 215         crypto_drop_skcipher(skcipher_instance_ctx(inst));
                                                   >> 216         kfree(inst);
                                                   >> 217 }
                                                   >> 218 
154 static int crypto_pcbc_create(struct crypto_te    219 static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
155 {                                                 220 {
156         struct skcipher_instance *inst;           221         struct skcipher_instance *inst;
                                                   >> 222         struct crypto_attr_type *algt;
                                                   >> 223         struct crypto_spawn *spawn;
                                                   >> 224         struct crypto_alg *alg;
157         int err;                                  225         int err;
158                                                   226 
159         inst = skcipher_alloc_instance_simple( !! 227         algt = crypto_get_attr_type(tb);
160         if (IS_ERR(inst))                      !! 228         if (IS_ERR(algt))
161                 return PTR_ERR(inst);          !! 229                 return PTR_ERR(algt);
                                                   >> 230 
                                                   >> 231         if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
                                                   >> 232             ~CRYPTO_ALG_INTERNAL)
                                                   >> 233                 return -EINVAL;
                                                   >> 234 
                                                   >> 235         inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
                                                   >> 236         if (!inst)
                                                   >> 237                 return -ENOMEM;
                                                   >> 238 
                                                   >> 239         alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
                                                   >> 240                                       (algt->type & CRYPTO_ALG_INTERNAL),
                                                   >> 241                                   CRYPTO_ALG_TYPE_MASK |
                                                   >> 242                                   (algt->mask & CRYPTO_ALG_INTERNAL));
                                                   >> 243         err = PTR_ERR(alg);
                                                   >> 244         if (IS_ERR(alg))
                                                   >> 245                 goto err_free_inst;
                                                   >> 246 
                                                   >> 247         spawn = skcipher_instance_ctx(inst);
                                                   >> 248         err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
                                                   >> 249                                 CRYPTO_ALG_TYPE_MASK);
                                                   >> 250         crypto_mod_put(alg);
                                                   >> 251         if (err)
                                                   >> 252                 goto err_free_inst;
                                                   >> 253 
                                                   >> 254         err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
                                                   >> 255         if (err)
                                                   >> 256                 goto err_drop_spawn;
                                                   >> 257 
                                                   >> 258         inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
                                                   >> 259         inst->alg.base.cra_priority = alg->cra_priority;
                                                   >> 260         inst->alg.base.cra_blocksize = alg->cra_blocksize;
                                                   >> 261         inst->alg.base.cra_alignmask = alg->cra_alignmask;
                                                   >> 262 
                                                   >> 263         inst->alg.ivsize = alg->cra_blocksize;
                                                   >> 264         inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
                                                   >> 265         inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
162                                                   266 
                                                   >> 267         inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
                                                   >> 268 
                                                   >> 269         inst->alg.init = crypto_pcbc_init_tfm;
                                                   >> 270         inst->alg.exit = crypto_pcbc_exit_tfm;
                                                   >> 271 
                                                   >> 272         inst->alg.setkey = crypto_pcbc_setkey;
163         inst->alg.encrypt = crypto_pcbc_encryp    273         inst->alg.encrypt = crypto_pcbc_encrypt;
164         inst->alg.decrypt = crypto_pcbc_decryp    274         inst->alg.decrypt = crypto_pcbc_decrypt;
165                                                   275 
                                                   >> 276         inst->free = crypto_pcbc_free;
                                                   >> 277 
166         err = skcipher_register_instance(tmpl,    278         err = skcipher_register_instance(tmpl, inst);
167         if (err)                                  279         if (err)
168                 inst->free(inst);              !! 280                 goto err_drop_spawn;
169                                                   281 
                                                   >> 282 out:
170         return err;                               283         return err;
                                                   >> 284 
                                                   >> 285 err_drop_spawn:
                                                   >> 286         crypto_drop_spawn(spawn);
                                                   >> 287 err_free_inst:
                                                   >> 288         kfree(inst);
                                                   >> 289         goto out;
171 }                                                 290 }
172                                                   291 
173 static struct crypto_template crypto_pcbc_tmpl    292 static struct crypto_template crypto_pcbc_tmpl = {
174         .name = "pcbc",                           293         .name = "pcbc",
175         .create = crypto_pcbc_create,             294         .create = crypto_pcbc_create,
176         .module = THIS_MODULE,                    295         .module = THIS_MODULE,
177 };                                                296 };
178                                                   297 
179 static int __init crypto_pcbc_module_init(void    298 static int __init crypto_pcbc_module_init(void)
180 {                                                 299 {
181         return crypto_register_template(&crypt    300         return crypto_register_template(&crypto_pcbc_tmpl);
182 }                                                 301 }
183                                                   302 
184 static void __exit crypto_pcbc_module_exit(voi    303 static void __exit crypto_pcbc_module_exit(void)
185 {                                                 304 {
186         crypto_unregister_template(&crypto_pcb    305         crypto_unregister_template(&crypto_pcbc_tmpl);
187 }                                                 306 }
188                                                   307 
189 subsys_initcall(crypto_pcbc_module_init);      !! 308 module_init(crypto_pcbc_module_init);
190 module_exit(crypto_pcbc_module_exit);             309 module_exit(crypto_pcbc_module_exit);
191                                                   310 
192 MODULE_LICENSE("GPL");                            311 MODULE_LICENSE("GPL");
193 MODULE_DESCRIPTION("PCBC block cipher mode of  !! 312 MODULE_DESCRIPTION("PCBC block cipher algorithm");
194 MODULE_ALIAS_CRYPTO("pcbc");                      313 MODULE_ALIAS_CRYPTO("pcbc");
195 MODULE_IMPORT_NS(CRYPTO_INTERNAL);             << 
196                                                   314 

~ [ 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