[BACK]Return to x99token.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / x99token

Annotation of src/usr.bin/x99token/x99token.c, Revision 1.10

1.10    ! deraadt     1: /*     $OpenBSD: x99token.c,v 1.9 2013/11/27 00:13:22 deraadt Exp $    */
1.6       millert     2:
1.1       millert     3: /*
                      4:  * X9.9 calculator
                      5:  * This software is provided AS IS with no express or implied warranty
                      6:  * October 1995, Paul Borman <prb@krystal.com>
1.5       millert     7:  *
                      8:  * Donated to the Public Domain by Paul Borman
1.1       millert     9:  */
1.10    ! deraadt    10:
1.2       millert    11: #include <sys/stat.h>
                     12:
                     13: #include <ctype.h>
                     14: #include <err.h>
1.1       millert    15: #include <pwd.h>
1.2       millert    16: #include <readpassphrase.h>
1.1       millert    17: #include <stdio.h>
                     18: #include <stdlib.h>
                     19: #include <string.h>
                     20: #include <unistd.h>
1.10    ! deraadt    21: #include <limits.h>
1.8       jsg        22: #include <openssl/des.h>
1.1       millert    23:
1.2       millert    24: #define        KEYFILE         ".keyfile.des"
1.1       millert    25: #define        HEXDIGITS       "0123456789abcdef"
                     26: #define        DECDIGITS       "0123456789012345"
                     27:
1.8       jsg        28: void predict(DES_key_schedule, const char *, int);
1.2       millert    29:
1.1       millert    30: char *digits = HEXDIGITS;
1.2       millert    31: extern char *__progname;
1.1       millert    32:
                     33: int
