gss_krb5_crypto.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /* checksum the plaintext data and hdrlen bytes of the token header */
  121. s32
  122. make_checksum(s32 cksumtype, char *header, int hdrlen, struct xdr_buf *body,
  123. struct xdr_netobj *cksum)
  124. {
  125. char *cksumname;
  126. struct crypto_tfm *tfm = NULL; /* XXX add to ctx? */
  127. struct scatterlist sg[1];
  128. u32 code = GSS_S_FAILURE;
  129. int len, thislen, offset;
  130. int i;
  131. switch (cksumtype) {
  132. case CKSUMTYPE_RSA_MD5:
  133. cksumname = "md5";
  134. break;
  135. default:
  136. dprintk("RPC: krb5_make_checksum:"
  137. " unsupported checksum %d", cksumtype);
  138. goto out;
  139. }
  140. if (!(tfm = crypto_alloc_tfm(cksumname, 0)))
  141. goto out;
  142. cksum->len = crypto_tfm_alg_digestsize(tfm);
  143. if ((cksum->data = kmalloc(cksum->len, GFP_KERNEL)) == NULL)
  144. goto out;
  145. crypto_digest_init(tfm);
  146. buf_to_sg(sg, header, hdrlen);
  147. crypto_digest_update(tfm, sg, 1);
  148. if (body->head[0].iov_len) {
  149. buf_to_sg(sg, body->head[0].iov_base, body->head[0].iov_len);
  150. crypto_digest_update(tfm, sg, 1);
  151. }
  152. len = body->page_len;
  153. if (len != 0) {
  154. offset = body->page_base & (PAGE_CACHE_SIZE - 1);
  155. i = body->page_base >> PAGE_CACHE_SHIFT;
  156. thislen = PAGE_CACHE_SIZE - offset;
  157. do {
  158. if (thislen > len)
  159. thislen = len;
  160. sg->page = body->pages[i];
  161. sg->offset = offset;
  162. sg->length = thislen;
  163. kmap(sg->page); /* XXX kmap_atomic? */
  164. crypto_digest_update(tfm, sg, 1);
  165. kunmap(sg->page);
  166. len -= thislen;
  167. i++;
  168. offset = 0;
  169. thislen = PAGE_CACHE_SIZE;
  170. } while(len != 0);
  171. }
  172. if (body->tail[0].iov_len) {
  173. buf_to_sg(sg, body->tail[0].iov_base, body->tail[0].iov_len);
  174. crypto_digest_update(tfm, sg, 1);
  175. }
  176. crypto_digest_final(tfm, cksum->data);
  177. code = 0;
  178. out:
  179. if (tfm)
  180. crypto_free_tfm(tfm);
  181. return code;
  182. }
  183. EXPORT_SYMBOL(make_checksum);