fips_rand.c 10.7 KB
Newer Older
D
Dr. Stephen Henson 已提交
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
/* ====================================================================
 * Copyright (c) 2007 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.
 *
 */

50
#define OPENSSL_FIPSAPI
51

D
Dr. Stephen Henson 已提交
52 53 54
/*
 * This is a FIPS approved AES PRNG based on ANSI X9.31 A.2.4.
 */
55
#include <openssl/crypto.h>
D
Dr. Stephen Henson 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
#include "e_os.h"

/* If we don't define _XOPEN_SOURCE_EXTENDED, struct timeval won't
   be defined and gettimeofday() won't be declared with strict compilers
   like DEC C in ANSI C mode.  */
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED 1
#endif

#include <openssl/rand.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/fips_rand.h>
69 70 71 72 73
#if !(defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS))
# include <sys/time.h>
#endif
#if defined(OPENSSL_SYS_VXWORKS)
# include <time.h>
D
Dr. Stephen Henson 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#endif
#include <assert.h>
#ifndef OPENSSL_SYS_WIN32
# ifdef OPENSSL_UNISTD
#  include OPENSSL_UNISTD
# else
#  include <unistd.h>
# endif
#endif
#include <string.h>
#include <openssl/fips.h>
#include "fips_locl.h"

#ifdef OPENSSL_FIPS

void *OPENSSL_stderr(void);

#define AES_BLOCK_LENGTH	16


/* AES FIPS PRNG implementation */

typedef struct 
	{
	int seeded;
	int keyed;
	int test_mode;
	int second;
	int error;
	unsigned long counter;
	AES_KEY ks;
	int vpos;
	/* Temporary storage for key if it equals seed length */
	unsigned char tmp_key[AES_BLOCK_LENGTH];
	unsigned char V[AES_BLOCK_LENGTH];
	unsigned char DT[AES_BLOCK_LENGTH];
	unsigned char last[AES_BLOCK_LENGTH];
	} FIPS_PRNG_CTX;

static FIPS_PRNG_CTX sctx;

static int fips_prng_fail = 0;

117
void FIPS_x931_stick(void)
D
Dr. Stephen Henson 已提交
118 119 120 121 122 123 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	{
	fips_prng_fail = 1;
	}

static void fips_rand_prng_reset(FIPS_PRNG_CTX *ctx)
	{
	ctx->seeded = 0;
	ctx->keyed = 0;
	ctx->test_mode = 0;
	ctx->counter = 0;
	ctx->second = 0;
	ctx->error = 0;
	ctx->vpos = 0;
	OPENSSL_cleanse(ctx->V, AES_BLOCK_LENGTH);
	OPENSSL_cleanse(&ctx->ks, sizeof(AES_KEY));
	}
	

static int fips_set_prng_key(FIPS_PRNG_CTX *ctx,
			const unsigned char *key, unsigned int keylen)
	{
	FIPS_selftest_check();
	if (keylen != 16 && keylen != 24 && keylen != 32)
		{
		/* error: invalid key size */
		return 0;
		}
	AES_set_encrypt_key(key, keylen << 3, &ctx->ks);
	if (keylen == 16)
		{
		memcpy(ctx->tmp_key, key, 16);
		ctx->keyed = 2;
		}
	else
		ctx->keyed = 1;
	ctx->seeded = 0;
	ctx->second = 0;
	return 1;
	}

static int fips_set_prng_seed(FIPS_PRNG_CTX *ctx,
			const unsigned char *seed, unsigned int seedlen)
	{
	unsigned int i;
	if (!ctx->keyed)
		return 0;
	/* In test mode seed is just supplied data */
	if (ctx->test_mode)
		{
		if (seedlen != AES_BLOCK_LENGTH)
			return 0;
		memcpy(ctx->V, seed, AES_BLOCK_LENGTH);
		ctx->seeded = 1;
		return 1;
		}
	/* Outside test mode XOR supplied data with existing seed */
	for (i = 0; i < seedlen; i++)
		{
		ctx->V[ctx->vpos++] ^= seed[i];
		if (ctx->vpos == AES_BLOCK_LENGTH)
			{
			ctx->vpos = 0;
			/* Special case if first seed and key length equals
 			 * block size check key and seed do not match.
 			 */ 
			if (ctx->keyed == 2)
				{
				if (!memcmp(ctx->tmp_key, ctx->V, 16))
					{
					RANDerr(RAND_F_FIPS_SET_PRNG_SEED,
						RAND_R_PRNG_SEED_MUST_NOT_MATCH_KEY);
					return 0;
					}
				OPENSSL_cleanse(ctx->tmp_key, 16);
				ctx->keyed = 1;
				}
			ctx->seeded = 1;
			}
		}
	return 1;
	}

static int fips_set_test_mode(FIPS_PRNG_CTX *ctx)
	{
	if (ctx->keyed)
		{
		RANDerr(RAND_F_FIPS_SET_TEST_MODE,RAND_R_PRNG_KEYED);
		return 0;
		}
	ctx->test_mode = 1;
	return 1;
	}

