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