gss_krb5_crypto.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 to 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_set_buf(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. EXPORT_SYMBOL(krb5_encrypt);
  77. u32
  78. krb5_decrypt(
  79. struct crypto_blkcipher *tfm,
  80. void * iv,
  81. void * in,
  82. void * out,
  83. int length)
  84. {
  85. u32 ret = -EINVAL;
  86. struct scatterlist sg[1];
  87. u8 local_iv[16] = {0};
  88. struct blkcipher_desc desc = { .tfm = tfm, .info = local_iv };
  89. if (length % crypto_blkcipher_blocksize(tfm) != 0)
  90. goto out;
  91. if (crypto_blkcipher_ivsize(tfm) > 16) {
  92. dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
  93. crypto_blkcipher_ivsize(tfm));
  94. goto out;
  95. }
  96. if (iv)
  97. memcpy(local_iv,iv, crypto_blkcipher_ivsize(tfm));
  98. memcpy(out, in, length);
  99. sg_set_buf(sg, out, length);
  100. ret = crypto_blkcipher_decrypt_iv(&desc, sg, sg, length);
  101. out:
  102. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(krb5_decrypt);
  106. static int
  107. checksummer(struct scatterlist *sg, void *data)
  108. {
  109. struct hash_desc *desc = data;
  110. return crypto_hash_update(desc, sg, sg->length);
  111. }
  112. /* checksum the plaintext data and hdrlen bytes of the token header */
  113. s32
  114. make_checksum(char *cksumname, char *header, int hdrlen, struct xdr_buf *body,
  115. int body_offset, struct xdr_netobj *cksum)
  116. {
  117. struct hash_desc desc; /* XXX add to ctx? */
  118. struct scatterlist sg[1];
  119. int err;
  120. desc.tfm = crypto_alloc_hash(cksumname, 0, CRYPTO_ALG_ASYNC);
  121. if (IS_ERR(desc.tfm))
  122. return GSS_S_FAILURE;
  123. cksum->len = crypto_hash_digestsize(desc.tfm);
  124. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  125. err = crypto_hash_init(&desc);
  126. if (err)
  127. goto out;
  128. sg_set_buf(sg, header, hdrlen);
  129. err = crypto_hash_update(&desc, sg, hdrlen);
  130. if (err)
  131. goto out;
  132. err = xdr_process_buf(body, body_offset, body->len - body_offset,
  133. checksummer, &desc);
  134. if (err)
  135. goto out;
  136. err = crypto_hash_final(&desc, cksum->data);
  137. out:
  138. crypto_free_hash(desc.tfm);
  139. return err ? GSS_S_FAILURE : 0;
  140. }
  141. EXPORT_SYMBOL(make_checksum);
  142. struct encryptor_desc {
  143. u8 iv[8]; /* XXX hard-coded blocksize */
  144. struct blkcipher_desc desc;
  145. int pos;
  146. struct xdr_buf *outbuf;
  147. struct page **pages;
  148. struct scatterlist infrags[4];
  149. struct scatterlist outfrags[4];
  150. int fragno;
  151. int fraglen;
  152. };
  153. static int
  154. encryptor(struct scatterlist *sg, void *data)
  155. {
  156. struct encryptor_desc *desc = data;
  157. struct xdr_buf *outbuf = desc->outbuf;
  158. struct page *in_page;
  159. int thislen = desc->fraglen + sg->length;
  160. int fraglen, ret;
  161. int page_pos;
  162. /* Worst case is 4 fragments: head, end of page 1, start
  163. * of page 2, tail. Anything more is a bug. */
  164. BUG_ON(desc->fragno > 3);
  165. desc->infrags[desc->fragno] = *sg;
  166. desc->outfrags[desc->fragno] = *sg;
  167. page_pos = desc->pos - outbuf->head[0].iov_len;
  168. if (page_pos >= 0 && page_pos < outbuf->page_len) {
  169. /* pages are not in place: */
  170. int i = (page_pos + outbuf->page_base) >> PAGE_CACHE_SHIFT;
  171. in_page = desc->pages[i];
  172. } else {
  173. in_page = sg->page;
  174. }
  175. desc->infrags[desc->fragno].page = in_page;
  176. desc->fragno++;
  177. desc->fraglen += sg->length;
  178. desc->pos += sg->length;
  179. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  180. thislen -= fraglen;
  181. if (thislen == 0)
  182. return 0;
  183. ret = crypto_blkcipher_encrypt_iv(&desc->desc, desc->outfrags,
  184. desc->infrags, thislen);
  185. if (ret)
  186. return ret;
  187. if (fraglen) {
  188. desc->outfrags[0].page = sg->page;
  189. desc->outfrags[0].offset = sg->offset + sg->length - fraglen;
  190. desc->outfrags[0].length = fraglen;
  191. desc->infrags[0] = desc->outfrags[0];
  192. desc->infrags[0].page = 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. ret = xdr_process_buf(buf, offset, buf->len - offset, encryptor, &desc);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(gss_encrypt_xdr_buf);
  221. struct decryptor_desc {
  222. u8 iv[8]; /* XXX hard-coded blocksize */
  223. struct blkcipher_desc desc;
  224. struct scatterlist frags[4];
  225. int fragno;
  226. int fraglen;
  227. };
  228. static int
  229. decryptor(struct scatterlist *sg, void *data)
  230. {
  231. struct decryptor_desc *desc = data;
  232. int thislen = desc->fraglen + sg->length;
  233. int fraglen, ret;
  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->frags[desc->fragno] = *sg;
  238. desc->fragno++;
  239. desc->fraglen += sg->length;
  240. fraglen = thislen & 7; /* XXX hardcoded blocksize */
  241. thislen -= fraglen;
  242. if (thislen == 0)
  243. return 0;
  244. ret = crypto_blkcipher_decrypt_iv(&desc->desc, desc->frags,
  245. desc->frags, thislen);
  246. if (ret)
  247. return ret;
  248. if (fraglen) {
  249. desc->frags[0].page = sg->page;
  250. desc->frags[0].offset = sg->offset + sg->length - fraglen;
  251. desc->frags[0].length = fraglen;
  252. desc->fragno = 1;
  253. desc->fraglen = fraglen;
  254. } else {
  255. desc->fragno = 0;
  256. desc->fraglen = 0;
  257. }
  258. return 0;
  259. }
  260. int
  261. gss_decrypt_xdr_buf(struct crypto_blkcipher *tfm, struct xdr_buf *buf,
  262. int offset)
  263. {
  264. struct decryptor_desc desc;
  265. /* XXXJBF: */
  266. BUG_ON((buf->len - offset) % crypto_blkcipher_blocksize(tfm) != 0);
  267. memset(desc.iv, 0, sizeof(desc.iv));
  268. desc.desc.tfm = tfm;
  269. desc.desc.info = desc.iv;
  270. desc.desc.flags = 0;
  271. desc.fragno = 0;
  272. desc.fraglen = 0;
  273. return xdr_process_buf(buf, offset, buf->len - offset, decryptor, &desc);
  274. }
  275. EXPORT_SYMBOL(gss_decrypt_xdr_buf);