211
int FIPS_x931_test_mode(void)
D
Dr. Stephen Henson 已提交
212 213 214 215
	{
	return fips_set_test_mode(&sctx);
	}

216
int FIPS_x931_set_dt(unsigned char *dt)
D
Dr. Stephen Henson 已提交
217 218 219
	{
	if (!sctx.test_mode)
		{
220
		RANDerr(RAND_F_FIPS_X931_SET_DT,RAND_R_NOT_IN_TEST_MODE);
D
Dr. Stephen Henson 已提交
221 222 223 224 225 226
		return 0;
		}
	memcpy(sctx.DT, dt, AES_BLOCK_LENGTH);
	return 1;
	}

227 228
void FIPS_get_timevec(unsigned char *buf, unsigned long *pctr)
	{
D
Dr. Stephen Henson 已提交
229 230
#ifdef OPENSSL_SYS_WIN32
	FILETIME ft;
231 232
#elif defined(OPENSSL_SYS_VXWORKS)
        struct timespec ts;
D
Dr. Stephen Henson 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
#else
	struct timeval tv;
#endif

#ifndef GETPID_IS_MEANINGLESS
	unsigned long pid;
#endif

#ifdef OPENSSL_SYS_WIN32
	GetSystemTimeAsFileTime(&ft);
	buf[0] = (unsigned char) (ft.dwHighDateTime & 0xff);
	buf[1] = (unsigned char) ((ft.dwHighDateTime >> 8) & 0xff);
	buf[2] = (unsigned char) ((ft.dwHighDateTime >> 16) & 0xff);
	buf[3] = (unsigned char) ((ft.dwHighDateTime >> 24) & 0xff);
	buf[4] = (unsigned char) (ft.dwLowDateTime & 0xff);
	buf[5] = (unsigned char) ((ft.dwLowDateTime >> 8) & 0xff);
	buf[6] = (unsigned char) ((ft.dwLowDateTime >> 16) & 0xff);
	buf[7] = (unsigned char) ((ft.dwLowDateTime >> 24) & 0xff);
251 252 253 254 255 256 257 258 259 260
#elif defined(OPENSSL_SYS_VXWORKS)
	clock_gettime(CLOCK_REALTIME, &ts);
	buf[0] = (unsigned char) (ts.tv_sec & 0xff);
	buf[1] = (unsigned char) ((ts.tv_sec >> 8) & 0xff);
	buf[2] = (unsigned char) ((ts.tv_sec >> 16) & 0xff);
	buf[3] = (unsigned char) ((ts.tv_sec >> 24) & 0xff);
	buf[4] = (unsigned char) (ts.tv_nsec & 0xff);
	buf[5] = (unsigned char) ((ts.tv_nsec >> 8) & 0xff);
	buf[6] = (unsigned char) ((ts.tv_nsec >> 16) & 0xff);
	buf[7] = (unsigned char) ((ts.tv_nsec >> 24) & 0xff);
D
Dr. Stephen Henson 已提交
261 262 263 264 265 266 267 268 269 270 271
#else
	gettimeofday(&tv,NULL);
	buf[0] = (unsigned char) (tv.tv_sec & 0xff);
	buf[1] = (unsigned char) ((tv.tv_sec >> 8) & 0xff);
	buf[2] = (unsigned char) ((tv.tv_sec >> 16) & 0xff);
	buf[3] = (unsigned char) ((tv.tv_sec >> 24) & 0xff);
	buf[4] = (unsigned char) (tv.tv_usec & 0xff);
	buf[5] = (unsigned char) ((tv.tv_usec >> 8) & 0xff);
	buf[6] = (unsigned char) ((tv.tv_usec >> 16) & 0xff);
	buf[7] = (unsigned char) ((tv.tv_usec >> 24) & 0xff);
#endif
272 273 274 275
	buf[8] = (unsigned char) (*pctr & 0xff);
	buf[9] = (unsigned char) ((*pctr >> 8) & 0xff);
	buf[10] = (unsigned char) ((*pctr >> 16) & 0xff);
	buf[11] = (unsigned char) ((*pctr >> 24) & 0xff);
D
Dr. Stephen Henson 已提交
276

277
	(*pctr)++;
D
Dr. Stephen Henson 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312


#ifndef GETPID_IS_MEANINGLESS
	pid=(unsigned long)getpid();
	buf[12] = (unsigned char) (pid & 0xff);
	buf[13] = (unsigned char) ((pid >> 8) & 0xff);
	buf[14] = (unsigned char) ((pid >> 16) & 0xff);
	buf[15] = (unsigned char) ((pid >> 24) & 0xff);
#endif
    }

