/* * This version is derived from the original implementation of FreeSec * (release 1.1) by David Burren. I've reviewed the changes made in * OpenBSD (as of 2.7) and modified the original code in a similar way * where applicable. I've also made it reentrant, reduced its memory * usage (with only minimal performance impact), and made the handling * of invalid salts mostly UFC-crypt compatible. * - Solar Designer */ /* * FreeSec: libcrypt for NetBSD * * Copyright (c) 1994 David Burren * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the author nor the names of other contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Owl: Owl/packages/glibc/crypt_freesec.c,v 1.6 2010/02/20 14:45:06 solar Exp $ * $Id: crypt.c,v 1.15 1994/09/13 04:58:49 davidb Exp $ * * This is an original implementation of the DES and the crypt(3) interfaces * by David Burren . * * An excellent reference on the underlying algorithm (and related * algorithms) is: * * B. Schneier, Applied Cryptography: protocols, algorithms, * and source code in C, John Wiley & Sons, 1994. * * Note that in that book's description of DES the lookups for the initial, * pbox, and final permutations are inverted (this has been brought to the * attention of the author). A list of errata for this book has been * posted to the sci.crypt newsgroup by the author and is available for FTP. * * ARCHITECTURE ASSUMPTIONS: * This code used to have some nasty ones, but these have been removed * by now. The code requires a 32-bit integer type, though. */ #include #include struct _crypt_extended_local { u_int32_t saltbits; u_int32_t en_keysl[16], en_keysr[16]; }; #define _PASSWORD_EFMT1 '_' static const u_char key_shifts[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; static const uint32_t psbox[8][64] = { { 0x00808200,0x00000000,0x00008000,0x00808202, 0x00808002,0x00008202,0x00000002,0x00008000, 0x00000200,0x00808200,0x00808202,0x00000200, 0x00800202,0x00808002,0x00800000,0x00000002, 0x00000202,0x00800200,0x00800200,0x00008200, 0x00008200,0x00808000,0x00808000,0x00800202, 0x00008002,0x00800002,0x00800002,0x00008002, 0x00000000,0x00000202,0x00008202,0x00800000, 0x00008000,0x00808202,0x00000002,0x00808000, 0x00808200,0x00800000,0x00800000,0x00000200, 0x00808002,0x00008000,0x00008200,0x00800002, 0x00000200,0x00000002,0x00800202,0x00008202, 0x00808202,0x00008002,0x00808000,0x00800202, 0x00800002,0x00000202,0x00008202,0x00808200, 0x00000202,0x00800200,0x00800200,0x00000000, 0x00008002,0x00008200,0x00000000,0x00808002, },{ 0x40084010,0x40004000,0x00004000,0x00084010, 0x00080000,0x00000010,0x40080010,0x40004010, 0x40000010,0x40084010,0x40084000,0x40000000, 0x40004000,0x00080000,0x00000010,0x40080010, 0x00084000,0x00080010,0x40004010,0x00000000, 0x40000000,0x00004000,0x00084010,0x40080000, 0x00080010,0x40000010,0x00000000,0x00084000, 0x00004010,0x40084000,0x40080000,0x00004010, 0x00000000,0x00084010,0x40080010,0x00080000, 0x40004010,0x40080000,0x40084000,0x00004000, 0x40080000,0x40004000,0x00000010,0x40084010, 0x00084010,0x00000010,0x00004000,0x40000000, 0x00004010,0x40084000,0x00080000,0x40000010, 0x00080010,0x40004010,0x40000010,0x00080010, 0x00084000,0x00000000,0x40004000,0x00004010, 0x40000000,0x40080010,0x40084010,0x00084000, },{ 0x00000104,0x04010100,0x00000000,0x04010004, 0x04000100,0x00000000,0x00010104,0x04000100, 0x00010004,0x04000004,0x04000004,0x00010000, 0x04010104,0x00010004,0x04010000,0x00000104, 0x04000000,0x00000004,0x04010100,0x00000100, 0x00010100,0x04010000,0x04010004,0x00010104, 0x04000104,0x00010100,0x00010000,0x04000104, 0x00000004,0x04010104,0x00000100,0x04000000, 0x04010100,0x04000000,0x00010004,0x00000104, 0x00010000,0x04010100,0x04000100,0x00000000, 0x00000100,0x00010004,0x04010104,0x04000100, 0x04000004,0x00000100,0x00000000,0x04010004, 0x04000104,0x00010000,0x04000000,0x04010104, 0x00000004,0x00010104,0x00010100,0x04000004, 0x04010000,0x04000104,0x00000104,0x04010000, 0x00010104,0x00000004,0x04010004,0x00010100, },{ 0x80401000,0x80001040,0x80001040,0x00000040, 0x00401040,0x80400040,0x80400000,0x80001000, 0x00000000,0x00401000,0x00401000,0x80401040, 0x80000040,0x00000000,0x00400040,0x80400000, 0x80000000,0x00001000,0x00400000,0x80401000, 0x00000040,0x00400000,0x80001000,0x00001040, 0x80400040,0x80000000,0x00001040,0x00400040, 0x00001000,0x00401040,0x80401040,0x80000040, 0x00400040,0x80400000,0x00401000,0x80401040, 0x80000040,0x00000000,0x00000000,0x00401000, 0x00001040,0x00400040,0x80400040,0x80000000, 0x80401000,0x80001040,0x80001040,0x00000040, 0x80401040,0x80000040,0x80000000,0x00001000, 0x80400000,0x80001000,0x00401040,0x80400040, 0x80001000,0x00001040,0x00400000,0x80401000, 0x00000040,0x00400000,0x00001000,0x00401040, },{ 0x00000080,0x01040080,0x01040000,0x21000080, 0x00040000,0x00000080,0x20000000,0x01040000, 0x20040080,0x00040000,0x01000080,0x20040080, 0x21000080,0x21040000,0x00040080,0x20000000, 0x01000000,0x20040000,0x20040000,0x00000000, 0x20000080,0x21040080,0x21040080,0x01000080, 0x21040000,0x20000080,0x00000000,0x21000000, 0x01040080,0x01000000,0x21000000,0x00040080, 0x00040000,0x21000080,0x00000080,0x01000000, 0x20000000,0x01040000,0x21000080,0x20040080, 0x01000080,0x20000000,0x21040000,0x01040080, 0x20040080,0x00000080,0x01000000,0x21040000, 0x21040080,0x00040080,0x21000000,0x21040080, 0x01040000,0x00000000,0x20040000,0x21000000, 0x00040080,0x01000080,0x20000080,0x00040000, 0x00000000,0x20040000,0x01040080,0x20000080, },{ 0x10000008,0x10200000,0x00002000,0x10202008, 0x10200000,0x00000008,0x10202008,0x00200000, 0x10002000,0x00202008,0x00200000,0x10000008, 0x00200008,0x10002000,0x10000000,0x00002008, 0x00000000,0x00200008,0x10002008,0x00002000, 0x00202000,0x10002008,0x00000008,0x10200008, 0x10200008,0x00000000,0x00202008,0x10202000, 0x00002008,0x00202000,0x10202000,0x10000000, 0x10002000,0x00000008,0x10200008,0x00202000, 0x10202008,0x00200000,0x00002008,0x10000008, 0x00200000,0x10002000,0x10000000,0x00002008, 0x10000008,0x10202008,0x00202000,0x10200000, 0x00202008,0x10202000,0x00000000,0x10200008, 0x00000008,0x00002000,0x10200000,0x00202008, 0x00002000,0x00200008,0x10002008,0x00000000, 0x10202000,0x10000000,0x00200008,0x10002008, },{ 0x00100000,0x02100001,0x02000401,0x00000000, 0x00000400,0x02000401,0x00100401,0x02100400, 0x02100401,0x00100000,0x00000000,0x02000001, 0x00000001,0x02000000,0x02100001,0x00000401, 0x02000400,0x00100401,0x00100001,0x02000400, 0x02000001,0x02100000,0x02100400,0x00100001, 0x02100000,0x00000400,0x00000401,0x02100401, 0x00100400,0x00000001,0x02000000,0x00100400, 0x02000000,0x00100400,0x00100000,0x02000401, 0x02000401,0x02100001,0x02100001,0x00000001, 0x00100001,0x02000000,0x02000400,0x00100000, 0x02100400,0x00000401,0x00100401,0x02100400, 0x00000401,0x02000001,0x02100401,0x02100000, 0x00100400,0x00000000,0x00000001,0x02100401, 0x00000000,0x00100401,0x02100000,0x00000400, 0x02000001,0x02000400,0x00000400,0x00100001, },{ 0x08000820,0x00000800,0x00020000,0x08020820, 0x08000000,0x08000820,0x00000020,0x08000000, 0x00020020,0x08020000,0x08020820,0x00020800, 0x08020800,0x00020820,0x00000800,0x00000020, 0x08020000,0x08000020,0x08000800,0x00000820, 0x00020800,0x00020020,0x08020020,0x08020800, 0x00000820,0x00000000,0x00000000,0x08020020, 0x08000020,0x08000800,0x00020820,0x00020000, 0x00020820,0x00020000,0x08020800,0x00000800, 0x00000020,0x08020020,0x00000800,0x00020820, 0x08000800,0x00000020,0x08000020,0x08020000, 0x08020020,0x08000000,0x00020000,0x08000820, 0x00000000,0x08020820,0x00020020,0x08000020, 0x08020000,0x08000800,0x08000820,0x00000000, 0x08020820,0x00020800,0x00020800,0x00000820, 0x00000820,0x00020020,0x08000000,0x08020800, }, }; static const uint32_t ip_maskl[16][16] = { { 0x00000000,0x00010000,0x00000000,0x00010000, 0x01000000,0x01010000,0x01000000,0x01010000, 0x00000000,0x00010000,0x00000000,0x00010000, 0x01000000,0x01010000,0x01000000,0x01010000, },{ 0x00000000,0x00000001,0x00000000,0x00000001, 0x00000100,0x00000101,0x00000100,0x00000101, 0x00000000,0x00000001,0x00000000,0x00000001, 0x00000100,0x00000101,0x00000100,0x00000101, },{ 0x00000000,0x00020000,0x00000000,0x00020000, 0x02000000,0x02020000,0x02000000,0x02020000, 0x00000000,0x00020000,0x00000000,0x00020000, 0x02000000,0x02020000,0x02000000,0x02020000, },{ 0x00000000,0x00000002,0x00000000,0x00000002, 0x00000200,0x00000202,0x00000200,0x00000202, 0x00000000,0x00000002,0x00000000,0x00000002, 0x00000200,0x00000202,0x00000200,0x00000202, },{ 0x00000000,0x00040000,0x00000000,0x00040000, 0x04000000,0x04040000,0x04000000,0x04040000, 0x00000000,0x00040000,0x00000000,0x00040000, 0x04000000,0x04040000,0x04000000,0x04040000, },{ 0x00000000,0x00000004,0x00000000,0x00000004, 0x00000400,0x00000404,0x00000400,0x00000404, 0x00000000,0x00000004,0x00000000,0x00000004, 0x00000400,0x00000404,0x00000400,0x00000404, },{ 0x00000000,0x00080000,0x00000000,0x00080000, 0x08000000,0x08080000,0x08000000,0x08080000, 0x00000000,0x00080000,0x00000000,0x00080000, 0x08000000,0x08080000,0x08000000,0x08080000, },{ 0x00000000,0x00000008,0x00000000,0x00000008, 0x00000800,0x00000808,0x00000800,0x00000808, 0x00000000,0x00000008,0x00000000,0x00000008, 0x00000800,0x00000808,0x00000800,0x00000808, },{ 0x00000000,0x00100000,0x00000000,0x00100000, 0x10000000,0x10100000,0x10000000,0x10100000, 0x00000000,0x00100000,0x00000000,0x00100000, 0x10000000,0x10100000,0x10000000,0x10100000, },{ 0x00000000,0x00000010,0x00000000,0x00000010, 0x00001000,0x00001010,0x00001000,0x00001010, 0x00000000,0x00000010,0x00000000,0x00000010, 0x00001000,0x00001010,0x00001000,0x00001010, },{ 0x00000000,0x00200000,0x00000000,0x00200000, 0x20000000,0x20200000,0x20000000,0x20200000, 0x00000000,0x00200000,0x00000000,0x00200000, 0x20000000,0x20200000,0x20000000,0x20200000, },{ 0x00000000,0x00000020,0x00000000,0x00000020, 0x00002000,0x00002020,0x00002000,0x00002020, 0x00000000,0x00000020,0x00000000,0x00000020, 0x00002000,0x00002020,0x00002000,0x00002020, },{ 0x00000000,0x00400000,0x00000000,0x00400000, 0x40000000,0x40400000,0x40000000,0x40400000, 0x00000000,0x00400000,0x00000000,0x00400000, 0x40000000,0x40400000,0x40000000,0x40400000, },{ 0x00000000,0x00000040,0x00000000,0x00000040, 0x00004000,0x00004040,0x00004000,0x00004040, 0x00000000,0x00000040,0x00000000,0x00000040, 0x00004000,0x00004040,0x00004000,0x00004040, },{ 0x00000000,0x00800000,0x00000000,0x00800000, 0x80000000,0x80800000,0x80000000,0x80800000, 0x00000000,0x00800000,0x00000000,0x00800000, 0x80000000,0x80800000,0x80000000,0x80800000, },{ 0x00000000,0x00000080,0x00000000,0x00000080, 0x00008000,0x00008080,0x00008000,0x00008080, 0x00000000,0x00000080,0x00000000,0x00000080, 0x00008000,0x00008080,0x00008000,0x00008080, }, }; static const uint32_t ip_maskr[16][16] = { { 0x00000000,0x00000000,0x00010000,0x00010000, 0x00000000,0x00000000,0x00010000,0x00010000, 0x01000000,0x01000000,0x01010000,0x01010000, 0x01000000,0x01000000,0x01010000,0x01010000, },{ 0x00000000,0x00000000,0x00000001,0x00000001, 0x00000000,0x00000000,0x00000001,0x00000001, 0x00000100,0x00000100,0x00000101,0x00000101, 0x00000100,0x00000100,0x00000101,0x00000101, },{ 0x00000000,0x00000000,0x00020000,0x00020000, 0x00000000,0x00000000,0x00020000,0x00020000, 0x02000000,0x02000000,0x02020000,0x02020000, 0x02000000,0x02000000,0x02020000,0x02020000, },{ 0x00000000,0x00000000,0x00000002,0x00000002, 0x00000000,0x00000000,0x00000002,0x00000002, 0x00000200,0x00000200,0x00000202,0x00000202, 0x00000200,0x00000200,0x00000202,0x00000202, },{ 0x00000000,0x00000000,0x00040000,0x00040000, 0x00000000,0x00000000,0x00040000,0x00040000, 0x04000000,0x04000000,0x04040000,0x04040000, 0x04000000,0x04000000,0x04040000,0x04040000, },{ 0x00000000,0x00000000,0x00000004,0x00000004, 0x00000000,0x00000000,0x00000004,0x00000004, 0x00000400,0x00000400,0x00000404,0x00000404, 0x00000400,0x00000400,0x00000404,0x00000404, },{ 0x00000000,0x00000000,0x00080000,0x00080000, 0x00000000,0x00000000,0x00080000,0x00080000, 0x08000000,0x08000000,0x08080000,0x08080000, 0x08000000,0x08000000,0x08080000,0x08080000, },{ 0x00000000,0x00000000,0x00000008,0x00000008, 0x00000000,0x00000000,0x00000008,0x00000008, 0x00000800,0x00000800,0x00000808,0x00000808, 0x00000800,0x00000800,0x00000808,0x00000808, },{ 0x00000000,0x00000000,0x00100000,0x00100000, 0x00000000,0x00000000,0x00100000,0x00100000, 0x10000000,0x10000000,0x10100000,0x10100000, 0x10000000,0x10000000,0x10100000,0x10100000, },{ 0x00000000,0x00000000,0x00000010,0x00000010, 0x00000000,0x00000000,0x00000010,0x00000010, 0x00001000,0x00001000,0x00001010,0x00001010, 0x00001000,0x00001000,0x00001010,0x00001010, },{ 0x00000000,0x00000000,0x00200000,0x00200000, 0x00000000,0x00000000,0x00200000,0x00200000, 0x20000000,0x20000000,0x20200000,0x20200000, 0x20000000,0x20000000,0x20200000,0x20200000, },{ 0x00000000,0x00000000,0x00000020,0x00000020, 0x00000000,0x00000000,0x00000020,0x00000020, 0x00002000,0x00002000,0x00002020,0x00002020, 0x00002000,0x00002000,0x00002020,0x00002020, },{ 0x00000000,0x00000000,0x00400000,0x00400000, 0x00000000,0x00000000,0x00400000,0x00400000, 0x40000000,0x40000000,0x40400000,0x40400000, 0x40000000,0x40000000,0x40400000,0x40400000, },{ 0x00000000,0x00000000,0x00000040,0x00000040, 0x00000000,0x00000000,0x00000040,0x00000040, 0x00004000,0x00004000,0x00004040,0x00004040, 0x00004000,0x00004000,0x00004040,0x00004040, },{ 0x00000000,0x00000000,0x00800000,0x00800000, 0x00000000,0x00000000,0x00800000,0x00800000, 0x80000000,0x80000000,0x80800000,0x80800000, 0x80000000,0x80000000,0x80800000,0x80800000, },{ 0x00000000,0x00000000,0x00000080,0x00000080, 0x00000000,0x00000000,0x00000080,0x00000080, 0x00008000,0x00008000,0x00008080,0x00008080, 0x00008000,0x00008000,0x00008080,0x00008080, }, }; static const uint32_t fp_maskl[16][16] = { { 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x40000000,0x00400000,0x40400000, 0x00004000,0x40004000,0x00404000,0x40404000, 0x00000040,0x40000040,0x00400040,0x40400040, 0x00004040,0x40004040,0x00404040,0x40404040, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x10000000,0x00100000,0x10100000, 0x00001000,0x10001000,0x00101000,0x10101000, 0x00000010,0x10000010,0x00100010,0x10100010, 0x00001010,0x10001010,0x00101010,0x10101010, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x04000000,0x00040000,0x04040000, 0x00000400,0x04000400,0x00040400,0x04040400, 0x00000004,0x04000004,0x00040004,0x04040004, 0x00000404,0x04000404,0x00040404,0x04040404, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x01000000,0x00010000,0x01010000, 0x00000100,0x01000100,0x00010100,0x01010100, 0x00000001,0x01000001,0x00010001,0x01010001, 0x00000101,0x01000101,0x00010101,0x01010101, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x80000000,0x00800000,0x80800000, 0x00008000,0x80008000,0x00808000,0x80808000, 0x00000080,0x80000080,0x00800080,0x80800080, 0x00008080,0x80008080,0x00808080,0x80808080, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x20000000,0x00200000,0x20200000, 0x00002000,0x20002000,0x00202000,0x20202000, 0x00000020,0x20000020,0x00200020,0x20200020, 0x00002020,0x20002020,0x00202020,0x20202020, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x08000000,0x00080000,0x08080000, 0x00000800,0x08000800,0x00080800,0x08080800, 0x00000008,0x08000008,0x00080008,0x08080008, 0x00000808,0x08000808,0x00080808,0x08080808, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x02000000,0x00020000,0x02020000, 0x00000200,0x02000200,0x00020200,0x02020200, 0x00000002,0x02000002,0x00020002,0x02020002, 0x00000202,0x02000202,0x00020202,0x02020202, }, }; static const uint32_t fp_maskr[16][16] = { { 0x00000000,0x40000000,0x00400000,0x40400000, 0x00004000,0x40004000,0x00404000,0x40404000, 0x00000040,0x40000040,0x00400040,0x40400040, 0x00004040,0x40004040,0x00404040,0x40404040, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x10000000,0x00100000,0x10100000, 0x00001000,0x10001000,0x00101000,0x10101000, 0x00000010,0x10000010,0x00100010,0x10100010, 0x00001010,0x10001010,0x00101010,0x10101010, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x04000000,0x00040000,0x04040000, 0x00000400,0x04000400,0x00040400,0x04040400, 0x00000004,0x04000004,0x00040004,0x04040004, 0x00000404,0x04000404,0x00040404,0x04040404, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x01000000,0x00010000,0x01010000, 0x00000100,0x01000100,0x00010100,0x01010100, 0x00000001,0x01000001,0x00010001,0x01010001, 0x00000101,0x01000101,0x00010101,0x01010101, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x80000000,0x00800000,0x80800000, 0x00008000,0x80008000,0x00808000,0x80808000, 0x00000080,0x80000080,0x00800080,0x80800080, 0x00008080,0x80008080,0x00808080,0x80808080, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x20000000,0x00200000,0x20200000, 0x00002000,0x20002000,0x00202000,0x20202000, 0x00000020,0x20000020,0x00200020,0x20200020, 0x00002020,0x20002020,0x00202020,0x20202020, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x08000000,0x00080000,0x08080000, 0x00000800,0x08000800,0x00080800,0x08080800, 0x00000008,0x08000008,0x00080008,0x08080008, 0x00000808,0x08000808,0x00080808,0x08080808, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x02000000,0x00020000,0x02020000, 0x00000200,0x02000200,0x00020200,0x02020200, 0x00000002,0x02000002,0x00020002,0x02020002, 0x00000202,0x02000202,0x00020202,0x02020202, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, }, }; static const uint32_t key_perm_maskl[16][16] = { { 0x00000000,0x00000000,0x00000010,0x00000010, 0x00001000,0x00001000,0x00001010,0x00001010, 0x00100000,0x00100000,0x00100010,0x00100010, 0x00101000,0x00101000,0x00101010,0x00101010, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000020,0x00000020, 0x00002000,0x00002000,0x00002020,0x00002020, 0x00200000,0x00200000,0x00200020,0x00200020, 0x00202000,0x00202000,0x00202020,0x00202020, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000040,0x00000040, 0x00004000,0x00004000,0x00004040,0x00004040, 0x00400000,0x00400000,0x00400040,0x00400040, 0x00404000,0x00404000,0x00404040,0x00404040, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000080,0x00000080, 0x00008000,0x00008000,0x00008080,0x00008080, 0x00800000,0x00800000,0x00800080,0x00800080, 0x00808000,0x00808000,0x00808080,0x00808080, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000001,0x00000100,0x00000101, 0x00010000,0x00010001,0x00010100,0x00010101, 0x01000000,0x01000001,0x01000100,0x01000101, 0x01010000,0x01010001,0x01010100,0x01010101, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000002,0x00000200,0x00000202, 0x00020000,0x00020002,0x00020200,0x00020202, 0x02000000,0x02000002,0x02000200,0x02000202, 0x02020000,0x02020002,0x02020200,0x02020202, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000004,0x00000400,0x00000404, 0x00040000,0x00040004,0x00040400,0x00040404, 0x04000000,0x04000004,0x04000400,0x04000404, 0x04040000,0x04040004,0x04040400,0x04040404, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000008,0x00000800,0x00000808, 0x00080000,0x00080008,0x00080800,0x00080808, 0x08000000,0x08000008,0x08000800,0x08000808, 0x08080000,0x08080008,0x08080800,0x08080808, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, }, }; static const uint32_t key_perm_maskr[16][16] = { { 0x00000000,0x00000001,0x00000000,0x00000001, 0x00000000,0x00000001,0x00000000,0x00000001, 0x00000000,0x00000001,0x00000000,0x00000001, 0x00000000,0x00000001,0x00000000,0x00000001, },{ 0x00000000,0x00000000,0x00100000,0x00100000, 0x00001000,0x00001000,0x00101000,0x00101000, 0x00000010,0x00000010,0x00100010,0x00100010, 0x00001010,0x00001010,0x00101010,0x00101010, },{ 0x00000000,0x00000002,0x00000000,0x00000002, 0x00000000,0x00000002,0x00000000,0x00000002, 0x00000000,0x00000002,0x00000000,0x00000002, 0x00000000,0x00000002,0x00000000,0x00000002, },{ 0x00000000,0x00000000,0x00200000,0x00200000, 0x00002000,0x00002000,0x00202000,0x00202000, 0x00000020,0x00000020,0x00200020,0x00200020, 0x00002020,0x00002020,0x00202020,0x00202020, },{ 0x00000000,0x00000004,0x00000000,0x00000004, 0x00000000,0x00000004,0x00000000,0x00000004, 0x00000000,0x00000004,0x00000000,0x00000004, 0x00000000,0x00000004,0x00000000,0x00000004, },{ 0x00000000,0x00000000,0x00400000,0x00400000, 0x00004000,0x00004000,0x00404000,0x00404000, 0x00000040,0x00000040,0x00400040,0x00400040, 0x00004040,0x00004040,0x00404040,0x00404040, },{ 0x00000000,0x00000008,0x00000000,0x00000008, 0x00000000,0x00000008,0x00000000,0x00000008, 0x00000000,0x00000008,0x00000000,0x00000008, 0x00000000,0x00000008,0x00000000,0x00000008, },{ 0x00000000,0x00000000,0x00800000,0x00800000, 0x00008000,0x00008000,0x00808000,0x00808000, 0x00000080,0x00000080,0x00800080,0x00800080, 0x00008080,0x00008080,0x00808080,0x00808080, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x01000000,0x01000000, 0x00010000,0x00010000,0x01010000,0x01010000, 0x00000100,0x00000100,0x01000100,0x01000100, 0x00010100,0x00010100,0x01010100,0x01010100, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x02000000,0x02000000, 0x00020000,0x00020000,0x02020000,0x02020000, 0x00000200,0x00000200,0x02000200,0x02000200, 0x00020200,0x00020200,0x02020200,0x02020200, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x04000000,0x04000000, 0x00040000,0x00040000,0x04040000,0x04040000, 0x00000400,0x00000400,0x04000400,0x04000400, 0x00040400,0x00040400,0x04040400,0x04040400, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x08000000,0x08000000, 0x00080000,0x00080000,0x08080000,0x08080000, 0x00000800,0x00000800,0x08000800,0x08000800, 0x00080800,0x00080800,0x08080800,0x08080800, }, }; static const uint32_t comp_maskl0[8][8] = { { 0x00000000,0x00020000,0x00000001,0x00020001, 0x00080000,0x000a0000,0x00080001,0x000a0001, },{ 0x00000000,0x00001000,0x00000000,0x00001000, 0x00000040,0x00001040,0x00000040,0x00001040, },{ 0x00000000,0x00400000,0x00000020,0x00400020, 0x00008000,0x00408000,0x00008020,0x00408020, },{ 0x00000000,0x00100000,0x00000800,0x00100800, 0x00000000,0x00100000,0x00000800,0x00100800, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, }, }; static const uint32_t comp_maskr0[8][8] = { { 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00200000,0x00020000,0x00220000, 0x00000002,0x00200002,0x00020002,0x00220002, },{ 0x00000000,0x00000000,0x00100000,0x00100000, 0x00000004,0x00000004,0x00100004,0x00100004, },{ 0x00000000,0x00004000,0x00000800,0x00004800, 0x00000000,0x00004000,0x00000800,0x00004800, },{ 0x00000000,0x00400000,0x00008000,0x00408000, 0x00000008,0x00400008,0x00008008,0x00408008, }, }; static const uint32_t comp_maskl1[8][16] = { { 0x00000000,0x00000010,0x00004000,0x00004010, 0x00040000,0x00040010,0x00044000,0x00044010, 0x00000100,0x00000110,0x00004100,0x00004110, 0x00040100,0x00040110,0x00044100,0x00044110, },{ 0x00000000,0x00800000,0x00000002,0x00800002, 0x00000200,0x00800200,0x00000202,0x00800202, 0x00200000,0x00a00000,0x00200002,0x00a00002, 0x00200200,0x00a00200,0x00200202,0x00a00202, },{ 0x00000000,0x00002000,0x00000004,0x00002004, 0x00000400,0x00002400,0x00000404,0x00002404, 0x00000000,0x00002000,0x00000004,0x00002004, 0x00000400,0x00002400,0x00000404,0x00002404, },{ 0x00000000,0x00010000,0x00000008,0x00010008, 0x00000080,0x00010080,0x00000088,0x00010088, 0x00000000,0x00010000,0x00000008,0x00010008, 0x00000080,0x00010080,0x00000088,0x00010088, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, }, }; static const uint32_t comp_maskr1[8][16] = { { 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, 0x00000000,0x00000000,0x00000000,0x00000000, },{ 0x00000000,0x00000000,0x00000080,0x00000080, 0x00002000,0x00002000,0x00002080,0x00002080, 0x00000001,0x00000001,0x00000081,0x00000081, 0x00002001,0x00002001,0x00002081,0x00002081, },{ 0x00000000,0x00000010,0x00800000,0x00800010, 0x00010000,0x00010010,0x00810000,0x00810010, 0x00000200,0x00000210,0x00800200,0x00800210, 0x00010200,0x00010210,0x00810200,0x00810210, },{ 0x00000000,0x00000400,0x00001000,0x00001400, 0x00080000,0x00080400,0x00081000,0x00081400, 0x00000020,0x00000420,0x00001020,0x00001420, 0x00080020,0x00080420,0x00081020,0x00081420, },{ 0x00000000,0x00000100,0x00040000,0x00040100, 0x00000000,0x00000100,0x00040000,0x00040100, 0x00000040,0x00000140,0x00040040,0x00040140, 0x00000040,0x00000140,0x00040040,0x00040140, }, }; static const u_char ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; /* 0000000000111111111122222222223333333333444444444455555555556666 */ /* 0123456789012345678901234567890123456789012345678901234567890123 */ /* * We match the behavior of UFC-crypt on systems where "char" is signed by * default (the majority), regardless of char's signedness on our system. */ static inline int ascii_to_bin(int ch) { int sch = ch>127 ? -(256-ch) : ch; int retval; retval = sch - '.'; if (sch >= 'A') { retval = sch - ('A' - 12); if (sch >= 'a') retval = sch - ('a' - 38); } retval &= 0x3f; return retval; } /* * When we choose to "support" invalid salts, nevertheless disallow those * containing characters that would violate the passwd file format. */ static inline int ascii_is_unsafe(int ch) { return !ch || ch == '\n' || ch == ':'; } static void setup_salt(u_int32_t salt, struct _crypt_extended_local *local) { u_int32_t obit, saltbit, saltbits; int i; saltbits = 0; saltbit = 1; obit = 0x800000; for (i = 0; i < 24; i++) { if (salt & saltbit) saltbits |= obit; saltbit <<= 1; obit >>= 1; } local->saltbits = saltbits; } static int des_setkey(const u_char *key, struct _crypt_extended_local *local) { u_int32_t k0, k1, rawkey0, rawkey1; int shifts, round; rawkey0 = (u_int32_t)(u_char)key[3] | ((u_int32_t)(u_char)key[2] << 8) | ((u_int32_t)(u_char)key[1] << 16) | ((u_int32_t)(u_char)key[0] << 24); rawkey1 = (u_int32_t)(u_char)key[7] | ((u_int32_t)(u_char)key[6] << 8) | ((u_int32_t)(u_char)key[5] << 16) | ((u_int32_t)(u_char)key[4] << 24); /* * Do key permutation and split into two 28-bit subkeys. */ { int i, inbit; k0 = k1 = 0; for (i = 0, inbit = 28; i < 8; i++, inbit -= 4) { k0 |= key_perm_maskl[i][(rawkey0 >> inbit) & 0xf] | key_perm_maskl[i + 8][(rawkey1 >> inbit) & 0xf]; k1 |= key_perm_maskr[i][(rawkey0 >> inbit) & 0xf] | key_perm_maskr[i + 8][(rawkey1 >> inbit) & 0xf]; } } /* * Rotate subkeys and do compression permutation. */ shifts = 0; for (round = 0; round < 16; round++) { u_int32_t t0, t1; shifts += key_shifts[round]; t0 = (k0 << shifts) | (k0 >> (28 - shifts)); t1 = (k1 << shifts) | (k1 >> (28 - shifts)); { int i, inbit; u_int32_t kl, kr; kl = kr = 0; inbit = 25; for (i = 0; i < 4; i++) { kl |= comp_maskl0[i][(t0 >> inbit) & 7] | comp_maskl0[i + 4][(t1 >> inbit) & 7]; kr |= comp_maskr0[i][(t0 >> inbit) & 7] | comp_maskr0[i + 4][(t1 >> inbit) & 7]; inbit -= 4; kl |= comp_maskl1[i][(t0 >> inbit) & 0xf] | comp_maskl1[i + 4][(t1 >> inbit) & 0xf]; kr |= comp_maskr1[i][(t0 >> inbit) & 0xf] | comp_maskr1[i + 4][(t1 >> inbit) & 0xf]; inbit -= 3; } local->en_keysl[round] = kl; local->en_keysr[round] = kr; } } return 0; } static int do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, int count, struct _crypt_extended_local *local) { /* * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. */ u_int32_t l, r, *kl, *kr, *kl1, *kr1; u_int32_t f, r48l, r48r, saltbits; int round; kl1 = local->en_keysl; kr1 = local->en_keysr; /* * Do initial permutation (IP). */ l = r = 0; if (l_in | r_in) { int i, inbit; for (i = 0, inbit = 28; i < 8; i++, inbit -= 4) { l |= ip_maskl[i][(l_in >> inbit) & 0xf] | ip_maskl[i + 8][(r_in >> inbit) & 0xf]; r |= ip_maskr[i][(l_in >> inbit) & 0xf] | ip_maskr[i + 8][(r_in >> inbit) & 0xf]; } } saltbits = local->saltbits; while (count--) { /* * Do each round. */ kl = kl1; kr = kr1; round = 16; while (round--) { /* * Expand R to 48 bits (simulate the E-box). */ r48l = ((r & 0x00000001) << 23) | ((r & 0xf8000000) >> 9) | ((r & 0x1f800000) >> 11) | ((r & 0x01f80000) >> 13) | ((r & 0x001f8000) >> 15); r48r = ((r & 0x0001f800) << 7) | ((r & 0x00001f80) << 5) | ((r & 0x000001f8) << 3) | ((r & 0x0000001f) << 1) | ((r & 0x80000000) >> 31); /* * Do salting for crypt() and friends, and * XOR with the permuted key. */ f = (r48l ^ r48r) & saltbits; r48l ^= f ^ *kl++; r48r ^= f ^ *kr++; /* * Do S-box lookups (which shrink it back to 32 bits) * and do the P-box permutation at the same time. */ f = psbox[0][r48l >> 18] | psbox[1][(r48l >> 12) & 0x3f] | psbox[2][(r48l >> 6) & 0x3f] | psbox[3][r48l & 0x3f] | psbox[4][r48r >> 18] | psbox[5][(r48r >> 12) & 0x3f] | psbox[6][(r48r >> 6) & 0x3f] | psbox[7][r48r & 0x3f]; /* * Now that we've permuted things, complete f(). */ f ^= l; l = r; r = f; } r = l; l = f; } /* * Do final permutation (inverse of IP). */ { int i, inbit; u_int32_t lo, ro; lo = ro = 0; for (i = 0, inbit = 28; i < 8; i++, inbit -= 4) { lo |= fp_maskl[i][(l >> inbit) & 0xf] | fp_maskl[i + 8][(r >> inbit) & 0xf]; ro |= fp_maskr[i][(l >> inbit) & 0xf] | fp_maskr[i + 8][(r >> inbit) & 0xf]; } *l_out = lo; *r_out = ro; } return 0; } static int des_cipher(const u_char *in, u_char *out, u_int32_t salt, int count, struct _crypt_extended_local *local) { u_int32_t l_out, r_out, rawl, rawr; int retval; setup_salt(salt, local); rawl = (u_int32_t)(u_char)in[3] | ((u_int32_t)(u_char)in[2] << 8) | ((u_int32_t)(u_char)in[1] << 16) | ((u_int32_t)(u_char)in[0] << 24); rawr = (u_int32_t)(u_char)in[7] | ((u_int32_t)(u_char)in[6] << 8) | ((u_int32_t)(u_char)in[5] << 16) | ((u_int32_t)(u_char)in[4] << 24); retval = do_des(rawl, rawr, &l_out, &r_out, count, local); out[0] = l_out >> 24; out[1] = l_out >> 16; out[2] = l_out >> 8; out[3] = l_out; out[4] = r_out >> 24; out[5] = r_out >> 16; out[6] = r_out >> 8; out[7] = r_out; return retval; } char * _crypt_extended_r(const char *key, const char *setting, char *output) { int i; u_int32_t count, salt, l, r0, r1, keybuf[2]; u_char *p, *q; struct _crypt_extended_local local = { 0 }; /* * Copy the key, shifting each character up by one bit * and padding with zeros. */ q = (u_char *) keybuf; while (q - (u_char *) keybuf < sizeof(keybuf)) { *q++ = *key << 1; if (*key) key++; } if (des_setkey((u_char *) keybuf, &local)) return NULL; if (*setting == _PASSWORD_EFMT1) { /* * "new"-style: * setting - underscore, 4 chars of count, 4 chars of salt * key - unlimited characters */ for (i = 1, count = 0; i < 5; i++) { int value = ascii_to_bin(setting[i]); if (ascii64[value] != setting[i]) return NULL; count |= value << (i - 1) * 6; } if (!count) return NULL; for (i = 5, salt = 0; i < 9; i++) { int value = ascii_to_bin(setting[i]); if (ascii64[value] != setting[i]) return NULL; salt |= value << (i - 5) * 6; } while (*key) { /* * Encrypt the key with itself. */ if (des_cipher((u_char *) keybuf, (u_char *) keybuf, 0, 1, &local)) return NULL; /* * And XOR with the next 8 characters of the key. */ q = (u_char *) keybuf; while (q - (u_char *) keybuf < sizeof(keybuf) && *key) *q++ ^= *key++ << 1; if (des_setkey((u_char *) keybuf, &local)) return NULL; } memcpy(output, setting, 9); output[9] = '\0'; p = (u_char *) output + 9; } else { /* * "old"-style: * setting - 2 chars of salt * key - up to 8 characters */ count = 25; if (ascii_is_unsafe(setting[0]) || ascii_is_unsafe(setting[1])) return NULL; salt = (ascii_to_bin(setting[1]) << 6) | ascii_to_bin(setting[0]); output[0] = setting[0]; output[1] = setting[1]; p = (u_char *) output + 2; } setup_salt(salt, &local); /* * Do it. */ if (do_des(0, 0, &r0, &r1, count, &local)) return NULL; /* * Now encode the result... */ l = (r0 >> 8); *p++ = ascii64[(l >> 18) & 0x3f]; *p++ = ascii64[(l >> 12) & 0x3f]; *p++ = ascii64[(l >> 6) & 0x3f]; *p++ = ascii64[l & 0x3f]; l = (r0 << 16) | ((r1 >> 16) & 0xffff); *p++ = ascii64[(l >> 18) & 0x3f]; *p++ = ascii64[(l >> 12) & 0x3f]; *p++ = ascii64[(l >> 6) & 0x3f]; *p++ = ascii64[l & 0x3f]; l = r1 << 2; *p++ = ascii64[(l >> 12) & 0x3f]; *p++ = ascii64[(l >> 6) & 0x3f]; *p++ = ascii64[l & 0x3f]; *p = 0; return output; } static char * _crypt_extended(const char *key, const char *setting) { static char output[21]; return _crypt_extended_r(key, setting, output); }