gss_krb5_crypto.c 9.9 KB

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