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

stefanberger / libtpms / 2698

pending completion
2698

Pull #353

travis-ci-com

web-flow
Merge a8cae3f5d into d08b929de
Pull Request #353: Travis: Update from bionic to focal and from xenial to bionic

33875 of 43923 relevant lines covered (77.12%)

93992.6 hits per line

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

0.0
/src/tpm2/BackwardsCompatibilityObject.c
1
/********************************************************************************/
2
/*                                                                                */
3
/*                Backwards compatibility stuff related to OBJECT                */
4
/*                             Written by Stefan Berger                                */
5
/*                       IBM Thomas J. Watson Research Center                        */
6
/*                                                                                */
7
/* (c) Copyright IBM Corporation 2017,2018.                                        */
8
/*                                                                                */
9
/* All rights reserved.                                                                */
10
/*                                                                                 */
11
/* Redistribution and use in source and binary forms, with or without                */
12
/* modification, are permitted provided that the following conditions are        */
13
/* met:                                                                                */
14
/*                                                                                 */
15
/* Redistributions of source code must retain the above copyright notice,        */
16
/* this list of conditions and the following disclaimer.                        */
17
/*                                                                                 */
18
/* Redistributions in binary form must reproduce the above copyright                */
19
/* notice, this list of conditions and the following disclaimer in the                */
20
/* documentation and/or other materials provided with the distribution.                */
21
/*                                                                                 */
22
/* Neither the names of the IBM Corporation nor the names of its                */
23
/* contributors may be used to endorse or promote products derived from                */
24
/* this software without specific prior written permission.                        */
25
/*                                                                                 */
26
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS                */
27
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT                */
28
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR        */
29
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
30
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,        */
31
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT                */
32
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,        */
33
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY        */
34
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT                */
35
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE        */
36
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
37
/********************************************************************************/
38

39
#include <assert.h>
40

41
#include "BackwardsCompatibilityObject.h"
42

43
#ifndef static_assert
44
#define static_assert(test, msg)
45
#endif
46

47
/* The following are data structure from libtpms 0.7.x with RSA 2048 support
48
 * that help to resume key and hash contexts (TPM2_ContextSave/Load) from this
49
 * earlier version. All structures that have different sizes in 0.8 are found
50
 * here.
51
 */
52
typedef union {
53
    struct {
54
        UINT16                  size;
55
        BYTE                    buffer[2048/8];
56
    }            t;
57
    TPM2B        b;
58
} OLD_TPM2B_PUBLIC_KEY_RSA;
59

60
typedef union {
61
    TPM2B_DIGEST             keyedHash;
62
    TPM2B_DIGEST             sym;
63
    OLD_TPM2B_PUBLIC_KEY_RSA rsa;
64
    TPMS_ECC_POINT           ecc;
65
//    TPMS_DERIVE             derive;
66
} OLD_TPMU_PUBLIC_ID;
67

68
typedef struct {
69
    TPMI_ALG_PUBLIC         type;
70
    TPMI_ALG_HASH           nameAlg;
71
    TPMA_OBJECT             objectAttributes;
72
    TPM2B_DIGEST            authPolicy;
73
    TPMU_PUBLIC_PARMS       parameters;
74
    OLD_TPMU_PUBLIC_ID      unique;
75
} OLD_TPMT_PUBLIC;
76

77
static_assert(sizeof(OLD_TPMT_PUBLIC) == 356,
78
              "OLD_TPMT_PUBLIC has wrong size");
79

80
typedef union {
81
    struct {
82
        UINT16                  size;
83
        BYTE                    buffer[((2048/8)/2)*5];
84
    }            t;
85
    TPM2B        b;
86
} OLD_TPM2B_PRIVATE_KEY_RSA;
87

88
static_assert(sizeof(OLD_TPM2B_PRIVATE_KEY_RSA) == 642,
89
              "OLD_TPM2B_PRIVATE_KEY_RSA has wrong size");
