| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 |  | 
 | 32 | #if 0 | 
 | 33 | #ident	"@(#)xcrypt.c	1.11	94/08/23 SMI" | 
 | 34 | #endif | 
 | 35 |  | 
 | 36 | #if !defined(lint) && defined(SCCSIDS) | 
 | 37 | static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro"; | 
 | 38 | #endif | 
 | 39 |  | 
 | 40 | /* | 
 | 41 |  * xcrypt.c: Hex encryption/decryption and utility routines | 
 | 42 |  */ | 
 | 43 |  | 
 | 44 | #include <ctype.h> | 
 | 45 | #include <stdio.h> | 
 | 46 | #include <stdlib.h> | 
 | 47 | #include <string.h> | 
 | 48 | #include <sys/types.h> | 
 | 49 | #include <rpc/des_crypt.h> | 
 | 50 | #include <shlib-compat.h> | 
 | 51 |  | 
 | 52 | static const char hex[16] = | 
 | 53 | { | 
 | 54 |   '0', '1', '2', '3', '4', '5', '6', '7', | 
 | 55 |   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', | 
 | 56 | }; | 
 | 57 |  | 
 | 58 |  | 
 | 59 | #ifdef _LIBC | 
 | 60 | # define hexval(c) \ | 
 | 61 |   (c >= '0' && c <= '9'							      \ | 
 | 62 |    ? c - '0'								      \ | 
 | 63 |    : ({	int upp = toupper (c);						      \ | 
 | 64 | 	upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; })) | 
 | 65 | #else | 
 | 66 | static char hexval (char) internal_function; | 
 | 67 | #endif | 
 | 68 |  | 
 | 69 | static void hex2bin (int, char *, char *) internal_function; | 
 | 70 | static void bin2hex (int, unsigned char *, char *) internal_function; | 
 | 71 | void passwd2des_internal (char *pw, char *key); | 
 | 72 | #ifdef _LIBC | 
 | 73 | libc_hidden_proto (passwd2des_internal) | 
 | 74 | #endif | 
 | 75 |  | 
 | 76 | /* | 
 | 77 |  * Turn password into DES key | 
 | 78 |  */ | 
 | 79 | void | 
 | 80 | passwd2des_internal (char *pw, char *key) | 
 | 81 | { | 
 | 82 |   int i; | 
 | 83 |  | 
 | 84 |   memset (key, 0, 8); | 
 | 85 |   for (i = 0; *pw && i < 8; ++i) | 
 | 86 |     key[i] ^= *pw++ << 1; | 
 | 87 |  | 
 | 88 |   des_setparity (key); | 
 | 89 | } | 
 | 90 |  | 
 | 91 | #ifdef _LIBC | 
 | 92 | libc_hidden_def (passwd2des_internal) | 
 | 93 | libc_sunrpc_symbol(passwd2des_internal, passwd2des, GLIBC_2_1) | 
 | 94 | #else | 
 | 95 | void passwd2des (char *pw, char *key) | 
 | 96 | { | 
 | 97 |   return passwd2des_internal (pw, key); | 
 | 98 | } | 
 | 99 | #endif | 
 | 100 |  | 
 | 101 | /* | 
 | 102 |  * Encrypt a secret key given passwd | 
 | 103 |  * The secret key is passed and returned in hex notation. | 
 | 104 |  * Its length must be a multiple of 16 hex digits (64 bits). | 
 | 105 |  */ | 
 | 106 | int | 
 | 107 | xencrypt (char *secret, char *passwd) | 
 | 108 | { | 
 | 109 |   char key[8]; | 
 | 110 |   char ivec[8]; | 
 | 111 |   char *buf; | 
 | 112 |   int err; | 
 | 113 |   int len; | 
 | 114 |  | 
 | 115 |   len = strlen (secret) / 2; | 
 | 116 |   buf = malloc ((unsigned) len); | 
 | 117 |   hex2bin (len, secret, buf); | 
 | 118 |   passwd2des_internal (passwd, key); | 
 | 119 |   memset (ivec, 0, 8); | 
 | 120 |  | 
 | 121 |   err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec); | 
 | 122 |   if (DES_FAILED (err)) | 
 | 123 |     { | 
 | 124 |       free (buf); | 
 | 125 |       return 0; | 
 | 126 |     } | 
 | 127 |   bin2hex (len, (unsigned char *) buf, secret); | 
 | 128 |   free (buf); | 
 | 129 |   return 1; | 
 | 130 | } | 
 | 131 | libc_hidden_nolink_sunrpc (xencrypt, GLIBC_2_0) | 
 | 132 |  | 
 | 133 | /* | 
 | 134 |  * Decrypt secret key using passwd | 
 | 135 |  * The secret key is passed and returned in hex notation. | 
 | 136 |  * Once again, the length is a multiple of 16 hex digits | 
 | 137 |  */ | 
 | 138 | int | 
 | 139 | xdecrypt (char *secret, char *passwd) | 
 | 140 | { | 
 | 141 |   char key[8]; | 
 | 142 |   char ivec[8]; | 
 | 143 |   char *buf; | 
 | 144 |   int err; | 
 | 145 |   int len; | 
 | 146 |  | 
 | 147 |   len = strlen (secret) / 2; | 
 | 148 |   buf = malloc ((unsigned) len); | 
 | 149 |  | 
 | 150 |   hex2bin (len, secret, buf); | 
 | 151 |   passwd2des_internal (passwd, key); | 
 | 152 |   memset (ivec, 0, 8); | 
 | 153 |  | 
 | 154 |   err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec); | 
 | 155 |   if (DES_FAILED (err)) | 
 | 156 |     { | 
 | 157 |       free (buf); | 
 | 158 |       return 0; | 
 | 159 |     } | 
 | 160 |   bin2hex (len, (unsigned char *) buf, secret); | 
 | 161 |   free (buf); | 
 | 162 |   return 1; | 
 | 163 | } | 
 | 164 | #ifdef EXPORT_RPC_SYMBOLS | 
 | 165 | libc_hidden_def (xdecrypt) | 
 | 166 | #else | 
 | 167 | libc_hidden_nolink_sunrpc (xdecrypt, GLIBC_2_1) | 
 | 168 | #endif | 
 | 169 |  | 
 | 170 | /* | 
 | 171 |  * Hex to binary conversion | 
 | 172 |  */ | 
 | 173 | static void | 
 | 174 | internal_function | 
 | 175 | hex2bin (int len, char *hexnum, char *binnum) | 
 | 176 | { | 
 | 177 |   int i; | 
 | 178 |  | 
 | 179 |   for (i = 0; i < len; i++) | 
 | 180 |     *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]); | 
 | 181 | } | 
 | 182 |  | 
 | 183 | /* | 
 | 184 |  * Binary to hex conversion | 
 | 185 |  */ | 
 | 186 | static void | 
 | 187 | internal_function | 
 | 188 | bin2hex (int len, unsigned char *binnum, char *hexnum) | 
 | 189 | { | 
 | 190 |   int i; | 
 | 191 |   unsigned val; | 
 | 192 |  | 
 | 193 |   for (i = 0; i < len; i++) | 
 | 194 |     { | 
 | 195 |       val = binnum[i]; | 
 | 196 |       hexnum[i * 2] = hex[val >> 4]; | 
 | 197 |       hexnum[i * 2 + 1] = hex[val & 0xf]; | 
 | 198 |     } | 
 | 199 |   hexnum[len * 2] = 0; | 
 | 200 | } | 
 | 201 |  | 
 | 202 | #ifndef _LIBC | 
 | 203 | static char | 
 | 204 | hexval (char c) | 
 | 205 | { | 
 | 206 |   if (c >= '0' && c <= '9') | 
 | 207 |     return (c - '0'); | 
 | 208 |   else if (c >= 'a' && c <= 'z') | 
 | 209 |     return (c - 'a' + 10); | 
 | 210 |   else if (c >= 'A' && c <= 'Z') | 
 | 211 |     return (c - 'A' + 10); | 
 | 212 |   else | 
 | 213 |     return -1; | 
 | 214 | } | 
 | 215 | #endif |