xref: /freebsd/crypto/openssl/apps/enc.c (revision a93cbc2be851ef88019a97cf7a22ba24e3a88fe1)
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>
70*a93cbc2bSJung-uk Kim #ifndef OPENSSL_NO_COMP
711f13597dSJung-uk Kim #include <openssl/comp.h>
72*a93cbc2bSJung-uk Kim #endif
735c87c606SMark Murray #include <ctype.h>
7474664626SKris Kennaway 
7574664626SKris Kennaway int set_hex(char *in,unsigned char *out,int size);
7674664626SKris Kennaway #undef SIZE
7774664626SKris Kennaway #undef BSIZE
7874664626SKris Kennaway #undef PROG
7974664626SKris Kennaway 
8074664626SKris Kennaway #define SIZE	(512)
8174664626SKris Kennaway #define BSIZE	(8*1024)
8274664626SKris Kennaway #define	PROG	enc_main
8374664626SKris Kennaway 
845c87c606SMark Murray static void show_ciphers(const OBJ_NAME *name,void *bio_)
855c87c606SMark Murray 	{
865c87c606SMark Murray 	BIO *bio=bio_;
875c87c606SMark Murray 	static int n;
885c87c606SMark Murray 
895c87c606SMark Murray 	if(!islower((unsigned char)*name->name))
905c87c606SMark Murray 		return;
915c87c606SMark Murray 
925c87c606SMark Murray 	BIO_printf(bio,"-%-25s",name->name);
935c87c606SMark Murray 	if(++n == 3)
945c87c606SMark Murray 		{
955c87c606SMark Murray 		BIO_printf(bio,"\n");
965c87c606SMark Murray 		n=0;
975c87c606SMark Murray 		}
985c87c606SMark Murray 	else
995c87c606SMark Murray 		BIO_printf(bio," ");
1005c87c606SMark Murray 	}
1015c87c606SMark Murray 
102f579bf8eSKris Kennaway int MAIN(int, char **);
103f579bf8eSKris Kennaway 
10474664626SKris Kennaway int MAIN(int argc, char **argv)
10574664626SKris Kennaway 	{
106f579bf8eSKris Kennaway 	static const char magic[]="Salted__";
1075c87c606SMark Murray 	char mbuf[sizeof magic-1];
10874664626SKris Kennaway 	char *strbuf=NULL;
10974664626SKris Kennaway 	unsigned char *buff=NULL,*bufsize=NULL;
11074664626SKris Kennaway 	int bsize=BSIZE,verbose=0;
11174664626SKris Kennaway 	int ret=1,inl;
1125c87c606SMark Murray 	int nopad = 0;
1135c87c606SMark Murray 	unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
114f579bf8eSKris Kennaway 	unsigned char salt[PKCS5_SALT_LEN];
115f579bf8eSKris Kennaway 	char *str=NULL, *passarg = NULL, *pass = NULL;
116f579bf8eSKris Kennaway 	char *hkey=NULL,*hiv=NULL,*hsalt = NULL;
1176be8ae07SJacques Vidrine 	char *md=NULL;
11874664626SKris Kennaway 	int enc=1,printkey=0,i,base64=0;
1191f13597dSJung-uk Kim #ifdef ZLIB
1201f13597dSJung-uk Kim 	int do_zlib=0;
1211f13597dSJung-uk Kim 	BIO *bzl = NULL;
1221f13597dSJung-uk Kim #endif
123f579bf8eSKris Kennaway 	int debug=0,olb64=0,nosalt=0;
12474664626SKris Kennaway 	const EVP_CIPHER *cipher=NULL,*c;
1253b4e3dcbSSimon L. B. Nielsen 	EVP_CIPHER_CTX *ctx = NULL;
12674664626SKris Kennaway 	char *inf=NULL,*outf=NULL;
12774664626SKris Kennaway 	BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL;
128c1803d78SJacques Vidrine #define PROG_NAME_SIZE  39
129c1803d78SJacques Vidrine 	char pname[PROG_NAME_SIZE+1];
130fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1315c87c606SMark Murray 	char *engine = NULL;
132fceca8a3SJacques Vidrine #endif
1336be8ae07SJacques Vidrine 	const EVP_MD *dgst=NULL;
134db522d3aSSimon L. B. Nielsen 	int non_fips_allow = 0;
13574664626SKris Kennaway 
13674664626SKris Kennaway 	apps_startup();
13774664626SKris Kennaway 
13874664626SKris Kennaway 	if (bio_err == NULL)
13974664626SKris Kennaway 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
14074664626SKris Kennaway 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
14174664626SKris Kennaway 
1425c87c606SMark Murray 	if (!load_config(bio_err, NULL))
1435c87c606SMark Murray 		goto end;
1445c87c606SMark Murray 
14574664626SKris Kennaway 	/* first check the program name */
1465c87c606SMark Murray 	program_name(argv[0],pname,sizeof pname);
14774664626SKris Kennaway 	if (strcmp(pname,"base64") == 0)
14874664626SKris Kennaway 		base64=1;
1491f13597dSJung-uk Kim #ifdef ZLIB
1501f13597dSJung-uk Kim 	if (strcmp(pname,"zlib") == 0)
1511f13597dSJung-uk Kim 		do_zlib=1;
1521f13597dSJung-uk Kim #endif
15374664626SKris Kennaway 
15474664626SKris Kennaway 	cipher=EVP_get_cipherbyname(pname);
1551f13597dSJung-uk Kim #ifdef ZLIB
1561f13597dSJung-uk Kim 	if (!do_zlib && !base64 && (cipher == NULL)
1571f13597dSJung-uk Kim 				&& (strcmp(pname,"enc") != 0))
1581f13597dSJung-uk Kim #else
15974664626SKris Kennaway 	if (!base64 && (cipher == NULL) && (strcmp(pname,"enc") != 0))
1601f13597dSJung-uk Kim #endif
16174664626SKris Kennaway 		{
16274664626SKris Kennaway 		BIO_printf(bio_err,"%s is an unknown cipher\n",pname);
16374664626SKris Kennaway 		goto bad;
16474664626SKris Kennaway 		}
16574664626SKris Kennaway 
16674664626SKris Kennaway 	argc--;
16774664626SKris Kennaway 	argv++;
16874664626SKris Kennaway 	while (argc >= 1)
16974664626SKris Kennaway 		{
17074664626SKris Kennaway 		if	(strcmp(*argv,"-e") == 0)
17174664626SKris Kennaway 			enc=1;
17274664626SKris Kennaway 		else if (strcmp(*argv,"-in") == 0)
17374664626SKris Kennaway 			{
17474664626SKris Kennaway 			if (--argc < 1) goto bad;
17574664626SKris Kennaway 			inf= *(++argv);
17674664626SKris Kennaway 			}
17774664626SKris Kennaway 		else if (strcmp(*argv,"-out") == 0)
17874664626SKris Kennaway 			{
17974664626SKris Kennaway 			if (--argc < 1) goto bad;
18074664626SKris Kennaway 			outf= *(++argv);
18174664626SKris Kennaway 			}
182f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-pass") == 0)
183f579bf8eSKris Kennaway 			{
184f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
185f579bf8eSKris Kennaway 			passarg= *(++argv);
186f579bf8eSKris Kennaway 			}
187fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1885c87c606SMark Murray 		else if (strcmp(*argv,"-engine") == 0)
1895c87c606SMark Murray 			{
1905c87c606SMark Murray 			if (--argc < 1) goto bad;
1915c87c606SMark Murray 			engine= *(++argv);
1925c87c606SMark Murray 			}
193fceca8a3SJacques Vidrine #endif
19474664626SKris Kennaway 		else if	(strcmp(*argv,"-d") == 0)
19574664626SKris Kennaway 			enc=0;
19674664626SKris Kennaway 		else if	(strcmp(*argv,"-p") == 0)
19774664626SKris Kennaway 			printkey=1;
19874664626SKris Kennaway 		else if	(strcmp(*argv,"-v") == 0)
19974664626SKris Kennaway 			verbose=1;
2005c87c606SMark Murray 		else if	(strcmp(*argv,"-nopad") == 0)
2015c87c606SMark Murray 			nopad=1;
202f579bf8eSKris Kennaway 		else if	(strcmp(*argv,"-salt") == 0)
203f579bf8eSKris Kennaway 			nosalt=0;
204f579bf8eSKris Kennaway 		else if	(strcmp(*argv,"-nosalt") == 0)
205f579bf8eSKris Kennaway 			nosalt=1;
206f579bf8eSKris Kennaway 		else if	(strcmp(*argv,"-debug") == 0)
20774664626SKris Kennaway 			debug=1;
20874664626SKris Kennaway 		else if	(strcmp(*argv,"-P") == 0)
20974664626SKris Kennaway 			printkey=2;
21074664626SKris Kennaway 		else if	(strcmp(*argv,"-A") == 0)
21174664626SKris Kennaway 			olb64=1;
21274664626SKris Kennaway 		else if	(strcmp(*argv,"-a") == 0)
21374664626SKris Kennaway 			base64=1;
21474664626SKris Kennaway 		else if	(strcmp(*argv,"-base64") == 0)
21574664626SKris Kennaway 			base64=1;
2161f13597dSJung-uk Kim #ifdef ZLIB
2171f13597dSJung-uk Kim 		else if	(strcmp(*argv,"-z") == 0)
2181f13597dSJung-uk Kim 			do_zlib=1;
2191f13597dSJung-uk Kim #endif
22074664626SKris Kennaway 		else if (strcmp(*argv,"-bufsize") == 0)
22174664626SKris Kennaway 			{
22274664626SKris Kennaway 			if (--argc < 1) goto bad;
22374664626SKris Kennaway 			bufsize=(unsigned char *)*(++argv);
22474664626SKris Kennaway 			}
22574664626SKris Kennaway 		else if (strcmp(*argv,"-k") == 0)
22674664626SKris Kennaway 			{
22774664626SKris Kennaway 			if (--argc < 1) goto bad;
22874664626SKris Kennaway 			str= *(++argv);
22974664626SKris Kennaway 			}
23074664626SKris Kennaway 		else if (strcmp(*argv,"-kfile") == 0)
23174664626SKris Kennaway 			{
23274664626SKris Kennaway 			static char buf[128];
23374664626SKris Kennaway 			FILE *infile;
23474664626SKris Kennaway 			char *file;
23574664626SKris Kennaway 
23674664626SKris Kennaway 			if (--argc < 1) goto bad;
23774664626SKris Kennaway 			file= *(++argv);
23874664626SKris Kennaway 			infile=fopen(file,"r");
23974664626SKris Kennaway 			if (infile == NULL)
24074664626SKris Kennaway 				{
24174664626SKris Kennaway 				BIO_printf(bio_err,"unable to read key from '%s'\n",
24274664626SKris Kennaway 					file);
24374664626SKris Kennaway 				goto bad;
24474664626SKris Kennaway 				}
24574664626SKris Kennaway 			buf[0]='\0';
2466a599222SSimon L. B. Nielsen 			if (!fgets(buf,sizeof buf,infile))
2476a599222SSimon L. B. Nielsen 				{
2486a599222SSimon L. B. Nielsen 				BIO_printf(bio_err,"unable to read key from '%s'\n",
2496a599222SSimon L. B. Nielsen 					file);
2506a599222SSimon L. B. Nielsen 				goto bad;
2516a599222SSimon L. B. Nielsen 				}
25274664626SKris Kennaway 			fclose(infile);
25374664626SKris Kennaway 			i=strlen(buf);
25474664626SKris Kennaway 			if ((i > 0) &&
25574664626SKris Kennaway 				((buf[i-1] == '\n') || (buf[i-1] == '\r')))
25674664626SKris Kennaway 				buf[--i]='\0';
25774664626SKris Kennaway 			if ((i > 0) &&
25874664626SKris Kennaway 				((buf[i-1] == '\n') || (buf[i-1] == '\r')))
25974664626SKris Kennaway 				buf[--i]='\0';
26074664626SKris Kennaway 			if (i < 1)
26174664626SKris Kennaway 				{
26274664626SKris Kennaway 				BIO_printf(bio_err,"zero length password\n");
26374664626SKris Kennaway 				goto bad;
26474664626SKris Kennaway 				}
26574664626SKris Kennaway 			str=buf;
26674664626SKris Kennaway 			}
26774664626SKris Kennaway 		else if (strcmp(*argv,"-K") == 0)
26874664626SKris Kennaway 			{
26974664626SKris Kennaway 			if (--argc < 1) goto bad;
27074664626SKris Kennaway 			hkey= *(++argv);
27174664626SKris Kennaway 			}
272f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-S") == 0)
273f579bf8eSKris Kennaway 			{
274f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
275f579bf8eSKris Kennaway 			hsalt= *(++argv);
276f579bf8eSKris Kennaway 			}
27774664626SKris Kennaway 		else if (strcmp(*argv,"-iv") == 0)
27874664626SKris Kennaway 			{
27974664626SKris Kennaway 			if (--argc < 1) goto bad;
28074664626SKris Kennaway 			hiv= *(++argv);
28174664626SKris Kennaway 			}
2826be8ae07SJacques Vidrine 		else if (strcmp(*argv,"-md") == 0)
2836be8ae07SJacques Vidrine 			{
2846be8ae07SJacques Vidrine 			if (--argc < 1) goto bad;
2856be8ae07SJacques Vidrine 			md= *(++argv);
2866be8ae07SJacques Vidrine 			}
287db522d3aSSimon L. B. Nielsen 		else if (strcmp(*argv,"-non-fips-allow") == 0)
288db522d3aSSimon L. B. Nielsen 			non_fips_allow = 1;
28974664626SKris Kennaway 		else if	((argv[0][0] == '-') &&
29074664626SKris Kennaway 			((c=EVP_get_cipherbyname(&(argv[0][1]))) != NULL))
29174664626SKris Kennaway 			{
29274664626SKris Kennaway 			cipher=c;
29374664626SKris Kennaway 			}
29474664626SKris Kennaway 		else if (strcmp(*argv,"-none") == 0)
29574664626SKris Kennaway 			cipher=NULL;
29674664626SKris Kennaway 		else
29774664626SKris Kennaway 			{
29874664626SKris Kennaway 			BIO_printf(bio_err,"unknown option '%s'\n",*argv);
29974664626SKris Kennaway bad:
30074664626SKris Kennaway 			BIO_printf(bio_err,"options are\n");
30174664626SKris Kennaway 			BIO_printf(bio_err,"%-14s input file\n","-in <file>");
302f579bf8eSKris Kennaway 			BIO_printf(bio_err,"%-14s output file\n","-out <file>");
303f579bf8eSKris Kennaway 			BIO_printf(bio_err,"%-14s pass phrase source\n","-pass <arg>");
30474664626SKris Kennaway 			BIO_printf(bio_err,"%-14s encrypt\n","-e");
30574664626SKris Kennaway 			BIO_printf(bio_err,"%-14s decrypt\n","-d");
30674664626SKris Kennaway 			BIO_printf(bio_err,"%-14s base64 encode/decode, depending on encryption flag\n","-a/-base64");
3076be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s passphrase is the next argument\n","-k");
3086be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s passphrase is the first line of the file argument\n","-kfile");
3096be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s the next argument is the md to use to create a key\n","-md");
3106be8ae07SJacques Vidrine 			BIO_printf(bio_err,"%-14s   from a passphrase.  One of md2, md5, sha or sha1\n","");
3111f13597dSJung-uk Kim 			BIO_printf(bio_err,"%-14s salt in hex is the next argument\n","-S");
31274664626SKris Kennaway 			BIO_printf(bio_err,"%-14s key/iv in hex is the next argument\n","-K/-iv");
31374664626SKris Kennaway 			BIO_printf(bio_err,"%-14s print the iv/key (then exit if -P)\n","-[pP]");
31474664626SKris Kennaway 			BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>");
3151f13597dSJung-uk Kim 			BIO_printf(bio_err,"%-14s disable standard block padding\n","-nopad");
316fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
3175c87c606SMark Murray 			BIO_printf(bio_err,"%-14s use engine e, possibly a hardware device.\n","-engine e");
318fceca8a3SJacques Vidrine #endif
31974664626SKris Kennaway 
32074664626SKris Kennaway 			BIO_printf(bio_err,"Cipher Types\n");
3215c87c606SMark Murray 			OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
3225c87c606SMark Murray 					       show_ciphers,
3235c87c606SMark Murray 					       bio_err);
3245c87c606SMark Murray 			BIO_printf(bio_err,"\n");
32574664626SKris Kennaway 
32674664626SKris Kennaway 			goto end;
32774664626SKris Kennaway 			}
32874664626SKris Kennaway 		argc--;
32974664626SKris Kennaway 		argv++;
33074664626SKris Kennaway 		}
33174664626SKris Kennaway 
332fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
333a3ddd25aSSimon L. B. Nielsen         setup_engine(bio_err, engine, 0);
334fceca8a3SJacques Vidrine #endif
3355c87c606SMark Murray 
33694ad176cSJung-uk Kim 	if (cipher && EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
33794ad176cSJung-uk Kim 		{
33894ad176cSJung-uk Kim 		BIO_printf(bio_err, "AEAD ciphers not supported by the enc utility\n");
33994ad176cSJung-uk Kim 		goto end;
34094ad176cSJung-uk Kim 		}
34194ad176cSJung-uk Kim 
342*a93cbc2bSJung-uk Kim 	if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE))
343*a93cbc2bSJung-uk Kim 		{
344*a93cbc2bSJung-uk Kim 		BIO_printf(bio_err, "Ciphers in XTS mode are not supported by the enc utility\n");
345*a93cbc2bSJung-uk Kim 		goto end;
346*a93cbc2bSJung-uk Kim 		}
347*a93cbc2bSJung-uk Kim 
3486be8ae07SJacques Vidrine 	if (md && (dgst=EVP_get_digestbyname(md)) == NULL)
3496be8ae07SJacques Vidrine 		{
3506be8ae07SJacques Vidrine 		BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
3516be8ae07SJacques Vidrine 		goto end;
3526be8ae07SJacques Vidrine 		}
3536be8ae07SJacques Vidrine 
3546be8ae07SJacques Vidrine 	if (dgst == NULL)
3556be8ae07SJacques Vidrine 		{
3566be8ae07SJacques Vidrine 		dgst = EVP_md5();
3576be8ae07SJacques Vidrine 		}
3586be8ae07SJacques Vidrine 
35974664626SKris Kennaway 	if (bufsize != NULL)
36074664626SKris Kennaway 		{
36174664626SKris Kennaway 		unsigned long n;
36274664626SKris Kennaway 
36374664626SKris Kennaway 		for (n=0; *bufsize; bufsize++)
36474664626SKris Kennaway 			{
36574664626SKris Kennaway 			i= *bufsize;
36674664626SKris Kennaway 			if ((i <= '9') && (i >= '0'))
36774664626SKris Kennaway 				n=n*10+i-'0';
36874664626SKris Kennaway 			else if (i == 'k')
36974664626SKris Kennaway 				{
37074664626SKris Kennaway 				n*=1024;
37174664626SKris Kennaway 				bufsize++;
37274664626SKris Kennaway 				break;
37374664626SKris Kennaway 				}
37474664626SKris Kennaway 			}
37574664626SKris Kennaway 		if (*bufsize != '\0')
37674664626SKris Kennaway 			{
37774664626SKris Kennaway 			BIO_printf(bio_err,"invalid 'bufsize' specified.\n");
37874664626SKris Kennaway 			goto end;
37974664626SKris Kennaway 			}
38074664626SKris Kennaway 
38174664626SKris Kennaway 		/* It must be large enough for a base64 encoded line */
382ed5d4f9aSSimon L. B. Nielsen 		if (base64 && n < 80) n=80;
38374664626SKris Kennaway 
38474664626SKris Kennaway 		bsize=(int)n;
38574664626SKris Kennaway 		if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize);
38674664626SKris Kennaway 		}
38774664626SKris Kennaway 
388ddd58736SKris Kennaway 	strbuf=OPENSSL_malloc(SIZE);
389ddd58736SKris Kennaway 	buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
39074664626SKris Kennaway 	if ((buff == NULL) || (strbuf == NULL))
39174664626SKris Kennaway 		{
392ddd58736SKris Kennaway 		BIO_printf(bio_err,"OPENSSL_malloc failure %ld\n",(long)EVP_ENCODE_LENGTH(bsize));
39374664626SKris Kennaway 		goto end;
39474664626SKris Kennaway 		}
39574664626SKris Kennaway 
39674664626SKris Kennaway 	in=BIO_new(BIO_s_file());
39774664626SKris Kennaway 	out=BIO_new(BIO_s_file());
39874664626SKris Kennaway 	if ((in == NULL) || (out == NULL))
39974664626SKris Kennaway 		{
40074664626SKris Kennaway 		ERR_print_errors(bio_err);
40174664626SKris Kennaway 		goto end;
40274664626SKris Kennaway 		}
40374664626SKris Kennaway 	if (debug)
40474664626SKris Kennaway 		{
40574664626SKris Kennaway 		BIO_set_callback(in,BIO_debug_callback);
40674664626SKris Kennaway 		BIO_set_callback(out,BIO_debug_callback);
4075471f83eSSimon L. B. Nielsen 		BIO_set_callback_arg(in,(char *)bio_err);
4085471f83eSSimon L. B. Nielsen 		BIO_set_callback_arg(out,(char *)bio_err);
40974664626SKris Kennaway 		}
41074664626SKris Kennaway 
41174664626SKris Kennaway 	if (inf == NULL)
412ed5d4f9aSSimon L. B. Nielsen 	        {
4131f13597dSJung-uk Kim #ifndef OPENSSL_NO_SETVBUF_IONBF
414ed5d4f9aSSimon L. B. Nielsen 		if (bufsize != NULL)
415ed5d4f9aSSimon L. B. Nielsen 			setvbuf(stdin, (char *)NULL, _IONBF, 0);
4161f13597dSJung-uk Kim #endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
41774664626SKris Kennaway 		BIO_set_fp(in,stdin,BIO_NOCLOSE);
418ed5d4f9aSSimon L. B. Nielsen 	        }
41974664626SKris Kennaway 	else
42074664626SKris Kennaway 		{
42174664626SKris Kennaway 		if (BIO_read_filename(in,inf) <= 0)
42274664626SKris Kennaway 			{
42374664626SKris Kennaway 			perror(inf);
42474664626SKris Kennaway 			goto end;
42574664626SKris Kennaway 			}
42674664626SKris Kennaway 		}
42774664626SKris Kennaway 
428f579bf8eSKris Kennaway 	if(!str && passarg) {
429f579bf8eSKris Kennaway 		if(!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
430f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error getting password\n");
431f579bf8eSKris Kennaway 			goto end;
432f579bf8eSKris Kennaway 		}
433f579bf8eSKris Kennaway 		str = pass;
434f579bf8eSKris Kennaway 	}
435f579bf8eSKris Kennaway 
43674664626SKris Kennaway 	if ((str == NULL) && (cipher != NULL) && (hkey == NULL))
43774664626SKris Kennaway 		{
43874664626SKris Kennaway 		for (;;)
43974664626SKris Kennaway 			{
44074664626SKris Kennaway 			char buf[200];
44174664626SKris Kennaway 
442ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"enter %s %s password:",
44374664626SKris Kennaway 				     OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
44474664626SKris Kennaway 				     (enc)?"encryption":"decryption");
44574664626SKris Kennaway 			strbuf[0]='\0';
44674664626SKris Kennaway 			i=EVP_read_pw_string((char *)strbuf,SIZE,buf,enc);
44774664626SKris Kennaway 			if (i == 0)
44874664626SKris Kennaway 				{
44974664626SKris Kennaway 				if (strbuf[0] == '\0')
45074664626SKris Kennaway 					{
45174664626SKris Kennaway 					ret=1;
45274664626SKris Kennaway 					goto end;
45374664626SKris Kennaway 					}
45474664626SKris Kennaway 				str=strbuf;
45574664626SKris Kennaway 				break;
45674664626SKris Kennaway 				}
45774664626SKris Kennaway 			if (i < 0)
45874664626SKris Kennaway 				{
45974664626SKris Kennaway 				BIO_printf(bio_err,"bad password read\n");
46074664626SKris Kennaway 				goto end;
46174664626SKris Kennaway 				}
46274664626SKris Kennaway 			}
46374664626SKris Kennaway 		}
46474664626SKris Kennaway 
46574664626SKris Kennaway 
46674664626SKris Kennaway 	if (outf == NULL)
467ddd58736SKris Kennaway 		{
46874664626SKris Kennaway 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
4691f13597dSJung-uk Kim #ifndef OPENSSL_NO_SETVBUF_IONBF
470ed5d4f9aSSimon L. B. Nielsen 		if (bufsize != NULL)
471ed5d4f9aSSimon L. B. Nielsen 			setvbuf(stdout, (char *)NULL, _IONBF, 0);
4721f13597dSJung-uk Kim #endif /* ndef OPENSSL_NO_SETVBUF_IONBF */
4735c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
474ddd58736SKris Kennaway 		{
475ddd58736SKris Kennaway 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
476ddd58736SKris Kennaway 		out = BIO_push(tmpbio, out);
477ddd58736SKris Kennaway 		}
478ddd58736SKris Kennaway #endif
479ddd58736SKris Kennaway 		}
48074664626SKris Kennaway 	else
48174664626SKris Kennaway 		{
48274664626SKris Kennaway 		if (BIO_write_filename(out,outf) <= 0)
48374664626SKris Kennaway 			{
48474664626SKris Kennaway 			perror(outf);
48574664626SKris Kennaway 			goto end;
48674664626SKris Kennaway 			}
48774664626SKris Kennaway 		}
48874664626SKris Kennaway 
48974664626SKris Kennaway 	rbio=in;
49074664626SKris Kennaway 	wbio=out;
49174664626SKris Kennaway 
4921f13597dSJung-uk Kim #ifdef ZLIB
4931f13597dSJung-uk Kim 
4941f13597dSJung-uk Kim 	if (do_zlib)
4951f13597dSJung-uk Kim 		{
4961f13597dSJung-uk Kim 		if ((bzl=BIO_new(BIO_f_zlib())) == NULL)
4971f13597dSJung-uk Kim 			goto end;
4981f13597dSJung-uk Kim 		if (enc)
4991f13597dSJung-uk Kim 			wbio=BIO_push(bzl,wbio);
5001f13597dSJung-uk Kim 		else
5011f13597dSJung-uk Kim 			rbio=BIO_push(bzl,rbio);
5021f13597dSJung-uk Kim 		}
5031f13597dSJung-uk Kim #endif
5041f13597dSJung-uk Kim 
50574664626SKris Kennaway 	if (base64)
50674664626SKris Kennaway 		{
50774664626SKris Kennaway 		if ((b64=BIO_new(BIO_f_base64())) == NULL)
50874664626SKris Kennaway 			goto end;
50974664626SKris Kennaway 		if (debug)
51074664626SKris Kennaway 			{
51174664626SKris Kennaway 			BIO_set_callback(b64,BIO_debug_callback);
5125471f83eSSimon L. B. Nielsen 			BIO_set_callback_arg(b64,(char *)bio_err);
51374664626SKris Kennaway 			}
51474664626SKris Kennaway 		if (olb64)
51574664626SKris Kennaway 			BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
51674664626SKris Kennaway 		if (enc)
51774664626SKris Kennaway 			wbio=BIO_push(b64,wbio);
51874664626SKris Kennaway 		else
51974664626SKris Kennaway 			rbio=BIO_push(b64,rbio);
52074664626SKris Kennaway 		}
52174664626SKris Kennaway 
522f579bf8eSKris Kennaway 	if (cipher != NULL)
523f579bf8eSKris Kennaway 		{
5245c87c606SMark Murray 		/* Note that str is NULL if a key was passed on the command
5255c87c606SMark Murray 		 * line, so we get no salt in that case. Is this a bug?
5265c87c606SMark Murray 		 */
527f579bf8eSKris Kennaway 		if (str != NULL)
528f579bf8eSKris Kennaway 			{
529f579bf8eSKris Kennaway 			/* Salt handling: if encrypting generate a salt and
530f579bf8eSKris Kennaway 			 * write to output BIO. If decrypting read salt from
531f579bf8eSKris Kennaway 			 * input BIO.
532f579bf8eSKris Kennaway 			 */
533f579bf8eSKris Kennaway 			unsigned char *sptr;
534f579bf8eSKris Kennaway 			if(nosalt) sptr = NULL;
535f579bf8eSKris Kennaway 			else {
536f579bf8eSKris Kennaway 				if(enc) {
537f579bf8eSKris Kennaway 					if(hsalt) {
5385c87c606SMark Murray 						if(!set_hex(hsalt,salt,sizeof salt)) {
539f579bf8eSKris Kennaway 							BIO_printf(bio_err,
540f579bf8eSKris Kennaway 								"invalid hex salt value\n");
541f579bf8eSKris Kennaway 							goto end;
542f579bf8eSKris Kennaway 						}
5435c87c606SMark Murray 					} else if (RAND_pseudo_bytes(salt, sizeof salt) < 0)
544f579bf8eSKris Kennaway 						goto end;
545f579bf8eSKris Kennaway 					/* If -P option then don't bother writing */
546f579bf8eSKris Kennaway 					if((printkey != 2)
547f579bf8eSKris Kennaway 					   && (BIO_write(wbio,magic,
548f579bf8eSKris Kennaway 							 sizeof magic-1) != sizeof magic-1
549f579bf8eSKris Kennaway 					       || BIO_write(wbio,
550f579bf8eSKris Kennaway 							    (char *)salt,
5515c87c606SMark Murray 							    sizeof salt) != sizeof salt)) {
552f579bf8eSKris Kennaway 						BIO_printf(bio_err,"error writing output file\n");
553f579bf8eSKris Kennaway 						goto end;
554f579bf8eSKris Kennaway 					}
555f579bf8eSKris Kennaway 				} else if(BIO_read(rbio,mbuf,sizeof mbuf) != sizeof mbuf
556f579bf8eSKris Kennaway 					  || BIO_read(rbio,
557f579bf8eSKris Kennaway 						      (unsigned char *)salt,
5585c87c606SMark Murray 				    sizeof salt) != sizeof salt) {
559f579bf8eSKris Kennaway 					BIO_printf(bio_err,"error reading input file\n");
560f579bf8eSKris Kennaway 					goto end;
561f579bf8eSKris Kennaway 				} else if(memcmp(mbuf,magic,sizeof magic-1)) {
562f579bf8eSKris Kennaway 				    BIO_printf(bio_err,"bad magic number\n");
563f579bf8eSKris Kennaway 				    goto end;
564f579bf8eSKris Kennaway 				}
565f579bf8eSKris Kennaway 
566f579bf8eSKris Kennaway 				sptr = salt;
567f579bf8eSKris Kennaway 			}
568f579bf8eSKris Kennaway 
5696be8ae07SJacques Vidrine 			EVP_BytesToKey(cipher,dgst,sptr,
570f579bf8eSKris Kennaway 				(unsigned char *)str,
571f579bf8eSKris Kennaway 				strlen(str),1,key,iv);
572f579bf8eSKris Kennaway 			/* zero the complete buffer or the string
573f579bf8eSKris Kennaway 			 * passed from the command line
574f579bf8eSKris Kennaway 			 * bug picked up by
575f579bf8eSKris Kennaway 			 * Larry J. Hughes Jr. <hughes@indiana.edu> */
576f579bf8eSKris Kennaway 			if (str == strbuf)
5775c87c606SMark Murray 				OPENSSL_cleanse(str,SIZE);
578f579bf8eSKris Kennaway 			else
5795c87c606SMark Murray 				OPENSSL_cleanse(str,strlen(str));
580f579bf8eSKris Kennaway 			}
5815c87c606SMark Murray 		if ((hiv != NULL) && !set_hex(hiv,iv,sizeof iv))
582f579bf8eSKris Kennaway 			{
583f579bf8eSKris Kennaway 			BIO_printf(bio_err,"invalid hex iv value\n");
584f579bf8eSKris Kennaway 			goto end;
585f579bf8eSKris Kennaway 			}
586db522d3aSSimon L. B. Nielsen 		if ((hiv == NULL) && (str == NULL)
587db522d3aSSimon L. B. Nielsen 		    && EVP_CIPHER_iv_length(cipher) != 0)
58826d191b4SKris Kennaway 			{
58926d191b4SKris Kennaway 			/* No IV was explicitly set and no IV was generated
59026d191b4SKris Kennaway 			 * during EVP_BytesToKey. Hence the IV is undefined,
59126d191b4SKris Kennaway 			 * making correct decryption impossible. */
59226d191b4SKris Kennaway 			BIO_printf(bio_err, "iv undefined\n");
59326d191b4SKris Kennaway 			goto end;
59426d191b4SKris Kennaway 			}
5955c87c606SMark Murray 		if ((hkey != NULL) && !set_hex(hkey,key,sizeof key))
596f579bf8eSKris Kennaway 			{
597f579bf8eSKris Kennaway 			BIO_printf(bio_err,"invalid hex key value\n");
598f579bf8eSKris Kennaway 			goto end;
599f579bf8eSKris Kennaway 			}
600f579bf8eSKris Kennaway 
601f579bf8eSKris Kennaway 		if ((benc=BIO_new(BIO_f_cipher())) == NULL)
602f579bf8eSKris Kennaway 			goto end;
6033b4e3dcbSSimon L. B. Nielsen 
6043b4e3dcbSSimon L. B. Nielsen 		/* Since we may be changing parameters work on the encryption
6053b4e3dcbSSimon L. B. Nielsen 		 * context rather than calling BIO_set_cipher().
6063b4e3dcbSSimon L. B. Nielsen 		 */
6073b4e3dcbSSimon L. B. Nielsen 
6085c87c606SMark Murray 		BIO_get_cipher_ctx(benc, &ctx);
609db522d3aSSimon L. B. Nielsen 
610db522d3aSSimon L. B. Nielsen 		if (non_fips_allow)
611db522d3aSSimon L. B. Nielsen 			EVP_CIPHER_CTX_set_flags(ctx,
612db522d3aSSimon L. B. Nielsen 				EVP_CIPH_FLAG_NON_FIPS_ALLOW);
613db522d3aSSimon L. B. Nielsen 
6143b4e3dcbSSimon L. B. Nielsen 		if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc))
6153b4e3dcbSSimon L. B. Nielsen 			{
6163b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err, "Error setting cipher %s\n",
6173b4e3dcbSSimon L. B. Nielsen 				EVP_CIPHER_name(cipher));
6183b4e3dcbSSimon L. B. Nielsen 			ERR_print_errors(bio_err);
6193b4e3dcbSSimon L. B. Nielsen 			goto end;
6205c87c606SMark Murray 			}
6213b4e3dcbSSimon L. B. Nielsen 
6223b4e3dcbSSimon L. B. Nielsen 		if (nopad)
6233b4e3dcbSSimon L. B. Nielsen 			EVP_CIPHER_CTX_set_padding(ctx, 0);
6243b4e3dcbSSimon L. B. Nielsen 
6253b4e3dcbSSimon L. B. Nielsen 		if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
6263b4e3dcbSSimon L. B. Nielsen 			{
6273b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err, "Error setting cipher %s\n",
6283b4e3dcbSSimon L. B. Nielsen 				EVP_CIPHER_name(cipher));
6293b4e3dcbSSimon L. B. Nielsen 			ERR_print_errors(bio_err);
6303b4e3dcbSSimon L. B. Nielsen 			goto end;
6313b4e3dcbSSimon L. B. Nielsen 			}
6323b4e3dcbSSimon L. B. Nielsen 
633f579bf8eSKris Kennaway 		if (debug)
634f579bf8eSKris Kennaway 			{
635f579bf8eSKris Kennaway 			BIO_set_callback(benc,BIO_debug_callback);
6365471f83eSSimon L. B. Nielsen 			BIO_set_callback_arg(benc,(char *)bio_err);
637f579bf8eSKris Kennaway 			}
638f579bf8eSKris Kennaway 
639f579bf8eSKris Kennaway 		if (printkey)
640f579bf8eSKris Kennaway 			{
641f579bf8eSKris Kennaway 			if (!nosalt)
642f579bf8eSKris Kennaway 				{
643f579bf8eSKris Kennaway 				printf("salt=");
6443b4e3dcbSSimon L. B. Nielsen 				for (i=0; i<(int)sizeof(salt); i++)
645f579bf8eSKris Kennaway 					printf("%02X",salt[i]);
646f579bf8eSKris Kennaway 				printf("\n");
647f579bf8eSKris Kennaway 				}
648f579bf8eSKris Kennaway 			if (cipher->key_len > 0)
649f579bf8eSKris Kennaway 				{
650f579bf8eSKris Kennaway 				printf("key=");
651f579bf8eSKris Kennaway 				for (i=0; i<cipher->key_len; i++)
652f579bf8eSKris Kennaway 					printf("%02X",key[i]);
653f579bf8eSKris Kennaway 				printf("\n");
654f579bf8eSKris Kennaway 				}
655f579bf8eSKris Kennaway 			if (cipher->iv_len > 0)
656f579bf8eSKris Kennaway 				{
657f579bf8eSKris Kennaway 				printf("iv =");
658f579bf8eSKris Kennaway 				for (i=0; i<cipher->iv_len; i++)
659f579bf8eSKris Kennaway 					printf("%02X",iv[i]);
660f579bf8eSKris Kennaway 				printf("\n");
661f579bf8eSKris Kennaway 				}
662f579bf8eSKris Kennaway 			if (printkey == 2)
663f579bf8eSKris Kennaway 				{
664f579bf8eSKris Kennaway 				ret=0;
665f579bf8eSKris Kennaway 				goto end;
666f579bf8eSKris Kennaway 				}
667f579bf8eSKris Kennaway 			}
668f579bf8eSKris Kennaway 		}
669f579bf8eSKris Kennaway 
67074664626SKris Kennaway 	/* Only encrypt/decrypt as we write the file */
67174664626SKris Kennaway 	if (benc != NULL)
67274664626SKris Kennaway 		wbio=BIO_push(benc,wbio);
67374664626SKris Kennaway 
67474664626SKris Kennaway 	for (;;)
67574664626SKris Kennaway 		{
67674664626SKris Kennaway 		inl=BIO_read(rbio,(char *)buff,bsize);
67774664626SKris Kennaway 		if (inl <= 0) break;
67874664626SKris Kennaway 		if (BIO_write(wbio,(char *)buff,inl) != inl)
67974664626SKris Kennaway 			{
68074664626SKris Kennaway 			BIO_printf(bio_err,"error writing output file\n");
68174664626SKris Kennaway 			goto end;
68274664626SKris Kennaway 			}
68374664626SKris Kennaway 		}
68474664626SKris Kennaway 	if (!BIO_flush(wbio))
68574664626SKris Kennaway 		{
68674664626SKris Kennaway 		BIO_printf(bio_err,"bad decrypt\n");
68774664626SKris Kennaway 		goto end;
68874664626SKris Kennaway 		}
68974664626SKris Kennaway 
69074664626SKris Kennaway 	ret=0;
69174664626SKris Kennaway 	if (verbose)
69274664626SKris Kennaway 		{
69374664626SKris Kennaway 		BIO_printf(bio_err,"bytes read   :%8ld\n",BIO_number_read(in));
69474664626SKris Kennaway 		BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out));
69574664626SKris Kennaway 		}
69674664626SKris Kennaway end:
697f579bf8eSKris Kennaway 	ERR_print_errors(bio_err);
698ddd58736SKris Kennaway 	if (strbuf != NULL) OPENSSL_free(strbuf);
699ddd58736SKris Kennaway 	if (buff != NULL) OPENSSL_free(buff);
70074664626SKris Kennaway 	if (in != NULL) BIO_free(in);
701ddd58736SKris Kennaway 	if (out != NULL) BIO_free_all(out);
70274664626SKris Kennaway 	if (benc != NULL) BIO_free(benc);
70374664626SKris Kennaway 	if (b64 != NULL) BIO_free(b64);
7041f13597dSJung-uk Kim #ifdef ZLIB
7051f13597dSJung-uk Kim 	if (bzl != NULL) BIO_free(bzl);
7061f13597dSJung-uk Kim #endif
707ddd58736SKris Kennaway 	if(pass) OPENSSL_free(pass);
7085c87c606SMark Murray 	apps_shutdown();
7095c87c606SMark Murray 	OPENSSL_EXIT(ret);
71074664626SKris Kennaway 	}
71174664626SKris Kennaway 
71274664626SKris Kennaway int set_hex(char *in, unsigned char *out, int size)
71374664626SKris Kennaway 	{
71474664626SKris Kennaway 	int i,n;
71574664626SKris Kennaway 	unsigned char j;
71674664626SKris Kennaway 
71774664626SKris Kennaway 	n=strlen(in);
71874664626SKris Kennaway 	if (n > (size*2))
71974664626SKris Kennaway 		{
72074664626SKris Kennaway 		BIO_printf(bio_err,"hex string is too long\n");
72174664626SKris Kennaway 		return(0);
72274664626SKris Kennaway 		}
72374664626SKris Kennaway 	memset(out,0,size);
72474664626SKris Kennaway 	for (i=0; i<n; i++)
72574664626SKris Kennaway 		{
72674664626SKris Kennaway 		j=(unsigned char)*in;
72774664626SKris Kennaway 		*(in++)='\0';
72874664626SKris Kennaway 		if (j == 0) break;
72974664626SKris Kennaway 		if ((j >= '0') && (j <= '9'))
73074664626SKris Kennaway 			j-='0';
73174664626SKris Kennaway 		else if ((j >= 'A') && (j <= 'F'))
73274664626SKris Kennaway 			j=j-'A'+10;
73374664626SKris Kennaway 		else if ((j >= 'a') && (j <= 'f'))
73474664626SKris Kennaway 			j=j-'a'+10;
73574664626SKris Kennaway 		else
73674664626SKris Kennaway 			{
73774664626SKris Kennaway 			BIO_printf(bio_err,"non-hex digit\n");
73874664626SKris Kennaway 			return(0);
73974664626SKris Kennaway 			}
74074664626SKris Kennaway 		if (i&1)
74174664626SKris Kennaway 			out[i/2]|=j;
74274664626SKris Kennaway 		else
74374664626SKris Kennaway 			out[i/2]=(j<<4);
74474664626SKris Kennaway 		}
74574664626SKris Kennaway 	return(1);
74674664626SKris Kennaway 	}
747