8 #include <openssl/sha1.h>
14 /* from %OpenBSD: skeylogin.c,v 1.32 1999/08/16 14:46:56 millert Exp % */
17 #define ROUND(x) (((x)[0] << 24) + (((x)[1]) << 16) + (((x)[2]) << 8) + \
30 if ((strlen(s) % sizeof(u_int32_t)) == 0)
31 target = strlen(s); /* Multiple of 4 */
33 target = strlen(s) - (strlen(s) % sizeof(u_int32_t));
35 for (i = 0, len = 0; len < target; len += 4)
42 skey_fake_keyinfo(char *username)
46 u_char hseed[SKEY_MAX_SEED_LEN], flg = 1, *up;
47 char pbuf[SKEY_MAX_PW_LEN+1];
48 static char skeyprompt[SKEY_MAX_CHALLENGE+1];
55 * Base first 4 chars of seed on hostname.
56 * Add some filler for short hostnames if necessary.
58 if (gethostname(pbuf, sizeof(pbuf)) == -1)
61 for (p = pbuf; *p && isalnum(*p); p++)
62 if (isalpha(*p) && isupper(*p))
64 if (*p && pbuf - p < 4)
65 (void)strncpy(p, "asjd", 4 - (pbuf - p));
68 /* Hash the username if possible */
69 if ((up = SHA1Data(username, strlen(username), NULL)) != NULL) {
74 /* Collapse the hash */
75 ptr = hash_collapse(up);
76 memset(up, 0, strlen(up));
78 /* See if the random file's there, else use ctime */
79 if ((fd = open(_SKEY_RAND_FILE_PATH_, O_RDONLY)) != -1
80 && fstat(fd, &sb) == 0 &&
81 sb.st_size > (off_t)SKEY_MAX_SEED_LEN &&
82 lseek(fd, ptr % (sb.st_size - SKEY_MAX_SEED_LEN),
83 SEEK_SET) != -1 && read(fd, hseed,
84 SKEY_MAX_SEED_LEN) == SKEY_MAX_SEED_LEN) {
87 secretlen = SKEY_MAX_SEED_LEN;
89 } else if (!stat(_PATH_MEM, &sb) || !stat("/", &sb)) {
92 secretlen = strlen(secret);
97 /* Put that in your pipe and smoke it */
99 /* Hash secret value with username */
101 SHA1Update(&ctx, secret, secretlen);
102 SHA1Update(&ctx, username, strlen(username));
106 memset(secret, 0, secretlen);
108 /* Now hash the hash */
110 SHA1Update(&ctx, up, strlen(up));
113 ptr = hash_collapse(up + 4);
115 for (i = 4; i < 9; i++) {
116 pbuf[i] = (ptr % 10) + '0';
121 /* Sequence number */
122 ptr = ((up[2] + up[3]) % 99) + 1;
124 memset(up, 0, 20); /* SHA1 specific */
127 (void)snprintf(skeyprompt, sizeof skeyprompt,
129 SKEY_MAX_HASHNAME_LEN,
130 skey_get_algorithm(),
131 ptr, SKEY_MAX_SEED_LEN,
134 /* Base last 8 chars of seed on username */
140 /* Pad remainder with zeros */
146 *p++ = (*u++ % 10) + '0';
150 (void)snprintf(skeyprompt, sizeof skeyprompt,
152 SKEY_MAX_HASHNAME_LEN,
153 skey_get_algorithm(),
154 99, SKEY_MAX_SEED_LEN, pbuf);