90

91
typedef union {
92
    struct {
93
        UINT16                  size;
94
        BYTE                    buffer[((2048/8)/2)*5];
95
    }            t;
96
    TPM2B        b;
97
} OLD_TPM2B_PRIVATE_VENDOR_SPECIFIC;
98

99
typedef union {
100
    OLD_TPM2B_PRIVATE_KEY_RSA         rsa;
101
    TPM2B_ECC_PARAMETER               ecc;
102
    TPM2B_SENSITIVE_DATA              bits;
103
    TPM2B_SYM_KEY                     sym;
104
    OLD_TPM2B_PRIVATE_VENDOR_SPECIFIC any;
105
} OLD_TPMU_SENSITIVE_COMPOSITE;
106

107
typedef struct {
108
    TPMI_ALG_PUBLIC              sensitiveType;
109
    TPM2B_AUTH                   authValue;
110
    TPM2B_DIGEST                 seedValue;
111
    OLD_TPMU_SENSITIVE_COMPOSITE sensitive;
112
} OLD_TPMT_SENSITIVE;
113

114
static_assert(sizeof(OLD_TPMT_SENSITIVE) == 776,
115
              "OLD_TPMT_SENSITIVE has wrong size");
116

117
BN_TYPE(old_prime, (2048 / 2));
118

119
typedef struct OLD_privateExponent
120
{
121
    bn_old_prime_t          Q;
122
    bn_old_prime_t          dP;
123
    bn_old_prime_t          dQ;
124
    bn_old_prime_t          qInv;
125
} OLD_privateExponent_t;
126

127
static inline void CopyFromOldPrimeT(bn_prime_t *dst,
128
                                     const bn_old_prime_t *src)
129
{
130
    dst->allocated = src->allocated;
×
131
    dst->size = src->size;
×
132
    memcpy(dst->d, src->d, sizeof(src->d));
×
133
}
134

135
static_assert(sizeof(OLD_privateExponent_t) == 608,
136
              "OLD_privateExponent_t has wrong size");
137

138
typedef struct OLD_OBJECT
139
{
140
    // The attributes field is required to be first followed by the publicArea.
141
    // This allows the overlay of the object structure and a sequence structure
142
    OBJECT_ATTRIBUTES   attributes;         // object attributes
143
    OLD_TPMT_PUBLIC     publicArea;         // public area of an object
144
    OLD_TPMT_SENSITIVE  sensitive;          // sensitive area of an object
145
    OLD_privateExponent_t privateExponent;  // Additional field for the private
146
    TPM2B_NAME          qualifiedName;      // object qualified name
147
    TPMI_DH_OBJECT      evictHandle;        // if the object is an evict object,
148
    // the original handle is kept here.
149
    // The 'working' handle will be the
150
    // handle of an object slot.
151
    TPM2B_NAME          name;               // Name of the object name. Kept here
152
    // to avoid repeatedly computing it.
153

154
    // libtpms added: OBJECT lies in NVRAM; to avoid that it needs different number
155
    // of bytes on 32 bit and 64 bit architectures, we need to make sure it's the
156
    // same size; simple padding at the end works here
157
    UINT32             _pad;
158
} OLD_OBJECT;
159

160
static_assert(sizeof(OLD_OBJECT) == 1896,
161
              "OLD_OBJECT has wrong size");
162

