fips_gcmtest.c 13.5 KB
Newer Older
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/* fips/aes/fips_gcmtest.c */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
 * project.
 */
/* ====================================================================
 * Copyright (c) 2011 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
 *    licensing@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.
 * ====================================================================
 */


#define OPENSSL_FIPSAPI
#include <openssl/opensslconf.h>

#ifndef OPENSSL_FIPS
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("No FIPS GCM support\n");
    return(0);
}
#else

#include <openssl/bn.h>
#include <openssl/dsa.h>
#include <openssl/fips.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <string.h>
#include <ctype.h>

#include "fips_utl.h"

78
static void gcmtest(FILE *in, FILE *out, int encrypt)
79 80 81 82 83 84 85 86 87 88
	{
	char buf[2048];
	char lbuf[2048];
	char *keyword, *value;
	int keylen = -1, ivlen = -1, aadlen = -1, taglen = -1, ptlen = -1;
	int rv;
	long l;
	unsigned char *key = NULL, *iv = NULL, *aad = NULL, *tag = NULL;
	unsigned char *ct = NULL, *pt = NULL;
	EVP_CIPHER_CTX ctx;
D
Dr. Stephen Henson 已提交
89
	const EVP_CIPHER *gcm = NULL;
90
	FIPS_cipher_ctx_init(&ctx);
91

92
	while(fgets(buf,sizeof buf,in) != NULL)
93
		{
94
		fputs(buf,out);
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 126 127 128 129 130 131 132 133 134 135 136 137 138
		if (!parse_line(&keyword, &value, lbuf, buf))
			continue;
		if(!strcmp(keyword,"[Keylen"))
			{
			keylen = atoi(value);
			if (keylen == 128)
				gcm = EVP_aes_128_gcm();
			else if (keylen == 192)
				gcm = EVP_aes_192_gcm();
			else if (keylen == 256)
				gcm = EVP_aes_256_gcm();
			else 
				{
				fprintf(stderr, "Unsupported keylen %d\n",
							keylen);
				}
			keylen >>= 3;
			}
		else if (!strcmp(keyword, "[IVlen"))
			ivlen = atoi(value) >> 3;
		else if (!strcmp(keyword, "[AADlen"))
			aadlen = atoi(value) >> 3;
		else if (!strcmp(keyword, "[Taglen"))
			taglen = atoi(value) >> 3;
		else if (!strcmp(keyword, "[PTlen"))
			ptlen = atoi(value) >> 3;
		else if(!strcmp(keyword,"Key"))
			{
			key = hex2bin_m(value, &l);
			if (l != keylen)
				{
				fprintf(stderr, "Inconsistent Key length\n");
				exit(1);
				}
			}
		else if(!strcmp(keyword,"IV"))
			{
			iv = hex2bin_m(value, &l);
			if (l != ivlen)
				{
				fprintf(stderr, "Inconsistent IV length\n");
				exit(1);
				}
			}
139 140 141 142 143 144 145 146 147
		else if(!strcmp(keyword,"PT"))
			{
			pt = hex2bin_m(value, &l);
			if (l != ptlen)
				{
				fprintf(stderr, "Inconsistent PT length\n");
				exit(1);
				}
			}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
		else if(!strcmp(keyword,"CT"))
			{
			ct = hex2bin_m(value, &l);
			if (l != ptlen)
				{
				fprintf(stderr, "Inconsistent CT length\n");
				exit(1);
				}
			}
		else if(!strcmp(keyword,"AAD"))
			{
			aad = hex2bin_m(value, &l);
			if (l != aadlen)
				{
				fprintf(stderr, "Inconsistent AAD length\n");
				exit(1);
				}
			}
		else if(!strcmp(keyword,"Tag"))
			{
			tag = hex2bin_m(value, &l);
			if (l != taglen)
				{
				fprintf(stderr, "Inconsistent Tag length\n");
				exit(1);
				}
174
			}
175
		if (encrypt && pt && aad && (iv || encrypt==1))
176 177
			{
			tag = OPENSSL_malloc(taglen);
178
			FIPS_cipherinit(&ctx, gcm, NULL, NULL, 1);
179 180
			/* Relax FIPS constraints for testing */
			M_EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
181
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, 0);
182 183 184 185 186
			if (encrypt == 1)
				{
				static unsigned char iv_fixed[4] = {1,2,3,4};
				if (!iv)
					iv = OPENSSL_malloc(ivlen);
187 188
				FIPS_cipherinit(&ctx, NULL, key, NULL, 1);
				FIPS_cipher_ctx_ctrl(&ctx,
189 190
						EVP_CTRL_GCM_SET_IV_FIXED,
						4, iv_fixed);
191
				if (!FIPS_cipher_ctx_ctrl(&ctx,
192 193 194 195 196
					EVP_CTRL_GCM_IV_GEN, 0, iv))
					{
					fprintf(stderr, "IV gen error\n");
					exit(1);
					}
197
				OutputValue("IV", iv, ivlen, out, 0);
198 199
				}
			else
200
				FIPS_cipherinit(&ctx, NULL, key, iv, 1);
201

202 203

			if (aadlen)
204
				FIPS_cipher(&ctx, NULL, aad, aadlen);
205
			if (ptlen)
206
				{
207
				ct = OPENSSL_malloc(ptlen);
208
				rv = FIPS_cipher(&ctx, ct, pt, ptlen);
209
				}
210 211
			FIPS_cipher(&ctx, NULL, NULL, 0);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG,
212
								taglen, tag);	
213 214
			OutputValue("CT", ct, ptlen, out, 0);
			OutputValue("Tag", tag, taglen, out, 0);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			if (iv)
				OPENSSL_free(iv);
			if (aad)
				OPENSSL_free(aad);
			if (ct)
				OPENSSL_free(ct);
			if (pt)
				OPENSSL_free(pt);
			if (key)
				OPENSSL_free(key);
			if (tag)
				OPENSSL_free(tag);
			iv = aad = ct = pt = key = tag = NULL;
			}	
		if (!encrypt && tag)
			{
231
			FIPS_cipherinit(&ctx, gcm, NULL, NULL, 0);
232 233
			/* Relax FIPS constraints for testing */
			M_EVP_CIPHER_CTX_set_flags(&ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
234 235 236
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, ivlen, 0);
			FIPS_cipherinit(&ctx, NULL, key, iv, 0);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, taglen, tag);
