xref: /aosp_15_r20/external/ms-tpm-20-ref/Samples/Nucleo-TPM/Shared/Platform/src/Entropy.c (revision 5c591343844d1f9da7da26467c4bf7efc8a7a413)
1 /* Microsoft Reference Implementation for TPM 2.0
2  *
3  *  The copyright in this software is being made available under the BSD License,
4  *  included below. This software may be subject to other third party and
5  *  contributor rights, including patent rights, and no such rights are granted
6  *  under this license.
7  *
8  *  Copyright (c) Microsoft Corporation
9  *
10  *  All rights reserved.
11  *
12  *  BSD License
13  *
14  *  Redistribution and use in source and binary forms, with or without modification,
15  *  are permitted provided that the following conditions are met:
16  *
17  *  Redistributions of source code must retain the above copyright notice, this list
18  *  of conditions and the following disclaimer.
19  *
20  *  Redistributions in binary form must reproduce the above copyright notice, this
21  *  list of conditions and the following disclaimer in the documentation and/or other
22  *  materials provided with the distribution.
23  *
24  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //** Includes
37 #include "stm32l4xx_hal.h"
38 #include <stdlib.h>
39 #include <memory.h>
40 #include "PlatformData.h"
41 #include "Platform_fp.h"
42 
43 #define MIN(a,b) ((a) < (b) ? (a) : (b))
44 
45 extern RNG_HandleTypeDef hrng;
46 
47 //** Local values
48 // This is the last 32-bits of hardware entropy produced. We have to check to
49 // see that two consecutive 32-bit values are not the same because
50 // (according to FIPS 140-2, annex C
51 //
52 // 1. If each call to a RNG produces blocks of n bits (where n > 15), the first
53 // n-bit block generated after power-up, initialization, or reset shall not be
54 // used, but shall be saved for comparison with the next n-bit block to be
55 // generated. Each subsequent generation of an n-bit block shall be compared with
56 // the previously generated block. The test shall fail if any two compared n-bit
57 // blocks are equal.
58 extern uint32_t        lastEntropy;
59 
60 extern int             firstValue;
61 
62 //** _plat__GetEntropy()
63 // This function is used to get available hardware entropy. In a hardware
64 // implementation of this function, there would be no call to the system
65 // to get entropy.
66 // If the caller does not ask for any entropy, then this is a startup indication
67 // and 'firstValue' should be reset.
68 
69 // return type: int32_t
70 //  < 0        hardware failure of the entropy generator, this is sticky
71 // >= 0        the returned amount of entropy (bytes)
72 //
73 LIB_EXPORT int32_t
_plat__GetEntropy(unsigned char * entropy,uint32_t amount)74 _plat__GetEntropy(
75     unsigned char       *entropy,           // output buffer
76     uint32_t             amount             // amount requested
77     )
78 {
79     uint32_t random32bit;
80 
81     if(amount == 0)
82     {
83         firstValue = 1;
84         return 0;
85     }
86 
87     if(firstValue)
88     {
89         firstValue = 0;
90     }
91 
92     for(uint32_t n = 0; n < amount; n += sizeof(random32bit))
93     {
94         if((HAL_RNG_GenerateRandomNumber(&hrng, &random32bit) != HAL_OK) ||
95            (~firstValue && (lastEntropy == random32bit)))
96         {
97             return -1;
98         }
99         memcpy(&entropy[n], &random32bit, MIN(sizeof(random32bit), amount - n));
100         lastEntropy = random32bit;
101     }
102 
103     return (int32_t)amount;
104 }
105