gss_krb5_crypto.c 8.1 KB

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