netwerk/srtp/src/crypto/rng/prng.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * prng.c
michael@0 3 *
michael@0 4 * pseudorandom source
michael@0 5 *
michael@0 6 * David A. McGrew
michael@0 7 * Cisco Systems, Inc.
michael@0 8 */
michael@0 9 /*
michael@0 10 *
michael@0 11 * Copyright(c) 2001-2006 Cisco Systems, Inc.
michael@0 12 * All rights reserved.
michael@0 13 *
michael@0 14 * Redistribution and use in source and binary forms, with or without
michael@0 15 * modification, are permitted provided that the following conditions
michael@0 16 * are met:
michael@0 17 *
michael@0 18 * Redistributions of source code must retain the above copyright
michael@0 19 * notice, this list of conditions and the following disclaimer.
michael@0 20 *
michael@0 21 * Redistributions in binary form must reproduce the above
michael@0 22 * copyright notice, this list of conditions and the following
michael@0 23 * disclaimer in the documentation and/or other materials provided
michael@0 24 * with the distribution.
michael@0 25 *
michael@0 26 * Neither the name of the Cisco Systems, Inc. nor the names of its
michael@0 27 * contributors may be used to endorse or promote products derived
michael@0 28 * from this software without specific prior written permission.
michael@0 29 *
michael@0 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
michael@0 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
michael@0 34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
michael@0 35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
michael@0 37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
michael@0 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
michael@0 41 * OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 42 *
michael@0 43 */
michael@0 44
michael@0 45
michael@0 46 #include "prng.h"
michael@0 47
michael@0 48 /* single, global prng structure */
michael@0 49
michael@0 50 x917_prng_t x917_prng;
michael@0 51
michael@0 52 err_status_t
michael@0 53 x917_prng_init(rand_source_func_t random_source) {
michael@0 54 uint8_t tmp_key[16];
michael@0 55 err_status_t status;
michael@0 56
michael@0 57 /* initialize output count to zero */
michael@0 58 x917_prng.octet_count = 0;
michael@0 59
michael@0 60 /* set random source */
michael@0 61 x917_prng.rand = random_source;
michael@0 62
michael@0 63 /* initialize secret key from random source */
michael@0 64 status = random_source(tmp_key, 16);
michael@0 65 if (status)
michael@0 66 return status;
michael@0 67
michael@0 68 /* expand aes key */
michael@0 69 aes_expand_encryption_key(tmp_key, 16, &x917_prng.key);
michael@0 70
michael@0 71 /* initialize prng state from random source */
michael@0 72 status = x917_prng.rand((uint8_t *)&x917_prng.state, 16);
michael@0 73 if (status)
michael@0 74 return status;
michael@0 75
michael@0 76 return err_status_ok;
michael@0 77 }
michael@0 78
michael@0 79 err_status_t
michael@0 80 x917_prng_get_octet_string(uint8_t *dest, uint32_t len) {
michael@0 81 uint32_t t;
michael@0 82 v128_t buffer;
michael@0 83 uint32_t i, tail_len;
michael@0 84 err_status_t status;
michael@0 85
michael@0 86 /*
michael@0 87 * if we need to re-initialize the prng, do so now
michael@0 88 *
michael@0 89 * avoid overflows by subtracting instead of adding
michael@0 90 */
michael@0 91 if (x917_prng.octet_count > MAX_PRNG_OUT_LEN - len) {
michael@0 92 status = x917_prng_init(x917_prng.rand);
michael@0 93 if (status)
michael@0 94 return status;
michael@0 95 }
michael@0 96 x917_prng.octet_count += len;
michael@0 97
michael@0 98 /* find out the time */
michael@0 99 t = (uint32_t)time(NULL);
michael@0 100
michael@0 101 /* loop until we have output enough data */
michael@0 102 for (i=0; i < len/16; i++) {
michael@0 103
michael@0 104 /* exor time into state */
michael@0 105 x917_prng.state.v32[0] ^= t;
michael@0 106
michael@0 107 /* copy state into buffer */
michael@0 108 v128_copy(&buffer, &x917_prng.state);
michael@0 109
michael@0 110 /* apply aes to buffer */
michael@0 111 aes_encrypt(&buffer, &x917_prng.key);
michael@0 112
michael@0 113 /* write data to output */
michael@0 114 *dest++ = buffer.v8[0];
michael@0 115 *dest++ = buffer.v8[1];
michael@0 116 *dest++ = buffer.v8[2];
michael@0 117 *dest++ = buffer.v8[3];
michael@0 118 *dest++ = buffer.v8[4];
michael@0 119 *dest++ = buffer.v8[5];
michael@0 120 *dest++ = buffer.v8[6];
michael@0 121 *dest++ = buffer.v8[7];
michael@0 122 *dest++ = buffer.v8[8];
michael@0 123 *dest++ = buffer.v8[9];
michael@0 124 *dest++ = buffer.v8[10];
michael@0 125 *dest++ = buffer.v8[11];
michael@0 126 *dest++ = buffer.v8[12];
michael@0 127 *dest++ = buffer.v8[13];
michael@0 128 *dest++ = buffer.v8[14];
michael@0 129 *dest++ = buffer.v8[15];
michael@0 130
michael@0 131 /* exor time into buffer */
michael@0 132 buffer.v32[0] ^= t;
michael@0 133
michael@0 134 /* encrypt buffer */
michael@0 135 aes_encrypt(&buffer, &x917_prng.key);
michael@0 136
michael@0 137 /* copy buffer into state */
michael@0 138 v128_copy(&x917_prng.state, &buffer);
michael@0 139
michael@0 140 }
michael@0 141
michael@0 142 /* if we need to output any more octets, we'll do so now */
michael@0 143 tail_len = len % 16;
michael@0 144 if (tail_len) {
michael@0 145
michael@0 146 /* exor time into state */
michael@0 147 x917_prng.state.v32[0] ^= t;
michael@0 148
michael@0 149 /* copy value into buffer */
michael@0 150 v128_copy(&buffer, &x917_prng.state);
michael@0 151
michael@0 152 /* apply aes to buffer */
michael@0 153 aes_encrypt(&buffer, &x917_prng.key);
michael@0 154
michael@0 155 /* write data to output */
michael@0 156 for (i=0; i < tail_len; i++) {
michael@0 157 *dest++ = buffer.v8[i];
michael@0 158 }
michael@0 159
michael@0 160 /* now update the state one more time */
michael@0 161
michael@0 162 /* exor time into buffer */
michael@0 163 buffer.v32[0] ^= t;
michael@0 164
michael@0 165 /* encrypt buffer */
michael@0 166 aes_encrypt(&buffer, &x917_prng.key);
michael@0 167
michael@0 168 /* copy buffer into state */
michael@0 169 v128_copy(&x917_prng.state, &buffer);
michael@0 170
michael@0 171 }
michael@0 172
michael@0 173 return err_status_ok;
michael@0 174 }
michael@0 175
michael@0 176 err_status_t
michael@0 177 x917_prng_deinit(void) {
michael@0 178
michael@0 179 return err_status_ok;
michael@0 180 }

mercurial