4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 1999-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
40 #include "ns_internal.h"
43 static char t1
[ROTORSIZE
];
44 static char t2
[ROTORSIZE
];
45 static char t3
[ROTORSIZE
];
46 static char hexdig
[] = "0123456789abcdef";
48 static mutex_t ns_crypt_lock
= DEFAULTMUTEX
;
49 static boolean_t crypt_inited
= B_FALSE
;
52 is_cleartext(const char *pwd
)
54 if (0 == strncmp(pwd
, CRYPTMARK
, strlen(CRYPTMARK
)))
61 hex2ascii(char *aString
, int aLen
)
66 if ((res
= (char *)calloc(aLen
*2 + 1, 1)) == NULL
) {
72 res
[i
] = hexdig
[(*aString
& 0xf0) >> 4];
73 res
[i
+ 1] = hexdig
[*aString
& 0x0f];
85 return (c
>= '0' && c
<= '9' ? c
- '0'
86 : c
>= 'A' && c
<= 'F' ? c
- 'A' + 10
92 ascii2hex(char *anHexaStr
, int *aResLen
)
95 char *theRes
= malloc(strlen(anHexaStr
) /2 + 1);
99 while (isxdigit(*anHexaStr
)) {
100 theRes
[theLen
] = unhex(*anHexaStr
) << 4;
101 if (++anHexaStr
!= '\0') {
102 theRes
[theLen
] += unhex(*anHexaStr
);
107 theRes
[theLen
] = '\0';
121 (void) mutex_lock(&ns_crypt_lock
);
123 (void) mutex_unlock(&ns_crypt_lock
);
126 (void) strcpy(buf
, "Homer J");
129 (void) strncpy(buf
, (char *)crypt(buf
, &buf
[8]), 13);
131 for (i
= 0; i
< 13; i
++)
132 seed
= seed
*buf
[i
] + i
;
133 for (i
= 0; i
< ROTORSIZE
; i
++) {
137 for (i
= 0; i
< ROTORSIZE
; i
++) {
138 seed
= 5*seed
+ buf
[i
%13];
139 random
= seed
% 65521;
141 ic
= (random
&MASK
)%(k
+1);
146 if (t3
[k
] != 0) continue;
147 ic
= (random
&MASK
) % k
;
148 while (t3
[ic
] != 0) ic
= (ic
+ 1) % k
;
152 for (i
= 0; i
< ROTORSIZE
; i
++)
154 crypt_inited
= B_TRUE
;
155 (void) mutex_unlock(&ns_crypt_lock
);
160 modvalue(char *str
, int len
, int *mod_len
)
170 if ((s
= (char *)malloc(2 * len
+ 1)) != NULL
) {
172 s
[i
] = t2
[(t3
[(t1
[(str
[i
]+n1
)&MASK
]+n2
)&MASK
]-n2
)&MASK
]-n1
;
175 if (n1
== ROTORSIZE
) {
178 if (n2
== ROTORSIZE
) n2
= 0;
192 char *modv
, *str
, *ev
;
197 * if not cleartext, return a copy of what ptr
198 * points to as that is what evalue does below.
200 if (FALSE
== is_cleartext(ptr
)) {
205 modv
= modvalue(ptr
, strlen(ptr
), &modv_len
);
206 str
= hex2ascii(modv
, modv_len
);
209 len
= strlen(str
) + strlen(CRYPTMARK
) + 1;
215 (void) snprintf(ev
, len
, CRYPTMARK
"%s", str
);
225 char *modv
, *str
, *sb
;
228 /* if cleartext return NULL (error!) */
229 if (TRUE
== is_cleartext(ptr
))
232 sb
= strchr(ptr
, '}');
235 str
= ascii2hex(sb
, &len
);
236 modv
= modvalue(str
, len
, NULL
);