237
			if (aadlen)
238
				FIPS_cipher(&ctx, NULL, aad, aadlen);
239 240 241
			if (ptlen)
				{
				pt = OPENSSL_malloc(ptlen);
242
				rv = FIPS_cipher(&ctx, pt, ct, ptlen);
243
				}
244
			rv = FIPS_cipher(&ctx, NULL, NULL, 0);
245
			if (rv < 0)
246
				fprintf(out, "FAIL" RESP_EOL);
247
			else
248
				OutputValue("PT", pt, ptlen, out, 0);
249 250 251 252 253 254 255 256 257 258 259 260
			if (iv)
				OPENSSL_free(iv);
			if (aad)
				OPENSSL_free(aad);
			if (ct)
				OPENSSL_free(ct);
			if (pt)
				OPENSSL_free(pt);
			if (key)
				OPENSSL_free(key);
			if (tag)
				OPENSSL_free(tag);
261
			iv = aad = ct = pt = key = tag = NULL;
262 263 264 265
			}
		}
	}

266 267
static void xtstest(FILE *in, FILE *out)
	{
268 269
	char buf[204800];
	char lbuf[204800];
270
	char *keyword, *value;
D
Dr. Stephen Henson 已提交
271
	int inlen = 0;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	int encrypt = 0;
	int rv;
	long l;
	unsigned char *key = NULL, *iv = NULL;
	unsigned char *inbuf = NULL, *outbuf = NULL;
	EVP_CIPHER_CTX ctx;
	const EVP_CIPHER *xts = NULL;
	FIPS_cipher_ctx_init(&ctx);

	while(fgets(buf,sizeof buf,in) != NULL)
		{
		fputs(buf,out);
		if (buf[0] == '[' && strlen(buf) >= 9)
			{
			if(!strncmp(buf,"[ENCRYPT]", 9))
				encrypt = 1;
			else if(!strncmp(buf,"[DECRYPT]", 9))
				encrypt = 0;
			}
291
		if  (!parse_line(&keyword, &value, lbuf, buf))
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
			continue;
		else if(!strcmp(keyword,"Key"))
			{
			key = hex2bin_m(value, &l);
			if (l == 32)
				xts = EVP_aes_128_xts();
			else if (l == 64)
				xts = EVP_aes_256_xts();
			else
				{
				fprintf(stderr, "Inconsistent Key length\n");
				exit(1);
				}
			}
		else if(!strcmp(keyword,"i"))
			{
			iv = hex2bin_m(value, &l);
			if (l != 16)
				{
				fprintf(stderr, "Inconsistent i length\n");
				exit(1);
				}
			}
		else if(encrypt && !strcmp(keyword,"PT"))
			{
			inbuf = hex2bin_m(value, &l);
			inlen = l;
			}
		else if(!encrypt && !strcmp(keyword,"CT"))
			{
			inbuf = hex2bin_m(value, &l);
			inlen = l;
			}
		if (inbuf)
			{
			FIPS_cipherinit(&ctx, xts, key, iv, encrypt);
			outbuf = OPENSSL_malloc(inlen);
			rv = FIPS_cipher(&ctx, outbuf, inbuf, inlen);
			OutputValue(encrypt ? "CT":"PT", outbuf, inlen, out, 0);
			OPENSSL_free(inbuf);
			OPENSSL_free(outbuf);
			OPENSSL_free(key);
			OPENSSL_free(iv);
			iv = key = inbuf = outbuf = NULL;
			}	
		}
	}

