From f4b996d3cd91a343ba44a0467063921be8d085b8 Mon Sep 17 00:00:00 2001
From: Joerg Schmidbauer <jschmidb@de.ibm.com>
Date: Fri, 14 Jun 2019 17:33:14 +0200
Subject: [PATCH 004/184] Use dynamic ICA flags to display available
 algorithms.

Remove routine to search for cards. Information about available
dynamic hardware is now correctly provided via the function list.
Add a third column to the icainfo output to show dynamic and static
hardware separately.

Signed-off-by: Joerg Schmidbauer <jschmidb@de.ibm.com>
---
 src/icainfo.c | 137 ++++++--------------------------------------------
 1 file changed, 16 insertions(+), 121 deletions(-)

diff --git a/src/icainfo.c b/src/icainfo.c
index e2a79f3..c8c120d 100644
--- a/src/icainfo.c
+++ b/src/icainfo.c
@@ -34,13 +34,6 @@
 #define CMD_NAME "icainfo"
 #define COPYRIGHT "Copyright IBM Corp. 2007, 2016."
 
-#define CARD_AVAILABLE		0x01
-#define CEXnA_AVAILABLE		0x02
-#define CEXnC_AVAILABLE		0x04
-#define CEX4C_AVAILABLE		0x08
-#define CEXnP_AVAILABLE		0x10
-
-
 void print_version(void)
 {
 	printf(CMD_NAME ": libica version " VERSION "\n" COPYRIGHT "\n");
@@ -117,64 +110,13 @@ static struct crypt_pair crypt_map[] = {
 };
 
 
-static int search_for_cards()
-{
-	int ret=0, nr, version;
-	char type, fname[80], buf[80];
-	FILE *f;
-
-	for (nr = 0; nr <= 0xFF; nr++) {
-		snprintf(fname, sizeof(fname), "/sys/devices/ap/card%02x/type", nr);
-		f = fopen(fname, "r");
-		if (f) {
-			if (fgets(buf, sizeof(buf), f)) {
-				version = 0;
-				type = ' ';
-				sscanf(buf + 3, "%d%c", &version, &type);
-				ret = ret | CARD_AVAILABLE;
-				if (type == 'A') {
-					ret = ret | CEXnA_AVAILABLE;
-				} else if (type == 'C') {
-					ret = ret | CEXnC_AVAILABLE;
-					if (version >= 4) {
-						ret = ret | CEX4C_AVAILABLE;
-					}
-				} else if (type == 'P') {
-					ret = ret | CEXnP_AVAILABLE;
-				}
-			}
-			fclose(f);
-		}
-	}
-
-	return ret;
-}
-
-static inline int card_available(unsigned int flags)
-{
-	if (flags)
-		return 1;
-	else
-		return 0;
-}
-
-static inline int cex4c_available(unsigned int flags)
-{
-	if ((flags & CEX4C_AVAILABLE) == CEX4C_AVAILABLE)
-		return 1;
-	else
-		return 0;
-}
-
-
 int main(int argc, char **argv)
 {
 	int rc;
 	int index = 0;
 	unsigned int mech_len, j;
 	libica_func_list_element *pmech_list = NULL;
-	int flags;
-	int i;
+	unsigned int i;
 
 	while ((rc = getopt_long(argc, argv, getopt_string,
 				 getopt_long_options, &index)) != -1) {
@@ -199,8 +141,8 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	printf("      Cryptographic algorithm support      \n");
-	printf("-------------------------------------------\n");
+	printf("              Cryptographic algorithm support      \n");
+	printf("------------------------------------------------------\n");
 
 	if (ica_get_functionlist(NULL, &mech_len) != 0){
 		perror("get_functionlist: ");
@@ -213,12 +155,11 @@ int main(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	flags = search_for_cards();
-
 	#define CELL_SIZE 3
 
-	printf(" function      |  hardware  |  software  \n");
-	printf("---------------+------------+------------\n");
+	printf("               |         hardware        |            \n");
+	printf(" function      |   dynamic  |   static   |  software  \n");
+	printf("---------------+------------+------------+------------\n");
 	for(i = 0; crypt_map[i].algo_id; i++){
 		for(j = 0; j < mech_len; j++){
 			if(crypt_map[i].algo_id == pmech_list[j].mech_mode_id){
@@ -226,71 +167,25 @@ int main(int argc, char **argv)
 				if (((ica_fips_status() & ICA_FIPS_MODE)
 				    && !fips_approved(pmech_list[j].mech_mode_id))
 				    || ica_fips_status() >> 1) {
-					printf("%14s |  blocked   "
-						"|   blocked\n",
+					printf("%14s |  blocked   |   blocked  |   blocked\n",
 						crypt_map[i].name);
 					break;
 				}
 #endif /* ICA_FIPS */
-
-				if (crypt_map[i].algo_id == EC_DH ||
-					crypt_map[i].algo_id == EC_DSA_SIGN ||
-					crypt_map[i].algo_id == EC_DSA_VERIFY ||
-					crypt_map[i].algo_id == EC_KGEN) {
-					/* Functions that need a CEX4C or later */
-					if (cex4c_available(flags)) {
-						printf("%14s |    %*s     |     %*s\n",
-							crypt_map[i].name,
-							CELL_SIZE,
-							pmech_list[j].flags &
-							(ICA_FLAG_SHW | ICA_FLAG_DHW)
-							? "yes" : "no",
-							CELL_SIZE,
-							pmech_list[j].flags & ICA_FLAG_SW
-							? "yes" : "no");
-					} else {
-						printf("%14s |    %*s     |     %*s\n",
-							crypt_map[i].name,
-							CELL_SIZE,
-							pmech_list[j].flags &
-							(ICA_FLAG_SHW)
-							? "yes" : "no",
-							CELL_SIZE,
-							pmech_list[j].flags & ICA_FLAG_SW
-							? "yes" : "no");
-					}
-
-				} else if (card_available(flags)) {
-					/* Functions that need any card */
-					printf("%14s |    %*s     |     %*s\n",
-						crypt_map[i].name,
-						CELL_SIZE,
-						pmech_list[j].flags &
-						(ICA_FLAG_SHW | ICA_FLAG_DHW)
-						? "yes" : "no",
-						CELL_SIZE,
-						pmech_list[j].flags & ICA_FLAG_SW
-						? "yes" : "no");
-				} else {
-					printf("%14s |    %*s     |     %*s\n",
-						crypt_map[i].name,
-						CELL_SIZE,
-						(pmech_list[j].flags &
-						ICA_FLAG_SHW)
-						? "yes" : "no",
-						CELL_SIZE,
-						pmech_list[j].flags & ICA_FLAG_SW
-						? "yes" : "no");
-
-				}
-				break;
+				printf("%14s |    %*s     |    %*s     |    %*s\n",
+					crypt_map[i].name,
+					CELL_SIZE,
+					pmech_list[j].flags & ICA_FLAG_DHW ? "yes" : "no",
+					CELL_SIZE,
+					pmech_list[j].flags & ICA_FLAG_SHW ? "yes" : "no",
+					CELL_SIZE,
+					pmech_list[j].flags & ICA_FLAG_SW ? "yes" : "no");
 			}
-
 		}
 	}
 	free(pmech_list);
+	printf("------------------------------------------------------\n");
 
-	printf("-------------------------------------------\n");
 #ifdef ICA_FIPS
 	printf("Built-in FIPS support: FIPS mode %s.\n",
 	    ica_fips_status() & ICA_FIPS_MODE ? "active" : "inactive");
-- 
2.35.3

