Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/gss_krb5_crypto.c |
| 3 | * |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 4 | * Copyright (c) 2000-2008 The Regents of the University of Michigan. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * Andy Adamson <andros@umich.edu> |
| 8 | * Bruce Fields <bfields@umich.edu> |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Copyright (C) 1998 by the FundsXpress, INC. |
| 13 | * |
| 14 | * All rights reserved. |
| 15 | * |
| 16 | * Export of this software from the United States of America may require |
| 17 | * a specific license from the United States Government. It is the |
| 18 | * responsibility of any person or organization contemplating export to |
| 19 | * obtain such a license before exporting. |
| 20 | * |
| 21 | * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and |
| 22 | * distribute this software and its documentation for any purpose and |
| 23 | * without fee is hereby granted, provided that the above copyright |
| 24 | * notice appear in all copies and that both that copyright notice and |
| 25 | * this permission notice appear in supporting documentation, and that |
| 26 | * the name of FundsXpress. not be used in advertising or publicity pertaining |
| 27 | * to distribution of the software without specific, written prior |
| 28 | * permission. FundsXpress makes no representations about the suitability of |
| 29 | * this software for any purpose. It is provided "as is" without express |
| 30 | * or implied warranty. |
| 31 | * |
| 32 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 33 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 34 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 35 | */ |
| 36 | |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 37 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/types.h> |
| 39 | #include <linux/mm.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 40 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/crypto.h> |
| 42 | #include <linux/highmem.h> |
| 43 | #include <linux/pagemap.h> |
| 44 | #include <linux/sunrpc/gss_krb5.h> |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 45 | #include <linux/sunrpc/xdr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #ifdef RPC_DEBUG |
| 48 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 49 | #endif |
| 50 | |
| 51 | u32 |
| 52 | krb5_encrypt( |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 53 | struct crypto_blkcipher *tfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | void * iv, |
| 55 | void * in, |
| 56 | void * out, |
| 57 | int length) |
| 58 | { |
| 59 | u32 ret = -EINVAL; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 60 | struct scatterlist sg[1]; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 61 | u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0}; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 62 | struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 64 | if (length % crypto_blkcipher_blocksize(tfm) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | goto out; |
| 66 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 67 | if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) { |
Kevin Coffman | 3d4a688 | 2008-02-21 13:44:12 -0500 | [diff] [blame] | 68 | dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n", |
| 69 | crypto_blkcipher_ivsize(tfm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | if (iv) |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 74 | memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | memcpy(out, in, length); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 77 | sg_init_one(sg, out, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 79 | ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | out: |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 81 | dprintk("RPC: krb5_encrypt returns %d\n", ret); |
J. Bruce Fields | 8fc7500 | 2006-12-04 20:22:31 -0500 | [diff] [blame] | 82 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | u32 |
| 86 | krb5_decrypt( |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 87 | struct crypto_blkcipher *tfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | void * iv, |
| 89 | void * in, |
| 90 | void * out, |
| 91 | int length) |
| 92 | { |
| 93 | u32 ret = -EINVAL; |
| 94 | struct scatterlist sg[1]; |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 95 | u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0}; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 96 | struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 98 | if (length % crypto_blkcipher_blocksize(tfm) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | goto out; |
| 100 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 101 | if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) { |
Kevin Coffman | 3d4a688 | 2008-02-21 13:44:12 -0500 | [diff] [blame] | 102 | dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n", |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 103 | crypto_blkcipher_ivsize(tfm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | goto out; |
| 105 | } |
| 106 | if (iv) |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 107 | memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | memcpy(out, in, length); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 110 | sg_init_one(sg, out, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 112 | ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | out: |
Chuck Lever | 8885cb3 | 2007-01-31 12:14:05 -0500 | [diff] [blame] | 114 | dprintk("RPC: gss_k5decrypt returns %d\n",ret); |
J. Bruce Fields | 8fc7500 | 2006-12-04 20:22:31 -0500 | [diff] [blame] | 115 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
J. Bruce Fields | f7b3af6 | 2005-10-13 16:55:03 -0400 | [diff] [blame] | 118 | static int |
J. Bruce Fields | f7b3af6 | 2005-10-13 16:55:03 -0400 | [diff] [blame] | 119 | checksummer(struct scatterlist *sg, void *data) |
| 120 | { |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 121 | struct hash_desc *desc = data; |
J. Bruce Fields | f7b3af6 | 2005-10-13 16:55:03 -0400 | [diff] [blame] | 122 | |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 123 | return crypto_hash_update(desc, sg, sg->length); |
J. Bruce Fields | f7b3af6 | 2005-10-13 16:55:03 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 126 | /* |
| 127 | * checksum the plaintext data and hdrlen bytes of the token header |
| 128 | * The checksum is performed over the first 8 bytes of the |
| 129 | * gss token header and then over the data body |
| 130 | */ |
| 131 | u32 |
| 132 | make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen, |
| 133 | struct xdr_buf *body, int body_offset, u8 *cksumkey, |
| 134 | struct xdr_netobj *cksumout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 136 | struct hash_desc desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | struct scatterlist sg[1]; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 138 | int err; |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 139 | u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN]; |
| 140 | unsigned int checksumlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 142 | if (cksumout->len < kctx->gk5e->cksumlength) { |
| 143 | dprintk("%s: checksum buffer length, %u, too small for %s\n", |
| 144 | __func__, cksumout->len, kctx->gk5e->name); |
| 145 | return GSS_S_FAILURE; |
| 146 | } |
| 147 | |
| 148 | desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 149 | if (IS_ERR(desc.tfm)) |
J. Bruce Fields | d4a30e7 | 2006-04-18 13:14:02 -0400 | [diff] [blame] | 150 | return GSS_S_FAILURE; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 151 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 153 | checksumlen = crypto_hash_digestsize(desc.tfm); |
| 154 | |
| 155 | if (cksumkey != NULL) { |
| 156 | err = crypto_hash_setkey(desc.tfm, cksumkey, |
| 157 | kctx->gk5e->keylength); |
| 158 | if (err) |
| 159 | goto out; |
| 160 | } |
| 161 | |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 162 | err = crypto_hash_init(&desc); |
| 163 | if (err) |
| 164 | goto out; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 165 | sg_init_one(sg, header, hdrlen); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 166 | err = crypto_hash_update(&desc, sg, hdrlen); |
| 167 | if (err) |
| 168 | goto out; |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 169 | err = xdr_process_buf(body, body_offset, body->len - body_offset, |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 170 | checksummer, &desc); |
| 171 | if (err) |
| 172 | goto out; |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 173 | err = crypto_hash_final(&desc, checksumdata); |
| 174 | if (err) |
| 175 | goto out; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 176 | |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 177 | switch (kctx->gk5e->ctype) { |
| 178 | case CKSUMTYPE_RSA_MD5: |
| 179 | err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata, |
| 180 | checksumdata, checksumlen); |
| 181 | if (err) |
| 182 | goto out; |
| 183 | memcpy(cksumout->data, |
| 184 | checksumdata + checksumlen - kctx->gk5e->cksumlength, |
| 185 | kctx->gk5e->cksumlength); |
| 186 | break; |
Kevin Coffman | 958142e | 2010-03-17 13:02:55 -0400 | [diff] [blame] | 187 | case CKSUMTYPE_HMAC_SHA1_DES3: |
| 188 | memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength); |
| 189 | break; |
Kevin Coffman | e1f6c07 | 2010-03-17 13:02:52 -0400 | [diff] [blame] | 190 | default: |
| 191 | BUG(); |
| 192 | break; |
| 193 | } |
| 194 | cksumout->len = kctx->gk5e->cksumlength; |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 195 | out: |
| 196 | crypto_free_hash(desc.tfm); |
| 197 | return err ? GSS_S_FAILURE : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Kevin Coffman | de9c17e | 2010-03-17 13:02:59 -0400 | [diff] [blame^] | 200 | /* |
| 201 | * checksum the plaintext data and hdrlen bytes of the token header |
| 202 | * Per rfc4121, sec. 4.2.4, the checksum is performed over the data |
| 203 | * body then over the first 16 octets of the MIC token |
| 204 | * Inclusion of the header data in the calculation of the |
| 205 | * checksum is optional. |
| 206 | */ |
| 207 | u32 |
| 208 | make_checksum_v2(struct krb5_ctx *kctx, char *header, int hdrlen, |
| 209 | struct xdr_buf *body, int body_offset, u8 *cksumkey, |
| 210 | struct xdr_netobj *cksumout) |
| 211 | { |
| 212 | struct hash_desc desc; |
| 213 | struct scatterlist sg[1]; |
| 214 | int err; |
| 215 | u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN]; |
| 216 | unsigned int checksumlen; |
| 217 | |
| 218 | if (kctx->gk5e->keyed_cksum == 0) { |
| 219 | dprintk("%s: expected keyed hash for %s\n", |
| 220 | __func__, kctx->gk5e->name); |
| 221 | return GSS_S_FAILURE; |
| 222 | } |
| 223 | if (cksumkey == NULL) { |
| 224 | dprintk("%s: no key supplied for %s\n", |
| 225 | __func__, kctx->gk5e->name); |
| 226 | return GSS_S_FAILURE; |
| 227 | } |
| 228 | |
| 229 | desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0, |
| 230 | CRYPTO_ALG_ASYNC); |
| 231 | if (IS_ERR(desc.tfm)) |
| 232 | return GSS_S_FAILURE; |
| 233 | checksumlen = crypto_hash_digestsize(desc.tfm); |
| 234 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 235 | |
| 236 | err = crypto_hash_setkey(desc.tfm, cksumkey, kctx->gk5e->keylength); |
| 237 | if (err) |
| 238 | goto out; |
| 239 | |
| 240 | err = crypto_hash_init(&desc); |
| 241 | if (err) |
| 242 | goto out; |
| 243 | err = xdr_process_buf(body, body_offset, body->len - body_offset, |
| 244 | checksummer, &desc); |
| 245 | if (err) |
| 246 | goto out; |
| 247 | if (header != NULL) { |
| 248 | sg_init_one(sg, header, hdrlen); |
| 249 | err = crypto_hash_update(&desc, sg, hdrlen); |
| 250 | if (err) |
| 251 | goto out; |
| 252 | } |
| 253 | err = crypto_hash_final(&desc, checksumdata); |
| 254 | if (err) |
| 255 | goto out; |
| 256 | |
| 257 | cksumout->len = kctx->gk5e->cksumlength; |
| 258 | |
| 259 | switch (kctx->gk5e->ctype) { |
| 260 | case CKSUMTYPE_HMAC_SHA1_96_AES128: |
| 261 | case CKSUMTYPE_HMAC_SHA1_96_AES256: |
| 262 | /* note that this truncates the hash */ |
| 263 | memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength); |
| 264 | break; |
| 265 | default: |
| 266 | BUG(); |
| 267 | break; |
| 268 | } |
| 269 | out: |
| 270 | crypto_free_hash(desc.tfm); |
| 271 | return err ? GSS_S_FAILURE : 0; |
| 272 | } |
| 273 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 274 | struct encryptor_desc { |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 275 | u8 iv[GSS_KRB5_MAX_BLOCKSIZE]; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 276 | struct blkcipher_desc desc; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 277 | int pos; |
| 278 | struct xdr_buf *outbuf; |
| 279 | struct page **pages; |
| 280 | struct scatterlist infrags[4]; |
| 281 | struct scatterlist outfrags[4]; |
| 282 | int fragno; |
| 283 | int fraglen; |
| 284 | }; |
| 285 | |
| 286 | static int |
| 287 | encryptor(struct scatterlist *sg, void *data) |
| 288 | { |
| 289 | struct encryptor_desc *desc = data; |
| 290 | struct xdr_buf *outbuf = desc->outbuf; |
| 291 | struct page *in_page; |
| 292 | int thislen = desc->fraglen + sg->length; |
| 293 | int fraglen, ret; |
| 294 | int page_pos; |
| 295 | |
| 296 | /* Worst case is 4 fragments: head, end of page 1, start |
| 297 | * of page 2, tail. Anything more is a bug. */ |
| 298 | BUG_ON(desc->fragno > 3); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 299 | |
| 300 | page_pos = desc->pos - outbuf->head[0].iov_len; |
| 301 | if (page_pos >= 0 && page_pos < outbuf->page_len) { |
| 302 | /* pages are not in place: */ |
| 303 | int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT; |
| 304 | in_page = desc->pages[i]; |
| 305 | } else { |
Jens Axboe | fa05f12 | 2007-10-22 19:44:26 +0200 | [diff] [blame] | 306 | in_page = sg_page(sg); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 307 | } |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 308 | sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length, |
| 309 | sg->offset); |
| 310 | sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length, |
| 311 | sg->offset); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 312 | desc->fragno++; |
| 313 | desc->fraglen += sg->length; |
| 314 | desc->pos += sg->length; |
| 315 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 316 | fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 317 | thislen -= fraglen; |
| 318 | |
| 319 | if (thislen == 0) |
| 320 | return 0; |
| 321 | |
Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 322 | sg_mark_end(&desc->infrags[desc->fragno - 1]); |
| 323 | sg_mark_end(&desc->outfrags[desc->fragno - 1]); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 324 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 325 | ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags, |
| 326 | desc->infrags, thislen); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 327 | if (ret) |
| 328 | return ret; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 329 | |
| 330 | sg_init_table(desc->infrags, 4); |
| 331 | sg_init_table(desc->outfrags, 4); |
| 332 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 333 | if (fraglen) { |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 334 | sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen, |
| 335 | sg->offset + sg->length - fraglen); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 336 | desc->infrags[0] = desc->outfrags[0]; |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 337 | sg_assign_page(&desc->infrags[0], in_page); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 338 | desc->fragno = 1; |
| 339 | desc->fraglen = fraglen; |
| 340 | } else { |
| 341 | desc->fragno = 0; |
| 342 | desc->fraglen = 0; |
| 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | int |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 348 | gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf, |
| 349 | int offset, struct page **pages) |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 350 | { |
| 351 | int ret; |
| 352 | struct encryptor_desc desc; |
| 353 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 354 | BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 355 | |
| 356 | memset(desc.iv, 0, sizeof(desc.iv)); |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 357 | desc.desc.tfm = tfm; |
| 358 | desc.desc.info = desc.iv; |
| 359 | desc.desc.flags = 0; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 360 | desc.pos = offset; |
| 361 | desc.outbuf = buf; |
| 362 | desc.pages = pages; |
| 363 | desc.fragno = 0; |
| 364 | desc.fraglen = 0; |
| 365 | |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 366 | sg_init_table(desc.infrags, 4); |
| 367 | sg_init_table(desc.outfrags, 4); |
| 368 | |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 369 | ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 370 | return ret; |
| 371 | } |
| 372 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 373 | struct decryptor_desc { |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 374 | u8 iv[GSS_KRB5_MAX_BLOCKSIZE]; |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 375 | struct blkcipher_desc desc; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 376 | struct scatterlist frags[4]; |
| 377 | int fragno; |
| 378 | int fraglen; |
| 379 | }; |
| 380 | |
| 381 | static int |
| 382 | decryptor(struct scatterlist *sg, void *data) |
| 383 | { |
| 384 | struct decryptor_desc *desc = data; |
| 385 | int thislen = desc->fraglen + sg->length; |
| 386 | int fraglen, ret; |
| 387 | |
| 388 | /* Worst case is 4 fragments: head, end of page 1, start |
| 389 | * of page 2, tail. Anything more is a bug. */ |
| 390 | BUG_ON(desc->fragno > 3); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 391 | sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length, |
| 392 | sg->offset); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 393 | desc->fragno++; |
| 394 | desc->fraglen += sg->length; |
| 395 | |
Kevin Coffman | 81d4a43 | 2010-03-17 13:02:51 -0400 | [diff] [blame] | 396 | fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 397 | thislen -= fraglen; |
| 398 | |
| 399 | if (thislen == 0) |
| 400 | return 0; |
| 401 | |
Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 402 | sg_mark_end(&desc->frags[desc->fragno - 1]); |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 403 | |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 404 | ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags, |
| 405 | desc->frags, thislen); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 406 | if (ret) |
| 407 | return ret; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 408 | |
| 409 | sg_init_table(desc->frags, 4); |
| 410 | |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 411 | if (fraglen) { |
Jens Axboe | 642f14903 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 412 | sg_set_page(&desc->frags[0], sg_page(sg), fraglen, |
| 413 | sg->offset + sg->length - fraglen); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 414 | desc->fragno = 1; |
| 415 | desc->fraglen = fraglen; |
| 416 | } else { |
| 417 | desc->fragno = 0; |
| 418 | desc->fraglen = 0; |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | int |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 424 | gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf, |
| 425 | int offset) |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 426 | { |
| 427 | struct decryptor_desc desc; |
| 428 | |
| 429 | /* XXXJBF: */ |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 430 | BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 431 | |
| 432 | memset(desc.iv, 0, sizeof(desc.iv)); |
Herbert Xu | 378c669 | 2006-08-22 20:33:54 +1000 | [diff] [blame] | 433 | desc.desc.tfm = tfm; |
| 434 | desc.desc.info = desc.iv; |
| 435 | desc.desc.flags = 0; |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 436 | desc.fragno = 0; |
| 437 | desc.fraglen = 0; |
Herbert Xu | 68e3f5d | 2007-10-27 00:52:07 -0700 | [diff] [blame] | 438 | |
| 439 | sg_init_table(desc.frags, 4); |
| 440 | |
Olga Kornievskaia | 37a4e6c | 2006-12-04 20:22:33 -0500 | [diff] [blame] | 441 | return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc); |
J. Bruce Fields | 14ae162 | 2005-10-13 16:55:13 -0400 | [diff] [blame] | 442 | } |
Kevin Coffman | 725f286 | 2010-03-17 13:02:46 -0400 | [diff] [blame] | 443 | |
| 444 | /* |
| 445 | * This function makes the assumption that it was ultimately called |
| 446 | * from gss_wrap(). |
| 447 | * |
| 448 | * The client auth_gss code moves any existing tail data into a |
| 449 | * separate page before calling gss_wrap. |
| 450 | * The server svcauth_gss code ensures that both the head and the |
| 451 | * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap. |
| 452 | * |
| 453 | * Even with that guarantee, this function may be called more than |
| 454 | * once in the processing of gss_wrap(). The best we can do is |
| 455 | * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the |
| 456 | * largest expected shift will fit within RPC_MAX_AUTH_SIZE. |
| 457 | * At run-time we can verify that a single invocation of this |
| 458 | * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE. |
| 459 | */ |
| 460 | |
| 461 | int |
| 462 | xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen) |
| 463 | { |
| 464 | u8 *p; |
| 465 | |
| 466 | if (shiftlen == 0) |
| 467 | return 0; |
| 468 | |
| 469 | BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE); |
| 470 | BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE); |
| 471 | |
| 472 | p = buf->head[0].iov_base + base; |
| 473 | |
| 474 | memmove(p + shiftlen, p, buf->head[0].iov_len - base); |
| 475 | |
| 476 | buf->head[0].iov_len += shiftlen; |
| 477 | buf->len += shiftlen; |
| 478 | |
| 479 | return 0; |
| 480 | } |