340
static void ccmtest(FILE *in, FILE *out)
341
	{
342 343
	char buf[200048];
	char lbuf[200048];
344 345 346 347 348 349
	char *keyword, *value;
	long l;
	unsigned char *Key = NULL, *Nonce = NULL;
	unsigned char *Adata = NULL, *Payload = NULL;
	unsigned char *CT = NULL;
	int Plen = -1, Nlen = -1, Tlen = -1, Alen = -1;
350
	int decr = 0;
351 352 353 354 355 356
	EVP_CIPHER_CTX ctx;
	const EVP_CIPHER *ccm = NULL;
	FIPS_cipher_ctx_init(&ctx);

	while(fgets(buf,sizeof buf,in) != NULL)
		{
357
		char *p;
358
		fputs(buf,out);
359
		redo:
360 361 362 363 364 365 366 367 368 369 370
		if (!parse_line(&keyword, &value, lbuf, buf))
			continue;

		/* If surrounded by square brackets zap them */
		if (keyword[0] == '[')
			{
			keyword++;
			p = strchr(value, ']');
			if (p)
				*p = 0;
			}
371 372 373 374 375 376 377 378
		/* See if we have a comma separated list of parameters
		 * if so copy rest of line back to buffer and redo later.
		 */
		p = strchr(value, ',');
		if (p)
			{
			*p = 0;
			strcpy(buf, p + 1);
379
			strcat(buf, "\n");
380 381
			decr = 1;
			}
382 383 384 385 386 387 388 389
		if (!strcmp(keyword,"Plen"))
			Plen = atoi(value);
		else if (!strcmp(keyword,"Nlen"))
			Nlen = atoi(value);
		else if (!strcmp(keyword,"Tlen"))
			Tlen = atoi(value);
		else if (!strcmp(keyword,"Alen"))
			Alen = atoi(value);
390 391 392
		if (p)
			goto redo;
		if (!strcmp(keyword,"Key"))
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
			{
			if (Key)
				OPENSSL_free(Key);
			Key = hex2bin_m(value, &l);
			if (l == 16)
				ccm = EVP_aes_128_ccm();
			else if (l == 24)
				ccm = EVP_aes_192_ccm();
			else if (l == 32)
				ccm = EVP_aes_256_ccm();
			else
				{
				fprintf(stderr, "Inconsistent Key length\n");
				exit(1);
				}
			}
		else if (!strcmp(keyword,"Nonce"))
			{
			if (Nonce)
				OPENSSL_free(Nonce);
			Nonce = hex2bin_m(value, &l);
			if (l != Nlen)
				{
				fprintf(stderr, "Inconsistent nonce length\n");
				exit(1);
				}
			}
420
		else if (!strcmp(keyword,"Payload") && !decr)
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
			{
			Payload = hex2bin_m(value, &l);
			if (Plen && l != Plen)
				{
				fprintf(stderr, "Inconsistent Payload length\n");
				exit(1);
				}
			}
		else if (!strcmp(keyword,"Adata"))
			{
			Adata = hex2bin_m(value, &l);
			if (Alen && l != Alen)
				{
				fprintf(stderr, "Inconsistent Payload length\n");
				exit(1);
				}
			}
438 439 440 441 442 443 444 445 446
		else if (!strcmp(keyword,"CT") && decr)
			{
			CT = hex2bin_m(value, &l);
			if (l != (Plen + Tlen))
				{
				fprintf(stderr, "Inconsistent CT length\n");
				exit(1);
				}
			}
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
		if (Payload)
			{
			FIPS_cipherinit(&ctx, ccm, NULL, NULL, 1);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, Nlen, 0);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG, Tlen, 0);
			FIPS_cipherinit(&ctx, NULL, Key, Nonce, 1);

			FIPS_cipher(&ctx, NULL, NULL, Plen);
			FIPS_cipher(&ctx, NULL, Adata, Alen);
			CT = OPENSSL_malloc(Plen + Tlen);
			FIPS_cipher(&ctx, CT, Payload, Plen);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_GET_TAG, Tlen,
						CT + Plen);
			OutputValue("CT", CT, Plen + Tlen, out, 0);
			OPENSSL_free(CT);
			OPENSSL_free(Payload);
			CT = Payload = NULL;
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
			}
		if (CT)
			{
			int rv;
			int len = Plen == 0 ? 1: Plen;
			FIPS_cipherinit(&ctx, ccm, NULL, NULL, 0);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_IVLEN, Nlen, 0);
			FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_CCM_SET_TAG,
						Tlen, CT + Plen);
			FIPS_cipherinit(&ctx, NULL, Key, Nonce, 0);
			FIPS_cipher(&ctx, NULL, NULL, Plen);
			FIPS_cipher(&ctx, NULL, Adata, Alen);
			Payload = OPENSSL_malloc(len);
			rv = FIPS_cipher(&ctx, Payload, CT, Plen);
			if (rv >= 0)
				{
				if (rv == 0)
					Payload[0] = 0;
482
				fputs("Result = Pass" RESP_EOL, out);
483 484 485
				OutputValue("Payload", Payload, len, out, 0);
				}
			else
