evp_test.c 9.7 KB
Newer Older
B
Ben Laurie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* Written by Ben Laurie, 2001 */
/*
 * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
52 53 54

#include "../e_os.h"

B
Ben Laurie 已提交
55
#include <openssl/evp.h>
56
#ifndef OPENSSL_NO_ENGINE
B
Ben Laurie 已提交
57
#include <openssl/engine.h>
58 59
#endif
#include <openssl/err.h>
60
#include <openssl/conf.h>
B
Ben Laurie 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
    {
    int n=0;

    fprintf(f,"%s",title);
    for( ; n < l ; ++n)
	{
	if((n%16) == 0)
	    fprintf(f,"\n%04x",n);
	fprintf(f," %02x",s[n]);
	}
    fprintf(f,"\n");
    }

static int convert(unsigned char *s)
    {
    unsigned char *d;

    for(d=s ; *s ; s+=2,++d)
	{
82
	unsigned int n;
B
Ben Laurie 已提交
83 84 85 86

	if(!s[1])
	    {
	    fprintf(stderr,"Odd number of hex digits!");
87
	    EXIT(4);
B
Ben Laurie 已提交
88 89 90 91 92 93 94
	    }
	sscanf((char *)s,"%2x",&n);
	*d=(unsigned char)n;
	}
    return s-d;
    }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static char *sstrsep(char **string, const char *delim)
    {
    char isdelim[256];
    char *token = *string;

    if (**string == 0)
        return NULL;

    memset(isdelim, 0, 256);
    isdelim[0] = 1;

    while (*delim)
        {
        isdelim[(unsigned char)(*delim)] = 1;
        delim++;
        }

    while (!isdelim[(unsigned char)(**string)])
        {
        (*string)++;
        }

    if (**string)
        {
        **string = 0;
        (*string)++;
        }

    return token;
    }

U
Ulf Möller 已提交
126
static unsigned char *ustrsep(char **p,const char *sep)
127
    { return (unsigned char *)sstrsep(p,sep); }
B
Ben Laurie 已提交
128

129 130 131
static int test1_exit(int ec)
	{
	EXIT(ec);
132
	return(0);		/* To keep some compilers quiet */
133 134
	}

B
Ben Laurie 已提交
135 136 137
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,
138 139
		  const unsigned char *ciphertext,int cn,
		  int encdec)
B
Ben Laurie 已提交
140 141 142 143 144
    {
    EVP_CIPHER_CTX ctx;
    unsigned char out[4096];
    int outl,outl2;

145 146
    printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
	   (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
B
Ben Laurie 已提交
147 148 149 150 151 152 153 154 155 156
    hexdump(stdout,"Key",key,kn);
    if(in)
	hexdump(stdout,"IV",iv,in);
    hexdump(stdout,"Plaintext",plaintext,pn);
    hexdump(stdout,"Ciphertext",ciphertext,cn);
    
    if(kn != c->key_len)
	{
	fprintf(stderr,"Key length doesn't match, got %d expected %d\n",kn,
		c->key_len);
157
	test1_exit(5);
B
Ben Laurie 已提交
158
	}
159
    EVP_CIPHER_CTX_init(&ctx);
160 161 162 163 164
    if (encdec != 0)
        {
	if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
	    {
	    fprintf(stderr,"EncryptInit failed\n");
165
	    test1_exit(10);
166 167
	    }
	EVP_CIPHER_CTX_set_padding(&ctx,0);
B
Ben Laurie 已提交
168

169 170 171
	if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
	    {
	    fprintf(stderr,"Encrypt failed\n");
172
	    test1_exit(6);
173 174 175 176
	    }
	if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
	    {
	    fprintf(stderr,"EncryptFinal failed\n");
177
	    test1_exit(7);
178
	    }
B
Ben Laurie 已提交
179

180 181 182 183
	if(outl+outl2 != cn)
	    {
	    fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
		    outl+outl2,cn);
184
	    test1_exit(8);
185
	    }
B
Ben Laurie 已提交
186

187 188 189 190 191
	if(memcmp(out,ciphertext,cn))
	    {
	    fprintf(stderr,"Ciphertext mismatch\n");
	    hexdump(stderr,"Got",out,cn);
	    hexdump(stderr,"Expected",ciphertext,cn);
192
	    test1_exit(9);
193
	    }
B
Ben Laurie 已提交
194 195
	}

196 197 198 199 200
    if (encdec <= 0)
        {
	if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
	    {
	    fprintf(stderr,"DecryptInit failed\n");
201
	    test1_exit(11);
202 203
	    }
	EVP_CIPHER_CTX_set_padding(&ctx,0);
B
Ben Laurie 已提交
204

205 206 207
	if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
	    {
	    fprintf(stderr,"Decrypt failed\n");
208
	    test1_exit(6);
209 210 211 212
	    }
	if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
	    {
	    fprintf(stderr,"DecryptFinal failed\n");
213
	    test1_exit(7);
214
	    }
B
Ben Laurie 已提交
215

216 217 218 219
	if(outl+outl2 != cn)
	    {
	    fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
		    outl+outl2,cn);
220
	    test1_exit(8);
221
	    }
B
Ben Laurie 已提交
222

223 224 225 226 227
	if(memcmp(out,plaintext,cn))
	    {
	    fprintf(stderr,"Plaintext mismatch\n");
	    hexdump(stderr,"Got",out,cn);
	    hexdump(stderr,"Expected",plaintext,cn);
228
	    test1_exit(9);
229
	    }
B
Ben Laurie 已提交
230 231
	}

