fips_utl.h 11.6 KB
Newer Older
D
Dr. Stephen Henson 已提交
1
/* ====================================================================
2
 * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
D
Dr. Stephen Henson 已提交
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
 *
 * 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.
 *
 */

50 51 52
#ifndef FIPS_UTL_H
#define FIPS_UTL_H

53 54
#define OPENSSL_FIPSAPI

55 56 57
#include <openssl/fips_rand.h>
#include <openssl/objects.h>

58 59 60 61 62 63
#ifdef OPENSSL_SYS_WIN32
#define RESP_EOL	"\n"
#else
#define RESP_EOL	"\r\n"
#endif

64 65 66 67 68 69 70 71 72
#ifndef FIPS_AUTH_OFFICER_PASS
#define FIPS_AUTH_OFFICER_PASS	"Default FIPS Crypto Officer Password"
#endif

#ifndef FIPS_AUTH_USER_PASS
#define FIPS_AUTH_USER_PASS	"Default FIPS Crypto User Password"
#endif


D
Dr. Stephen Henson 已提交
73 74 75
int hex2bin(const char *in, unsigned char *out);
unsigned char *hex2bin_m(const char *in, long *plen);
int do_hex2bn(BIGNUM **pr, const char *in);
76 77
int do_bn_print(FILE *out, const BIGNUM *bn);
int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
D
Dr. Stephen Henson 已提交
78
int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
79
int parse_line2(char **pkw, char **pval, char *linebuf, char *olinebuf, int eol);
D
Dr. Stephen Henson 已提交
80 81
BIGNUM *hex2bn(const char *in);
int tidy_line(char *linebuf, char *olinebuf);
82
int copy_line(const char *in, FILE *ofp);
D
Dr. Stephen Henson 已提交
83 84 85 86
int bint2bin(const char *in, int len, unsigned char *out);
int bin2bint(const unsigned char *in,int len,char *out);
void PrintValue(char *tag, unsigned char *val, int len);
void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode);
87
void fips_algtest_init(void);
88
void do_entropy_stick(void);
89 90
int fips_strncasecmp(const char *str1, const char *str2, size_t n);
int fips_strcasecmp(const char *str1, const char *str2);
D
Dr. Stephen Henson 已提交
91 92 93 94 95

static int no_err;

static void put_err_cb(int lib, int func,int reason,const char *file,int line)
	{
96 97 98
	if (no_err)
		return;
	fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
D
Dr. Stephen Henson 已提交
99
				":file=%s:line=%d\n",
R
Richard Levitte 已提交
100
			ERR_PACK(lib, func, reason),
D
Dr. Stephen Henson 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
			lib, func, reason, file, line);
	}

static void add_err_cb(int num, va_list args)
	{
	int i;
	char *str;
	if (no_err)
		return;
	fputs("\t", stderr);
	for (i = 0; i < num; i++)
		{
		str = va_arg(args, char *);
		if (str)
			fputs(str, stderr);
		}
	fputs("\n", stderr);
	}

120 121 122 123 124 125 126 127 128 129 130 131 132
/* Dummy Entropy to keep DRBG happy. WARNING: THIS IS TOTALLY BOGUS
 * HAS ZERO SECURITY AND MUST NOT BE USED IN REAL APPLICATIONS.
 */

static unsigned char dummy_entropy[1024];

static size_t dummy_cb(DRBG_CTX *ctx, unsigned char **pout,
                                int entropy, size_t min_len, size_t max_len)
	{
	*pout = dummy_entropy;
	return min_len;
	}

133 134
static int entropy_stick = 0;

135
static void fips_algtest_init_nofips(void)
D
Dr. Stephen Henson 已提交
136
	{
137
	DRBG_CTX *ctx;
138
	size_t i;
D
Dr. Stephen Henson 已提交
139
	FIPS_set_error_callbacks(put_err_cb, add_err_cb);
140 141 142 143
	for (i = 0; i < sizeof(dummy_entropy); i++)
		dummy_entropy[i] = i & 0xff;
	if (entropy_stick)
		memcpy(dummy_entropy + 32, dummy_entropy + 16, 16);
144 145
	ctx = FIPS_get_default_drbg();
	FIPS_drbg_init(ctx, NID_aes_256_ctr, DRBG_FLAG_CTR_USE_DF);
146
	FIPS_drbg_set_callbacks(ctx, dummy_cb, 0, 16, dummy_cb, 0);
147
	FIPS_drbg_instantiate(ctx, dummy_entropy, 10);
148
	FIPS_rand_set_method(FIPS_drbg_method());
D
Dr. Stephen Henson 已提交
149 150
	}

151 152 153 154 155
void do_entropy_stick(void)
	{
	entropy_stick = 1;
	}

