Skip to content

Commit f9167ce

Browse files
committed
Expose TPM2_ASN_TrimZeros.
1 parent cb65f93 commit f9167ce

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/tpm2_asn.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
#ifndef WOLFTPM2_NO_ASN
2929

30-
#if defined(HAVE_ECC) && (defined(WOLFTPM_CRYPTOCB) || \
31-
(defined(HAVE_PK_CALLBACKS) && !defined(WOLFCRYPT_ONLY)))
3230
/* Helper to trim leading zeros when not required */
3331
byte* TPM2_ASN_TrimZeros(byte* in, word32* len)
3432
{
@@ -40,7 +38,6 @@ byte* TPM2_ASN_TrimZeros(byte* in, word32* len)
4038
*len -= idx;
4139
return in;
4240
}
43-
#endif
4441

4542
int TPM2_ASN_GetLength_ex(const uint8_t* input, word32* inOutIdx, int* len,
4643
word32 maxIdx, int check)

tests/unit_tests.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ static void test_wolfTPM2_CSR(void)
397397
#endif
398398
}
399399

400-
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC)
400+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \
401+
!defined(WOLFTPM2_NO_ASN)
401402
static void test_wolfTPM2_EccSignVerifyDig(const byte* digest, int digestSz,
402403
TPM_ECC_CURVE curve, TPMI_ALG_HASH hashAlg)
403404
{
@@ -829,7 +830,8 @@ int unit_tests(int argc, char *argv[])
829830
test_wolfTPM2_KeyBlob(TPM_ALG_ECC);
830831
test_wolfTPM2_Cleanup();
831832
test_wolfTPM2_thread_local_storage();
832-
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC)
833+
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(HAVE_ECC) && \
834+
!defined(WOLFTPM2_NO_ASN)
833835
test_wolfTPM2_EccSignVerify();
834836
#endif
835837
#endif /* !WOLFTPM2_NO_WRAPPER */

wolftpm/tpm2_asn.h

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ typedef struct DecodedX509 {
8080
\param maxIdx Maximum allowed index in buffer
8181
\return Length on success, TPM_RC_INSUFFICIENT on buffer error
8282
*/
83-
WOLFTPM_API int TPM2_ASN_GetLength(const uint8_t* input, word32* inOutIdx, int* len,
84-
word32 maxIdx);
83+
WOLFTPM_API int TPM2_ASN_GetLength(const uint8_t* input, word32* inOutIdx,
84+
int* len, word32 maxIdx);
85+
8586
/*!
8687
\ingroup ASN
8788
\brief Decodes ASN.1 length with optional length checking
@@ -92,8 +93,9 @@ WOLFTPM_API int TPM2_ASN_GetLength(const uint8_t* input, word32* inOutIdx, int*
9293
\param check Flag to enable length validation
9394
\return Length on success, TPM_RC_INSUFFICIENT on buffer error
9495
*/
95-
WOLFTPM_API int TPM2_ASN_GetLength_ex(const uint8_t* input, word32* inOutIdx, int* len,
96-
word32 maxIdx, int check);
96+
WOLFTPM_API int TPM2_ASN_GetLength_ex(const uint8_t* input, word32* inOutIdx,
97+
int* len, word32 maxIdx, int check);
98+
9799
/*!
98100
\ingroup ASN
99101
\brief Decodes ASN.1 tag and validates length
@@ -104,44 +106,59 @@ WOLFTPM_API int TPM2_ASN_GetLength_ex(const uint8_t* input, word32* inOutIdx, in
104106
\param tag Expected ASN.1 tag value
105107
\return 0 on success, TPM_RC_INSUFFICIENT on buffer error, TPM_RC_VALUE on tag mismatch
106108
*/
107-
WOLFTPM_API int TPM2_ASN_DecodeTag(const uint8_t* input, int inputSz, int* inOutIdx, int* tag_len, uint8_t tag);
109+
WOLFTPM_API int TPM2_ASN_DecodeTag(const uint8_t* input, int inputSz,
110+
int* inOutIdx, int* tag_len, uint8_t tag);
111+
108112
/*!
109113
\ingroup ASN
110114
\brief Decodes RSA signature from ASN.1 format
111115
\param pInput Pointer to buffer containing ASN.1 encoded RSA signature
112116
\param inputSz Size of input buffer
113-
\return Size of decoded signature on success, TPM_RC_VALUE on invalid input, TPM_RC_INSUFFICIENT on buffer error
117+
\return Size of decoded signature on success, TPM_RC_VALUE on invalid input,
118+
TPM_RC_INSUFFICIENT on buffer error
114119
*/
115120
WOLFTPM_API int TPM2_ASN_RsaDecodeSignature(uint8_t** pInput, int inputSz);
121+
116122
/*!
117123
\brief Decodes an X.509 certificate
118124
\param input Buffer containing ASN.1 encoded X.509 certificate
119125
\param inputSz Size of input buffer
120126
\param x509 Structure to store decoded certificate data
121127
\return 0 on success, TPM_RC_VALUE on invalid input, TPM_RC_INSUFFICIENT on buffer error
122128
*/
123-
WOLFTPM_API int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz, DecodedX509* x509);
129+
WOLFTPM_API int TPM2_ASN_DecodeX509Cert(uint8_t* input, int inputSz,
130+
DecodedX509* x509);
131+
124132
/*!
125133
\ingroup ASN
126134
\brief Decodes RSA public key from ASN.1 format into TPM2B_PUBLIC structure
127135
\param input Buffer containing ASN.1 encoded RSA public key
128136
\param inputSz Size of input buffer
129137
\param pub TPM2B_PUBLIC structure to store decoded key
130-
\return 0 on success, TPM_RC_VALUE on invalid input, TPM_RC_INSUFFICIENT on buffer error
138+
\return 0 on success, TPM_RC_VALUE on invalid input,
139+
TPM_RC_INSUFFICIENT on buffer error
131140
*/
132-
WOLFTPM_API int TPM2_ASN_DecodeRsaPubKey(uint8_t* input, int inputSz, TPM2B_PUBLIC* pub);
141+
WOLFTPM_API int TPM2_ASN_DecodeRsaPubKey(uint8_t* input, int inputSz,
142+
TPM2B_PUBLIC* pub);
143+
133144
/*!
134145
\ingroup ASN
135146
\brief Removes PKCS#1 v1.5 padding from RSA signature
136-
\param pSig Pointer to buffer containing padded signature, updated to point to unpadded data
147+
\param pSig Pointer to buffer containing padded signature, updated to point
148+
to unpadded data
137149
\param sigSz Size of signature buffer, updated with unpadded size
138150
\return 0 on success, TPM_RC_VALUE on invalid padding
139151
*/
140152
WOLFTPM_API int TPM2_ASN_RsaUnpadPkcsv15(uint8_t** pSig, int* sigSz);
141153

142-
143-
WOLFTPM_LOCAL byte* TPM2_ASN_TrimZeros(byte* in, word32* len);
144-
154+
/*!
155+
\ingroup ASN
156+
\brief Removes leading zero bytes from a buffer
157+
\param in Pointer to input buffer containing data to trim
158+
\param len Pointer to length of input buffer, updated with new length after trimming
159+
\return Pointer to the trimmed buffer (may be same as input if no trimming needed)
160+
*/
161+
WOLFTPM_API byte* TPM2_ASN_TrimZeros(byte* in, word32* len);
145162

146163
#ifdef __cplusplus
147164
} /* extern "C" */

0 commit comments

Comments
 (0)