2 * UFC-crypt: ultra fast crypt(3) implementation
4 * Copyright (C) 1991-1993,1996-1997,2007,2012 Free Software Foundation, Inc.
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
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * The GNU C 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 * Lesser General Public License for more details.
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
23 * @(#)crypt-entry.c 1.2 12/20/96
37 #include "ufc-crypt.h"
40 * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein)
46 #include "crypt-private.h"
48 /* Prototypes for local functions. */
49 #ifndef __GNU_LIBRARY__
50 void _ufc_clearmem (char *start
, int cnt
);
52 #define _ufc_clearmem(start, cnt) memset(start, 0, cnt)
54 extern char *__md5_crypt_r (const char *key
, const char *salt
, char *buffer
,
56 extern char *__md5_crypt (const char *key
, const char *salt
);
57 extern char *__sha256_crypt_r (const char *key
, const char *salt
,
58 char *buffer
, int buflen
);
59 extern char *__sha256_crypt (const char *key
, const char *salt
);
60 extern char *__sha512_crypt_r (const char *key
, const char *salt
,
61 char *buffer
, int buflen
);
62 extern char *__sha512_crypt (const char *key
, const char *salt
);
64 /* Define our magic string to mark salt for MD5 encryption
65 replacement. This is meant to be the same as for other MD5 based
66 encryption implementations. */
67 static const char md5_salt_prefix
[] = "$1$";
69 /* Magic string for SHA256 encryption. */
70 static const char sha256_salt_prefix
[] = "$5$";
72 /* Magic string for SHA512 encryption. */
73 static const char sha512_salt_prefix
[] = "$6$";
75 /* For use by the old, non-reentrant routines (crypt/encrypt/setkey) */
76 extern struct crypt_data _ufc_foobar
;
83 __crypt_r (key
, salt
, data
)
86 struct crypt_data
* __restrict data
;
90 ufc_long xx
= 25; /* to cope with GCC long long compiler bugs */
93 /* Try to find out whether we have to use MD5 encryption replacement. */
94 if (strncmp (md5_salt_prefix
, salt
, sizeof (md5_salt_prefix
) - 1) == 0)
95 return __md5_crypt_r (key
, salt
, (char *) data
,
96 sizeof (struct crypt_data
));
98 /* Try to find out whether we have to use SHA256 encryption replacement. */
99 if (strncmp (sha256_salt_prefix
, salt
, sizeof (sha256_salt_prefix
) - 1) == 0)
100 return __sha256_crypt_r (key
, salt
, (char *) data
,
101 sizeof (struct crypt_data
));
103 /* Try to find out whether we have to use SHA512 encryption replacement. */
104 if (strncmp (sha512_salt_prefix
, salt
, sizeof (sha512_salt_prefix
) - 1) == 0)
105 return __sha512_crypt_r (key
, salt
, (char *) data
,
106 sizeof (struct crypt_data
));
110 * Hack DES tables according to salt
112 _ufc_setup_salt_r (salt
, data
);
117 _ufc_clearmem (ktab
, (int) sizeof (ktab
));
118 (void) strncpy (ktab
, key
, 8);
119 _ufc_mk_keytab_r (ktab
, data
);
122 * Go for the 25 DES encryptions
124 _ufc_clearmem ((char*) res
, (int) sizeof (res
));
125 _ufc_doit_r (xx
, data
, &res
[0]);
128 * Do final permutations
130 _ufc_dofinalperm_r (res
, data
);
133 * And convert back to 6 bit ASCII
135 _ufc_output_conversion_r (res
[0], res
[1], salt
, data
);
136 return data
->crypt_3_buf
;
138 weak_alias (__crypt_r
, crypt_r
)
146 /* Try to find out whether we have to use MD5 encryption replacement. */
147 if (strncmp (md5_salt_prefix
, salt
, sizeof (md5_salt_prefix
) - 1) == 0)
148 return __md5_crypt (key
, salt
);
150 /* Try to find out whether we have to use SHA256 encryption replacement. */
151 if (strncmp (sha256_salt_prefix
, salt
, sizeof (sha256_salt_prefix
) - 1) == 0)
152 return __sha256_crypt (key
, salt
);
154 /* Try to find out whether we have to use SHA512 encryption replacement. */
155 if (strncmp (sha512_salt_prefix
, salt
, sizeof (sha512_salt_prefix
) - 1) == 0)
156 return __sha512_crypt (key
, salt
);
159 return __crypt_r (key
, salt
, &_ufc_foobar
);
164 * To make fcrypt users happy.
165 * They don't need to call init_des.
168 weak_alias (crypt
, fcrypt
)
175 return crypt (key
, salt
);