gss_krb5_crypto.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include <linux/sunrpc/xdr.h>
  45. #ifdef RPC_DEBUG
  46. # define RPCDBG_FACILITY RPCDBG_AUTH
  47. #endif
  48. u32
  49. krb5_encrypt(
  50. struct crypto_blkcipher *tfm,
  51. void * iv,
  52. void * in,
  53. void * out,
  54. int length)
  55. {
  56. u32 ret = -EINVAL;
  57. struct scatterlist sg[1];
  58. u8 local_iv[16] = {0};
  59. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  60. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  61. goto out;
  62. if (crypto_blkcipher_ivsize(tfm) > 16) {
  63. dprintk("RPC: gss_k5encrypt: tfm iv size too large %d\n",
  64. crypto_blkcipher_ivsize(tfm));
  65. goto out;
  66. }
  67. if (iv)
  68. memcpy(local_iv, iv, crypto_blkcipher_ivsize(tfm));
  69. memcpy(out, in, length);
  70. sg_init_one(sg, out, length);
  71. ret = crypto_blkcipher_encrypt_iv(&desc, sg, sg, length);
  72. out:
  73. dprintk("RPC: krb5_encrypt returns %d\n", ret);
  74. return ret;
  75. }
  76. u32
  77. krb5_decrypt(
  78. struct crypto_blkcipher *tfm,
  79. void * iv,
  80. void * in,
  81. void * out,
  82. int length)
  83. {
  84. u32 ret = -EINVAL;
  85. struct scatterlist sg[1];
  86. u8 local_iv[16] = {0};
  87. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  88. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  89. goto out;
  90. if (crypto_blkcipher_ivsize(tfm) > 16) {
  91. dprintk("RPC: gss_k5decrypt: tfm iv size too large %d\n",
  92. crypto_blkcipher_ivsize(tfm));
  93. goto out;
  94. }
  95. if (iv)
  96. memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
  97. memcpy(out, in, length);
  98. sg_init_one(sg, out, length);
  99. ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
  100. out:
  101. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  102. return ret;
  103. }
  104. static int
  105. checksummer(struct scatterlist *sg, void *data)
  106. {
  107. struct hash_desc *desc = data;
  108. return crypto_hash_update(desc, sg, sg->length);
  109. }
  110. /* checksum the plaintext data and hdrlen bytes of the token header */
  111. s32
  112. make_checksum(char *cksumname, char *header, int hdrlen, struct xdr_buf *body,
  113. int body_offset, struct xdr_netobj *cksum)
  114. {
  115. struct hash_desc desc; /* XXX add to ctx? */
  116. struct scatterlist sg[1];
  117. int err;
  118. desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC);
  119. if (IS_ERR(desc.tfm))
  120. return GSS_S_FAILURE;
  121. cksum->len = crypto_hash_digestsize(desc.tfm);
  122. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  123. err = crypto_hash_init(&desc);
  124. if (err)
  125. goto out;
  126. sg_init_one(sg, header, hdrlen);
  127. err = crypto_hash_update(&desc, sg, hdrlen);
  128. if (err)
  129. goto out;
  130. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  131. checksummer, &desc);
  132. if (err)
  133. goto out;
  134. err = crypto_hash_final(&desc, cksum->data);
  135. out:
  136. crypto_free_hash(desc.tfm);
  137. return err ? GSS_S_FAILURE : 0;
  138. }
  139. struct encryptor_desc {
  140. u8 iv[8]; /* XXX hard-coded blocksize */
  141. struct blkcipher_desc desc;
  142. int pos;
  143. struct xdr_buf *outbuf;
  144. struct page **pages;
  145. struct scatterlist infrags[4];
  146. struct scatterlist outfrags[4];
  147. int fragno;
  148. int fraglen;
  149. };
  150. static int
  151. encryptor(struct scatterlist *sg, void *data)
  152. {
  153. struct encryptor_desc *desc = data;
  154. struct xdr_buf *outbuf = desc->outbuf;
  155. struct page *in_page;
  156. int thislen = desc->fraglen + sg->length;
  157. int fraglen, ret;
  158. int page_pos;
  159. /* Worst case is 4 fragments: head, end of page 1, start
  160. * of page 2, tail. Anything more is a bug. */
  161. BUG_ON(desc->fragno > 3);
  162. page_pos = desc->pos - outbuf->head[0].iov_len;
  163. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  164. /* pages are not in place: */
  165. int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
  166. in_page = desc->pages[i];
  167. } else {
  168. in_page = sg_page(sg);
  169. }
  170. sg_set_page(&desc->infrags[desc->fragno], in_page, sg->length,
  171. sg->offset);
  172. sg_set_page(&desc->outfrags[desc->fragno], sg_page(sg), sg->length,
  173. sg->offset);
  174. desc->fragno++;
  175. desc->fraglen += sg->length;
  176. desc->pos += sg->length;
  177. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  178. thislen -= fraglen;
  179. if (thislen == 0)
  180. return 0;
  181. sg_mark_end(&desc->infrags[desc->fragno - 1]);
  182. sg_mark_end(&desc->outfrags[desc->fragno - 1]);
  183. ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
  184. desc->infrags, thislen);
  185. if (ret)
  186. return ret;
  187. sg_init_table(desc->infrags, 4);
  188. sg_init_table(desc->outfrags, 4);
  189. if (fraglen) {
  190. sg_set_page(&desc->outfrags[0], sg_page(sg), fraglen,
  191. sg->offset + sg->length - fraglen);
  192. desc->infrags[0] = desc->outfrags[0];
  193. sg_assign_page(&desc->infrags[0], in_page);
  194. desc->fragno = 1;
  195. desc->fraglen = fraglen;
  196. } else {
  197. desc->fragno = 0;
  198. desc->fraglen = 0;
  199. }
  200. return 0;
  201. }
  202. int
  203. gss_encrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  204. int offset, struct page **pages)
  205. {
  206. int ret;
  207. struct encryptor_desc desc;
  208. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  209. memset(desc.iv, 0, sizeof(desc.iv));
  210. desc.desc.tfm = tfm;
  211. desc.desc.info = desc.iv;
  212. desc.desc.flags = 0;
  213. desc.pos = offset;
  214. desc.outbuf = buf;
  215. desc.pages = pages;
  216. desc.fragno = 0;
  217. desc.fraglen = 0;
  218. sg_init_table(desc.infrags, 4);
  219. sg_init_table(desc.outfrags, 4);
  220. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  221. return ret;
  222. }
  223. struct decryptor_desc {
  224. u8 iv[8]; /* XXX hard-coded blocksize */
  225. struct blkcipher_desc desc;
  226. struct scatterlist frags[4];
  227. int fragno;
  228. int fraglen;
  229. };
  230. static int
  231. decryptor(struct scatterlist *sg, void *data)
  232. {
  233. struct decryptor_desc *desc = data;
  234. int thislen = desc->fraglen + sg->length;
  235. int fraglen, ret;
  236. /* Worst case is 4 fragments: head, end of page 1, start
  237. * of page 2, tail. Anything more is a bug. */
  238. BUG_ON(desc->fragno > 3);
  239. sg_set_page(&desc->frags[desc->fragno], sg_page(sg), sg->length,
  240. sg->offset);
  241. desc->fragno++;
  242. desc->fraglen += sg->length;
  243. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  244. thislen -= fraglen;
  245. if (thislen == 0)
  246. return 0;
  247. sg_mark_end(&desc->frags[desc->fragno - 1]);
  248. ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
  249. desc->frags, thislen);
  250. if (ret)
  251. return ret;
  252. sg_init_table(desc->frags, 4);
  253. if (fraglen) {
  254. sg_set_page(&desc->frags[0], sg_page(sg), fraglen,
  255. sg->offset + sg->length - fraglen);
  256. desc->fragno = 1;
  257. desc->fraglen = fraglen;
  258. } else {
  259. desc->fragno = 0;
  260. desc->fraglen = 0;
  261. }
  262. return 0;
  263. }
  264. int
  265. gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  266. int offset)
  267. {
  268. struct decryptor_desc desc;
  269. /* XXXJBF: */
  270. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  271. memset(desc.iv, 0, sizeof(desc.iv));
  272. desc.desc.tfm = tfm;
  273. desc.desc.info = desc.iv;
  274. desc.desc.flags = 0;
  275. desc.fragno = 0;
  276. desc.fraglen = 0;
  277. sg_init_table(desc.frags, 4);
  278. return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  279. }