Coverage Report

Created: 2025-07-18 06:04

/src/tpm2/CpriMisc.c
Line
Count
Source (jump to first uncovered line)
1
// This file was extracted from the TCG Published
2
// Trusted Platform Module Library
3
// Part 4: Supporting Routines
4
// Family "2.0"
5
// Level 00 Revision 01.16
6
// October 30, 2014
7
8
#include "OsslCryptoEngine.h"
9
//
10
//
11
//      Functions
12
//
13
//      BnTo2B()
14
//
15
//     This function is used to convert a BigNum() to a byte array of the specified size. If the number is too large
16
//     to fit, then 0 is returned. Otherwise, the number is converted into the low-order bytes of the provided array
17
//     and the upper bytes are set to zero.
18
//
19
//     Return Value                     Meaning
20
//
21
//     0                                failure (probably fatal)
22
//     1                                conversion successful
23
//
24
BOOL
25
BnTo2B(
26
    TPM2B               *outVal,             // OUT: place for the result
27
    BIGNUM              *inVal,              // IN: number to convert
28
    UINT16               size                // IN: size of the output.
29
    )
30
10
{
31
10
    BYTE      *pb = outVal->buffer;
32
10
    UINT16    unpaddedSize = (((UINT16) BN_num_bits(inVal) + 7) / 8);
33
10
    outVal->size = size;
34
10
    if(size < unpaddedSize)
35
0
        return FALSE;
36
37
10
    size -= unpaddedSize;
38
13
    for(;size > 0; size--)
39
3
        *pb++ = 0;
40
10
    BN_bn2bin(inVal, pb);
41
10
    return TRUE;
42
10
}
43
//
44
//
45
//      Copy2B()
46
//
47
//     This function copies a TPM2B structure. The compiler can't generate a copy of a TPM2B generic
48
//     structure because the actual size is not known. This function performs the copy on any TPM2B pair. The
49
//     size of the destination should have been checked before this call to make sure that it will hold the TPM2B
50
//     being copied.
51
//     This replicates the functionality in the MemoryLib.c.
52
//
53
void
54
Copy2B(
55
    TPM2B               *out,                // OUT: The TPM2B to receive the copy
56
    TPM2B               *in                  // IN: the TPM2B to copy
57
    )
58
0
{
59
0
    BYTE        *pIn = in->buffer;
60
0
    BYTE        *pOut = out->buffer;
61
0
    int          count;
62
0
    out->size = in->size;
63
0
    for(count = in->size; count > 0; count--)
64
0
       *pOut++ = *pIn++;
65
0
   return;
66
0
}
67
//
68
//
69
//      BnFrom2B()
70
//
71
//     This function creates a BIGNUM from a TPM2B and fails if the conversion fails.
72
//
73
BIGNUM *
74
BnFrom2B(
75
   BIGNUM              *out,              // OUT: The BIGNUM
76
   const TPM2B         *in                // IN: the TPM2B to copy
77
   )
78
24
{
79
24
   if(BN_bin2bn(in->buffer, in->size, out) == NULL)
80
0
       FAIL(FATAL_ERROR_INTERNAL);
81
24
   return out;
82
24
}