xref: /freebsd/crypto/openssl/apps/enc.c (revision dee36b4f92f269b95977ae9c4a37046fb7cc351c)
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>
70a93cbc2bSJung-uk Kim #ifndef OPENSSL_NO_COMP
711f13597dSJung-uk Kim # include <openssl/comp.h>
72a93cbc2bSJung-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 
84ed7112f0SJung-uk Kim struct doall_enc_ciphers {
85ed7112f0SJung-uk Kim     BIO *bio;
86ed7112f0SJung-uk Kim     int n;
87ed7112f0SJung-uk Kim };
88ed7112f0SJung-uk Kim 
89ed7112f0SJung-uk Kim static void show_ciphers(const OBJ_NAME *name, void *arg)
905c87c606SMark Murray {
91ed7112f0SJung-uk Kim     struct doall_enc_ciphers *dec = (struct doall_enc_ciphers *)arg;
92ed7112f0SJung-uk Kim     const EVP_CIPHER *cipher;
935c87c606SMark Murray 
945c87c606SMark Murray     if (!islower((unsigned char)*name->name))
955c87c606SMark Murray         return;
965c87c606SMark Murray 
97ed7112f0SJung-uk Kim     /* Filter out ciphers that we cannot use */
98ed7112f0SJung-uk Kim     cipher = EVP_get_cipherbyname(name->name);
99ed7112f0SJung-uk Kim     if (cipher == NULL ||
100ed7112f0SJung-uk Kim             (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0 ||
101ed7112f0SJung-uk Kim             EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE)
102ed7112f0SJung-uk Kim         return;
103ed7112f0SJung-uk Kim 
104ed7112f0SJung-uk Kim     BIO_printf(dec->bio, "-%-25s", name->name);
105ed7112f0SJung-uk Kim     if (++dec->n == 3) {
106ed7112f0SJung-uk Kim         BIO_printf(dec->bio, "\n");
107ed7112f0SJung-uk Kim         dec->n = 0;
1086f9291ceSJung-uk Kim     } else
109ed7112f0SJung-uk Kim         BIO_printf(dec->bio, " ");
1105c87c606SMark Murray }
1115c87c606SMark Murray 
112f579bf8eSKris Kennaway int MAIN(int, char **);
113f579bf8eSKris Kennaway 
11474664626SKris Kennaway int MAIN(int argc, char **argv)
11574664626SKris Kennaway {
116f579bf8eSKris Kennaway     static const char magic[] = "Salted__";
117*dee36b4fSJung-uk Kim     char mbuf[sizeof(magic) - 1];
11874664626SKris Kennaway     char *strbuf = NULL;
11974664626SKris Kennaway     unsigned char *buff = NULL, *bufsize = NULL;
12074664626SKris Kennaway     int bsize = BSIZE, verbose = 0;
12174664626SKris Kennaway     int ret = 1, inl;
1225c87c606SMark Murray     int nopad = 0;
1235c87c606SMark Murray     unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
124f579bf8eSKris Kennaway     unsigned char salt[PKCS5_SALT_LEN];
125f579bf8eSKris Kennaway     char *str = NULL, *passarg = NULL, *pass = NULL;
126f579bf8eSKris Kennaway     char *hkey = NULL, *hiv = NULL, *hsalt = NULL;
1276be8ae07SJacques Vidrine     char *md = NULL;
12874664626SKris Kennaway     int enc = 1, printkey = 0, i, base64 = 0;
1291f13597dSJung-uk Kim #ifdef ZLIB
1301f13597dSJung-uk Kim     int do_zlib = 0;
1311f13597dSJung-uk Kim     BIO *bzl = NULL;
1321f13597dSJung-uk Kim #endif
133f579bf8eSKris Kennaway     int debug = 0, olb64 = 0, nosalt = 0;
13474664626SKris Kennaway     const EVP_CIPHER *cipher = NULL, *c;
1353b4e3dcbSSimon L. B. Nielsen     EVP_CIPHER_CTX *ctx = NULL;
13674664626SKris Kennaway     char *inf = NULL, *outf = NULL;
1376f9291ceSJung-uk Kim     BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
1386f9291ceSJung-uk Kim         NULL, *wbio = NULL;
139c1803d78SJacques Vidrine #define PROG_NAME_SIZE  39
140c1803d78SJacques Vidrine     char pname[PROG_NAME_SIZE + 1];
1415c87c606SMark Murray     char *engine = NULL;
1426cf8931aSJung-uk Kim     ENGINE *e = NULL;
1436be8ae07SJacques Vidrine     const EVP_MD *dgst = NULL;
144db522d3aSSimon L. B. Nielsen     int non_fips_allow = 0;
145ed7112f0SJung-uk Kim     struct doall_enc_ciphers dec;
14674664626SKris Kennaway 
14774664626SKris Kennaway     apps_startup();
14874664626SKris Kennaway 
14974664626SKris Kennaway     if (bio_err == NULL)
15074664626SKris Kennaway         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
15174664626SKris Kennaway             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
15274664626SKris Kennaway 
1535c87c606SMark Murray     if (!load_config(bio_err, NULL))
1545c87c606SMark Murray         goto end;
1555c87c606SMark Murray 
15674664626SKris Kennaway     /* first check the program name */
157*dee36b4fSJung-uk Kim     program_name(argv[0], pname, sizeof(pname));
15874664626SKris Kennaway     if (strcmp(pname, "base64") == 0)
15974664626SKris Kennaway         base64 = 1;
1601f13597dSJung-uk Kim #ifdef ZLIB
1611f13597dSJung-uk Kim     if (strcmp(pname, "zlib") == 0)
1621f13597dSJung-uk Kim         do_zlib = 1;
1631f13597dSJung-uk Kim #endif
16474664626SKris Kennaway 
16574664626SKris Kennaway     cipher = EVP_get_cipherbyname(pname);
1661f13597dSJung-uk Kim #ifdef ZLIB
1671f13597dSJung-uk Kim     if (!do_zlib && !base64 && (cipher == NULL)
1681f13597dSJung-uk Kim         && (strcmp(pname, "enc") != 0))
1691f13597dSJung-uk Kim #else
17074664626SKris Kennaway     if (!base64 && (cipher == NULL) && (strcmp(pname, "enc") != 0))
1711f13597dSJung-uk Kim #endif
17274664626SKris Kennaway     {
17374664626SKris Kennaway         BIO_printf(bio_err, "%s is an unknown cipher\n", pname);
17474664626SKris Kennaway         goto bad;
17574664626SKris Kennaway     }
17674664626SKris Kennaway 
17774664626SKris Kennaway     argc--;
17874664626SKris Kennaway     argv++;
1796f9291ceSJung-uk Kim     while (argc >= 1) {
18074664626SKris Kennaway         if (strcmp(*argv, "-e") == 0)
18174664626SKris Kennaway             enc = 1;
1826f9291ceSJung-uk Kim         else if (strcmp(*argv, "-in") == 0) {
1836f9291ceSJung-uk Kim             if (--argc < 1)
1846f9291ceSJung-uk Kim                 goto bad;
18574664626SKris Kennaway             inf = *(++argv);
1866f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-out") == 0) {
1876f9291ceSJung-uk Kim             if (--argc < 1)
1886f9291ceSJung-uk Kim                 goto bad;
18974664626SKris Kennaway             outf = *(++argv);
1906f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-pass") == 0) {
1916f9291ceSJung-uk Kim             if (--argc < 1)
1926f9291ceSJung-uk Kim                 goto bad;
193f579bf8eSKris Kennaway             passarg = *(++argv);
194f579bf8eSKris Kennaway         }
195fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1966f9291ceSJung-uk Kim         else if (strcmp(*argv, "-engine") == 0) {
1976f9291ceSJung-uk Kim             if (--argc < 1)
1986f9291ceSJung-uk Kim                 goto bad;
1995c87c606SMark Murray             engine = *(++argv);
2005c87c606SMark Murray         }
201fceca8a3SJacques Vidrine #endif
20274664626SKris Kennaway         else if (strcmp(*argv, "-d") == 0)
20374664626SKris Kennaway             enc = 0;
20474664626SKris Kennaway         else if (strcmp(*argv, "-p") == 0)
20574664626SKris Kennaway             printkey = 1;
20674664626SKris Kennaway         else if (strcmp(*argv, "-v") == 0)
20774664626SKris Kennaway             verbose = 1;
2085c87c606SMark Murray         else if (strcmp(*argv, "-nopad") == 0)
2095c87c606SMark Murray             nopad = 1;
210f579bf8eSKris Kennaway         else if (strcmp(*argv, "-salt") == 0)
211f579bf8eSKris Kennaway             nosalt = 0;
212f579bf8eSKris Kennaway         else if (strcmp(*argv, "-nosalt") == 0)
213f579bf8eSKris Kennaway             nosalt = 1;
214f579bf8eSKris Kennaway         else if (strcmp(*argv, "-debug") == 0)
21574664626SKris Kennaway             debug = 1;
21674664626SKris Kennaway         else if (strcmp(*argv, "-P") == 0)
21774664626SKris Kennaway             printkey = 2;
21874664626SKris Kennaway         else if (strcmp(*argv, "-A") == 0)
21974664626SKris Kennaway             olb64 = 1;
22074664626SKris Kennaway         else if (strcmp(*argv, "-a") == 0)
22174664626SKris Kennaway             base64 = 1;
22274664626SKris Kennaway         else if (strcmp(*argv, "-base64") == 0)
22374664626SKris Kennaway             base64 = 1;
2241f13597dSJung-uk Kim #ifdef ZLIB
2251f13597dSJung-uk Kim         else if (strcmp(*argv, "-z") == 0)
2261f13597dSJung-uk Kim             do_zlib = 1;
2271f13597dSJung-uk Kim #endif
2286f9291ceSJung-uk Kim         else if (strcmp(*argv, "-bufsize") == 0) {
2296f9291ceSJung-uk Kim             if (--argc < 1)
2306f9291ceSJung-uk Kim                 goto bad;
23174664626SKris Kennaway             bufsize = (unsigned char *)*(++argv);
2326f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-k") == 0) {
2336f9291ceSJung-uk Kim             if (--argc < 1)
2346f9291ceSJung-uk Kim                 goto bad;
23574664626SKris Kennaway             str = *(++argv);
2366f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-kfile") == 0) {
23774664626SKris Kennaway             static char buf[128];
23874664626SKris Kennaway             FILE *infile;
23974664626SKris Kennaway             char *file;
24074664626SKris Kennaway 
2416f9291ceSJung-uk Kim             if (--argc < 1)
2426f9291ceSJung-uk Kim                 goto bad;
24374664626SKris Kennaway             file = *(++argv);
24474664626SKris Kennaway             infile = fopen(file, "r");
2456f9291ceSJung-uk Kim             if (infile == NULL) {
2466f9291ceSJung-uk Kim                 BIO_printf(bio_err, "unable to read key from '%s'\n", file);
24774664626SKris Kennaway                 goto bad;
24874664626SKris Kennaway             }
24974664626SKris Kennaway             buf[0] = '\0';
250*dee36b4fSJung-uk Kim             if (!fgets(buf, sizeof(buf), infile)) {
2516f9291ceSJung-uk Kim                 BIO_printf(bio_err, "unable to read key from '%s'\n", file);
2526a599222SSimon L. B. Nielsen                 goto bad;
2536a599222SSimon L. B. Nielsen             }
25474664626SKris Kennaway             fclose(infile);
25574664626SKris Kennaway             i = strlen(buf);
2566f9291ceSJung-uk Kim             if ((i > 0) && ((buf[i - 1] == '\n') || (buf[i - 1] == '\r')))
25774664626SKris Kennaway                 buf[--i] = '\0';
2586f9291ceSJung-uk Kim             if ((i > 0) && ((buf[i - 1] == '\n') || (buf[i - 1] == '\r')))
25974664626SKris Kennaway                 buf[--i] = '\0';
2606f9291ceSJung-uk Kim             if (i < 1) {
26174664626SKris Kennaway                 BIO_printf(bio_err, "zero length password\n");
26274664626SKris Kennaway                 goto bad;
26374664626SKris Kennaway             }
26474664626SKris Kennaway             str = buf;
2656f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-K") == 0) {
2666f9291ceSJung-uk Kim             if (--argc < 1)
2676f9291ceSJung-uk Kim                 goto bad;
26874664626SKris Kennaway             hkey = *(++argv);
2696f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-S") == 0) {
2706f9291ceSJung-uk Kim             if (--argc < 1)
2716f9291ceSJung-uk Kim                 goto bad;
272f579bf8eSKris Kennaway             hsalt = *(++argv);
2736f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-iv") == 0) {
2746f9291ceSJung-uk Kim             if (--argc < 1)
2756f9291ceSJung-uk Kim                 goto bad;
27674664626SKris Kennaway             hiv = *(++argv);
2776f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-md") == 0) {
2786f9291ceSJung-uk Kim             if (--argc < 1)
2796f9291ceSJung-uk Kim                 goto bad;
2806be8ae07SJacques Vidrine             md = *(++argv);
2816f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-non-fips-allow") == 0)
282db522d3aSSimon L. B. Nielsen             non_fips_allow = 1;
28374664626SKris Kennaway         else if ((argv[0][0] == '-') &&
2846f9291ceSJung-uk Kim                  ((c = EVP_get_cipherbyname(&(argv[0][1]))) != NULL)) {
28574664626SKris Kennaway             cipher = c;
2866f9291ceSJung-uk Kim         } else if (strcmp(*argv, "-none") == 0)
28774664626SKris Kennaway             cipher = NULL;
2886f9291ceSJung-uk Kim         else {
28974664626SKris Kennaway             BIO_printf(bio_err, "unknown option '%s'\n", *argv);
29074664626SKris Kennaway  bad:
29174664626SKris Kennaway             BIO_printf(bio_err, "options are\n");
29274664626SKris Kennaway             BIO_printf(bio_err, "%-14s input file\n", "-in <file>");
293f579bf8eSKris Kennaway             BIO_printf(bio_err, "%-14s output file\n", "-out <file>");
294f579bf8eSKris Kennaway             BIO_printf(bio_err, "%-14s pass phrase source\n", "-pass <arg>");
29574664626SKris Kennaway             BIO_printf(bio_err, "%-14s encrypt\n", "-e");
29674664626SKris Kennaway             BIO_printf(bio_err, "%-14s decrypt\n", "-d");
2976f9291ceSJung-uk Kim             BIO_printf(bio_err,
2986f9291ceSJung-uk Kim                        "%-14s base64 encode/decode, depending on encryption flag\n",
2996f9291ceSJung-uk Kim                        "-a/-base64");
3006f9291ceSJung-uk Kim             BIO_printf(bio_err, "%-14s passphrase is the next argument\n",
3016f9291ceSJung-uk Kim                        "-k");
3026f9291ceSJung-uk Kim             BIO_printf(bio_err,
3036f9291ceSJung-uk Kim                        "%-14s passphrase is the first line of the file argument\n",
3046f9291ceSJung-uk Kim                        "-kfile");
3056f9291ceSJung-uk Kim             BIO_printf(bio_err,
3066f9291ceSJung-uk Kim                        "%-14s the next argument is the md to use to create a key\n",
3076f9291ceSJung-uk Kim                        "-md");
3086f9291ceSJung-uk Kim             BIO_printf(bio_err,
3096f9291ceSJung-uk Kim                        "%-14s   from a passphrase.  One of md2, md5, sha or sha1\n",
3106f9291ceSJung-uk Kim                        "");
3116f9291ceSJung-uk Kim             BIO_printf(bio_err, "%-14s salt in hex is the next argument\n",
3126f9291ceSJung-uk Kim                        "-S");
3136f9291ceSJung-uk Kim             BIO_printf(bio_err, "%-14s key/iv in hex is the next argument\n",
3146f9291ceSJung-uk Kim                        "-K/-iv");
3156f9291ceSJung-uk Kim             BIO_printf(bio_err, "%-14s print the iv/key (then exit if -P)\n",
3166f9291ceSJung-uk Kim                        "-[pP]");
31774664626SKris Kennaway             BIO_printf(bio_err, "%-14s buffer size\n", "-bufsize <n>");
3186f9291ceSJung-uk Kim             BIO_printf(bio_err, "%-14s disable standard block padding\n",
3196f9291ceSJung-uk Kim                        "-nopad");
320fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
3216f9291ceSJung-uk Kim             BIO_printf(bio_err,
3226f9291ceSJung-uk Kim                        "%-14s use engine e, possibly a hardware device.\n",
3236f9291ceSJung-uk Kim                        "-engine e");
324fceca8a3SJacques Vidrine #endif
32574664626SKris Kennaway 
32674664626SKris Kennaway             BIO_printf(bio_err, "Cipher Types\n");
327ed7112f0SJung-uk Kim             dec.n = 0;
328ed7112f0SJung-uk Kim             dec.bio = bio_err;
3295c87c606SMark Murray             OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
330ed7112f0SJung-uk Kim                                    show_ciphers, &dec);
3315c87c606SMark Murray             BIO_printf(bio_err, "\n");
33274664626SKris Kennaway 
33374664626SKris Kennaway             goto end;
33474664626SKris Kennaway         }
33574664626SKris Kennaway         argc--;
33674664626SKris Kennaway         argv++;
33774664626SKris Kennaway     }
33874664626SKris Kennaway 
3396cf8931aSJung-uk Kim     e = setup_engine(bio_err, engine, 0);
3405c87c606SMark Murray 
3416f9291ceSJung-uk Kim     if (cipher && EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
3426f9291ceSJung-uk Kim         BIO_printf(bio_err,
3436f9291ceSJung-uk Kim                    "AEAD ciphers not supported by the enc utility\n");
34494ad176cSJung-uk Kim         goto end;
34594ad176cSJung-uk Kim     }
34694ad176cSJung-uk Kim 
3476f9291ceSJung-uk Kim     if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE)) {
3486f9291ceSJung-uk Kim         BIO_printf(bio_err,
3496f9291ceSJung-uk Kim                    "Ciphers in XTS mode are not supported by the enc utility\n");
350a93cbc2bSJung-uk Kim         goto end;
351a93cbc2bSJung-uk Kim     }
352a93cbc2bSJung-uk Kim 
3536f9291ceSJung-uk Kim     if (md && (dgst = EVP_get_digestbyname(md)) == NULL) {
3546be8ae07SJacques Vidrine         BIO_printf(bio_err, "%s is an unsupported message digest type\n", md);
3556be8ae07SJacques Vidrine         goto end;
3566be8ae07SJacques Vidrine     }
3576be8ae07SJacques Vidrine 
3586f9291ceSJung-uk Kim     if (dgst == NULL) {
3596be8ae07SJacques Vidrine         dgst = EVP_md5();
3606be8ae07SJacques Vidrine     }
3616be8ae07SJacques Vidrine 
3626f9291ceSJung-uk Kim     if (bufsize != NULL) {
36374664626SKris Kennaway         unsigned long n;
36474664626SKris Kennaway 
3656f9291ceSJung-uk Kim         for (n = 0; *bufsize; bufsize++) {
36674664626SKris Kennaway             i = *bufsize;
36774664626SKris Kennaway             if ((i <= '9') && (i >= '0'))
36874664626SKris Kennaway                 n = n * 10 + i - '0';
3696f9291ceSJung-uk Kim             else if (i == 'k') {
37074664626SKris Kennaway                 n *= 1024;
37174664626SKris Kennaway                 bufsize++;
37274664626SKris Kennaway                 break;
37374664626SKris Kennaway             }
37474664626SKris Kennaway         }
3756f9291ceSJung-uk Kim         if (*bufsize != '\0') {
37674664626SKris Kennaway             BIO_printf(bio_err, "invalid 'bufsize' specified.\n");
37774664626SKris Kennaway             goto end;
37874664626SKris Kennaway         }
37974664626SKris Kennaway 
38074664626SKris Kennaway         /* It must be large enough for a base64 encoded line */
3816f9291ceSJung-uk Kim         if (base64 && n < 80)
3826f9291ceSJung-uk Kim             n = 80;
38374664626SKris Kennaway 
38474664626SKris Kennaway         bsize = (int)n;
3856f9291ceSJung-uk Kim         if (verbose)
3866f9291ceSJung-uk Kim             BIO_printf(bio_err, "bufsize=%d\n", bsize);
38774664626SKris Kennaway     }
38874664626SKris Kennaway 
389ddd58736SKris Kennaway     strbuf = OPENSSL_malloc(SIZE);
390ddd58736SKris Kennaway     buff = (unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
3916f9291ceSJung-uk Kim     if ((buff == NULL) || (strbuf == NULL)) {
3926f9291ceSJung-uk Kim         BIO_printf(bio_err, "OPENSSL_malloc failure %ld\n",
3936f9291ceSJung-uk Kim                    (long)EVP_ENCODE_LENGTH(bsize));
39474664626SKris Kennaway         goto end;
39574664626SKris Kennaway     }
39674664626SKris Kennaway 
39774664626SKris Kennaway     in = BIO_new(BIO_s_file());
39874664626SKris Kennaway     out = BIO_new(BIO_s_file());
3996f9291ceSJung-uk Kim     if ((in == NULL) || (out == NULL)) {
40074664626SKris Kennaway         ERR_print_errors(bio_err);
40174664626SKris Kennaway         goto end;
40274664626SKris Kennaway     }
4036f9291ceSJung-uk Kim     if (debug) {
40474664626SKris Kennaway         BIO_set_callback(in, BIO_debug_callback);
40574664626SKris Kennaway         BIO_set_callback(out, BIO_debug_callback);
4065471f83eSSimon L. B. Nielsen         BIO_set_callback_arg(in, (char *)bio_err);
4075471f83eSSimon L. B. Nielsen         BIO_set_callback_arg(out, (char *)bio_err);
40874664626SKris Kennaway     }
40974664626SKris Kennaway 
4106f9291ceSJung-uk Kim     if (inf == NULL) {
4111f13597dSJung-uk Kim #ifndef OPENSSL_NO_SETVBUF_IONBF
412ed5d4f9aSSimon L. B. Nielsen         if (bufsize != NULL)
413ed5d4f9aSSimon L. B. Nielsen             setvbuf(stdin, (char *)NULL, _IONBF, 0);
4141f13597dSJung-uk Kim #endif                          /* ndef OPENSSL_NO_SETVBUF_IONBF */
41574664626SKris Kennaway         BIO_set_fp(in, stdin, BIO_NOCLOSE);
4166f9291ceSJung-uk Kim     } else {
4176f9291ceSJung-uk Kim         if (BIO_read_filename(in, inf) <= 0) {
41874664626SKris Kennaway             perror(inf);
41974664626SKris Kennaway             goto end;
42074664626SKris Kennaway         }
42174664626SKris Kennaway     }
42274664626SKris Kennaway 
423f579bf8eSKris Kennaway     if (!str && passarg) {
424f579bf8eSKris Kennaway         if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
425f579bf8eSKris Kennaway             BIO_printf(bio_err, "Error getting password\n");
426f579bf8eSKris Kennaway             goto end;
427f579bf8eSKris Kennaway         }
428f579bf8eSKris Kennaway         str = pass;
429f579bf8eSKris Kennaway     }
430f579bf8eSKris Kennaway 
4316f9291ceSJung-uk Kim     if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) {
4326f9291ceSJung-uk Kim         for (;;) {
43374664626SKris Kennaway             char buf[200];
43474664626SKris Kennaway 
435*dee36b4fSJung-uk Kim             BIO_snprintf(buf, sizeof(buf), "enter %s %s password:",
43674664626SKris Kennaway                          OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
43774664626SKris Kennaway                          (enc) ? "encryption" : "decryption");
43874664626SKris Kennaway             strbuf[0] = '\0';
43974664626SKris Kennaway             i = EVP_read_pw_string((char *)strbuf, SIZE, buf, enc);
4406f9291ceSJung-uk Kim             if (i == 0) {
4416f9291ceSJung-uk Kim                 if (strbuf[0] == '\0') {
44274664626SKris Kennaway                     ret = 1;
44374664626SKris Kennaway                     goto end;
44474664626SKris Kennaway                 }
44574664626SKris Kennaway                 str = strbuf;
44674664626SKris Kennaway                 break;
44774664626SKris Kennaway             }
4486f9291ceSJung-uk Kim             if (i < 0) {
44974664626SKris Kennaway                 BIO_printf(bio_err, "bad password read\n");
45074664626SKris Kennaway                 goto end;
45174664626SKris Kennaway             }
45274664626SKris Kennaway         }
45374664626SKris Kennaway     }
45474664626SKris Kennaway 
4556f9291ceSJung-uk Kim     if (outf == NULL) {
45674664626SKris Kennaway         BIO_set_fp(out, stdout, BIO_NOCLOSE);
4571f13597dSJung-uk Kim #ifndef OPENSSL_NO_SETVBUF_IONBF
458ed5d4f9aSSimon L. B. Nielsen         if (bufsize != NULL)
459ed5d4f9aSSimon L. B. Nielsen             setvbuf(stdout, (char *)NULL, _IONBF, 0);
4601f13597dSJung-uk Kim #endif                          /* ndef OPENSSL_NO_SETVBUF_IONBF */
4615c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
462ddd58736SKris Kennaway         {
463ddd58736SKris Kennaway             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
464ddd58736SKris Kennaway             out = BIO_push(tmpbio, out);
465ddd58736SKris Kennaway         }
466ddd58736SKris Kennaway #endif
4676f9291ceSJung-uk Kim     } else {
4686f9291ceSJung-uk Kim         if (BIO_write_filename(out, outf) <= 0) {
46974664626SKris Kennaway             perror(outf);
47074664626SKris Kennaway             goto end;
47174664626SKris Kennaway         }
47274664626SKris Kennaway     }
47374664626SKris Kennaway 
47474664626SKris Kennaway     rbio = in;
47574664626SKris Kennaway     wbio = out;
47674664626SKris Kennaway 
4771f13597dSJung-uk Kim #ifdef ZLIB
4781f13597dSJung-uk Kim 
4796f9291ceSJung-uk Kim     if (do_zlib) {
4801f13597dSJung-uk Kim         if ((bzl = BIO_new(BIO_f_zlib())) == NULL)
4811f13597dSJung-uk Kim             goto end;
4821f13597dSJung-uk Kim         if (enc)
4831f13597dSJung-uk Kim             wbio = BIO_push(bzl, wbio);
4841f13597dSJung-uk Kim         else
4851f13597dSJung-uk Kim             rbio = BIO_push(bzl, rbio);
4861f13597dSJung-uk Kim     }
4871f13597dSJung-uk Kim #endif
4881f13597dSJung-uk Kim 
4896f9291ceSJung-uk Kim     if (base64) {
49074664626SKris Kennaway         if ((b64 = BIO_new(BIO_f_base64())) == NULL)
49174664626SKris Kennaway             goto end;
4926f9291ceSJung-uk Kim         if (debug) {
49374664626SKris Kennaway             BIO_set_callback(b64, BIO_debug_callback);
4945471f83eSSimon L. B. Nielsen             BIO_set_callback_arg(b64, (char *)bio_err);
49574664626SKris Kennaway         }
49674664626SKris Kennaway         if (olb64)
49774664626SKris Kennaway             BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
49874664626SKris Kennaway         if (enc)
49974664626SKris Kennaway             wbio = BIO_push(b64, wbio);
50074664626SKris Kennaway         else
50174664626SKris Kennaway             rbio = BIO_push(b64, rbio);
50274664626SKris Kennaway     }
50374664626SKris Kennaway 
5046f9291ceSJung-uk Kim     if (cipher != NULL) {
5056f9291ceSJung-uk Kim         /*
5066f9291ceSJung-uk Kim          * Note that str is NULL if a key was passed on the command line, so
5076f9291ceSJung-uk Kim          * we get no salt in that case. Is this a bug?
5085c87c606SMark Murray          */
5096f9291ceSJung-uk Kim         if (str != NULL) {
5106f9291ceSJung-uk Kim             /*
5116f9291ceSJung-uk Kim              * Salt handling: if encrypting generate a salt and write to
5126f9291ceSJung-uk Kim              * output BIO. If decrypting read salt from input BIO.
513f579bf8eSKris Kennaway              */
514f579bf8eSKris Kennaway             unsigned char *sptr;
5156f9291ceSJung-uk Kim             if (nosalt)
5166f9291ceSJung-uk Kim                 sptr = NULL;
517f579bf8eSKris Kennaway             else {
518f579bf8eSKris Kennaway                 if (enc) {
519f579bf8eSKris Kennaway                     if (hsalt) {
520*dee36b4fSJung-uk Kim                         if (!set_hex(hsalt, salt, sizeof(salt))) {
5216f9291ceSJung-uk Kim                             BIO_printf(bio_err, "invalid hex salt value\n");
522f579bf8eSKris Kennaway                             goto end;
523f579bf8eSKris Kennaway                         }
524*dee36b4fSJung-uk Kim                     } else if (RAND_bytes(salt, sizeof(salt)) <= 0)
525f579bf8eSKris Kennaway                         goto end;
5266f9291ceSJung-uk Kim                     /*
5276f9291ceSJung-uk Kim                      * If -P option then don't bother writing
5286f9291ceSJung-uk Kim                      */
529f579bf8eSKris Kennaway                     if ((printkey != 2)
530f579bf8eSKris Kennaway                         && (BIO_write(wbio, magic,
531*dee36b4fSJung-uk Kim                                       sizeof(magic) - 1) != sizeof(magic) - 1
532f579bf8eSKris Kennaway                             || BIO_write(wbio,
533f579bf8eSKris Kennaway                                          (char *)salt,
534*dee36b4fSJung-uk Kim                                          sizeof(salt)) != sizeof(salt))) {
535f579bf8eSKris Kennaway                         BIO_printf(bio_err, "error writing output file\n");
536f579bf8eSKris Kennaway                         goto end;
537f579bf8eSKris Kennaway                     }
538*dee36b4fSJung-uk Kim                 } else if (BIO_read(rbio, mbuf, sizeof(mbuf)) != sizeof(mbuf)
539f579bf8eSKris Kennaway                            || BIO_read(rbio,
540f579bf8eSKris Kennaway                                        (unsigned char *)salt,
541*dee36b4fSJung-uk Kim                                        sizeof(salt)) != sizeof(salt)) {
542f579bf8eSKris Kennaway                     BIO_printf(bio_err, "error reading input file\n");
543f579bf8eSKris Kennaway                     goto end;
544*dee36b4fSJung-uk Kim                 } else if (memcmp(mbuf, magic, sizeof(magic) - 1)) {
545f579bf8eSKris Kennaway                     BIO_printf(bio_err, "bad magic number\n");
546f579bf8eSKris Kennaway                     goto end;
547f579bf8eSKris Kennaway                 }
548f579bf8eSKris Kennaway 
549f579bf8eSKris Kennaway                 sptr = salt;
550f579bf8eSKris Kennaway             }
551f579bf8eSKris Kennaway 
5526be8ae07SJacques Vidrine             EVP_BytesToKey(cipher, dgst, sptr,
5536f9291ceSJung-uk Kim                            (unsigned char *)str, strlen(str), 1, key, iv);
5546f9291ceSJung-uk Kim             /*
5556f9291ceSJung-uk Kim              * zero the complete buffer or the string passed from the command
5566f9291ceSJung-uk Kim              * line bug picked up by Larry J. Hughes Jr. <hughes@indiana.edu>
5576f9291ceSJung-uk Kim              */
558f579bf8eSKris Kennaway             if (str == strbuf)
5595c87c606SMark Murray                 OPENSSL_cleanse(str, SIZE);
560f579bf8eSKris Kennaway             else
5615c87c606SMark Murray                 OPENSSL_cleanse(str, strlen(str));
562f579bf8eSKris Kennaway         }
563ed6b93beSJung-uk Kim         if (hiv != NULL) {
564ed6b93beSJung-uk Kim             int siz = EVP_CIPHER_iv_length(cipher);
565ed6b93beSJung-uk Kim             if (siz == 0) {
566ed6b93beSJung-uk Kim                 BIO_printf(bio_err, "warning: iv not use by this cipher\n");
567*dee36b4fSJung-uk Kim             } else if (!set_hex(hiv, iv, sizeof(iv))) {
568f579bf8eSKris Kennaway                 BIO_printf(bio_err, "invalid hex iv value\n");
569f579bf8eSKris Kennaway                 goto end;
570f579bf8eSKris Kennaway             }
571ed6b93beSJung-uk Kim         }
572db522d3aSSimon L. B. Nielsen         if ((hiv == NULL) && (str == NULL)
5736f9291ceSJung-uk Kim             && EVP_CIPHER_iv_length(cipher) != 0) {
5746f9291ceSJung-uk Kim             /*
5756f9291ceSJung-uk Kim              * No IV was explicitly set and no IV was generated during
5766f9291ceSJung-uk Kim              * EVP_BytesToKey. Hence the IV is undefined, making correct
5776f9291ceSJung-uk Kim              * decryption impossible.
5786f9291ceSJung-uk Kim              */
57926d191b4SKris Kennaway             BIO_printf(bio_err, "iv undefined\n");
58026d191b4SKris Kennaway             goto end;
58126d191b4SKris Kennaway         }
582ed6b93beSJung-uk Kim         if ((hkey != NULL) && !set_hex(hkey, key, EVP_CIPHER_key_length(cipher))) {
583f579bf8eSKris Kennaway             BIO_printf(bio_err, "invalid hex key value\n");
584f579bf8eSKris Kennaway             goto end;
585f579bf8eSKris Kennaway         }
586f579bf8eSKris Kennaway 
587f579bf8eSKris Kennaway         if ((benc = BIO_new(BIO_f_cipher())) == NULL)
588f579bf8eSKris Kennaway             goto end;
5893b4e3dcbSSimon L. B. Nielsen 
5906f9291ceSJung-uk Kim         /*
5916f9291ceSJung-uk Kim          * Since we may be changing parameters work on the encryption context
5926f9291ceSJung-uk Kim          * rather than calling BIO_set_cipher().
5933b4e3dcbSSimon L. B. Nielsen          */
5943b4e3dcbSSimon L. B. Nielsen 
5955c87c606SMark Murray         BIO_get_cipher_ctx(benc, &ctx);
596db522d3aSSimon L. B. Nielsen 
597db522d3aSSimon L. B. Nielsen         if (non_fips_allow)
5986f9291ceSJung-uk Kim             EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
599db522d3aSSimon L. B. Nielsen 
6006f9291ceSJung-uk Kim         if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) {
6013b4e3dcbSSimon L. B. Nielsen             BIO_printf(bio_err, "Error setting cipher %s\n",
6023b4e3dcbSSimon L. B. Nielsen                        EVP_CIPHER_name(cipher));
6033b4e3dcbSSimon L. B. Nielsen             ERR_print_errors(bio_err);
6043b4e3dcbSSimon L. B. Nielsen             goto end;
6055c87c606SMark Murray         }
6063b4e3dcbSSimon L. B. Nielsen 
6073b4e3dcbSSimon L. B. Nielsen         if (nopad)
6083b4e3dcbSSimon L. B. Nielsen             EVP_CIPHER_CTX_set_padding(ctx, 0);
6093b4e3dcbSSimon L. B. Nielsen 
6106f9291ceSJung-uk Kim         if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) {
6113b4e3dcbSSimon L. B. Nielsen             BIO_printf(bio_err, "Error setting cipher %s\n",
6123b4e3dcbSSimon L. B. Nielsen                        EVP_CIPHER_name(cipher));
6133b4e3dcbSSimon L. B. Nielsen             ERR_print_errors(bio_err);
6143b4e3dcbSSimon L. B. Nielsen             goto end;
6153b4e3dcbSSimon L. B. Nielsen         }
6163b4e3dcbSSimon L. B. Nielsen 
6176f9291ceSJung-uk Kim         if (debug) {
618f579bf8eSKris Kennaway             BIO_set_callback(benc, BIO_debug_callback);
6195471f83eSSimon L. B. Nielsen             BIO_set_callback_arg(benc, (char *)bio_err);
620f579bf8eSKris Kennaway         }
621f579bf8eSKris Kennaway 
6226f9291ceSJung-uk Kim         if (printkey) {
6236f9291ceSJung-uk Kim             if (!nosalt) {
624f579bf8eSKris Kennaway                 printf("salt=");
6253b4e3dcbSSimon L. B. Nielsen                 for (i = 0; i < (int)sizeof(salt); i++)
626f579bf8eSKris Kennaway                     printf("%02X", salt[i]);
627f579bf8eSKris Kennaway                 printf("\n");
628f579bf8eSKris Kennaway             }
6296f9291ceSJung-uk Kim             if (cipher->key_len > 0) {
630f579bf8eSKris Kennaway                 printf("key=");
631f579bf8eSKris Kennaway                 for (i = 0; i < cipher->key_len; i++)
632f579bf8eSKris Kennaway                     printf("%02X", key[i]);
633f579bf8eSKris Kennaway                 printf("\n");
634f579bf8eSKris Kennaway             }
6356f9291ceSJung-uk Kim             if (cipher->iv_len > 0) {
636f579bf8eSKris Kennaway                 printf("iv =");
637f579bf8eSKris Kennaway                 for (i = 0; i < cipher->iv_len; i++)
638f579bf8eSKris Kennaway                     printf("%02X", iv[i]);
639f579bf8eSKris Kennaway                 printf("\n");
640f579bf8eSKris Kennaway             }
6416f9291ceSJung-uk Kim             if (printkey == 2) {
642f579bf8eSKris Kennaway                 ret = 0;
643f579bf8eSKris Kennaway                 goto end;
644f579bf8eSKris Kennaway             }
645f579bf8eSKris Kennaway         }
646f579bf8eSKris Kennaway     }
647f579bf8eSKris Kennaway 
64874664626SKris Kennaway     /* Only encrypt/decrypt as we write the file */
64974664626SKris Kennaway     if (benc != NULL)
65074664626SKris Kennaway         wbio = BIO_push(benc, wbio);
65174664626SKris Kennaway 
6526f9291ceSJung-uk Kim     for (;;) {
65374664626SKris Kennaway         inl = BIO_read(rbio, (char *)buff, bsize);
6546f9291ceSJung-uk Kim         if (inl <= 0)
6556f9291ceSJung-uk Kim             break;
6566f9291ceSJung-uk Kim         if (BIO_write(wbio, (char *)buff, inl) != inl) {
65774664626SKris Kennaway             BIO_printf(bio_err, "error writing output file\n");
65874664626SKris Kennaway             goto end;
65974664626SKris Kennaway         }
66074664626SKris Kennaway     }
6616f9291ceSJung-uk Kim     if (!BIO_flush(wbio)) {
66274664626SKris Kennaway         BIO_printf(bio_err, "bad decrypt\n");
66374664626SKris Kennaway         goto end;
66474664626SKris Kennaway     }
66574664626SKris Kennaway 
66674664626SKris Kennaway     ret = 0;
6676f9291ceSJung-uk Kim     if (verbose) {
66874664626SKris Kennaway         BIO_printf(bio_err, "bytes read   :%8ld\n", BIO_number_read(in));
66974664626SKris Kennaway         BIO_printf(bio_err, "bytes written:%8ld\n", BIO_number_written(out));
67074664626SKris Kennaway     }
67174664626SKris Kennaway  end:
672f579bf8eSKris Kennaway     ERR_print_errors(bio_err);
6736f9291ceSJung-uk Kim     if (strbuf != NULL)
6746f9291ceSJung-uk Kim         OPENSSL_free(strbuf);
6756f9291ceSJung-uk Kim     if (buff != NULL)
6766f9291ceSJung-uk Kim         OPENSSL_free(buff);
6776f9291ceSJung-uk Kim     if (in != NULL)
6786f9291ceSJung-uk Kim         BIO_free(in);
6796f9291ceSJung-uk Kim     if (out != NULL)
6806f9291ceSJung-uk Kim         BIO_free_all(out);
6816f9291ceSJung-uk Kim     if (benc != NULL)
6826f9291ceSJung-uk Kim         BIO_free(benc);
6836f9291ceSJung-uk Kim     if (b64 != NULL)
6846f9291ceSJung-uk Kim         BIO_free(b64);
6851f13597dSJung-uk Kim #ifdef ZLIB
6866f9291ceSJung-uk Kim     if (bzl != NULL)
6876f9291ceSJung-uk Kim         BIO_free(bzl);
6881f13597dSJung-uk Kim #endif
6896cf8931aSJung-uk Kim     release_engine(e);
6906f9291ceSJung-uk Kim     if (pass)
6916f9291ceSJung-uk Kim         OPENSSL_free(pass);
6925c87c606SMark Murray     apps_shutdown();
6935c87c606SMark Murray     OPENSSL_EXIT(ret);
69474664626SKris Kennaway }
69574664626SKris Kennaway 
69674664626SKris Kennaway int set_hex(char *in, unsigned char *out, int size)
69774664626SKris Kennaway {
69874664626SKris Kennaway     int i, n;
69974664626SKris Kennaway     unsigned char j;
70074664626SKris Kennaway 
70174664626SKris Kennaway     n = strlen(in);
7026f9291ceSJung-uk Kim     if (n > (size * 2)) {
70374664626SKris Kennaway         BIO_printf(bio_err, "hex string is too long\n");
70474664626SKris Kennaway         return (0);
70574664626SKris Kennaway     }
70674664626SKris Kennaway     memset(out, 0, size);
7076f9291ceSJung-uk Kim     for (i = 0; i < n; i++) {
70874664626SKris Kennaway         j = (unsigned char)*in;
70974664626SKris Kennaway         *(in++) = '\0';
7106f9291ceSJung-uk Kim         if (j == 0)
7116f9291ceSJung-uk Kim             break;
71274664626SKris Kennaway         if ((j >= '0') && (j <= '9'))
71374664626SKris Kennaway             j -= '0';
71474664626SKris Kennaway         else if ((j >= 'A') && (j <= 'F'))
71574664626SKris Kennaway             j = j - 'A' + 10;
71674664626SKris Kennaway         else if ((j >= 'a') && (j <= 'f'))
71774664626SKris Kennaway             j = j - 'a' + 10;
7186f9291ceSJung-uk Kim         else {
71974664626SKris Kennaway             BIO_printf(bio_err, "non-hex digit\n");
72074664626SKris Kennaway             return (0);
72174664626SKris Kennaway         }
72274664626SKris Kennaway         if (i & 1)
72374664626SKris Kennaway             out[i / 2] |= j;
72474664626SKris Kennaway         else
72574664626SKris Kennaway             out[i / 2] = (j << 4);
72674664626SKris Kennaway     }
72774664626SKris Kennaway     return (1);
72874664626SKris Kennaway }
729