D
 
Dr. Stephen Henson 已提交
232 233
    EVP_CIPHER_CTX_cleanup(&ctx);

B
Ben Laurie 已提交
234 235 236
    printf("\n");
    }

B
Ben Laurie 已提交
237 238 239
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,
240 241
		       const unsigned char *ciphertext,int cn,
		       int encdec)
B
Ben Laurie 已提交
242 243 244 245 246 247 248
    {
    const EVP_CIPHER *c;

    c=EVP_get_cipherbyname(cipher);
    if(!c)
	return 0;

249
    test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
B
Ben Laurie 已提交
250 251 252 253 254 255

    return 1;
    }

static int test_digest(const char *digest,
		       const unsigned char *plaintext,int pn,
D
Dr. Stephen Henson 已提交
256
		       const unsigned char *ciphertext, unsigned int cn)
B
Ben Laurie 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    {
    const EVP_MD *d;
    EVP_MD_CTX ctx;
    unsigned char md[EVP_MAX_MD_SIZE];
    unsigned int mdn;

    d=EVP_get_digestbyname(digest);
    if(!d)
	return 0;

    printf("Testing digest %s\n",EVP_MD_name(d));
    hexdump(stdout,"Plaintext",plaintext,pn);
    hexdump(stdout,"Digest",ciphertext,cn);

    EVP_MD_CTX_init(&ctx);
D
 
Dr. Stephen Henson 已提交
272
    if(!EVP_DigestInit_ex(&ctx,d, NULL))
B
Ben Laurie 已提交
273 274
	{
	fprintf(stderr,"DigestInit failed\n");
275
	EXIT(100);
B
Ben Laurie 已提交
276 277 278 279
	}
    if(!EVP_DigestUpdate(&ctx,plaintext,pn))
	{
	fprintf(stderr,"DigestUpdate failed\n");
280
	EXIT(101);
B
Ben Laurie 已提交
281
	}
D
 
Dr. Stephen Henson 已提交
282
    if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
B
Ben Laurie 已提交
283
	{
284
	fprintf(stderr,"DigestFinal failed\n");
285
	EXIT(101);
B
Ben Laurie 已提交
286
	}
287
    EVP_MD_CTX_cleanup(&ctx);
B
Ben Laurie 已提交
288 289 290 291

    if(mdn != cn)
	{
	fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
292
	EXIT(102);
B
Ben Laurie 已提交
293 294 295 296 297 298 299
	}

    if(memcmp(md,ciphertext,cn))
	{
	fprintf(stderr,"Digest mismatch\n");
	hexdump(stderr,"Got",md,cn);
	hexdump(stderr,"Expected",ciphertext,cn);
300
	EXIT(103);
B
Ben Laurie 已提交
301 302 303 304
	}

    printf("\n");

D
 
Dr. Stephen Henson 已提交
305 306
    EVP_MD_CTX_cleanup(&ctx);

B
Ben Laurie 已提交
307 308 309
    return 1;
    }

