Coverage Report

Created: 2025-11-09 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ibmswtpm2/src/BnMemory.c
Line
Count
Source
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
/* 10.2.5 BnMemory.c */
63
/* 10.2.5.1 Introduction */
64
/* This file contains the memory setup functions used by the bigNum functions in CryptoEngine() */
65
/* 10.2.5.2 Includes */
66
#include "Tpm.h"
67
/* 10.2.5.3 Functions */
68
/* 10.2.5.3.1 BnSetTop() */
69
/* This function is used when the size of a bignum_t is changed. It makes sure that the unused words
70
   are set to zero and that any significant words of zeros are eliminated from the used size
71
   indicator. */
72
LIB_EXPORT bigNum
73
BnSetTop(
74
   bigNum           bn,        // IN/OUT: number to clean
75
   crypt_uword_t    top        // IN: the new top
76
   )
77
0
{
78
0
    if(bn != NULL)
79
0
  {
80
0
      pAssert(top <= bn->allocated);
81
      // If forcing the size to be decreased, make sure that the words being
82
      // discarded are being set to 0
83
0
      while(bn->size > top)
84
0
    bn->d[--bn->size] = 0;
85
0
      bn->size = top;
86
      // Now make sure that the words that are left are 'normalized' (no high-order
87
      // words of zero.
88
0
      while((bn->size > 0) && (bn->d[bn->size - 1] == 0))
89
0
    bn->size -= 1;
90
0
  }
91
0
    return bn;
92
0
}
93
/* 10.2.5.3.2 BnClearTop() */
94
/* This function will make sure that all unused words are zero. */
95
LIB_EXPORT bigNum
96
BnClearTop(
97
     bigNum          bn
98
     )
99
0
{
100
0
    crypt_uword_t       i;
101
    //
102
0
    if(bn != NULL)
103
0
  {
104
0
      for(i = bn->size; i < bn->allocated; i++)
105
0
    bn->d[i] = 0;
106
0
      while((bn->size > 0) && (bn->d[bn->size] == 0))
107
0
    bn->size -= 1;
108
0
  }
109
0
    return bn;
110
0
}
111
/* 10.2.5.3.3 BnInitializeWord() */
112
/* This function is used to initialize an allocated bigNum with a word value. The bigNum does not
113
   have to be allocated with a single word. */
114
LIB_EXPORT bigNum
115
BnInitializeWord(
116
     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
/* 10.2.5.3.4 BnInit() */
129
/* This function initializes a stack allocated bignum_t. It initializes allocated and size and zeros
130
   the words of d. */
131
LIB_EXPORT bigNum
132
BnInit(
133
       bigNum               bn,
134
       crypt_uword_t        allocated
135
       )
136
0
{
137
0
    if(bn != NULL)
138
0
  {
139
0
      bn->allocated = allocated;
140
0
      bn->size = 0;
141
0
      while(allocated != 0)
142
0
    bn->d[--allocated] = 0;
143
0
  }
144
0
    return bn;
145
0
}
146
/* 10.2.5.3.5 BnCopy() */
147
/* Function to copy a bignum_t. If the output is NULL, then nothing happens. If the input is NULL,
148
   the output is set to zero. */
149
LIB_EXPORT BOOL
150
BnCopy(
151
       bigNum           out,
152
       bigConst         in
153
       )
154
0
{
155
0
    if(in == out)
156
0
  BnSetTop(out, BnGetSize(out));
157
0
    else if(out != NULL)
158
0
  {
159
0
      if(in != NULL)
160
0
    {
161
0
        unsigned int         i;
162
0
        pAssert(BnGetAllocated(out) >= BnGetSize(in));
163
0
        for(i = 0; i < BnGetSize(in); i++)
164
0
      out->d[i] = in->d[i];
165
0
        BnSetTop(out, BnGetSize(in));
166
0
    }
167
0
      else
168
0
    BnSetTop(out, 0);
169
0
  }
170
0
    return TRUE;
171
0
}
172
#if ALG_ECC
173
/* 10.2.5.3.6 BnPointCopy() */
174
/* Function to copy a bn point. */
175
LIB_EXPORT BOOL
176
BnPointCopy(
177
      bigPoint                 pOut,
178
      pointConst               pIn
179
      )
180
0
{
181
0
    return BnCopy(pOut->x, pIn->x)
182
0
  && BnCopy(pOut->y, pIn->y)
183
0
  && BnCopy(pOut->z, pIn->z);
184
0
}
185
/* 10.2.5.3.7 BnInitializePoint() */
186
/* This function is used to initialize a point structure with the addresses of the coordinates. */
187
LIB_EXPORT bn_point_t *
188
BnInitializePoint(
189
      bigPoint             p,     // OUT: structure to receive pointers
190
      bigNum               x,     // IN: x coordinate
191
      bigNum               y,     // IN: y coordinate
192
      bigNum               z      // IN: x coordinate
193
      )
194
0
{
195
0
    p->x = x;
196
0
    p->y = y;
197
0
    p->z = z;
198
0
    BnSetWord(z, 1);
199
0
    return p;
200
0
}
201
#endif // TPM_ALG_ECC