156 157
void fips_algtest_init(void)
	{
158
	fips_algtest_init_nofips();
159
	if (!FIPS_module_mode_set(1, FIPS_AUTH_USER_PASS))
160 161 162 163 164 165
		{
		fprintf(stderr, "Error entering FIPS mode\n");
		exit(1);
		}
	}

D
Dr. Stephen Henson 已提交
166 167
int hex2bin(const char *in, unsigned char *out)
    {
168
    int n1, n2, isodd = 0;
D
Dr. Stephen Henson 已提交
169 170
    unsigned char ch;

171 172 173 174 175 176 177
    n1 = strlen(in);
    if (in[n1 - 1] == '\n')
	n1--;

    if (n1 & 1)
	isodd = 1;

D
Dr. Stephen Henson 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
	{ /* first byte */
	if ((in[n1] >= '0') && (in[n1] <= '9'))
	    ch = in[n1++] - '0';
	else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
	    ch = in[n1++] - 'A' + 10;
	else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
	    ch = in[n1++] - 'a' + 10;
	else
	    return -1;
	if(!in[n1])
	    {
	    out[n2++]=ch;
	    break;
	    }
193 194 195 196
	/* If input is odd length first digit is least significant: assumes
	 * all digits valid hex and null terminated which is true for the
	 * strings we pass.
	 */
197
	if (n1 == 1 && isodd)
198 199 200 201
		{
		out[n2++] = ch;
		continue;
		}
D
Dr. Stephen Henson 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	out[n2] = ch << 4;
	/* second byte */
	if ((in[n1] >= '0') && (in[n1] <= '9'))
	    ch = in[n1++] - '0';
	else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
	    ch = in[n1++] - 'A' + 10;
	else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
	    ch = in[n1++] - 'a' + 10;
	else
	    return -1;
	out[n2++] |= ch;
	}
    return n2;
    }

unsigned char *hex2bin_m(const char *in, long *plen)
	{
	unsigned char *p;
220 221 222 223 224
	if (strlen(in) == 0)
		{
		*plen = 0;
		return OPENSSL_malloc(1);
		}
D
Dr. Stephen Henson 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	p = OPENSSL_malloc((strlen(in) + 1)/2);
	*plen = hex2bin(in, p);
	return p;
	}

int do_hex2bn(BIGNUM **pr, const char *in)
	{
	unsigned char *p;
	long plen;
	int r = 0;
	p = hex2bin_m(in, &plen);
	if (!p)
		return 0;
	if (!*pr)
		*pr = BN_new();
	if (!*pr)
		return 0;
	if (BN_bin2bn(p, plen, *pr))
		r = 1;
	OPENSSL_free(p);
	return r;
	}

248
int do_bn_print(FILE *out, const BIGNUM *bn)
D
Dr. Stephen Henson 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	{
	int len, i;
	unsigned char *tmp;
	len = BN_num_bytes(bn);
	if (len == 0)
		{
		fputs("00", out);
		return 1;
		}

	tmp = OPENSSL_malloc(len);
	if (!tmp)
		{
		fprintf(stderr, "Memory allocation error\n");
		return 0;
		}
	BN_bn2bin(bn, tmp);
	for (i = 0; i < len; i++)
		fprintf(out, "%02x", tmp[i]);
	OPENSSL_free(tmp);
	return 1;
	}

272
int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
D
Dr. Stephen Henson 已提交
273 274 275 276 277 278
	{
	int r;
	fprintf(out, "%s = ", name);
	r = do_bn_print(out, bn);
	if (!r)
		return 0;
279
	fputs(RESP_EOL, out);
D
Dr. Stephen Henson 已提交
280 281 282 283
	return 1;
	}

int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
284 285 286 287 288
	{
	return parse_line2(pkw, pval, linebuf, olinebuf, 1);
	}

int parse_line2(char **pkw, char **pval, char *linebuf, char *olinebuf, int eol)
D
Dr. Stephen Henson 已提交
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 319
	{
	char *keyword, *value, *p, *q;
	strcpy(linebuf, olinebuf);
	keyword = linebuf;
	/* Skip leading space */
	while (isspace((unsigned char)*keyword))
		keyword++;

	/* Look for = sign */
	p = strchr(linebuf, '=');

	/* If no '=' exit */
	if (!p)
		return 0;

	q = p - 1;

	/* Remove trailing space */
	while (isspace((unsigned char)*q))
		*q-- = 0;

	*p = 0;
	value = p + 1;

	/* Remove leading space from value */
	while (isspace((unsigned char)*value))
		value++;

	/* Remove trailing space from value */
	p = value + strlen(value) - 1;

320
	if (eol && *p != '\n')
321 322
		fprintf(stderr, "Warning: missing EOL\n");

D
Dr. Stephen Henson 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	while (*p == '\n' || isspace((unsigned char)*p))
		*p-- = 0;

	*pkw = keyword;
	*pval = value;
	return 1;
	}

