gss_krb5_crypto.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/sunrpc/gss_krb5.h>
  43. #include <linux/sunrpc/xdr.h>
  44. #ifdef RPC_DEBUG
  45. # define RPCDBG_FACILITY RPCDBG_AUTH
  46. #endif
  47. u32
  48. krb5_encrypt(
  49. struct crypto_blkcipher *tfm,
  50. void * iv,
  51. void * in,
  52. void * out,
  53. int length)
  54. {
  55. u32 ret = -EINVAL;
  56. struct scatterlist sg[1];
  57. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  58. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  59. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  60. goto out;
  61. if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  62. dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
  63. crypto_blkcipher_ivsize(tfm));
  64. goto out;
  65. }
  66. if (iv)
  67. memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
  68. memcpy(out, in, length);
  69. sg_init_one(sg, out, length);
  70. ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
  71. out:
  72. dprintk("RPC: krb5_encrypt returns %d\n", ret);
  73. return ret;
  74. }
  75. u32
  76. krb5_decrypt(
  77. struct crypto_blkcipher *tfm,
  78. void * iv,
  79. void * in,
  80. void * out,
  81. int length)
  82. {
  83. u32 ret = -EINVAL;
  84. struct scatterlist sg[1];
  85. u8 local_iv[GSS_KRB5_MAX_BLOCKSIZE] = {0};
  86. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  87. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  88. goto out;
  89. if (crypto_blkcipher_ivsize(tfm) > GSS_KRB5_MAX_BLOCKSIZE) {
  90. dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
  91. crypto_blkcipher_ivsize(tfm));
  92. goto out;
  93. }
  94. if (iv)
  95. memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
  96. memcpy(out, in, length);
  97. sg_init_one(sg, out, length);
  98. ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
  99. out:
  100. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  101. return ret;
  102. }
  103. static int
  104. checksummer(struct scatterlist *sg, void *data)
  105. {
  106. struct hash_desc *desc = data;
  107. return crypto_hash_update(desc, sg, sg->length);
  108. }
  109. /*
  110. * checksum the plaintext data and hdrlen bytes of the token header
  111. * The checksum is performed over the first 8 bytes of the
  112. * gss token header and then over the data body
  113. */
  114. u32
  115. make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
  116. struct xdr_buf *body, int body_offset, u8 *cksumkey,
  117. struct xdr_netobj *cksumout)
  118. {
  119. struct hash_desc desc;
  120. struct scatterlist sg[1];
  121. int err;
  122. u8 checksumdata[GSS_KRB5_MAX_CKSUM_LEN];
  123. unsigned int checksumlen;
  124. if (cksumout->len < kctx->gk5e->cksumlength) {
  125. dprintk("%s: checksum buffer length, %u, too small for %s\n",
  126. __func__, cksumout->len, kctx->gk5e->name);
  127. return GSS_S_FAILURE;
  128. }
  129. desc.tfm = crypto_alloc_hash(kctx->gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  130. if (IS_ERR(desc.tfm))
  131. return GSS_S_FAILURE;
  132. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  133. checksumlen = crypto_hash_digestsize(desc.tfm);
  134. if (cksumkey != NULL) {
  135. err = crypto_hash_setkey(desc.tfm, cksumkey,
  136. kctx->gk5e->keylength);
  137. if (err)
  138. goto out;
  139. }
  140. err = crypto_hash_init(&desc);
  141. if (err)
  142. goto out;
  143. sg_init_one(sg, header, hdrlen);
  144. err = crypto_hash_update(&desc, sg, hdrlen);
  145. if (err)
  146. goto out;
  147. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  148. checksummer, &desc);
  149. if (err)
  150. goto out;
  151. err = crypto_hash_final(&desc, checksumdata);
  152. if (err)
  153. goto out;
  154. switch (kctx->gk5e->ctype) {
  155. case CKSUMTYPE_RSA_MD5:
  156. err = kctx->gk5e->encrypt(kctx->seq, NULL, checksumdata,
  157. checksumdata, checksumlen);
  158. if (err)
  159. goto out;
  160. memcpy(cksumout->data,
  161. checksumdata + checksumlen - kctx->gk5e->cksumlength,
  162. kctx->gk5e->cksumlength);
  163. break;
  164. default:
  165. BUG();
  166. break;
  167. }
  168. cksumout->len = kctx->gk5e->cksumlength;
  169. out:
  170. crypto_free_hash(desc.tfm);
  171. return err ? GSS_S_FAILURE : 0;
  172. }
  173. struct encryptor_desc {
  174. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  175. struct blkcipher_desc desc;
  176. int pos;
  177. struct xdr_buf *outbuf;
  178. struct page **pages;
  179. struct scatterlist infrags[4];
  180. struct scatterlist outfrags[4];
  181. int fragno;
  182. int fraglen;
  183. };
  184. static int
  185. encryptor(struct scatterlist *sg, void *data)
  186. {
  187. struct encryptor_desc *desc = data;
  188. struct xdr_buf *outbuf = desc->outbuf;
  189. struct page *in_page;
  190. int thislen = desc->fraglen + sg->length;
  191. int fraglen, ret;
  192. int page_pos;
  193. /* Worst case is 4 fragments: head, end of page 1, start
  194. * of page 2, tail. Anything more is a bug. */
  195. BUG_ON(desc->fragno > 3);
  196. page_pos = desc->pos - outbuf->head[0].iov_len;
  197. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  198. /* pages are not in place: */
  199. int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
  200. in_page = desc->pages[i];
  201. } else {
  202. in_page = sg_page(sg);
  203. }
  204. sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
  205. sg->offset);
  206. sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
  207. sg->offset);
  208. desc->fragno++;
  209. desc->fraglen += sg->length;
  210. desc->pos += sg->length;
  211. fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
  212. thislen -= fraglen;
  213. if (thislen == 0)
  214. return 0;
  215. sg_mark_end(&desc->infrags[desc->fragno - 1]);
  216. sg_mark_end(&desc->outfrags[desc->fragno - 1]);
  217. ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
  218. desc->infrags, thislen);
  219. if (ret)
  220. return ret;
  221. sg_init_table(desc->infrags, 4);
  222. sg_init_table(desc->outfrags, 4);
  223. if (fraglen) {
  224. sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
  225. sg->offset + sg->length - fraglen);
  226. desc->infrags[0] = desc->outfrags[0];
  227. sg_assign_page(&desc->infrags[0], in_page);
  228. desc->fragno = 1;
  229. desc->fraglen = fraglen;
  230. } else {
  231. desc->fragno = 0;
  232. desc->fraglen = 0;
  233. }
  234. return 0;
  235. }
  236. int
  237. gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  238. int offset, struct page **pages)
  239. {
  240. int ret;
  241. struct encryptor_desc desc;
  242. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  243. memset(desc.iv, 0, sizeof(desc.iv));
  244. desc.desc.tfm = tfm;
  245. desc.desc.info = desc.iv;
  246. desc.desc.flags = 0;
  247. desc.pos = offset;
  248. desc.outbuf = buf;
  249. desc.pages = pages;
  250. desc.fragno = 0;
  251. desc.fraglen = 0;
  252. sg_init_table(desc.infrags, 4);
  253. sg_init_table(desc.outfrags, 4);
  254. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  255. return ret;
  256. }
  257. struct decryptor_desc {
  258. u8 iv[GSS_KRB5_MAX_BLOCKSIZE];
  259. struct blkcipher_desc desc;
  260. struct scatterlist frags[4];
  261. int fragno;
  262. int fraglen;
  263. };
  264. static int
  265. decryptor(struct scatterlist *sg, void *data)
  266. {
  267. struct decryptor_desc *desc = data;
  268. int thislen = desc->fraglen + sg->length;
  269. int fraglen, ret;
  270. /* Worst case is 4 fragments: head, end of page 1, start
  271. * of page 2, tail. Anything more is a bug. */
  272. BUG_ON(desc->fragno > 3);
  273. sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
  274. sg->offset);
  275. desc->fragno++;
  276. desc->fraglen += sg->length;
  277. fraglen = thislen & (crypto_blkcipher_blocksize(desc->desc.tfm) - 1);
  278. thislen -= fraglen;
  279. if (thislen == 0)
  280. return 0;
  281. sg_mark_end(&desc->frags[desc->fragno - 1]);
  282. ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
  283. desc->frags, thislen);
  284. if (ret)
  285. return ret;
  286. sg_init_table(desc->frags, 4);
  287. if (fraglen) {
  288. sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
  289. sg->offset + sg->length - fraglen);
  290. desc->fragno = 1;
  291. desc->fraglen = fraglen;
  292. } else {
  293. desc->fragno = 0;
  294. desc->fraglen = 0;
  295. }
  296. return 0;
  297. }
  298. int
  299. gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  300. int offset)
  301. {
  302. struct decryptor_desc desc;
  303. /* XXXJBF: */
  304. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  305. memset(desc.iv, 0, sizeof(desc.iv));
  306. desc.desc.tfm = tfm;
  307. desc.desc.info = desc.iv;
  308. desc.desc.flags = 0;
  309. desc.fragno = 0;
  310. desc.fraglen = 0;
  311. sg_init_table(desc.frags, 4);
  312. return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  313. }
  314. /*
  315. * This function makes the assumption that it was ultimately called
  316. * from gss_wrap().
  317. *
  318. * The client auth_gss code moves any existing tail data into a
  319. * separate page before calling gss_wrap.
  320. * The server svcauth_gss code ensures that both the head and the
  321. * tail have slack space of RPC_MAX_AUTH_SIZE before calling gss_wrap.
  322. *
  323. * Even with that guarantee, this function may be called more than
  324. * once in the processing of gss_wrap(). The best we can do is
  325. * verify at compile-time (see GSS_KRB5_SLACK_CHECK) that the
  326. * largest expected shift will fit within RPC_MAX_AUTH_SIZE.
  327. * At run-time we can verify that a single invocation of this
  328. * function doesn't attempt to use more the RPC_MAX_AUTH_SIZE.
  329. */
  330. int
  331. xdr_extend_head(struct xdr_buf *buf, unsigned int base, unsigned int shiftlen)
  332. {
  333. u8 *p;
  334. if (shiftlen == 0)
  335. return 0;
  336. BUILD_BUG_ON(GSS_KRB5_MAX_SLACK_NEEDED > RPC_MAX_AUTH_SIZE);
  337. BUG_ON(shiftlen > RPC_MAX_AUTH_SIZE);
  338. p = buf->head[0].iov_base + base;
  339. memmove(p + shiftlen, p, buf->head[0].iov_len - base);
  340. buf->head[0].iov_len += shiftlen;
  341. buf->len += shiftlen;
  342. return 0;
  343. }