486
				fputs("Result = Fail" RESP_EOL, out);
487 488 489 490
			OPENSSL_free(CT);
			OPENSSL_free(Payload);
			CT = Payload = NULL;
			}
491 492 493 494 495 496 497 498
		}
	if (Key)
		OPENSSL_free(Key);
	if (Nonce)
		OPENSSL_free(Nonce);
	FIPS_cipher_ctx_cleanup(&ctx);
	}

499 500 501 502 503
#ifdef FIPS_ALGVS
int fips_gcmtest_main(int argc, char **argv)
#else
int main(int argc, char **argv)
#endif
504 505
	{
	int encrypt;
506
	int xts = 0, ccm = 0;
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	FILE *in, *out;
	if (argc == 4)
		{
		in = fopen(argv[2], "r");
		if (!in)
			{
			fprintf(stderr, "Error opening input file\n");
			exit(1);
			}
		out = fopen(argv[3], "w");
		if (!out)
			{
			fprintf(stderr, "Error opening output file\n");
			exit(1);
			}
		}
	else if (argc == 2)
		{
		in = stdin;
		out = stdout;
		}
	else
529 530 531 532
		{
		fprintf(stderr,"%s [-encrypt|-decrypt]\n",argv[0]);
		exit(1);
		}
533
	fips_algtest_init();
534 535
	if(!strcmp(argv[1],"-encrypt"))
		encrypt = 1;
536 537
	else if(!strcmp(argv[1],"-encryptIVext"))
		encrypt = 2;
538 539
	else if(!strcmp(argv[1],"-decrypt"))
		encrypt = 0;
540 541
	else if(!strcmp(argv[1],"-ccm"))
		ccm = 1;
542 543
	else if(!strcmp(argv[1],"-xts"))
		xts = 1;
544 545 546 547 548 549
	else
		{
		fprintf(stderr,"Don't know how to %s.\n",argv[1]);
		exit(1);
		}

550 551
	if (ccm)
		ccmtest(in, out);
552
	else if (xts)
553 554 555
		xtstest(in, out);
	else
		gcmtest(in, out, encrypt);
556 557 558 559 560 561

	if (argc == 4)
		{
		fclose(in);
		fclose(out);
		}
562 563 564 565 566

	return 0;
}

#endif