gss_krb5_crypto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * linux/net/sunrpc/gss_krb5_crypto.c
  3. *
  4. * Copyright (c) 2000-2008 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Andy Adamson <andros@umich.edu>
  8. * Bruce Fields <bfields@umich.edu>
  9. */
  10. /*
  11. * Copyright (C) 1998 by the FundsXpress, INC.
  12. *
  13. * All rights reserved.
  14. *
  15. * Export of this software from the United States of America may require
  16. * a specific license from the United States Government. It is the
  17. * responsibility of any person or organization contemplating export to
  18. * obtain such a license before exporting.
  19. *
  20. * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
  21. * distribute this software and its documentation for any purpose and
  22. * without fee is hereby granted, provided that the above copyright
  23. * notice appear in all copies and that both that copyright notice and
  24. * this permission notice appear in supporting documentation, and that
  25. * the name of FundsXpress. not be used in advertising or publicity pertaining
  26. * to distribution of the software without specific, written prior
  27. * permission. FundsXpress makes no representations about the suitability of
  28. * this software for any purpose. It is provided "as is" without express
  29. * or implied warranty.
  30. *
  31. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  32. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  33. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  34. */
  35. #include <linux/err.h>
  36. #include <linux/types.h>
  37. #include <linux/mm.h>
  38. #include <linux/scatterlist.h>
  39. #include <linux/crypto.h>
  40. #include <linux/highmem.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/random.h>
  43. #include <linux/sunrpc/gss_krb5.h>
  44. #include <linux/sunrpc/xdr.h>
  45. #ifdef RPC_DEBUG
  46. # define RPCDBG_FACILITY RPCDBG_AUTH
  47. #endif
  48. u32
  49. krb5_encrypt(
  50. struct crypto_blkcipher *tfm,
  51. void * iv,
  52. void * in,
  53. void * out,
  54. int length)
  55. {
  56. u32 ret = -EINVAL;
  57. struct scatterlist sg[1];
  58. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  59. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  60. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  61. goto out;
  62. if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  63. dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
  64. crypto_blkcipher_ivsize(tfm));
  65. goto out;
  66. }
  67. if (iv)
  68. memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
  69. memcpy(out, in, length);
  70. sg_init_one(sg, out, length);
  71. ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
  72. out:
  73. dprintk("RPC: krb5_encrypt returns %d\n", ret);
  74. return ret;
  75. }
  76. u32
  77. krb5_decrypt(
  78. struct crypto_blkcipher *tfm,
  79. void * iv,
  80. void * in,
  81. void * out,
  82. int length)
  83. {
  84. u32 ret = -EINVAL;
  85. struct scatterlist sg[1];
  86. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  87. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  88. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  89. goto out;
  90. if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  91. dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
  92. crypto_blkcipher_ivsize(tfm));
  93. goto out;
  94. }
  95. if (iv)
  96. memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
  97. memcpy(out, in, length);
  98. sg_init_one(sg, out, length);
  99. ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
  100. out:
  101. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  102. return ret;
  103. }
  104. static int
  105. checksummer(struct scatterlist *sg, void *data)
  106. {
  107. struct hash_desc *desc = data;
  108. return crypto_hash_update(desc, sg, sg->length);
  109. }
  110. /*
  111. * checksum the plaintext data and hdrlen bytes of the token header
  112. * The checksum is performed over the first 8 bytes of the
  113. * gss token header and then over the data body
  114. */
  115. u32
  116. make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
  117. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  118. unsigned int usage, struct xdr_netobj *cksumout)
  119. {
  120. struct hash_desc desc;
  121. struct scatterlist sg[1];
  122. int err;
  123. u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  124. unsigned int checksumlen;
  125. if (cksumout->len < kctx->gk5e->cksumlength) {
  126. dprintk("%s: checksum buffer length, %u, too small for %s\n",
  127. __func__, cksumout->len, kctx->gk5e->name);
  128. return GSS_S_FAILURE;
  129. }
  130. desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  131. if (IS_ERR(desc.tfm))
  132. return GSS_S_FAILURE;
  133. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  134. checksumlen = crypto_hash_digestsize(desc.tfm);
  135. if (cksumkey != NULL) {
  136. err = crypto_hash_setkey(desc.tfm, cksumkey,
  137. kctx->gk5e->keylength);
  138. if (err)
  139. goto out;
  140. }
  141. err = crypto_hash_init(&desc);
  142. if (err)
  143. goto out;
  144. sg_init_one(sg, header, hdrlen);
  145. err = crypto_hash_update(&desc, sg, hdrlen);
  146. if (err)
  147. goto out;
  148. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  149. checksummer, &desc);
  150. if (err)
  151. goto out;
  152. err = crypto_hash_final(&desc, checksumdata);
  153. if (err)
  154. goto out;
  155. switch (kctx->gk5e->ctype) {
  156. case CKSUMTYPE_RSA_MD5:
  157. err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata,
  158. checksumdata, checksumlen);
  159. if (err)
  160. goto out;
  161. memcpy(cksumout->data,
  162. checksumdata + checksumlen - kctx->gk5e->cksumlength,
  163. kctx->gk5e->cksumlength);
  164. break;
  165. case CKSUMTYPE_HMAC_SHA1_DES3:
  166. memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
  167. break;
  168. default:
  169. BUG();
  170. break;
  171. }
  172. cksumout->len = kctx->gk5e->cksumlength;
  173. out:
  174. crypto_free_hash(desc.tfm);
  175. return err ? GSS_S_FAILURE : 0;
  176. }
  177. /*
  178. * checksum the plaintext data and hdrlen bytes of the token header
  179. * Per rfc4121, sec. 4.2.4, the checksum is performed over the data
  180. * body then over the first 16 octets of the MIC token
  181. * Inclusion of the header data in the calculation of the
  182. * checksum is optional.
  183. */
  184. u32
  185. make_checksum_v2(struct krb5_ctx *kctx, char *header, int hdrlen,
  186. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  187. unsigned int usage, struct xdr_netobj *cksumout)
  188. {
  189. struct hash_desc desc;
  190. struct scatterlist sg[1];
  191. int err;
  192. u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  193. unsigned int checksumlen;
  194. if (kctx->gk5e->keyed_cksum == 0) {
  195. dprintk("%s: expected keyed hash for %s\n",
  196. __func__, kctx->gk5e->name);
  197. return GSS_S_FAILURE;
  198. }
  199. if (cksumkey == NULL) {
  200. dprintk("%s: no key supplied for %s\n",
  201. __func__, kctx->gk5e->name);
  202. return GSS_S_FAILURE;
  203. }
  204. desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0,
  205. CRYPTO_ALG_ASYNC);
  206. if (IS_ERR(desc.tfm))
  207. return GSS_S_FAILURE;
  208. checksumlen = crypto_hash_digestsize(desc.tfm);
  209. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  210. err = crypto_hash_setkey(desc.tfm, cksumkey, kctx->gk5e->keylength);
  211. if (err)
  212. goto out;
  213. err = crypto_hash_init(&desc);
  214. if (err)
  215. goto out;
  216. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  217. checksummer, &desc);
  218. if (err)
  219. goto out;
  220. if (header != NULL) {
  221. sg_init_one(sg, header, hdrlen);
  222. err = crypto_hash_update(&desc, sg, hdrlen);
  223. if (err)
  224. goto out;
  225. }
  226. err = crypto_hash_final(&desc, checksumdata);
  227. if (err)
  228. goto out;
  229. cksumout->len = kctx->gk5e->cksumlength;
  230. switch (kctx->gk5e->ctype) {
  231. case CKSUMTYPE_HMAC_SHA1_96_AES128:
  232. case CKSUMTYPE_HMAC_SHA1_96_AES256:
  233. /* note that this truncates the hash */
  234. memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
  235. break;
  236. default:
  237. BUG();
  238. break;
  239. }
  240. out:
  241. crypto_free_hash(desc.tfm);
  242. return err ? GSS_S_FAILURE : 0;
  243. }
  244. struct encryptor_desc {
  245. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  246. struct blkcipher_desc desc;
  247. int pos;
  248. struct xdr_buf *outbuf;
  249. struct page **pages;
  250. struct scatterlist infrags[4];
  251. struct scatterlist outfrags[4];
  252. int fragno;
  253. int fraglen;
  254. };
  255. static int
  256. encryptor(struct scatterlist *sg, void *data)
  257. {
  258. struct encryptor_desc *desc = data;
  259. struct xdr_buf *outbuf = desc->outbuf;
  260. struct page *in_page;
  261. int thislen = desc->fraglen + sg->length;
  262. int fraglen, ret;
  263. int page_pos;
  264. /* Worst case is 4 fragments: head, end of page 1, start
  265. * of page 2, tail. Anything more is a bug. */
  266. BUG_ON(desc->fragno > 3);
  267. page_pos = desc->pos - outbuf->head[0].iov_len;
  268. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  269. /* pages are not in place: */
  270. int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
  271. in_page = desc->pages[i];
  272. } else {
  273. in_page = sg_page(sg);
  274. }
  275. sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
  276. sg->offset);
  277. sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
  278. sg->offset);
  279. desc->fragno++;
  280. desc->fraglen += sg->length;
  281. desc->pos += sg->length;
  282. fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
  283. thislen -= fraglen;
  284. if (thislen == 0)
  285. return 0;
  286. sg_mark_end(&desc->infrags[desc->fragno - 1]);
  287. sg_mark_end(&desc->outfrags[desc->fragno - 1]);
  288. ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
  289. desc->infrags, thislen);
  290. if (ret)
  291. return ret;
  292. sg_init_table(desc->infrags, 4);
  293. sg_init_table(desc->outfrags, 4);
  294. if (fraglen) {
  295. sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
  296. sg->offset + sg->length - fraglen);
  297. desc->infrags[0] = desc->outfrags[0];
  298. sg_assign_page(&desc->infrags[0], in_page);
  299. desc->fragno = 1;
  300. desc->fraglen = fraglen;
  301. } else {
  302. desc->fragno = 0;
  303. desc->fraglen = 0;
  304. }
  305. return 0;
  306. }
  307. int
  308. gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  309. int offset, struct page **pages)
  310. {
  311. int ret;
  312. struct encryptor_desc desc;
  313. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  314. memset(desc.iv, 0, sizeof(desc.iv));
  315. desc.desc.tfm = tfm;
  316. desc.desc.info = desc.iv;
  317. desc.desc.flags = 0;
  318. desc.pos = offset;
  319. desc.outbuf = buf;
  320. desc.pages = pages;
  321. desc.fragno = 0;
  322. desc.fraglen = 0;
  323. sg_init_table(desc.infrags, 4);
  324. sg_init_table(desc.outfrags, 4);
  325. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  326. return ret;
  327. }
  328. struct decryptor_desc {
  329. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  330. struct blkcipher_desc desc;
  331. struct scatterlist frags[4];
  332. int fragno;
  333. int fraglen;
  334. };
  335. static int
  336. decryptor(struct scatterlist *sg, void *data)
  337. {
  338. struct decryptor_desc *desc = data;
  339. int thislen = desc->fraglen + sg->length;
  340. int fraglen, ret;
  341. /* Worst case is 4 fragments: head, end of page 1, start
  342. * of page 2, tail. Anything more is a bug. */
  343. BUG_ON(desc->fragno > 3);
  344. sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
  345. sg->offset);
  346. desc->fragno++;
  347. desc->fraglen += sg->length;
  348. fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
  349. thislen -= fraglen;
  350. if (thislen == 0)
  351. return 0;
  352. sg_mark_end(&desc->frags[desc->fragno - 1]);
  353. ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
  354. desc->frags, thislen);
  355. if (ret)
  356. return ret;
  357. sg_init_table(desc->frags, 4);
  358. if (fraglen) {
  359. sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
  360. sg->offset + sg->length - fraglen);
  361. desc->fragno = 1;
  362. desc->fraglen = fraglen;
  363. } else {
  364. desc->fragno = 0;
  365. desc->fraglen = 0;
  366. }
  367. return 0;
  368. }
  369. int
  370. gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  371. int offset)
  372. {
  373. struct decryptor_desc desc;
  374. /* XXXJBF: */
  375. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  376. memset(desc.iv, 0, sizeof(desc.iv));
  377. desc.desc.tfm = tfm;
  378. desc.desc.info = desc.iv;
  379. desc.desc.flags = 0;
  380. desc.fragno = 0;
  381. desc.fraglen = 0;
  382. sg_init_table(desc.frags, 4);
  383. return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  384. }
  385. /*
  386. * This function makes the assumption that it was ultimately called
  387. * from gss_wrap().
  388. *
  389. * The client auth_gss code moves any existing tail data into a
  390. * separate page before calling gss_wrap.
  391. * The server svcauth_gss code ensures that both the head and the
  392. * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
  393. *
  394. * Even with that guarantee, this function may be called more than
  395. * once in the processing of gss_wrap(). The best we can do is
  396. * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
  397. * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
  398. * At run-time we can verify that a single invocation of this
  399. * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
  400. */
  401. int
  402. xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
  403. {
  404. u8 *p;
  405. if (shiftlen == 0)
  406. return 0;
  407. BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE);
  408. BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
  409. p = buf->head[0].iov_base + base;
  410. memmove(p + shiftlen, p, buf->head[0].iov_len - base);
  411. buf->head[0].iov_len += shiftlen;
  412. buf->len += shiftlen;
  413. return 0;
  414. }
  415. static u32
  416. gss_krb5_cts_crypt(struct crypto_blkcipher *cipher, struct xdr_buf *buf,
  417. u32 offset, u8 *iv, struct page **pages, int encrypt)
  418. {
  419. u32 ret;
  420. struct scatterlist sg[1];
  421. struct blkcipher_desc desc = { .tfm = cipher, .info = iv };
  422. u8 data[crypto_blkcipher_blocksize(cipher) * 2];
  423. struct page **save_pages;
  424. u32 len = buf->len - offset;
  425. BUG_ON(len > crypto_blkcipher_blocksize(cipher) * 2);
  426. /*
  427. * For encryption, we want to read from the cleartext
  428. * page cache pages, and write the encrypted data to
  429. * the supplied xdr_buf pages.
  430. */
  431. save_pages = buf->pages;
  432. if (encrypt)
  433. buf->pages = pages;
  434. ret = read_bytes_from_xdr_buf(buf, offset, data, len);
  435. buf->pages = save_pages;
  436. if (ret)
  437. goto out;
  438. sg_init_one(sg, data, len);
  439. if (encrypt)
  440. ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, len);
  441. else
  442. ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, len);
  443. if (ret)
  444. goto out;
  445. ret = write_bytes_to_xdr_buf(buf, offset, data, len);
  446. out:
  447. return ret;
  448. }
  449. u32
  450. gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
  451. struct xdr_buf *buf, int ec, struct page **pages)
  452. {
  453. u32 err;
  454. struct xdr_netobj hmac;
  455. u8 *cksumkey;
  456. u8 *ecptr;
  457. struct crypto_blkcipher *cipher, *aux_cipher;
  458. int blocksize;
  459. struct page **save_pages;
  460. int nblocks, nbytes;
  461. struct encryptor_desc desc;
  462. u32 cbcbytes;
  463. unsigned int usage;
  464. if (kctx->initiate) {
  465. cipher = kctx->initiator_enc;
  466. aux_cipher = kctx->initiator_enc_aux;
  467. cksumkey = kctx->initiator_integ;
  468. usage = KG_USAGE_INITIATOR_SEAL;
  469. } else {
  470. cipher = kctx->acceptor_enc;
  471. aux_cipher = kctx->acceptor_enc_aux;
  472. cksumkey = kctx->acceptor_integ;
  473. usage = KG_USAGE_ACCEPTOR_SEAL;
  474. }
  475. blocksize = crypto_blkcipher_blocksize(cipher);
  476. /* hide the gss token header and insert the confounder */
  477. offset += GSS_KRB5_TOK_HDR_LEN;
  478. if (xdr_extend_head(buf, offset, blocksize))
  479. return GSS_S_FAILURE;
  480. gss_krb5_make_confounder(buf->head[0].iov_base + offset, blocksize);
  481. offset -= GSS_KRB5_TOK_HDR_LEN;
  482. if (buf->tail[0].iov_base != NULL) {
  483. ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
  484. } else {
  485. buf->tail[0].iov_base = buf->head[0].iov_base
  486. + buf->head[0].iov_len;
  487. buf->tail[0].iov_len = 0;
  488. ecptr = buf->tail[0].iov_base;
  489. }
  490. memset(ecptr, 'X', ec);
  491. buf->tail[0].iov_len += ec;
  492. buf->len += ec;
  493. /* copy plaintext gss token header after filler (if any) */
  494. memcpy(ecptr + ec, buf->head[0].iov_base + offset,
  495. GSS_KRB5_TOK_HDR_LEN);
  496. buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
  497. buf->len += GSS_KRB5_TOK_HDR_LEN;
  498. /* Do the HMAC */
  499. hmac.len = GSS_KRB5_MAX_CKSUM_LEN;
  500. hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  501. /*
  502. * When we are called, pages points to the real page cache
  503. * data -- which we can't go and encrypt! buf->pages points
  504. * to scratch pages which we are going to send off to the
  505. * client/server. Swap in the plaintext pages to calculate
  506. * the hmac.
  507. */
  508. save_pages = buf->pages;
  509. buf->pages = pages;
  510. err = make_checksum_v2(kctx, NULL, 0, buf,
  511. offset + GSS_KRB5_TOK_HDR_LEN,
  512. cksumkey, usage, &hmac);
  513. buf->pages = save_pages;
  514. if (err)
  515. return GSS_S_FAILURE;
  516. nbytes = buf->len - offset - GSS_KRB5_TOK_HDR_LEN;
  517. nblocks = (nbytes + blocksize - 1) / blocksize;
  518. cbcbytes = 0;
  519. if (nblocks > 2)
  520. cbcbytes = (nblocks - 2) * blocksize;
  521. memset(desc.iv, 0, sizeof(desc.iv));
  522. if (cbcbytes) {
  523. desc.pos = offset + GSS_KRB5_TOK_HDR_LEN;
  524. desc.fragno = 0;
  525. desc.fraglen = 0;
  526. desc.pages = pages;
  527. desc.outbuf = buf;
  528. desc.desc.info = desc.iv;
  529. desc.desc.flags = 0;
  530. desc.desc.tfm = aux_cipher;
  531. sg_init_table(desc.infrags, 4);
  532. sg_init_table(desc.outfrags, 4);
  533. err = xdr_process_buf(buf, offset + GSS_KRB5_TOK_HDR_LEN,
  534. cbcbytes, encryptor, &desc);
  535. if (err)
  536. goto out_err;
  537. }
  538. /* Make sure IV carries forward from any CBC results. */
  539. err = gss_krb5_cts_crypt(cipher, buf,
  540. offset + GSS_KRB5_TOK_HDR_LEN + cbcbytes,
  541. desc.iv, pages, 1);
  542. if (err) {
  543. err = GSS_S_FAILURE;
  544. goto out_err;
  545. }
  546. /* Now update buf to account for HMAC */
  547. buf->tail[0].iov_len += kctx->gk5e->cksumlength;
  548. buf->len += kctx->gk5e->cksumlength;
  549. out_err:
  550. if (err)
  551. err = GSS_S_FAILURE;
  552. return err;
  553. }
  554. u32
  555. gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf,
  556. u32 *headskip, u32 *tailskip)
  557. {
  558. struct xdr_buf subbuf;
  559. u32 ret = 0;
  560. u8 *cksum_key;
  561. struct crypto_blkcipher *cipher, *aux_cipher;
  562. struct xdr_netobj our_hmac_obj;
  563. u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  564. u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  565. int nblocks, blocksize, cbcbytes;
  566. struct decryptor_desc desc;
  567. unsigned int usage;
  568. if (kctx->initiate) {
  569. cipher = kctx->acceptor_enc;
  570. aux_cipher = kctx->acceptor_enc_aux;
  571. cksum_key = kctx->acceptor_integ;
  572. usage = KG_USAGE_ACCEPTOR_SEAL;
  573. } else {
  574. cipher = kctx->initiator_enc;
  575. aux_cipher = kctx->initiator_enc_aux;
  576. cksum_key = kctx->initiator_integ;
  577. usage = KG_USAGE_INITIATOR_SEAL;
  578. }
  579. blocksize = crypto_blkcipher_blocksize(cipher);
  580. /* create a segment skipping the header and leaving out the checksum */
  581. xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
  582. (buf->len - offset - GSS_KRB5_TOK_HDR_LEN -
  583. kctx->gk5e->cksumlength));
  584. nblocks = (subbuf.len + blocksize - 1) / blocksize;
  585. cbcbytes = 0;
  586. if (nblocks > 2)
  587. cbcbytes = (nblocks - 2) * blocksize;
  588. memset(desc.iv, 0, sizeof(desc.iv));
  589. if (cbcbytes) {
  590. desc.fragno = 0;
  591. desc.fraglen = 0;
  592. desc.desc.info = desc.iv;
  593. desc.desc.flags = 0;
  594. desc.desc.tfm = aux_cipher;
  595. sg_init_table(desc.frags, 4);
  596. ret = xdr_process_buf(&subbuf, 0, cbcbytes, decryptor, &desc);
  597. if (ret)
  598. goto out_err;
  599. }
  600. /* Make sure IV carries forward from any CBC results. */
  601. ret = gss_krb5_cts_crypt(cipher, &subbuf, cbcbytes, desc.iv, NULL, 0);
  602. if (ret)
  603. goto out_err;
  604. /* Calculate our hmac over the plaintext data */
  605. our_hmac_obj.len = sizeof(our_hmac);
  606. our_hmac_obj.data = our_hmac;
  607. ret = make_checksum_v2(kctx, NULL, 0, &subbuf, 0,
  608. cksum_key, usage, &our_hmac_obj);
  609. if (ret)
  610. goto out_err;
  611. /* Get the packet's hmac value */
  612. ret = read_bytes_from_xdr_buf(buf, buf->len - kctx->gk5e->cksumlength,
  613. pkt_hmac, kctx->gk5e->cksumlength);
  614. if (ret)
  615. goto out_err;
  616. if (memcmp(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
  617. ret = GSS_S_BAD_SIG;
  618. goto out_err;
  619. }
  620. *headskip = crypto_blkcipher_blocksize(cipher);
  621. *tailskip = kctx->gk5e->cksumlength;
  622. out_err:
  623. if (ret && ret != GSS_S_BAD_SIG)
  624. ret = GSS_S_FAILURE;
  625. return ret;
  626. }