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
22 * other 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 //** Introduction
36 // This file contains the memory setup functions used by the bigNum functions
37 // in CryptoEngine
38
39 //** Includes
40 #include "Tpm.h"
41
42 //** Functions
43
44 //*** BnSetTop()
45 // This function is used when the size of a bignum_t is changed. It
46 // makes sure that the unused words are set to zero and that any significant
47 // words of zeros are eliminated from the used size indicator.
48 LIB_EXPORT bigNum
BnSetTop(bigNum bn,crypt_uword_t top)49 BnSetTop(
50 bigNum bn, // IN/OUT: number to clean
51 crypt_uword_t top // IN: the new top
52 )
53 {
54 if(bn != NULL)
55 {
56 pAssert(top <= bn->allocated);
57 // If forcing the size to be decreased, make sure that the words being
58 // discarded are being set to 0
59 while(bn->size > top)
60 bn->d[--bn->size] = 0;
61 bn->size = top;
62 // Now make sure that the words that are left are 'normalized' (no high-order
63 // words of zero.
64 while((bn->size > 0) && (bn->d[bn->size - 1] == 0))
65 bn->size -= 1;
66 }
67 return bn;
68 }
69
70 //*** BnClearTop()
71 // This function will make sure that all unused words are zero.
72 LIB_EXPORT bigNum
BnClearTop(bigNum bn)73 BnClearTop(
74 bigNum bn
75 )
76 {
77 crypt_uword_t i;
78 //
79 if(bn != NULL)
80 {
81 for(i = bn->size; i < bn->allocated; i++)
82 bn->d[i] = 0;
83 while((bn->size > 0) && (bn->d[bn->size] == 0))
84 bn->size -= 1;
85 }
86 return bn;
87 }
88
89 //*** BnInitializeWord()
90 // This function is used to initialize an allocated bigNum with a word value. The
91 // bigNum does not have to be allocated with a single word.
92 LIB_EXPORT bigNum
BnInitializeWord(bigNum bn,crypt_uword_t allocated,crypt_uword_t word)93 BnInitializeWord(
94 bigNum bn, // IN:
95 crypt_uword_t allocated, // IN:
96 crypt_uword_t word // IN:
97 )
98 {
99 bn->allocated = allocated;
100 bn->size = (word != 0);
101 bn->d[0] = word;
102 while(allocated > 1)
103 bn->d[--allocated] = 0;
104 return bn;
105 }
106
107 //*** BnInit()
108 // This function initializes a stack allocated bignum_t. It initializes
109 // 'allocated' and 'size' and zeros the words of 'd'.
110 LIB_EXPORT bigNum
BnInit(bigNum bn,crypt_uword_t allocated)111 BnInit(
112 bigNum bn,
113 crypt_uword_t allocated
114 )
115 {
116 if(bn != NULL)
117 {
118 bn->allocated = allocated;
119 bn->size = 0;
120 while(allocated != 0)
121 bn->d[--allocated] = 0;
122 }
123 return bn;
124 }
125
126 //*** BnCopy()
127 // Function to copy a bignum_t. If the output is NULL, then
128 // nothing happens. If the input is NULL, the output is set
129 // to zero.
130 LIB_EXPORT BOOL
BnCopy(bigNum out,bigConst in)131 BnCopy(
132 bigNum out,
133 bigConst in
134 )
135 {
136 if(in == out)
137 BnSetTop(out, BnGetSize(out));
138 else if(out != NULL)
139 {
140 if(in != NULL)
141 {
142 unsigned int i;
143 pAssert(BnGetAllocated(out) >= BnGetSize(in));
144 for(i = 0; i < BnGetSize(in); i++)
145 out->d[i] = in->d[i];
146 BnSetTop(out, BnGetSize(in));
147 }
148 else
149 BnSetTop(out, 0);
150 }
151 return TRUE;
152 }
153
154 #if ALG_ECC
155
156 //*** BnPointCopy()
157 // Function to copy a bn point.
158 LIB_EXPORT BOOL
BnPointCopy(bigPoint pOut,pointConst pIn)159 BnPointCopy(
160 bigPoint pOut,
161 pointConst pIn
162 )
163 {
164 return BnCopy(pOut->x, pIn->x)
165 && BnCopy(pOut->y, pIn->y)
166 && BnCopy(pOut->z, pIn->z);
167 }
168
169 //*** BnInitializePoint()
170 // This function is used to initialize a point structure with the addresses
171 // of the coordinates.
172 LIB_EXPORT bn_point_t *
BnInitializePoint(bigPoint p,bigNum x,bigNum y,bigNum z)173 BnInitializePoint(
174 bigPoint p, // OUT: structure to receive pointers
175 bigNum x, // IN: x coordinate
176 bigNum y, // IN: y coordinate
177 bigNum z // IN: x coordinate
178 )
179 {
180 p->x = x;
181 p->y = y;
182 p->z = z;
183 BnSetWord(z, 1);
184 return p;
185 }
186
187 #endif // ALG_ECC