xcrypt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2010, Oracle America, Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * * Neither the name of the "Oracle America, Inc." nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #if 0
  32. #ident "@(#)xcrypt.c 1.11 94/08/23 SMI"
  33. #endif
  34. #if !defined(lint) && defined(SCCSIDS)
  35. static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
  36. #endif
  37. /*
  38. * xcrypt.c: Hex encryption/decryption and utility routines
  39. */
  40. #include <ctype.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include <rpc/des_crypt.h>
  46. #include <shlib-compat.h>
  47. static const char hex[16] =
  48. {
  49. '0', '1', '2', '3', '4', '5', '6', '7',
  50. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  51. };
  52. #ifdef _LIBC
  53. # define hexval(c) \
  54. (c >= '0' && c <= '9' \
  55. ? c - '0' \
  56. : ({ int upp = toupper (c); \
  57. upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; }))
  58. #else
  59. static char hexval (char);
  60. #endif
  61. static void hex2bin (int, char *, char *);
  62. static void bin2hex (int, unsigned char *, char *);
  63. void passwd2des_internal (char *pw, char *key);
  64. #ifdef _LIBC
  65. libc_hidden_proto (passwd2des_internal)
  66. #endif
  67. /*
  68. * Turn password into DES key
  69. */
  70. void
  71. passwd2des_internal (char *pw, char *key)
  72. {
  73. int i;
  74. memset (key, 0, 8);
  75. for (i = 0; *pw && i < 8; ++i)
  76. key[i] ^= *pw++ << 1;
  77. des_setparity (key);
  78. }
  79. #ifdef _LIBC
  80. libc_hidden_def (passwd2des_internal)
  81. libc_sunrpc_symbol(passwd2des_internal, passwd2des, GLIBC_2_1)
  82. #else
  83. void passwd2des (char *pw, char *key)
  84. {
  85. return passwd2des_internal (pw, key);
  86. }
  87. #endif
  88. /*
  89. * Encrypt a secret key given passwd
  90. * The secret key is passed and returned in hex notation.
  91. * Its length must be a multiple of 16 hex digits (64 bits).
  92. */
  93. int
  94. xencrypt (char *secret, char *passwd)
  95. {
  96. char key[8];
  97. char ivec[8];
  98. char *buf;
  99. int err;
  100. int len;
  101. len = strlen (secret) / 2;
  102. buf = malloc ((unsigned) len);
  103. hex2bin (len, secret, buf);
  104. passwd2des_internal (passwd, key);
  105. memset (ivec, 0, 8);
  106. err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
  107. if (DES_FAILED (err))
  108. {
  109. free (buf);
  110. return 0;
  111. }
  112. bin2hex (len, (unsigned char *) buf, secret);
  113. free (buf);
  114. return 1;
  115. }
  116. libc_hidden_nolink_sunrpc (xencrypt, GLIBC_2_0)
  117. /*
  118. * Decrypt secret key using passwd
  119. * The secret key is passed and returned in hex notation.
  120. * Once again, the length is a multiple of 16 hex digits
  121. */
  122. int
  123. xdecrypt (char *secret, char *passwd)
  124. {
  125. char key[8];
  126. char ivec[8];
  127. char *buf;
  128. int err;
  129. int len;
  130. len = strlen (secret) / 2;
  131. buf = malloc ((unsigned) len);
  132. hex2bin (len, secret, buf);
  133. passwd2des_internal (passwd, key);
  134. memset (ivec, 0, 8);
  135. err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
  136. if (DES_FAILED (err))
  137. {
  138. free (buf);
  139. return 0;
  140. }
  141. bin2hex (len, (unsigned char *) buf, secret);
  142. free (buf);
  143. return 1;
  144. }
  145. #ifdef EXPORT_RPC_SYMBOLS
  146. libc_hidden_def (xdecrypt)
  147. #else
  148. libc_hidden_nolink_sunrpc (xdecrypt, GLIBC_2_1)
  149. #endif
  150. /*
  151. * Hex to binary conversion
  152. */
  153. static void
  154. hex2bin (int len, char *hexnum, char *binnum)
  155. {
  156. int i;
  157. for (i = 0; i < len; i++)
  158. *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
  159. }
  160. /*
  161. * Binary to hex conversion
  162. */
  163. static void
  164. bin2hex (int len, unsigned char *binnum, char *hexnum)
  165. {
  166. int i;
  167. unsigned val;
  168. for (i = 0; i < len; i++)
  169. {
  170. val = binnum[i];
  171. hexnum[i * 2] = hex[val >> 4];
  172. hexnum[i * 2 + 1] = hex[val & 0xf];
  173. }
  174. hexnum[len * 2] = 0;
  175. }
  176. #ifndef _LIBC
  177. static char
  178. hexval (char c)
  179. {
  180. if (c >= '0' && c <= '9')
  181. return (c - '0');
  182. else if (c >= 'a' && c <= 'z')
  183. return (c - 'a' + 10);
  184. else if (c >= 'A' && c <= 'Z')
  185. return (c - 'A' + 10);
  186. else
  187. return -1;
  188. }
  189. #endif