gss_krb5_crypto.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/types.h>
  36. #include <linux/mm.h>
  37. #include <linux/slab.h>
  38. #include <asm/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. #ifdef RPC_DEBUG
  44. # define RPCDBG_FACILITY RPCDBG_AUTH
  45. #endif
  46. u32
  47. krb5_encrypt(
  48. struct crypto_tfm *tfm,
  49. void * iv,
  50. void * in,
  51. void * out,
  52. int length)
  53. {
  54. u32 ret = -EINVAL;
  55. struct scatterlist sg[1];
  56. u8 local_iv[16] = {0};
  57. dprintk("RPC: krb5_encrypt: input data:\n");
  58. print_hexl((u32 *)in, length, 0);
  59. if (length % crypto_tfm_alg_blocksize(tfm) != 0)
  60. goto out;
  61. if (crypto_tfm_alg_ivsize(tfm) > 16) {
  62. dprintk("RPC: gss_k5encrypt: tfm iv size to large %d\n",
  63. crypto_tfm_alg_ivsize(tfm));
  64. goto out;
  65. }
  66. if (iv)
  67. memcpy(local_iv, iv, crypto_tfm_alg_ivsize(tfm));
  68. memcpy(out, in, length);
  69. sg[0].page = virt_to_page(out);
  70. sg[0].offset = offset_in_page(out);
  71. sg[0].length = length;
  72. ret = crypto_cipher_encrypt_iv(tfm, sg, sg, length, local_iv);
  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_tfm *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. dprintk("RPC: krb5_decrypt: input data:\n");
  92. print_hexl((u32 *)in, length, 0);
  93. if (length % crypto_tfm_alg_blocksize(tfm) != 0)
  94. goto out;
  95. if (crypto_tfm_alg_ivsize(tfm) > 16) {
  96. dprintk("RPC: gss_k5decrypt: tfm iv size to large %d\n",
  97. crypto_tfm_alg_ivsize(tfm));
  98. goto out;
  99. }
  100. if (iv)
  101. memcpy(local_iv,iv, crypto_tfm_alg_ivsize(tfm));
  102. memcpy(out, in, length);
  103. sg[0].page = virt_to_page(out);
  104. sg[0].offset = offset_in_page(out);
  105. sg[0].length = length;
  106. ret = crypto_cipher_decrypt_iv(tfm, sg, sg, length, local_iv);
  107. dprintk("RPC: krb5_decrypt: output_data:\n");
  108. print_hexl((u32 *)out, length, 0);
  109. out:
  110. dprintk("RPC: gss_k5decrypt returns %d\n",ret);
  111. return(ret);
  112. }
  113. EXPORT_SYMBOL(krb5_decrypt);
  114. static void
  115. buf_to_sg(struct scatterlist *sg, char *ptr, int len) {
  116. sg->page = virt_to_page(ptr);
  117. sg->offset = offset_in_page(ptr);
  118. sg->length = len;
  119. }
  120. static int
  121. process_xdr_buf(struct xdr_buf *buf, int offset, int len,
  122. int (*actor)(struct scatterlist *, void *), void *data)
  123. {
  124. int i, page_len, thislen, page_offset, ret = 0;
  125. struct scatterlist sg[1];
  126. if (offset >= buf->head[0].iov_len) {
  127. offset -= buf->head[0].iov_len;
  128. } else {
  129. thislen = buf->head[0].iov_len - offset;
  130. if (thislen > len)
  131. thislen = len;
  132. buf_to_sg(sg, buf->head[0].iov_base + offset, thislen);
  133. ret = actor(sg, data);
  134. if (ret)
  135. goto out;
  136. offset = 0;
  137. len -= thislen;
  138. }
  139. if (len == 0)
  140. goto out;
  141. if (offset >= buf->page_len) {
  142. offset -= buf->page_len;
  143. } else {
  144. page_len = buf->page_len - offset;
  145. if (page_len > len)
  146. page_len = len;
  147. len -= page_len;
  148. page_offset = (offset + buf->page_base) & (PAGE_CACHE_SIZE - 1);
  149. i = (offset + buf->page_base) >> PAGE_CACHE_SHIFT;
  150. thislen = PAGE_CACHE_SIZE - page_offset;
  151. do {
  152. if (thislen > page_len)
  153. thislen = page_len;
  154. sg->page = buf->pages[i];
  155. sg->offset = page_offset;
  156. sg->length = thislen;
  157. ret = actor(sg, data);
  158. if (ret)
  159. goto out;
  160. page_len -= thislen;
  161. i++;
  162. page_offset = 0;
  163. thislen = PAGE_CACHE_SIZE;
  164. } while (page_len != 0);
  165. offset = 0;
  166. }
  167. if (len == 0)
  168. goto out;
  169. if (offset < buf->tail[0].iov_len) {
  170. thislen = buf->tail[0].iov_len - offset;
  171. if (thislen > len)
  172. thislen = len;
  173. buf_to_sg(sg, buf->tail[0].iov_base + offset, thislen);
  174. ret = actor(sg, data);
  175. len -= thislen;
  176. }
  177. if (len != 0)
  178. ret = -EINVAL;
  179. out:
  180. return ret;
  181. }
  182. static int
  183. checksummer(struct scatterlist *sg, void *data)
  184. {
  185. struct crypto_tfm *tfm = (struct crypto_tfm *)data;
  186. crypto_digest_update(tfm, sg, 1);
  187. return 0;
  188. }
  189. /* checksum the plaintext data and hdrlen bytes of the token header */
  190. s32
  191. make_checksum(s32 cksumtype, char *header, int hdrlen, struct xdr_buf *body,
  192. struct xdr_netobj *cksum)
  193. {
  194. char *cksumname;
  195. struct crypto_tfm *tfm = NULL; /* XXX add to ctx? */
  196. struct scatterlist sg[1];
  197. u32 code = GSS_S_FAILURE;
  198. switch (cksumtype) {
  199. case CKSUMTYPE_RSA_MD5:
  200. cksumname = "md5";
  201. break;
  202. default:
  203. dprintk("RPC: krb5_make_checksum:"
  204. " unsupported checksum %d", cksumtype);
  205. goto out;
  206. }
  207. if (!(tfm = crypto_alloc_tfm(cksumname, CRYPTO_TFM_REQ_MAY_SLEEP)))
  208. goto out;
  209. cksum->len = crypto_tfm_alg_digestsize(tfm);
  210. if ((cksum->data = kmalloc(cksum->len, GFP_KERNEL)) == NULL)
  211. goto out;
  212. crypto_digest_init(tfm);
  213. buf_to_sg(sg, header, hdrlen);
  214. crypto_digest_update(tfm, sg, 1);
  215. process_xdr_buf(body, 0, body->len, checksummer, tfm);
  216. crypto_digest_final(tfm, cksum->data);
  217. code = 0;
  218. out:
  219. crypto_free_tfm(tfm);
  220. return code;
  221. }
  222. EXPORT_SYMBOL(make_checksum);