gss_krb5_crypto.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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/hash.h>
  36. #include <crypto/skcipher.h>
  37. #include <crypto/utils.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. #include <kunit/visibility.h>
  48. #include "gss_krb5_internal.h"
  49. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  50. # define RPCDBG_FACILITY RPCDBG_AUTH
  51. #endif
  52. /**
  53. * krb5_make_confounder - Generate a confounder string
  54. * @p: memory location into which to write the string
  55. * @conflen: string length to write, in octets
  56. *
  57. * RFCs 1964 and 3961 mention only "a random confounder" without going
  58. * into detail about its function or cryptographic requirements. The
  59. * assumed purpose is to prevent repeated encryption of a plaintext with
  60. * the same key from generating the same ciphertext. It is also used to
  61. * pad minimum plaintext length to at least a single cipher block.
  62. *
  63. * However, in situations like the GSS Kerberos 5 mechanism, where the
  64. * encryption IV is always all zeroes, the confounder also effectively
  65. * functions like an IV. Thus, not only must it be unique from message
  66. * to message, but it must also be difficult to predict. Otherwise an
  67. * attacker can correlate the confounder to previous or future values,
  68. * making the encryption easier to break.
  69. *
  70. * Given that the primary consumer of this encryption mechanism is a
  71. * network storage protocol, a type of traffic that often carries
  72. * predictable payloads (eg, all zeroes when reading unallocated blocks
  73. * from a file), our confounder generation has to be cryptographically
  74. * strong.
  75. */
  76. void krb5_make_confounder(u8 *p, int conflen)
  77. {
  78. get_random_bytes(p, conflen);
  79. }
  80. /**
  81. * krb5_encrypt - simple encryption of an RPCSEC GSS payload
  82. * @tfm: initialized cipher transform
  83. * @iv: pointer to an IV
  84. * @in: plaintext to encrypt
  85. * @out: OUT: ciphertext
  86. * @length: length of input and output buffers, in bytes
  87. *
  88. * @iv may be NULL to force the use of an all-zero IV.
  89. * The buffer containing the IV must be as large as the
  90. * cipher's ivsize.
  91. *
  92. * Return values:
  93. * %0: @in successfully encrypted into @out
  94. * negative errno: @in not encrypted
  95. */
  96. u32
  97. krb5_encrypt(
  98. struct crypto_sync_skcipher *tfm,
  99. void * iv,
  100. void * in,
  101. void * out,
  102. int length)
  103. {
  104. u32 ret = -EINVAL;
  105. struct scatterlist sg[1];
  106. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  107. SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
  108. if (length % crypto_sync_skcipher_blocksize(tfm) != 0)
  109. goto out;
  110. if (crypto_sync_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  111. dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
  112. crypto_sync_skcipher_ivsize(tfm));
  113. goto out;
  114. }
  115. if (iv)
  116. memcpy(local_iv, iv, crypto_sync_skcipher_ivsize(tfm));
  117. memcpy(out, in, length);
  118. sg_init_one(sg, out, length);
  119. skcipher_request_set_sync_tfm(req, tfm);
  120. skcipher_request_set_callback(req, 0, NULL, NULL);
  121. skcipher_request_set_crypt(req, sg, sg, length, local_iv);
  122. ret = crypto_skcipher_encrypt(req);
  123. skcipher_request_zero(req);
  124. out:
  125. dprintk("RPC: krb5_encrypt returns %d\n", ret);
  126. return ret;
  127. }
  128. /**
  129. * krb5_decrypt - simple decryption of an RPCSEC GSS payload
  130. * @tfm: initialized cipher transform
  131. * @iv: pointer to an IV
  132. * @in: ciphertext to decrypt
  133. * @out: OUT: plaintext
  134. * @length: length of input and output buffers, in bytes
  135. *
  136. * @iv may be NULL to force the use of an all-zero IV.
  137. * The buffer containing the IV must be as large as the
  138. * cipher's ivsize.
  139. *
  140. * Return values:
  141. * %0: @in successfully decrypted into @out
  142. * negative errno: @in not decrypted
  143. */
  144. u32
  145. krb5_decrypt(
  146. struct crypto_sync_skcipher *tfm,
  147. void * iv,
  148. void * in,
  149. void * out,
  150. int length)
  151. {
  152. u32 ret = -EINVAL;
  153. struct scatterlist sg[1];
  154. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  155. SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
  156. if (length % crypto_sync_skcipher_blocksize(tfm) != 0)
  157. goto out;
  158. if (crypto_sync_skcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  159. dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
  160. crypto_sync_skcipher_ivsize(tfm));
  161. goto out;
  162. }
  163. if (iv)
  164. memcpy(local_iv, iv, crypto_sync_skcipher_ivsize(tfm));
  165. memcpy(out, in, length);
  166. sg_init_one(sg, out, length);
  167. skcipher_request_set_sync_tfm(req, tfm);
  168. skcipher_request_set_callback(req, 0, NULL, NULL);
  169. skcipher_request_set_crypt(req, sg, sg, length, local_iv);
  170. ret = crypto_skcipher_decrypt(req);
  171. skcipher_request_zero(req);
  172. out:
  173. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  174. return ret;
  175. }
  176. static int
  177. checksummer(struct scatterlist *sg, void *data)
  178. {
  179. struct ahash_request *req = data;
  180. ahash_request_set_crypt(req, sg, NULL, sg->length);
  181. return crypto_ahash_update(req);
  182. }
  183. /*
  184. * checksum the plaintext data and hdrlen bytes of the token header
  185. * The checksum is performed over the first 8 bytes of the
  186. * gss token header and then over the data body
  187. */
  188. u32
  189. make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
  190. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  191. unsigned int usage, struct xdr_netobj *cksumout)
  192. {
  193. struct crypto_ahash *tfm;
  194. struct ahash_request *req;
  195. struct scatterlist sg[1];
  196. int err = -1;
  197. u8 *checksumdata;
  198. unsigned int checksumlen;
  199. if (cksumout->len < kctx->gk5e->cksumlength) {
  200. dprintk("%s: checksum buffer length, %u, too small for %s\n",
  201. __func__, cksumout->len, kctx->gk5e->name);
  202. return GSS_S_FAILURE;
  203. }
  204. checksumdata = kmalloc(GSS_KRB5_MAX_CKSUM_LEN, GFP_KERNEL);
  205. if (checksumdata == NULL)
  206. return GSS_S_FAILURE;
  207. tfm = crypto_alloc_ahash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  208. if (IS_ERR(tfm))
  209. goto out_free_cksum;
  210. req = ahash_request_alloc(tfm, GFP_KERNEL);
  211. if (!req)
  212. goto out_free_ahash;
  213. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  214. checksumlen = crypto_ahash_digestsize(tfm);
  215. if (cksumkey != NULL) {
  216. err = crypto_ahash_setkey(tfm, cksumkey,
  217. kctx->gk5e->keylength);
  218. if (err)
  219. goto out;
  220. }
  221. err = crypto_ahash_init(req);
  222. if (err)
  223. goto out;
  224. sg_init_one(sg, header, hdrlen);
  225. ahash_request_set_crypt(req, sg, NULL, hdrlen);
  226. err = crypto_ahash_update(req);
  227. if (err)
  228. goto out;
  229. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  230. checksummer, req);
  231. if (err)
  232. goto out;
  233. ahash_request_set_crypt(req, NULL, checksumdata, 0);
  234. err = crypto_ahash_final(req);
  235. if (err)
  236. goto out;
  237. switch (kctx->gk5e->ctype) {
  238. case CKSUMTYPE_RSA_MD5:
  239. err = krb5_encrypt(kctx->seq, NULL, checksumdata,
  240. checksumdata, checksumlen);
  241. if (err)
  242. goto out;
  243. memcpy(cksumout->data,
  244. checksumdata + checksumlen - kctx->gk5e->cksumlength,
  245. kctx->gk5e->cksumlength);
  246. break;
  247. case CKSUMTYPE_HMAC_SHA1_DES3:
  248. memcpy(cksumout->data, checksumdata, kctx->gk5e->cksumlength);
  249. break;
  250. default:
  251. BUG();
  252. break;
  253. }
  254. cksumout->len = kctx->gk5e->cksumlength;
  255. out:
  256. ahash_request_free(req);
  257. out_free_ahash:
  258. crypto_free_ahash(tfm);
  259. out_free_cksum:
  260. kfree(checksumdata);
  261. return err ? GSS_S_FAILURE : 0;
  262. }
  263. /**
  264. * gss_krb5_checksum - Compute the MAC for a GSS Wrap or MIC token
  265. * @tfm: an initialized hash transform
  266. * @header: pointer to a buffer containing the token header, or NULL
  267. * @hdrlen: number of octets in @header
  268. * @body: xdr_buf containing an RPC message (body.len is the message length)
  269. * @body_offset: byte offset into @body to start checksumming
  270. * @cksumout: OUT: a buffer to be filled in with the computed HMAC
  271. *
  272. * Usually expressed as H = HMAC(K, message)[1..h] .
  273. *
  274. * Caller provides the truncation length of the output token (h) in
  275. * cksumout.len.
  276. *
  277. * Return values:
  278. * %GSS_S_COMPLETE: Digest computed, @cksumout filled in
  279. * %GSS_S_FAILURE: Call failed
  280. */
  281. u32
  282. gss_krb5_checksum(struct crypto_ahash *tfm, char *header, int hdrlen,
  283. const struct xdr_buf *body, int body_offset,
  284. struct xdr_netobj *cksumout)
  285. {
  286. struct ahash_request *req;
  287. int err = -ENOMEM;
  288. u8 *checksumdata;
  289. checksumdata = kmalloc(crypto_ahash_digestsize(tfm), GFP_KERNEL);
  290. if (!checksumdata)
  291. return GSS_S_FAILURE;
  292. req = ahash_request_alloc(tfm, GFP_KERNEL);
  293. if (!req)
  294. goto out_free_cksum;
  295. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  296. err = crypto_ahash_init(req);
  297. if (err)
  298. goto out_free_ahash;
  299. /*
  300. * Per RFC 4121 Section 4.2.4, the checksum is performed over the
  301. * data body first, then over the octets in "header".
  302. */
  303. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  304. checksummer, req);
  305. if (err)
  306. goto out_free_ahash;
  307. if (header) {
  308. struct scatterlist sg[1];
  309. sg_init_one(sg, header, hdrlen);
  310. ahash_request_set_crypt(req, sg, NULL, hdrlen);
  311. err = crypto_ahash_update(req);
  312. if (err)
  313. goto out_free_ahash;
  314. }
  315. ahash_request_set_crypt(req, NULL, checksumdata, 0);
  316. err = crypto_ahash_final(req);
  317. if (err)
  318. goto out_free_ahash;
  319. memcpy(cksumout->data, checksumdata,
  320. min_t(int, cksumout->len, crypto_ahash_digestsize(tfm)));
  321. out_free_ahash:
  322. ahash_request_free(req);
  323. out_free_cksum:
  324. kfree_sensitive(checksumdata);
  325. return err ? GSS_S_FAILURE : GSS_S_COMPLETE;
  326. }
  327. EXPORT_SYMBOL_IF_KUNIT(gss_krb5_checksum);
  328. struct encryptor_desc {
  329. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  330. struct skcipher_request *req;
  331. int pos;
  332. struct xdr_buf *outbuf;
  333. struct page **pages;
  334. struct scatterlist infrags[4];
  335. struct scatterlist outfrags[4];
  336. int fragno;
  337. int fraglen;
  338. };
  339. static int
  340. encryptor(struct scatterlist *sg, void *data)
  341. {
  342. struct encryptor_desc *desc = data;
  343. struct xdr_buf *outbuf = desc->outbuf;
  344. struct crypto_sync_skcipher *tfm =
  345. crypto_sync_skcipher_reqtfm(desc->req);
  346. struct page *in_page;
  347. int thislen = desc->fraglen + sg->length;
  348. int fraglen, ret;
  349. int page_pos;
  350. /* Worst case is 4 fragments: head, end of page 1, start
  351. * of page 2, tail. Anything more is a bug. */
  352. BUG_ON(desc->fragno > 3);
  353. page_pos = desc->pos - outbuf->head[0].iov_len;
  354. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  355. /* pages are not in place: */
  356. int i = (page_pos + outbuf->page_base) >> PAGE_SHIFT;
  357. in_page = desc->pages[i];
  358. } else {
  359. in_page = sg_page(sg);
  360. }
  361. sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
  362. sg->offset);
  363. sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
  364. sg->offset);
  365. desc->fragno++;
  366. desc->fraglen += sg->length;
  367. desc->pos += sg->length;
  368. fraglen = thislen & (crypto_sync_skcipher_blocksize(tfm) - 1);
  369. thislen -= fraglen;
  370. if (thislen == 0)
  371. return 0;
  372. sg_mark_end(&desc->infrags[desc->fragno - 1]);
  373. sg_mark_end(&desc->outfrags[desc->fragno - 1]);
  374. skcipher_request_set_crypt(desc->req, desc->infrags, desc->outfrags,
  375. thislen, desc->iv);
  376. ret = crypto_skcipher_encrypt(desc->req);
  377. if (ret)
  378. return ret;
  379. sg_init_table(desc->infrags, 4);
  380. sg_init_table(desc->outfrags, 4);
  381. if (fraglen) {
  382. sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
  383. sg->offset + sg->length - fraglen);
  384. desc->infrags[0] = desc->outfrags[0];
  385. sg_assign_page(&desc->infrags[0], in_page);
  386. desc->fragno = 1;
  387. desc->fraglen = fraglen;
  388. } else {
  389. desc->fragno = 0;
  390. desc->fraglen = 0;
  391. }
  392. return 0;
  393. }
  394. int
  395. gss_encrypt_xdr_buf(struct crypto_sync_skcipher *tfm, struct xdr_buf *buf,
  396. int offset, struct page **pages)
  397. {
  398. int ret;
  399. struct encryptor_desc desc;
  400. SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
  401. BUG_ON((buf->len - offset) % crypto_sync_skcipher_blocksize(tfm) != 0);
  402. skcipher_request_set_sync_tfm(req, tfm);
  403. skcipher_request_set_callback(req, 0, NULL, NULL);
  404. memset(desc.iv, 0, sizeof(desc.iv));
  405. desc.req = req;
  406. desc.pos = offset;
  407. desc.outbuf = buf;
  408. desc.pages = pages;
  409. desc.fragno = 0;
  410. desc.fraglen = 0;
  411. sg_init_table(desc.infrags, 4);
  412. sg_init_table(desc.outfrags, 4);
  413. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  414. skcipher_request_zero(req);
  415. return ret;
  416. }
  417. struct decryptor_desc {
  418. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  419. struct skcipher_request *req;
  420. struct scatterlist frags[4];
  421. int fragno;
  422. int fraglen;
  423. };
  424. static int
  425. decryptor(struct scatterlist *sg, void *data)
  426. {
  427. struct decryptor_desc *desc = data;
  428. int thislen = desc->fraglen + sg->length;
  429. struct crypto_sync_skcipher *tfm =
  430. crypto_sync_skcipher_reqtfm(desc->req);
  431. int fraglen, ret;
  432. /* Worst case is 4 fragments: head, end of page 1, start
  433. * of page 2, tail. Anything more is a bug. */
  434. BUG_ON(desc->fragno > 3);
  435. sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
  436. sg->offset);
  437. desc->fragno++;
  438. desc->fraglen += sg->length;
  439. fraglen = thislen & (crypto_sync_skcipher_blocksize(tfm) - 1);
  440. thislen -= fraglen;
  441. if (thislen == 0)
  442. return 0;
  443. sg_mark_end(&desc->frags[desc->fragno - 1]);
  444. skcipher_request_set_crypt(desc->req, desc->frags, desc->frags,
  445. thislen, desc->iv);
  446. ret = crypto_skcipher_decrypt(desc->req);
  447. if (ret)
  448. return ret;
  449. sg_init_table(desc->frags, 4);
  450. if (fraglen) {
  451. sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
  452. sg->offset + sg->length - fraglen);
  453. desc->fragno = 1;
  454. desc->fraglen = fraglen;
  455. } else {
  456. desc->fragno = 0;
  457. desc->fraglen = 0;
  458. }
  459. return 0;
  460. }
  461. int
  462. gss_decrypt_xdr_buf(struct crypto_sync_skcipher *tfm, struct xdr_buf *buf,
  463. int offset)
  464. {
  465. int ret;
  466. struct decryptor_desc desc;
  467. SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm);
  468. /* XXXJBF: */
  469. BUG_ON((buf->len - offset) % crypto_sync_skcipher_blocksize(tfm) != 0);
  470. skcipher_request_set_sync_tfm(req, tfm);
  471. skcipher_request_set_callback(req, 0, NULL, NULL);
  472. memset(desc.iv, 0, sizeof(desc.iv));
  473. desc.req = req;
  474. desc.fragno = 0;
  475. desc.fraglen = 0;
  476. sg_init_table(desc.frags, 4);
  477. ret = xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  478. skcipher_request_zero(req);
  479. return ret;
  480. }
  481. /*
  482. * This function makes the assumption that it was ultimately called
  483. * from gss_wrap().
  484. *
  485. * The client auth_gss code moves any existing tail data into a
  486. * separate page before calling gss_wrap.
  487. * The server svcauth_gss code ensures that both the head and the
  488. * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
  489. *
  490. * Even with that guarantee, this function may be called more than
  491. * once in the processing of gss_wrap(). The best we can do is
  492. * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
  493. * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
  494. * At run-time we can verify that a single invocation of this
  495. * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
  496. */
  497. int
  498. xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
  499. {
  500. u8 *p;
  501. if (shiftlen == 0)
  502. return 0;
  503. BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
  504. p = buf->head[0].iov_base + base;
  505. memmove(p + shiftlen, p, buf->head[0].iov_len - base);
  506. buf->head[0].iov_len += shiftlen;
  507. buf->len += shiftlen;
  508. return 0;
  509. }
  510. static u32
  511. gss_krb5_cts_crypt(struct crypto_sync_skcipher *cipher, struct xdr_buf *buf,
  512. u32 offset, u8 *iv, struct page **pages, int encrypt)
  513. {
  514. u32 ret;
  515. struct scatterlist sg[1];
  516. SYNC_SKCIPHER_REQUEST_ON_STACK(req, cipher);
  517. u8 *data;
  518. struct page **save_pages;
  519. u32 len = buf->len - offset;
  520. if (len > GSS_KRB5_MAX_BLOCKSIZE * 2) {
  521. WARN_ON(0);
  522. return -ENOMEM;
  523. }
  524. data = kmalloc(GSS_KRB5_MAX_BLOCKSIZE * 2, GFP_KERNEL);
  525. if (!data)
  526. return -ENOMEM;
  527. /*
  528. * For encryption, we want to read from the cleartext
  529. * page cache pages, and write the encrypted data to
  530. * the supplied xdr_buf pages.
  531. */
  532. save_pages = buf->pages;
  533. if (encrypt)
  534. buf->pages = pages;
  535. ret = read_bytes_from_xdr_buf(buf, offset, data, len);
  536. buf->pages = save_pages;
  537. if (ret)
  538. goto out;
  539. sg_init_one(sg, data, len);
  540. skcipher_request_set_sync_tfm(req, cipher);
  541. skcipher_request_set_callback(req, 0, NULL, NULL);
  542. skcipher_request_set_crypt(req, sg, sg, len, iv);
  543. if (encrypt)
  544. ret = crypto_skcipher_encrypt(req);
  545. else
  546. ret = crypto_skcipher_decrypt(req);
  547. skcipher_request_zero(req);
  548. if (ret)
  549. goto out;
  550. ret = write_bytes_to_xdr_buf(buf, offset, data, len);
  551. #if IS_ENABLED(CONFIG_KUNIT)
  552. /*
  553. * CBC-CTS does not define an output IV but RFC 3962 defines it as the
  554. * penultimate block of ciphertext, so copy that into the IV buffer
  555. * before returning.
  556. */
  557. if (encrypt)
  558. memcpy(iv, data, crypto_sync_skcipher_ivsize(cipher));
  559. #endif
  560. out:
  561. kfree(data);
  562. return ret;
  563. }
  564. /**
  565. * krb5_cbc_cts_encrypt - encrypt in CBC mode with CTS
  566. * @cts_tfm: CBC cipher with CTS
  567. * @cbc_tfm: base CBC cipher
  568. * @offset: starting byte offset for plaintext
  569. * @buf: OUT: output buffer
  570. * @pages: plaintext
  571. * @iv: output CBC initialization vector, or NULL
  572. * @ivsize: size of @iv, in octets
  573. *
  574. * To provide confidentiality, encrypt using cipher block chaining
  575. * with ciphertext stealing. Message integrity is handled separately.
  576. *
  577. * Return values:
  578. * %0: encryption successful
  579. * negative errno: encryption could not be completed
  580. */
  581. VISIBLE_IF_KUNIT
  582. int krb5_cbc_cts_encrypt(struct crypto_sync_skcipher *cts_tfm,
  583. struct crypto_sync_skcipher *cbc_tfm,
  584. u32 offset, struct xdr_buf *buf, struct page **pages,
  585. u8 *iv, unsigned int ivsize)
  586. {
  587. u32 blocksize, nbytes, nblocks, cbcbytes;
  588. struct encryptor_desc desc;
  589. int err;
  590. blocksize = crypto_sync_skcipher_blocksize(cts_tfm);
  591. nbytes = buf->len - offset;
  592. nblocks = (nbytes + blocksize - 1) / blocksize;
  593. cbcbytes = 0;
  594. if (nblocks > 2)
  595. cbcbytes = (nblocks - 2) * blocksize;
  596. memset(desc.iv, 0, sizeof(desc.iv));
  597. /* Handle block-sized chunks of plaintext with CBC. */
  598. if (cbcbytes) {
  599. SYNC_SKCIPHER_REQUEST_ON_STACK(req, cbc_tfm);
  600. desc.pos = offset;
  601. desc.fragno = 0;
  602. desc.fraglen = 0;
  603. desc.pages = pages;
  604. desc.outbuf = buf;
  605. desc.req = req;
  606. skcipher_request_set_sync_tfm(req, cbc_tfm);
  607. skcipher_request_set_callback(req, 0, NULL, NULL);
  608. sg_init_table(desc.infrags, 4);
  609. sg_init_table(desc.outfrags, 4);
  610. err = xdr_process_buf(buf, offset, cbcbytes, encryptor, &desc);
  611. skcipher_request_zero(req);
  612. if (err)
  613. return err;
  614. }
  615. /* Remaining plaintext is handled with CBC-CTS. */
  616. err = gss_krb5_cts_crypt(cts_tfm, buf, offset + cbcbytes,
  617. desc.iv, pages, 1);
  618. if (err)
  619. return err;
  620. if (unlikely(iv))
  621. memcpy(iv, desc.iv, ivsize);
  622. return 0;
  623. }
  624. EXPORT_SYMBOL_IF_KUNIT(krb5_cbc_cts_encrypt);
  625. /**
  626. * krb5_cbc_cts_decrypt - decrypt in CBC mode with CTS
  627. * @cts_tfm: CBC cipher with CTS
  628. * @cbc_tfm: base CBC cipher
  629. * @offset: starting byte offset for plaintext
  630. * @buf: OUT: output buffer
  631. *
  632. * Return values:
  633. * %0: decryption successful
  634. * negative errno: decryption could not be completed
  635. */
  636. VISIBLE_IF_KUNIT
  637. int krb5_cbc_cts_decrypt(struct crypto_sync_skcipher *cts_tfm,
  638. struct crypto_sync_skcipher *cbc_tfm,
  639. u32 offset, struct xdr_buf *buf)
  640. {
  641. u32 blocksize, nblocks, cbcbytes;
  642. struct decryptor_desc desc;
  643. int err;
  644. blocksize = crypto_sync_skcipher_blocksize(cts_tfm);
  645. nblocks = (buf->len + blocksize - 1) / blocksize;
  646. cbcbytes = 0;
  647. if (nblocks > 2)
  648. cbcbytes = (nblocks - 2) * blocksize;
  649. memset(desc.iv, 0, sizeof(desc.iv));
  650. /* Handle block-sized chunks of plaintext with CBC. */
  651. if (cbcbytes) {
  652. SYNC_SKCIPHER_REQUEST_ON_STACK(req, cbc_tfm);
  653. desc.fragno = 0;
  654. desc.fraglen = 0;
  655. desc.req = req;
  656. skcipher_request_set_sync_tfm(req, cbc_tfm);
  657. skcipher_request_set_callback(req, 0, NULL, NULL);
  658. sg_init_table(desc.frags, 4);
  659. err = xdr_process_buf(buf, 0, cbcbytes, decryptor, &desc);
  660. skcipher_request_zero(req);
  661. if (err)
  662. return err;
  663. }
  664. /* Remaining plaintext is handled with CBC-CTS. */
  665. return gss_krb5_cts_crypt(cts_tfm, buf, cbcbytes, desc.iv, NULL, 0);
  666. }
  667. EXPORT_SYMBOL_IF_KUNIT(krb5_cbc_cts_decrypt);
  668. u32
  669. gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
  670. struct xdr_buf *buf, struct page **pages)
  671. {
  672. u32 err;
  673. struct xdr_netobj hmac;
  674. u8 *ecptr;
  675. struct crypto_sync_skcipher *cipher, *aux_cipher;
  676. struct crypto_ahash *ahash;
  677. struct page **save_pages;
  678. unsigned int conflen;
  679. if (kctx->initiate) {
  680. cipher = kctx->initiator_enc;
  681. aux_cipher = kctx->initiator_enc_aux;
  682. ahash = kctx->initiator_integ;
  683. } else {
  684. cipher = kctx->acceptor_enc;
  685. aux_cipher = kctx->acceptor_enc_aux;
  686. ahash = kctx->acceptor_integ;
  687. }
  688. conflen = crypto_sync_skcipher_blocksize(cipher);
  689. /* hide the gss token header and insert the confounder */
  690. offset += GSS_KRB5_TOK_HDR_LEN;
  691. if (xdr_extend_head(buf, offset, conflen))
  692. return GSS_S_FAILURE;
  693. krb5_make_confounder(buf->head[0].iov_base + offset, conflen);
  694. offset -= GSS_KRB5_TOK_HDR_LEN;
  695. if (buf->tail[0].iov_base != NULL) {
  696. ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
  697. } else {
  698. buf->tail[0].iov_base = buf->head[0].iov_base
  699. + buf->head[0].iov_len;
  700. buf->tail[0].iov_len = 0;
  701. ecptr = buf->tail[0].iov_base;
  702. }
  703. /* copy plaintext gss token header after filler (if any) */
  704. memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
  705. buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
  706. buf->len += GSS_KRB5_TOK_HDR_LEN;
  707. hmac.len = kctx->gk5e->cksumlength;
  708. hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  709. /*
  710. * When we are called, pages points to the real page cache
  711. * data -- which we can't go and encrypt! buf->pages points
  712. * to scratch pages which we are going to send off to the
  713. * client/server. Swap in the plaintext pages to calculate
  714. * the hmac.
  715. */
  716. save_pages = buf->pages;
  717. buf->pages = pages;
  718. err = gss_krb5_checksum(ahash, NULL, 0, buf,
  719. offset + GSS_KRB5_TOK_HDR_LEN, &hmac);
  720. buf->pages = save_pages;
  721. if (err)
  722. return GSS_S_FAILURE;
  723. err = krb5_cbc_cts_encrypt(cipher, aux_cipher,
  724. offset + GSS_KRB5_TOK_HDR_LEN,
  725. buf, pages, NULL, 0);
  726. if (err)
  727. return GSS_S_FAILURE;
  728. /* Now update buf to account for HMAC */
  729. buf->tail[0].iov_len += kctx->gk5e->cksumlength;
  730. buf->len += kctx->gk5e->cksumlength;
  731. return GSS_S_COMPLETE;
  732. }
  733. u32
  734. gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
  735. struct xdr_buf *buf, u32 *headskip, u32 *tailskip)
  736. {
  737. struct crypto_sync_skcipher *cipher, *aux_cipher;
  738. struct crypto_ahash *ahash;
  739. struct xdr_netobj our_hmac_obj;
  740. u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  741. u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  742. struct xdr_buf subbuf;
  743. u32 ret = 0;
  744. if (kctx->initiate) {
  745. cipher = kctx->acceptor_enc;
  746. aux_cipher = kctx->acceptor_enc_aux;
  747. ahash = kctx->acceptor_integ;
  748. } else {
  749. cipher = kctx->initiator_enc;
  750. aux_cipher = kctx->initiator_enc_aux;
  751. ahash = kctx->initiator_integ;
  752. }
  753. /* create a segment skipping the header and leaving out the checksum */
  754. xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
  755. (len - offset - GSS_KRB5_TOK_HDR_LEN -
  756. kctx->gk5e->cksumlength));
  757. ret = krb5_cbc_cts_decrypt(cipher, aux_cipher, 0, &subbuf);
  758. if (ret)
  759. goto out_err;
  760. our_hmac_obj.len = kctx->gk5e->cksumlength;
  761. our_hmac_obj.data = our_hmac;
  762. ret = gss_krb5_checksum(ahash, NULL, 0, &subbuf, 0, &our_hmac_obj);
  763. if (ret)
  764. goto out_err;
  765. /* Get the packet's hmac value */
  766. ret = read_bytes_from_xdr_buf(buf, len - kctx->gk5e->cksumlength,
  767. pkt_hmac, kctx->gk5e->cksumlength);
  768. if (ret)
  769. goto out_err;
  770. if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
  771. ret = GSS_S_BAD_SIG;
  772. goto out_err;
  773. }
  774. *headskip = crypto_sync_skcipher_blocksize(cipher);
  775. *tailskip = kctx->gk5e->cksumlength;
  776. out_err:
  777. if (ret && ret != GSS_S_BAD_SIG)
  778. ret = GSS_S_FAILURE;
  779. return ret;
  780. }
  781. /**
  782. * krb5_etm_checksum - Compute a MAC for a GSS Wrap token
  783. * @cipher: an initialized cipher transform
  784. * @tfm: an initialized hash transform
  785. * @body: xdr_buf containing an RPC message (body.len is the message length)
  786. * @body_offset: byte offset into @body to start checksumming
  787. * @cksumout: OUT: a buffer to be filled in with the computed HMAC
  788. *
  789. * Usually expressed as H = HMAC(K, IV | ciphertext)[1..h] .
  790. *
  791. * Caller provides the truncation length of the output token (h) in
  792. * cksumout.len.
  793. *
  794. * Return values:
  795. * %GSS_S_COMPLETE: Digest computed, @cksumout filled in
  796. * %GSS_S_FAILURE: Call failed
  797. */
  798. VISIBLE_IF_KUNIT
  799. u32 krb5_etm_checksum(struct crypto_sync_skcipher *cipher,
  800. struct crypto_ahash *tfm, const struct xdr_buf *body,
  801. int body_offset, struct xdr_netobj *cksumout)
  802. {
  803. unsigned int ivsize = crypto_sync_skcipher_ivsize(cipher);
  804. struct ahash_request *req;
  805. struct scatterlist sg[1];
  806. u8 *iv, *checksumdata;
  807. int err = -ENOMEM;
  808. checksumdata = kmalloc(crypto_ahash_digestsize(tfm), GFP_KERNEL);
  809. if (!checksumdata)
  810. return GSS_S_FAILURE;
  811. /* For RPCSEC, the "initial cipher state" is always all zeroes. */
  812. iv = kzalloc(ivsize, GFP_KERNEL);
  813. if (!iv)
  814. goto out_free_mem;
  815. req = ahash_request_alloc(tfm, GFP_KERNEL);
  816. if (!req)
  817. goto out_free_mem;
  818. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  819. err = crypto_ahash_init(req);
  820. if (err)
  821. goto out_free_ahash;
  822. sg_init_one(sg, iv, ivsize);
  823. ahash_request_set_crypt(req, sg, NULL, ivsize);
  824. err = crypto_ahash_update(req);
  825. if (err)
  826. goto out_free_ahash;
  827. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  828. checksummer, req);
  829. if (err)
  830. goto out_free_ahash;
  831. ahash_request_set_crypt(req, NULL, checksumdata, 0);
  832. err = crypto_ahash_final(req);
  833. if (err)
  834. goto out_free_ahash;
  835. memcpy(cksumout->data, checksumdata, cksumout->len);
  836. out_free_ahash:
  837. ahash_request_free(req);
  838. out_free_mem:
  839. kfree(iv);
  840. kfree_sensitive(checksumdata);
  841. return err ? GSS_S_FAILURE : GSS_S_COMPLETE;
  842. }
  843. EXPORT_SYMBOL_IF_KUNIT(krb5_etm_checksum);
  844. /**
  845. * krb5_etm_encrypt - Encrypt using the RFC 8009 rules
  846. * @kctx: Kerberos context
  847. * @offset: starting offset of the payload, in bytes
  848. * @buf: OUT: send buffer to contain the encrypted payload
  849. * @pages: plaintext payload
  850. *
  851. * The main difference with aes_encrypt is that "The HMAC is
  852. * calculated over the cipher state concatenated with the AES
  853. * output, instead of being calculated over the confounder and
  854. * plaintext. This allows the message receiver to verify the
  855. * integrity of the message before decrypting the message."
  856. *
  857. * RFC 8009 Section 5:
  858. *
  859. * encryption function: as follows, where E() is AES encryption in
  860. * CBC-CS3 mode, and h is the size of truncated HMAC (128 bits or
  861. * 192 bits as described above).
  862. *
  863. * N = random value of length 128 bits (the AES block size)
  864. * IV = cipher state
  865. * C = E(Ke, N | plaintext, IV)
  866. * H = HMAC(Ki, IV | C)
  867. * ciphertext = C | H[1..h]
  868. *
  869. * This encryption formula provides AEAD EtM with key separation.
  870. *
  871. * Return values:
  872. * %GSS_S_COMPLETE: Encryption successful
  873. * %GSS_S_FAILURE: Encryption failed
  874. */
  875. u32
  876. krb5_etm_encrypt(struct krb5_ctx *kctx, u32 offset,
  877. struct xdr_buf *buf, struct page **pages)
  878. {
  879. struct crypto_sync_skcipher *cipher, *aux_cipher;
  880. struct crypto_ahash *ahash;
  881. struct xdr_netobj hmac;
  882. unsigned int conflen;
  883. u8 *ecptr;
  884. u32 err;
  885. if (kctx->initiate) {
  886. cipher = kctx->initiator_enc;
  887. aux_cipher = kctx->initiator_enc_aux;
  888. ahash = kctx->initiator_integ;
  889. } else {
  890. cipher = kctx->acceptor_enc;
  891. aux_cipher = kctx->acceptor_enc_aux;
  892. ahash = kctx->acceptor_integ;
  893. }
  894. conflen = crypto_sync_skcipher_blocksize(cipher);
  895. offset += GSS_KRB5_TOK_HDR_LEN;
  896. if (xdr_extend_head(buf, offset, conflen))
  897. return GSS_S_FAILURE;
  898. krb5_make_confounder(buf->head[0].iov_base + offset, conflen);
  899. offset -= GSS_KRB5_TOK_HDR_LEN;
  900. if (buf->tail[0].iov_base) {
  901. ecptr = buf->tail[0].iov_base + buf->tail[0].iov_len;
  902. } else {
  903. buf->tail[0].iov_base = buf->head[0].iov_base
  904. + buf->head[0].iov_len;
  905. buf->tail[0].iov_len = 0;
  906. ecptr = buf->tail[0].iov_base;
  907. }
  908. memcpy(ecptr, buf->head[0].iov_base + offset, GSS_KRB5_TOK_HDR_LEN);
  909. buf->tail[0].iov_len += GSS_KRB5_TOK_HDR_LEN;
  910. buf->len += GSS_KRB5_TOK_HDR_LEN;
  911. err = krb5_cbc_cts_encrypt(cipher, aux_cipher,
  912. offset + GSS_KRB5_TOK_HDR_LEN,
  913. buf, pages, NULL, 0);
  914. if (err)
  915. return GSS_S_FAILURE;
  916. hmac.data = buf->tail[0].iov_base + buf->tail[0].iov_len;
  917. hmac.len = kctx->gk5e->cksumlength;
  918. err = krb5_etm_checksum(cipher, ahash,
  919. buf, offset + GSS_KRB5_TOK_HDR_LEN, &hmac);
  920. if (err)
  921. goto out_err;
  922. buf->tail[0].iov_len += kctx->gk5e->cksumlength;
  923. buf->len += kctx->gk5e->cksumlength;
  924. return GSS_S_COMPLETE;
  925. out_err:
  926. return GSS_S_FAILURE;
  927. }
  928. /**
  929. * krb5_etm_decrypt - Decrypt using the RFC 8009 rules
  930. * @kctx: Kerberos context
  931. * @offset: starting offset of the ciphertext, in bytes
  932. * @len:
  933. * @buf:
  934. * @headskip: OUT: the enctype's confounder length, in octets
  935. * @tailskip: OUT: the enctype's HMAC length, in octets
  936. *
  937. * RFC 8009 Section 5:
  938. *
  939. * decryption function: as follows, where D() is AES decryption in
  940. * CBC-CS3 mode, and h is the size of truncated HMAC.
  941. *
  942. * (C, H) = ciphertext
  943. * (Note: H is the last h bits of the ciphertext.)
  944. * IV = cipher state
  945. * if H != HMAC(Ki, IV | C)[1..h]
  946. * stop, report error
  947. * (N, P) = D(Ke, C, IV)
  948. *
  949. * Return values:
  950. * %GSS_S_COMPLETE: Decryption successful
  951. * %GSS_S_BAD_SIG: computed HMAC != received HMAC
  952. * %GSS_S_FAILURE: Decryption failed
  953. */
  954. u32
  955. krb5_etm_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
  956. struct xdr_buf *buf, u32 *headskip, u32 *tailskip)
  957. {
  958. struct crypto_sync_skcipher *cipher, *aux_cipher;
  959. u8 our_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  960. u8 pkt_hmac[GSS_KRB5_MAX_CKSUM_LEN];
  961. struct xdr_netobj our_hmac_obj;
  962. struct crypto_ahash *ahash;
  963. struct xdr_buf subbuf;
  964. u32 ret = 0;
  965. if (kctx->initiate) {
  966. cipher = kctx->acceptor_enc;
  967. aux_cipher = kctx->acceptor_enc_aux;
  968. ahash = kctx->acceptor_integ;
  969. } else {
  970. cipher = kctx->initiator_enc;
  971. aux_cipher = kctx->initiator_enc_aux;
  972. ahash = kctx->initiator_integ;
  973. }
  974. /* Extract the ciphertext into @subbuf. */
  975. xdr_buf_subsegment(buf, &subbuf, offset + GSS_KRB5_TOK_HDR_LEN,
  976. (len - offset - GSS_KRB5_TOK_HDR_LEN -
  977. kctx->gk5e->cksumlength));
  978. our_hmac_obj.data = our_hmac;
  979. our_hmac_obj.len = kctx->gk5e->cksumlength;
  980. ret = krb5_etm_checksum(cipher, ahash, &subbuf, 0, &our_hmac_obj);
  981. if (ret)
  982. goto out_err;
  983. ret = read_bytes_from_xdr_buf(buf, len - kctx->gk5e->cksumlength,
  984. pkt_hmac, kctx->gk5e->cksumlength);
  985. if (ret)
  986. goto out_err;
  987. if (crypto_memneq(pkt_hmac, our_hmac, kctx->gk5e->cksumlength) != 0) {
  988. ret = GSS_S_BAD_SIG;
  989. goto out_err;
  990. }
  991. ret = krb5_cbc_cts_decrypt(cipher, aux_cipher, 0, &subbuf);
  992. if (ret) {
  993. ret = GSS_S_FAILURE;
  994. goto out_err;
  995. }
  996. *headskip = crypto_sync_skcipher_blocksize(cipher);
  997. *tailskip = kctx->gk5e->cksumlength;
  998. return GSS_S_COMPLETE;
  999. out_err:
  1000. if (ret != GSS_S_BAD_SIG)
  1001. ret = GSS_S_FAILURE;
  1002. return ret;
  1003. }