9d99c53a2b81a32e404ec23f0bce4f7600e4e434
[platform/upstream/glibc.git] / crypt / crypt-entry.c
1 /*
2  * UFC-crypt: ultra fast crypt(3) implementation
3  *
4  * Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; see the file COPYING.LIB.  If not,
18  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * crypt entry points
22  *
23  * @(#)crypt-entry.c    1.2 12/20/96
24  *
25  */
26
27 #ifdef DEBUG
28 #include <stdio.h>
29 #endif
30 #include <string.h>
31
32 #ifndef STATIC
33 #define STATIC static
34 #endif
35
36 #ifndef DOS
37 #include "ufc-crypt.h"
38 #else
39 /*
40  * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein)
41  * for DOS patches
42  */
43 #include "ufc.h"
44 #endif
45 #include "crypt.h"
46 #include "crypt-private.h"
47
48 /* Prototypes for local functions.  */
49 #if __STDC__ - 0
50 #ifndef __GNU_LIBRARY__
51 void _ufc_clearmem (char *start, int cnt);
52 #else
53 #define _ufc_clearmem(start, cnt)   memset(start, 0, cnt)
54 #endif
55 extern char *__md5_crypt_r (const char *key, const char *salt, char *buffer,
56                             int buflen);
57 extern char *__md5_crypt (const char *key, const char *salt);
58 #endif
59
60 /* Define our magic string to mark salt for MD5 encryption
61    replacement.  This is meant to be the same as for other MD5 based
62    encryption implementations.  */
63 static const char md5_salt_prefix[] = "$1$";
64
65 /* For use by the old, non-reentrant routines (crypt/encrypt/setkey)  */
66 extern struct crypt_data _ufc_foobar;
67
68 /*
69  * UNIX crypt function
70  */
71
72 char *
73 __crypt_r (key, salt, data)
74      const char *key;
75      const char *salt;
76      struct crypt_data * __restrict data;
77 {
78   ufc_long res[4];
79   char ktab[9];
80   ufc_long xx = 25; /* to cope with GCC long long compiler bugs */
81
82 #ifdef _LIBC
83   /* Try to find out whether we have to use MD5 encryption replacement.  */
84   if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
85     return __md5_crypt_r (key, salt, (char *) data,
86                           sizeof (struct crypt_data));
87 #endif
88
89   /*
90    * Hack DES tables according to salt
91    */
92   _ufc_setup_salt_r (salt, data);
93
94   /*
95    * Setup key schedule
96    */
97   _ufc_clearmem (ktab, (int) sizeof (ktab));
98   (void) strncpy (ktab, key, 8);
99   _ufc_mk_keytab_r (ktab, data);
100
101   /*
102    * Go for the 25 DES encryptions
103    */
104   _ufc_clearmem ((char*) res, (int) sizeof (res));
105   _ufc_doit_r (xx,  data, &res[0]);
106
107   /*
108    * Do final permutations
109    */
110   _ufc_dofinalperm_r (res, data);
111
112   /*
113    * And convert back to 6 bit ASCII
114    */
115   _ufc_output_conversion_r (res[0], res[1], salt, data);
116   return data->crypt_3_buf;
117 }
118 weak_alias (__crypt_r, crypt_r)
119
120 char *
121 crypt (key, salt)
122      const char *key;
123      const char *salt;
124 {
125 #ifdef _LIBC
126   /* Try to find out whether we have to use MD5 encryption replacement.  */
127   if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0)
128     return __md5_crypt (key, salt);
129 #endif
130
131   return __crypt_r (key, salt, &_ufc_foobar);
132 }
133
134
135 /*
136  * To make fcrypt users happy.
137  * They don't need to call init_des.
138  */
139 #ifdef _LIBC
140 weak_alias (crypt, fcrypt)
141 #else
142 char *
143 __fcrypt (key, salt)
144      const char *key;
145      const char *salt;
146 {
147   return crypt (key, salt);
148 }
149 #endif