X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=apps%2Fenc.c;h=d033e380c4febf2e8acf3123ed2264d118500227;hp=d7c990911f083dcdf2812a49ff44492807031ca2;hb=fd699ac55f736e5b39b5fe3c244115e6b776eafa;hpb=b7896b3cb86d80206af14a14d69b0717786f2729 diff --git a/apps/enc.c b/apps/enc.c index d7c990911f..d033e380c4 100644 --- a/apps/enc.c +++ b/apps/enc.c @@ -1,5 +1,5 @@ /* apps/enc.c */ -/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written @@ -60,22 +60,18 @@ #include #include #include "apps.h" -#include "bio.h" -#include "err.h" -#include "evp.h" -#include "objects.h" -#include "x509.h" -#ifdef NO_MD5 -#include "md5.h" +#include +#include +#include +#include +#include +#include +#ifndef NO_MD5 +#include #endif -#include "pem.h" +#include -#ifndef NOPROTO int set_hex(char *in,unsigned char *out,int size); -#else -int set_hex(); -#endif - #undef SIZE #undef BSIZE #undef PROG @@ -84,33 +80,32 @@ int set_hex(); #define BSIZE (8*1024) #define PROG enc_main -int MAIN(argc,argv) -int argc; -char **argv; +int MAIN(int argc, char **argv) { char *strbuf=NULL; unsigned char *buff=NULL,*bufsize=NULL; int bsize=BSIZE,verbose=0; int ret=1,inl; unsigned char key[24],iv[MD5_DIGEST_LENGTH]; + unsigned char salt[PKCS5_SALT_LEN]; char *str=NULL; - char *hkey=NULL,*hiv=NULL; + char *hkey=NULL,*hiv=NULL,*hsalt = NULL; int enc=1,printkey=0,i,base64=0; - int debug=0; - EVP_CIPHER *cipher=NULL,*c; + int debug=0,olb64=0,nosalt=1; + const EVP_CIPHER *cipher=NULL,*c; char *inf=NULL,*outf=NULL; BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL; #define PROG_NAME_SIZE 16 - char pname[PROG_NAME_SIZE]; + char pname[PROG_NAME_SIZE]; apps_startup(); if (bio_err == NULL) if ((bio_err=BIO_new(BIO_s_file())) != NULL) - BIO_set_fp(bio_err,stderr,BIO_NOCLOSE); + BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); /* first check the program name */ - program_name(argv[0],pname,PROG_NAME_SIZE); + program_name(argv[0],pname,PROG_NAME_SIZE); if (strcmp(pname,"base64") == 0) base64=1; @@ -143,11 +138,16 @@ char **argv; printkey=1; else if (strcmp(*argv,"-v") == 0) verbose=1; - else if ((strcmp(*argv,"-debug") == 0) || - (strcmp(*argv,"-d") == 0)) + else if (strcmp(*argv,"-salt") == 0) + nosalt=0; + else if (strcmp(*argv,"-nosalt") == 0) + nosalt=1; + else if (strcmp(*argv,"-debug") == 0) debug=1; else if (strcmp(*argv,"-P") == 0) printkey=2; + else if (strcmp(*argv,"-A") == 0) + olb64=1; else if (strcmp(*argv,"-a") == 0) base64=1; else if (strcmp(*argv,"-base64") == 0) @@ -199,6 +199,11 @@ char **argv; if (--argc < 1) goto bad; hkey= *(++argv); } + else if (strcmp(*argv,"-S") == 0) + { + if (--argc < 1) goto bad; + hsalt= *(++argv); + } else if (strcmp(*argv,"-iv") == 0) { if (--argc < 1) goto bad; @@ -237,7 +242,7 @@ bad: #ifndef NO_RC4 BIO_printf(bio_err,"rc2 :128 bit key RC2 encryption\n"); #endif -#ifndef NO_BLOWFISH +#ifndef NO_BF BIO_printf(bio_err,"bf :128 bit key BlowFish encryption\n"); #endif #ifndef NO_RC4 @@ -275,11 +280,23 @@ bad: LN_rc2_cfb64, LN_rc2_ofb64); BIO_printf(bio_err," -%-4s (%s)\n","rc2", LN_rc2_cbc); #endif -#ifndef NO_BLOWFISH +#ifndef NO_BF BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", LN_bf_ecb, LN_bf_cbc, LN_bf_cfb64, LN_bf_ofb64); BIO_printf(bio_err," -%-4s (%s)\n","bf", LN_bf_cbc); +#endif +#ifndef NO_CAST + BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", + LN_cast5_ecb, LN_cast5_cbc, + LN_cast5_cfb64, LN_cast5_ofb64); + BIO_printf(bio_err," -%-4s (%s)\n","cast", LN_cast5_cbc); +#endif +#ifndef NO_RC5 + BIO_printf(bio_err," -%-12s -%-12s -%-12s -%-12s", + LN_rc5_ecb, LN_rc5_cbc, + LN_rc5_cfb64, LN_rc5_ofb64); + BIO_printf(bio_err," -%-4s (%s)\n","rc5", LN_rc5_cbc); #endif goto end; } @@ -379,11 +396,73 @@ bad: } } + + if (outf == NULL) + BIO_set_fp(out,stdout,BIO_NOCLOSE); + else + { + if (BIO_write_filename(out,outf) <= 0) + { + perror(outf); + goto end; + } + } + + rbio=in; + wbio=out; + + if (base64) + { + if ((b64=BIO_new(BIO_f_base64())) == NULL) + goto end; + if (debug) + { + BIO_set_callback(b64,BIO_debug_callback); + BIO_set_callback_arg(b64,bio_err); + } + if (olb64) + BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); + if (enc) + wbio=BIO_push(b64,wbio); + else + rbio=BIO_push(b64,rbio); + } + if (cipher != NULL) { if (str != NULL) { - EVP_BytesToKey(cipher,EVP_md5(),NULL, + /* Salt handling: if encrypting generate a salt and + * write to output BIO. If decrypting read salt from + * input BIO. + */ + unsigned char *sptr; + if(nosalt) sptr = NULL; + else { + if(enc) { + if(hsalt) { + if(!set_hex(hsalt,salt,PKCS5_SALT_LEN)) { + BIO_printf(bio_err, + "invalid hex salt value\n"); + goto end; + } + } else RAND_bytes(salt, PKCS5_SALT_LEN); + /* If -P option then don't bother writing */ + if((printkey != 2) && (BIO_write(wbio, + (unsigned char *) salt, + PKCS5_SALT_LEN) != PKCS5_SALT_LEN)) { + BIO_printf(bio_err,"error writing output file\n"); + goto end; + } + } else if(BIO_read(rbio, (unsigned char *)salt, + PKCS5_SALT_LEN) != PKCS5_SALT_LEN) { + BIO_printf(bio_err,"error reading input file\n"); + goto end; + } + sptr = salt; + } + + EVP_BytesToKey(cipher,EVP_md5(),sptr, (unsigned char *)str, strlen(str),1,key,iv); /* zero the complete buffer or the string @@ -417,6 +496,13 @@ bad: if (printkey) { + if (!nosalt) + { + printf("salt="); + for (i=0; ikey_len > 0) { printf("key="); @@ -439,36 +525,6 @@ bad: } } - - if (outf == NULL) - BIO_set_fp(out,stdout,BIO_NOCLOSE); - else - { - if (BIO_write_filename(out,outf) <= 0) - { - perror(outf); - goto end; - } - } - - rbio=in; - wbio=out; - - if (base64) - { - if ((b64=BIO_new(BIO_f_base64())) == NULL) - goto end; - if (debug) - { - BIO_set_callback(b64,BIO_debug_callback); - BIO_set_callback_arg(b64,bio_err); - } - if (enc) - wbio=BIO_push(b64,wbio); - else - rbio=BIO_push(b64,rbio); - } - /* Only encrypt/decrypt as we write the file */ if (benc != NULL) wbio=BIO_push(benc,wbio); @@ -505,10 +561,7 @@ end: EXIT(ret); } -int set_hex(in,out,size) -char *in; -unsigned char *out; -int size; +int set_hex(char *in, unsigned char *out, int size) { int i,n; unsigned char j;