Skip to content

Commit 48338d3

Browse files
committed
fix f-1370 key_len size check for void* in wc_SignatureGetSize
1 parent 68eaf67 commit 48338d3

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

doc/dox_comments/header_files/signature.h

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
\brief This function returns the maximum size of the resulting signature.
55
66
\return Returns SIG_TYPE_E if sig_type is not supported. Returns
7-
BAD_FUNC_ARG if sig_type was invalid. A positive return value indicates
7+
BAD_FUNC_ARG if sig_type was invalid or key_len does not exactly match
8+
the size of the expected key structure. A positive return value indicates
89
the maximum size of a signature.
910
1011
\param sig_type A signature type enum value such as
1112
WC_SIGNATURE_TYPE_ECC or WC_SIGNATURE_TYPE_RSA.
12-
\param key Pointer to a key structure such as ecc_key or RsaKey.
13-
\param key_len Size of the key structure.
13+
\param key Pointer to the key structure corresponding to sig_type:
14+
pass an ecc_key* (cast to const void*) for
15+
WC_SIGNATURE_TYPE_ECC, or a RsaKey* for
16+
WC_SIGNATURE_TYPE_RSA / WC_SIGNATURE_TYPE_RSA_W_ENC.
17+
The caller is responsible for ensuring the pointer refers to the correct
18+
type; this function cannot verify the actual runtime type of the object.
19+
\param key_len Must be exactly sizeof(ecc_key) or
20+
sizeof(RsaKey) matching the sig_type. Passing any other value
21+
causes the function to return BAD_FUNC_ARG without dereferencing key.
22+
The conventional idiom is to pass sizeof(*key) at the call site.
1423
1524
_Example_
1625
\code
@@ -43,16 +52,19 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type,
4352
\return BAD_FUNC_ARG -173, bad function argument provided
4453
\return BUFFER_E -132, output buffer too small or input too large.
4554
46-
\param hash_type A hash type from the enum wc_HashType such as
47-
WC_HASH_TYPE_SHA256.
55+
\param hash_type A hash type from the "enum wc_HashType" such as
56+
"WC_HASH_TYPE_SHA256".
4857
\param sig_type A signature type enum value such as
4958
WC_SIGNATURE_TYPE_ECC or WC_SIGNATURE_TYPE_RSA.
5059
\param data Pointer to buffer containing the data to hash.
5160
\param data_len Length of the data buffer.
5261
\param sig Pointer to buffer to output signature.
5362
\param sig_len Length of the signature output buffer.
54-
\param key Pointer to a key structure such as ecc_key or RsaKey.
55-
\param key_len Size of the key structure.
63+
\param key Pointer to the key structure corresponding to sig_type.
64+
See wc_SignatureGetSize() for the type-safety constraints that apply
65+
to this parameter.
66+
\param key_len Must be exactly sizeof(ecc_key) or
67+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
5668
5769
_Example_
5870
\code
@@ -93,16 +105,19 @@ int wc_SignatureVerify(
93105
\return BAD_FUNC_ARG -173, bad function argument provided
94106
\return BUFFER_E -132, output buffer too small or input too large.
95107
96-
\param hash_type A hash type from the enum wc_HashType
97-
such as WC_HASH_TYPE_SHA256.
108+
\param hash_type A hash type from the "enum wc_HashType"
109+
such as "WC_HASH_TYPE_SHA256".
98110
\param sig_type A signature type enum value such as
99111
WC_SIGNATURE_TYPE_ECC or WC_SIGNATURE_TYPE_RSA.
100112
\param data Pointer to buffer containing the data to hash.
101113
\param data_len Length of the data buffer.
102114
\param sig Pointer to buffer to output signature.
103115
\param sig_len Length of the signature output buffer.
104-
\param key Pointer to a key structure such as ecc_key or RsaKey.
105-
\param key_len Size of the key structure.
116+
\param key Pointer to the key structure corresponding to sig_type.
117+
See wc_SignatureGetSize() for the type-safety constraints that apply
118+
to this parameter.
119+
\param key_len Must be exactly sizeof(ecc_key) or
120+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
106121
\param rng Pointer to an initialized RNG structure.
107122
108123
_Example_
@@ -166,8 +181,11 @@ int wc_SignatureGenerate(
166181
\param hash_len Length of the hash buffer
167182
\param sig Pointer to buffer containing the signature
168183
\param sig_len Length of the signature buffer
169-
\param key Pointer to a key structure such as ecc_key or RsaKey
170-
\param key_len Size of the key structure
184+
\param key Pointer to the key structure corresponding to sig_type.
185+
See wc_SignatureGetSize() for the type-safety constraints that apply
186+
to this parameter.
187+
\param key_len Must be exactly sizeof(ecc_key) or
188+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
171189
172190
_Example_
173191
\code
@@ -216,8 +234,11 @@ int wc_SignatureVerifyHash(enum wc_HashType hash_type,
216234
\param hash_len Length of the hash buffer
217235
\param sig Pointer to buffer to output signature
218236
\param sig_len Pointer to length of signature output buffer
219-
\param key Pointer to a key structure such as ecc_key or RsaKey
220-
\param key_len Size of the key structure
237+
\param key Pointer to the key structure corresponding to sig_type.
238+
See wc_SignatureGetSize() for the type-safety constraints that apply
239+
to this parameter.
240+
\param key_len Must be exactly sizeof(ecc_key) or
241+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
221242
\param rng Pointer to an initialized RNG structure
222243
223244
_Example_
@@ -266,8 +287,11 @@ int wc_SignatureGenerateHash(enum wc_HashType hash_type,
266287
\param hash_len Length of the hash buffer
267288
\param sig Pointer to buffer to output signature
268289
\param sig_len Pointer to length of signature output buffer
269-
\param key Pointer to a key structure such as ecc_key or RsaKey
270-
\param key_len Size of the key structure
290+
\param key Pointer to the key structure corresponding to sig_type.
291+
See wc_SignatureGetSize() for the type-safety constraints that apply
292+
to this parameter.
293+
\param key_len Must be exactly sizeof(ecc_key) or
294+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
271295
\param rng Pointer to an initialized RNG structure
272296
\param verify If non-zero, verify the signature after generation
273297
@@ -317,8 +341,11 @@ int wc_SignatureGenerateHash_ex(enum wc_HashType hash_type,
317341
\param data_len Length of the data buffer
318342
\param sig Pointer to buffer to output signature
319343
\param sig_len Pointer to length of signature output buffer
320-
\param key Pointer to a key structure such as ecc_key or RsaKey
321-
\param key_len Size of the key structure
344+
\param key Pointer to the key structure corresponding to sig_type.
345+
See wc_SignatureGetSize() for the type-safety constraints that apply
346+
to this parameter.
347+
\param key_len Must be exactly sizeof(ecc_key) or
348+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
322349
\param rng Pointer to an initialized RNG structure
323350
\param verify If non-zero, verify the signature after generation
324351

tests/api/test_signature.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ int test_wc_SignatureGetSize_ecc(void)
6868
sig_type = WC_SIGNATURE_TYPE_ECC;
6969
ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
7070
key_len = (word32)0;
71+
ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len),
72+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
73+
/* key_len must be exactly sizeof(ecc_key): one less or one more is invalid */
74+
key_len = (word32)(sizeof(ecc_key) - 1);
75+
ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len),
76+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
77+
key_len = (word32)(sizeof(ecc_key) + 1);
7178
ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len),
7279
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
7380

@@ -138,6 +145,13 @@ int test_wc_SignatureGetSize_rsa(void)
138145
ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len),
139146
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
140147
key_len = (word32)0;
148+
ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len),
149+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
150+
/* key_len must be exactly sizeof(RsaKey): one less or one more is invalid */
151+
key_len = (word32)(sizeof(RsaKey) - 1);
152+
ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len),
153+
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
154+
key_len = (word32)(sizeof(RsaKey) + 1);
141155
ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len),
142156
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
143157

