gss_krb5_crypto.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * linux/net/sunrpc/gss_krb5_crypto.c
  3. *
  4. * Copyright (c) 2000 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/types.h>
  36. #include <linux/mm.h>
  37. #include <linux/slab.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. #ifdef RPC_DEBUG
  44. # define RPCDBG_FACILITY RPCDBG_AUTH
  45. #endif
  46. u32
  47. krb5_encrypt(
  48. struct crypto_tfm *tfm,
  49. void * iv,
  50. void * in,
  51. void * out,
  52. int length)
  53. {
  54. u32 ret = -EINVAL;
  55. struct scatterlist sg[1];
  56. u8 local_iv[16] = {0};
  57. dprintk("RPC: krb5_encrypt: input data:\n");
  58. print_hexl((u32 *)in, length, 0);
  59. if (length % crypto_tfm_alg_blocksize(tfm) != 0)
  60. goto out;
  61. if (crypto_tfm_alg_ivsize(tfm) > 16) {
  62. dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
  63. crypto_tfm_alg_ivsize(tfm));
  64. goto out;
  65. }
  66. if (iv)
  67. memcpy(local_iv, iv, crypto_tfm_alg_ivsize(tfm));
  68. memcpy(out, in, length);
  69. sg_set_buf(sg, out, length);
  70. ret = crypto_cipher_encrypt_iv(tfm, sg, sg, length, local_iv);
  71. dprintk("RPC: krb5_encrypt: output data:\n");
  72. print_hexl((u32 *)out, length, 0);
  73. out:
  74. dprintk("RPC: krb5_encrypt returns %d\n",ret);
  75. return(ret);
  76. }
  77. EXPORT_SYMBOL(krb5_encrypt);
  78. u32
  79. krb5_decrypt(
  80. struct crypto_tfm *tfm,
  81. void * iv,
  82. void * in,
  83. void * out,
  84. int length)
  85. {
  86. u32 ret = -EINVAL;
  87. struct scatterlist sg[1];
  88. u8 local_iv[16] = {0};
  89. dprintk("RPC: krb5_decrypt: input data:\n");
  90. print_hexl((u32 *)in, length, 0);
  91. if (length % crypto_tfm_alg_blocksize(tfm) != 0)
  92. goto out;
  93. if (crypto_tfm_alg_ivsize(tfm) > 16) {
  94. dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
  95. crypto_tfm_alg_ivsize(tfm));
  96. goto out;
  97. }
  98. if (iv)
  99. memcpy(local_iv,iv, crypto_tfm_alg_ivsize(tfm));
  100. memcpy(out, in, length);
  101. sg_set_buf(sg, out, length);
  102. ret = crypto_cipher_decrypt_iv(tfm, sg, sg, length, local_iv);
  103. dprintk("RPC: krb5_decrypt: output_data:\n");
  104. print_hexl((u32 *)out, length, 0);
  105. out:
  106. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  107. return(ret);
  108. }
  109. EXPORT_SYMBOL(krb5_decrypt);
  110. static int
  111. process_xdr_buf(struct xdr_buf *buf, int offset, int len,
  112. int (*actor)(struct scatterlist *, void *), void *data)
  113. {
  114. int i, page_len, thislen, page_offset, ret = 0;
  115. struct scatterlist sg[1];
  116. if (offset >= buf->head[0].iov_len) {
  117. offset -= buf->head[0].iov_len;
  118. } else {
  119. thislen = buf->head[0].iov_len - offset;
  120. if (thislen > len)
  121. thislen = len;
  122. sg_set_buf(sg, buf->head[0].iov_base + offset, thislen);
  123. ret = actor(sg, data);
  124. if (ret)
  125. goto out;
  126. offset = 0;
  127. len -= thislen;
  128. }
  129. if (len == 0)
  130. goto out;
  131. if (offset >= buf->page_len) {
  132. offset -= buf->page_len;
  133. } else {
  134. page_len = buf->page_len - offset;
  135. if (page_len > len)
  136. page_len = len;
  137. len -= page_len;
  138. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  139. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  140. thislen = PAGE_CACHE_SIZE - page_offset;
  141. do {
  142. if (thislen > page_len)
  143. thislen = page_len;
  144. sg->page = buf->pages[i];
  145. sg->offset = page_offset;
  146. sg->length = thislen;
  147. ret = actor(sg, data);
  148. if (ret)
  149. goto out;
  150. page_len -= thislen;
  151. i++;
  152. page_offset = 0;
  153. thislen = PAGE_CACHE_SIZE;
  154. } while (page_len != 0);
  155. offset = 0;
  156. }
  157. if (len == 0)
  158. goto out;
  159. if (offset < buf->tail[0].iov_len) {
  160. thislen = buf->tail[0].iov_len - offset;
  161. if (thislen > len)
  162. thislen = len;
  163. sg_set_buf(sg, buf->tail[0].iov_base + offset, thislen);
  164. ret = actor(sg, data);
  165. len -= thislen;
  166. }
  167. if (len != 0)
  168. ret = -EINVAL;
  169. out:
  170. return ret;
  171. }
  172. static int
  173. checksummer(struct scatterlist *sg, void *data)
  174. {
  175. struct crypto_tfm *tfm = (struct crypto_tfm *)data;
  176. crypto_digest_update(tfm, sg, 1);
  177. return 0;
  178. }
  179. /* checksum the plaintext data and hdrlen bytes of the token header */
  180. s32
  181. make_checksum(s32 cksumtype, char *header, int hdrlen, struct xdr_buf *body,
  182. int body_offset, struct xdr_netobj *cksum)
  183. {
  184. char *cksumname;
  185. struct crypto_tfm *tfm = NULL; /* XXX add to ctx? */
  186. struct scatterlist sg[1];
  187. u32 code = GSS_S_FAILURE;
  188. switch (cksumtype) {
  189. case CKSUMTYPE_RSA_MD5:
  190. cksumname = "md5";
  191. break;
  192. default:
  193. dprintk("RPC: krb5_make_checksum:"
  194. " unsupported checksum %d", cksumtype);
  195. goto out;
  196. }
  197. if (!(tfm = crypto_alloc_tfm(cksumname, CRYPTO_TFM_REQ_MAY_SLEEP)))
  198. goto out;
  199. cksum->len = crypto_tfm_alg_digestsize(tfm);
  200. if ((cksum->data = kmalloc(cksum->len, GFP_KERNEL)) == NULL)
  201. goto out;
  202. crypto_digest_init(tfm);
  203. sg_set_buf(sg, header, hdrlen);
  204. crypto_digest_update(tfm, sg, 1);
  205. process_xdr_buf(body, body_offset, body->len - body_offset,
  206. checksummer, tfm);
  207. crypto_digest_final(tfm, cksum->data);
  208. code = 0;
  209. out:
  210. crypto_free_tfm(tfm);
  211. return code;
  212. }
  213. EXPORT_SYMBOL(make_checksum);
  214. struct encryptor_desc {
  215. u8 iv[8]; /* XXX hard-coded blocksize */
  216. struct crypto_tfm *tfm;
  217. int pos;
  218. struct xdr_buf *outbuf;
  219. struct page **pages;
  220. struct scatterlist infrags[4];
  221. struct scatterlist outfrags[4];
  222. int fragno;
  223. int fraglen;
  224. };
  225. static int
  226. encryptor(struct scatterlist *sg, void *data)
  227. {
  228. struct encryptor_desc *desc = data;
  229. struct xdr_buf *outbuf = desc->outbuf;
  230. struct page *in_page;
  231. int thislen = desc->fraglen + sg->length;
  232. int fraglen, ret;
  233. int page_pos;
  234. /* Worst case is 4 fragments: head, end of page 1, start
  235. * of page 2, tail. Anything more is a bug. */
  236. BUG_ON(desc->fragno > 3);
  237. desc->infrags[desc->fragno] = *sg;
  238. desc->outfrags[desc->fragno] = *sg;
  239. page_pos = desc->pos - outbuf->head[0].iov_len;
  240. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  241. /* pages are not in place: */
  242. int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
  243. in_page = desc->pages[i];
  244. } else {
  245. in_page = sg->page;
  246. }
  247. desc->infrags[desc->fragno].page = in_page;
  248. desc->fragno++;
  249. desc->fraglen += sg->length;
  250. desc->pos += sg->length;
  251. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  252. thislen -= fraglen;
  253. if (thislen == 0)
  254. return 0;
  255. ret = crypto_cipher_encrypt_iv(desc->tfm, desc->outfrags, desc->infrags,
  256. thislen, desc->iv);
  257. if (ret)
  258. return ret;
  259. if (fraglen) {
  260. desc->outfrags[0].page = sg->page;
  261. desc->outfrags[0].offset = sg->offset + sg->length - fraglen;
  262. desc->outfrags[0].length = fraglen;
  263. desc->infrags[0] = desc->outfrags[0];
  264. desc->infrags[0].page = in_page;
  265. desc->fragno = 1;
  266. desc->fraglen = fraglen;
  267. } else {
  268. desc->fragno = 0;
  269. desc->fraglen = 0;
  270. }
  271. return 0;
  272. }
  273. int
  274. gss_encrypt_xdr_buf(struct crypto_tfm *tfm, struct xdr_buf *buf, int offset,
  275. struct page **pages)
  276. {
  277. int ret;
  278. struct encryptor_desc desc;
  279. BUG_ON((buf->len - offset) % crypto_tfm_alg_blocksize(tfm) != 0);
  280. memset(desc.iv, 0, sizeof(desc.iv));
  281. desc.tfm = tfm;
  282. desc.pos = offset;
  283. desc.outbuf = buf;
  284. desc.pages = pages;
  285. desc.fragno = 0;
  286. desc.fraglen = 0;
  287. ret = process_xdr_buf(buf, offset, buf->len - offset, encryptor, &desc);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL(gss_encrypt_xdr_buf);
  291. struct decryptor_desc {
  292. u8 iv[8]; /* XXX hard-coded blocksize */
  293. struct crypto_tfm *tfm;
  294. struct scatterlist frags[4];
  295. int fragno;
  296. int fraglen;
  297. };
  298. static int
  299. decryptor(struct scatterlist *sg, void *data)
  300. {
  301. struct decryptor_desc *desc = data;
  302. int thislen = desc->fraglen + sg->length;
  303. int fraglen, ret;
  304. /* Worst case is 4 fragments: head, end of page 1, start
  305. * of page 2, tail. Anything more is a bug. */
  306. BUG_ON(desc->fragno > 3);
  307. desc->frags[desc->fragno] = *sg;
  308. desc->fragno++;
  309. desc->fraglen += sg->length;
  310. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  311. thislen -= fraglen;
  312. if (thislen == 0)
  313. return 0;
  314. ret = crypto_cipher_decrypt_iv(desc->tfm, desc->frags, desc->frags,
  315. thislen, desc->iv);
  316. if (ret)
  317. return ret;
  318. if (fraglen) {
  319. desc->frags[0].page = sg->page;
  320. desc->frags[0].offset = sg->offset + sg->length - fraglen;
  321. desc->frags[0].length = fraglen;
  322. desc->fragno = 1;
  323. desc->fraglen = fraglen;
  324. } else {
  325. desc->fragno = 0;
  326. desc->fraglen = 0;
  327. }
  328. return 0;
  329. }
  330. int
  331. gss_decrypt_xdr_buf(struct crypto_tfm *tfm, struct xdr_buf *buf, int offset)
  332. {
  333. struct decryptor_desc desc;
  334. /* XXXJBF: */
  335. BUG_ON((buf->len - offset) % crypto_tfm_alg_blocksize(tfm) != 0);
  336. memset(desc.iv, 0, sizeof(desc.iv));
  337. desc.tfm = tfm;
  338. desc.fragno = 0;
  339. desc.fraglen = 0;
  340. return process_xdr_buf(buf, offset, buf->len - offset, decryptor, &desc);
  341. }
  342. EXPORT_SYMBOL(gss_decrypt_xdr_buf);