Blame SOURCES/gnutls-3.7.6-fips-service-indicator-test-functions.patch

7e1ffc
From e8b35d31817d207d8b4b87ccf104d3b93aa446e0 Mon Sep 17 00:00:00 2001
7e1ffc
From: Daiki Ueno <ueno@gnu.org>
7e1ffc
Date: Mon, 17 Oct 2022 11:11:43 +0900
7e1ffc
Subject: [PATCH] tests: move FIPS service indicator functions to common file
7e1ffc
7e1ffc
Signed-off-by: Daiki Ueno <ueno@gnu.org>
7e1ffc
---
7e1ffc
 tests/dh-compute.c     | 36 +++++---------------------
7e1ffc
 tests/fips-rsa-sizes.c | 24 -----------------
7e1ffc
 tests/fips-test.c      | 27 +++-----------------
7e1ffc
 tests/kdf-api.c        | 27 +-------------------
7e1ffc
 tests/pkcs12_encode.c  | 24 -----------------
7e1ffc
 tests/privkey-keygen.c | 24 -----------------
7e1ffc
 tests/utils.h          | 58 ++++++++++++++++++++++++++++++++++++++++++
7e1ffc
 7 files changed, 69 insertions(+), 151 deletions(-)
7e1ffc
7e1ffc
diff --git a/tests/dh-compute.c b/tests/dh-compute.c
7e1ffc
index 828fb05e9c..6c1d5328f6 100644
7e1ffc
--- a/tests/dh-compute.c
7e1ffc
+++ b/tests/dh-compute.c
7e1ffc
@@ -156,34 +156,10 @@ void doit(void)
7e1ffc
 		{ NULL }
7e1ffc
 	};
7e1ffc
 
