Back to index

glibc  2.9
xcrypt.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 
00030 /*
00031  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
00032  */
00033 
00034 #if 0
00035 #ident "@(#)xcrypt.c 1.11   94/08/23 SMI"
00036 #endif
00037 
00038 #if !defined(lint) && defined(SCCSIDS)
00039 static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
00040 #endif
00041 
00042 /*
00043  * xcrypt.c: Hex encryption/decryption and utility routines
00044  */
00045 
00046 #include <ctype.h>
00047 #include <stdio.h>
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <sys/types.h>
00051 #include <rpc/des_crypt.h>
00052 
00053 static const char hex[16] =
00054 {
00055   '0', '1', '2', '3', '4', '5', '6', '7',
00056   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
00057 };
00058 
00059 
00060 #ifdef _LIBC
00061 # define hexval(c) \
00062   (c >= '0' && c <= '9'                                                     \
00063    ? c - '0'                                                         \
00064    : ({       int upp = toupper (c);                                               \
00065        upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; }))
00066 #else
00067 static char hexval (char) internal_function;
00068 #endif
00069 
00070 static void hex2bin (int, char *, char *) internal_function;
00071 static void bin2hex (int, unsigned char *, char *) internal_function;
00072 void passwd2des_internal (char *pw, char *key);
00073 #ifdef _LIBC
00074 libc_hidden_proto (passwd2des_internal)
00075 #endif
00076 
00077 /*
00078  * Turn password into DES key
00079  */
00080 void
00081 passwd2des_internal (char *pw, char *key)
00082 {
00083   int i;
00084 
00085   memset (key, 0, 8);
00086   for (i = 0; *pw && i < 8; ++i)
00087     key[i] ^= *pw++ << 1;
00088 
00089   des_setparity (key);
00090 }
00091 
00092 #ifdef _LIBC
00093 libc_hidden_def (passwd2des_internal)
00094 strong_alias (passwd2des_internal, passwd2des)
00095 #else
00096 void passwd2des (char *pw, char *key)
00097 {
00098   return passwd2des_internal (pw, key);
00099 }
00100 #endif
00101 
00102 /*
00103  * Encrypt a secret key given passwd
00104  * The secret key is passed and returned in hex notation.
00105  * Its length must be a multiple of 16 hex digits (64 bits).
00106  */
00107 int
00108 xencrypt (char *secret, char *passwd)
00109 {
00110   char key[8];
00111   char ivec[8];
00112   char *buf;
00113   int err;
00114   int len;
00115 
00116   len = strlen (secret) / 2;
00117   buf = malloc ((unsigned) len);
00118   hex2bin (len, secret, buf);
00119   passwd2des_internal (passwd, key);
00120   memset (ivec, 0, 8);
00121 
00122   err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
00123   if (DES_FAILED (err))
00124     {
00125       free (buf);
00126       return 0;
00127     }
00128   bin2hex (len, (unsigned char *) buf, secret);
00129   free (buf);
00130   return 1;
00131 }
00132 
00133 /*
00134  * Decrypt secret key using passwd
00135  * The secret key is passed and returned in hex notation.
00136  * Once again, the length is a multiple of 16 hex digits
00137  */
00138 int
00139 xdecrypt (char *secret, char *passwd)
00140 {
00141   char key[8];
00142   char ivec[8];
00143   char *buf;
00144   int err;
00145   int len;
00146 
00147   len = strlen (secret) / 2;
00148   buf = malloc ((unsigned) len);
00149 
00150   hex2bin (len, secret, buf);
00151   passwd2des_internal (passwd, key);
00152   memset (ivec, 0, 8);
00153 
00154   err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
00155   if (DES_FAILED (err))
00156     {
00157       free (buf);
00158       return 0;
00159     }
00160   bin2hex (len, (unsigned char *) buf, secret);
00161   free (buf);
00162   return 1;
00163 }
00164 
00165 /*
00166  * Hex to binary conversion
00167  */
00168 static void
00169 internal_function
00170 hex2bin (int len, char *hexnum, char *binnum)
00171 {
00172   int i;
00173 
00174   for (i = 0; i < len; i++)
00175     *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
00176 }
00177 
00178 /*
00179  * Binary to hex conversion
00180  */
00181 static void
00182 internal_function
00183 bin2hex (int len, unsigned char *binnum, char *hexnum)
00184 {
00185   int i;
00186   unsigned val;
00187 
00188   for (i = 0; i < len; i++)
00189     {
00190       val = binnum[i];
00191       hexnum[i * 2] = hex[val >> 4];
00192       hexnum[i * 2 + 1] = hex[val & 0xf];
00193     }
00194   hexnum[len * 2] = 0;
00195 }
00196 
00197 #ifndef _LIBC
00198 static char
00199 hexval (char c)
00200 {
00201   if (c >= '0' && c <= '9')
00202     return (c - '0');
00203   else if (c >= 'a' && c <= 'z')
00204     return (c - 'a' + 10);
00205   else if (c >= 'A' && c <= 'Z')
00206     return (c - 'A' + 10);
00207   else
00208     return -1;
00209 }
00210 #endif