xref: /freebsd/crypto/openssl/apps/enc.c (revision 94ad176c6825318dda7216a27ac1be5527c1cb21)
174664626SKris Kennaway /* apps/enc.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include <stdlib.h>
6174664626SKris Kennaway #include <string.h>
6274664626SKris Kennaway #include "apps.h"
6374664626SKris Kennaway #include <openssl/bio.h>
6474664626SKris Kennaway #include <openssl/err.h>
6574664626SKris Kennaway #include <openssl/evp.h>
6674664626SKris Kennaway #include <openssl/objects.h>
6774664626SKris Kennaway #include <openssl/x509.h>
68f579bf8eSKris Kennaway #include <openssl/rand.h>
6974664626SKris Kennaway #include <openssl/pem.h>
701f13597dSJung-uk Kim #include <openssl/comp.h>
715c87c606SMark Murray #include <ctype.h>
7274664626SKris Kennaway 
7374664626SKris Kennaway int set_hex(char *in,unsigned char *out,int size);
7474664626SKris Kennaway #undef SIZE
7574664626SKris Kennaway #undef BSIZE
7674664626SKris Kennaway #undef PROG
7774664626SKris Kennaway 
7874664626SKris Kennaway #define SIZE	(512)
7974664626SKris Kennaway #define BSIZE	(8*1024)
8074664626SKris Kennaway #define	PROG	enc_main
8174664626SKris Kennaway 
825c87c606SMark Murray static void show_ciphers(const OBJ_NAME *name,void *bio_)
835c87c606SMark Murray 	{
845c87c606SMark Murray 	BIO *bio=bio_;
855c87c606SMark Murray 	static int n;
865c87c606SMark Murray 
875c87c606SMark Murray 	if(!islower((unsigned char)*name->name))
885c87c606SMark Murray 		return;
895c87c606SMark Murray 
905c87c606SMark Murray 	BIO_printf(bio,"-%-25s",name->name);
915c87c606SMark Murray 	if(++n == 3)
925c87c606SMark Murray 		{
935c87c606SMark Murray 		BIO_printf(bio,"\n");
945c87c606SMark Murray 		n=0;
955c87c606SMark Murray 		}
965c87c606SMark Murray 	else
975c87c606SMark Murray 		BIO_printf(bio," ");
985c87c606SMark Murray 	}
995c87c606SMark Murray 
100f579bf8eSKris Kennaway int MAIN(int, char **);
101f579bf8eSKris Kennaway 
10274664626SKris Kennaway int MAIN(int argc, char **argv)
10374664626SKris Kennaway 	{
104f579bf8eSKris Kennaway 	static const char magic[]="Salted__";
1055c87c606SMark Murray 	char mbuf[sizeof magic-1];
10674664626SKris Kennaway 	char *strbuf=NULL;
10774664626SKris Kennaway 	unsigned char *buff=NULL,*bufsize=NULL;
10874664626SKris Kennaway 	int bsize=BSIZE,verbose=0;
10974664626SKris Kennaway 	int ret=1,inl;
1105c87c606SMark Murray 	int nopad = 0;
1115c87c606SMark Murray 	unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
112f579bf8eSKris Kennaway 	unsigned char salt[PKCS5_SALT_LEN];
113f579bf8eSKris Kennaway 	char *str=NULL, *passarg = NULL, *pass = NULL;
114f579bf8eSKris Kennaway 	char *hkey=NULL,*hiv=NULL,*hsalt = NULL;
1156be8ae07SJacques Vidrine 	char *md=NULL;
11674664626SKris Kennaway 	int enc=1,printkey=0,i,base64=0;
1171f13597dSJung-uk Kim #ifdef ZLIB
1181f13597dSJung-uk Kim 	int do_zlib=0;
1191f13597dSJung-uk Kim 	BIO *bzl = NULL;
1201f13597dSJung-uk Kim #endif
121f579bf8eSKris Kennaway 	int debug=0,olb64=0,nosalt=0;
12274664626SKris Kennaway 	const EVP_CIPHER *cipher=NULL,*c;
1233b4e3dcbSSimon L. B. Nielsen 	EVP_CIPHER_CTX *ctx = NULL;
12474664626SKris Kennaway 	char *inf=NULL,*outf=NULL;
12574664626SKris Kennaway 	BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL;
126c1803d78SJacques Vidrine #define PROG_NAME_SIZE  39
127c1803d78SJacques Vidrine 	char pname[PROG_NAME_SIZE+1];
128fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1295c87c606SMark Murray 	char *engine = NULL;
130fceca8a3SJacques Vidrine #endif
1316be8ae07SJacques Vidrine 	const EVP_MD *dgst=NULL;
132db522d3aSSimon L. B. Nielsen 	int non_fips_allow = 0;
13374664626SKris Kennaway 
13474664626SKris Kennaway 	apps_startup();
13574664626SKris Kennaway 
13674664626SKris Kennaway 	if (bio_err == NULL)
13774664626SKris Kennaway 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
13874664626SKris Kennaway 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
13974664626SKris Kennaway 
1405c87c606SMark Murray 	if (!load_config(bio_err, NULL))
1415c87c606SMark Murray 		goto end;
1425c87c606SMark Murray 
14374664626SKris Kennaway 	/* first check the program name */
1445c87c606SMark Murray 	program_name(argv[0],pname,sizeof pname);
14574664626SKris Kennaway 	if (strcmp(pname,"base64") == 0)
14674664626SKris Kennaway 		base64=1;
1471f13597dSJung-uk Kim #ifdef ZLIB
1481f13597dSJung-uk Kim 	if (strcmp(pname,"zlib") == 0)
1491f13597dSJung-uk Kim 		do_zlib=1;
1501f13597dSJung-uk Kim #endif
15174664626SKris Kennaway 
15274664626SKris Kennaway 	cipher=EVP_get_cipherbyname(pname);
1531f13597dSJung-uk Kim #ifdef ZLIB
1541f13597dSJung-uk Kim 	if (!do_zlib && !base64 && (cipher == NULL)
1551f13597dSJung-uk Kim 				&& (strcmp(pname,"enc") != 0))
1561f13597dSJung-uk Kim #else
15774664626SKris Kennaway 	if (!base64 && (cipher == NULL) && (strcmp(pname,"enc") != 0))
1581f13597dSJung-uk Kim #endif
15974664626SKris Kennaway 		{
16074664626SKris Kennaway 		BIO_printf(bio_err,"%s is an unknown cipher\n",pname);
16174664626SKris Kennaway 		goto bad;
16274664626SKris Kennaway 		}
16374664626SKris Kennaway 
16474664626SKris Kennaway 	argc--;
16574664626SKris Kennaway 	argv++;
16674664626SKris Kennaway 	while (argc >= 1)
16774664626SKris Kennaway 		{
16874664626SKris Kennaway 		if	(strcmp(*argv,"-e") == 0)
16974664626SKris Kennaway 			enc=1;
17074664626SKris Kennaway 		else if (strcmp(*argv,"-in") == 0)
17174664626SKris Kennaway 			{
17274664626SKris Kennaway 			if (--argc < 1) goto bad;
17374664626SKris Kennaway 			inf= *(++argv);
17474664626SKris Kennaway 			}
17574664626SKris Kennaway 		else if (strcmp(*argv,"-out") == 0)
17674664626SKris Kennaway 			{
17774664626SKris Kennaway 			if (--argc < 1) goto bad;
17874664626SKris Kennaway 			outf= *(++argv);
17974664626SKris Kennaway 			}
180f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-pass") == 0)
181f579bf8eSKris Kennaway 			{
182f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
183f579bf8eSKris Kennaway 			passarg= *(++argv);
184f579bf8eSKris Kennaway 			}
185fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1865c87c606SMark Murray 		else if (strcmp(*argv,"-engine") == 0)
1875c87c606SMark Murray 			{
1885c87c606SMark Murray 			if (--argc < 1) goto bad;
1895c87c606SMark Murray 			engine= *(++argv);
1905c87c606SMark Murray 			}
191fceca8a3SJacques Vidrine #endif
19274664626SKris Kennaway 		else if	(strcmp(*argv,"-d") == 0)
19374664626SKris Kennaway 			enc=0;
19474664626SKris Kennaway 		else if	(strcmp(*argv,"-p") == 0)
19574664626SKris Kennaway 			printkey=1;
19674664626SKris Kennaway 		else if	(strcmp(*argv,"-v") == 0)
19774664626SKris Kennaway 			verbose=1;
1985c87c606SMark Murray 		else if	(strcmp(*argv,"-nopad") == 0)
1995c87c606SMark Murray 			nopad=1;
200f579bf8eSKris Kennaway 		else if	(strcmp(*argv,"-salt") == 0)
201f579bf8eSKris Kennaway 			nosalt=0;
202f579bf8eSKris Kennaway 		else if	(strcmp(*argv,"-nosalt") == 0)
203f579bf8eSKris Kennaway 			nosalt=1;
204f579bf8eSKris Kennaway 		else if	(strcmp(*argv,"-debug") == 0)
20574664626SKris Kennaway 			debug=1;
20674664626SKris Kennaway 		else if	(strcmp(*argv,"-P") == 0)
20774664626SKris Kennaway 			printkey=2;
20874664626SKris Kennaway 		else if	(strcmp(*argv,"-A") == 0)
20974664626SKris Kennaway 			olb64=1;
21074664626SKris Kennaway 		else if	(strcmp(*argv,"-a") == 0)
21174664626SKris Kennaway 			base64=1;
21274664626SKris Kennaway 		else if	(strcmp(*argv,"-base64") == 0)
21374664626SKris Kennaway 			base64=1;
2141f13597dSJung-uk Kim #ifdef ZLIB
2151f13597dSJung-uk Kim 		else if	(strcmp(*argv,"-z") == 0)
2161f13597dSJung-uk Kim 			do_zlib=1;
2171f13597dSJung-uk Kim #endif
21874664626SKris Kennaway 		else if (strcmp(*argv,"-bufsize") == 0)
21974664626SKris Kennaway 			{
22074664626SKris Kennaway 			if (--argc < 1) goto bad;
22174664626SKris Kennaway 			bufsize=(unsigned char *)*(++argv);
22274664626SKris Kennaway 			}
22374664626SKris Kennaway 		else if (strcmp(*argv,"-k") == 0)
22474664626SKris Kennaway 			{
22574664626SKris Kennaway 			if (--argc < 1) goto bad;
22674664626SKris Kennaway 			str= *(++argv);
22774664626SKris Kennaway 			}
22874664626SKris Kennaway 		else if (strcmp(*argv,"-kfile") == 0)
22974664626SKris Kennaway 			{
23074664626SKris Kennaway 			static char buf[128];
23174664626SKris Kennaway 			FILE *infile;
23274664626SKris Kennaway 			char *file;
23374664626SKris Kennaway 
23474664626SKris Kennaway 			if (--argc < 1) goto bad;
23574664626SKris Kennaway 			file= *(++argv);
23674664626SKris Kennaway 			infile=fopen(file,"r");
23774664626SKris Kennaway 			if (infile == NULL)
23874664626SKris Kennaway 				{
23974664626SKris Kennaway 				BIO_printf(bio_err,"unable to read key from '%s'\n",
24074664626SKris Kennaway 					file);
24174664626SKris Kennaway 				goto bad;
24274664626SKris Kennaway 				}
24374664626SKris Kennaway 			buf[0]='\0';
2446a599222SSimon L. B. Nielsen 			if (!fgets(buf,sizeof buf,infile))
2456a599222SSimon L. B. Nielsen 				{
2466a599222SSimon L. B. Nielsen 				BIO_printf(bio_err,"unable to read key from '%s'\n",
2476a599222SSimon L. B. Nielsen 					file);
2486a599222SSimon L. B. Nielsen 				goto bad;
2496a599222SSimon L. B. Nielsen 				}
25074664626SKris Kennaway 			fclose(infile);
25174664626SKris Kennaway 			i=strlen(buf);
25274664626SKris Kennaway 			if ((i > 0) &&
25374664626SKris Kennaway 				((buf[i-1] == '\n') || (buf[i-1] == '\r')))
25474664626SKris Kennaway 				buf[--i]='\0';
25574664626SKris Kennaway 			if ((i > 0) &&
25674664626SKris Kennaway 				((buf[i-1] == '\n') || (buf[i-1] == '\r')))
25774664626SKris Kennaway 				buf[--i]='\0';
25874664626SKris Kennaway 			if (i < 1)
25974664626SKris Kennaway 				{
26074664626SKris Kennaway 				BIO_printf(bio_err,"zero length password\n");
26174664626SKris Kennaway 				goto bad;
26274664626SKris Kennaway 				}
26374664626SKris Kennaway 			str=buf;
26474664626SKris Kennaway 			}
26574664626SKris Kennaway 		else if (strcmp(*argv,"-K") == 0)
26674664626SKris Kennaway 			{
26774664626SKris Kennaway 			if (--argc < 1) goto bad;
26874664626SKris Kennaway 			hkey= *(++argv);
26974664626SKris Kennaway 			}
270f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-S") == 0)
271f579bf8eSKris Kennaway 			{
272f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
273f579bf8eSKris Kennaway 			hsalt= *(++argv);
274f579bf8eSKris Kennaway 			}
27574664626SKris Kennaway 		else if (strcmp(*argv,"-iv") == 0)
27674664626SKris Kennaway 			{
27774664626SKris Kennaway 			if (--argc < 1) goto bad;
27874664626SKris Kennaway 			hiv= *(++argv);
27974664626SKris Kennaway 			}
2806be8ae07SJacques Vidrine 		else if (strcmp(*argv,"-md") == 0)
2816be8ae07SJacques Vidrine 			{
2826be8ae07SJacques Vidrine 			if (--argc < 1) goto bad;
2836be8ae07SJacques Vidrine 			md= *(++argv);
2846be8ae07SJacques Vidrine 			}
285db522d3aSSimon L. B. Nielsen 		else if (strcmp(*argv,"-non-fips-allow") == 0)
286db522d3aSSimon L. B. Nielsen 			non_fips_allow = 1;
28774664626SKris Kennaway 		else if	((argv[0][0] == '-') &&
28874664626SKris Kennaway 			((c=EVP_get_cipherbyname(&(argv[0][1]))) != NULL))
28974664626SKris Kennaway 			{
29074664626SKris Kennaway 			cipher=c;
29174664626SKris Kennaway 			}
29274664626SKris Kennaway 		else if (strcmp(*argv,"-none") == 0)
29374664626SKris Kennaway 			cipher=NULL;
29474664626SKris Kennaway 		else
29574664626SKris Kennaway 			{
29674664626SKris Kennaway 			BIO_printf(bio_err,"unknown option '%s'\n",*argv);
29774664626SKris Kennaway bad:
29874664626SKris Kennaway 			BIO_printf(bio_err,"options are\n");
29974664626SKris Kennaway 			BIO_printf(bio_err,"%-14s input file\n","-in <file>");
300f579bf8eSKris Kennaway 			BIO_printf(bio_err,"%-14s output file\n","-out <file>");
301f579bf8eSKris Kennaway 			BIO_printf(bio_err,"%-14s pass phrase source\n","-pass <arg>");
30274664626SKris Kennaway 			BIO_printf(bio_err,"%-14s encrypt\n","-e");
30374664626SKris Kennaway 			BIO_printf(bio_err,"%-14s decrypt\n","-d");
30474664626SKris Kennaway 			BIO_printf(bio_err,"%-14s base64 encode/decode, depending on encryption flag\n","-a/-base64");
3056be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s passphrase is the next argument\n","-k");
3066be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s passphrase is the first line of the file argument\n","-kfile");
3076be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s the next argument is the md to use to create a key\n","-md");
3086be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s   from a passphrase.  One of md2, md5, sha or sha1\n","");
3091f13597dSJung-uk Kim 			BIO_printf(bio_err,"%-14s salt in hex is the next argument\n","-S");
31074664626SKris Kennaway 			BIO_printf(bio_err,"%-14s key/iv in hex is the next argument\n","-K/-iv");
31174664626SKris Kennaway 			BIO_printf(bio_err,"%-14s print the iv/key (then exit if -P)\n","-[pP]");
31274664626SKris Kennaway 			BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>");
3131f13597dSJung-uk Kim 			BIO_printf(bio_err,"%-14s disable standard block padding\n","-nopad");
314fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
3155c87c606SMark Murray 			BIO_printf(bio_err,"%-14s use engine e, possibly a hardware device.\n","-engine e");
316fceca8a3SJacques Vidrine #endif
31774664626SKris Kennaway 
31874664626SKris Kennaway 			BIO_printf(bio_err,"Cipher Types\n");
3195c87c606SMark Murray 			OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
3205c87c606SMark Murray 					       show_ciphers,
3215c87c606SMark Murray 					       bio_err);
3225c87c606SMark Murray 			BIO_printf(bio_err,"\n");
32374664626SKris Kennaway 
32474664626SKris Kennaway 			goto end;
32574664626SKris Kennaway 			}
32674664626SKris Kennaway 		argc--;
32774664626SKris Kennaway 		argv++;
32874664626SKris Kennaway 		}
32974664626SKris Kennaway 
330fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
331a3ddd25aSSimon L. B. Nielsen         setup_engine(bio_err, engine, 0);
332fceca8a3SJacques Vidrine #endif
3335c87c606SMark Murray 
334*94ad176cSJung-uk Kim 	if (cipher && EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
335*94ad176cSJung-uk Kim 		{
336*94ad176cSJung-uk Kim 		BIO_printf(bio_err, "AEAD ciphers not supported by the enc utility\n");
337*94ad176cSJung-uk Kim 		goto end;
338*94ad176cSJung-uk Kim 		}
339*94ad176cSJung-uk Kim 
3406be8ae07SJacques Vidrine 	if (md && (dgst=EVP_get_digestbyname(md)) == NULL)
3416be8ae07SJacques Vidrine 		{
3426be8ae07SJacques Vidrine 		BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
3436be8ae07SJacques Vidrine 		goto end;
3446be8ae07SJacques Vidrine 		}
3456be8ae07SJacques Vidrine 
3466be8ae07SJacques Vidrine 	if (dgst == NULL)
3476be8ae07SJacques Vidrine 		{
3486be8ae07SJacques Vidrine 		dgst = EVP_md5();
3496be8ae07SJacques Vidrine 		}
3506be8ae07SJacques Vidrine 
35174664626SKris Kennaway 	if (bufsize != NULL)
35274664626SKris Kennaway 		{
35374664626SKris Kennaway 		unsigned long n;
35474664626SKris Kennaway 
35574664626SKris Kennaway 		for (n=0; *bufsize; bufsize++)
35674664626SKris Kennaway 			{
35774664626SKris Kennaway 			i= *bufsize;
35874664626SKris Kennaway 			if ((i <= '9') && (i >= '0'))
35974664626SKris Kennaway 				n=n*10+i-'0';
36074664626SKris Kennaway 			else if (i == 'k')
36174664626SKris Kennaway 				{
36274664626SKris Kennaway 				n*=1024;
36374664626SKris Kennaway 				bufsize++;
36474664626SKris Kennaway 				break;
36574664626SKris Kennaway 				}
36674664626SKris Kennaway 			}
36774664626SKris Kennaway 		if (*bufsize != '\0')
36874664626SKris Kennaway 			{
36974664626SKris Kennaway 			BIO_printf(bio_err,"invalid 'bufsize' specified.\n");
37074664626SKris Kennaway 			goto end;
37174664626SKris Kennaway 			}
37274664626SKris Kennaway 
37374664626SKris Kennaway 		/* It must be large enough for a base64 encoded line */
374ed5d4f9aSSimon L. B. Nielsen 		if (base64 && n < 80) n=80;
37574664626SKris Kennaway 
37674664626SKris Kennaway 		bsize=(int)n;
37774664626SKris Kennaway 		if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize);
37874664626SKris Kennaway 		}
37974664626SKris Kennaway 
380ddd58736SKris Kennaway 	strbuf=OPENSSL_malloc(SIZE);
381ddd58736SKris Kennaway 	buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
38274664626SKris Kennaway 	if ((buff == NULL) || (strbuf == NULL))
38374664626SKris Kennaway 		{
384ddd58736SKris Kennaway 		BIO_printf(bio_err,"OPENSSL_malloc failure %ld\n",(long)EVP_ENCODE_LENGTH(bsize));
38574664626SKris Kennaway 		goto end;
38674664626SKris Kennaway 		}
38774664626SKris Kennaway 
38874664626SKris Kennaway 	in=BIO_new(BIO_s_file());
38974664626SKris Kennaway 	out=BIO_new(BIO_s_file());
39074664626SKris Kennaway 	if ((in == NULL) || (out == NULL))
39174664626SKris Kennaway 		{
39274664626SKris Kennaway 		ERR_print_errors(bio_err);
39374664626SKris Kennaway 		goto end;
39474664626SKris Kennaway 		}
39574664626SKris Kennaway 	if (debug)
39674664626SKris Kennaway 		{
39774664626SKris Kennaway 		BIO_set_callback(in,BIO_debug_callback);
39874664626SKris Kennaway 		BIO_set_callback(out,BIO_debug_callback);
3995471f83eSSimon L. B. Nielsen 		BIO_set_callback_arg(in,(char *)bio_err);
4005471f83eSSimon L. B. Nielsen 		BIO_set_callback_arg(out,(char *)bio_err);
40174664626SKris Kennaway 		}
40274664626SKris Kennaway 
40374664626SKris Kennaway 	if (inf == NULL)
404ed5d4f9aSSimon L. B. Nielsen 	        {
4051f13597dSJung-uk Kim #ifndef OPENSSL_NO_SETVBUF_IONBF
406ed5d4f9aSSimon L. B. Nielsen 		if (bufsize != NULL)
407ed5d4f9aSSimon L. B. Nielsen 			setvbuf(stdin, (char *)NULL, _IONBF, 0);
4081f13597dSJung-uk Kim #endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
40974664626SKris Kennaway 		BIO_set_fp(in,stdin,BIO_NOCLOSE);
410ed5d4f9aSSimon L. B. Nielsen 	        }
41174664626SKris Kennaway 	else
41274664626SKris Kennaway 		{
41374664626SKris Kennaway 		if (BIO_read_filename(in,inf) <= 0)
41474664626SKris Kennaway 			{
41574664626SKris Kennaway 			perror(inf);
41674664626SKris Kennaway 			goto end;
41774664626SKris Kennaway 			}
41874664626SKris Kennaway 		}
41974664626SKris Kennaway 
420f579bf8eSKris Kennaway 	if(!str && passarg) {
421f579bf8eSKris Kennaway 		if(!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
422f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error getting password\n");
423f579bf8eSKris Kennaway 			goto end;
424f579bf8eSKris Kennaway 		}
425f579bf8eSKris Kennaway 		str = pass;
426f579bf8eSKris Kennaway 	}
427f579bf8eSKris Kennaway 
42874664626SKris Kennaway 	if ((str == NULL) && (cipher != NULL) && (hkey == NULL))
42974664626SKris Kennaway 		{
43074664626SKris Kennaway 		for (;;)
43174664626SKris Kennaway 			{
43274664626SKris Kennaway 			char buf[200];
43374664626SKris Kennaway 
434ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"enter %s %s password:",
43574664626SKris Kennaway 				     OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
43674664626SKris Kennaway 				     (enc)?"encryption":"decryption");
43774664626SKris Kennaway 			strbuf[0]='\0';
43874664626SKris Kennaway 			i=EVP_read_pw_string((char *)strbuf,SIZE,buf,enc);
43974664626SKris Kennaway 			if (i == 0)
44074664626SKris Kennaway 				{
44174664626SKris Kennaway 				if (strbuf[0] == '\0')
44274664626SKris Kennaway 					{
44374664626SKris Kennaway 					ret=1;
44474664626SKris Kennaway 					goto end;
44574664626SKris Kennaway 					}
44674664626SKris Kennaway 				str=strbuf;
44774664626SKris Kennaway 				break;
44874664626SKris Kennaway 				}
44974664626SKris Kennaway 			if (i < 0)
45074664626SKris Kennaway 				{
45174664626SKris Kennaway 				BIO_printf(bio_err,"bad password read\n");
45274664626SKris Kennaway 				goto end;
45374664626SKris Kennaway 				}
45474664626SKris Kennaway 			}
45574664626SKris Kennaway 		}
45674664626SKris Kennaway 
45774664626SKris Kennaway 
45874664626SKris Kennaway 	if (outf == NULL)
459ddd58736SKris Kennaway 		{
46074664626SKris Kennaway 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
4611f13597dSJung-uk Kim #ifndef OPENSSL_NO_SETVBUF_IONBF
462ed5d4f9aSSimon L. B. Nielsen 		if (bufsize != NULL)
463ed5d4f9aSSimon L. B. Nielsen 			setvbuf(stdout, (char *)NULL, _IONBF, 0);
4641f13597dSJung-uk Kim #endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
4655c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
466ddd58736SKris Kennaway 		{
467ddd58736SKris Kennaway 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
468ddd58736SKris Kennaway 		out = BIO_push(tmpbio, out);
469ddd58736SKris Kennaway 		}
470ddd58736SKris Kennaway #endif
471ddd58736SKris Kennaway 		}
47274664626SKris Kennaway 	else
47374664626SKris Kennaway 		{
47474664626SKris Kennaway 		if (BIO_write_filename(out,outf) <= 0)
47574664626SKris Kennaway 			{
47674664626SKris Kennaway 			perror(outf);
47774664626SKris Kennaway 			goto end;
47874664626SKris Kennaway 			}
47974664626SKris Kennaway 		}
48074664626SKris Kennaway 
48174664626SKris Kennaway 	rbio=in;
48274664626SKris Kennaway 	wbio=out;
48374664626SKris Kennaway 
4841f13597dSJung-uk Kim #ifdef ZLIB
4851f13597dSJung-uk Kim 
4861f13597dSJung-uk Kim 	if (do_zlib)
4871f13597dSJung-uk Kim 		{
4881f13597dSJung-uk Kim 		if ((bzl=BIO_new(BIO_f_zlib())) == NULL)
4891f13597dSJung-uk Kim 			goto end;
4901f13597dSJung-uk Kim 		if (enc)
4911f13597dSJung-uk Kim 			wbio=BIO_push(bzl,wbio);
4921f13597dSJung-uk Kim 		else
4931f13597dSJung-uk Kim 			rbio=BIO_push(bzl,rbio);
4941f13597dSJung-uk Kim 		}
4951f13597dSJung-uk Kim #endif
4961f13597dSJung-uk Kim 
49774664626SKris Kennaway 	if (base64)
49874664626SKris Kennaway 		{
49974664626SKris Kennaway 		if ((b64=BIO_new(BIO_f_base64())) == NULL)
50074664626SKris Kennaway 			goto end;
50174664626SKris Kennaway 		if (debug)
50274664626SKris Kennaway 			{
50374664626SKris Kennaway 			BIO_set_callback(b64,BIO_debug_callback);
5045471f83eSSimon L. B. Nielsen 			BIO_set_callback_arg(b64,(char *)bio_err);
50574664626SKris Kennaway 			}
50674664626SKris Kennaway 		if (olb64)
50774664626SKris Kennaway 			BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
50874664626SKris Kennaway 		if (enc)
50974664626SKris Kennaway 			wbio=BIO_push(b64,wbio);
51074664626SKris Kennaway 		else
51174664626SKris Kennaway 			rbio=BIO_push(b64,rbio);
51274664626SKris Kennaway 		}
51374664626SKris Kennaway 
514f579bf8eSKris Kennaway 	if (cipher != NULL)
515f579bf8eSKris Kennaway 		{
5165c87c606SMark Murray 		/* Note that str is NULL if a key was passed on the command
5175c87c606SMark Murray 		 * line, so we get no salt in that case. Is this a bug?
5185c87c606SMark Murray 		 */
519f579bf8eSKris Kennaway 		if (str != NULL)
520f579bf8eSKris Kennaway 			{
521f579bf8eSKris Kennaway 			/* Salt handling: if encrypting generate a salt and
522f579bf8eSKris Kennaway 			 * write to output BIO. If decrypting read salt from
523f579bf8eSKris Kennaway 			 * input BIO.
524f579bf8eSKris Kennaway 			 */
525f579bf8eSKris Kennaway 			unsigned char *sptr;
526f579bf8eSKris Kennaway 			if(nosalt) sptr = NULL;
527f579bf8eSKris Kennaway 			else {
528f579bf8eSKris Kennaway 				if(enc) {
529f579bf8eSKris Kennaway 					if(hsalt) {
5305c87c606SMark Murray 						if(!set_hex(hsalt,salt,sizeof salt)) {
531f579bf8eSKris Kennaway 							BIO_printf(bio_err,
532f579bf8eSKris Kennaway 								"invalid hex salt value\n");
533f579bf8eSKris Kennaway 							goto end;
534f579bf8eSKris Kennaway 						}
5355c87c606SMark Murray 					} else if (RAND_pseudo_bytes(salt, sizeof salt) < 0)
536f579bf8eSKris Kennaway 						goto end;
537f579bf8eSKris Kennaway 					/* If -P option then don't bother writing */
538f579bf8eSKris Kennaway 					if((printkey != 2)
539f579bf8eSKris Kennaway 					   && (BIO_write(wbio,magic,
540f579bf8eSKris Kennaway 							 sizeof magic-1) != sizeof magic-1
541f579bf8eSKris Kennaway 					       || BIO_write(wbio,
542f579bf8eSKris Kennaway 							    (char *)salt,
5435c87c606SMark Murray 							    sizeof salt) != sizeof salt)) {
544f579bf8eSKris Kennaway 						BIO_printf(bio_err,"error writing output file\n");
545f579bf8eSKris Kennaway 						goto end;
546f579bf8eSKris Kennaway 					}
547f579bf8eSKris Kennaway 				} else if(BIO_read(rbio,mbuf,sizeof mbuf) != sizeof mbuf
548f579bf8eSKris Kennaway 					  || BIO_read(rbio,
549f579bf8eSKris Kennaway 						      (unsigned char *)salt,
5505c87c606SMark Murray 				    sizeof salt) != sizeof salt) {
551f579bf8eSKris Kennaway 					BIO_printf(bio_err,"error reading input file\n");
552f579bf8eSKris Kennaway 					goto end;
553f579bf8eSKris Kennaway 				} else if(memcmp(mbuf,magic,sizeof magic-1)) {
554f579bf8eSKris Kennaway 				    BIO_printf(bio_err,"bad magic number\n");
555f579bf8eSKris Kennaway 				    goto end;
556f579bf8eSKris Kennaway 				}
557f579bf8eSKris Kennaway 
558f579bf8eSKris Kennaway 				sptr = salt;
559f579bf8eSKris Kennaway 			}
560f579bf8eSKris Kennaway 
5616be8ae07SJacques Vidrine 			EVP_BytesToKey(cipher,dgst,sptr,
562f579bf8eSKris Kennaway 				(unsigned char *)str,
563f579bf8eSKris Kennaway 				strlen(str),1,key,iv);
564f579bf8eSKris Kennaway 			/* zero the complete buffer or the string
565f579bf8eSKris Kennaway 			 * passed from the command line
566f579bf8eSKris Kennaway 			 * bug picked up by
567f579bf8eSKris Kennaway 			 * Larry J. Hughes Jr. <hughes@indiana.edu> */
568f579bf8eSKris Kennaway 			if (str == strbuf)
5695c87c606SMark Murray 				OPENSSL_cleanse(str,SIZE);
570f579bf8eSKris Kennaway 			else
5715c87c606SMark Murray 				OPENSSL_cleanse(str,strlen(str));
572f579bf8eSKris Kennaway 			}
5735c87c606SMark Murray 		if ((hiv != NULL) && !set_hex(hiv,iv,sizeof iv))
574f579bf8eSKris Kennaway 			{
575f579bf8eSKris Kennaway 			BIO_printf(bio_err,"invalid hex iv value\n");
576f579bf8eSKris Kennaway 			goto end;
577f579bf8eSKris Kennaway 			}
578db522d3aSSimon L. B. Nielsen 		if ((hiv == NULL) && (str == NULL)
579db522d3aSSimon L. B. Nielsen 		    && EVP_CIPHER_iv_length(cipher) != 0)
58026d191b4SKris Kennaway 			{
58126d191b4SKris Kennaway 			/* No IV was explicitly set and no IV was generated
58226d191b4SKris Kennaway 			 * during EVP_BytesToKey. Hence the IV is undefined,
58326d191b4SKris Kennaway 			 * making correct decryption impossible. */
58426d191b4SKris Kennaway 			BIO_printf(bio_err, "iv undefined\n");
58526d191b4SKris Kennaway 			goto end;
58626d191b4SKris Kennaway 			}
5875c87c606SMark Murray 		if ((hkey != NULL) && !set_hex(hkey,key,sizeof key))
588f579bf8eSKris Kennaway 			{
589f579bf8eSKris Kennaway 			BIO_printf(bio_err,"invalid hex key value\n");
590f579bf8eSKris Kennaway 			goto end;
591f579bf8eSKris Kennaway 			}
592f579bf8eSKris Kennaway 
593f579bf8eSKris Kennaway 		if ((benc=BIO_new(BIO_f_cipher())) == NULL)
594f579bf8eSKris Kennaway 			goto end;
5953b4e3dcbSSimon L. B. Nielsen 
5963b4e3dcbSSimon L. B. Nielsen 		/* Since we may be changing parameters work on the encryption
5973b4e3dcbSSimon L. B. Nielsen 		 * context rather than calling BIO_set_cipher().
5983b4e3dcbSSimon L. B. Nielsen 		 */
5993b4e3dcbSSimon L. B. Nielsen 
6005c87c606SMark Murray 		BIO_get_cipher_ctx(benc, &ctx);
601db522d3aSSimon L. B. Nielsen 
602db522d3aSSimon L. B. Nielsen 		if (non_fips_allow)
603db522d3aSSimon L. B. Nielsen 			EVP_CIPHER_CTX_set_flags(ctx,
604db522d3aSSimon L. B. Nielsen 				EVP_CIPH_FLAG_NON_FIPS_ALLOW);
605db522d3aSSimon L. B. Nielsen 
6063b4e3dcbSSimon L. B. Nielsen 		if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc))
6073b4e3dcbSSimon L. B. Nielsen 			{
6083b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err, "Error setting cipher %s\n",
6093b4e3dcbSSimon L. B. Nielsen 				EVP_CIPHER_name(cipher));
6103b4e3dcbSSimon L. B. Nielsen 			ERR_print_errors(bio_err);
6113b4e3dcbSSimon L. B. Nielsen 			goto end;
6125c87c606SMark Murray 			}
6133b4e3dcbSSimon L. B. Nielsen 
6143b4e3dcbSSimon L. B. Nielsen 		if (nopad)
6153b4e3dcbSSimon L. B. Nielsen 			EVP_CIPHER_CTX_set_padding(ctx, 0);
6163b4e3dcbSSimon L. B. Nielsen 
6173b4e3dcbSSimon L. B. Nielsen 		if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
6183b4e3dcbSSimon L. B. Nielsen 			{
6193b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err, "Error setting cipher %s\n",
6203b4e3dcbSSimon L. B. Nielsen 				EVP_CIPHER_name(cipher));
6213b4e3dcbSSimon L. B. Nielsen 			ERR_print_errors(bio_err);
6223b4e3dcbSSimon L. B. Nielsen 			goto end;
6233b4e3dcbSSimon L. B. Nielsen 			}
6243b4e3dcbSSimon L. B. Nielsen 
625f579bf8eSKris Kennaway 		if (debug)
626f579bf8eSKris Kennaway 			{
627f579bf8eSKris Kennaway 			BIO_set_callback(benc,BIO_debug_callback);
6285471f83eSSimon L. B. Nielsen 			BIO_set_callback_arg(benc,(char *)bio_err);
629f579bf8eSKris Kennaway 			}
630f579bf8eSKris Kennaway 
631f579bf8eSKris Kennaway 		if (printkey)
632f579bf8eSKris Kennaway 			{
633f579bf8eSKris Kennaway 			if (!nosalt)
634f579bf8eSKris Kennaway 				{
635f579bf8eSKris Kennaway 				printf("salt=");
6363b4e3dcbSSimon L. B. Nielsen 				for (i=0; i<(int)sizeof(salt); i++)
637f579bf8eSKris Kennaway 					printf("%02X",salt[i]);
638f579bf8eSKris Kennaway 				printf("\n");
639f579bf8eSKris Kennaway 				}
640f579bf8eSKris Kennaway 			if (cipher->key_len > 0)
641f579bf8eSKris Kennaway 				{
642f579bf8eSKris Kennaway 				printf("key=");
643f579bf8eSKris Kennaway 				for (i=0; i<cipher->key_len; i++)
644f579bf8eSKris Kennaway 					printf("%02X",key[i]);
645f579bf8eSKris Kennaway 				printf("\n");
646f579bf8eSKris Kennaway 				}
647f579bf8eSKris Kennaway 			if (cipher->iv_len > 0)
648f579bf8eSKris Kennaway 				{
649f579bf8eSKris Kennaway 				printf("iv =");
650f579bf8eSKris Kennaway 				for (i=0; i<cipher->iv_len; i++)
651f579bf8eSKris Kennaway 					printf("%02X",iv[i]);
652f579bf8eSKris Kennaway 				printf("\n");
653f579bf8eSKris Kennaway 				}
654f579bf8eSKris Kennaway 			if (printkey == 2)
655f579bf8eSKris Kennaway 				{
656f579bf8eSKris Kennaway 				ret=0;
657f579bf8eSKris Kennaway 				goto end;
658f579bf8eSKris Kennaway 				}
659f579bf8eSKris Kennaway 			}
660f579bf8eSKris Kennaway 		}
661f579bf8eSKris Kennaway 
66274664626SKris Kennaway 	/* Only encrypt/decrypt as we write the file */
66374664626SKris Kennaway 	if (benc != NULL)
66474664626SKris Kennaway 		wbio=BIO_push(benc,wbio);
66574664626SKris Kennaway 
66674664626SKris Kennaway 	for (;;)
66774664626SKris Kennaway 		{
66874664626SKris Kennaway 		inl=BIO_read(rbio,(char *)buff,bsize);
66974664626SKris Kennaway 		if (inl <= 0) break;
67074664626SKris Kennaway 		if (BIO_write(wbio,(char *)buff,inl) != inl)
67174664626SKris Kennaway 			{
67274664626SKris Kennaway 			BIO_printf(bio_err,"error writing output file\n");
67374664626SKris Kennaway 			goto end;
67474664626SKris Kennaway 			}
67574664626SKris Kennaway 		}
67674664626SKris Kennaway 	if (!BIO_flush(wbio))
67774664626SKris Kennaway 		{
67874664626SKris Kennaway 		BIO_printf(bio_err,"bad decrypt\n");
67974664626SKris Kennaway 		goto end;
68074664626SKris Kennaway 		}
68174664626SKris Kennaway 
68274664626SKris Kennaway 	ret=0;
68374664626SKris Kennaway 	if (verbose)
68474664626SKris Kennaway 		{
68574664626SKris Kennaway 		BIO_printf(bio_err,"bytes read   :%8ld\n",BIO_number_read(in));
68674664626SKris Kennaway 		BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out));
68774664626SKris Kennaway 		}
68874664626SKris Kennaway end:
689f579bf8eSKris Kennaway 	ERR_print_errors(bio_err);
690ddd58736SKris Kennaway 	if (strbuf != NULL) OPENSSL_free(strbuf);
691ddd58736SKris Kennaway 	if (buff != NULL) OPENSSL_free(buff);
69274664626SKris Kennaway 	if (in != NULL) BIO_free(in);
693ddd58736SKris Kennaway 	if (out != NULL) BIO_free_all(out);
69474664626SKris Kennaway 	if (benc != NULL) BIO_free(benc);
69574664626SKris Kennaway 	if (b64 != NULL) BIO_free(b64);
6961f13597dSJung-uk Kim #ifdef ZLIB
6971f13597dSJung-uk Kim 	if (bzl != NULL) BIO_free(bzl);
6981f13597dSJung-uk Kim #endif
699ddd58736SKris Kennaway 	if(pass) OPENSSL_free(pass);
7005c87c606SMark Murray 	apps_shutdown();
7015c87c606SMark Murray 	OPENSSL_EXIT(ret);
70274664626SKris Kennaway 	}
70374664626SKris Kennaway 
70474664626SKris Kennaway int set_hex(char *in, unsigned char *out, int size)
70574664626SKris Kennaway 	{
70674664626SKris Kennaway 	int i,n;
70774664626SKris Kennaway 	unsigned char j;
70874664626SKris Kennaway 
70974664626SKris Kennaway 	n=strlen(in);
71074664626SKris Kennaway 	if (n > (size*2))
71174664626SKris Kennaway 		{
71274664626SKris Kennaway 		BIO_printf(bio_err,"hex string is too long\n");
71374664626SKris Kennaway 		return(0);
71474664626SKris Kennaway 		}
71574664626SKris Kennaway 	memset(out,0,size);
71674664626SKris Kennaway 	for (i=0; i<n; i++)
71774664626SKris Kennaway 		{
71874664626SKris Kennaway 		j=(unsigned char)*in;
71974664626SKris Kennaway 		*(in++)='\0';
72074664626SKris Kennaway 		if (j == 0) break;
72174664626SKris Kennaway 		if ((j >= '0') && (j <= '9'))
72274664626SKris Kennaway 			j-='0';
72374664626SKris Kennaway 		else if ((j >= 'A') && (j <= 'F'))
72474664626SKris Kennaway 			j=j-'A'+10;
72574664626SKris Kennaway 		else if ((j >= 'a') && (j <= 'f'))
72674664626SKris Kennaway 			j=j-'a'+10;
72774664626SKris Kennaway 		else
72874664626SKris Kennaway 			{
72974664626SKris Kennaway 			BIO_printf(bio_err,"non-hex digit\n");
73074664626SKris Kennaway 			return(0);
73174664626SKris Kennaway 			}
73274664626SKris Kennaway 		if (i&1)
73374664626SKris Kennaway 			out[i/2]|=j;
73474664626SKris Kennaway 		else
73574664626SKris Kennaway 			out[i/2]=(j<<4);
73674664626SKris Kennaway 		}
73774664626SKris Kennaway 	return(1);
73874664626SKris Kennaway 	}
739