1.2       millert    34: main(int argc, char **argv)
1.1       millert    35: {
                     36:        int i;
                     37:        char buf[256];
1.8       jsg        38:        DES_key_schedule ks;
                     39:        DES_cblock key;
1.10    ! deraadt    40:        char _keyfile[PATH_MAX];
1.1       millert    41:        char *keyfile = 0;
                     42:        FILE *fp;
                     43:        int init = 0;
                     44:        int hex = 1;
                     45:        int cnt = 1;
1.2       millert    46:        unsigned int pin;
1.1       millert    47:        struct passwd *pwd;
                     48:
1.2       millert    49:        while ((i = getopt(argc, argv, "dk:in:")) != -1) {
1.1       millert    50:                switch (i) {
                     51:                case 'k':
                     52:                        keyfile = optarg;
                     53:                        break;
                     54:                case 'i':
                     55:                        init = 1;
                     56:                        break;
                     57:                case 'd':
                     58:                        hex = 0;
                     59:                        break;
                     60:                case 'n':
                     61:                        cnt = atoi(optarg);
1.2       millert    62:                        if (cnt <= 0)
                     63:                                err(1, "invalid count: %s", optarg);
1.1       millert    64:                        break;
                     65:                default:
1.7       jmc        66:                        fprintf(stderr,
                     67:                            "usage: %s [-d] [-k keyfile] [-n count]\n"
                     68:                            "       %s -i [-k keyfile]\n",
                     69:                            __progname, __progname);
1.1       millert    70:                        exit(1);
                     71:                }
1.2       millert    72:        }
1.1       millert    73:
                     74:        if (!keyfile) {
1.2       millert    75:                if ((pwd = getpwuid(getuid())) == NULL) {
1.1       millert    76:                        fprintf(stderr, "Say, just who are you, anyhow?\n");
                     77:                        exit(1);
                     78:                }
1.2       millert    79:                snprintf(_keyfile, sizeof(_keyfile), "%s/%s", pwd->pw_dir,
                     80:                    KEYFILE);
1.1       millert    81:                keyfile = _keyfile;
                     82:        }
                     83:
1.2       millert    84:        if (init)
                     85:                readpassphrase("Enter Key: ", buf, sizeof(buf), 0);
                     86:        else if ((fp = fopen(keyfile, "r")) == NULL)
                     87:                err(1, "unable to open %s", keyfile);
                     88:        else {
1.1       millert    89:                if (fgets(buf, sizeof(buf), fp) == NULL) {
                     90:                        fprintf(stderr, "No key in %s\n", keyfile);
                     91:                        exit(1);
                     92:                }
                     93:                fclose(fp);
                     94:        }
                     95:
                     96:        memset(key, 0, sizeof(key));
                     97:        if (init && buf[3] == ' ') {
                     98:                char *b = buf;
                     99:                /* Assume octal input */
                    100:                for (i = 0; i < 8; ++i) {
1.4       millert   101:                        if (!*b)
1.1       millert   102:                                fprintf(stderr, "%s: invalid key\n", buf);
1.9       deraadt   103:                        while (isdigit((unsigned char)*b))
1.4       millert   104:                                key[i] = key[i] << 3 | (*b++ - '0');
1.9       deraadt   105:                        while (*b && !isdigit((unsigned char)*b))
1.1       millert   106:                                ++b;
                    107:                }
1.2       millert   108:        } else {
1.1       millert   109:                for (i = 0; i < 16; ++i) {
                    110:                        int d;
                    111:
1.9       deraadt   112:                        if (islower((unsigned char)buf[i]))
                    113:                                buf[i] = toupper((unsigned char)buf[i]);
1.1       millert   114:                        if (buf[i] >= '0' && buf[i] <= '9')
                    115:                                d = buf[i] - '0';
                    116:                        else if (buf[i] >= 'A' && buf[i] <= 'F')
                    117:                                d = buf[i] - 'A' + 10;
                    118:                        else {
                    119:                                fprintf(stderr, "invalid key: %s\n", buf);
                    120:                                exit(1);
                    121:                        }
                    122:                        key[i>>1] |= d << ((i & 1) ? 0 : 4);
                    123:                }
1.2       millert   124:        }
1.1       millert   125:
1.2       millert   126:        /* XXX - should warn on non-space or non-digit */
                    127:        readpassphrase("Enter Pin: ", buf, sizeof(buf), 0);
                    128:        for (i = 0, pin = 0; buf[i] && buf[i] != '\n'; ++i)
1.9       deraadt   129:                if (isdigit((unsigned char)buf[i]))
1.1       millert   130:                        pin = pin * 16 + buf[i] - '0' + 1;
                    131:
1.2       millert   132:        if ((pin & 0xffff0000) == 0)
1.1       millert   133:                pin |= pin << 16;
                    134:
                    135:        for (i = 0; i < 8; ++i)
                    136:                key[0] ^= (pin >> ((i * 7) % 26)) & 0x7f;
                    137:
                    138:        if (init) {
1.2       millert   139:                if ((fp = fopen(keyfile, "w")) == NULL)
                    140:                        err(1, "could not open %s for writing", keyfile);
                    141:                fchmod(fileno(fp), 0600);
1.1       millert   142:                for (i = 0; i < 8; ++i) {
                    143:                        fprintf(fp, "%c", digits[(key[i]>>4)&0xf]);
                    144:                        fprintf(fp, "%c", digits[(key[i]>>0)&0xf]);
                    145:                }
1.2       millert   146:                fputc('\n', fp);
1.1       millert   147:                fclose(fp);
                    148:                exit(0);
                    149:        }
                    150:
1.8       jsg       151:        DES_fixup_key_parity(&key);
                    152:        DES_key_sched(&key, &ks);
1.1       millert   153:
1.2       millert   154:        buf[0] = '\0';
1.3       deraadt   155:        readpassphrase("Enter challenge: ", buf, sizeof(buf), RPP_ECHO_ON);
1.2       millert   156:        if (buf[0] == '\0')
1.1       millert   157:                exit(0);
                    158:
                    159:        for (i = 0; i < 8; ++i)
                    160:                if (buf[i] == '\n')
                    161:                        buf[i] = '\0';
                    162:
                    163:        if (!hex)
                    164:                digits = DECDIGITS;
                    165:
                    166:        predict(ks, buf, cnt);
                    167:
1.2       millert   168:        memset(&ks, 0, sizeof(ks));
                    169:        memset(buf, 0, sizeof(buf));
                    170:
1.1       millert   171:        exit(0);
                    172: }
                    173:
                    174: void
1.8       jsg       175: predict(DES_key_schedule ks, const char *chal, int cnt)
1.1       millert   176: {
                    177:        int i;
1.8       jsg       178:        DES_cblock cb;
1.1       millert   179:
1.4       millert   180:        memcpy(&cb, chal, sizeof(cb));
1.1       millert   181:        while (cnt-- > 0) {
1.4       millert   182:                printf("%.8s: ", (char *)cb);
1.8       jsg       183:                DES_ecb_encrypt(&cb, &cb, &ks, DES_ENCRYPT);
1.1       millert   184:                for (i = 0; i < 4; ++i) {
1.2       millert   185:                        printf("%c", digits[(cb[i]>>4) & 0xf]);
                    186:                        printf("%c", digits[(cb[i]>>0) & 0xf]);
1.1       millert   187:                }
1.2       millert   188:                putchar('\n');
1.1       millert   189:                for (i = 0; i < 8; ++i) {
1.2       millert   190:                        if ((cb[i] &= 0xf) > 9)
                    191:                                cb[i] -= 10;
                    192:                        cb[i] |= 0x30;
1.1       millert   193:                }
                    194:        }
1.2       millert   195:        memset(&cb, 0, sizeof(cb));
1.1       millert   196: }