From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 0C4F6A00C4;
	Sat, 29 Jan 2022 18:55:44 +0100 (CET)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id C522041157;
	Sat, 29 Jan 2022 18:55:25 +0100 (CET)
Received: from mail-qv1-f68.google.com (mail-qv1-f68.google.com
 [209.85.219.68]) by mails.dpdk.org (Postfix) with ESMTP id 92DB240041
 for <dev@dpdk.org>; Sat, 29 Jan 2022 18:55:22 +0100 (CET)
Received: by mail-qv1-f68.google.com with SMTP id d8so8864033qvv.2
 for <dev@dpdk.org>; Sat, 29 Jan 2022 09:55:22 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iol.unh.edu; s=unh-iol;
 h=from:to:cc:subject:date:message-id:in-reply-to:references
 :mime-version:content-transfer-encoding;
 bh=HhV+nhzoXkgvP3wYCuIKQ9G29915TRTYDyslJAMrpBY=;
 b=KfMjLWzL5dAzk+W087Gsr3QeAsW5mpVBHEgqPj/k6zIkTtPq255q+H9sGARAOuZaMP
 T5louN+H0Kn0u3RKVzKcdl9JivFFeYxzv0GXGuice+1JpmC9hO0gmB+SPZmyWsWvP4jA
 KxZQI4hN3L4aIykuNPOwZaQdQMHZz8190U84Y=
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
 :references:mime-version:content-transfer-encoding;
 bh=HhV+nhzoXkgvP3wYCuIKQ9G29915TRTYDyslJAMrpBY=;
 b=z9Egjeq27DrYCfrzaa5i8AFJO1wz/ijpoGFNCrCHNsA++yRYJfdwEXV42x003Yt51L
 6pAuDGlpywW7Hy2to2kza57hWmTu7VQBie/GzECcU9PtVGb9/3nvbc557xtvxLSUS6r2
 pxXQ72KxE74CC1fQFeP1jcC2tFno3xdeURkxYsToUE4PWNX12Ij3EgybTm03y3Imnvq9
 lLngf9DtcF2RuQpDgXnrHbGbgY80liCC1fxagvn13v/mjr3NsVWltroC7AtsMejLXH92
 HB0N2g4xZKCbn1d8ih0jtTVINu5AXW+wfKNo9y2DuBOQkwEoesBmHmdso4rzqUk6Eu5Q
 T41w==
X-Gm-Message-State: AOAM531Qr2r53BDq71Dl5IgeUwcvzVdbxA1I9vihR4/ASG38DKMtWSBI
 MuLn5sB33ezek2VgQeP6v30aoA==
X-Google-Smtp-Source: ABdhPJytrcWnGpkVxwqRakuXF5EMIMwQbZchjL51PgFHfJB850r3NOGxb/P+ngw7iYW7hR4cOMMyXQ==
X-Received: by 2002:ad4:5be9:: with SMTP id k9mr11477874qvc.45.1643478922068; 
 Sat, 29 Jan 2022 09:55:22 -0800 (PST)
Received: from blo.hsd1.nh.comcast.net
 ([2601:187:807e:37c0:8f3:b5b9:6ccc:8a5c])
 by smtp.gmail.com with ESMTPSA id 195sm5423654qkf.30.2022.01.29.09.55.21
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sat, 29 Jan 2022 09:55:21 -0800 (PST)
From: Brandon Lo <blo@iol.unh.edu>
To: roy.fan.zhang@intel.com,
	ciara.power@intel.com
Cc: dev@dpdk.org,
	Brandon Lo <blo@iol.unh.edu>
Subject: [PATCH v3 5/5] examples/fips_validation: add json to gcm test
Date: Sat, 29 Jan 2022 12:55:14 -0500
Message-Id: <20220129175514.746925-6-blo@iol.unh.edu>
X-Mailer: git-send-email 2.25.1
In-Reply-To: <20220129175514.746925-1-blo@iol.unh.edu>
References: <20220129170313.489986-1-blo@iol.unh.edu>
 <20220129175514.746925-1-blo@iol.unh.edu>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

