Home | History | Annotate | Download | only in tpm2
      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 {
     31     BYTE      *pb = outVal->buffer;
     32     outVal->size = size;
     33     size = size - (((UINT16) BN_num_bits(inVal) + 7) / 8);
     34     if(size < 0)
     35         return FALSE;
     36     for(;size > 0; size--)
     37         *pb++ = 0;
     38     BN_bn2bin(inVal, pb);
     39     return TRUE;
     40 }
     41 //
     42 //
     43 //      Copy2B()
     44 //
     45 //     This function copies a TPM2B structure. The compiler can't generate a copy of a TPM2B generic
     46 //     structure because the actual size is not known. This function performs the copy on any TPM2B pair. The
     47 //     size of the destination should have been checked before this call to make sure that it will hold the TPM2B
     48 //     being copied.
     49 //     This replicates the functionality in the MemoryLib.c.
     50 //
     51 void
     52 Copy2B(
     53     TPM2B               *out,                // OUT: The TPM2B to receive the copy
     54     TPM2B               *in                  // IN: the TPM2B to copy
     55     )
     56 {
     57     BYTE        *pIn = in->buffer;
     58     BYTE        *pOut = out->buffer;
     59     int          count;
     60     out->size = in->size;
     61     for(count = in->size; count > 0; count--)
     62        *pOut++ = *pIn++;
     63    return;
     64 }
     65 //
     66 //
     67 //      BnFrom2B()
     68 //
     69 //     This function creates a BIGNUM from a TPM2B and fails if the conversion fails.
     70 //
     71 BIGNUM *
     72 BnFrom2B(
     73    BIGNUM              *out,              // OUT: The BIGNUM
     74    const TPM2B         *in                // IN: the TPM2B to copy
     75    )
     76 {
     77    if(BN_bin2bn(in->buffer, in->size, out) == NULL)
     78        FAIL(FATAL_ERROR_INTERNAL);
     79    return out;
     80 }
     81