Skip to content

Commit 5790a03

Browse files
authored
Merge pull request #351 from ColtonWilley/wp_fips_cast_optimization
Optimize FIPS CAST startup tests
2 parents 98c0aea + 66e1ea1 commit 5790a03

17 files changed

+211
-35
lines changed

include/wolfprovider/internal.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,44 @@ int wp_provctx_lock_rng(WOLFPROV_CTX* provCtx);
180180
void wp_provctx_unlock_rng(WOLFPROV_CTX* provCtx);
181181

182182
#ifdef HAVE_FIPS
183-
wolfSSL_Mutex *wp_get_cast_mutex(void);
183+
/* CAST self-test algorithm categories */
184+
#define WP_CAST_ALGO_AES 0
185+
#define WP_CAST_ALGO_HMAC 1
186+
#define WP_CAST_ALGO_DRBG 2
187+
#define WP_CAST_ALGO_RSA 3
188+
#define WP_CAST_ALGO_ECDSA 4
189+
#define WP_CAST_ALGO_ECDH 5
190+
#define WP_CAST_ALGO_DH 6
191+
#define WP_CAST_ALGO_COUNT 7
192+
193+
int wp_init_cast(int algo);
194+
195+
/**
196+
* Check FIPS CAST for algorithm. Returns 0 on failure.
197+
* Use at function entry points that return int (1=success, 0=failure).
198+
*/
199+
#define WP_CHECK_FIPS_ALGO(algo) \
200+
do { \
201+
if (wp_init_cast(algo) != 1) { \
202+
return 0; \
203+
} \
204+
} while (0)
205+
206+
/**
207+
* Check FIPS CAST for algorithm. Returns NULL on failure.
208+
* Use at function entry points that return pointers (NULL=failure).
209+
*/
210+
#define WP_CHECK_FIPS_ALGO_PTR(algo) \
211+
do { \
212+
if (wp_init_cast(algo) != 1) { \
213+
return NULL; \
214+
} \
215+
} while (0)
216+
217+
#else
218+
/* Non-FIPS: no-op */
219+
#define WP_CHECK_FIPS_ALGO(algo) do { } while (0)
220+
#define WP_CHECK_FIPS_ALGO_PTR(algo) do { } while (0)
184221
#endif
185222
#endif
186223

include/wolfprovider/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
#define WP_HAVE_GMAC
7777
#endif
7878

79+
#ifndef NO_AES
80+
#define WP_HAVE_AES
81+
#endif
7982
#ifdef HAVE_AES_ECB
8083
#define WP_HAVE_AESECB
8184
#endif