Adds json-specific testing and writeback function. Allows
the user to test AES-GCM vector sets.

Signed-off-by: Brandon Lo <blo@iol.unh.edu>
---
v3:
* fix checkpatch warnings

 .../fips_validation/fips_validation_gcm.c     | 150 ++++++++++++++++++
 1 file changed, 150 insertions(+)

diff --git a/examples/fips_validation/fips_validation_gcm.c b/examples/fips_validation/fips_validation_gcm.c
index 250d09bf90..5c72dbf790 100644
--- a/examples/fips_validation/fips_validation_gcm.c
+++ b/examples/fips_validation/fips_validation_gcm.c
@@ -6,6 +6,10 @@
 #include <time.h>
 #include <stdio.h>
 
+#ifdef RTE_HAS_JANSSON
+#include <jansson.h>
+#endif /* RTE_HAS_JANSSON */
+
 #include <rte_cryptodev.h>
 #include <rte_malloc.h>
 
@@ -37,6 +41,27 @@
 #define OP_ENC_EXT_STR	"ExtIV"
 #define OP_ENC_INT_STR	"IntIV"
 
+#define KEYLEN_JSON_STR		"keyLen"
+#define IVLEN_JSON_STR		"ivLen"
+#define PAYLOADLEN_JSON_STR	"payloadLen"
+#define AADLEN_JSON_STR		"aadLen"
+#define TAGLEN_JSON_STR		"tagLen"
+
+#define KEY_JSON_STR	"key"
+#define IV_JSON_STR		"iv"
+#define PT_JSON_STR		"pt"
+#define CT_JSON_STR		"ct"
+#define AAD_JSON_STR	"aad"
+#define TAG_JSON_STR	"tag"
+#define DIR_JSON_STR	"direction"
+
+#define OP_ENC_JSON_STR	"encrypt"
+#define OP_DEC_JSON_STR	"decrypt"
+
+#define IVGEN_JSON_STR	"ivGen"
+#define OP_ENC_EXT_JSON_STR	"external"
+#define OP_ENC_INT_JSON_STR	"internal"
+
 #define NEG_TEST_STR	"FAIL"
 
 /**
@@ -136,6 +161,40 @@ struct fips_test_callback gcm_enc_vectors[] = {
 		{NULL, NULL, NULL} /**< end pointer */
 };
 
