gss_krb5_crypto.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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 <crypto/algapi.h>
  36. #include <crypto/hash.h>
  37. #include <crypto/skcipher.h>
  38. #include <linux/err.h>
  39. #include <linux/types.h>
  40. #include <linux/mm.h>
  41. #include <linux/scatterlist.h>
  42. #include <linux/highmem.h>
  43. #include <linux/pagemap.h>
  44. #include <linux/random.h>
  45. #include <linux/sunrpc/gss_krb5.h>
  46. #include <linux/sunrpc/xdr.h>
  47. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  48. # define RPCDBG_FACILITY RPCDBG_AUTH
  49. #endif
  50. u32
  51. krb5_encrypt(
  52. struct crypto_skcipher *tfm,
  53. void * iv,
  54. void * in,
  55. void * out,
  56. int length)
  57. {
  58. u32 ret = -EINVAL;
  59. struct scatterlist sg[1];
  60. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  61. SKCIPHER_REQUEST_ON_STACK(req, tfm);
  62. if (length % crypto_skcipher_blocksize(tfm) != 0)
  63. goto out;
  64. if (crypto_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  65. dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
  66. crypto_skcipher_ivsize(tfm));
  67. goto out;
  68. }
  69. if (iv)
  70. memcpy(local_iv, iv, crypto_skcipher_ivsize(tfm));
  71. memcpy(out, in, length);
  72. sg_init_one(sg, out, length);
  73. skcipher_request_set_tfm(req, tfm);
  74. skcipher_request_set_callback(req, 0, NULL, NULL);
  75. skcipher_request_set_crypt(req, sg, sg, length, local_iv);
  76. ret = crypto_skcipher_encrypt(req);
  77. skcipher_request_zero(req);
  78. out:
  79. dprintk("RPC: krb5_encrypt returns %d\n", ret);
  80. return ret;
  81. }
  82. u32
  83. krb5_decrypt(
  84. struct crypto_skcipher *tfm,
  85. void * iv,
  86. void * in,
  87. void * out,
  88. int length)
  89. {
  90. u32 ret = -EINVAL;
  91. struct scatterlist sg[1];
  92. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  93. SKCIPHER_REQUEST_ON_STACK(req, tfm);
  94. if (length % crypto_skcipher_blocksize(tfm) != 0)
  95. goto out;
  96. if (crypto_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  97. dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
  98. crypto_skcipher_ivsize(tfm));
  99. goto out;
  100. }
  101. if (iv)
  102. memcpy(local_iv,iv, crypto_skcipher_ivsize(tfm));
  103. memcpy(out, in, length);
  104. sg_init_one(sg, out, length);
  105. skcipher_request_set_tfm(req, tfm);
  106. skcipher_request_set_callback(req, 0, NULL, NULL);
  107. skcipher_request_set_crypt(req, sg, sg, length, local_iv);
  108. ret = crypto_skcipher_decrypt(req);
  109. skcipher_request_zero(req);
  110. out:
  111. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  112. return ret;
  113. }
  114. static int
  115. checksummer(struct scatterlist *sg, void *data)
  116. {
  117. struct ahash_request *req = data;
  118. ahash_request_set_crypt(req, sg, NULL, sg->length);
  119. return crypto_ahash_update(req);
  120. }
  121. static int
  122. arcfour_hmac_md5_usage_to_salt(unsigned int usage, u8 salt[4])
  123. {
  124. unsigned int ms_usage;
  125. switch (usage) {
  126. case KG_USAGE_SIGN:
  127. ms_usage = 15;
  128. break;
  129. case KG_USAGE_SEAL:
  130. ms_usage = 13;
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. salt[0] = (ms_usage >> 0) & 0xff;
  136. salt[1] = (ms_usage >> 8) & 0xff;
  137. salt[2] = (ms_usage >> 16) & 0xff;
  138. salt[3] = (ms_usage >> 24) & 0xff;
  139. return 0;
  140. }
  141. static u32
  142. make_checksum_hmac_md5(struct krb5_ctx *kctx, char *header, int hdrlen,
  143. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  144. unsigned int usage, struct xdr_netobj *cksumout)
  145. {
  146. struct scatterlist sg[1];
  147. int err = -1;
  148. u8 *checksumdata;
  149. u8 rc4salt[4];
  150. struct crypto_ahash *md5;
  151. struct crypto_ahash *hmac_md5;
  152. struct ahash_request *req;
  153. if (cksumkey == NULL)
  154. return GSS_S_FAILURE;
  155. if (cksumout->len < kctx->gk5e->cksumlength) {
  156. dprintk("%s: checksum buffer length, %u, too small for %s\n",
  157. __func__, cksumout->len, kctx->gk5e->name);
  158. return GSS_S_FAILURE;
  159. }
  160. if (arcfour_hmac_md5_usage_to_salt(usage, rc4salt)) {
  161. dprintk("%s: invalid usage value %u\n", __func__, usage);
  162. return GSS_S_FAILURE;
  163. }
  164. checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_NOFS);
  165. if (!checksumdata)
  166. return GSS_S_FAILURE;
  167. md5 = crypto_alloc_ahash("md5", 0, CRYPTO_ALG_ASYNC);
  168. if (IS_ERR(md5))
  169. goto out_free_cksum;
  170. hmac_md5 = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0,
  171. CRYPTO_ALG_ASYNC);
  172. if (IS_ERR(hmac_md5))
  173. goto out_free_md5;
  174. req = ahash_request_alloc(md5, GFP_NOFS);
  175. if (!req)
  176. goto out_free_hmac_md5;
  177. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  178. err = crypto_ahash_init(req);
  179. if (err)
  180. goto out;
  181. sg_init_one(sg, rc4salt, 4);
  182. ahash_request_set_crypt(req, sg, NULL, 4);
  183. err = crypto_ahash_update(req);
  184. if (err)
  185. goto out;
  186. sg_init_one(sg, header, hdrlen);
  187. ahash_request_set_crypt(req, sg, NULL, hdrlen);
  188. err = crypto_ahash_update(req);
  189. if (err)
  190. goto out;
  191. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  192. checksummer, req);
  193. if (err)
  194. goto out;
  195. ahash_request_set_crypt(req, NULL, checksumdata, 0);
  196. err = crypto_ahash_final(req);
  197. if (err)
  198. goto out;
  199. ahash_request_free(req);
  200. req = ahash_request_alloc(hmac_md5, GFP_NOFS);
  201. if (!req)
  202. goto out_free_hmac_md5;
  203. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  204. err = crypto_ahash_setkey(hmac_md5, cksumkey, kctx->gk5e->keylength);
  205. if (err)
  206. goto out;
  207. sg_init_one(sg, checksumdata, crypto_ahash_digestsize(md5));
  208. ahash_request_set_crypt(req, sg, checksumdata,
  209. crypto_ahash_digestsize(md5));
  210. err = crypto_ahash_digest(req);
  211. if (err)
  212. goto out;
  213. memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
  214. cksumout->len = kctx->gk5e->cksumlength;
  215. out:
  216. ahash_request_free(req);
  217. out_free_hmac_md5:
  218. crypto_free_ahash(hmac_md5);
  219. out_free_md5:
  220. crypto_free_ahash(md5);
  221. out_free_cksum:
  222. kfree(checksumdata);
  223. return err ? GSS_S_FAILURE : 0;
  224. }
  225. /*
  226. * checksum the plaintext data and hdrlen bytes of the token header
  227. * The checksum is performed over the first 8 bytes of the
  228. * gss token header and then over the data body
  229. */
  230. u32
  231. make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
  232. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  233. unsigned int usage, struct xdr_netobj *cksumout)
  234. {
  235. struct crypto_ahash *tfm;
  236. struct ahash_request *req;
  237. struct scatterlist sg[1];
  238. int err = -1;
  239. u8 *checksumdata;
  240. unsigned int checksumlen;
  241. if (kctx->gk5e->ctype == CKSUMTYPE_HMAC_MD5_ARCFOUR)
  242. return make_checksum_hmac_md5(kctx, header, hdrlen,
  243. body, body_offset,
  244. cksumkey, usage, cksumout);
  245. if (cksumout->len < kctx->gk5e->cksumlength) {
  246. dprintk("%s: checksum buffer length, %u, too small for %s\n",
  247. __func__, cksumout->len, kctx->gk5e->name);
  248. return GSS_S_FAILURE;
  249. }
  250. checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_NOFS);
  251. if (checksumdata == NULL)
  252. return GSS_S_FAILURE;
  253. tfm = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  254. if (IS_ERR(tfm))
  255. goto out_free_cksum;
  256. req = ahash_request_alloc(tfm, GFP_NOFS);
  257. if (!req)
  258. goto out_free_ahash;
  259. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  260. checksumlen = crypto_ahash_digestsize(tfm);
  261. if (cksumkey != NULL) {
  262. err = crypto_ahash_setkey(tfm, cksumkey,
  263. kctx->gk5e->keylength);
  264. if (err)
  265. goto out;
  266. }
  267. err = crypto_ahash_init(req);
  268. if (err)
  269. goto out;
  270. sg_init_one(sg, header, hdrlen);
  271. ahash_request_set_crypt(req, sg, NULL, hdrlen);
  272. err = crypto_ahash_update(req);
  273. if (err)
  274. goto out;
  275. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  276. checksummer, req);
  277. if (err)
  278. goto out;
  279. ahash_request_set_crypt(req, NULL, checksumdata, 0);
  280. err = crypto_ahash_final(req);
  281. if (err)
  282. goto out;
  283. switch (kctx->gk5e->ctype) {
  284. case CKSUMTYPE_RSA_MD5:
  285. err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata,
  286. checksumdata, checksumlen);
  287. if (err)
  288. goto out;
  289. memcpy(cksumout->data,
  290. checksumdata + checksumlen - kctx->gk5e->cksumlength,
  291. kctx->gk5e->cksumlength);
  292. break;
  293. case CKSUMTYPE_HMAC_SHA1_DES3:
  294. memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
  295. break;
  296. default:
  297. BUG();
  298. break;
  299. }
  300. cksumout->len = kctx->gk5e->cksumlength;
  301. out:
  302. ahash_request_free(req);
  303. out_free_ahash:
  304. crypto_free_ahash(tfm);
  305. out_free_cksum:
  306. kfree(checksumdata);
  307. return err ? GSS_S_FAILURE : 0;
  308. }
  309. /*
  310. * checksum the plaintext data and hdrlen bytes of the token header
  311. * Per rfc4121, sec. 4.2.4, the checksum is performed over the data
  312. * body then over the first 16 octets of the MIC token
  313. * Inclusion of the header data in the calculation of the
  314. * checksum is optional.
  315. */
  316. u32
  317. make_checksum_v2(struct krb5_ctx *kctx, char *header, int hdrlen,
  318. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  319. unsigned int usage, struct xdr_netobj *cksumout)
  320. {
  321. struct crypto_ahash *tfm;
  322. struct ahash_request *req;
  323. struct scatterlist sg[1];
  324. int err = -1;
  325. u8 *checksumdata;
  326. unsigned int checksumlen;
  327. if (kctx->gk5e->keyed_cksum == 0) {
  328. dprintk("%s: expected keyed hash for %s\n",
  329. __func__, kctx->gk5e->name);
  330. return GSS_S_FAILURE;
  331. }
  332. if (cksumkey == NULL) {
  333. dprintk("%s: no key supplied for %s\n",
  334. __func__, kctx->gk5e->name);
  335. return GSS_S_FAILURE;
  336. }
  337. checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_NOFS);
  338. if (!checksumdata)
  339. return GSS_S_FAILURE;
  340. tfm = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  341. if (IS_ERR(tfm))
  342. goto out_free_cksum;
  343. checksumlen = crypto_ahash_digestsize(tfm);
  344. req = ahash_request_alloc(tfm, GFP_NOFS);
  345. if (!req)
  346. goto out_free_ahash;
  347. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  348. err = crypto_ahash_setkey(tfm, cksumkey, kctx->gk5e->keylength);
  349. if (err)
  350. goto out;
  351. err = crypto_ahash_init(req);
  352. if (err)
  353. goto out;
  354. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  355. checksummer, req);
  356. if (err)
  357. goto out;
  358. if (header != NULL) {
  359. sg_init_one(sg, header, hdrlen);
  360. ahash_request_set_crypt(req, sg, NULL, hdrlen);
  361. err = crypto_ahash_update(req);
  362. if (err)
  363. goto out;
  364. }
  365. ahash_request_set_crypt(req, NULL, checksumdata, 0);
  366. err = crypto_ahash_final(req);
  367. if (err)
  368. goto out;
  369. cksumout->len = kctx->gk5e->cksumlength;
  370. switch (kctx->gk5e->ctype) {
  371. case CKSUMTYPE_HMAC_SHA1_96_AES128:
  372. case CKSUMTYPE_HMAC_SHA1_96_AES256:
  373. /* note that this truncates the hash */
  374. memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
  375. break;
  376. default:
  377. BUG();
  378. break;
  379. }
  380. out:
  381. ahash_request_free(req);
  382. out_free_ahash:
  383. crypto_free_ahash(tfm);
  384. out_free_cksum:
  385. kfree(checksumdata);
  386. return err ? GSS_S_FAILURE : 0;
  387. }
  388. struct encryptor_desc {
  389. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  390. struct skcipher_request *req;
  391. int pos;
  392. struct xdr_buf *outbuf;
  393. struct page **pages;
  394. struct scatterlist infrags[4];
  395. struct scatterlist outfrags[4];
  396. int fragno;
  397. int fraglen;
  398. };
  399. static int
  400. encryptor(struct scatterlist *sg, void *data)
  401. {
  402. struct encryptor_desc *desc = data;
  403. struct xdr_buf *outbuf = desc->outbuf;
  404. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(desc->req);
  405. struct page *in_page;
  406. int thislen = desc->fraglen + sg->length;
  407. int fraglen, ret;
  408. int page_pos;
  409. /* Worst case is 4 fragments: head, end of page 1, start
  410. * of page 2, tail. Anything more is a bug. */
  411. BUG_ON(desc->fragno > 3);
  412. page_pos = desc->pos - outbuf->head[0].iov_len;
  413. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  414. /* pages are not in place: */
  415. int i = (page_pos + outbuf->page_base) >> PAGE_SHIFT;
  416. in_page = desc->pages[i];
  417. } else {
  418. in_page = sg_page(sg);
  419. }
  420. sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
  421. sg->offset);
  422. sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
  423. sg->offset);
  424. desc->fragno++;
  425. desc->fraglen += sg->length;
  426. desc->pos += sg->length;
  427. fraglen = thislen & (crypto_skcipher_blocksize(tfm) - 1);
  428. thislen -= fraglen;
  429. if (thislen == 0)
  430. return 0;
  431. sg_mark_end(&desc->infrags[desc->fragno - 1]);
  432. sg_mark_end(&desc->outfrags[desc->fragno - 1]);
  433. skcipher_request_set_crypt(desc->req, desc->infrags, desc->outfrags,
  434. thislen, desc->iv);
  435. ret = crypto_skcipher_encrypt(desc->req);
  436. if (ret)
  437. return ret;
  438. sg_init_table(desc->infrags, 4);
  439. sg_init_table(desc->outfrags, 4);
  440. if (fraglen) {
  441. sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
  442. sg->offset + sg->length - fraglen);
  443. desc->infrags[0] = desc->outfrags[0];
  444. sg_assign_page(&desc->infrags[0], in_page);
  445. desc->fragno = 1;
  446. desc->fraglen = fraglen;
  447. } else {
  448. desc->fragno = 0;
  449. desc->fraglen = 0;
  450. }
  451. return 0;
  452. }
  453. int
  454. gss_encrypt_xdr_buf(struct crypto_skcipher *tfm, struct xdr_buf *buf,
  455. int offset, struct page **pages)
  456. {
  457. int ret;
  458. struct encryptor_desc desc;
  459. SKCIPHER_REQUEST_ON_STACK(req, tfm);
  460. BUG_ON((buf->len - offset) % crypto_skcipher_blocksize(tfm) != 0);
  461. skcipher_request_set_tfm(req, tfm);
  462. skcipher_request_set_callback(req, 0, NULL, NULL);
  463. memset(desc.iv, 0, sizeof(desc.iv));
  464. desc.req = req;
  465. desc.pos = offset;
  466. desc.outbuf = buf;
  467. desc.pages = pages;
  468. desc.fragno = 0;
  469. desc.fraglen = 0;
  470. sg_init_table(desc.infrags, 4);
  471. sg_init_table(desc.outfrags, 4);
  472. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  473. skcipher_request_zero(req);
  474. return ret;
  475. }
  476. struct decryptor_desc {
  477. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  478. struct skcipher_request *req;
  479. struct scatterlist frags[4];
  480. int fragno;
  481. int fraglen;
  482. };
  483. static int
  484. decryptor(struct scatterlist *sg, void *data)
  485. {
  486. struct decryptor_desc *desc = data;
  487. int thislen = desc->fraglen + sg->length;
  488. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(desc->req);
  489. int fraglen, ret;
  490. /* Worst case is 4 fragments: head, end of page 1, start
  491. * of page 2, tail. Anything more is a bug. */
  492. BUG_ON(desc->fragno > 3);
  493. sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
  494. sg->offset);
  495. desc->fragno++;
  496. desc->fraglen += sg->length;
  497. fraglen = thislen & (crypto_skcipher_blocksize(tfm) - 1);
  498. thislen -= fraglen;
  499. if (thislen == 0)
  500. return 0;
  501. sg_mark_end(&desc->frags[desc->fragno - 1]);
  502. skcipher_request_set_crypt(desc->req, desc->frags, desc->frags,
  503. thislen, desc->iv);
  504. ret = crypto_skcipher_decrypt(desc->req);
  505. if (ret)
  506. return ret;
  507. sg_init_table(desc->frags, 4);
  508. if (fraglen) {
  509. sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
  510. sg->offset + sg->length - fraglen);
  511. desc->fragno = 1;
  512. desc->fraglen = fraglen;
  513. } else {
  514. desc->fragno = 0;
  515. desc->fraglen = 0;
  516. }
  517. return 0;
  518. }
  519. int
  520. gss_decrypt_xdr_buf(struct crypto_skcipher *tfm, struct xdr_buf *buf,
  521. int offset)
  522. {
  523. int ret;
  524. struct decryptor_desc desc;
  525. SKCIPHER_REQUEST_ON_STACK(req, tfm);
  526. /* XXXJBF: */
  527. BUG_ON((buf->len - offset) % crypto_skcipher_blocksize(tfm) != 0);
  528. skcipher_request_set_tfm(req, tfm);
  529. skcipher_request_set_callback(req, 0, NULL, NULL);
  530. memset(desc.iv, 0, sizeof(desc.iv));
  531. desc.req = req;
  532. desc.fragno = 0;
  533. desc.fraglen = 0;
  534. sg_init_table(desc.frags, 4);
  535. ret = xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  536. skcipher_request_zero(req);
  537. return ret;
  538. }
  539. /*
  540. * This function makes the assumption that it was ultimately called
  541. * from gss_wrap().
  542. *
  543. * The client auth_gss code moves any existing tail data into a
  544. * separate page before calling gss_wrap.
  545. * The server svcauth_gss code ensures that both the head and the
  546. * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
  547. *
  548. * Even with that guarantee, this function may be called more than
  549. * once in the processing of gss_wrap(). The best we can do is
  550. * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
  551. * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
  552. * At run-time we can verify that a single invocation of this
  553. * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
  554. */
  555. int
  556. xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
  557. {
  558. u8 *p;
  559. if (shiftlen == 0)
  560. return 0;
  561. BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE);
  562. BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
  563. p = buf->head[0].iov_base + base;
  564. memmove(p + shiftlen, p, buf->head[0].iov_len - base);
  565. buf->head[0].iov_len += shiftlen;
  566. buf->len += shiftlen;
  567. return 0;
  568. }
  569. static u32
  570. gss_krb5_cts_crypt(struct crypto_skcipher *cipher, struct xdr_buf *buf,
  571. u32 offset, u8 *iv, struct page **pages, int encrypt)
  572. {
  573. u32 ret;
  574. struct scatterlist sg[1];
  575. SKCIPHER_REQUEST_ON_STACK(req, cipher);
  576. u8 *data;
  577. struct page **save_pages;
  578. u32 len = buf->len - offset;
  579. if (len > GSS_KRB5_MAX_BLOCKSIZE * 2) {
  580. WARN_ON(0);
  581. return -ENOMEM;
  582. }
  583. data = kmalloc(GSS_KRB5_MAX_BLOCKSIZE * 2, GFP_NOFS);
  584. if (!data)
  585. return -ENOMEM;
  586. /*
  587. * For encryption, we want to read from the cleartext
  588. * page cache pages, and write the encrypted data to
  589. * the supplied xdr_buf pages.
  590. */
  591. save_pages = buf->pages;
  592. if (encrypt)
  593. buf->pages = pages;
  594. ret = read_bytes_from_xdr_buf(buf, offset, data, len);
  595. buf->pages = save_pages;
  596. if (ret)
  597. goto out;
  598. sg_init_one(sg, data, len);
  599. skcipher_request_set_tfm(req, cipher);
  600. skcipher_request_set_callback(req, 0, NULL, NULL);
  601. skcipher_request_set_crypt(req, sg, sg, len, iv);
  602. if (encrypt)
  603. ret = crypto_skcipher_encrypt(req);
  604. else
  605. ret = crypto_skcipher_decrypt(req);
  606. skcipher_request_zero(req);
  607. if (ret)
  608. goto out;
  609. ret = write_bytes_to_xdr_buf(buf, offset, data, len);
  610. out:
  611. kfree(data);
  612. return ret;
  613. }
  614. u32
  615. gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
  616. struct xdr_buf *buf, struct page **pages)
  617. {
  618. u32 err;
  619. struct xdr_netobj hmac;
  620. u8 *cksumkey;
  621. u8 *ecptr;
  622. struct crypto_skcipher *cipher, *aux_cipher;
  623. int blocksize;
  624. struct page **save_pages;
  625. int nblocks, nbytes;
  626. struct encryptor_desc desc;
  627. u32 cbcbytes;
  628. unsigned int usage;
  629. if (kctx->initiate) {
  630. cipher = kctx->initiator_enc;
  631. aux_cipher = kctx->initiator_enc_aux;
  632. cksumkey = kctx->initiator_integ;
  633. usage = KG_USAGE_INITIATOR_SEAL;
  634. } else {
  635. cipher = kctx->acceptor_enc;
  636. aux_cipher = kctx->acceptor_enc_aux;
  637. cksumkey = kctx->acceptor_integ;
  638. usage = KG_USAGE_ACCEPTOR_SEAL;
  639. }
  640. blocksize = crypto_skcipher_blocksize(cipher);
  641. /* hide the gss token header and insert the confounder */
  642. offset += GSS_KRB5_TOK_HDR_LEN;
  643. if (xdr_extend_head(buf, offset, kctx->gk5e->conflen))
  644. return GSS_S_FAILURE;
  645. gss_krb5_make_confounder(buf->head[0].iov_base + offset, kctx->gk5e->conflen);
  646. offset -= GSS_KRB5_TOK_HDR_LEN;
  647. if (buf->tail[0].iov_base != NULL) {
  648. ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
  649. } else {
  650. buf->tail[0].iov_base = buf->head[0].iov_base
  651. + buf->head[0].iov_len;
  652. buf->tail[0].iov_len = 0;
  653. ecptr = buf->tail[0].iov_base;
  654. }
  655. /* copy plaintext gss token header after filler (if any) */
  656. memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
  657. buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
  658. buf->len += GSS_KRB5_TOK_HDR_LEN;
  659. /* Do the HMAC */
  660. hmac.len = GSS_KRB5_MAX_CKSUM_LEN;
  661. hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  662. /*
  663. * When we are called, pages points to the real page cache
  664. * data -- which we can't go and encrypt! buf->pages points
  665. * to scratch pages which we are going to send off to the
  666. * client/server. Swap in the plaintext pages to calculate
  667. * the hmac.
  668. */
  669. save_pages = buf->pages;
  670. buf->pages = pages;
  671. err = make_checksum_v2(kctx, NULL, 0, buf,
  672. offset + GSS_KRB5_TOK_HDR_LEN,
  673. cksumkey, usage, &hmac);
  674. buf->pages = save_pages;
  675. if (err)
  676. return GSS_S_FAILURE;
  677. nbytes = buf->len - offset - GSS_KRB5_TOK_HDR_LEN;
  678. nblocks = (nbytes + blocksize - 1) / blocksize;
  679. cbcbytes = 0;
  680. if (nblocks > 2)
  681. cbcbytes = (nblocks - 2) * blocksize;
  682. memset(desc.iv, 0, sizeof(desc.iv));
  683. if (cbcbytes) {
  684. SKCIPHER_REQUEST_ON_STACK(req, aux_cipher);
  685. desc.pos = offset + GSS_KRB5_TOK_HDR_LEN;
  686. desc.fragno = 0;
  687. desc.fraglen = 0;
  688. desc.pages = pages;
  689. desc.outbuf = buf;
  690. desc.req = req;
  691. skcipher_request_set_tfm(req, aux_cipher);
  692. skcipher_request_set_callback(req, 0, NULL, NULL);
  693. sg_init_table(desc.infrags, 4);
  694. sg_init_table(desc.outfrags, 4);
  695. err = xdr_process_buf(buf, offset + GSS_KRB5_TOK_HDR_LEN,
  696. cbcbytes, encryptor, &desc);
  697. skcipher_request_zero(req);
  698. if (err)
  699. goto out_err;
  700. }
  701. /* Make sure IV carries forward from any CBC results. */
  702. err = gss_krb5_cts_crypt(cipher, buf,
  703. offset + GSS_KRB5_TOK_HDR_LEN + cbcbytes,
  704. desc.iv, pages, 1);
  705. if (err) {
  706. err = GSS_S_FAILURE;
  707. goto out_err;
  708. }
  709. /* Now update buf to account for HMAC */
  710. buf->tail[0].iov_len += kctx->gk5e->cksumlength;
  711. buf->len += kctx->gk5e->cksumlength;
  712. out_err:
  713. if (err)
  714. err = GSS_S_FAILURE;
  715. return err;
  716. }
  717. u32
  718. gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf,
  719. u32 *headskip, u32 *tailskip)
  720. {
  721. struct xdr_buf subbuf;
  722. u32 ret = 0;
  723. u8 *cksum_key;
  724. struct crypto_skcipher *cipher, *aux_cipher;
  725. struct xdr_netobj our_hmac_obj;
  726. u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  727. u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  728. int nblocks, blocksize, cbcbytes;
  729. struct decryptor_desc desc;
  730. unsigned int usage;
  731. if (kctx->initiate) {
  732. cipher = kctx->acceptor_enc;
  733. aux_cipher = kctx->acceptor_enc_aux;
  734. cksum_key = kctx->acceptor_integ;
  735. usage = KG_USAGE_ACCEPTOR_SEAL;
  736. } else {
  737. cipher = kctx->initiator_enc;
  738. aux_cipher = kctx->initiator_enc_aux;
  739. cksum_key = kctx->initiator_integ;
  740. usage = KG_USAGE_INITIATOR_SEAL;
  741. }
  742. blocksize = crypto_skcipher_blocksize(cipher);
  743. /* create a segment skipping the header and leaving out the checksum */
  744. xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
  745. (buf->len - offset - GSS_KRB5_TOK_HDR_LEN -
  746. kctx->gk5e->cksumlength));
  747. nblocks = (subbuf.len + blocksize - 1) / blocksize;
  748. cbcbytes = 0;
  749. if (nblocks > 2)
  750. cbcbytes = (nblocks - 2) * blocksize;
  751. memset(desc.iv, 0, sizeof(desc.iv));
  752. if (cbcbytes) {
  753. SKCIPHER_REQUEST_ON_STACK(req, aux_cipher);
  754. desc.fragno = 0;
  755. desc.fraglen = 0;
  756. desc.req = req;
  757. skcipher_request_set_tfm(req, aux_cipher);
  758. skcipher_request_set_callback(req, 0, NULL, NULL);
  759. sg_init_table(desc.frags, 4);
  760. ret = xdr_process_buf(&subbuf, 0, cbcbytes, decryptor, &desc);
  761. skcipher_request_zero(req);
  762. if (ret)
  763. goto out_err;
  764. }
  765. /* Make sure IV carries forward from any CBC results. */
  766. ret = gss_krb5_cts_crypt(cipher, &subbuf, cbcbytes, desc.iv, NULL, 0);
  767. if (ret)
  768. goto out_err;
  769. /* Calculate our hmac over the plaintext data */
  770. our_hmac_obj.len = sizeof(our_hmac);
  771. our_hmac_obj.data = our_hmac;
  772. ret = make_checksum_v2(kctx, NULL, 0, &subbuf, 0,
  773. cksum_key, usage, &our_hmac_obj);
  774. if (ret)
  775. goto out_err;
  776. /* Get the packet's hmac value */
  777. ret = read_bytes_from_xdr_buf(buf, buf->len - kctx->gk5e->cksumlength,
  778. pkt_hmac, kctx->gk5e->cksumlength);
  779. if (ret)
  780. goto out_err;
  781. if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
  782. ret = GSS_S_BAD_SIG;
  783. goto out_err;
  784. }
  785. *headskip = kctx->gk5e->conflen;
  786. *tailskip = kctx->gk5e->cksumlength;
  787. out_err:
  788. if (ret && ret != GSS_S_BAD_SIG)
  789. ret = GSS_S_FAILURE;
  790. return ret;
  791. }
  792. /*
  793. * Compute Kseq given the initial session key and the checksum.
  794. * Set the key of the given cipher.
  795. */
  796. int
  797. krb5_rc4_setup_seq_key(struct krb5_ctx *kctx, struct crypto_skcipher *cipher,
  798. unsigned char *cksum)
  799. {
  800. struct crypto_shash *hmac;
  801. struct shash_desc *desc;
  802. u8 Kseq[GSS_KRB5_MAX_KEYLEN];
  803. u32 zeroconstant = 0;
  804. int err;
  805. dprintk("%s: entered\n", __func__);
  806. hmac = crypto_alloc_shash(kctx->gk5e->cksum_name, 0, 0);
  807. if (IS_ERR(hmac)) {
  808. dprintk("%s: error %ld, allocating hash '%s'\n",
  809. __func__, PTR_ERR(hmac), kctx->gk5e->cksum_name);
  810. return PTR_ERR(hmac);
  811. }
  812. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac),
  813. GFP_NOFS);
  814. if (!desc) {
  815. dprintk("%s: failed to allocate shash descriptor for '%s'\n",
  816. __func__, kctx->gk5e->cksum_name);
  817. crypto_free_shash(hmac);
  818. return -ENOMEM;
  819. }
  820. desc->tfm = hmac;
  821. desc->flags = 0;
  822. /* Compute intermediate Kseq from session key */
  823. err = crypto_shash_setkey(hmac, kctx->Ksess, kctx->gk5e->keylength);
  824. if (err)
  825. goto out_err;
  826. err = crypto_shash_digest(desc, (u8 *)&zeroconstant, 4, Kseq);
  827. if (err)
  828. goto out_err;
  829. /* Compute final Kseq from the checksum and intermediate Kseq */
  830. err = crypto_shash_setkey(hmac, Kseq, kctx->gk5e->keylength);
  831. if (err)
  832. goto out_err;
  833. err = crypto_shash_digest(desc, cksum, 8, Kseq);
  834. if (err)
  835. goto out_err;
  836. err = crypto_skcipher_setkey(cipher, Kseq, kctx->gk5e->keylength);
  837. if (err)
  838. goto out_err;
  839. err = 0;
  840. out_err:
  841. kzfree(desc);
  842. crypto_free_shash(hmac);
  843. dprintk("%s: returning %d\n", __func__, err);
  844. return err;
  845. }
  846. /*
  847. * Compute Kcrypt given the initial session key and the plaintext seqnum.
  848. * Set the key of cipher kctx->enc.
  849. */
  850. int
  851. krb5_rc4_setup_enc_key(struct krb5_ctx *kctx, struct crypto_skcipher *cipher,
  852. s32 seqnum)
  853. {
  854. struct crypto_shash *hmac;
  855. struct shash_desc *desc;
  856. u8 Kcrypt[GSS_KRB5_MAX_KEYLEN];
  857. u8 zeroconstant[4] = {0};
  858. u8 seqnumarray[4];
  859. int err, i;
  860. dprintk("%s: entered, seqnum %u\n", __func__, seqnum);
  861. hmac = crypto_alloc_shash(kctx->gk5e->cksum_name, 0, 0);
  862. if (IS_ERR(hmac)) {
  863. dprintk("%s: error %ld, allocating hash '%s'\n",
  864. __func__, PTR_ERR(hmac), kctx->gk5e->cksum_name);
  865. return PTR_ERR(hmac);
  866. }
  867. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(hmac),
  868. GFP_NOFS);
  869. if (!desc) {
  870. dprintk("%s: failed to allocate shash descriptor for '%s'\n",
  871. __func__, kctx->gk5e->cksum_name);
  872. crypto_free_shash(hmac);
  873. return -ENOMEM;
  874. }
  875. desc->tfm = hmac;
  876. desc->flags = 0;
  877. /* Compute intermediate Kcrypt from session key */
  878. for (i = 0; i < kctx->gk5e->keylength; i++)
  879. Kcrypt[i] = kctx->Ksess[i] ^ 0xf0;
  880. err = crypto_shash_setkey(hmac, Kcrypt, kctx->gk5e->keylength);
  881. if (err)
  882. goto out_err;
  883. err = crypto_shash_digest(desc, zeroconstant, 4, Kcrypt);
  884. if (err)
  885. goto out_err;
  886. /* Compute final Kcrypt from the seqnum and intermediate Kcrypt */
  887. err = crypto_shash_setkey(hmac, Kcrypt, kctx->gk5e->keylength);
  888. if (err)
  889. goto out_err;
  890. seqnumarray[0] = (unsigned char) ((seqnum >> 24) & 0xff);
  891. seqnumarray[1] = (unsigned char) ((seqnum >> 16) & 0xff);
  892. seqnumarray[2] = (unsigned char) ((seqnum >> 8) & 0xff);
  893. seqnumarray[3] = (unsigned char) ((seqnum >> 0) & 0xff);
  894. err = crypto_shash_digest(desc, seqnumarray, 4, Kcrypt);
  895. if (err)
  896. goto out_err;
  897. err = crypto_skcipher_setkey(cipher, Kcrypt, kctx->gk5e->keylength);
  898. if (err)
  899. goto out_err;
  900. err = 0;
  901. out_err:
  902. kzfree(desc);
  903. crypto_free_shash(hmac);
  904. dprintk("%s: returning %d\n", __func__, err);
  905. return err;
  906. }