Skip to content

Commit ae0a387

Browse files
authored
Merge pull request #10122 from miyazakh/f-1370_SigGetSize
F-1370 : Tighten key_len check from `>=` to `==`
2 parents e673316 + e04fe0c commit ae0a387

File tree

4 files changed

+73
-24
lines changed

4 files changed

+73
-24
lines changed

doc/dox_comments/header_files/signature.h

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@
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* for WC_SIGNATURE_TYPE_ECC, or a RsaKey* for
15+
WC_SIGNATURE_TYPE_RSA / WC_SIGNATURE_TYPE_RSA_W_ENC.
16+
The caller is responsible for ensuring the pointer refers to the correct
17+
type; this function cannot verify the actual runtime type of the object.
18+
\param key_len If key is non-NULL, key_len must be exactly sizeof(ecc_key)
19+
or sizeof(RsaKey) matching the sig_type. Passing any other value
20+
causes the function to return BAD_FUNC_ARG without dereferencing key.
21+
Always pass the size of the concrete key type at the call site: if you
22+
have a typed pointer (e.g., ecc_key* k), use sizeof(*k); otherwise use
23+
sizeof(ecc_key) or sizeof(RsaKey) directly. Do not use sizeof(*key)
24+
on the const void* parameter itself, as dereferencing void is invalid.
1425
1526
_Example_
1627
\code
@@ -43,16 +54,19 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type,
4354
\return BAD_FUNC_ARG -173, bad function argument provided
4455
\return BUFFER_E -132, output buffer too small or input too large.
4556
46-
\param hash_type A hash type from the enum wc_HashType such as
47-
WC_HASH_TYPE_SHA256.
57+
\param hash_type A hash type from the "enum wc_HashType" such as
58+
"WC_HASH_TYPE_SHA256".
4859
\param sig_type A signature type enum value such as
4960
WC_SIGNATURE_TYPE_ECC or WC_SIGNATURE_TYPE_RSA.
5061
\param data Pointer to buffer containing the data to hash.
5162
\param data_len Length of the data buffer.
5263
\param sig Pointer to buffer to output signature.
5364
\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.
65+
\param key Pointer to the key structure corresponding to sig_type.
66+
See wc_SignatureGetSize() for the type-safety constraints that apply
67+
to this parameter.
68+
\param key_len Must be exactly sizeof(ecc_key) or
69+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
5670
5771
_Example_
5872
\code
@@ -93,16 +107,19 @@ int wc_SignatureVerify(
93107
\return BAD_FUNC_ARG -173, bad function argument provided
94108
\return BUFFER_E -132, output buffer too small or input too large.
95109
96-
\param hash_type A hash type from the enum wc_HashType
97-
such as WC_HASH_TYPE_SHA256.
110+
\param hash_type A hash type from the "enum wc_HashType"
111+
such as "WC_HASH_TYPE_SHA256".
98112
\param sig_type A signature type enum value such as
99113
WC_SIGNATURE_TYPE_ECC or WC_SIGNATURE_TYPE_RSA.
100114
\param data Pointer to buffer containing the data to hash.
101115
\param data_len Length of the data buffer.
102116
\param sig Pointer to buffer to output signature.
103117
\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.
118+
\param key Pointer to the key structure corresponding to sig_type.
119+
See wc_SignatureGetSize() for the type-safety constraints that apply
120+
to this parameter.
121+
\param key_len Must be exactly sizeof(ecc_key) or
122+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
106123
\param rng Pointer to an initialized RNG structure.
107124
108125
_Example_
@@ -166,8 +183,11 @@ int wc_SignatureGenerate(
166183
\param hash_len Length of the hash buffer
167184
\param sig Pointer to buffer containing the signature
168185
\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
186+
\param key Pointer to the key structure corresponding to sig_type.
187+
See wc_SignatureGetSize() for the type-safety constraints that apply
188+
to this parameter.
189+
\param key_len Must be exactly sizeof(ecc_key) or
190+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
171191
172192
_Example_
173193
\code
@@ -216,8 +236,11 @@ int wc_SignatureVerifyHash(enum wc_HashType hash_type,
216236
\param hash_len Length of the hash buffer
217237
\param sig Pointer to buffer to output signature
218238
\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
239+
\param key Pointer to the key structure corresponding to sig_type.
240+
See wc_SignatureGetSize() for the type-safety constraints that apply
241+
to this parameter.
242+
\param key_len Must be exactly sizeof(ecc_key) or
243+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
221244
\param rng Pointer to an initialized RNG structure
222245
223246
_Example_
@@ -266,8 +289,11 @@ int wc_SignatureGenerateHash(enum wc_HashType hash_type,
266289
\param hash_len Length of the hash buffer
267290
\param sig Pointer to buffer to output signature
268291
\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
292+
\param key Pointer to the key structure corresponding to sig_type.
293+
See wc_SignatureGetSize() for the type-safety constraints that apply
294+
to this parameter.
295+
\param key_len Must be exactly sizeof(ecc_key) or
296+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
271297
\param rng Pointer to an initialized RNG structure
272298
\param verify If non-zero, verify the signature after generation
273299
@@ -317,8 +343,11 @@ int wc_SignatureGenerateHash_ex(enum wc_HashType hash_type,
317343
\param data_len Length of the data buffer
318344
\param sig Pointer to buffer to output signature
319345
\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
346+
\param key Pointer to the key structure corresponding to sig_type.
347+
See wc_SignatureGetSize() for the type-safety constraints that apply
348+
to this parameter.
349+
\param key_len Must be exactly sizeof(ecc_key) or
350+
sizeof(RsaKey) matching sig_type. See wc_SignatureGetSize().
322351
\param rng Pointer to an initialized RNG structure
323352
\param verify If non-zero, verify the signature after generation
324353

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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ 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:
98+
* the const void* API cannot verify the actual runtime
99+
* type of the pointed-to object.
100+
* Callers must pass a valid ecc_key* cast to const void*. */
101+
if ((size_t)key_len == sizeof(ecc_key)) {
98102
#if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && FIPS_VERSION3_LT(5,0,0))
99103
sig_len = wc_ecc_sig_size((ecc_key*)(wc_ptr_t)key);
100104
#else
@@ -112,8 +116,10 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type,
112116
case WC_SIGNATURE_TYPE_RSA_W_ENC:
113117
case WC_SIGNATURE_TYPE_RSA:
114118
#ifndef NO_RSA
115-
/* Sanity check that void* key is at least RsaKey in size */
116-
if (key_len >= sizeof(RsaKey)) {
119+
/* Verify that key_len matches exactly sizeof(RsaKey).
120+
* Same caveat as the ECC case above: size equality is necessary
121+
* but not sufficient; the caller must pass a valid RsaKey*. */
122+
if ((size_t)key_len == sizeof(RsaKey)) {
117123
#if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && FIPS_VERSION3_LT(5,0,0))
118124
sig_len = wc_RsaEncryptSize((RsaKey*)(wc_ptr_t)key);
119125
#else

0 commit comments

Comments
 (0)