~ [ 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.18.20)


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

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