mux/src/crypt/crypt-entry.cpp

Go to the documentation of this file.
00001 /*
00002  * UFC-crypt: ultra fast crypt(3) implementation
00003  *
00004  * Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; see the file COPYING.LIB.  If not,
00018  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  *
00021  * crypt entry points
00022  *
00023  * @(#)crypt-entry.c    1.2 12/20/96
00024  *
00025  */
00026 
00027 #ifdef DEBUG
00028 #include <stdio.h>
00029 #endif
00030 #include <string.h>
00031 
00032 #ifndef STATIC
00033 #define STATIC static
00034 #endif
00035 
00036 #ifndef DOS
00037 #include "ufc-crypt.h"
00038 #else
00039 /*
00040  * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein)
00041  * for DOS patches
00042  */
00043 #include "pl.h"
00044 #include "ufc.h"
00045 #endif
00046 #include "crypt.h"
00047 #include "crypt-private.h"
00048 
00049 /* Prototypes for local functions.  */
00050 extern void _ufc_setup_salt_r(const char *s, struct crypt_data *__data);
00051 extern void _ufc_mk_keytab_r(char *key, struct crypt_data *__data);
00052 extern void _ufc_dofinalperm_r (ufc_long *res, struct crypt_data *__data);
00053 extern void _ufc_output_conversion_r (ufc_long v1, ufc_long v2,
00054                       const char *salt,
00055                       struct crypt_data *__data);
00056 
00057 extern char *__md5_crypt_r (const char *key, const char *salt, char *buffer,
00058                 int buflen);
00059 extern char *__md5_crypt (const char *key, const char *salt);
00060 
00061 /* Define our magic string to mark salt for MD5 encryption
00062    replacement.  This is meant to be the same as for other MD5 based
00063    encryption implementations.  */
00064 static const char md5_salt_prefix[] = "$1$";
00065 
00066 /* For use by the old, non-reentrant routines (crypt/encrypt/setkey)  */
00067 extern struct crypt_data _ufc_foobar;
00068 
00069 /*
00070  * UNIX crypt function
00071  */
00072 
00073 char *
00074 crypt_r (const char *key, const char *salt, struct crypt_data *data)
00075 {
00076   ufc_long res[4];
00077   char ktab[9];
00078   ufc_long xx = 25; /* to cope with GCC long long compiler bugs */
00079 
00080 #ifdef _LIBC
00081   /* Try to find out whether we have to use MD5 encryption replacement.  */
00082   if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
00083     return __md5_crypt_r (key, salt, (char *) data,
00084               sizeof (struct crypt_data));
00085 #endif
00086 
00087   /*
00088    * Hack DES tables according to salt
00089    */
00090   _ufc_setup_salt_r (salt, data);
00091 
00092   /*
00093    * Setup key schedule
00094    */
00095   memset(ktab, 0, sizeof(ktab));
00096   strncpy (ktab, key, 8);
00097   _ufc_mk_keytab_r (ktab, data);
00098 
00099   /*
00100    * Go for the 25 DES encryptions
00101    */
00102   memset(res, 0, sizeof (res));
00103   _ufc_doit_r (xx,  data, &res[0]);
00104 
00105   /*
00106    * Do final permutations
00107    */
00108   _ufc_dofinalperm_r (res, data);
00109 
00110   /*
00111    * And convert back to 6 bit ASCII
00112    */
00113   _ufc_output_conversion_r (res[0], res[1], salt, data);
00114   return data->crypt_3_buf;
00115 }
00116 //weak_alias (__crypt_r, crypt_r)
00117 
00118 char *crypt(const char *key, const char *salt)
00119 {
00120 #ifdef _LIBC
00121   /* Try to find out whether we have to use MD5 encryption replacement.  */
00122   if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
00123     return __md5_crypt (key, salt);
00124 #endif
00125 
00126   return crypt_r (key, salt, &_ufc_foobar);
00127 }
00128 
00129 
00130 /*
00131  * To make fcrypt users happy.
00132  * They don't need to call init_des.
00133  */
00134 #ifdef _LIBC
00135 weak_alias (crypt, fcrypt)
00136 #else
00137 char *
00138 __fcrypt (const char *key, const char *salt)
00139 {
00140   return crypt (key, salt);
00141 }
00142 #endif

Generated on Mon May 28 04:40:08 2007 for MUX by  doxygen 1.4.7