+#ifdef RTE_HAS_JANSSON
+struct fips_test_callback gcm_dec_json_vectors[] = {
+		{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
+		{IV_JSON_STR, parse_uint8_known_len_hex_str, &vec.iv},
+		{CT_JSON_STR, parse_gcm_pt_ct_str, &vec.ct},
+		{AAD_JSON_STR, parse_gcm_aad_str, &vec.aead.aad},
+		{TAG_JSON_STR, parse_uint8_known_len_hex_str,
+				&vec.aead.digest},
+		{NULL, NULL, NULL} /**< end pointer */
+};
+
+struct fips_test_callback gcm_interim_json_vectors[] = {
+		{KEYLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.key},
+		{IVLEN_JSON_STR, parser_read_uint32_bit_val, &vec.iv},
+		{PAYLOADLEN_JSON_STR, parser_read_gcm_pt_len, &vec.pt},
+		{PAYLOADLEN_JSON_STR, parser_read_uint32_bit_val, &vec.ct},
+		/**< The NIST json test vectors use 'payloadLen' to denote input text
+		 *  length in case of decrypt & encrypt operations.
+		 */
+		{AADLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.aad},
+		{TAGLEN_JSON_STR, parser_read_uint32_bit_val,
+				&vec.aead.digest},
+		{NULL, NULL, NULL} /**< end pointer */
+};
+
+struct fips_test_callback gcm_enc_json_vectors[] = {
+		{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
+		{IV_JSON_STR, parse_uint8_known_len_hex_str, &vec.iv},
+		{PT_JSON_STR, parse_gcm_pt_ct_str, &vec.pt},
+		{AAD_JSON_STR, parse_gcm_aad_str, &vec.aead.aad},
+		{NULL, NULL, NULL} /**< end pointer */
+};
+#endif /* RTE_HAS_JANSSON */
+
 static int
 parse_test_gcm_writeback(struct fips_val *val)
 {
@@ -188,12 +247,103 @@ parse_test_gcm_writeback(struct fips_val *val)
 	return 0;
 }
 
+#ifdef RTE_HAS_JANSSON
+static int
+parse_test_gcm_json_writeback(struct fips_val *val)
+{
+	struct fips_val tmp_val;
+	json_t *tcId, *tag;
+
+	tcId = json_object_get(json_info.json_test_case, "tcId");
+
+	json_info.json_write_case = json_object();
+	json_object_set(json_info.json_write_case, "tcId", tcId);
+
+	if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
+		json_t *ct;
+
+		tmp_val.val = val->val;
+		tmp_val.len = vec.pt.len;
+
+		writeback_hex_str("", info.one_line_text, &tmp_val);
+		ct = json_string(info.one_line_text);
+		json_object_set_new(json_info.json_write_case, CT_JSON_STR, ct);
+
+		if (info.interim_info.gcm_data.gen_iv) {
+			json_t *iv;
+			tmp_val.val = vec.iv.val;
+			tmp_val.len = vec.iv.len;
+
+			writeback_hex_str("", info.one_line_text, &tmp_val);
+			iv = json_string(info.one_line_text);
+			json_object_set_new(json_info.json_write_case, IV_JSON_STR, iv);
+
+			rte_free(vec.iv.val);
+			vec.iv.val = NULL;
+		}
+
+		tmp_val.val = val->val + vec.pt.len;
+		tmp_val.len = val->len - vec.pt.len;
+
+		writeback_hex_str("", info.one_line_text, &tmp_val);
+		tag = json_string(info.one_line_text);
+		json_object_set_new(json_info.json_write_case, TAG_JSON_STR, tag);
+	} else {
+		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
+			if (!info.interim_info.gcm_data.is_gmac) {
+				tmp_val.val = val->val;
+				tmp_val.len = vec.pt.len;
+
+				writeback_hex_str("", info.one_line_text, &tmp_val);
+				json_object_set_new(json_info.json_write_case, PT_JSON_STR,
+					json_string(info.one_line_text));
+			}
+		} else {
+			json_object_set_new(json_info.json_write_case, "testPassed", json_false());
+		}
+	}
+
+	return 0;
+}
+#endif /* RTE_HAS_JANSSON */
+
 int
 parse_test_gcm_init(void)
 {
 	char *tmp;
 	uint32_t i;
 
+#ifdef RTE_HAS_JANSSON
+	if (json_info.json_test_group) {
+		json_t *direction_obj;
+		const char *direction_str;
+
+		direction_obj = json_object_get(json_info.json_test_group, DIR_JSON_STR);
+		direction_str = json_string_value(direction_obj);
+
+		if (strcmp(direction_str, OP_ENC_JSON_STR) == 0) {
+			json_t *ivGen_obj;
+			const char *ivGen_str;
+
+			ivGen_obj = json_object_get(json_info.json_test_group, IVGEN_JSON_STR);
+			ivGen_str = json_string_value(ivGen_obj);
+			info.op = FIPS_TEST_ENC_AUTH_GEN;
+			info.callbacks = gcm_enc_json_vectors;
+
+			if (strcmp(ivGen_str, OP_ENC_INT_JSON_STR) == 0)
+				info.interim_info.gcm_data.gen_iv = 1;
+		} else if (strcmp(direction_str, OP_DEC_JSON_STR) == 0) {
+			info.op = FIPS_TEST_DEC_AUTH_VERIF;
+			info.callbacks = gcm_dec_json_vectors;
+		} else {
+			return -EINVAL;
+		}
+		info.interim_callbacks = gcm_interim_json_vectors;
+		info.parse_writeback = parse_test_gcm_json_writeback;
+
+		return 0;
+	}
+#endif /* RTE_HAS_JANSSON */
 
 	for (i = 0; i < info.nb_vec_lines; i++) {
 		char *line = info.vec[i];
-- 
2.25.1