Skip to content

Commit

Permalink
Merge pull request #560 from cisco/feature/56cUts
Browse files Browse the repository at this point in the history
Fixes to safeC stub, updating all changes from internal
  • Loading branch information
bfussell authored Jan 23, 2021
2 parents 2584d9e + 065714d commit e90b70e
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 61 deletions.
1 change: 1 addition & 0 deletions app/app_kas.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ int app_kas_ifc_handler(ACVP_TEST_CASE *test_case) {
return rv;
}


int app_kts_ifc_handler(ACVP_TEST_CASE *test_case) {
if (!test_case) {
return -1;
Expand Down
4 changes: 3 additions & 1 deletion app/app_kas_kdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ int app_kas_kdf_onestep_handler(ACVP_TEST_CASE *test_case) {
unsigned char *fixedInfo = NULL;
unsigned char *h_output = NULL;
unsigned char *result = NULL;
#if OPENSSL_VERSION_NUMBER <= 0x10100000L
HMAC_CTX static_ctx;
#endif
HMAC_CTX *hmac_ctx = NULL;
EVP_MD_CTX *sha_ctx = NULL;

Expand All @@ -316,7 +319,6 @@ int app_kas_kdf_onestep_handler(ACVP_TEST_CASE *test_case) {
//if the test case has a salt, we are using HMAC, otherwise, SHA
if (stc->salt) {
#if OPENSSL_VERSION_NUMBER <= 0x10100000L
HMAC_CTX static_ctx;
hmac_ctx = &static_ctx;
HMAC_CTX_init(hmac_ctx);
#else
Expand Down
2 changes: 1 addition & 1 deletion app/ketopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define ko_required_argument 1
#define ko_optional_argument 2

#define OPTION_NAME_MAX 16
#define OPTION_NAME_MAX 128
#define OSTR_MAX 2 /* Change according to the ostr parameter in app_cli.c */

typedef struct {
Expand Down
6 changes: 3 additions & 3 deletions ms/resources/Source.def
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ EXPORTS
acvp_cleanup
acvp_version
acvp_protocol_version
acvp_kas_kdf_enable
acvp_kas_kdf_set_parm
acvp_kas_kdf_set_domain
acvp_cap_kas_kdf_enable
acvp_cap_kas_kdf_set_parm
acvp_cap_kas_kdf_set_domain
acvp_cap_kas_ifc_enable
acvp_cap_kas_ifc_set_parm
acvp_cap_kas_ifc_set_exponent
Expand Down
1 change: 1 addition & 0 deletions ms/resources/acvp_app.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@
<ClCompile Include="..\..\app\app_hmac.c" />
<ClCompile Include="..\..\app\app_kas.c" />
<ClCompile Include="..\..\app\app_kdf.c" />
<ClCompile Include="..\..\app\app_kas_kdf.c" />
<ClCompile Include="..\..\app\app_main.c" />
<ClCompile Include="..\..\app\app_rsa.c" />
<ClCompile Include="..\..\app\app_sha.c" />
Expand Down
3 changes: 3 additions & 0 deletions ms/resources/acvp_app.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClCompile Include="..\..\app\app_kdf.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\app\app_kas_kdf.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\app\app_main.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
5 changes: 5 additions & 0 deletions ms/resources/libacvp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@
<ClCompile Include="..\..\src\acvp_hmac.c" />
<ClCompile Include="..\..\src\acvp_kas_ecc.c" />
<ClCompile Include="..\..\src\acvp_kas_ffc.c" />
<ClCompile Include="..\..\src\acvp_kas_ifc.c" />
<ClCompile Include="..\..\src\acvp_kts_ifc.c" />
<ClCompile Include="..\..\src\acvp_pbkdf.c" />
<ClCompile Include="..\..\src\acvp_kas_kdf.c" />
<ClCompile Include="..\..\src\acvp_rsa_prim.c" />
<ClCompile Include="..\..\src\acvp_kdf108.c" />
<ClCompile Include="..\..\src\acvp_kdf135_ikev1.c" />
<ClCompile Include="..\..\src\acvp_kdf135_ikev2.c" />
Expand Down
15 changes: 15 additions & 0 deletions ms/resources/libacvp.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@
<ClCompile Include="..\..\src\acvp_kas_ffc.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\acvp_kas_ifc.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\acvp_kts_ifc.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\acvp_pbkdf.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\acvp_kas_kdf.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\acvp_rsa_prim.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\acvp_kdf108.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
10 changes: 5 additions & 5 deletions safe_c_stub/src/safe_str_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <stdint.h>
#include <string.h>
#include <ctype.h>

#include "safe_lib.h"

#define SAFEC_STUB_UNUSED(x) (void)(x)
Expand All @@ -49,8 +48,8 @@
*/
errno_t strcmp_s (const char *dest, rsize_t dmax, const char *src, int *indicator) {
if (!src || !dest) return (ESNULLP);
if (dmax == 0) return (ESZEROL);
*indicator = strncmp(dest, src, dmax);
if (dmax == 0 || dmax > RSIZE_MAX_STR) return (ESZEROL);
*indicator = strcmp(dest, src);
return (EOK);
}

Expand All @@ -62,8 +61,9 @@ errno_t strcmp_s (const char *dest, rsize_t dmax, const char *src, int *indicato
errno_t strncmp_s (const char *dest, rsize_t dmax, const char *src, rsize_t smax, int *indicator) {
if (!src || !dest) return (ESNULLP);
if (dmax == 0) return (ESZEROL);
if (smax > RSIZE_MAX_STR) return (EINVAL);
*indicator = strncmp(dest, src, dmax);
size_t dlen = strnlen(dest, dmax);
if (smax > RSIZE_MAX_STR || smax > dlen) return (EINVAL);
*indicator = strncmp(dest, src, smax);
return (EOK);
}

Expand Down
96 changes: 49 additions & 47 deletions src/acvp_kas_kdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,77 +436,79 @@ ACVP_KAS_KDF_PATTERN_CANDIDATE cmp_pattern_str(ACVP_CTX *ctx, ACVP_CIPHER cipher
ACVP_LOG_ERR("pattern candidate too long");
return 0;
}
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_UPARTYINFO_STR, sizeof(ACVP_KAS_KDF_PATTERN_UPARTYINFO_STR) - 1, &diff);
strcmp_s(str, len, ACVP_KAS_KDF_PATTERN_UPARTYINFO_STR, &diff);
if (!diff && len == sizeof(ACVP_KAS_KDF_PATTERN_UPARTYINFO_STR) - 1) {
return ACVP_KAS_KDF_PATTERN_UPARTYINFO;
}
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_VPARTYINFO_STR, sizeof(ACVP_KAS_KDF_PATTERN_VPARTYINFO_STR) - 1, &diff);
strcmp_s(str, len, ACVP_KAS_KDF_PATTERN_VPARTYINFO_STR, &diff);
if (!diff && len == sizeof(ACVP_KAS_KDF_PATTERN_VPARTYINFO_STR) - 1) {
return ACVP_KAS_KDF_PATTERN_VPARTYINFO;
}
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_CONTEXT_STR, sizeof(ACVP_KAS_KDF_PATTERN_CONTEXT_STR) - 1, &diff);
strcmp_s(str, len, ACVP_KAS_KDF_PATTERN_CONTEXT_STR, &diff);
if (!diff && len == sizeof(ACVP_KAS_KDF_PATTERN_CONTEXT_STR) - 1) {
return ACVP_KAS_KDF_PATTERN_CONTEXT;
}
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_ALGID_STR, sizeof(ACVP_KAS_KDF_PATTERN_ALGID_STR) - 1, &diff);
strcmp_s(str, len, ACVP_KAS_KDF_PATTERN_ALGID_STR, &diff);
if (!diff && len == sizeof(ACVP_KAS_KDF_PATTERN_ALGID_STR) - 1) {
return ACVP_KAS_KDF_PATTERN_ALGID;
}
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_LABEL_STR, sizeof(ACVP_KAS_KDF_PATTERN_LABEL_STR) - 1, &diff);
strcmp_s(str, len, ACVP_KAS_KDF_PATTERN_LABEL_STR, &diff);
if (!diff && len == sizeof(ACVP_KAS_KDF_PATTERN_LABEL_STR) - 1) {
return ACVP_KAS_KDF_PATTERN_LABEL;
}
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_LENGTH_STR, sizeof(ACVP_KAS_KDF_PATTERN_LENGTH_STR) - 1, &diff);
strcmp_s(str, len, ACVP_KAS_KDF_PATTERN_LENGTH_STR, &diff);
if (!diff && len == sizeof(ACVP_KAS_KDF_PATTERN_LENGTH_STR) - 1) {
return ACVP_KAS_KDF_PATTERN_L;
}
//only compares first X number of characters, so should match, even though string is literal[0000000]
strncmp_s(ACVP_KAS_KDF_PATTERN_LITERAL_STR, sizeof(ACVP_KAS_KDF_PATTERN_LITERAL_STR) - 1, str, len, &diff);
if (!diff) {
//copy string so it can be tokenized
tmp = calloc(len + 1, sizeof(char));
if (!tmp) {
ACVP_LOG_ERR("Failed to allocate memory when checking literal pattern");
goto err;
}
strncpy_s(tmp, len + 1, str, len);
if (sizeof(ACVP_KAS_KDF_PATTERN_LITERAL_STR) - 1 < len) {
strncmp_s(str, len, ACVP_KAS_KDF_PATTERN_LITERAL_STR, sizeof(ACVP_KAS_KDF_PATTERN_LITERAL_STR) - 1, &diff);
if (!diff) {
//copy string so it can be tokenized
tmp = calloc(len + 1, sizeof(char));
if (!tmp) {
ACVP_LOG_ERR("Failed to allocate memory when checking literal pattern");
goto err;
}
strncpy_s(tmp, len + 1, str, len);

//tokenize around the [] characters
token = strtok_s(tmp, &len, "[", &lit);
if (!token) {
ACVP_LOG_ERR("Invalid literal pattern candidate");
goto err;
}
token = strtok_s(NULL, &len, "]", &lit); //the actual hex string
if (!token) {
ACVP_LOG_ERR("Invalid literal pattern candidate");
goto err;
}
if (strnlen_s(token, ACVP_KAS_KDF_PATTERN_LITERAL_STR_LEN_MAX + 1) > ACVP_KAS_KDF_PATTERN_LITERAL_STR_LEN_MAX) {
ACVP_LOG_ERR("Patttern literal too long");
goto err;
}
if (cipher == ACVP_KAS_HKDF) {
tc->tc.kas_hkdf->literalCandidate = calloc(ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, 1);
if (!tc->tc.kas_hkdf->literalCandidate) {
ACVP_LOG_ERR("Failed to allocate memory when setting literal pattern");
//tokenize around the [] characters
token = strtok_s(tmp, &len, "[", &lit);
if (!token) {
ACVP_LOG_ERR("Invalid literal pattern candidate");
goto err;
}
rv = acvp_hexstr_to_bin(token, tc->tc.kas_hkdf->literalCandidate, ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, &(tc->tc.kas_hkdf->literalLen));
} else {
tc->tc.kas_kdf_onestep->literalCandidate = calloc(ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, 1);
if (!tc->tc.kas_kdf_onestep->literalCandidate) {
ACVP_LOG_ERR("Failed to allocate memory when setting literal pattern");
token = strtok_s(NULL, &len, "]", &lit); //the actual hex string
if (!token) {
ACVP_LOG_ERR("Invalid literal pattern candidate");
goto err;
}
rv = acvp_hexstr_to_bin(token, tc->tc.kas_kdf_onestep->literalCandidate, ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, &(tc->tc.kas_kdf_onestep->literalLen));
}
if (rv != ACVP_SUCCESS) {
ACVP_LOG_ERR("Hex conversion failure (literal candidate)");
goto err;
if (strnlen_s(token, ACVP_KAS_KDF_PATTERN_LITERAL_STR_LEN_MAX + 1) > ACVP_KAS_KDF_PATTERN_LITERAL_STR_LEN_MAX) {
ACVP_LOG_ERR("Patttern literal too long");
goto err;
}
if (cipher == ACVP_KAS_HKDF) {
tc->tc.kas_hkdf->literalCandidate = calloc(ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, 1);
if (!tc->tc.kas_hkdf->literalCandidate) {
ACVP_LOG_ERR("Failed to allocate memory when setting literal pattern");
goto err;
}
rv = acvp_hexstr_to_bin(token, tc->tc.kas_hkdf->literalCandidate, ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, &(tc->tc.kas_hkdf->literalLen));
} else {
tc->tc.kas_kdf_onestep->literalCandidate = calloc(ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, 1);
if (!tc->tc.kas_kdf_onestep->literalCandidate) {
ACVP_LOG_ERR("Failed to allocate memory when setting literal pattern");
goto err;
}
rv = acvp_hexstr_to_bin(token, tc->tc.kas_kdf_onestep->literalCandidate, ACVP_KAS_KDF_PATTERN_LITERAL_BYTE_MAX, &(tc->tc.kas_kdf_onestep->literalLen));
}
if (rv != ACVP_SUCCESS) {
ACVP_LOG_ERR("Hex conversion failure (literal candidate)");
goto err;
}
if (tmp) free(tmp);
return ACVP_KAS_KDF_PATTERN_LITERAL;
}
if (tmp) free(tmp);
return ACVP_KAS_KDF_PATTERN_LITERAL;
}

ACVP_LOG_ERR("Candidate string provided does not match any valid candidate");
Expand All @@ -516,7 +518,7 @@ ACVP_KAS_KDF_PATTERN_CANDIDATE cmp_pattern_str(ACVP_CTX *ctx, ACVP_CIPHER cipher
}

static ACVP_KAS_KDF_PATTERN_CANDIDATE* read_info_pattern(ACVP_CTX *ctx, ACVP_CIPHER cipher, const char *str, ACVP_TEST_CASE *tc) {
ACVP_KAS_KDF_PATTERN_CANDIDATE currentCand = -1;
ACVP_KAS_KDF_PATTERN_CANDIDATE currentCand;
char *cpy = NULL;
ACVP_KAS_KDF_PATTERN_CANDIDATE *rv = NULL;
int hasUParty = 0, hasVParty = 0; //Currently, these are required
Expand Down
6 changes: 6 additions & 0 deletions src/acvp_rsa_prim.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ ACVP_RESULT acvp_rsa_decprim_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) {
ACVP_LOG_ERR("Server JSON missing 'cipher'");
rv = ACVP_MISSING_ARG;
json_value_free(r_tval);
json_value_free(r_cval);
goto err;
}
cipher_len = strnlen_s(cipher, ACVP_RSA_EXP_BYTE_MAX + 1);
Expand All @@ -398,6 +399,7 @@ ACVP_RESULT acvp_rsa_decprim_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) {
ACVP_RSA_SEEDLEN_MAX);
rv = ACVP_INVALID_ARG;
json_value_free(r_tval);
json_value_free(r_cval);
goto err;
}

Expand All @@ -413,6 +415,7 @@ ACVP_RESULT acvp_rsa_decprim_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) {
ACVP_LOG_ERR("ERROR: crypto module failed the operation");
rv = ACVP_CRYPTO_MODULE_FAIL;
json_value_free(r_tval);
json_value_free(r_cval);
goto err;
}
ACVP_LOG_INFO("Looping on fail/pass %d/%d %d/%d", fail, stc.fail, pass, stc.pass);
Expand All @@ -428,6 +431,7 @@ ACVP_RESULT acvp_rsa_decprim_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) {
if (rv != ACVP_SUCCESS) {
ACVP_LOG_ERR("ERROR: JSON output failure in primitive module");
json_value_free(r_tval);
json_value_free(r_cval);
goto err;
}
/*
Expand Down Expand Up @@ -632,13 +636,15 @@ ACVP_RESULT acvp_rsa_sigprim_kat_handler(ACVP_CTX *ctx, JSON_Object *obj) {
if (!e_str || !n_str || !d_str) {
ACVP_LOG_ERR("Missing e|n|d from server json");
rv = ACVP_MISSING_ARG;
json_value_free(r_tval);
goto err;
}
if ((strnlen_s(e_str, ACVP_RSA_EXP_LEN_MAX + 1) > ACVP_RSA_EXP_LEN_MAX) ||
(strnlen_s(n_str, ACVP_RSA_EXP_LEN_MAX + 1) > ACVP_RSA_EXP_LEN_MAX) ||
(strnlen_s(d_str, ACVP_RSA_EXP_LEN_MAX + 1) > ACVP_RSA_EXP_LEN_MAX)) {
ACVP_LOG_ERR("server provided d or e or n of invalid length");
rv = ACVP_INVALID_ARG;
json_value_free(r_tval);
goto err;
}

Expand Down
2 changes: 1 addition & 1 deletion src/acvp_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ const char* acvp_lookup_aux_function_alg_str(ACVP_CIPHER alg) {
ACVP_CIPHER acvp_lookup_aux_function_alg_tbl(const char *str) {
int diff = 1, i = 0;
for (i = 0; i < acvp_aux_function_tbl_len; i++) {
strncmp_s(acvp_aux_function_tbl[i].name, ACVP_ALG_NAME_MAX, str, ACVP_ALG_NAME_MAX, &diff);
strcmp_s(acvp_aux_function_tbl[i].name, strnlen_s(acvp_aux_function_tbl[i].name, ACVP_ALG_NAME_MAX), str, &diff);
if (!diff) {
return acvp_aux_function_tbl[i].cipher;
}
Expand Down
2 changes: 2 additions & 0 deletions test/test_acvp_kas_ifc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static void setup(void) {
rv = acvp_cap_kas_ifc_set_exponent(ctx, ACVP_KAS_IFC_SSC, ACVP_KAS_IFC_FIXEDPUBEXP, expo_str);
cr_assert(rv == ACVP_SUCCESS);

free(expo_str);
}

static void teardown(void) {
Expand Down Expand Up @@ -94,6 +95,7 @@ Test(KAS_IFC_CAPABILITY, good) {
cr_assert(rv == ACVP_SUCCESS);

teardown_ctx(&ctx);
free(expo_str);
}

/*
Expand Down
Loading

0 comments on commit e90b70e

Please sign in to comment.