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 //** Introduction
37 //
38 // The functions in this file are used for initialization of the interface to the
39 // LibTomCrypt and MpsLib libraries. This is not used if only the LTC hash and
40 // symmetric functions are used.
41 
42 //** Defines and Includes
43 
44 #include "Tpm.h"
45 
46 #if defined(HASH_LIB_LTC) || defined(MATH_LIB_LTC) || defined(SYM_LIB_LTC)
47 
48 // This state is used because there is no way to pass the random number state
49 // to LibTomCrypt. I do not think that this is currently an issue because...
50 // Heck, just put in an assert and see what happens.
51 static void             *s_randState;
52 
53 //*** LtcRand()
54 // This is a stub function that is called from the LibTomCrypt or libmpa code
55 // to get a random number. In turn, this will call the random RandGenerate
56 // function that was passed in LibraryInit(). This function will pass the pointer
57 // to the current rand state along with the random byte request.
LtcRand(void * buf,size_t blen)58 uint32_t     LtcRand(
59     void            *buf,
60     size_t           blen
61     )
62 {
63     pAssert(1);
64     DRBG_Generate(s_randState, buf, (uint16_t)blen);
65     return 0;
66 }
67 
68 //*** SupportLibInit()
69 // This does any initialization required by the support library.
70 LIB_EXPORT int
SupportLibInit(void)71 SupportLibInit(
72     void
73     )
74 {
75     mpa_set_random_generator(LtcRand);
76     s_randState = NULL;
77     external_mem_pool = NULL;
78     return 1;
79 }
80 
81 //*** LtcPoolInit()
82 // Function to initialize a pool. ****
83 LIB_EXPORT mpa_scratch_mem
LtcPoolInit(mpa_word_t * poolAddress,int vars,int bits)84 LtcPoolInit(
85     mpa_word_t      *poolAddress,
86     int              vars,
87     int              bits
88     )
89 {
90     mpa_scratch_mem     pool = (mpa_scratch_mem)poolAddress;
91     mpa_init_scratch_mem(pool, vars, bits);
92     init_mpa_tomcrypt(pool);
93     return pool;
94 }
95 
96 #endif // HASH_LIB_LTC || MATH_LIB_LTC || SYM_LIB_LTC
97