]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/bigcrypt.c
Relevant BUGIDs:
[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 "config.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31 #include <security/_pam_macros.h>
32 #ifdef HAVE_CRYPT_H
33 #include <crypt.h>
34 #endif
35
36 #include "bigcrypt.h"
37
38 /*
39  * Max cleartext password length in segments of 8 characters this
40  * function can deal with (16 segments of 8 chars= max 128 character
41  * password).
42  */
43
44 #define MAX_PASS_LEN       16
45 #define SEGMENT_SIZE       8
46 #define SALT_SIZE          2
47 #define KEYBUF_SIZE        ((MAX_PASS_LEN*SEGMENT_SIZE)+SALT_SIZE)
48 #define ESEGMENT_SIZE      11
49 #define CBUF_SIZE          ((MAX_PASS_LEN*ESEGMENT_SIZE)+SALT_SIZE+1)
50
51 char *bigcrypt(const char *key, const char *salt)
52 {
53         char *dec_c2_cryptbuf;
54
55         unsigned long int keylen, n_seg, j;
56         char *cipher_ptr, *plaintext_ptr, *tmp_ptr, *salt_ptr;
57         char keybuf[KEYBUF_SIZE + 1];
58
59         D(("called with key='%s', salt='%s'.", key, salt));
60
61         /* reset arrays */
62         dec_c2_cryptbuf = malloc(CBUF_SIZE);
63         if (!dec_c2_cryptbuf) {
64                 return NULL;
65         }
66         memset(keybuf, 0, KEYBUF_SIZE + 1);
67         memset(dec_c2_cryptbuf, 0, CBUF_SIZE);
68
69         /* fill KEYBUF_SIZE with key */
70         strncpy(keybuf, key, KEYBUF_SIZE);
71
72         /* deal with case that we are doing a password check for a
73            conventially encrypted password: the salt will be
74            SALT_SIZE+ESEGMENT_SIZE long. */
75         if (strlen(salt) == (SALT_SIZE + ESEGMENT_SIZE))
76                 keybuf[SEGMENT_SIZE] = '\0';    /* terminate password early(?) */
77
78         keylen = strlen(keybuf);
79
80         if (!keylen) {
81                 n_seg = 1;
82         } else {
83                 /* work out how many segments */
84                 n_seg = 1 + ((keylen - 1) / SEGMENT_SIZE);
85         }
86
87         if (n_seg > MAX_PASS_LEN)
88                 n_seg = MAX_PASS_LEN;   /* truncate at max length */
89
90         /* set up some pointers */
91         cipher_ptr = dec_c2_cryptbuf;
92         plaintext_ptr = keybuf;
93
94         /* do the first block with supplied salt */
95         tmp_ptr = crypt(plaintext_ptr, salt);   /* libc crypt() */
96
97         /* and place in the static area */
98         strncpy(cipher_ptr, tmp_ptr, 13);
99         cipher_ptr += ESEGMENT_SIZE + SALT_SIZE;
100         plaintext_ptr += SEGMENT_SIZE;  /* first block of SEGMENT_SIZE */
101
102         /* change the salt (1st 2 chars of previous block) - this was found
103            by dowsing */
104
105         salt_ptr = cipher_ptr - ESEGMENT_SIZE;
106
107         /* so far this is identical to "return crypt(key, salt);", if
108            there is more than one block encrypt them... */
109
110         if (n_seg > 1) {
111                 for (j = 2; j <= n_seg; j++) {
112
113                         tmp_ptr = crypt(plaintext_ptr, salt_ptr);
114
115                         /* skip the salt for seg!=0 */
116                         strncpy(cipher_ptr, (tmp_ptr + SALT_SIZE), ESEGMENT_SIZE);
117
118                         cipher_ptr += ESEGMENT_SIZE;
119                         plaintext_ptr += SEGMENT_SIZE;
120                         salt_ptr = cipher_ptr - ESEGMENT_SIZE;
121                 }
122         }
123         D(("key=|%s|, salt=|%s|\nbuf=|%s|\n", key, salt, dec_c2_cryptbuf));
124
125         /* this is the <NUL> terminated encrypted password */
126
127         return dec_c2_cryptbuf;
128 }