BIGNUM *hex2bn(const char *in)
    {
    BIGNUM *p=NULL;

    if (!do_hex2bn(&p, in))
	return NULL;

    return p;
    }

/* To avoid extensive changes to test program at this stage just convert
 * the input line into an acceptable form. Keyword lines converted to form
 * "keyword = value\n" no matter what white space present, all other lines
 * just have leading and trailing space removed.
 */

int tidy_line(char *linebuf, char *olinebuf)
	{
	char *keyword, *value, *p, *q;
	strcpy(linebuf, olinebuf);
	keyword = linebuf;
	/* Skip leading space */
	while (isspace((unsigned char)*keyword))
		keyword++;
	/* Look for = sign */
	p = strchr(linebuf, '=');

	/* If no '=' just chop leading, trailing ws */
	if (!p)
		{
		p = keyword + strlen(keyword) - 1;
		while (*p == '\n' || isspace((unsigned char)*p))
			*p-- = 0;
		strcpy(olinebuf, keyword);
		strcat(olinebuf, "\n");
		return 1;
		}

	q = p - 1;

	/* Remove trailing space */
	while (isspace((unsigned char)*q))
		*q-- = 0;

	*p = 0;
	value = p + 1;

	/* Remove leading space from value */
	while (isspace((unsigned char)*value))
		value++;

	/* Remove trailing space from value */
	p = value + strlen(value) - 1;

	while (*p == '\n' || isspace((unsigned char)*p))
		*p-- = 0;

	strcpy(olinebuf, keyword);
	strcat(olinebuf, " = ");
	strcat(olinebuf, value);
	strcat(olinebuf, "\n");

	return 1;
	}
395 396 397 398 399 400 401 402 403 404 405 406 407 408
/* Copy supplied line to ofp replacing \n with \r\n */
int copy_line(const char *in, FILE *ofp)
	{
	const char *p;
	p = strchr(in, '\n');
	if (p)
		{
		fwrite(in, 1, (size_t)(p - in), ofp);
		fputs(RESP_EOL, ofp);
		}
	else
		fputs(in, ofp);
	return 1;
	}
D
Dr. Stephen Henson 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

/* NB: this return the number of _bits_ read */
int bint2bin(const char *in, int len, unsigned char *out)
    {
    int n;

    memset(out,0,len);
    for(n=0 ; n < len ; ++n)
	if(in[n] == '1')
	    out[n/8]|=(0x80 >> (n%8));
    return len;
    }

int bin2bint(const unsigned char *in,int len,char *out)
    {
    int n;

    for(n=0 ; n < len ; ++n)
	out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
    return n;
    }

/*-----------------------------------------------*/

void PrintValue(char *tag, unsigned char *val, int len)
{
435 436
#ifdef VERBOSE
	OutputValue(tag, val, len, stdout, 0);
D
Dr. Stephen Henson 已提交
437 438 439 440 441 442 443 444 445
#endif
}

void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
    {
    char obuf[2048];
    int olen;

    if(bitmode)
446
	{
D
Dr. Stephen Henson 已提交
447
	olen=bin2bint(val,len,obuf);
448
    	fprintf(rfp, "%s = %.*s" RESP_EOL, tag, olen, obuf);
449
	}
D
Dr. Stephen Henson 已提交
450
    else
451 452 453 454 455
	{
	int i;
    	fprintf(rfp, "%s = ", tag);
	for (i = 0; i < len; i++)
		fprintf(rfp, "%02x", val[i]);
456
	fputs(RESP_EOL, rfp);
457
	}
D
Dr. Stephen Henson 已提交
458 459 460 461 462 463

#if VERBOSE
    printf("%s = %.*s\n", tag, olen, obuf);
#endif
    }

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
/* Not all platforms support strcasecmp and strncasecmp: implement versions
 * in here to avoid need to include them in the validated module. Taken
 * from crypto/o_str.c written by Richard Levitte (richard@levitte.org)
 */

int fips_strncasecmp(const char *str1, const char *str2, size_t n)
	{
	while (*str1 && *str2 && n)
		{
		int res = toupper(*str1) - toupper(*str2);
		if (res) return res < 0 ? -1 : 1;
		str1++;
		str2++;
		n--;
		}
	if (n == 0)
		return 0;
	if (*str1)
		return 1;
	if (*str2)
		return -1;
	return 0;
	}

int fips_strcasecmp(const char *str1, const char *str2)
	{
	return fips_strncasecmp(str1, str2, (size_t)-1);
	}

493 494

#endif