Commit e6bd5e8a authored by Richard Levitte's avatar Richard Levitte
Browse files

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

parent 94f1b50c
Loading
Loading
Loading
Loading
+79 −62
Original line number Diff line number Diff line
@@ -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,
		  const unsigned char *iv,int in,
		  const unsigned char *plaintext,int pn,
		  const unsigned char *ciphertext,int cn)
		  const unsigned char *ciphertext,int cn,
		  int encdec)
    {
    EVP_CIPHER_CTX ctx;
    unsigned char out[4096];
    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);
    if(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);
	}
    EVP_CIPHER_CTX_init(&ctx);
    if (encdec != 0)
        {
	if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
	    {
	    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);
	    exit(9);
	    }
	}

    if (encdec <= 0)
        {
	if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
	    {
	    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);

    if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,pn))
	if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
	    {
	    fprintf(stderr,"Decrypt failed\n");
	    exit(6);
@@ -208,6 +215,7 @@ static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
	    hexdump(stderr,"Expected",plaintext,cn);
	    exit(9);
	    }
	}

    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,
		       const unsigned char *iv,int in,
		       const unsigned char *plaintext,int pn,
		       const unsigned char *ciphertext,int cn)
		       const unsigned char *ciphertext,int cn,
		       int encdec)
    {
    const EVP_CIPHER *c;

@@ -225,7 +234,7 @@ static int test_cipher(const char *cipher,const unsigned char *key,int kn,
    if(!c)
	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;
    }
@@ -332,6 +341,7 @@ int main(int argc,char **argv)
	char *p;
	char *cipher;
	unsigned char *iv,*key,*plaintext,*ciphertext;
	int encdec;
	int kn,in,pn,cn;

	if(!fgets((char *)line,sizeof line,f))
@@ -343,14 +353,21 @@ int main(int argc,char **argv)
	key=ustrsep(&p,":");
	iv=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);
	in=convert(iv);
	pn=convert(plaintext);
	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))
	    {
	    fprintf(stderr,"Can't find %s\n",cipher);