1*5c591343SA. Cody Schuffelen /* Microsoft Reference Implementation for TPM 2.0
2*5c591343SA. Cody Schuffelen *
3*5c591343SA. Cody Schuffelen * The copyright in this software is being made available under the BSD License,
4*5c591343SA. Cody Schuffelen * included below. This software may be subject to other third party and
5*5c591343SA. Cody Schuffelen * contributor rights, including patent rights, and no such rights are granted
6*5c591343SA. Cody Schuffelen * under this license.
7*5c591343SA. Cody Schuffelen *
8*5c591343SA. Cody Schuffelen * Copyright (c) Microsoft Corporation
9*5c591343SA. Cody Schuffelen *
10*5c591343SA. Cody Schuffelen * All rights reserved.
11*5c591343SA. Cody Schuffelen *
12*5c591343SA. Cody Schuffelen * BSD License
13*5c591343SA. Cody Schuffelen *
14*5c591343SA. Cody Schuffelen * Redistribution and use in source and binary forms, with or without modification,
15*5c591343SA. Cody Schuffelen * are permitted provided that the following conditions are met:
16*5c591343SA. Cody Schuffelen *
17*5c591343SA. Cody Schuffelen * Redistributions of source code must retain the above copyright notice, this list
18*5c591343SA. Cody Schuffelen * of conditions and the following disclaimer.
19*5c591343SA. Cody Schuffelen *
20*5c591343SA. Cody Schuffelen * Redistributions in binary form must reproduce the above copyright notice, this
21*5c591343SA. Cody Schuffelen * list of conditions and the following disclaimer in the documentation and/or
22*5c591343SA. Cody Schuffelen * other materials provided with the distribution.
23*5c591343SA. Cody Schuffelen *
24*5c591343SA. Cody Schuffelen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25*5c591343SA. Cody Schuffelen * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*5c591343SA. Cody Schuffelen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27*5c591343SA. Cody Schuffelen * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28*5c591343SA. Cody Schuffelen * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*5c591343SA. Cody Schuffelen * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30*5c591343SA. Cody Schuffelen * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31*5c591343SA. Cody Schuffelen * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*5c591343SA. Cody Schuffelen * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5c591343SA. Cody Schuffelen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*5c591343SA. Cody Schuffelen */
35*5c591343SA. Cody Schuffelen //** Includes and Local Values
36*5c591343SA. Cody Schuffelen
37*5c591343SA. Cody Schuffelen #define _CRT_RAND_S
38*5c591343SA. Cody Schuffelen #include <stdlib.h>
39*5c591343SA. Cody Schuffelen #include <memory.h>
40*5c591343SA. Cody Schuffelen #include <time.h>
41*5c591343SA. Cody Schuffelen #include "Platform.h"
42*5c591343SA. Cody Schuffelen
43*5c591343SA. Cody Schuffelen #ifdef _MSC_VER
44*5c591343SA. Cody Schuffelen #include <process.h>
45*5c591343SA. Cody Schuffelen #else
46*5c591343SA. Cody Schuffelen #include <unistd.h>
47*5c591343SA. Cody Schuffelen #endif
48*5c591343SA. Cody Schuffelen
49*5c591343SA. Cody Schuffelen // This is the last 32-bits of hardware entropy produced. We have to check to
50*5c591343SA. Cody Schuffelen // see that two consecutive 32-bit values are not the same because
51*5c591343SA. Cody Schuffelen // according to FIPS 140-2, annex C:
52*5c591343SA. Cody Schuffelen //
53*5c591343SA. Cody Schuffelen // "If each call to an RNG produces blocks of n bits (where n > 15), the first
54*5c591343SA. Cody Schuffelen // n-bit block generated after power-up, initialization, or reset shall not be
55*5c591343SA. Cody Schuffelen // used, but shall be saved for comparison with the next n-bit block to be
56*5c591343SA. Cody Schuffelen // generated. Each subsequent generation of an n-bit block shall be compared with
57*5c591343SA. Cody Schuffelen // the previously generated block. The test shall fail if any two compared n-bit
58*5c591343SA. Cody Schuffelen // blocks are equal."
59*5c591343SA. Cody Schuffelen extern uint32_t lastEntropy;
60*5c591343SA. Cody Schuffelen
61*5c591343SA. Cody Schuffelen //** Functions
62*5c591343SA. Cody Schuffelen
63*5c591343SA. Cody Schuffelen //*** rand32()
64*5c591343SA. Cody Schuffelen // Local function to get a 32-bit random number
65*5c591343SA. Cody Schuffelen static uint32_t
rand32(void)66*5c591343SA. Cody Schuffelen rand32(
67*5c591343SA. Cody Schuffelen void
68*5c591343SA. Cody Schuffelen )
69*5c591343SA. Cody Schuffelen {
70*5c591343SA. Cody Schuffelen uint32_t rndNum = rand();
71*5c591343SA. Cody Schuffelen #if RAND_MAX < UINT16_MAX
72*5c591343SA. Cody Schuffelen // If the maximum value of the random number is a 15-bit number, then shift it up
73*5c591343SA. Cody Schuffelen // 15 bits, get 15 more bits, shift that up 2 and then XOR in another value to get
74*5c591343SA. Cody Schuffelen // a full 32 bits.
75*5c591343SA. Cody Schuffelen rndNum = (rndNum << 15) ^ rand();
76*5c591343SA. Cody Schuffelen rndNum = (rndNum << 2) ^ rand();
77*5c591343SA. Cody Schuffelen #elif RAND_MAX == UINT16_MAX
78*5c591343SA. Cody Schuffelen // If the maximum size is 16-bits, shift it and add another 16 bits
79*5c591343SA. Cody Schuffelen rndNum = (rndNum << 16) ^ rand();
80*5c591343SA. Cody Schuffelen #elif RAND_MAX < UINT32_MAX
81*5c591343SA. Cody Schuffelen // If 31 bits, then shift 1 and include another random value to get the extra bit
82*5c591343SA. Cody Schuffelen rndNum = (rndNum << 1) ^ rand();
83*5c591343SA. Cody Schuffelen #endif
84*5c591343SA. Cody Schuffelen return rndNum;
85*5c591343SA. Cody Schuffelen }
86*5c591343SA. Cody Schuffelen
87*5c591343SA. Cody Schuffelen
88*5c591343SA. Cody Schuffelen //*** _plat__GetEntropy()
89*5c591343SA. Cody Schuffelen // This function is used to get available hardware entropy. In a hardware
90*5c591343SA. Cody Schuffelen // implementation of this function, there would be no call to the system
91*5c591343SA. Cody Schuffelen // to get entropy.
92*5c591343SA. Cody Schuffelen // Return Type: int32_t
93*5c591343SA. Cody Schuffelen // < 0 hardware failure of the entropy generator, this is sticky
94*5c591343SA. Cody Schuffelen // >= 0 the returned amount of entropy (bytes)
95*5c591343SA. Cody Schuffelen //
96*5c591343SA. Cody Schuffelen LIB_EXPORT int32_t
_plat__GetEntropy(unsigned char * entropy,uint32_t amount)97*5c591343SA. Cody Schuffelen _plat__GetEntropy(
98*5c591343SA. Cody Schuffelen unsigned char *entropy, // output buffer
99*5c591343SA. Cody Schuffelen uint32_t amount // amount requested
100*5c591343SA. Cody Schuffelen )
101*5c591343SA. Cody Schuffelen {
102*5c591343SA. Cody Schuffelen uint32_t rndNum;
103*5c591343SA. Cody Schuffelen int32_t ret;
104*5c591343SA. Cody Schuffelen //
105*5c591343SA. Cody Schuffelen if(amount == 0)
106*5c591343SA. Cody Schuffelen {
107*5c591343SA. Cody Schuffelen // Seed the platform entropy source if the entropy source is software. There
108*5c591343SA. Cody Schuffelen // is no reason to put a guard macro (#if or #ifdef) around this code because
109*5c591343SA. Cody Schuffelen // this code would not be here if someone was changing it for a system with
110*5c591343SA. Cody Schuffelen // actual hardware.
111*5c591343SA. Cody Schuffelen //
112*5c591343SA. Cody Schuffelen // NOTE 1: The following command does not provide proper cryptographic
113*5c591343SA. Cody Schuffelen // entropy. Its primary purpose to make sure that different instances of the
114*5c591343SA. Cody Schuffelen // simulator, possibly started by a script on the same machine, are seeded
115*5c591343SA. Cody Schuffelen // differently. Vendors of the actual TPMs need to ensure availability of
116*5c591343SA. Cody Schuffelen // proper entropy using their platform-specific means.
117*5c591343SA. Cody Schuffelen //
118*5c591343SA. Cody Schuffelen // NOTE 2: In debug builds by default the reference implementation will seed
119*5c591343SA. Cody Schuffelen // its RNG deterministically (without using any platform provided randomness).
120*5c591343SA. Cody Schuffelen // See the USE_DEBUG_RNG macro and DRBG_GetEntropy() function.
121*5c591343SA. Cody Schuffelen #ifdef _MSC_VER
122*5c591343SA. Cody Schuffelen srand((unsigned)_plat__RealTime() ^ _getpid());
123*5c591343SA. Cody Schuffelen #else
124*5c591343SA. Cody Schuffelen srand((unsigned)_plat__RealTime() ^ getpid());
125*5c591343SA. Cody Schuffelen #endif
126*5c591343SA. Cody Schuffelen lastEntropy = rand32();
127*5c591343SA. Cody Schuffelen ret = 0;
128*5c591343SA. Cody Schuffelen }
129*5c591343SA. Cody Schuffelen else
130*5c591343SA. Cody Schuffelen {
131*5c591343SA. Cody Schuffelen rndNum = rand32();
132*5c591343SA. Cody Schuffelen if(rndNum == lastEntropy)
133*5c591343SA. Cody Schuffelen {
134*5c591343SA. Cody Schuffelen ret = -1;
135*5c591343SA. Cody Schuffelen }
136*5c591343SA. Cody Schuffelen else
137*5c591343SA. Cody Schuffelen {
138*5c591343SA. Cody Schuffelen lastEntropy = rndNum;
139*5c591343SA. Cody Schuffelen // Each process will have its random number generator initialized
140*5c591343SA. Cody Schuffelen // according to the process id and the initialization time. This is not a
141*5c591343SA. Cody Schuffelen // lot of entropy so, to add a bit more, XOR the current time value into
142*5c591343SA. Cody Schuffelen // the returned entropy value.
143*5c591343SA. Cody Schuffelen // NOTE: the reason for including the time here rather than have it in
144*5c591343SA. Cody Schuffelen // in the value assigned to lastEntropy is that rand() could be broken and
145*5c591343SA. Cody Schuffelen // using the time would in the lastEntropy value would hide this.
146*5c591343SA. Cody Schuffelen rndNum ^= (uint32_t)_plat__RealTime();
147*5c591343SA. Cody Schuffelen
148*5c591343SA. Cody Schuffelen // Only provide entropy 32 bits at a time to test the ability
149*5c591343SA. Cody Schuffelen // of the caller to deal with partial results.
150*5c591343SA. Cody Schuffelen ret = MIN(amount, sizeof(rndNum));
151*5c591343SA. Cody Schuffelen memcpy(entropy, &rndNum, ret);
152*5c591343SA. Cody Schuffelen }
153*5c591343SA. Cody Schuffelen }
154*5c591343SA. Cody Schuffelen return ret;
155*5c591343SA. Cody Schuffelen }