Make it possible to give vectors only for decryption or encryption.

This commit is contained in:
Richard Levitte 2002-05-31 13:12:49 +00:00
parent 94f1b50c0f
commit e6bd5e8a6d

View File

@ -123,13 +123,15 @@ static unsigned char *ustrsep(char **p,const char *sep)
static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn, static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
const unsigned char *iv,int in, const unsigned char *iv,int in,
const unsigned char *plaintext,int pn, const unsigned char *plaintext,int pn,
const unsigned char *ciphertext,int cn) const unsigned char *ciphertext,int cn,
int encdec)
{ {
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX ctx;
unsigned char out[4096]; unsigned char out[4096];
int outl,outl2; int outl,outl2;
printf("Testing cipher %s\n",EVP_CIPHER_name(c)); printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
(encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
hexdump(stdout,"Key",key,kn); hexdump(stdout,"Key",key,kn);
if(in) if(in)
hexdump(stdout,"IV",iv,in); hexdump(stdout,"IV",iv,in);
@ -143,6 +145,8 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
exit(5); exit(5);
} }
EVP_CIPHER_CTX_init(&ctx); EVP_CIPHER_CTX_init(&ctx);
if (encdec != 0)
{
if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv)) if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
{ {
fprintf(stderr,"EncryptInit failed\n"); fprintf(stderr,"EncryptInit failed\n");
@ -175,7 +179,10 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
hexdump(stderr,"Expected",ciphertext,cn); hexdump(stderr,"Expected",ciphertext,cn);
exit(9); exit(9);
} }
}
if (encdec <= 0)
{
if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv)) if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
{ {
fprintf(stderr,"DecryptInit failed\n"); fprintf(stderr,"DecryptInit failed\n");
@ -183,7 +190,7 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
} }
EVP_CIPHER_CTX_set_padding(&ctx,0); EVP_CIPHER_CTX_set_padding(&ctx,0);
if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,pn)) if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
{ {
fprintf(stderr,"Decrypt failed\n"); fprintf(stderr,"Decrypt failed\n");
exit(6); exit(6);
@ -208,6 +215,7 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
hexdump(stderr,"Expected",plaintext,cn); hexdump(stderr,"Expected",plaintext,cn);
exit(9); exit(9);
} }
}
EVP_CIPHER_CTX_cleanup(&ctx); EVP_CIPHER_CTX_cleanup(&ctx);
@ -217,7 +225,8 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
static int test_cipher(const char *cipher,const unsigned char *key,int kn, static int test_cipher(const char *cipher,const unsigned char *key,int kn,
const unsigned char *iv,int in, const unsigned char *iv,int in,
const unsigned char *plaintext,int pn, const unsigned char *plaintext,int pn,
const unsigned char *ciphertext,int cn) const unsigned char *ciphertext,int cn,
int encdec)
{ {
const EVP_CIPHER *c; const EVP_CIPHER *c;
@ -225,7 +234,7 @@ static int test_cipher(const char *cipher,const unsigned char *key,int kn,
if(!c) if(!c)
return 0; return 0;
test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn); test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
return 1; return 1;
} }
@ -332,6 +341,7 @@ int main(int argc,char **argv)
char *p; char *p;
char *cipher; char *cipher;
unsigned char *iv,*key,*plaintext,*ciphertext; unsigned char *iv,*key,*plaintext,*ciphertext;
int encdec;
int kn,in,pn,cn; int kn,in,pn,cn;
if(!fgets((char *)line,sizeof line,f)) if(!fgets((char *)line,sizeof line,f))
@ -343,14 +353,21 @@ int main(int argc,char **argv)
key=ustrsep(&p,":"); key=ustrsep(&p,":");
iv=ustrsep(&p,":"); iv=ustrsep(&p,":");
plaintext=ustrsep(&p,":"); plaintext=ustrsep(&p,":");
ciphertext=ustrsep(&p,"\n"); ciphertext=ustrsep(&p,":");
if (p[-1] == '\n') {
p[-1] = '\0';
encdec = -1;
} else {
encdec = atoi(ustrsep(&p,"\n"));
}
kn=convert(key); kn=convert(key);
in=convert(iv); in=convert(iv);
pn=convert(plaintext); pn=convert(plaintext);
cn=convert(ciphertext); cn=convert(ciphertext);
if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn) if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
&& !test_digest(cipher,plaintext,pn,ciphertext,cn)) && !test_digest(cipher,plaintext,pn,ciphertext,cn))
{ {
fprintf(stderr,"Can't find %s\n",cipher); fprintf(stderr,"Can't find %s\n",cipher);