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.c104
1 files changed, 104 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..63a297e510
--- /dev/null
+++ b/src/lib/libc/crypt/crypt2.c
@@ -0,0 +1,104 @@
1/* $OpenBSD: crypt2.c,v 1.3 2005/08/08 08:05:33 espie 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#include <sys/types.h>
50#include <sys/param.h>
51#include <pwd.h>
52#include <unistd.h>
53#include <string.h>
54
55#ifdef DEBUG
56# include <stdio.h>
57#endif
58
59extern const u_char _des_bits8[8];
60extern const u_int32_t _des_bits32[32];
61extern int _des_initialised;
62
63int
64setkey(const char *key)
65{
66 int i, j;
67 u_int32_t packed_keys[2];
68 u_char *p;
69
70 p = (u_char *) packed_keys;
71
72 for (i = 0; i < 8; i++) {
73 p[i] = 0;
74 for (j = 0; j < 8; j++)
75 if (*key++ & 1)
76 p[i] |= _des_bits8[j];
77 }
78 return(des_setkey((char *)p));
79}
80
81int
82encrypt(char *block, int flag)
83{
84 u_int32_t io[2];
85 u_char *p;
86 int i, j, retval;
87
88 if (!_des_initialised)
89 _des_init();
90
91 _des_setup_salt(0);
92 p = (u_char *)block;
93 for (i = 0; i < 2; i++) {
94 io[i] = 0L;
95 for (j = 0; j < 32; j++)
96 if (*p++ & 1)
97 io[i] |= _des_bits32[j];
98 }
99 retval = _des_do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
100 for (i = 0; i < 2; i++)
101 for (j = 0; j < 32; j++)
102 block[(i << 5) | j] = (io[i] & _des_bits32[j]) ? 1 : 0;
103 return(retval);
104}