rc4.c revision 296465
11541Srgrimes/* crypto/rc4/rc4.c */
21541Srgrimes/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * This package is an SSL implementation written
61541Srgrimes * by Eric Young (eay@cryptsoft.com).
71541Srgrimes * The implementation was written so as to conform with Netscapes SSL.
81541Srgrimes *
91541Srgrimes * This library is free for commercial and non-commercial use as long as
101541Srgrimes * the following conditions are aheared to.  The following conditions
111541Srgrimes * apply to all code found in this distribution, be it the RC4, RSA,
121541Srgrimes * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
131541Srgrimes * included with this distribution is covered by the same copyright terms
141541Srgrimes * except that the holder is Tim Hudson (tjh@cryptsoft.com).
151541Srgrimes *
161541Srgrimes * Copyright remains Eric Young's, and as such any Copyright notices in
171541Srgrimes * the code are not to be removed.
181541Srgrimes * If this package is used in a product, Eric Young should be given attribution
191541Srgrimes * as the author of the parts of the library used.
201541Srgrimes * This can be in the form of a textual message at program startup or
211541Srgrimes * in documentation (online or textual) provided with the package.
221541Srgrimes *
231541Srgrimes * Redistribution and use in source and binary forms, with or without
241541Srgrimes * modification, are permitted provided that the following conditions
251541Srgrimes * are met:
261541Srgrimes * 1. Redistributions of source code must retain the copyright
271541Srgrimes *    notice, this list of conditions and the following disclaimer.
281541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
291541Srgrimes *    notice, this list of conditions and the following disclaimer in the
301541Srgrimes *    documentation and/or other materials provided with the distribution.
311541Srgrimes * 3. All advertising materials mentioning features or use of this software
321541Srgrimes *    must display the following acknowledgement:
3314497Shsu *    "This product includes cryptographic software written by
3424131Sbde *     Eric Young (eay@cryptsoft.com)"
351541Srgrimes *    The word 'cryptographic' can be left out if the rouines from the library
361541Srgrimes *    being used are not cryptographic related :-).
372165Spaul * 4. If you include any Windows specific code (or a derivative thereof) from
3818020Sbde *    the apps directory (application code) you must include an acknowledgement:
392165Spaul *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4024131Sbde *
411541Srgrimes * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
421541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4318020Sbde * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
441541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
451541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4614497Shsu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4714497Shsu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
481541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
491541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
501541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
511541Srgrimes * SUCH DAMAGE.
521541Srgrimes *
531541Srgrimes * The licence and distribution terms for any publically available version or
541541Srgrimes * derivative of this code cannot be changed.  i.e. this code cannot simply be
551541Srgrimes * copied and put under another distribution licence
5614497Shsu * [including the GNU Public Licence.]
571541Srgrimes */
581541Srgrimes
591541Srgrimes#include <stdio.h>
6020691Sbde#include <stdlib.h>
6120691Sbde#include <string.h>
621541Srgrimes#include <openssl/rc4.h>
631541Srgrimes#include <openssl/evp.h>
641541Srgrimes
651541Srgrimeschar *usage[] = {
661541Srgrimes    "usage: rc4 args\n",
671541Srgrimes    "\n",
681541Srgrimes    " -in arg         - input file - default stdin\n",
691541Srgrimes    " -out arg        - output file - default stdout\n",
701541Srgrimes    " -key key        - password\n",
711541Srgrimes    NULL
721541Srgrimes};
731541Srgrimes
741541Srgrimesint main(int argc, char *argv[])
751541Srgrimes{
761541Srgrimes    FILE *in = NULL, *out = NULL;
7721002Sdyson    char *infile = NULL, *outfile = NULL, *keystr = NULL;
7821002Sdyson    RC4_KEY key;
7921002Sdyson    char buf[BUFSIZ];
8021002Sdyson    int badops = 0, i;
8121002Sdyson    char **pp;
8221002Sdyson    unsigned char md[MD5_DIGEST_LENGTH];
8321002Sdyson
841541Srgrimes    argc--;
851541Srgrimes    argv++;
861541Srgrimes    while (argc >= 1) {
871541Srgrimes        if (strcmp(*argv, "-in") == 0) {
8814497Shsu            if (--argc < 1)
8914497Shsu                goto bad;
907090Sbde            infile = *(++argv);
911541Srgrimes        } else if (strcmp(*argv, "-out") == 0) {
926577Sguido            if (--argc < 1)
931541Srgrimes                goto bad;
941541Srgrimes            outfile = *(++argv);
951541Srgrimes        } else if (strcmp(*argv, "-key") == 0) {
962165Spaul            if (--argc < 1)
9718020Sbde                goto bad;
98            keystr = *(++argv);
99        } else {
100            fprintf(stderr, "unknown option %s\n", *argv);
101            badops = 1;
102            break;
103        }
104        argc--;
105        argv++;
106    }
107
108    if (badops) {
109 bad:
110        for (pp = usage; (*pp != NULL); pp++)
111            fprintf(stderr, "%s", *pp);
112        exit(1);
113    }
114
115    if (infile == NULL)
116        in = stdin;
117    else {
118        in = fopen(infile, "r");
119        if (in == NULL) {
120            perror("open");
121            exit(1);
122        }
123
124    }
125    if (outfile == NULL)
126        out = stdout;
127    else {
128        out = fopen(outfile, "w");
129        if (out == NULL) {
130            perror("open");
131            exit(1);
132        }
133    }
134
135#ifdef OPENSSL_SYS_MSDOS
136    /* This should set the file to binary mode. */
137    {
138# include <fcntl.h>
139        setmode(fileno(in), O_BINARY);
140        setmode(fileno(out), O_BINARY);
141    }
142#endif
143
144    if (keystr == NULL) {       /* get key */
145        i = EVP_read_pw_string(buf, BUFSIZ, "Enter RC4 password:", 0);
146        if (i != 0) {
147            OPENSSL_cleanse(buf, BUFSIZ);
148            fprintf(stderr, "bad password read\n");
149            exit(1);
150        }
151        keystr = buf;
152    }
153
154    EVP_Digest((unsigned char *)keystr, strlen(keystr), md, NULL, EVP_md5(),
155               NULL);
156    OPENSSL_cleanse(keystr, strlen(keystr));
157    RC4_set_key(&key, MD5_DIGEST_LENGTH, md);
158
159    for (;;) {
160        i = fread(buf, 1, BUFSIZ, in);
161        if (i == 0)
162            break;
163        if (i < 0) {
164            perror("read");
165            exit(1);
166        }
167        RC4(&key, (unsigned int)i, (unsigned char *)buf,
168            (unsigned char *)buf);
169        i = fwrite(buf, (unsigned int)i, 1, out);
170        if (i != 1) {
171            perror("write");
172            exit(1);
173        }
174    }
175    fclose(out);
176    fclose(in);
177    exit(0);
178    return (1);
179}
180