blob: 4096ac3b593acc3985baf6e09276766688f61e12 [file] [log] [blame]
Vadim Bendebury56797522015-05-20 10:32:25 -07001// This file was extracted from the TCG Published
2// Trusted Platform Module Library
3// Part 4: Supporting Routines
4// Family "2.0"
5// Level 00 Revision 01.16
6// October 30, 2014
7
Vadim Bendebury56797522015-05-20 10:32:25 -07008#include <stdlib.h>
9#include <stdint.h>
10#include <memory.h>
11#include "TpmBuildSwitches.h"
12//
13//
14// Local values
15//
16// This is the last 32-bits of hardware entropy produced. We have to check to see that two consecutive 32-
17// bit values are not the same because (according to FIPS 140-2, annex C
18// “If each call to a RNG produces blocks of n bits (where n > 15), the first n-bit block generated after
19// power-up, initialization, or reset shall not be used, but shall be saved for comparison with the next n-
20// bit block to be generated. Each subsequent generation of an n-bit block shall be compared with the
21// previously generated block. The test shall fail if any two compared n-bit blocks are equal.”
22//
23extern uint32_t lastEntropy;
24extern int firstValue;
25//
26//
27// _plat__GetEntropy()
28//
29// This function is used to get available hardware entropy. In a hardware implementation of this function,
30// there would be no call to the system to get entropy. If the caller does not ask for any entropy, then this is
31// a startup indication and firstValue should be reset.
32//
33// Return Value Meaning
34//
35// <0 hardware failure of the entropy generator, this is sticky
36// >= 0 the returned amount of entropy (bytes)
37//
38LIB_EXPORT int32_t
39_plat__GetEntropy(
40 unsigned char *entropy, // output buffer
41 uint32_t amount // amount requested
42)
43{
44 uint32_t rndNum;
Vadim Bendebury61cee3d2015-05-29 17:30:55 -070045
Vadim Bendebury56797522015-05-20 10:32:25 -070046 if(amount == 0)
47 {
48 firstValue = 1;
49 return 0;
50 }
51 // Only provide entropy 32 bits at a time to test the ability
52 // of the caller to deal with partial results.
Vadim Bendebury61cee3d2015-05-29 17:30:55 -070053 rndNum = random(); //TODO(vbendeb): compare to rand_s case
54 if(firstValue)
55 firstValue = 0;
56
57 lastEntropy = rndNum;
58 if(amount > sizeof(rndNum))
59 amount = sizeof(rndNum);
60 memcpy(entropy, &rndNum, amount);
61
62 return (int32_t)amount;
Vadim Bendebury56797522015-05-20 10:32:25 -070063}