B
Ben Laurie 已提交
310 311 312 313 314 315 316 317
int main(int argc,char **argv)
    {
    const char *szTestFile;
    FILE *f;

    if(argc != 2)
	{
	fprintf(stderr,"%s <test file>\n",argv[0]);
318
	EXIT(1);
B
Ben Laurie 已提交
319
	}
320 321 322
    CRYPTO_malloc_debug_init();
    CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
B
Ben Laurie 已提交
323 324 325 326 327 328 329

    szTestFile=argv[1];

    f=fopen(szTestFile,"r");
    if(!f)
	{
	perror(szTestFile);
330
	EXIT(2);
B
Ben Laurie 已提交
331 332
	}

333
    /* Load up the software EVP_CIPHER and EVP_MD definitions */
B
Ben Laurie 已提交
334
    OpenSSL_add_all_ciphers();
B
Ben Laurie 已提交
335
    OpenSSL_add_all_digests();
336
#ifndef OPENSSL_NO_ENGINE
337
    /* Load all compiled-in ENGINEs */
B
Ben Laurie 已提交
338
    ENGINE_load_builtin_engines();
339
#endif
340
#if 0
341
    OPENSSL_config();
342
#endif
343
#ifndef OPENSSL_NO_ENGINE
344 345 346 347 348 349 350 351
    /* Register all available ENGINE implementations of ciphers and digests.
     * This could perhaps be changed to "ENGINE_register_all_complete()"? */
    ENGINE_register_all_ciphers();
    ENGINE_register_all_digests();
    /* If we add command-line options, this statement should be switchable.
     * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
     * they weren't already initialised. */
    /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
352
#endif
B
Ben Laurie 已提交
353 354 355

    for( ; ; )
	{
B
Ben Laurie 已提交
356
	char line[4096];
B
Ben Laurie 已提交
357 358 359
	char *p;
	char *cipher;
	unsigned char *iv,*key,*plaintext,*ciphertext;
360
	int encdec;
B
Ben Laurie 已提交
361 362
	int kn,in,pn,cn;

B
Ben Laurie 已提交
363
	if(!fgets((char *)line,sizeof line,f))
B
Ben Laurie 已提交
364
	    break;
B
Ben Laurie 已提交
365
	if(line[0] == '#' || line[0] == '\n')
B
Ben Laurie 已提交
366
	    continue;
B
Ben Laurie 已提交
367
	p=line;
368
	cipher=sstrsep(&p,":");	
B
Ben Laurie 已提交
369 370 371
	key=ustrsep(&p,":");
	iv=ustrsep(&p,":");
	plaintext=ustrsep(&p,":");
372 373 374 375 376
	ciphertext=ustrsep(&p,":");
	if (p[-1] == '\n') {
	    p[-1] = '\0';
	    encdec = -1;
	} else {
377
	    encdec = atoi(sstrsep(&p,"\n"));
378 379
	}
	      
B
Ben Laurie 已提交
380 381 382 383 384 385

	kn=convert(key);
	in=convert(iv);
	pn=convert(plaintext);
	cn=convert(ciphertext);

386
	if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
B
Ben Laurie 已提交
387
	   && !test_digest(cipher,plaintext,pn,ciphertext,cn))
B
Ben Laurie 已提交
388
	    {
B
Ben Laurie 已提交
389
	    fprintf(stderr,"Can't find %s\n",cipher);
390
	    EXIT(3);
B
Ben Laurie 已提交
391 392 393
	    }
	}

394
#ifndef OPENSSL_NO_ENGINE
395
    ENGINE_cleanup();
396
#endif
397 398 399 400 401
    EVP_cleanup();
    CRYPTO_cleanup_all_ex_data();
    ERR_remove_state(0);
    ERR_free_strings();
    CRYPTO_mem_leaks_fp(stderr);
B
Ben Laurie 已提交
402 403 404

    return 0;
    }