From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 08397A0547; Wed, 12 Oct 2022 08:13:05 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id A6EA242D80; Wed, 12 Oct 2022 08:12:55 +0200 (CEST) Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by mails.dpdk.org (Postfix) with ESMTP id EE2B742D7D for ; Wed, 12 Oct 2022 08:12:53 +0200 (CEST) Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29C2AWKY027131; Tue, 11 Oct 2022 23:12:53 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=BRoFnhwcFRAXKrDvZJBcL5N5dl8enj0tsaG1OeYPkGM=; b=acfDczmPFQDj+43t4p8Am0k3E0dyTJGrjd+FdUjjS0wUTFe5YTC9QgoZWqFpA1j46BTx 4k2Ah6ptUR6txCgyqOisgGOLs2badqwss6PvpQUsQ/z/ac8Z4mzIGPsJaL4CJ8Okm1/b UrmfnEhwZCG0h0w7VtNmSiVozLKdw3HFcLIpb3vGyDxUtn60VLyNowGUBbJokADunu87 h8BO0JVnr6fZ+ddqn6djQwzAPxVZdsfWFAy7bJNA4Ce6Tb0Lxd1ly8csgoekbmPIYRJP 4C2uU0oj+qxUkENt9rjmevkLAldJfViM5nX8rjTAed7e0gX+GNdQbfm6tfaa4qiaO27Y Cw== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3k5mjg0phh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Tue, 11 Oct 2022 23:12:52 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Tue, 11 Oct 2022 23:12:51 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.18 via Frontend Transport; Tue, 11 Oct 2022 23:12:51 -0700 Received: from localhost.localdomain (unknown [10.28.34.38]) by maili.marvell.com (Postfix) with ESMTP id 5E5BF5B692A; Tue, 11 Oct 2022 23:12:49 -0700 (PDT) From: Gowrishankar Muthukrishnan To: CC: Anoob Joseph , Brian Dooley , Akhil Goyal , , "Gowrishankar Muthukrishnan" Subject: [v7 3/3] examples/fips_validation: randomize message for conformance test Date: Wed, 12 Oct 2022 11:42:38 +0530 Message-ID: <20221012061238.222059-4-gmuthukrishn@marvell.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221012061238.222059-1-gmuthukrishn@marvell.com> References: <20221012040532.70628-1-gmuthukrishn@marvell.com> <20221012061238.222059-1-gmuthukrishn@marvell.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-ORIG-GUID: zj1aPtocOC5lLs3kd0EjnaSABgl9I8Ja X-Proofpoint-GUID: zj1aPtocOC5lLs3kd0EjnaSABgl9I8Ja X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-12_03,2022-10-11_02,2022-06-22_01 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org FIPS conformance tests require randomizing message based on SP 800-106. Signed-off-by: Gowrishankar Muthukrishnan --- examples/fips_validation/fips_validation.h | 4 + .../fips_validation/fips_validation_rsa.c | 112 +++++++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/examples/fips_validation/fips_validation.h b/examples/fips_validation/fips_validation.h index 7c275403c7..eec9616462 100644 --- a/examples/fips_validation/fips_validation.h +++ b/examples/fips_validation/fips_validation.h @@ -213,6 +213,7 @@ struct rsa_interim_data { uint16_t saltlen; enum rte_crypto_rsa_padding_type padding; enum rte_crypto_rsa_priv_key_type privkey; + uint8_t random_msg; }; #ifdef USE_JANSSON @@ -339,6 +340,9 @@ parse_test_tdes_json_init(void); int parse_test_rsa_json_init(void); + +int +fips_test_randomize_message(struct fips_val *msg, struct fips_val *rand); #endif /* USE_JANSSON */ int diff --git a/examples/fips_validation/fips_validation_rsa.c b/examples/fips_validation/fips_validation_rsa.c index d3699f54d0..22c0faf3cb 100644 --- a/examples/fips_validation/fips_validation_rsa.c +++ b/examples/fips_validation/fips_validation_rsa.c @@ -19,11 +19,13 @@ #include "fips_validation.h" +#define CONFORMANCE_JSON_STR "conformance" #define TESTTYPE_JSON_STR "testType" #define SIGTYPE_JSON_STR "sigType" #define MOD_JSON_STR "modulo" #define HASH_JSON_STR "hashAlg" #define SALT_JSON_STR "saltLen" +#define RV_JSON_STR "randomValue" #define E_JSON_STR "e" #define N_JSON_STR "n" @@ -31,6 +33,10 @@ #define MSG_JSON_STR "message" #define SIG_JSON_STR "signature" + +#define RV_BUF_LEN (1024/8) +#define RV_BIT_LEN (256) + #ifdef USE_JANSSON struct { uint8_t type; @@ -259,6 +265,13 @@ prepare_vec_rsa(void) if (!BN_mod_inverse(qinv, q, p, ctx)) goto err; + if (info.interim_info.rsa_data.random_msg) { + if (!BN_generate_prime_ex(r, RV_BIT_LEN, 0, NULL, NULL, NULL)) + goto err; + + parse_uint8_hex_str("", BN_bn2hex(r), &vec.rsa.seed); + } + parse_uint8_hex_str("", BN_bn2hex(e), &vec.rsa.e); parse_uint8_hex_str("", BN_bn2hex(p), &vec.rsa.p); parse_uint8_hex_str("", BN_bn2hex(q), &vec.rsa.q); @@ -297,6 +310,11 @@ parse_test_rsa_json_interim_writeback(struct fips_val *val) { RTE_SET_USED(val); + if (info.interim_info.rsa_data.random_msg) { + json_object_set_new(json_info.json_write_group, "conformance", + json_string("SP800-106")); + } + if (info.op == FIPS_TEST_ASYM_SIGGEN) { json_t *obj; @@ -367,6 +385,14 @@ parse_test_rsa_json_writeback(struct fips_val *val) writeback_hex_str("", info.one_line_text, &vec.rsa.signature); obj = json_string(info.one_line_text); json_object_set_new(json_info.json_write_case, "signature", obj); + + if (info.interim_info.rsa_data.random_msg) { + writeback_hex_str("", info.one_line_text, &vec.rsa.seed); + obj = json_string(info.one_line_text); + json_object_set_new(json_info.json_write_case, "randomValue", obj); + json_object_set_new(json_info.json_write_case, "randomValueLen", + json_integer(vec.rsa.seed.len * 8)); + } } else if (info.op == FIPS_TEST_ASYM_SIGVER) { if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) json_object_set_new(json_info.json_write_case, "testPassed", json_true()); @@ -406,6 +432,8 @@ parse_interim_str(const char *key, char *src, struct fips_val *val) if (i >= RTE_DIM(rsa_auth_algs)) return -EINVAL; + } else if (strcmp(key, CONFORMANCE_JSON_STR) == 0) { + info.interim_info.rsa_data.random_msg = 1; } else if (strcmp(key, SALT_JSON_STR) == 0) { info.interim_info.rsa_data.saltlen = atoi(src); } else if (strcmp(key, TESTTYPE_JSON_STR) == 0) { @@ -436,6 +464,83 @@ parse_keygen_e_str(const char *key, char *src, struct fips_val *val) return prepare_vec_rsa(); } +/* + * Message randomization function as per NIST SP 800-106. + */ +int +fips_test_randomize_message(struct fips_val *msg, struct fips_val *rand) +{ + uint8_t m[FIPS_TEST_JSON_BUF_LEN], rv[RV_BUF_LEN]; + uint32_t m_bitlen, rv_bitlen, count, remain, i, j; + uint16_t rv_len; + + if (!msg->val || !rand->val || rand->len > RV_BUF_LEN + || msg->len > FIPS_TEST_JSON_BUF_LEN) + return -EINVAL; + + memset(rv, 0, sizeof(rv)); + memcpy(rv, rand->val, rand->len); + rv_bitlen = rand->len * 8; + rv_len = rand->len; + + memset(m, 0, sizeof(m)); + memcpy(m, msg->val, msg->len); + m_bitlen = msg->len * 8; + + if (m_bitlen >= (rv_bitlen - 1)) { + m[msg->len] = 0x80; + m_bitlen += 8; + } else { + m[msg->len] = 0x80; + m_bitlen += (rv_bitlen - m_bitlen - 8); + } + + count = m_bitlen / rv_bitlen; + remain = m_bitlen % rv_bitlen; + for (i = 0; i < count * rv_len; i++) + m[i] ^= rv[i % rv_len]; + + for (j = 0; j < remain / 8; j++) + m[i + j] ^= rv[j]; + + m[i + j] = ((uint8_t *)&rv_bitlen)[0]; + m[i + j + 1] = (((uint8_t *)&rv_bitlen)[1] >> 8) & 0xFF; + + rte_free(msg->val); + msg->len = (rv_bitlen + m_bitlen + 16) / 8; + msg->val = rte_zmalloc(NULL, msg->len, 0); + if (!msg->val) + return -EPERM; + + memcpy(msg->val, rv, rv_len); + memcpy(&msg->val[rv_len], m, (m_bitlen + 16) / 8); + return 0; +} + +static int +parse_siggen_message_str(const char *key, char *src, struct fips_val *val) +{ + int ret = 0; + + parse_uint8_hex_str(key, src, val); + if (info.interim_info.rsa_data.random_msg) + ret = fips_test_randomize_message(val, &vec.rsa.seed); + + return ret; +} + +static int +parse_sigver_randomvalue_str(const char *key, char *src, struct fips_val *val) +{ + int ret = 0; + + parse_uint8_hex_str(key, src, val); + if (info.interim_info.rsa_data.random_msg) + ret = fips_test_randomize_message(&vec.pt, val); + + return ret; +} + struct fips_test_callback rsa_keygen_interim_json_vectors[] = { {MOD_JSON_STR, parse_interim_str, NULL}, {HASH_JSON_STR, parse_interim_str, NULL}, @@ -447,6 +552,7 @@ struct fips_test_callback rsa_siggen_interim_json_vectors[] = { {SIGTYPE_JSON_STR, parse_interim_str, NULL}, {MOD_JSON_STR, parse_interim_str, NULL}, {HASH_JSON_STR, parse_interim_str, NULL}, + {CONFORMANCE_JSON_STR, parse_interim_str, NULL}, {SALT_JSON_STR, parse_interim_str, NULL}, {TESTTYPE_JSON_STR, parse_interim_str, NULL}, {NULL, NULL, NULL} /**< end pointer */ @@ -456,6 +562,7 @@ struct fips_test_callback rsa_sigver_interim_json_vectors[] = { {SIGTYPE_JSON_STR, parse_interim_str, NULL}, {MOD_JSON_STR, parse_interim_str, NULL}, {HASH_JSON_STR, parse_interim_str, NULL}, + {CONFORMANCE_JSON_STR, parse_interim_str, NULL}, {SALT_JSON_STR, parse_interim_str, NULL}, {N_JSON_STR, parse_uint8_hex_str, &vec.rsa.n}, {E_JSON_STR, parse_uint8_hex_str, &vec.rsa.e}, @@ -470,13 +577,14 @@ struct fips_test_callback rsa_keygen_json_vectors[] = { }; struct fips_test_callback rsa_siggen_json_vectors[] = { - {MSG_JSON_STR, parse_uint8_hex_str, &vec.pt}, + {MSG_JSON_STR, parse_siggen_message_str, &vec.pt}, {NULL, NULL, NULL} /**< end pointer */ }; struct fips_test_callback rsa_sigver_json_vectors[] = { {MSG_JSON_STR, parse_uint8_hex_str, &vec.pt}, {SIG_JSON_STR, parse_uint8_hex_str, &vec.rsa.signature}, + {RV_JSON_STR, parse_sigver_randomvalue_str, &vec.rsa.seed}, {NULL, NULL, NULL} /**< end pointer */ }; @@ -492,6 +600,7 @@ parse_test_rsa_json_init(void) info.parse_writeback = NULL; info.interim_callbacks = NULL; info.parse_interim_writeback = NULL; + info.interim_info.rsa_data.random_msg = 0; if (strcmp(mode_str, "keyGen") == 0) { info.op = FIPS_TEST_ASYM_KEYGEN; @@ -506,6 +615,7 @@ parse_test_rsa_json_init(void) info.op = FIPS_TEST_ASYM_SIGVER; info.callbacks = rsa_sigver_json_vectors; info.interim_callbacks = rsa_sigver_interim_json_vectors; + info.parse_interim_writeback = parse_test_rsa_json_interim_writeback; } else { return -EINVAL; } -- 2.25.1