Annotation of src/usr.bin/encrypt/encrypt.c, Revision 1.32
1.32 ! jmc 1: /* $OpenBSD: encrypt.c,v 1.31 2014/09/03 07:47:50 giovanni Exp $ */
1.1 downsj 2:
3: /*
4: * Copyright (c) 1996, Jason Downs. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: *
15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
19: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25: * SUCH DAMAGE.
26: */
27:
28: #include <sys/types.h>
1.15 millert 29: #include <ctype.h>
1.1 downsj 30: #include <err.h>
1.3 downsj 31: #include <errno.h>
1.15 millert 32: #include <pwd.h>
33: #include <stdio.h>
1.7 kstailey 34: #include <stdlib.h>
1.1 downsj 35: #include <string.h>
36: #include <unistd.h>
1.14 millert 37: #include <login_cap.h>
1.27 jdixon 38: #include <limits.h>
1.1 downsj 39:
40: /*
41: * Very simple little program, for encrypting passwords from the command
42: * line. Useful for scripts and such.
43: */
44:
1.5 provos 45: #define DO_MAKEKEY 0
46: #define DO_DES 1
1.31 giovanni 47: #define DO_BLF 2
1.5 provos 48:
1.15 millert 49: extern char *__progname;
1.5 provos 50: char buffer[_PASSWORD_LEN];
1.19 deraadt 51:
52: void usage(void);
1.30 deraadt 53: int ideal_rounds(void);
1.19 deraadt 54: void print_passwd(char *, int, void *);
1.3 downsj 55:
1.15 millert 56: void
57: usage(void)
1.1 downsj 58: {
1.15 millert 59:
60: (void)fprintf(stderr,
1.32 ! jmc 61: "usage: %s [-k] [-b rounds] [-c class] [-p | string] [-s salt]\n",
1.15 millert 62: __progname);
63: exit(1);
1.1 downsj 64: }
65:
1.29 tedu 66: /*
67: * Time how long 8 rounds takes to measure this system's performance.
68: * We are aiming for something that takes between 0.25 and 0.5 seconds.
69: */
70: int
1.30 deraadt 71: ideal_rounds(void)
1.29 tedu 72: {
73: clock_t before, after;
74: int r = 8;
75: char buf[_PASSWORD_LEN];
76: int duration;
77:
78: strlcpy(buf, bcrypt_gensalt(r), _PASSWORD_LEN);
79: before = clock();
80: crypt("testpassword", buf);
81: after = clock();
82:
83: duration = after - before;
84:
85: /* too quick? slow it down. */
86: while (duration <= CLOCKS_PER_SEC / 4) {
87: r += 1;
88: duration *= 2;
89: }
90: /* too slow? speed it up. */
91: while (duration > CLOCKS_PER_SEC / 2) {
92: r -= 1;
93: duration /= 2;
94: }
95:
96: return r;
97: }
98:
99:
1.15 millert 100: void
101: print_passwd(char *string, int operation, void *extra)
1.5 provos 102: {
1.23 moritz 103: char msalt[3], *salt, *cryptstr;
1.15 millert 104: login_cap_t *lc;
1.21 millert 105: int pwd_gensalt(char *, int, login_cap_t *, char);
1.22 deraadt 106: void to64(char *, u_int32_t, int n);
1.15 millert 107:
108: switch(operation) {
109: case DO_MAKEKEY:
110: /*
111: * makekey mode: parse string into separate DES key and salt.
112: */
113: if (strlen(string) != 10) {
114: /* To be compatible... */
115: errx(1, "%s", strerror(EFTYPE));
116: }
1.17 deraadt 117: strlcpy(msalt, &string[8], sizeof msalt);
1.15 millert 118: salt = msalt;
119: break;
120:
121: case DO_BLF:
122: strlcpy(buffer, bcrypt_gensalt(*(int *)extra), _PASSWORD_LEN);
123: salt = buffer;
124: break;
125:
126: case DO_DES:
127: salt = extra;
128: break;
129:
130: default:
1.18 millert 131: if ((lc = login_getclass(extra)) == NULL)
132: errx(1, "unable to get login class `%s'",
133: extra ? (char *)extra : "default");
1.21 millert 134: if (!pwd_gensalt(buffer, _PASSWORD_LEN, lc, 'l'))
1.15 millert 135: errx(1, "can't generate salt");
136: salt = buffer;
137: break;
138: }
139:
1.23 moritz 140: if ((cryptstr = crypt(string, salt)) == NULL)
141: errx(1, "crypt failed");
142: fputs(cryptstr, stdout);
1.5 provos 143: }
144:
1.15 millert 145: int
146: main(int argc, char **argv)
1.1 downsj 147: {
1.15 millert 148: int opt;
149: int operation = -1;
150: int prompt = 0;
151: int rounds;
1.18 millert 152: void *extra = NULL; /* Store salt or number of rounds */
1.27 jdixon 153: const char *errstr;
1.15 millert 154:
155: if (strcmp(__progname, "makekey") == 0)
156: operation = DO_MAKEKEY;
157:
1.31 giovanni 158: while ((opt = getopt(argc, argv, "kps:b:c:")) != -1) {
1.15 millert 159: switch (opt) {
160: case 'k': /* Stdin/Stdout Unix crypt */
161: if (operation != -1 || prompt)
162: usage();
163: operation = DO_MAKEKEY;
164: break;
165:
166: case 'p':
167: if (operation == DO_MAKEKEY)
168: usage();
169: prompt = 1;
170: break;
171:
172: case 's': /* Unix crypt (DES) */
173: if (operation != -1 || optarg[0] == '$')
174: usage();
175: operation = DO_DES;
176: extra = optarg;
177: break;
178:
179: case 'b': /* Blowfish password hash */
180: if (operation != -1)
181: usage();
182: operation = DO_BLF;
1.29 tedu 183: if (strcmp(optarg, "a") == 0)
184: rounds = ideal_rounds();
185: else
186: rounds = strtonum(optarg, 1, INT_MAX, &errstr);
1.27 jdixon 187: if (errstr != NULL)
188: errx(1, "%s: %s", errstr, optarg);
1.15 millert 189: extra = &rounds;
1.18 millert 190: break;
191:
192: case 'c': /* user login class */
193: extra = optarg;
194: operation = -1;
1.15 millert 195: break;
196:
197: default:
198: usage();
199: }
1.1 downsj 200: }
201:
1.15 millert 202: if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
203: char line[BUFSIZ], *string;
1.1 downsj 204:
1.15 millert 205: if (prompt) {
1.20 otto 206: if ((string = getpass("Enter string: ")) == NULL)
207: err(1, "getpass");
1.15 millert 208: print_passwd(string, operation, extra);
209: (void)fputc('\n', stdout);
210: } else {
1.28 krw 211: size_t len;
1.15 millert 212: /* Encrypt stdin to stdout. */
213: while (!feof(stdin) &&
214: (fgets(line, sizeof(line), stdin) != NULL)) {
1.28 krw 215: len = strlen(line);
216: if (len == 0 || line[0] == '\n')
1.15 millert 217: continue;
1.28 krw 218: if (line[len - 1] == '\n')
219: line[len - 1] = '\0';
220:
221: print_passwd(line, operation, extra);
1.15 millert 222:
223: if (operation == DO_MAKEKEY) {
224: fflush(stdout);
225: break;
226: }
227: (void)fputc('\n', stdout);
228: }
229: }
1.9 alex 230: } else {
1.15 millert 231: char *string;
232:
233: /* can't combine -p with a supplied string */
234: if (prompt)
235: usage();
236:
237: /* Perhaps it isn't worth worrying about, but... */
238: if ((string = strdup(argv[optind])) == NULL)
239: err(1, NULL);
240: /* Wipe the argument. */
241: memset(argv[optind], 0, strlen(argv[optind]));
242:
1.9 alex 243: print_passwd(string, operation, extra);
244:
1.15 millert 245: (void)fputc('\n', stdout);
246:
247: /* Wipe our copy, before we free it. */
248: memset(string, 0, strlen(string));
249: free(string);
1.1 downsj 250: }
1.15 millert 251: exit(0);
1.1 downsj 252: }