Skip to content

Commit 6499cab

Browse files
authored
Merge pull request #118 from haydenroche5/fips_ctrl_cmd
Add control command to enable/disable FIPS checks at runtime.
2 parents 424637f + 0b3f664 commit 6499cab

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

include/wolfengine/we_internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@
7878

7979
#include <wolfengine/we_logging.h>
8080

81+
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
82+
/*
83+
* Global FIPS checks flag.
84+
*/
85+
extern int fipsChecks;
86+
#endif /* HAVE_FIPS || HAVE_FIPS_VERSION */
87+
8188
/*
8289
* Global random
8390
*/

src/we_internal.c

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <wolfengine/we_wolfengine.h>
2323
#include <wolfengine/we_internal.h>
2424

25+
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
26+
int fipsChecks = 1;
27+
#endif /* HAVE_FIPS || HAVE_FIPS_VERSION */
28+
2529
/** Engine bound to. */
2630
static ENGINE *bound = NULL;
2731

@@ -1054,6 +1058,7 @@ static int wolfengine_destroy(ENGINE *e)
10541058
#define WOLFENGINE_CMD_SET_LOG_LEVEL (ENGINE_CMD_BASE + 1)
10551059
#define WOLFENGINE_CMD_SET_LOG_COMPONENTS (ENGINE_CMD_BASE + 2)
10561060
#define WOLFENGINE_CMD_SET_LOGGING_CB (ENGINE_CMD_BASE + 3)
1061+
#define WOLFENGINE_CMD_ENABLE_FIPS_CHECKS (ENGINE_CMD_BASE + 4)
10571062

10581063
/**
10591064
* wolfEngine control command list.
@@ -1105,6 +1110,10 @@ static ENGINE_CMD_DEFN wolfengine_cmd_defns[] = {
11051110
"set_logging_cb",
11061111
"Set wolfEngine logging callback",
11071112
ENGINE_CMD_FLAG_INTERNAL },
1113+
{ WOLFENGINE_CMD_ENABLE_FIPS_CHECKS,
1114+
"enable_fips_checks",
1115+
"Enable wolfEngine FIPS checks (1=enable, 0=disable)",
1116+
ENGINE_CMD_FLAG_NUMERIC },
11081117

11091118
/* last element MUST be NULL/0 entry, do not remove */
11101119
{0, NULL, NULL, 0}
@@ -1147,7 +1156,8 @@ static int wolfengine_ctrl(ENGINE* e, int cmd, long i, void* p,
11471156
if (wolfEngine_Debugging_ON() < 0) {
11481157
ret = 0;
11491158
}
1150-
} else {
1159+
}
1160+
else {
11511161
wolfEngine_Debugging_OFF();
11521162
}
11531163
break;
@@ -1171,11 +1181,26 @@ static int wolfengine_ctrl(ENGINE* e, int cmd, long i, void* p,
11711181
WOLFENGINE_ERROR_MSG(WE_LOG_ENGINE,
11721182
"Error registering wolfEngine logging callback");
11731183
ret = 0;
1174-
} else {
1184+
}
1185+
else {
11751186
WOLFENGINE_MSG(WE_LOG_ENGINE,
11761187
"wolfEngine user logging callback registered");
11771188
}
11781189
break;
1190+
case WOLFENGINE_CMD_ENABLE_FIPS_CHECKS:
1191+
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
1192+
if (i > 0) {
1193+
fipsChecks = 1;
1194+
}
1195+
else {
1196+
fipsChecks = 0;
1197+
}
1198+
#else
1199+
WOLFENGINE_MSG(WE_LOG_ENGINE, "Control command "
1200+
"WOLFENGINE_CMD_ENABLE_FIPS_CHECKS has no effect when "
1201+
"wolfCrypt isn't FIPS.");
1202+
#endif /* HAVE_FIPS || HAVE_FIPS_VERSION */
1203+
break;
11791204
default:
11801205
XSNPRINTF(errBuff, sizeof(errBuff), "Unsupported ctrl type %d",
11811206
cmd);

src/we_rsa.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,23 @@ static int we_check_rsa_key_size(int size, int allow1024) {
8585
char errBuff[WOLFENGINE_MAX_LOG_WIDTH];
8686

8787
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
88-
ret = size == 2048 || size == 3072 || size == 4096;
89-
if (allow1024 == 1) {
90-
ret |= size == 1024;
88+
if (fipsChecks == 1) {
89+
ret = size == 2048 || size == 3072 || size == 4096;
90+
if (allow1024 == 1) {
91+
ret |= size == 1024;
92+
}
9193
}
92-
#else
93-
(void)allow1024;
94-
ret = size >= RSA_MIN_SIZE && size <= RSA_MAX_SIZE;
94+
else
9595
#endif /* HAVE_FIPS || HAVE_FIPS_VERSION */
96+
{
97+
(void)allow1024;
98+
ret = size >= RSA_MIN_SIZE && size <= RSA_MAX_SIZE;
9699

97-
if (ret == 0) {
98-
XSNPRINTF(errBuff, sizeof(errBuff), "RSA key size %d not allowed.",
99-
size);
100-
WOLFENGINE_ERROR_MSG(WE_LOG_PK, errBuff);
100+
if (ret == 0) {
101+
XSNPRINTF(errBuff, sizeof(errBuff), "RSA key size %d not allowed.",
102+
size);
103+
WOLFENGINE_ERROR_MSG(WE_LOG_PK, errBuff);
104+
}
101105
}
102106

103107
return ret;

test/test_rsa.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,23 @@ int test_rsa_direct_key_gen(ENGINE *e, void *data)
597597
NULL) != 0;
598598
}
599599
}
600+
#if defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION)
601+
if (err == 0) {
602+
PRINT_MSG("Check that disabling FIPS checks allows 1024-bit key gen.");
603+
err = ENGINE_ctrl_cmd(e, "enable_fips_checks", 0, NULL, NULL, 0) == 0;
604+
}
605+
if (err == 0) {
606+
err = RSA_generate_key_ex(rsaKey, 1024, pubExp, NULL) == 0;
607+
}
608+
if (err == 0) {
609+
PRINT_MSG("Check that re-enabling FIPS checks disallows 1024-bit key "
610+
"gen.");
611+
err = ENGINE_ctrl_cmd(e, "enable_fips_checks", 1, NULL, NULL, 0) == 0;
612+
}
613+
if (err == 0) {
614+
err = RSA_generate_key_ex(rsaKey, 1024, pubExp, NULL) != 0;
615+
}
616+
#endif /* HAVE_FIPS || HAVE_FIPS_VERSION */
600617

601618
if (pubExp != NULL) {
602619
BN_free(pubExp);

0 commit comments

Comments
 (0)