163
// Convert an OLD_OBJECT that was copied into buffer using MemoryCopy
164
TPM_RC
165
OLD_OBJECTToOBJECT(OBJECT *newObject, BYTE *buffer, INT32 size)
×
166
{
167
    OLD_OBJECT    oldObject;
×
168
    TPM_RC        rc = 0;
×
169

170
    // get the attributes
171
    MemoryCopy(newObject, buffer, sizeof(newObject->attributes));
×
172
    if (ObjectIsSequence(newObject))
×
173
        {
174
            /* resuming old hash contexts is not supported */
175
            rc = TPM_RC_DISABLED;
176
        }
177
    else
178
        {
179
            if (size != sizeof(OLD_OBJECT))
×
180
                return TPM_RC_SIZE;
181
            MemoryCopy(&oldObject, buffer, sizeof(OLD_OBJECT));
×
182

183
            /* fill the newObject with the contents of the oldObject */
184
            newObject->attributes = oldObject.attributes;
×
185

186
            newObject->publicArea.type = oldObject.publicArea.type;
×
187
            newObject->publicArea.nameAlg = oldObject.publicArea.nameAlg;
×
188
            newObject->publicArea.objectAttributes = oldObject.publicArea.objectAttributes;
×
189
            newObject->publicArea.authPolicy = oldObject.publicArea.authPolicy;
×
190
            newObject->publicArea.parameters = oldObject.publicArea.parameters;
×
191
            /* the unique part can be one or two TPM2B's */
192
            switch (newObject->publicArea.type) {
×
193
            case TPM_ALG_KEYEDHASH:
×
194
                MemoryCopy2B(&newObject->publicArea.unique.keyedHash.b,
×
195
                             &oldObject.publicArea.unique.keyedHash.b,
196
                             sizeof(oldObject.publicArea.unique.keyedHash.t.buffer));
197
                break;
×
198
            case TPM_ALG_SYMCIPHER:
×
199
                MemoryCopy2B(&newObject->publicArea.unique.sym.b,
×
200
                             &oldObject.publicArea.unique.sym.b,
201
                             sizeof(oldObject.publicArea.unique.sym.t.buffer));
202
                break;
×
203
            case TPM_ALG_RSA:
×
204
                MemoryCopy2B(&newObject->publicArea.unique.rsa.b,
×
205
                             &oldObject.publicArea.unique.rsa.b,
206
                             sizeof(oldObject.publicArea.unique.rsa.t.buffer));
207
                break;
×
208
            case TPM_ALG_ECC:
×
209
                MemoryCopy2B(&newObject->publicArea.unique.ecc.x.b,
×
210
                             &oldObject.publicArea.unique.ecc.x.b,
211
                             sizeof(oldObject.publicArea.unique.ecc.x.t.buffer));
212
                MemoryCopy2B(&newObject->publicArea.unique.ecc.y.b,
×
213
                             &oldObject.publicArea.unique.ecc.y.b,
214
                             sizeof(oldObject.publicArea.unique.ecc.y.t.buffer));
215
                break;
×
216
            }
217

218
            newObject->sensitive.sensitiveType = oldObject.sensitive.sensitiveType;
×
219
            newObject->sensitive.authValue = oldObject.sensitive.authValue;
×
220
            newObject->sensitive.seedValue = oldObject.sensitive.seedValue;
×
221
            /* The OLD_TPMU_SENSITIVE_COMPOSITE is always a TPM2B */
222
            MemoryCopy2B(&newObject->sensitive.sensitive.any.b,
×
223
                         &oldObject.sensitive.sensitive.any.b,
224
                         sizeof(oldObject.sensitive.sensitive.any.t.buffer));
225

226
            CopyFromOldPrimeT(&newObject->privateExponent.Q, &oldObject.privateExponent.Q);
×
227
            CopyFromOldPrimeT(&newObject->privateExponent.dP, &oldObject.privateExponent.dP);
×
228
            CopyFromOldPrimeT(&newObject->privateExponent.dQ, &oldObject.privateExponent.dQ);
×
229
            CopyFromOldPrimeT(&newObject->privateExponent.qInv, &oldObject.privateExponent.qInv);
×
230

231
            newObject->qualifiedName = oldObject.qualifiedName;
×
232
            newObject->evictHandle = oldObject.evictHandle;
×
233
            newObject->name = oldObject.name;
×
234
    }
235

236
    return rc;
237
}
238

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