gss_krb5_crypto.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/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[16] = {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) > 16) {
  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[16] = {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) > 16) {
  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. /* checksum the plaintext data and hdrlen bytes of the token header */
  110. s32
  111. make_checksum(char *cksumname, char *header, int hdrlen, struct xdr_buf *body,
  112. int body_offset, struct xdr_netobj *cksum)
  113. {
  114. struct hash_desc desc; /* XXX add to ctx? */
  115. struct scatterlist sg[1];
  116. int err;
  117. desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC);
  118. if (IS_ERR(desc.tfm))
  119. return GSS_S_FAILURE;
  120. cksum->len = crypto_hash_digestsize(desc.tfm);
  121. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  122. err = crypto_hash_init(&desc);
  123. if (err)
  124. goto out;
  125. sg_init_one(sg, header, hdrlen);
  126. err = crypto_hash_update(&desc, sg, hdrlen);
  127. if (err)
  128. goto out;
  129. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  130. checksummer, &desc);
  131. if (err)
  132. goto out;
  133. err = crypto_hash_final(&desc, cksum->data);
  134. out:
  135. crypto_free_hash(desc.tfm);
  136. return err ? GSS_S_FAILURE : 0;
  137. }
  138. struct encryptor_desc {
  139. u8 iv[8]; /* XXX hard-coded blocksize */
  140. struct blkcipher_desc desc;
  141. int pos;
  142. struct xdr_buf *outbuf;
  143. struct page **pages;
  144. struct scatterlist infrags[4];
  145. struct scatterlist outfrags[4];
  146. int fragno;
  147. int fraglen;
  148. };
  149. static int
  150. encryptor(struct scatterlist *sg, void *data)
  151. {
  152. struct encryptor_desc *desc = data;
  153. struct xdr_buf *outbuf = desc->outbuf;
  154. struct page *in_page;
  155. int thislen = desc->fraglen + sg->length;
  156. int fraglen, ret;
  157. int page_pos;
  158. /* Worst case is 4 fragments: head, end of page 1, start
  159. * of page 2, tail. Anything more is a bug. */
  160. BUG_ON(desc->fragno > 3);
  161. page_pos = desc->pos - outbuf->head[0].iov_len;
  162. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  163. /* pages are not in place: */
  164. int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
  165. in_page = desc->pages[i];
  166. } else {
  167. in_page = sg_page(sg);
  168. }
  169. sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
  170. sg->offset);
  171. sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
  172. sg->offset);
  173. desc->fragno++;
  174. desc->fraglen += sg->length;
  175. desc->pos += sg->length;
  176. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  177. thislen -= fraglen;
  178. if (thislen == 0)
  179. return 0;
  180. sg_mark_end(&desc->infrags[desc->fragno - 1]);
  181. sg_mark_end(&desc->outfrags[desc->fragno - 1]);
  182. ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
  183. desc->infrags, thislen);
  184. if (ret)
  185. return ret;
  186. sg_init_table(desc->infrags, 4);
  187. sg_init_table(desc->outfrags, 4);
  188. if (fraglen) {
  189. sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
  190. sg->offset + sg->length - fraglen);
  191. desc->infrags[0] = desc->outfrags[0];
  192. sg_assign_page(&desc->infrags[0], in_page);
  193. desc->fragno = 1;
  194. desc->fraglen = fraglen;
  195. } else {
  196. desc->fragno = 0;
  197. desc->fraglen = 0;
  198. }
  199. return 0;
  200. }
  201. int
  202. gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  203. int offset, struct page **pages)
  204. {
  205. int ret;
  206. struct encryptor_desc desc;
  207. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  208. memset(desc.iv, 0, sizeof(desc.iv));
  209. desc.desc.tfm = tfm;
  210. desc.desc.info = desc.iv;
  211. desc.desc.flags = 0;
  212. desc.pos = offset;
  213. desc.outbuf = buf;
  214. desc.pages = pages;
  215. desc.fragno = 0;
  216. desc.fraglen = 0;
  217. sg_init_table(desc.infrags, 4);
  218. sg_init_table(desc.outfrags, 4);
  219. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  220. return ret;
  221. }
  222. struct decryptor_desc {
  223. u8 iv[8]; /* XXX hard-coded blocksize */
  224. struct blkcipher_desc desc;
  225. struct scatterlist frags[4];
  226. int fragno;
  227. int fraglen;
  228. };
  229. static int
  230. decryptor(struct scatterlist *sg, void *data)
  231. {
  232. struct decryptor_desc *desc = data;
  233. int thislen = desc->fraglen + sg->length;
  234. int fraglen, ret;
  235. /* Worst case is 4 fragments: head, end of page 1, start
  236. * of page 2, tail. Anything more is a bug. */
  237. BUG_ON(desc->fragno > 3);
  238. sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
  239. sg->offset);
  240. desc->fragno++;
  241. desc->fraglen += sg->length;
  242. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  243. thislen -= fraglen;
  244. if (thislen == 0)
  245. return 0;
  246. sg_mark_end(&desc->frags[desc->fragno - 1]);
  247. ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
  248. desc->frags, thislen);
  249. if (ret)
  250. return ret;
  251. sg_init_table(desc->frags, 4);
  252. if (fraglen) {
  253. sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
  254. sg->offset + sg->length - fraglen);
  255. desc->fragno = 1;
  256. desc->fraglen = fraglen;
  257. } else {
  258. desc->fragno = 0;
  259. desc->fraglen = 0;
  260. }
  261. return 0;
  262. }
  263. int
  264. gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  265. int offset)
  266. {
  267. struct decryptor_desc desc;
  268. /* XXXJBF: */
  269. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  270. memset(desc.iv, 0, sizeof(desc.iv));
  271. desc.desc.tfm = tfm;
  272. desc.desc.info = desc.iv;
  273. desc.desc.flags = 0;
  274. desc.fragno = 0;
  275. desc.fraglen = 0;
  276. sg_init_table(desc.frags, 4);
  277. return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  278. }