tests/api/test_signature.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ int test_wc_SignatureGetSize_rsa(void);
2929

3030
#define TEST_SIGNATURE_DECLS \
3131
TEST_DECL_GROUP("signature", test_wc_SignatureGetSize_ecc), \
32-
TEST_DECL_GROUP("signature", test_wc_SignatureGetSize_ecc)
32+
TEST_DECL_GROUP("signature", test_wc_SignatureGetSize_rsa)
3333

3434
#endif /* WOLFCRYPT_TEST_SIGNATURE_H */

wolfcrypt/src/signature.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type,
9393
switch(sig_type) {
9494
case WC_SIGNATURE_TYPE_ECC:
9595
#ifdef HAVE_ECC
96-
/* Sanity check that void* key is at least ecc_key in size */
97-
if (key_len >= sizeof(ecc_key)) {
96+
/* Verify that key_len matches exactly sizeof(ecc_key).
97+
* This is a necessary but not sufficient type check: the void*
98+
* API cannot verify the actual runtime type of the pointed-to
99+
* object. Callers must pass a valid ecc_key* cast to void*. */
100+
if (key_len == sizeof(ecc_key)) {
98101
sig_len = wc_ecc_sig_size((ecc_key*)key);
99102
}
100103
else {
@@ -108,8 +111,10 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type,
108111
case WC_SIGNATURE_TYPE_RSA_W_ENC:
109112
case WC_SIGNATURE_TYPE_RSA:
110113
#ifndef NO_RSA
111-
/* Sanity check that void* key is at least RsaKey in size */
112-
if (key_len >= sizeof(RsaKey)) {
114+
/* Verify that key_len matches exactly sizeof(RsaKey).
115+
* Same caveat as the ECC case above: size equality is necessary
116+
* but not sufficient; the caller must pass a valid RsaKey*. */
117+
if (key_len == sizeof(RsaKey)) {
113118
sig_len = wc_RsaEncryptSize((RsaKey*)key);
114119
}
115120
else {

0 commit comments

Comments
 (0)