Coverage Report

Created: 2025-08-03 06:18

/src/libtpms/src/tpm2/BnMemory.c
Line
Count
Source (jump to first uncovered line)
1
/********************************************************************************/
2
/*                    */
3
/*                  */
4
/*           Written by Ken Goldman       */
5
/*           IBM Thomas J. Watson Research Center     */
6
/*            $Id: BnMemory.c 1262 2018-07-11 21:03:43Z kgoldman $      */
7
/*                    */
8
/*  Licenses and Notices              */
9
/*                    */
10
/*  1. Copyright Licenses:              */
11
/*                    */
12
/*  - Trusted Computing Group (TCG) grants to the user of the source code in  */
13
/*    this specification (the "Source Code") a worldwide, irrevocable,    */
14
/*    nonexclusive, royalty free, copyright license to reproduce, create  */
15
/*    derivative works, distribute, display and perform the Source Code and */
16
/*    derivative works thereof, and to grant others the rights granted herein.  */
17
/*                    */
18
/*  - The TCG grants to the user of the other parts of the specification  */
19
/*    (other than the Source Code) the rights to reproduce, distribute,   */
20
/*    display, and perform the specification solely for the purpose of    */
21
/*    developing products based on such documents.        */
22
/*                    */
23
/*  2. Source Code Distribution Conditions:         */
24
/*                    */
25
/*  - Redistributions of Source Code must retain the above copyright licenses,  */
26
/*    this list of conditions and the following disclaimers.      */
27
/*                    */
28
/*  - Redistributions in binary form must reproduce the above copyright   */
29
/*    licenses, this list of conditions and the following disclaimers in the  */
30
/*    documentation and/or other materials provided with the distribution.  */
31
/*                    */
32
/*  3. Disclaimers:               */
33
/*                    */
34
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
35
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
36
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
37
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.   */
38
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for    */
39
/*  information on specification licensing rights available through TCG   */
40
/*  membership agreements.              */
41
/*                    */
42
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED   */
43
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR   */
44
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR    */
45
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY    */
46
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.   */
47
/*                    */
48
/*  - Without limitation, TCG and its members and licensors disclaim all  */
49
/*    liability, including liability for infringement of any proprietary  */
50
/*    rights, relating to use of information in this specification and to the */
51
/*    implementation of this specification, and TCG disclaims all liability for */
52
/*    cost of procurement of substitute goods or services, lost profits, loss   */
53
/*    of use, loss of data or any incidental, consequential, direct, indirect,  */
54
/*    or special damages, whether under contract, tort, warranty or otherwise,  */
55
/*    arising in any way out of use or reliance upon this specification or any  */
56
/*    information herein.             */
57
/*                    */
58
/*  (c) Copyright IBM Corp. and others, 2016          */
59
/*                    */
60
/********************************************************************************/
61
62
//** Introduction
63
// This file contains the memory setup functions used by the bigNum functions
64
// in CryptoEngine
65
66
//** Includes
67
#include "TpmBigNum.h"
68
69
//** Functions
70
71
//*** BnSetTop()
72
// This function is used when the size of a bignum_t is changed. It
73
// makes sure that the unused words are set to zero and that any significant
74
// words of zeros are eliminated from the used size indicator.
75
LIB_EXPORT bigNum BnSetTop(bigNum        bn,  // IN/OUT: number to clean
76
                           crypt_uword_t top  // IN: the new top
77
)
78
2.12M
{
79
2.12M
    if(bn != NULL)
80
2.12M
    {
81
2.12M
        pAssert(top <= bn->allocated);
82
        // If forcing the size to be decreased, make sure that the words being
83
        // discarded are being set to 0
84
2.13M
        while(bn->size > top)
85
14.9k
            bn->d[--bn->size] = 0;
86
2.12M
        bn->size = top;
87
        // Now make sure that the words that are left are 'normalized' (no high-order
88
        // words of zero.
89
2.12M
        while((bn->size > 0) && (bn->d[bn->size - 1] == 0))
90
588
            bn->size -= 1;
91
2.12M
    }
92
2.12M
    return bn;
93
2.12M
}
94
#if 0 /* libtpms added */
95
96
//*** BnClearTop()
97
// This function will make sure that all unused words are zero.
98
LIB_EXPORT bigNum BnClearTop(bigNum bn)
99
{
100
    crypt_uword_t i;
101
    //
102
    if(bn != NULL)
103
    {
104
        for(i = bn->size; i < bn->allocated; i++)
105
            bn->d[i] = 0;
106
        while((bn->size > 0) && (bn->d[bn->size] == 0))
107
            bn->size -= 1;
108
    }
109
    return bn;
110
}
111
#endif /* libtpms added */
112
113
//*** BnInitializeWord()
114
// This function is used to initialize an allocated bigNum with a word value. The
115
// bigNum does not have to be allocated with a single word.
116
LIB_EXPORT bigNum BnInitializeWord(bigNum        bn,         // IN:
117
                                   crypt_uword_t allocated,  // IN:
118
                                   crypt_uword_t word        // IN:
119
)
120
0
{
121
0
    bn->allocated = allocated;
122
0
    bn->size      = (word != 0);
123
0
    bn->d[0]      = word;
124
0
    while(allocated > 1)
125
0
        bn->d[--allocated] = 0;
126
0
    return bn;
127
0
}
128
129
//*** BnInit()
130
// This function initializes a stack allocated bignum_t. It initializes
131
// 'allocated' and 'size' and zeros the words of 'd'.
132
LIB_EXPORT bigNum BnInit(bigNum bn, crypt_uword_t allocated)
133
3.83M
{
134
3.83M
    if(bn != NULL)
135
3.83M
    {
136
3.83M
        bn->allocated = allocated;
137
3.83M
        bn->size      = 0;
138
132M
        while(allocated != 0)
139
129M
            bn->d[--allocated] = 0;
140
3.83M
    }
141
3.83M
    return bn;
142
3.83M
}
143
144
//*** BnCopy()
145
// Function to copy a bignum_t. If the output is NULL, then
146
// nothing happens. If the input is NULL, the output is set
147
// to zero.
148
LIB_EXPORT BOOL BnCopy(bigNum out, bigConst in)
149
2.98k
{
150
2.98k
    if(in == out)
151
0
        BnSetTop(out, BnGetSize(out));
152
2.98k
    else if(out != NULL)
153
2.98k
    {
154
2.98k
        if(in != NULL)
155
2.98k
        {
156
2.98k
            unsigned int i;
157
2.98k
            pAssert(BnGetAllocated(out) >= BnGetSize(in));
158
59.8k
            for(i = 0; i < BnGetSize(in); i++)
159
56.8k
                out->d[i] = in->d[i];
160
2.98k
            BnSetTop(out, BnGetSize(in));
161
2.98k
        }
162
0
        else
163
0
            BnSetTop(out, 0);
164
2.98k
    }
165
2.98k
    return TRUE;
166
2.98k
}
167
168
#if ALG_ECC
169
#if 0 /* libtpms added */
170
171
//*** BnPointCopy()
172
// Function to copy a bn point.
173
LIB_EXPORT BOOL BnPointCopy(bigPoint pOut, pointConst pIn)
174
{
175
    return BnCopy(pOut->x, pIn->x) && BnCopy(pOut->y, pIn->y)
176
           && BnCopy(pOut->z, pIn->z);
177
}
178
#endif /* libtpms added */
179
180
//*** BnInitializePoint()
181
// This function is used to initialize a point structure with the addresses
182
// of the coordinates.
183
LIB_EXPORT bn_point_t* BnInitializePoint(
184
    bigPoint p,  // OUT: structure to receive pointers
185
    bigNum   x,  // IN: x coordinate
186
    bigNum   y,  // IN: y coordinate
187
    bigNum   z   // IN: x coordinate
188
)
189
1.27k
{
190
1.27k
    p->x = x;
191
1.27k
    p->y = y;
192
1.27k
    p->z = z;
193
1.27k
    BnSetWord(z, 1);
194
1.27k
    return p;
195
1.27k
}
196
197
#endif  // ALG_ECC