static int fips_rand(FIPS_PRNG_CTX *ctx,
			unsigned char *out, unsigned int outlen)
	{
	unsigned char R[AES_BLOCK_LENGTH], I[AES_BLOCK_LENGTH];
	unsigned char tmp[AES_BLOCK_LENGTH];
	int i;
	if (ctx->error)
		{
		RANDerr(RAND_F_FIPS_RAND,RAND_R_PRNG_ERROR);
		return 0;
		}
	if (!ctx->keyed)
		{
		RANDerr(RAND_F_FIPS_RAND,RAND_R_NO_KEY_SET);
		return 0;
		}
	if (!ctx->seeded)
		{
		RANDerr(RAND_F_FIPS_RAND,RAND_R_PRNG_NOT_SEEDED);
		return 0;
		}
	for (;;)
		{
		if (!ctx->test_mode)
313
			FIPS_get_timevec(ctx->DT, &ctx->counter);
D
Dr. Stephen Henson 已提交
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
		AES_encrypt(ctx->DT, I, &ctx->ks);
		for (i = 0; i < AES_BLOCK_LENGTH; i++)
			tmp[i] = I[i] ^ ctx->V[i];
		AES_encrypt(tmp, R, &ctx->ks);
		for (i = 0; i < AES_BLOCK_LENGTH; i++)
			tmp[i] = R[i] ^ I[i];
		AES_encrypt(tmp, ctx->V, &ctx->ks);
		/* Continuous PRNG test */
		if (ctx->second)
			{
			if (fips_prng_fail)
				memcpy(ctx->last, R, AES_BLOCK_LENGTH);
			if (!memcmp(R, ctx->last, AES_BLOCK_LENGTH))
				{
	    			RANDerr(RAND_F_FIPS_RAND,RAND_R_PRNG_STUCK);
				ctx->error = 1;
				fips_set_selftest_fail();
				return 0;
				}
			}
		memcpy(ctx->last, R, AES_BLOCK_LENGTH);
		if (!ctx->second)
			{
			ctx->second = 1;
			if (!ctx->test_mode)
				continue;
			}

		if (outlen <= AES_BLOCK_LENGTH)
			{
			memcpy(out, R, outlen);
			break;
			}

		memcpy(out, R, AES_BLOCK_LENGTH);
		out += AES_BLOCK_LENGTH;
		outlen -= AES_BLOCK_LENGTH;
		}
	return 1;
	}


356
int FIPS_x931_set_key(const unsigned char *key, int keylen)
D
Dr. Stephen Henson 已提交
357 358 359 360 361 362 363 364
	{
	int ret;
	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
	ret = fips_set_prng_key(&sctx, key, keylen);
	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
	return ret;
	}

365
int FIPS_x931_seed(const void *seed, int seedlen)
D
Dr. Stephen Henson 已提交
366 367 368 369 370 371 372 373 374
	{
	int ret;
	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
	ret = fips_set_prng_seed(&sctx, seed, seedlen);
	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
	return ret;
	}


375
int FIPS_x931_bytes(unsigned char *out, int count)
D
Dr. Stephen Henson 已提交
376 377 378 379 380 381 382 383
	{
	int ret;
	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
	ret = fips_rand(&sctx, out, count);
	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
	return ret;
	}

384
int FIPS_x931_status(void)
D
Dr. Stephen Henson 已提交
385 386 387 388 389 390 391 392
	{
	int ret;
	CRYPTO_r_lock(CRYPTO_LOCK_RAND);
	ret = sctx.seeded;
	CRYPTO_r_unlock(CRYPTO_LOCK_RAND);
	return ret;
	}

393
void FIPS_x931_reset(void)
D
Dr. Stephen Henson 已提交
394 395 396 397 398 399 400 401
	{
	CRYPTO_w_lock(CRYPTO_LOCK_RAND);
	fips_rand_prng_reset(&sctx);
	CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
	}

static int fips_do_rand_seed(const void *seed, int seedlen)
	{
402
	FIPS_x931_seed(seed, seedlen);
D
Dr. Stephen Henson 已提交
403 404 405 406 407 408
	return 1;
	}

static int fips_do_rand_add(const void *seed, int seedlen,
					double add_entropy)
	{
409
	FIPS_x931_seed(seed, seedlen);
D
Dr. Stephen Henson 已提交
410 411 412
	return 1;
	}

413
static const RAND_METHOD rand_x931_meth=
D
Dr. Stephen Henson 已提交
414 415
    {
    fips_do_rand_seed,
416 417
    FIPS_x931_bytes,
    FIPS_x931_reset,
D
Dr. Stephen Henson 已提交
418
    fips_do_rand_add,
419 420
    FIPS_x931_bytes,
    FIPS_x931_status
D
Dr. Stephen Henson 已提交
421 422
    };

423
const RAND_METHOD *FIPS_x931_method(void)
D
Dr. Stephen Henson 已提交
424
{
425
  return &rand_x931_meth;
D
Dr. Stephen Henson 已提交
426 427 428
}

#endif