fips_drbgvs.c 9.0 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
/* fips/rand/fips_drbgvs.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)
{
63
    printf("No FIPS DRBG support\n");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    return(0);
}
#else

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

#include "fips_utl.h"

79
static int dparse_md(char *str)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	{
	switch(atoi(str + 5))
		{
		case 1:
		return NID_sha1;

		case 224:
		return NID_sha224;

		case 256:
		return NID_sha256;

		case 384:
		return NID_sha384;

		case 512:
		return NID_sha512;

		}

	return NID_undef;
	}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static int parse_ec(char *str)
	{
	int curve_nid, md_nid;
	char *md;
	md = strchr(str, ' ');
	if (!md)
		return NID_undef;
	if (!strncmp(str, "[P-256", 6))
		curve_nid = NID_X9_62_prime256v1;
	else if (!strncmp(str, "[P-384", 6))
		curve_nid = NID_secp384r1;
	else if (!strncmp(str, "[P-521", 6))
		curve_nid = NID_secp521r1;
	else
		return NID_undef;
118
	md_nid = dparse_md(md);
119 120 121 122 123
	if (md_nid == NID_undef)
		return NID_undef;
	return (curve_nid << 16) | md_nid;
	}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static int parse_aes(char *str, int *pdf)
	{

	if (!strncmp(str + 9, "no", 2))
		*pdf = 0;
	else
		*pdf = DRBG_FLAG_CTR_USE_DF;

	switch(atoi(str + 5))
		{
		case 128:
		return NID_aes_128_ctr;

		case 192:
		return NID_aes_192_ctr;

		case 256:
		return NID_aes_256_ctr;

		default:
		return NID_undef;

		}
	}

typedef struct 
	{
	unsigned char *ent;
	size_t entlen;
	unsigned char *nonce;
	size_t noncelen;
	} TEST_ENT;

157
static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
158 159 160
                                int entropy, size_t min_len, size_t max_len)
	{
	TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
161
	*pout = (unsigned char *)t->ent;
162 163 164
	return t->entlen;
	}

165
static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
166 167 168
                                int entropy, size_t min_len, size_t max_len)
	{
	TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
169
	*pout = (unsigned char *)t->nonce;
170 171 172
	return t->noncelen;
	}

173 174 175
#ifdef FIPS_ALGVS
int fips_drbgvs_main(int argc,char **argv)
#else
176
int main(int argc,char **argv)
177
#endif
178
	{
179
	FILE *in, *out;
180
	DRBG_CTX *dctx = NULL;
181
	TEST_ENT t;
182
	int r, nid = 0;
183 184
	int pr = 0;
	char buf[2048], lbuf[2048];
185
	unsigned char randout[2048];
186 187 188 189
	char *keyword = NULL, *value = NULL;

	unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
	long entlen, noncelen, perslen, adinlen;
190
	int df = 0;
191

D
Dr. Stephen Henson 已提交
192 193 194
	enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC }
		drbg_type = DRBG_NONE;

195
	int randoutlen = 0;
196 197 198

	int gen = 0;

199
	fips_algtest_init();
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

	if (argc == 3)
		{
		in = fopen(argv[1], "r");
		if (!in)
			{
			fprintf(stderr, "Error opening input file\n");
			exit(1);
			}
		out = fopen(argv[2], "w");
		if (!out)
			{
			fprintf(stderr, "Error opening output file\n");
			exit(1);
			}
		}
	else if (argc == 1)
		{
		in = stdin;
		out = stdout;
		}
	else
		{
		fprintf(stderr,"%s (infile outfile)\n",argv[0]);
		exit(1);
		}

	while (fgets(buf, sizeof(buf), in) != NULL)
228
		{
229
		fputs(buf, out);
D
Dr. Stephen Henson 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242
		if (drbg_type == DRBG_NONE)
			{
			if (strstr(buf, "CTR_DRBG"))
				drbg_type = DRBG_CTR;
			else if (strstr(buf, "Hash_DRBG"))
				drbg_type = DRBG_HASH;
			else if (strstr(buf, "HMAC_DRBG"))
				drbg_type = DRBG_HMAC;
			else if (strstr(buf, "Dual_EC_DRBG"))
				drbg_type = DRBG_DUAL_EC;
			else
				continue;
			}
243 244
		if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
			{
245
			nid = dparse_md(buf);
246 247
			if (nid == NID_undef)
				exit(1);
D
Dr. Stephen Henson 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
			if (drbg_type == DRBG_HMAC)
				{
				switch (nid)
					{
					case NID_sha1:
					nid = NID_hmacWithSHA1;
					break;

					case NID_sha224:
					nid = NID_hmacWithSHA224;
					break;

					case NID_sha256:
					nid = NID_hmacWithSHA256;
					break;

					case NID_sha384:
					nid = NID_hmacWithSHA384;
					break;

					case NID_sha512:
					nid = NID_hmacWithSHA512;
					break;

					default:
					exit(1);
					}
				}
276 277 278 279 280 281 282
			}
		if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
			{
			nid = parse_aes(buf, &df);
			if (nid == NID_undef)
				exit(1);
			}
283 284 285 286 287 288
		if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3))
			{
			nid = parse_ec(buf);
			if (nid == NID_undef)
				exit(1);
			}
289 290 291 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
		if (!parse_line(&keyword, &value, lbuf, buf))
			continue;

		if (!strcmp(keyword, "[PredictionResistance"))
			{
			if (!strcmp(value, "True]"))
				pr = 1;
			else if (!strcmp(value, "False]"))
				pr = 0;
			else
				exit(1);
			}

		if (!strcmp(keyword, "EntropyInput"))
			{
			ent = hex2bin_m(value, &entlen);
			t.ent = ent;
			t.entlen = entlen;
			}

		if (!strcmp(keyword, "Nonce"))
			{
			nonce = hex2bin_m(value, &noncelen);
			t.nonce = nonce;
			t.noncelen = noncelen;
			}

		if (!strcmp(keyword, "PersonalizationString"))
			{
			pers = hex2bin_m(value, &perslen);
319 320 321 322 323
			if (nid == 0)
				{
				fprintf(stderr, "DRBG type not recognised!\n");
				exit (1);
				}
324
			dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
325 326
			if (!dctx)
				exit (1);
327
			FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
328
							test_nonce, 0);
329
			FIPS_drbg_set_app_data(dctx, &t);
330
			randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
331
			r = FIPS_drbg_instantiate(dctx, pers, perslen);
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
			if (!r)
				{
				fprintf(stderr, "Error instantiating DRBG\n");
				exit(1);
				}
			OPENSSL_free(pers);
			OPENSSL_free(ent);
			OPENSSL_free(nonce);
			ent = nonce = pers = NULL;
			gen = 0;
			}

		if (!strcmp(keyword, "AdditionalInput"))
			{
			adin = hex2bin_m(value, &adinlen);
			if (pr)
				continue;
349
			r = FIPS_drbg_generate(dctx, randout, randoutlen, 0,
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
								adin, adinlen);
			if (!r)
				{
				fprintf(stderr, "Error generating DRBG bits\n");
				exit(1);
				}
			if (!r)
				exit(1);
			OPENSSL_free(adin);
			adin = NULL;
			gen++;
			}

		if (pr)
			{
			if (!strcmp(keyword, "EntropyInputPR"))
				{
				ent = hex2bin_m(value, &entlen);
				t.ent = ent;
				t.entlen = entlen;
370
				r = FIPS_drbg_generate(dctx,
371
							randout, randoutlen,
372
							1, adin, adinlen);
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
				if (!r)
					{
					fprintf(stderr,
						"Error generating DRBG bits\n");
					exit(1);
					}
				OPENSSL_free(adin);
				OPENSSL_free(ent);
				adin = ent = NULL;
				gen++;
				}
			}
		if (!strcmp(keyword, "EntropyInputReseed"))
			{
			ent = hex2bin_m(value, &entlen);
			t.ent = ent;
			t.entlen = entlen;
			}
		if (!strcmp(keyword, "AdditionalInputReseed"))
			{
			adin = hex2bin_m(value, &adinlen);
			FIPS_drbg_reseed(dctx, adin, adinlen);
			OPENSSL_free(ent);
			OPENSSL_free(adin);
			ent = adin = NULL;
			}
		if (gen == 2)
			{
401 402
			OutputValue("ReturnedBits", randout, randoutlen,
									out, 0);
403 404
			FIPS_drbg_free(dctx);
			dctx = NULL;
405 406 407 408 409 410 411 412
			gen = 0;
			}

		}
	return 0;
	}

#endif