michael@0: /* michael@0: * prng.c michael@0: * michael@0: * pseudorandom source michael@0: * michael@0: * David A. McGrew michael@0: * Cisco Systems, Inc. michael@0: */ michael@0: /* michael@0: * michael@0: * Copyright(c) 2001-2006 Cisco Systems, Inc. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * Redistributions in binary form must reproduce the above michael@0: * copyright notice, this list of conditions and the following michael@0: * disclaimer in the documentation and/or other materials provided michael@0: * with the distribution. michael@0: * michael@0: * Neither the name of the Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS michael@0: * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE michael@0: * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, michael@0: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES michael@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@0: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, michael@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED michael@0: * OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: michael@0: #include "prng.h" michael@0: michael@0: /* single, global prng structure */ michael@0: michael@0: x917_prng_t x917_prng; michael@0: michael@0: err_status_t michael@0: x917_prng_init(rand_source_func_t random_source) { michael@0: uint8_t tmp_key[16]; michael@0: err_status_t status; michael@0: michael@0: /* initialize output count to zero */ michael@0: x917_prng.octet_count = 0; michael@0: michael@0: /* set random source */ michael@0: x917_prng.rand = random_source; michael@0: michael@0: /* initialize secret key from random source */ michael@0: status = random_source(tmp_key, 16); michael@0: if (status) michael@0: return status; michael@0: michael@0: /* expand aes key */ michael@0: aes_expand_encryption_key(tmp_key, 16, &x917_prng.key); michael@0: michael@0: /* initialize prng state from random source */ michael@0: status = x917_prng.rand((uint8_t *)&x917_prng.state, 16); michael@0: if (status) michael@0: return status; michael@0: michael@0: return err_status_ok; michael@0: } michael@0: michael@0: err_status_t michael@0: x917_prng_get_octet_string(uint8_t *dest, uint32_t len) { michael@0: uint32_t t; michael@0: v128_t buffer; michael@0: uint32_t i, tail_len; michael@0: err_status_t status; michael@0: michael@0: /* michael@0: * if we need to re-initialize the prng, do so now michael@0: * michael@0: * avoid overflows by subtracting instead of adding michael@0: */ michael@0: if (x917_prng.octet_count > MAX_PRNG_OUT_LEN - len) { michael@0: status = x917_prng_init(x917_prng.rand); michael@0: if (status) michael@0: return status; michael@0: } michael@0: x917_prng.octet_count += len; michael@0: michael@0: /* find out the time */ michael@0: t = (uint32_t)time(NULL); michael@0: michael@0: /* loop until we have output enough data */ michael@0: for (i=0; i < len/16; i++) { michael@0: michael@0: /* exor time into state */ michael@0: x917_prng.state.v32[0] ^= t; michael@0: michael@0: /* copy state into buffer */ michael@0: v128_copy(&buffer, &x917_prng.state); michael@0: michael@0: /* apply aes to buffer */ michael@0: aes_encrypt(&buffer, &x917_prng.key); michael@0: michael@0: /* write data to output */ michael@0: *dest++ = buffer.v8[0]; michael@0: *dest++ = buffer.v8[1]; michael@0: *dest++ = buffer.v8[2]; michael@0: *dest++ = buffer.v8[3]; michael@0: *dest++ = buffer.v8[4]; michael@0: *dest++ = buffer.v8[5]; michael@0: *dest++ = buffer.v8[6]; michael@0: *dest++ = buffer.v8[7]; michael@0: *dest++ = buffer.v8[8]; michael@0: *dest++ = buffer.v8[9]; michael@0: *dest++ = buffer.v8[10]; michael@0: *dest++ = buffer.v8[11]; michael@0: *dest++ = buffer.v8[12]; michael@0: *dest++ = buffer.v8[13]; michael@0: *dest++ = buffer.v8[14]; michael@0: *dest++ = buffer.v8[15]; michael@0: michael@0: /* exor time into buffer */ michael@0: buffer.v32[0] ^= t; michael@0: michael@0: /* encrypt buffer */ michael@0: aes_encrypt(&buffer, &x917_prng.key); michael@0: michael@0: /* copy buffer into state */ michael@0: v128_copy(&x917_prng.state, &buffer); michael@0: michael@0: } michael@0: michael@0: /* if we need to output any more octets, we'll do so now */ michael@0: tail_len = len % 16; michael@0: if (tail_len) { michael@0: michael@0: /* exor time into state */ michael@0: x917_prng.state.v32[0] ^= t; michael@0: michael@0: /* copy value into buffer */ michael@0: v128_copy(&buffer, &x917_prng.state); michael@0: michael@0: /* apply aes to buffer */ michael@0: aes_encrypt(&buffer, &x917_prng.key); michael@0: michael@0: /* write data to output */ michael@0: for (i=0; i < tail_len; i++) { michael@0: *dest++ = buffer.v8[i]; michael@0: } michael@0: michael@0: /* now update the state one more time */ michael@0: michael@0: /* exor time into buffer */ michael@0: buffer.v32[0] ^= t; michael@0: michael@0: /* encrypt buffer */ michael@0: aes_encrypt(&buffer, &x917_prng.key); michael@0: michael@0: /* copy buffer into state */ michael@0: v128_copy(&x917_prng.state, &buffer); michael@0: michael@0: } michael@0: michael@0: return err_status_ok; michael@0: } michael@0: michael@0: err_status_t michael@0: x917_prng_deinit(void) { michael@0: michael@0: return err_status_ok; michael@0: }