Line | Count | Source |
1 | | // 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 | | |
8 | | #include <stdlib.h> |
9 | | #include <stdint.h> |
10 | | #include <memory.h> |
11 | | #include "PlatformData.h" |
12 | | #include "TpmBuildSwitches.h" |
13 | | // |
14 | | // |
15 | | // Local values |
16 | | // |
17 | | // This is the last 32-bits of hardware entropy produced. We have to check to see that two consecutive 32- |
18 | | // bit values are not the same because (according to FIPS 140-2, annex C |
19 | | // “If each call to a RNG produces blocks of n bits (where n > 15), the first n-bit block generated after |
20 | | // power-up, initialization, or reset shall not be used, but shall be saved for comparison with the next n- |
21 | | // bit block to be generated. Each subsequent generation of an n-bit block shall be compared with the |
22 | | // previously generated block. The test shall fail if any two compared n-bit blocks are equal.” |
23 | | // |
24 | | // |
25 | | // |
26 | | // _plat__GetEntropy() |
27 | | // |
28 | | // This function is used to get available hardware entropy. In a hardware implementation of this function, |
29 | | // there would be no call to the system to get entropy. If the caller does not ask for any entropy, then this is |
30 | | // a startup indication and firstValue should be reset. |
31 | | // |
32 | | // Return Value Meaning |
33 | | // |
34 | | // <0 hardware failure of the entropy generator, this is sticky |
35 | | // >= 0 the returned amount of entropy (bytes) |
36 | | // |
37 | | LIB_EXPORT int32_t |
38 | | _plat__GetEntropy( |
39 | | unsigned char *entropy, // output buffer |
40 | | uint32_t amount // amount requested |
41 | | ) |
42 | 8.41k | { |
43 | 8.41k | uint32_t rndNum; |
44 | | |
45 | 8.41k | if(amount == 0) |
46 | 495 | { |
47 | 495 | firstValue = 1; |
48 | 495 | return 0; |
49 | 495 | } |
50 | | // Only provide entropy 32 bits at a time to test the ability |
51 | | // of the caller to deal with partial results. |
52 | 7.92k | rndNum = random(); //TODO(vbendeb): compare to rand_s case |
53 | 7.92k | if(firstValue) |
54 | 495 | firstValue = 0; |
55 | | |
56 | 7.92k | lastEntropy = rndNum; |
57 | 7.92k | if(amount > sizeof(rndNum)) |
58 | 7.42k | amount = sizeof(rndNum); |
59 | 7.92k | memcpy(entropy, &rndNum, amount); |
60 | | |
61 | 7.92k | return (int32_t)amount; |
62 | 8.41k | } |