7e1ffc
-#define FIPS_PUSH_CONTEXT() do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_push_context(fips_context);	\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_push_context failed\n");	\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
-#define FIPS_POP_CONTEXT(state) do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_pop_context();			\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_context_pop failed\n");	\
7e1ffc
-		}							\
7e1ffc
-		fips_state = gnutls_fips140_get_operation_state(fips_context); \
7e1ffc
-		if (fips_state != state) {				\
7e1ffc
-			fail("operation state is not %d (%d)\n",	\
7e1ffc
-			     state, fips_state);			\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
 	for (int i = 0; test_data[i].name != NULL; i++) {
7e1ffc
 		gnutls_datum_t priv_key, pub_key;
7e1ffc
 		gnutls_dh_params_t dh_params;
7e1ffc
 		gnutls_fips140_context_t fips_context;
7e1ffc
-		gnutls_fips140_operation_state_t fips_state;
7e1ffc
 		int ret;
7e1ffc
 
7e1ffc
 		if (gnutls_fips140_mode_enabled()) {
7e1ffc
@@ -193,24 +169,24 @@ void doit(void)
7e1ffc
 			}
7e1ffc
 		}
7e1ffc
 
7e1ffc
-		FIPS_PUSH_CONTEXT();
7e1ffc
+		fips_push_context(fips_context);
7e1ffc
 		params(&dh_params, &test_data[i].prime, &test_data[i].q,
7e1ffc
 		       &test_data[i].generator);
7e1ffc
-		FIPS_POP_CONTEXT(GNUTLS_FIPS140_OP_INITIAL);
7e1ffc
+		fips_pop_context(fips_context, GNUTLS_FIPS140_OP_INITIAL);
7e1ffc
 
7e1ffc
 		success("%s genkey\n", test_data[i].name);
7e1ffc
 
7e1ffc
-		FIPS_PUSH_CONTEXT();
7e1ffc
+		fips_push_context(fips_context);
7e1ffc
 		genkey(dh_params, &priv_key, &pub_key);
7e1ffc
-		FIPS_POP_CONTEXT(test_data[i].fips_state_genkey);
7e1ffc
+		fips_pop_context(fips_context, test_data[i].fips_state_genkey);
7e1ffc
 
7e1ffc
 		success("%s compute_key\n", test_data[i].name);
7e1ffc
 		
7e1ffc
-		FIPS_PUSH_CONTEXT();
7e1ffc
+		fips_push_context(fips_context);
7e1ffc
 		compute_key(test_data[i].name, dh_params, &priv_key,
7e1ffc
 			    &pub_key, &test_data[i].peer_key,
7e1ffc
 			    test_data[i].expected_error, NULL, 0);
7e1ffc
-		FIPS_POP_CONTEXT(test_data[i].fips_state_compute_key);
7e1ffc
+		fips_pop_context(fips_context, test_data[i].fips_state_compute_key);
7e1ffc
 
7e1ffc
 		gnutls_dh_params_deinit(dh_params);
7e1ffc
 		gnutls_free(priv_key.data);
7e1ffc
diff --git a/tests/fips-rsa-sizes.c b/tests/fips-rsa-sizes.c
7e1ffc
index 84b9affabb..5feb284503 100644
7e1ffc
--- a/tests/fips-rsa-sizes.c
7e1ffc
+++ b/tests/fips-rsa-sizes.c
7e1ffc
@@ -27,25 +27,6 @@
7e1ffc
 #include <gnutls/abstract.h>
7e1ffc
 #include <gnutls/x509.h>
7e1ffc
 
7e1ffc
-#define FIPS_PUSH_CONTEXT() do {				\
7e1ffc
-	ret = gnutls_fips140_push_context(fips_context);	\
7e1ffc
-	if (ret < 0) {						\
7e1ffc
-		fail("gnutls_fips140_push_context failed\n");	\
7e1ffc
-	}							\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
-#define FIPS_POP_CONTEXT(state) do {					\
7e1ffc
-	ret = gnutls_fips140_pop_context();				\
7e1ffc
-	if (ret < 0) {							\
7e1ffc
-		fail("gnutls_fips140_context_pop failed\n");		\
7e1ffc
-	}								\
7e1ffc
-	fips_state = gnutls_fips140_get_operation_state(fips_context);	\
7e1ffc
-	if (fips_state != GNUTLS_FIPS140_OP_ ## state) {		\
7e1ffc
-		fail("operation state is not " # state " (%d)\n",	\
7e1ffc
-		     fips_state);					\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
 
7e1ffc
 void generate_successfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
7e1ffc
                            unsigned int size);
7e1ffc
@@ -63,7 +44,6 @@ void generate_successfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
7e1ffc
 	int ret;
7e1ffc
 	gnutls_x509_privkey_t xprivkey;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 	assert(gnutls_fips140_context_init(&fips_context) == 0);
7e1ffc
 
7e1ffc
 	fprintf(stderr, "%d-bit\n", size);
7e1ffc
@@ -102,7 +82,6 @@ void generate_unsuccessfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
7e1ffc
 	int ret;
7e1ffc
 	gnutls_x509_privkey_t xprivkey;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 	assert(gnutls_fips140_context_init(&fips_context) == 0);
7e1ffc
 
7e1ffc
 	fprintf(stderr, "%d-bit\n", size);
7e1ffc
@@ -156,7 +135,6 @@ void generate_unsuccessfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
7e1ffc
 void sign_verify_successfully(gnutls_privkey_t privkey, gnutls_pubkey_t pubkey) {
7e1ffc
 	int ret;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 
7e1ffc
 	gnutls_datum_t signature;
7e1ffc
 	gnutls_datum_t plaintext = {
7e1ffc
@@ -190,7 +168,6 @@ void sign_verify_unsuccessfully(gnutls_privkey_t privkey,
7e1ffc
                                 gnutls_pubkey_t pubkey) {
7e1ffc
 	int ret;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 
7e1ffc
 	gnutls_datum_t signature;
7e1ffc
 	gnutls_datum_t plaintext = {
7e1ffc
@@ -225,7 +202,6 @@ void sign_verify_unsuccessfully(gnutls_privkey_t privkey,
7e1ffc
 void nosign_verify(gnutls_privkey_t privkey, gnutls_pubkey_t pubkey) {
7e1ffc
 	int ret;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 
7e1ffc
 	gnutls_datum_t signature;
7e1ffc
 	gnutls_datum_t plaintext = {
7e1ffc
diff --git a/tests/fips-test.c b/tests/fips-test.c
7e1ffc
index f789afb107..b0bae4ef9f 100644
7e1ffc
--- a/tests/fips-test.c
7e1ffc
+++ b/tests/fips-test.c
7e1ffc
@@ -12,25 +12,6 @@
7e1ffc
 /* This does check the FIPS140 support.
7e1ffc
  */
7e1ffc
 
7e1ffc
-#define FIPS_PUSH_CONTEXT() do {				\
7e1ffc
-	ret = gnutls_fips140_push_context(fips_context);	\
7e1ffc
-	if (ret < 0) {						\
7e1ffc
-		fail("gnutls_fips140_push_context failed\n");	\
7e1ffc
-	}							\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
-#define FIPS_POP_CONTEXT(state) do {					\
7e1ffc
-	ret = gnutls_fips140_pop_context();				\
7e1ffc
-	if (ret < 0) {							\
7e1ffc
-		fail("gnutls_fips140_context_pop failed\n");		\
7e1ffc
-	}								\
7e1ffc
-	fips_state = gnutls_fips140_get_operation_state(fips_context);	\
7e1ffc
-	if (fips_state != GNUTLS_FIPS140_OP_ ## state) {		\
7e1ffc
-		fail("operation state is not " # state " (%d)\n",	\
7e1ffc
-		     fips_state);					\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
 void _gnutls_lib_simulate_error(void);
7e1ffc
 
7e1ffc
 static void tls_log_func(int level, const char *str)
7e1ffc
@@ -40,10 +21,9 @@ static void tls_log_func(int level, const char *str)
7e1ffc
 
7e1ffc
 static uint8_t key16[16];
7e1ffc
 static uint8_t iv16[16];
7e1ffc
-uint8_t key_data[64];
7e1ffc
-uint8_t iv_data[16];
7e1ffc
-gnutls_fips140_context_t fips_context;
7e1ffc
-gnutls_fips140_operation_state_t fips_state;
7e1ffc
+static uint8_t key_data[64];
7e1ffc
+static uint8_t iv_data[16];
7e1ffc
+static gnutls_fips140_context_t fips_context;
7e1ffc
 
7e1ffc
 static const gnutls_datum_t data = { .data = (unsigned char *)"foo", 3 };
7e1ffc
 static const uint8_t rsa2342_sha1_sig_data[] = {
7e1ffc
@@ -276,6 +256,7 @@ test_ciphers(void)
7e1ffc
 void doit(void)
7e1ffc
 {
7e1ffc
 	int ret;
7e1ffc
+	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 	unsigned int mode;
7e1ffc
 	gnutls_cipher_hd_t ch;
7e1ffc
 	gnutls_hmac_hd_t mh;
7e1ffc
diff --git a/tests/kdf-api.c b/tests/kdf-api.c
7e1ffc
index 9724502005..a28ce82a62 100644
7e1ffc
--- a/tests/kdf-api.c
7e1ffc
+++ b/tests/kdf-api.c
7e1ffc
@@ -33,30 +33,7 @@
7e1ffc
 #define MAX_BUF 1024
7e1ffc
 
7e1ffc
 static gnutls_fips140_context_t fips_context;
7e1ffc
-static gnutls_fips140_operation_state_t fips_state;
7e1ffc
-
7e1ffc
-#define FIPS_PUSH_CONTEXT() do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_push_context(fips_context);	\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_push_context failed\n");	\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
-#define FIPS_POP_CONTEXT(state) do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_pop_context();			\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_context_pop failed\n");	\
7e1ffc
-		}							\
7e1ffc
-		fips_state = gnutls_fips140_get_operation_state(fips_context); \
7e1ffc
-		if (fips_state != GNUTLS_FIPS140_OP_ ## state) {	\
7e1ffc
-			fail("operation state is not " # state " (%d)\n", \
7e1ffc
-			     fips_state);				\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
+
7e1ffc
 
7e1ffc
 static void
7e1ffc
 test_hkdf(gnutls_mac_algorithm_t mac,
7e1ffc
@@ -74,7 +51,6 @@ test_hkdf(gnutls_mac_algorithm_t mac,
7e1ffc
 	gnutls_datum_t prk;
7e1ffc
 	gnutls_datum_t okm;
7e1ffc
 	uint8_t buf[MAX_BUF];
7e1ffc
-	int ret;
7e1ffc
 
7e1ffc
 	success("HKDF test with %s\n", gnutls_mac_get_name(mac));
7e1ffc
 
7e1ffc
@@ -144,7 +120,6 @@ test_pbkdf2(gnutls_mac_algorithm_t mac,
7e1ffc
 	gnutls_datum_t salt;
7e1ffc
 	gnutls_datum_t okm;
7e1ffc
 	uint8_t buf[MAX_BUF];
7e1ffc
-	int ret;
7e1ffc
 
7e1ffc
 	success("PBKDF2 test with %s\n", gnutls_mac_get_name(mac));
7e1ffc
 
7e1ffc
diff --git a/tests/pkcs12_encode.c b/tests/pkcs12_encode.c
7e1ffc
index ea39f3d69e..dc55daccde 100644
7e1ffc
--- a/tests/pkcs12_encode.c
7e1ffc
+++ b/tests/pkcs12_encode.c
7e1ffc
@@ -70,29 +70,6 @@ static void tls_log_func(int level, const char *str)
7e1ffc
 	fprintf(stderr, "|<%d>| %s", level, str);
7e1ffc
 }
7e1ffc
 
7e1ffc
-#define FIPS_PUSH_CONTEXT() do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_push_context(fips_context);	\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_push_context failed\n");	\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
-#define FIPS_POP_CONTEXT(state) do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_pop_context();			\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_context_pop failed\n");	\
7e1ffc
-		}							\
7e1ffc
-		fips_state = gnutls_fips140_get_operation_state(fips_context); \
7e1ffc
-		if (fips_state != GNUTLS_FIPS140_OP_ ## state) {	\
7e1ffc
-			fail("operation state is not " # state " (%d)\n", \
7e1ffc
-			     fips_state);				\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
 void doit(void)
7e1ffc
 {
7e1ffc
 	gnutls_pkcs12_t pkcs12;
7e1ffc
@@ -106,7 +83,6 @@ void doit(void)
7e1ffc
 	size_t size;
7e1ffc
 	unsigned i;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
 	size_t n_tests = 0;
7e1ffc
 	struct tests {
7e1ffc
 		const char *name;
7e1ffc
diff --git a/tests/privkey-keygen.c b/tests/privkey-keygen.c
7e1ffc
index 2766afee08..2531906d71 100644
7e1ffc
--- a/tests/privkey-keygen.c
7e1ffc
+++ b/tests/privkey-keygen.c
7e1ffc
@@ -119,30 +119,6 @@ void doit(void)
7e1ffc
 	gnutls_x509_privkey_t pkey, dst;
7e1ffc
 	int ret, algorithm, i;
7e1ffc
 	gnutls_fips140_context_t fips_context;
7e1ffc
-	gnutls_fips140_operation_state_t fips_state;
7e1ffc
-
7e1ffc
-#define FIPS_PUSH_CONTEXT() do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_push_context(fips_context);	\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_push_context failed\n");	\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
-
7e1ffc
-#define FIPS_POP_CONTEXT(state) do {					\
7e1ffc
-	if (gnutls_fips140_mode_enabled()) {				\
7e1ffc
-		ret = gnutls_fips140_pop_context();			\
7e1ffc
-		if (ret < 0) {						\
7e1ffc
-			fail("gnutls_fips140_context_pop failed\n");	\
7e1ffc
-		}							\
7e1ffc
-		fips_state = gnutls_fips140_get_operation_state(fips_context); \
7e1ffc
-		if (fips_state != GNUTLS_FIPS140_OP_ ## state) {	\
7e1ffc
-			fail("operation state is not " # state " (%d)\n", \
7e1ffc
-			     fips_state);				\
7e1ffc
-		}							\
7e1ffc
-	}								\
7e1ffc
-} while (0)
7e1ffc
 
7e1ffc
 	ret = global_init();
7e1ffc
 	if (ret < 0)
7e1ffc
diff --git a/tests/utils.h b/tests/utils.h
7e1ffc
index d3a2ba8d16..4433a07057 100644
7e1ffc
--- a/tests/utils.h
7e1ffc
+++ b/tests/utils.h
7e1ffc
@@ -210,4 +210,62 @@ inline static unsigned int get_dtls_retransmit_timeout(void) {
7e1ffc
 	return (unsigned int) ul;
7e1ffc
 }
7e1ffc
 
7e1ffc
+static inline const char *
7e1ffc
+fips_operation_state_to_string(gnutls_fips140_operation_state_t state)
7e1ffc
+{
7e1ffc
+	switch (state) {
7e1ffc
+	case GNUTLS_FIPS140_OP_INITIAL:
7e1ffc
+		return "INITIAL";
7e1ffc
+	case GNUTLS_FIPS140_OP_APPROVED:
7e1ffc
+		return "APPROVED";
7e1ffc
+	case GNUTLS_FIPS140_OP_NOT_APPROVED:
7e1ffc
+		return "NOT_APPROVED";
7e1ffc
+	case GNUTLS_FIPS140_OP_ERROR:
7e1ffc
+		return "ERROR";
7e1ffc
+	default:
7e1ffc
+		/*NOTREACHED*/
7e1ffc
+		assert(0);
7e1ffc
+		return NULL;
7e1ffc
+	}
7e1ffc
+}
7e1ffc
+
7e1ffc
+static inline void
7e1ffc
+fips_push_context(gnutls_fips140_context_t context)
7e1ffc
+{
7e1ffc
+	if (gnutls_fips140_mode_enabled()) {
7e1ffc
+		int ret;
7e1ffc
+
7e1ffc
+		ret = gnutls_fips140_push_context(context);
7e1ffc
+		if (ret < 0) {
7e1ffc
+			fail("gnutls_fips140_push_context failed\n");
7e1ffc
+		}
7e1ffc
+	}
7e1ffc
+}
7e1ffc
+
7e1ffc
+static inline void
7e1ffc
+fips_pop_context(gnutls_fips140_context_t context,
7e1ffc
+		 gnutls_fips140_operation_state_t expected_state)
7e1ffc
+{
7e1ffc
+	gnutls_fips140_operation_state_t state;
7e1ffc
+
7e1ffc
+	if (gnutls_fips140_mode_enabled()) {
7e1ffc
+		int ret;
7e1ffc
+
7e1ffc
+		ret = gnutls_fips140_pop_context();
7e1ffc
+		if (ret < 0) {
7e1ffc
+			fail("gnutls_fips140_context_pop failed\n");
7e1ffc
+		}
7e1ffc
+		state = gnutls_fips140_get_operation_state(context);
7e1ffc
+		if (state != expected_state) {
7e1ffc
+			fail("operation state is not %s (%s)\n",
7e1ffc
+			     fips_operation_state_to_string(expected_state),
7e1ffc
+			     fips_operation_state_to_string(state));
7e1ffc
+		}
7e1ffc
+	}
7e1ffc
+}
7e1ffc
+
7e1ffc
+/* To use those convenient macros, define fips_context variable. */
7e1ffc
+#define FIPS_PUSH_CONTEXT() fips_push_context(fips_context)
7e1ffc
+#define FIPS_POP_CONTEXT(state) fips_pop_context(fips_context, GNUTLS_FIPS140_OP_ ## state)
7e1ffc
+
7e1ffc
 #endif /* GNUTLS_TESTS_UTILS_H */
7e1ffc
-- 
7e1ffc
2.38.1
7e1ffc