• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / libtpms / #2022

23 Sep 2025 01:11PM UTC coverage: 77.227% (+0.009%) from 77.218%
#2022

push

travis-ci

web-flow
Merge a45c293de into 4504f47c6

36116 of 46766 relevant lines covered (77.23%)

125180.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.05
/src/tpm2/BnMemory.c
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
1,966,425✔
76
                           crypt_uword_t top  // IN: the new top
77
)
78
{
79
    if(bn != NULL)
1,966,425✔
80
    {
81
        pAssert(top <= bn->allocated);
1,966,425✔
82
        // If forcing the size to be decreased, make sure that the words being
83
        // discarded are being set to 0
84
        while(bn->size > top)
1,986,934✔
85
            bn->d[--bn->size] = 0;
20,509✔
86
        bn->size = top;
1,966,425✔
87
        // Now make sure that the words that are left are 'normalized' (no high-order
88
        // words of zero.
89
        while((bn->size > 0) && (bn->d[bn->size - 1] == 0))
1,966,817✔
90
            bn->size -= 1;
392✔
91
    }
92
    return bn;
1,966,425✔
93
}
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
{
121
    bn->allocated = allocated;
×
122
    bn->size      = (word != 0);
×
123
    bn->d[0]      = word;
×
124
    while(allocated > 1)
×
125
        bn->d[--allocated] = 0;
×
126
    return bn;
×
127
}
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)
3,489,077✔
133
{
134
    if(bn != NULL)
3,489,077✔
135
    {
136
        bn->allocated = allocated;
3,489,077✔
137
        bn->size      = 0;
3,489,077✔
138
        while(allocated != 0)
121,121,877✔
139
            bn->d[--allocated] = 0;
117,632,800✔
140
    }
141
    return bn;
3,489,077✔
142
}
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)
5,773✔
149
{
150
    if(in == out)
5,773✔
151
        BnSetTop(out, BnGetSize(out));
×
152
    else if(out != NULL)
5,773✔
153
    {
154
        if(in != NULL)
5,773✔
155
        {
156
            unsigned int i;
5,773✔
157
            pAssert(BnGetAllocated(out) >= BnGetSize(in));
5,773✔
158
            for(i = 0; i < BnGetSize(in); i++)
92,597✔
159
                out->d[i] = in->d[i];
86,824✔
160
            BnSetTop(out, BnGetSize(in));
5,773✔
161
        }
162
        else
163
            BnSetTop(out, 0);
×
164
    }
165
    return TRUE;
5,773✔
166
}
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(
2,305✔
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
{
190
    p->x = x;
2,305✔
191
    p->y = y;
2,305✔
192
    p->z = z;
2,305✔
193
    BnSetWord(z, 1);
2,305✔
194
    return p;
2,305✔
195
}
196

197
#endif  // ALG_ECC
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc