1 /**	$MirOS: src/lib/libc/crypt/crypt2.c,v 1.3 2005/09/22 20:06:58 tg Exp $	*/
2 /*	$OpenBSD: crypt2.c,v 1.3 2005/08/08 08:05:33 espie Exp $	*/
3 
4 /*
5  * FreeSec: libcrypt
6  *
7  * Copyright (c) 1994 David Burren
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the author nor the names of other contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *
35  * This is an original implementation of the DES and the crypt(3) interfaces
36  * by David Burren <davidb@werj.com.au>.
37  *
38  * An excellent reference on the underlying algorithm (and related
39  * algorithms) is:
40  *
41  *	B. Schneier, Applied Cryptography: protocols, algorithms,
42  *	and source code in C, John Wiley & Sons, 1994.
43  *
44  * Note that in that book's description of DES the lookups for the initial,
45  * pbox, and final permutations are inverted (this has been brought to the
46  * attention of the author).  A list of errata for this book has been
47  * posted to the sci.crypt newsgroup by the author and is available for FTP.
48  */
49 
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <pwd.h>
53 #include <unistd.h>
54 #include <string.h>
55 
56 #ifdef DEBUG
57 # include <stdio.h>
58 #endif
59 
60 extern const u_char _des_bits8[8];
61 extern const u_int32_t _des_bits32[32];
62 extern int	_des_initialised;
63 
64 void _des_init(void);
65 void _des_setup_salt(int32_t);
66 int _des_do_des(u_int32_t, u_int32_t, u_int32_t *, u_int32_t *, int);
67 
68 int
setkey(const char * key)69 setkey(const char *key)
70 {
71 	int	i, j;
72 	u_int32_t packed_keys[2];
73 	u_char	*p;
74 
75 	p = (u_char *) packed_keys;
76 
77 	for (i = 0; i < 8; i++) {
78 		p[i] = 0;
79 		for (j = 0; j < 8; j++)
80 			if (*key++ & 1)
81 				p[i] |= _des_bits8[j];
82 	}
83 	return(des_setkey((char *)p));
84 }
85 
86 int
encrypt(char * block,int flag)87 encrypt(char *block, int flag)
88 {
89 	u_int32_t io[2];
90 	u_char	*p;
91 	int	i, j, retval;
92 
93 	if (!_des_initialised)
94 		_des_init();
95 
96 	_des_setup_salt(0);
97 	p = (u_char *)block;
98 	for (i = 0; i < 2; i++) {
99 		io[i] = 0L;
100 		for (j = 0; j < 32; j++)
101 			if (*p++ & 1)
102 				io[i] |= _des_bits32[j];
103 	}
104 	retval = _des_do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
105 	for (i = 0; i < 2; i++)
106 		for (j = 0; j < 32; j++)
107 			block[(i << 5) | j] = (io[i] & _des_bits32[j]) ? 1 : 0;
108 	return(retval);
109 }
110