src/wp_aes_aead.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,9 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key,
10221022
if (!wolfssl_prov_is_running()) {
10231023
ok = 0;
10241024
}
1025+
if (ok) {
1026+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
1027+
}
10251028
#ifdef WOLFSSL_AESGCM_STREAM
10261029
if (ok) {
10271030
int rc;
@@ -1108,6 +1111,9 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key,
11081111
if (!wolfssl_prov_is_running()) {
11091112
ok = 0;
11101113
}
1114+
if (ok) {
1115+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
1116+
}
11111117
#ifdef WOLFSSL_AESGCM_STREAM
11121118
if (ok && key != NULL) {
11131119
int rc = wc_AesGcmDecryptInit(aes, key, (word32)keyLen, iv, (word32)ivLen);
@@ -1754,6 +1760,9 @@ static int wp_aesccm_init(wp_AeadCtx* ctx, const unsigned char *key,
17541760
if (!wolfssl_prov_is_running()) {
17551761
ok = 0;
17561762
}
1763+
if (ok) {
1764+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
1765+
}
17571766
if (ok && (key != NULL)) {
17581767
rc = wc_AesCcmSetKey(&ctx->aes, key, (word32)keyLen);
17591768
if (rc != 0) {

src/wp_aes_block.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ static int wp_aes_block_init(wp_AesBlockCtx *ctx, const unsigned char *key,
328328
ok = 0;
329329
}
330330
if (ok) {
331-
int rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, ctx->iv,
331+
int rc;
332+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
333+
rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, ctx->iv,
332334
enc ? AES_ENCRYPTION : AES_DECRYPTION);
333335
if (rc != 0) {
334336
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesSetKey", rc);

src/wp_aes_stream.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,14 @@ static int wp_aes_stream_init(wp_AesStreamCtx *ctx, const unsigned char *key,
314314
ok = 0;
315315
}
316316
if (ok) {
317+
int rc;
317318
#if defined(WP_HAVE_AESCTS)
318319
if (ctx->mode == EVP_CIPH_CBC_MODE && !enc) {
319320
dir = AES_DECRYPTION;
320321
}
321322
#endif
322-
int rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, iv,
323+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
324+
rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, iv,
323325
dir);
324326
if (rc != 0) {
325327
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesSetKey", rc);

src/wp_aes_wrap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ static int wp_aes_wrap_init(wp_AesWrapCtx *ctx, const unsigned char *key,
266266
}
267267
if (ok) {
268268
#if LIBWOLFSSL_VERSION_HEX >= 0x05000000
269-
int rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, iv,
269+
int rc;
270+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_AES);
271+
rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, iv,
270272
wrap ? AES_ENCRYPTION : AES_DECRYPTION);
271273
if (rc != 0) {
272274
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesSetKey", rc);

src/wp_dh_kmgmt.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,9 @@ static int wp_dh_import(wp_Dh* dh, int selection, const OSSL_PARAM params[])
11561156
if (!wolfssl_prov_is_running()) {
11571157
ok = 0;
11581158
}
1159+
if (ok) {
1160+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_DH);
1161+
}
11591162
if (ok && (dh == NULL)) {
11601163
ok = 0;
11611164
}
@@ -1832,6 +1835,8 @@ static wp_Dh* wp_dh_gen(wp_DhGenCtx *ctx, OSSL_CALLBACK *cb, void *cbArg)
18321835
(void)cb;
18331836
(void)cbArg;
18341837

1838+
WP_CHECK_FIPS_ALGO_PTR(WP_CAST_ALGO_DH);
1839+
18351840
/* Create a new DH key object to hold generated data. */
18361841
dh = wp_dh_new(ctx->provCtx);
18371842
if (dh != NULL) {
@@ -2064,6 +2069,8 @@ static int wp_dh_decode_spki(wp_Dh* dh, unsigned char* data, word32 len)
20642069

20652070
WOLFPROV_ENTER_SILENT(WP_LOG_COMP_DH, WOLFPROV_FUNC_NAME);
20662071

2072+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_DH);
2073+
20672074
rc = wc_DhPublicKeyDecode(data, &idx, &dh->key, len);
20682075
if (rc != 0) {
20692076
ok = 0;
@@ -2127,6 +2134,8 @@ static int wp_dh_decode_pki(wp_Dh* dh, unsigned char* data, word32 len)
21272134

21282135
WOLFPROV_ENTER_SILENT(WP_LOG_COMP_DH, WOLFPROV_FUNC_NAME);
21292136

2137+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_DH);
2138+
21302139
rc = wc_DhKeyDecode(data, &idx, &dh->key, len);
21312140
if (rc != 0) {
21322141
ok = 0;

src/wp_ecc_kmgmt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,9 @@ static wp_Ecc* wp_ecc_gen(wp_EccGenCtx *ctx, OSSL_CALLBACK *cb, void *cbArg)
17681768
(void)cb;
17691769
(void)cbArg;
17701770

1771+
WP_CHECK_FIPS_ALGO_PTR(WP_CAST_ALGO_ECDSA);
1772+
WP_CHECK_FIPS_ALGO_PTR(WP_CAST_ALGO_ECDH);
1773+
17711774
if (ctx->curveName[0] != '\0') {
17721775
ecc = wp_ecc_new(ctx->provCtx);
17731776
}

src/wp_ecdh_exch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ static int wp_ecdh_init(wp_EcdhCtx* ctx, wp_Ecc* ecc, const OSSL_PARAM params[])
184184
if (!wolfssl_prov_is_running()) {
185185
ok = 0;
186186
}
187+
if (ok) {
188+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_ECDH);
189+
}
187190
if (ok && (ctx->key != ecc)) {
188191
/* Free old key and up reference new key. */
189192
wp_ecc_free(ctx->key);

src/wp_ecdsa_sig.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ static int wp_ecdsa_signverify_init(wp_EcdsaSigCtx *ctx, wp_Ecc* ecc,
190190
if (ctx == NULL || (ecc == NULL && ctx->ecc == NULL)) {
191191
ok = 0;
192192
}
193-
else if (ecc != NULL) {
193+
if (ok) {
194+
WP_CHECK_FIPS_ALGO(WP_CAST_ALGO_ECDSA);
195+
}
196+
if (ok && (ecc != NULL)) {
194197
if (!wp_ecc_up_ref(ecc)) {
195198
ok = 0;
196199
}

0 commit comments

Comments
 (0)