]>
Commit | Line | Data |
---|---|---|
63f791d3 GK |
1 | /* |
2 | * UFC-crypt: ultra fast crypt(3) implementation | |
3 | * | |
c3266dc0 | 4 | * Copyright (C) 1991,1992,1993,1996,1997,2007 Free Software Foundation, Inc. |
63f791d3 | 5 | * |
a1b36134 AJ |
6 | * The GNU C Library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
63f791d3 | 8 | * License as published by the Free Software Foundation; either |
a1b36134 | 9 | * version 2.1 of the License, or (at your option) any later version. |
63f791d3 | 10 | * |
a1b36134 | 11 | * The GNU C Library is distributed in the hope that it will be useful, |
63f791d3 GK |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
a1b36134 | 14 | * Lesser General Public License for more details. |
63f791d3 | 15 | * |
a1b36134 AJ |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with the GNU C Library; if not, write to the Free | |
18 | * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | * 02111-1307 USA. | |
63f791d3 GK |
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); | |
c3266dc0 UD |
58 | extern char *__sha256_crypt_r (const char *key, const char *salt, |
59 | char *buffer, int buflen); | |
60 | extern char *__sha256_crypt (const char *key, const char *salt); | |
61 | extern char *__sha512_crypt_r (const char *key, const char *salt, | |
62 | char *buffer, int buflen); | |
63 | extern char *__sha512_crypt (const char *key, const char *salt); | |
63f791d3 GK |
64 | #endif |
65 | ||
66 | /* Define our magic string to mark salt for MD5 encryption | |
67 | replacement. This is meant to be the same as for other MD5 based | |
68 | encryption implementations. */ | |
69 | static const char md5_salt_prefix[] = "$1$"; | |
70 | ||
c3266dc0 UD |
71 | /* Magic string for SHA256 encryption. */ |
72 | static const char sha256_salt_prefix[] = "$5$"; | |
73 | ||
74 | /* Magic string for SHA512 encryption. */ | |
75 | static const char sha512_salt_prefix[] = "$6$"; | |
76 | ||
63f791d3 GK |
77 | /* For use by the old, non-reentrant routines (crypt/encrypt/setkey) */ |
78 | extern struct crypt_data _ufc_foobar; | |
79 | ||
80 | /* | |
81 | * UNIX crypt function | |
82 | */ | |
83 | ||
84 | char * | |
85 | __crypt_r (key, salt, data) | |
86 | const char *key; | |
87 | const char *salt; | |
88 | struct crypt_data * __restrict data; | |
89 | { | |
90 | ufc_long res[4]; | |
91 | char ktab[9]; | |
92 | ufc_long xx = 25; /* to cope with GCC long long compiler bugs */ | |
93 | ||
94 | #ifdef _LIBC | |
95 | /* Try to find out whether we have to use MD5 encryption replacement. */ | |
96 | if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) | |
97 | return __md5_crypt_r (key, salt, (char *) data, | |
98 | sizeof (struct crypt_data)); | |
c3266dc0 UD |
99 | |
100 | /* Try to find out whether we have to use SHA256 encryption replacement. */ | |
101 | if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0) | |
102 | return __sha256_crypt_r (key, salt, (char *) data, | |
103 | sizeof (struct crypt_data)); | |
104 | ||
105 | /* Try to find out whether we have to use SHA512 encryption replacement. */ | |
106 | if (strncmp (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0) | |
107 | return __sha512_crypt_r (key, salt, (char *) data, | |
108 | sizeof (struct crypt_data)); | |
63f791d3 GK |
109 | #endif |
110 | ||
111 | /* | |
112 | * Hack DES tables according to salt | |
113 | */ | |
114 | _ufc_setup_salt_r (salt, data); | |
115 | ||
116 | /* | |
117 | * Setup key schedule | |
118 | */ | |
119 | _ufc_clearmem (ktab, (int) sizeof (ktab)); | |
120 | (void) strncpy (ktab, key, 8); | |
121 | _ufc_mk_keytab_r (ktab, data); | |
122 | ||
123 | /* | |
124 | * Go for the 25 DES encryptions | |
125 | */ | |
126 | _ufc_clearmem ((char*) res, (int) sizeof (res)); | |
127 | _ufc_doit_r (xx, data, &res[0]); | |
128 | ||
129 | /* | |
130 | * Do final permutations | |
131 | */ | |
132 | _ufc_dofinalperm_r (res, data); | |
133 | ||
134 | /* | |
135 | * And convert back to 6 bit ASCII | |
136 | */ | |
137 | _ufc_output_conversion_r (res[0], res[1], salt, data); | |
138 | return data->crypt_3_buf; | |
139 | } | |
140 | weak_alias (__crypt_r, crypt_r) | |
141 | ||
142 | char * | |
143 | crypt (key, salt) | |
144 | const char *key; | |
145 | const char *salt; | |
146 | { | |
147 | #ifdef _LIBC | |
148 | /* Try to find out whether we have to use MD5 encryption replacement. */ | |
149 | if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) | |
150 | return __md5_crypt (key, salt); | |
c3266dc0 UD |
151 | |
152 | /* Try to find out whether we have to use SHA256 encryption replacement. */ | |
153 | if (strncmp (sha256_salt_prefix, salt, sizeof (sha256_salt_prefix) - 1) == 0) | |
154 | return __sha256_crypt (key, salt); | |
155 | ||
156 | /* Try to find out whether we have to use SHA512 encryption replacement. */ | |
157 | if (strncmp (sha512_salt_prefix, salt, sizeof (sha512_salt_prefix) - 1) == 0) | |
158 | return __sha512_crypt (key, salt); | |
63f791d3 GK |
159 | #endif |
160 | ||
161 | return __crypt_r (key, salt, &_ufc_foobar); | |
162 | } | |
163 | ||
164 | ||
165 | /* | |
166 | * To make fcrypt users happy. | |
167 | * They don't need to call init_des. | |
168 | */ | |
169 | #ifdef _LIBC | |
170 | weak_alias (crypt, fcrypt) | |
171 | #else | |
172 | char * | |
173 | __fcrypt (key, salt) | |
174 | const char *key; | |
175 | const char *salt; | |
176 | { | |
177 | return crypt (key, salt); | |
178 | } | |
179 | #endif |