]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/bigcrypt.c
Relevant BUGIDs: 123399
[linux-pam] / modules / pam_unix / bigcrypt.c
1 /*
2  * This function implements the "bigcrypt" algorithm specifically for
3  * Linux-PAM.
4  *  
5  * This algorithm is algorithm 0 (default) shipped with the C2 secure
6  * implementation of Digital UNIX.
7  * 
8  * Disclaimer: This work is not based on the source code to Digital
9  * UNIX, nor am I connected to Digital Equipment Corp, in any way
10  * other than as a customer. This code is based on published
11  * interfaces and reasonable guesswork.
12  * 
13  * Description: The cleartext is divided into blocks of SEGMENT_SIZE=8
14  * characters or less. Each block is encrypted using the standard UNIX
15  * libc crypt function. The result of the encryption for one block
16  * provides the salt for the suceeding block.
17  * 
18  * Restrictions: The buffer used to hold the encrypted result is
19  * statically allocated. (see MAX_PASS_LEN below).  This is necessary,
20  * as the returned pointer points to "static data that are overwritten
21  * by each call", (XPG3: XSI System Interface + Headers pg 109), and
22  * this is a drop in replacement for crypt();
23  *
24  * Andy Phillips <atp@mssl.ucl.ac.uk>
25  */
26
27 #include <string.h>
28 #include <security/_pam_macros.h>
29
30 char *crypt(const char *key, const char *salt);
31 char *bigcrypt(const char *key, const char *salt);
32
33 /*
34  * Max cleartext password length in segments of 8 characters this
35  * function can deal with (16 segments of 8 chars= max 128 character
36  * password).
37  */
38
39 #define MAX_PASS_LEN       16
40 #define SEGMENT_SIZE       8
41 #define SALT_SIZE          2
42 #define KEYBUF_SIZE        ((MAX_PASS_LEN*SEGMENT_SIZE)+SALT_SIZE)
43 #define ESEGMENT_SIZE      11
44 #define CBUF_SIZE          ((MAX_PASS_LEN*ESEGMENT_SIZE)+SALT_SIZE+1)
45
46 char *bigcrypt(const char *key, const char *salt)
47 {
48         static char dec_c2_cryptbuf[CBUF_SIZE];         /* static storage area */
49
50         unsigned long int keylen, n_seg, j;
51         char *cipher_ptr, *plaintext_ptr, *tmp_ptr, *salt_ptr;
52         char keybuf[KEYBUF_SIZE + 1];
53
54         D(("called with key='%s', salt='%s'.", key, salt));
55
56         /* reset arrays */
57         memset(keybuf, 0, KEYBUF_SIZE + 1);
58         memset(dec_c2_cryptbuf, 0, CBUF_SIZE);
59
60         /* fill KEYBUF_SIZE with key */
61         strncpy(keybuf, key, KEYBUF_SIZE);
62
63         /* deal with case that we are doing a password check for a
64            conventially encrypted password: the salt will be
65            SALT_SIZE+ESEGMENT_SIZE long. */
66         if (strlen(salt) == (SALT_SIZE + ESEGMENT_SIZE))
67                 keybuf[SEGMENT_SIZE] = '\0';    /* terminate password early(?) */
68
69         keylen = strlen(keybuf);
70
71         if (!keylen) {
72                 n_seg = 1;
73         } else {
74                 /* work out how many segments */
75                 n_seg = 1 + ((keylen - 1) / SEGMENT_SIZE);
76         }
77
78         if (n_seg > MAX_PASS_LEN)
79                 n_seg = MAX_PASS_LEN;   /* truncate at max length */
80
81         /* set up some pointers */
82         cipher_ptr = dec_c2_cryptbuf;
83         plaintext_ptr = keybuf;
84
85         /* do the first block with supplied salt */
86         tmp_ptr = crypt(plaintext_ptr, salt);   /* libc crypt() */
87
88         /* and place in the static area */
89         strncpy(cipher_ptr, tmp_ptr, 13);
90         cipher_ptr += ESEGMENT_SIZE + SALT_SIZE;
91         plaintext_ptr += SEGMENT_SIZE;  /* first block of SEGMENT_SIZE */
92
93         /* change the salt (1st 2 chars of previous block) - this was found
94            by dowsing */
95
96         salt_ptr = cipher_ptr - ESEGMENT_SIZE;
97
98         /* so far this is identical to "return crypt(key, salt);", if
99            there is more than one block encrypt them... */
100
101         if (n_seg > 1) {
102                 for (j = 2; j <= n_seg; j++) {
103
104                         tmp_ptr = crypt(plaintext_ptr, salt_ptr);
105
106                         /* skip the salt for seg!=0 */
107                         strncpy(cipher_ptr, (tmp_ptr + SALT_SIZE), ESEGMENT_SIZE);
108
109                         cipher_ptr += ESEGMENT_SIZE;
110                         plaintext_ptr += SEGMENT_SIZE;
111                         salt_ptr = cipher_ptr - ESEGMENT_SIZE;
112                 }
113         }
114         D(("key=|%s|, salt=|%s|\nbuf=|%s|\n", key, salt, dec_c2_cryptbuf));
115
116         /* this is the <NUL> terminated encrypted password */
117
118         return dec_c2_cryptbuf;
119 }