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

TOMOYO Linux Cross Reference
Linux/crypto/keywrap.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/keywrap.c (Version linux-6.11.5) and /crypto/keywrap.c (Version linux-4.11.12)


  1 /*                                                  1 /*
  2  * Key Wrapping: RFC3394 / NIST SP800-38F           2  * Key Wrapping: RFC3394 / NIST SP800-38F
  3  *                                                  3  *
  4  * Copyright (C) 2015, Stephan Mueller <smuell      4  * Copyright (C) 2015, Stephan Mueller <smueller@chronox.de>
  5  *                                                  5  *
  6  * Redistribution and use in source and binary      6  * Redistribution and use in source and binary forms, with or without
  7  * modification, are permitted provided that t      7  * modification, are permitted provided that the following conditions
  8  * are met:                                         8  * are met:
  9  * 1. Redistributions of source code must reta      9  * 1. Redistributions of source code must retain the above copyright
 10  *    notice, and the entire permission notice     10  *    notice, and the entire permission notice in its entirety,
 11  *    including the disclaimer of warranties.      11  *    including the disclaimer of warranties.
 12  * 2. Redistributions in binary form must repr     12  * 2. Redistributions in binary form must reproduce the above copyright
 13  *    notice, this list of conditions and the      13  *    notice, this list of conditions and the following disclaimer in the
 14  *    documentation and/or other materials pro     14  *    documentation and/or other materials provided with the distribution.
 15  * 3. The name of the author may not be used t     15  * 3. The name of the author may not be used to endorse or promote
 16  *    products derived from this software with     16  *    products derived from this software without specific prior
 17  *    written permission.                          17  *    written permission.
 18  *                                                 18  *
 19  * ALTERNATIVELY, this product may be distribu     19  * ALTERNATIVELY, this product may be distributed under the terms of
 20  * the GNU General Public License, in which ca     20  * the GNU General Public License, in which case the provisions of the GPL2
 21  * are required INSTEAD OF the above restricti     21  * are required INSTEAD OF the above restrictions.  (This clause is
 22  * necessary due to a potential bad interactio     22  * necessary due to a potential bad interaction between the GPL and
 23  * the restrictions contained in a BSD-style c     23  * the restrictions contained in a BSD-style copyright.)
 24  *                                                 24  *
 25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY     25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO,      26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 27  * OF MERCHANTABILITY AND FITNESS FOR A PARTIC     27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
 28  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT S     28  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
 29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL     29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT L     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 31  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF US     31  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 32  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND O     32  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIAB     33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING     34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 35  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED O     35  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
 36  * DAMAGE.                                         36  * DAMAGE.
 37  */                                                37  */
 38                                                    38 
 39 /*                                                 39 /*
 40  * Note for using key wrapping:                    40  * Note for using key wrapping:
 41  *                                                 41  *
 42  *      * The result of the encryption operati     42  *      * The result of the encryption operation is the ciphertext starting
 43  *        with the 2nd semiblock. The first se     43  *        with the 2nd semiblock. The first semiblock is provided as the IV.
 44  *        The IV used to start the encryption      44  *        The IV used to start the encryption operation is the default IV.
 45  *                                                 45  *
 46  *      * The input for the decryption is the      46  *      * The input for the decryption is the first semiblock handed in as an
 47  *        IV. The ciphertext is the data start     47  *        IV. The ciphertext is the data starting with the 2nd semiblock. The
 48  *        return code of the decryption operat     48  *        return code of the decryption operation will be EBADMSG in case an
 49  *        integrity error occurs.                  49  *        integrity error occurs.
 50  *                                                 50  *
 51  * To obtain the full result of an encryption      51  * To obtain the full result of an encryption as expected by SP800-38F, the
 52  * caller must allocate a buffer of plaintext      52  * caller must allocate a buffer of plaintext + 8 bytes:
 53  *                                                 53  *
 54  *      unsigned int datalen = ptlen + crypto_     54  *      unsigned int datalen = ptlen + crypto_skcipher_ivsize(tfm);
 55  *      u8 data[datalen];                          55  *      u8 data[datalen];
 56  *      u8 *iv = data;                             56  *      u8 *iv = data;
 57  *      u8 *pt = data + crypto_skcipher_ivsize     57  *      u8 *pt = data + crypto_skcipher_ivsize(tfm);
 58  *              <ensure that pt contains the p     58  *              <ensure that pt contains the plaintext of size ptlen>
 59  *      sg_init_one(&sg, pt, ptlen);           !!  59  *      sg_init_one(&sg, ptdata, ptlen);
 60  *      skcipher_request_set_crypt(req, &sg, &     60  *      skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv);
 61  *                                                 61  *
 62  *      ==> After encryption, data now contain     62  *      ==> After encryption, data now contains full KW result as per SP800-38F.
 63  *                                                 63  *
 64  * In case of decryption, ciphertext now alrea     64  * In case of decryption, ciphertext now already has the expected length
 65  * and must be segmented appropriately:            65  * and must be segmented appropriately:
 66  *                                                 66  *
 67  *      unsigned int datalen = CTLEN;              67  *      unsigned int datalen = CTLEN;
 68  *      u8 data[datalen];                          68  *      u8 data[datalen];
 69  *              <ensure that data contains ful     69  *              <ensure that data contains full ciphertext>
 70  *      u8 *iv = data;                             70  *      u8 *iv = data;
 71  *      u8 *ct = data + crypto_skcipher_ivsize     71  *      u8 *ct = data + crypto_skcipher_ivsize(tfm);
 72  *      unsigned int ctlen = datalen - crypto_     72  *      unsigned int ctlen = datalen - crypto_skcipher_ivsize(tfm);
 73  *      sg_init_one(&sg, ct, ctlen);           !!  73  *      sg_init_one(&sg, ctdata, ctlen);
 74  *      skcipher_request_set_crypt(req, &sg, & !!  74  *      skcipher_request_set_crypt(req, &sg, &sg, ptlen, iv);
 75  *                                                 75  *
 76  *      ==> After decryption (which hopefully      76  *      ==> After decryption (which hopefully does not return EBADMSG), the ct
 77  *      pointer now points to the plaintext of     77  *      pointer now points to the plaintext of size ctlen.
 78  *                                                 78  *
 79  * Note 2: KWP is not implemented as this woul     79  * Note 2: KWP is not implemented as this would defy in-place operation.
 80  *         If somebody wants to wrap non-align     80  *         If somebody wants to wrap non-aligned data, he should simply pad
 81  *         the input with zeros to fill it up      81  *         the input with zeros to fill it up to the 8 byte boundary.
 82  */                                                82  */
 83                                                    83 
 84 #include <linux/module.h>                          84 #include <linux/module.h>
 85 #include <linux/crypto.h>                          85 #include <linux/crypto.h>
 86 #include <linux/scatterlist.h>                     86 #include <linux/scatterlist.h>
 87 #include <crypto/scatterwalk.h>                    87 #include <crypto/scatterwalk.h>
 88 #include <crypto/internal/cipher.h>            << 
 89 #include <crypto/internal/skcipher.h>              88 #include <crypto/internal/skcipher.h>
 90                                                    89 
                                                   >>  90 struct crypto_kw_ctx {
                                                   >>  91         struct crypto_cipher *child;
                                                   >>  92 };
                                                   >>  93 
 91 struct crypto_kw_block {                           94 struct crypto_kw_block {
 92 #define SEMIBSIZE 8                                95 #define SEMIBSIZE 8
 93         __be64 A;                              !!  96         u8 A[SEMIBSIZE];
 94         __be64 R;                              !!  97         u8 R[SEMIBSIZE];
 95 };                                                 98 };
 96                                                    99 
                                                   >> 100 /* convert 64 bit integer into its string representation */
                                                   >> 101 static inline void crypto_kw_cpu_to_be64(u64 val, u8 *buf)
                                                   >> 102 {
                                                   >> 103         __be64 *a = (__be64 *)buf;
                                                   >> 104 
                                                   >> 105         *a = cpu_to_be64(val);
                                                   >> 106 }
                                                   >> 107 
 97 /*                                                108 /*
 98  * Fast forward the SGL to the "end" length mi    109  * Fast forward the SGL to the "end" length minus SEMIBSIZE.
 99  * The start in the SGL defined by the fast-fo    110  * The start in the SGL defined by the fast-forward is returned with
100  * the walk variable                              111  * the walk variable
101  */                                               112  */
102 static void crypto_kw_scatterlist_ff(struct sc    113 static void crypto_kw_scatterlist_ff(struct scatter_walk *walk,
103                                      struct sc    114                                      struct scatterlist *sg,
104                                      unsigned     115                                      unsigned int end)
105 {                                                 116 {
106         unsigned int skip = 0;                    117         unsigned int skip = 0;
107                                                   118 
108         /* The caller should only operate on f    119         /* The caller should only operate on full SEMIBLOCKs. */
109         BUG_ON(end < SEMIBSIZE);                  120         BUG_ON(end < SEMIBSIZE);
110                                                   121 
111         skip = end - SEMIBSIZE;                   122         skip = end - SEMIBSIZE;
112         while (sg) {                              123         while (sg) {
113                 if (sg->length > skip) {          124                 if (sg->length > skip) {
114                         scatterwalk_start(walk    125                         scatterwalk_start(walk, sg);
115                         scatterwalk_advance(wa    126                         scatterwalk_advance(walk, skip);
116                         break;                    127                         break;
117                 }                              !! 128                 } else
                                                   >> 129                         skip -= sg->length;
118                                                   130 
119                 skip -= sg->length;            << 
120                 sg = sg_next(sg);                 131                 sg = sg_next(sg);
121         }                                         132         }
122 }                                                 133 }
123                                                   134 
124 static int crypto_kw_decrypt(struct skcipher_r !! 135 static int crypto_kw_decrypt(struct blkcipher_desc *desc,
                                                   >> 136                              struct scatterlist *dst, struct scatterlist *src,
                                                   >> 137                              unsigned int nbytes)
125 {                                                 138 {
126         struct crypto_skcipher *tfm = crypto_s !! 139         struct crypto_blkcipher *tfm = desc->tfm;
127         struct crypto_cipher *cipher = skciphe !! 140         struct crypto_kw_ctx *ctx = crypto_blkcipher_ctx(tfm);
128         struct crypto_kw_block block;          !! 141         struct crypto_cipher *child = ctx->child;
129         struct scatterlist *src, *dst;         !! 142 
130         u64 t = 6 * ((req->cryptlen) >> 3);    !! 143         unsigned long alignmask = max_t(unsigned long, SEMIBSIZE,
                                                   >> 144                                         crypto_cipher_alignmask(child));
131         unsigned int i;                           145         unsigned int i;
                                                   >> 146 
                                                   >> 147         u8 blockbuf[sizeof(struct crypto_kw_block) + alignmask];
                                                   >> 148         struct crypto_kw_block *block = (struct crypto_kw_block *)
                                                   >> 149                                         PTR_ALIGN(blockbuf + 0, alignmask + 1);
                                                   >> 150 
                                                   >> 151         u64 t = 6 * ((nbytes) >> 3);
                                                   >> 152         struct scatterlist *lsrc, *ldst;
132         int ret = 0;                              153         int ret = 0;
133                                                   154 
134         /*                                        155         /*
135          * Require at least 2 semiblocks (note    156          * Require at least 2 semiblocks (note, the 3rd semiblock that is
136          * required by SP800-38F is the IV.       157          * required by SP800-38F is the IV.
137          */                                       158          */
138         if (req->cryptlen < (2 * SEMIBSIZE) || !! 159         if (nbytes < (2 * SEMIBSIZE) || nbytes % SEMIBSIZE)
139                 return -EINVAL;                   160                 return -EINVAL;
140                                                   161 
141         /* Place the IV into block A */           162         /* Place the IV into block A */
142         memcpy(&block.A, req->iv, SEMIBSIZE);  !! 163         memcpy(block->A, desc->info, SEMIBSIZE);
143                                                   164 
144         /*                                        165         /*
145          * src scatterlist is read-only. dst s    166          * src scatterlist is read-only. dst scatterlist is r/w. During the
146          * first loop, src points to req->src  !! 167          * first loop, lsrc points to src and ldst to dst. For any
147          * subsequent round, the code operates !! 168          * subsequent round, the code operates on dst only.
148          */                                       169          */
149         src = req->src;                        !! 170         lsrc = src;
150         dst = req->dst;                        !! 171         ldst = dst;
151                                                   172 
152         for (i = 0; i < 6; i++) {                 173         for (i = 0; i < 6; i++) {
                                                   >> 174                 u8 tbe_buffer[SEMIBSIZE + alignmask];
                                                   >> 175                 /* alignment for the crypto_xor and the _to_be64 operation */
                                                   >> 176                 u8 *tbe = PTR_ALIGN(tbe_buffer + 0, alignmask + 1);
                                                   >> 177                 unsigned int tmp_nbytes = nbytes;
153                 struct scatter_walk src_walk,     178                 struct scatter_walk src_walk, dst_walk;
154                 unsigned int nbytes = req->cry << 
155                                                   179 
156                 while (nbytes) {               !! 180                 while (tmp_nbytes) {
157                         /* move pointer by nby !! 181                         /* move pointer by tmp_nbytes in the SGL */
158                         crypto_kw_scatterlist_ !! 182                         crypto_kw_scatterlist_ff(&src_walk, lsrc, tmp_nbytes);
159                         /* get the source bloc    183                         /* get the source block */
160                         scatterwalk_copychunks !! 184                         scatterwalk_copychunks(block->R, &src_walk, SEMIBSIZE,
161                                                   185                                                false);
162                                                   186 
                                                   >> 187                         /* perform KW operation: get counter as byte string */
                                                   >> 188                         crypto_kw_cpu_to_be64(t, tbe);
163                         /* perform KW operatio    189                         /* perform KW operation: modify IV with counter */
164                         block.A ^= cpu_to_be64 !! 190                         crypto_xor(block->A, tbe, SEMIBSIZE);
165                         t--;                      191                         t--;
166                         /* perform KW operatio    192                         /* perform KW operation: decrypt block */
167                         crypto_cipher_decrypt_ !! 193                         crypto_cipher_decrypt_one(child, (u8*)block,
168                                                !! 194                                                   (u8*)block);
169                                                   195 
170                         /* move pointer by nby !! 196                         /* move pointer by tmp_nbytes in the SGL */
171                         crypto_kw_scatterlist_ !! 197                         crypto_kw_scatterlist_ff(&dst_walk, ldst, tmp_nbytes);
172                         /* Copy block->R into     198                         /* Copy block->R into place */
173                         scatterwalk_copychunks !! 199                         scatterwalk_copychunks(block->R, &dst_walk, SEMIBSIZE,
174                                                   200                                                true);
175                                                   201 
176                         nbytes -= SEMIBSIZE;   !! 202                         tmp_nbytes -= SEMIBSIZE;
177                 }                                 203                 }
178                                                   204 
179                 /* we now start to operate on     205                 /* we now start to operate on the dst SGL only */
180                 src = req->dst;                !! 206                 lsrc = dst;
181                 dst = req->dst;                !! 207                 ldst = dst;
182         }                                         208         }
183                                                   209 
184         /* Perform authentication check */        210         /* Perform authentication check */
185         if (block.A != cpu_to_be64(0xa6a6a6a6a !! 211         if (crypto_memneq("\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6", block->A,
                                                   >> 212                           SEMIBSIZE))
186                 ret = -EBADMSG;                   213                 ret = -EBADMSG;
187                                                   214 
188         memzero_explicit(&block, sizeof(struct !! 215         memzero_explicit(block, sizeof(struct crypto_kw_block));
189                                                   216 
190         return ret;                               217         return ret;
191 }                                                 218 }
192                                                   219 
193 static int crypto_kw_encrypt(struct skcipher_r !! 220 static int crypto_kw_encrypt(struct blkcipher_desc *desc,
                                                   >> 221                              struct scatterlist *dst, struct scatterlist *src,
                                                   >> 222                              unsigned int nbytes)
194 {                                                 223 {
195         struct crypto_skcipher *tfm = crypto_s !! 224         struct crypto_blkcipher *tfm = desc->tfm;
196         struct crypto_cipher *cipher = skciphe !! 225         struct crypto_kw_ctx *ctx = crypto_blkcipher_ctx(tfm);
197         struct crypto_kw_block block;          !! 226         struct crypto_cipher *child = ctx->child;
198         struct scatterlist *src, *dst;         !! 227 
199         u64 t = 1;                             !! 228         unsigned long alignmask = max_t(unsigned long, SEMIBSIZE,
                                                   >> 229                                         crypto_cipher_alignmask(child));
200         unsigned int i;                           230         unsigned int i;
201                                                   231 
                                                   >> 232         u8 blockbuf[sizeof(struct crypto_kw_block) + alignmask];
                                                   >> 233         struct crypto_kw_block *block = (struct crypto_kw_block *)
                                                   >> 234                                         PTR_ALIGN(blockbuf + 0, alignmask + 1);
                                                   >> 235 
                                                   >> 236         u64 t = 1;
                                                   >> 237         struct scatterlist *lsrc, *ldst;
                                                   >> 238 
202         /*                                        239         /*
203          * Require at least 2 semiblocks (note    240          * Require at least 2 semiblocks (note, the 3rd semiblock that is
204          * required by SP800-38F is the IV tha    241          * required by SP800-38F is the IV that occupies the first semiblock.
205          * This means that the dst memory must    242          * This means that the dst memory must be one semiblock larger than src.
206          * Also ensure that the given data is     243          * Also ensure that the given data is aligned to semiblock.
207          */                                       244          */
208         if (req->cryptlen < (2 * SEMIBSIZE) || !! 245         if (nbytes < (2 * SEMIBSIZE) || nbytes % SEMIBSIZE)
209                 return -EINVAL;                   246                 return -EINVAL;
210                                                   247 
211         /*                                        248         /*
212          * Place the predefined IV into block     249          * Place the predefined IV into block A -- for encrypt, the caller
213          * does not need to provide an IV, but    250          * does not need to provide an IV, but he needs to fetch the final IV.
214          */                                       251          */
215         block.A = cpu_to_be64(0xa6a6a6a6a6a6a6 !! 252         memcpy(block->A, "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6", SEMIBSIZE);
216                                                   253 
217         /*                                        254         /*
218          * src scatterlist is read-only. dst s    255          * src scatterlist is read-only. dst scatterlist is r/w. During the
219          * first loop, src points to req->src  !! 256          * first loop, lsrc points to src and ldst to dst. For any
220          * subsequent round, the code operates !! 257          * subsequent round, the code operates on dst only.
221          */                                       258          */
222         src = req->src;                        !! 259         lsrc = src;
223         dst = req->dst;                        !! 260         ldst = dst;
224                                                   261 
225         for (i = 0; i < 6; i++) {                 262         for (i = 0; i < 6; i++) {
                                                   >> 263                 u8 tbe_buffer[SEMIBSIZE + alignmask];
                                                   >> 264                 u8 *tbe = PTR_ALIGN(tbe_buffer + 0, alignmask + 1);
                                                   >> 265                 unsigned int tmp_nbytes = nbytes;
226                 struct scatter_walk src_walk,     266                 struct scatter_walk src_walk, dst_walk;
227                 unsigned int nbytes = req->cry << 
228                                                   267 
229                 scatterwalk_start(&src_walk, s !! 268                 scatterwalk_start(&src_walk, lsrc);
230                 scatterwalk_start(&dst_walk, d !! 269                 scatterwalk_start(&dst_walk, ldst);
231                                                   270 
232                 while (nbytes) {               !! 271                 while (tmp_nbytes) {
233                         /* get the source bloc    272                         /* get the source block */
234                         scatterwalk_copychunks !! 273                         scatterwalk_copychunks(block->R, &src_walk, SEMIBSIZE,
235                                                   274                                                false);
236                                                   275 
237                         /* perform KW operatio    276                         /* perform KW operation: encrypt block */
238                         crypto_cipher_encrypt_ !! 277                         crypto_cipher_encrypt_one(child, (u8 *)block,
239                                                !! 278                                                   (u8 *)block);
                                                   >> 279                         /* perform KW operation: get counter as byte string */
                                                   >> 280                         crypto_kw_cpu_to_be64(t, tbe);
240                         /* perform KW operatio    281                         /* perform KW operation: modify IV with counter */
241                         block.A ^= cpu_to_be64 !! 282                         crypto_xor(block->A, tbe, SEMIBSIZE);
242                         t++;                      283                         t++;
243                                                   284 
244                         /* Copy block->R into     285                         /* Copy block->R into place */
245                         scatterwalk_copychunks !! 286                         scatterwalk_copychunks(block->R, &dst_walk, SEMIBSIZE,
246                                                   287                                                true);
247                                                   288 
248                         nbytes -= SEMIBSIZE;   !! 289                         tmp_nbytes -= SEMIBSIZE;
249                 }                                 290                 }
250                                                   291 
251                 /* we now start to operate on     292                 /* we now start to operate on the dst SGL only */
252                 src = req->dst;                !! 293                 lsrc = dst;
253                 dst = req->dst;                !! 294                 ldst = dst;
254         }                                         295         }
255                                                   296 
256         /* establish the IV for the caller to     297         /* establish the IV for the caller to pick up */
257         memcpy(req->iv, &block.A, SEMIBSIZE);  !! 298         memcpy(desc->info, block->A, SEMIBSIZE);
258                                                   299 
259         memzero_explicit(&block, sizeof(struct !! 300         memzero_explicit(block, sizeof(struct crypto_kw_block));
260                                                   301 
261         return 0;                                 302         return 0;
262 }                                                 303 }
263                                                   304 
264 static int crypto_kw_create(struct crypto_temp !! 305 static int crypto_kw_setkey(struct crypto_tfm *parent, const u8 *key,
                                                   >> 306                             unsigned int keylen)
265 {                                                 307 {
266         struct skcipher_instance *inst;        !! 308         struct crypto_kw_ctx *ctx = crypto_tfm_ctx(parent);
267         struct crypto_alg *alg;                !! 309         struct crypto_cipher *child = ctx->child;
268         int err;                                  310         int err;
269                                                   311 
270         inst = skcipher_alloc_instance_simple( !! 312         crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
271         if (IS_ERR(inst))                      !! 313         crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
272                 return PTR_ERR(inst);          !! 314                                        CRYPTO_TFM_REQ_MASK);
                                                   >> 315         err = crypto_cipher_setkey(child, key, keylen);
                                                   >> 316         crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
                                                   >> 317                                      CRYPTO_TFM_RES_MASK);
                                                   >> 318         return err;
                                                   >> 319 }
273                                                   320 
274         alg = skcipher_ialg_simple(inst);      !! 321 static int crypto_kw_init_tfm(struct crypto_tfm *tfm)
                                                   >> 322 {
                                                   >> 323         struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
                                                   >> 324         struct crypto_spawn *spawn = crypto_instance_ctx(inst);
                                                   >> 325         struct crypto_kw_ctx *ctx = crypto_tfm_ctx(tfm);
                                                   >> 326         struct crypto_cipher *cipher;
                                                   >> 327 
                                                   >> 328         cipher = crypto_spawn_cipher(spawn);
                                                   >> 329         if (IS_ERR(cipher))
                                                   >> 330                 return PTR_ERR(cipher);
                                                   >> 331 
                                                   >> 332         ctx->child = cipher;
                                                   >> 333         return 0;
                                                   >> 334 }
                                                   >> 335 
                                                   >> 336 static void crypto_kw_exit_tfm(struct crypto_tfm *tfm)
                                                   >> 337 {
                                                   >> 338         struct crypto_kw_ctx *ctx = crypto_tfm_ctx(tfm);
                                                   >> 339 
                                                   >> 340         crypto_free_cipher(ctx->child);
                                                   >> 341 }
                                                   >> 342 
                                                   >> 343 static struct crypto_instance *crypto_kw_alloc(struct rtattr **tb)
                                                   >> 344 {
                                                   >> 345         struct crypto_instance *inst = NULL;
                                                   >> 346         struct crypto_alg *alg = NULL;
                                                   >> 347         int err;
                                                   >> 348 
                                                   >> 349         err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
                                                   >> 350         if (err)
                                                   >> 351                 return ERR_PTR(err);
                                                   >> 352 
                                                   >> 353         alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
                                                   >> 354                                   CRYPTO_ALG_TYPE_MASK);
                                                   >> 355         if (IS_ERR(alg))
                                                   >> 356                 return ERR_CAST(alg);
275                                                   357 
276         err = -EINVAL;                         !! 358         inst = ERR_PTR(-EINVAL);
277         /* Section 5.1 requirement for KW */      359         /* Section 5.1 requirement for KW */
278         if (alg->cra_blocksize != sizeof(struc    360         if (alg->cra_blocksize != sizeof(struct crypto_kw_block))
279                 goto out_free_inst;            !! 361                 goto err;
280                                                   362 
281         inst->alg.base.cra_blocksize = SEMIBSI !! 363         inst = crypto_alloc_instance("kw", alg);
282         inst->alg.base.cra_alignmask = 0;      !! 364         if (IS_ERR(inst))
283         inst->alg.ivsize = SEMIBSIZE;          !! 365                 goto err;
284                                                << 
285         inst->alg.encrypt = crypto_kw_encrypt; << 
286         inst->alg.decrypt = crypto_kw_decrypt; << 
287                                                << 
288         err = skcipher_register_instance(tmpl, << 
289         if (err) {                             << 
290 out_free_inst:                                 << 
291                 inst->free(inst);              << 
292         }                                      << 
293                                                   366 
294         return err;                            !! 367         inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
                                                   >> 368         inst->alg.cra_priority = alg->cra_priority;
                                                   >> 369         inst->alg.cra_blocksize = SEMIBSIZE;
                                                   >> 370         inst->alg.cra_alignmask = 0;
                                                   >> 371         inst->alg.cra_type = &crypto_blkcipher_type;
                                                   >> 372         inst->alg.cra_blkcipher.ivsize = SEMIBSIZE;
                                                   >> 373         inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
                                                   >> 374         inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
                                                   >> 375 
                                                   >> 376         inst->alg.cra_ctxsize = sizeof(struct crypto_kw_ctx);
                                                   >> 377 
                                                   >> 378         inst->alg.cra_init = crypto_kw_init_tfm;
                                                   >> 379         inst->alg.cra_exit = crypto_kw_exit_tfm;
                                                   >> 380 
                                                   >> 381         inst->alg.cra_blkcipher.setkey = crypto_kw_setkey;
                                                   >> 382         inst->alg.cra_blkcipher.encrypt = crypto_kw_encrypt;
                                                   >> 383         inst->alg.cra_blkcipher.decrypt = crypto_kw_decrypt;
                                                   >> 384 
                                                   >> 385 err:
                                                   >> 386         crypto_mod_put(alg);
                                                   >> 387         return inst;
                                                   >> 388 }
                                                   >> 389 
                                                   >> 390 static void crypto_kw_free(struct crypto_instance *inst)
                                                   >> 391 {
                                                   >> 392         crypto_drop_spawn(crypto_instance_ctx(inst));
                                                   >> 393         kfree(inst);
295 }                                                 394 }
296                                                   395 
297 static struct crypto_template crypto_kw_tmpl =    396 static struct crypto_template crypto_kw_tmpl = {
298         .name = "kw",                             397         .name = "kw",
299         .create = crypto_kw_create,            !! 398         .alloc = crypto_kw_alloc,
                                                   >> 399         .free = crypto_kw_free,
300         .module = THIS_MODULE,                    400         .module = THIS_MODULE,
301 };                                                401 };
302                                                   402 
303 static int __init crypto_kw_init(void)            403 static int __init crypto_kw_init(void)
304 {                                                 404 {
305         return crypto_register_template(&crypt    405         return crypto_register_template(&crypto_kw_tmpl);
306 }                                                 406 }
307                                                   407 
308 static void __exit crypto_kw_exit(void)           408 static void __exit crypto_kw_exit(void)
309 {                                                 409 {
310         crypto_unregister_template(&crypto_kw_    410         crypto_unregister_template(&crypto_kw_tmpl);
311 }                                                 411 }
312                                                   412 
313 subsys_initcall(crypto_kw_init);               !! 413 module_init(crypto_kw_init);
314 module_exit(crypto_kw_exit);                      414 module_exit(crypto_kw_exit);
315                                                   415 
316 MODULE_LICENSE("Dual BSD/GPL");                   416 MODULE_LICENSE("Dual BSD/GPL");
317 MODULE_AUTHOR("Stephan Mueller <smueller@chron    417 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
318 MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NI    418 MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NIST SP800-38F)");
319 MODULE_ALIAS_CRYPTO("kw");                        419 MODULE_ALIAS_CRYPTO("kw");
320 MODULE_IMPORT_NS(CRYPTO_INTERNAL);             << 
321                                                   420 

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