5bcc82
diff -up libgcrypt-1.7.3/tests/cavs_driver.pl.cavs libgcrypt-1.7.3/tests/cavs_driver.pl
5bcc82
--- libgcrypt-1.7.3/tests/cavs_driver.pl.cavs	2013-03-15 20:25:38.000000000 +0100
5bcc82
+++ libgcrypt-1.7.3/tests/cavs_driver.pl	2016-11-22 17:29:06.067553077 +0100
5bcc82
@@ -1,9 +1,11 @@
5bcc82
 #!/usr/bin/env perl
5bcc82
 #
5bcc82
-# $Id: cavs_driver.pl 1497 2009-01-22 14:01:29Z smueller $
5bcc82
+# $Id: cavs_driver.pl 2124 2010-12-20 07:56:30Z smueller $
5bcc82
 #
5bcc82
 # CAVS test driver (based on the OpenSSL driver)
5bcc82
 # Written by: Stephan Müller <sm@atsec.com>
5bcc82
+#	      Werner Koch <wk@g10code.com> (libgcrypt interface)
5bcc82
+#	      Tomas Mraz <tmraz@redhat.com> (addition of DSA2)
5bcc82
 # Copyright (c) atsec information security corporation
5bcc82
 #
5bcc82
 # Permission is hereby granted, free of charge, to any person obtaining a copy
5bcc82
@@ -85,13 +87,16 @@
5bcc82
 #	T[CBC|CFB??|ECB|OFB]varkey
5bcc82
 #	T[CBC|CFB??|ECB|OFB]invperm
5bcc82
 #	T[CBC|CFB??|ECB|OFB]vartext
5bcc82
+# WARNING: TDES in CFB and OFB mode problems see below
5bcc82
 #
5bcc82
 # ANSI X9.31 RNG
5bcc82
 # 	ANSI931_AES128MCT
5bcc82
 # 	ANSI931_AES128VST
5bcc82
 #
5bcc82
-# DSA
5bcc82
+# DSA2
5bcc82
 # 	PQGGen
5bcc82
+#	PQGVer
5bcc82
+#	KeyPair
5bcc82
 # 	SigGen
5bcc82
 # 	SigVer
5bcc82
 #
5bcc82
@@ -101,6 +106,36 @@
5bcc82
 # 	RC4PltBD
5bcc82
 # 	RC4REGT
5bcc82
 #
5bcc82
+#
5bcc82
+# TDES MCT for CFB and OFB:
5bcc82
+# -------------------------
5bcc82
+# The inner loop cannot be handled by this script. If you want to have tests
5bcc82
+# for these cipher types, implement your own inner loop and add it to
5bcc82
+# crypto_mct.
5bcc82
+#
5bcc82
+# the value $next_source in crypto_mct is NOT set by the standard implementation
5bcc82
+# of this script. It would need to be set as follows for these two (code take
5bcc82
+# from fipsdrv.c from libgcrypt - the value input at the end will contain the
5bcc82
+# the value for $next_source:
5bcc82
+#
5bcc82
+# ... inner loop ...
5bcc82
+# ...
5bcc82
+# get_current_iv (hd, last_iv, blocklen);
5bcc82
+# ... encrypt / decrypt (input is the data to be en/decrypted and output is the
5bcc82
+#                        result of operation) ...
5bcc82
+# if (encrypt_mode && (cipher_mode == GCRY_CIPHER_MODE_CFB))
5bcc82
+#   memcpy (input, last_iv, blocklen);
5bcc82
+# else if (cipher_mode == GCRY_CIPHER_MODE_OFB)
5bcc82
+#   memcpy (input, last_iv, blocklen);
5bcc82
+# else if (!encrypt_mode && cipher_mode == GCRY_CIPHER_MODE_CFB)
5bcc82
+#   {
5bcc82
+#      /* Reconstruct the output vector.  */
5bcc82
+#      int i;
5bcc82
+#      for (i=0; i < blocklen; i++)
5bcc82
+#         input[i] ^= output[i];
5bcc82
+#   }
5bcc82
+# ... inner loop ends ...
5bcc82
+# ==> now, the value of input is to be put into $next_source
5bcc82
 
5bcc82
 use strict;
5bcc82
 use warnings;
5bcc82
@@ -226,6 +261,8 @@ my $hmac;
5bcc82
 # Generate the P, Q, G, Seed, counter, h (value used to generate g) values
5bcc82
 # for DSA
5bcc82
 # $1: modulus size
5bcc82
+# $2: q size
5bcc82
+# $3: seed (might be empty string)
5bcc82
 # return: string with the calculated values in hex format, where each value
5bcc82
 # 	  is separated from the previous with a \n in the following order:
5bcc82
 #         P\n
5bcc82
@@ -236,6 +273,19 @@ my $hmac;
5bcc82
 #         h
5bcc82
 my $dsa_pqggen;
5bcc82
 
5bcc82
+# Generate the G value from P and Q
5bcc82
+# for DSA
5bcc82
+# $1: modulus size
5bcc82
+# $2: q size
5bcc82
+# $3: P in hex form
5bcc82
+# $4: Q in hex form
5bcc82
+# return: string with the calculated values in hex format, where each value
5bcc82
+#        is separated from the previous with a \n in the following order:
5bcc82
+#         P\n
5bcc82
+#         Q\n
5bcc82
+#         G\n
5bcc82
+my $dsa_ggen;
5bcc82
+
5bcc82
 #
5bcc82
 # Generate an DSA public key from the provided parameters:
5bcc82
 # $1: Name of file to create
5bcc82
@@ -255,16 +305,30 @@ my $dsa_verify;
5bcc82
 
5bcc82
 # generate a new DSA key with the following properties:
5bcc82
 #	PEM format
5bcc82
-# $1 keyfile name
5bcc82
-# return: file created, hash with keys of P, Q, G in hex format
5bcc82
+# $1: modulus size
5bcc82
+# $2: q size
5bcc82
+# $3 keyfile name
5bcc82
+# return: file created with key, string with values of P, Q, G in hex format
5bcc82
 my $gen_dsakey;
5bcc82
 
5bcc82
+# generate a new DSA private key XY parameters in domain:
5bcc82
+#      PEM format
5bcc82
+# $1: P in hex form
5bcc82
+# $2: Q in hex form
5bcc82
+# $3: G in hex form
5bcc82
+# return: string with values of X, Y in hex format
5bcc82
+my $gen_dsakey_domain;
5bcc82
+
5bcc82
 # Sign a message with DSA
5bcc82
 # $1: data to be signed in hex form
5bcc82
 # $2: Key file in PEM format with the private key
5bcc82
 # return: hash of digest information in hex format with Y, R, S as keys
5bcc82
 my $dsa_sign;
5bcc82
 
5bcc82
+my $rsa_keygen;
5bcc82
+
5bcc82
+my $rsa_keygen_kat;
5bcc82
+
5bcc82
 ################################################################
5bcc82
 ##### OpenSSL interface functions
5bcc82
 ################################################################
5bcc82
@@ -404,6 +468,35 @@ sub libgcrypt_rsa_derive($$$$$$$$) {
5bcc82
 }
5bcc82
 
5bcc82
 
5bcc82
+sub libgcrypt_rsa_keygen($) {
5bcc82
+	my $n   = shift;
5bcc82
+	my $sexp;
5bcc82
+
5bcc82
+	$n = sprintf ("%u", $n);
5bcc82
+	$sexp = "(genkey(rsa(nbits " . sprintf ("%u:%s", length($n), $n) . ")))\n";
5bcc82
+
5bcc82
+	return pipe_through_program($sexp, "fipsdrv rsa-keygen");
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+sub libgcrypt_rsa_keygen_kat($$$$) {
5bcc82
+	my $n   = shift;
5bcc82
+	my $e   = shift;
5bcc82
+	my $p   = shift;
5bcc82
+	my $q   = shift;
5bcc82
+	my $sexp;
5bcc82
+
5bcc82
+	$n = sprintf ("%u", $n);
5bcc82
+	$sexp = "(genkey(rsa(nbits " . sprintf ("%u:%s", length($n), $n) . ")"
5bcc82
+		. "(test-parms"
5bcc82
+		. "(e #$e#)"
5bcc82
+		. "(p #$p#)"
5bcc82
+		. "(q #$q#))))\n";
5bcc82
+
5bcc82
+	return pipe_through_program($sexp, "fipsdrv rsa-keygen-kat");
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
 sub libgcrypt_rsa_sign($$$) {
5bcc82
 	my $data = shift;
5bcc82
 	my $hashalgo = shift;
5bcc82
@@ -500,17 +593,32 @@ sub libgcrypt_hmac($$$$) {
5bcc82
 	return pipe_through_program($msg, $program);
5bcc82
 }
5bcc82
 
5bcc82
-sub libgcrypt_dsa_pqggen($) {
5bcc82
+sub libgcrypt_dsa_pqggen($$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
+	my $seed = shift;
5bcc82
+
5bcc82
+	my $program = "fipsdrv --keysize $mod --qsize $qsize dsa-pqg-gen";
5bcc82
+	return pipe_through_program($seed, $program);
5bcc82
+}
5bcc82
+
5bcc82
+sub libgcrypt_dsa_ggen($$$$) {
5bcc82
 	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
+	my $p = shift;
5bcc82
+	my $q = shift;
5bcc82
+	my $domain = "(domain (p #$p#)(q #$q#))";
5bcc82
 
5bcc82
-	my $program = "fipsdrv --keysize $mod dsa-pqg-gen";
5bcc82
+	my $program = "fipsdrv --keysize $mod --qsize $qsize --key \'$domain\' dsa-g-gen";
5bcc82
 	return pipe_through_program("", $program);
5bcc82
 }
5bcc82
 
5bcc82
-sub libgcrypt_gen_dsakey($) {
5bcc82
+sub libgcrypt_gen_dsakey($$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
 	my $file = shift;
5bcc82
 
5bcc82
-	my $program = "fipsdrv --keysize 1024 --key $file dsa-gen";
5bcc82
+	my $program = "fipsdrv --keysize $mod --qsize $qsize --key $file dsa-gen";
5bcc82
 	my $tmp;
5bcc82
 	my %ret;
5bcc82
 
5bcc82
@@ -519,10 +627,21 @@ sub libgcrypt_gen_dsakey($) {
5bcc82
 	$tmp = pipe_through_program("", $program);
5bcc82
 	die "dsa key gen failed: file $file not created" if (! -f $file);
5bcc82
 
5bcc82
-	@ret{'P', 'Q', 'G', 'Seed', 'c', 'H'} = split(/\n/, $tmp);
5bcc82
+	@ret{'P', 'Q', 'G'} = split(/\n/, $tmp);
5bcc82
 	return %ret;
5bcc82
 }
5bcc82
 
5bcc82
+sub libgcrypt_gen_dsakey_domain($$$) {
5bcc82
+	my $p = shift;
5bcc82
+	my $q = shift;
5bcc82
+	my $g = shift;
5bcc82
+	my $domain = "(domain (p #$p#)(q #$q#)(g #$g#))";
5bcc82
+
5bcc82
+	my $program = "fipsdrv --key '$domain' dsa-gen-key";
5bcc82
+
5bcc82
+	return pipe_through_program("", $program);
5bcc82
+}
5bcc82
+
5bcc82
 sub libgcrypt_dsa_genpubkey($$$$$) {
5bcc82
 	my $filename = shift;
5bcc82
 	my $p = shift;
5bcc82
@@ -1139,7 +1258,7 @@ sub hmac_kat($$$$) {
5bcc82
 	$out .= "Tlen = $tlen\n";
5bcc82
 	$out .= "Key = $key\n";
5bcc82
 	$out .= "Msg = $msg\n";
5bcc82
-	$out .= "Mac = " . &$hmac($key, $tlen, $msg, $hashtype{$tlen}) . "\n";
5bcc82
+	$out .= "Mac = " . lc(&$hmac($key, $tlen, $msg, $hashtype{$tlen})) . "\n";
5bcc82
 
5bcc82
 	return $out;
5bcc82
 }
5bcc82
@@ -1205,7 +1324,7 @@ sub crypto_mct($$$$$$$$) {
5bcc82
                 }
5bcc82
                 my ($CO, $CI);
5bcc82
 		my $cipher_imp = &$state_cipher($cipher, $enc, $bufsize, $key1, $iv);
5bcc82
-		$cipher_imp = &$state_cipher_des($cipher, $enc, $bufsize, $key1, $iv) if($cipher =~ /des/);
5bcc82
+		$cipher_imp = &$state_cipher_des($cipher, $enc, $bufsize, $key1, $iv) if($cipher =~ /des/ && defined($state_cipher_des));
5bcc82
                 my $pid = open2($CO, $CI, $cipher_imp);
5bcc82
 
5bcc82
                 my $calc_data = $iv; # CT[j]
5bcc82
@@ -1213,8 +1332,8 @@ sub crypto_mct($$$$$$$$) {
5bcc82
                 my $old_old_calc_data; # CT[j-2]
5bcc82
 		my $next_source;
5bcc82
 
5bcc82
-		# TDES inner loop implements logic within driver
5bcc82
-		if ($cipher =~ /des/) {
5bcc82
+		# TDES inner loop implements logic within driver of libgcrypt
5bcc82
+		if ($cipher =~ /des/ && $opt{'I'} && $opt{'I'} eq 'libgcrypt' ) {
5bcc82
 			# Need to provide a dummy IV in case of ECB mode.
5bcc82
 			my $iv_arg = (defined($iv) && $iv ne "")
5bcc82
 					? bin2hex($iv)
5bcc82
@@ -1238,6 +1357,10 @@ sub crypto_mct($$$$$$$$) {
5bcc82
 			$line = <$CO>;
5bcc82
 		} else {
5bcc82
 	                for (my $j = 0; $j < $iloop; ++$j) {
5bcc82
+				if ($cipher =~ /des-ede3-ofb/ ||
5bcc82
+				    (!$enc && $cipher =~ /des-ede3-cfb/)) {
5bcc82
+					die "Implementation lacks support for TDES OFB and TDES CFB in encryption mode - the problem is that we would need to extract the IV of the last round of encryption which would be the input for the next round - see comments in this script for implementation requirements"; 
5bcc82
+				}
5bcc82
 				$old_old_calc_data = $old_calc_data;
5bcc82
                 	        $old_calc_data = $calc_data;
5bcc82
 
5bcc82
@@ -1429,7 +1552,7 @@ sub rsa_sigver($$$$$) {
5bcc82
 # $7 xq2
5bcc82
 # $8 Xq
5bcc82
 # return: string formatted as expected by CAVS
5bcc82
-sub rsa_keygen($$$$$$$$) {
5bcc82
+sub rsa_keygen_x931($$$$$$$$) {
5bcc82
 	my $modulus = shift;
5bcc82
 	my $e = shift;
5bcc82
 	my $xp1 = shift;
5bcc82
@@ -1503,21 +1626,23 @@ sub rngx931($$$$) {
5bcc82
 	return $out;
5bcc82
 }
5bcc82
 
5bcc82
-# DSA PQGGen test
5bcc82
+# DSA PQGen test
5bcc82
 # $1 modulus size
5bcc82
-# $2 number of rounds to perform the test
5bcc82
+# $2 q size
5bcc82
+# $3 number of rounds to perform the test
5bcc82
 # return: string formatted as expected by CAVS
5bcc82
-sub dsa_pqggen_driver($$) {
5bcc82
+sub dsa_pqgen_driver($$$) {
5bcc82
 	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
 	my $rounds = shift;
5bcc82
 
5bcc82
 	my $out = "";
5bcc82
 	for(my $i=0; $i<$rounds; $i++) {
5bcc82
-		my $ret = &$dsa_pqggen($mod);
5bcc82
+		my $ret = &$dsa_pqggen($mod, $qsize, "");
5bcc82
 		my ($P, $Q, $G, $Seed, $c, $H) = split(/\n/, $ret);
5bcc82
-		die "Return value does not contain all expected values of P, Q, G, Seed, c, H for dsa_pqggen"
5bcc82
-			if (!defined($P) || !defined($Q) || !defined($G) ||
5bcc82
-			    !defined($Seed) || !defined($c) || !defined($H));
5bcc82
+		die "Return value does not contain all expected values of P, Q, Seed, c for dsa_pqggen"
5bcc82
+			if (!defined($P) || !defined($Q) ||
5bcc82
+			    !defined($Seed) || !defined($c));
5bcc82
 
5bcc82
 		# now change the counter to decimal as CAVS wants decimal
5bcc82
 		# counter value although all other is HEX
5bcc82
@@ -1525,15 +1650,166 @@ sub dsa_pqggen_driver($$) {
5bcc82
 
5bcc82
 		$out .= "P = $P\n";
5bcc82
 		$out .= "Q = $Q\n";
5bcc82
-		$out .= "G = $G\n";
5bcc82
-		$out .= "Seed = $Seed\n";
5bcc82
-		$out .= "c = $c\n";
5bcc82
-		$out .= "H = $H\n\n";
5bcc82
+		$out .= "domain_parameter_seed = $Seed\n";
5bcc82
+		$out .= "counter = $c\n\n";
5bcc82
 	}
5bcc82
 
5bcc82
 	return $out;
5bcc82
 }
5bcc82
 
5bcc82
+# DSA GGen test
5bcc82
+# $1 modulus size
5bcc82
+# $2 q size
5bcc82
+# $3 p in hex form
5bcc82
+# $4 q in hex form
5bcc82
+# return: string formatted as expected by CAVS
5bcc82
+sub dsa_ggen_driver($$$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
+	my $p = shift;
5bcc82
+	my $q = shift;
5bcc82
+
5bcc82
+	my $out = "";
5bcc82
+	my $ret = &$dsa_ggen($mod, $qsize, $p, $q);
5bcc82
+	my ($P, $Q, $G) = split(/\n/, $ret);
5bcc82
+	die "Return value does not contain all expected values of P, Q, G for dsa_ggen"
5bcc82
+		if (!defined($P) || !defined($Q) || !defined($G));
5bcc82
+
5bcc82
+	$out .= "G = $G\n\n";
5bcc82
+
5bcc82
+	return $out;
5bcc82
+}
5bcc82
+
5bcc82
+sub hexcomp($$) {
5bcc82
+	my $a = lc shift;
5bcc82
+	my $b = lc shift;
5bcc82
+
5bcc82
+	if (length $a < length $b) {
5bcc82
+		my $c = $a;
5bcc82
+		$a = $b;
5bcc82
+		$b = $a;
5bcc82
+	}
5bcc82
+
5bcc82
+	while (length $b < length $a) {
5bcc82
+		$b = "00$b";
5bcc82
+	}
5bcc82
+
5bcc82
+	return $a eq $b;
5bcc82
+}
5bcc82
+
5bcc82
+# DSA PQVer test
5bcc82
+# $1 modulus size
5bcc82
+# $2 q size
5bcc82
+# $3 p in hex form
5bcc82
+# $4 q in hex form
5bcc82
+# $5 seed in hex form
5bcc82
+# $6 c decimal counter
5bcc82
+# return: string formatted as expected by CAVS
5bcc82
+sub dsa_pqver_driver($$$$$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
+	my $p = shift;
5bcc82
+	my $q = shift;
5bcc82
+	my $seed = shift;
5bcc82
+	my $c = shift;
5bcc82
+
5bcc82
+	my $out = "";
5bcc82
+	my $ret = &$dsa_pqggen($mod, $qsize, $seed);
5bcc82
+	my ($P, $Q, $G, $seed2, $c2, $h2) = split(/\n/, $ret);
5bcc82
+	die "Return value does not contain all expected values of P, Q, G, seed, c for dsa_pqggen"
5bcc82
+		if (!defined($P) || !defined($Q) || !defined($G) ||
5bcc82
+		    !defined($seed2) || !defined($c2));
5bcc82
+
5bcc82
+	$c2 = hex($c2);
5bcc82
+
5bcc82
+	$out .= "Seed = $seed\n";
5bcc82
+	$out .= "c = $c\n";
5bcc82
+
5bcc82
+	if (hexcomp($P, $p) && hexcomp($Q, $q) && hexcomp($seed, $seed2) && $c == $c2) {
5bcc82
+		$out .= "Result = P\n\n";
5bcc82
+	}
5bcc82
+	else {
5bcc82
+		$out .= "Result = F\n\n";
5bcc82
+	}
5bcc82
+	return $out;
5bcc82
+}
5bcc82
+
5bcc82
+# DSA PQGVer test
5bcc82
+# $1 modulus size
5bcc82
+# $2 q size
5bcc82
+# $3 p in hex form
5bcc82
+# $4 q in hex form
5bcc82
+# $5 g in hex form
5bcc82
+# $6 seed in hex form
5bcc82
+# $7 c decimal counter
5bcc82
+# $8 h in hex form
5bcc82
+# return: string formatted as expected by CAVS
5bcc82
+sub dsa_pqgver_driver($$$$$$$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
+	my $p = shift;
5bcc82
+	my $q = shift;
5bcc82
+	my $g = shift;
5bcc82
+	my $seed = shift;
5bcc82
+	my $c = shift;
5bcc82
+	my $h = shift;
5bcc82
+
5bcc82
+	my $out = "";
5bcc82
+	my $ret = &$dsa_pqggen($mod, $qsize, $seed);
5bcc82
+	my ($P, $Q, $G, $seed2, $c2, $h2) = split(/\n/, $ret);
5bcc82
+	die "Return value does not contain all expected values of P, Q, G, seed, c, H for dsa_pqggen"
5bcc82
+		if (!defined($P) || !defined($Q) || !defined($G) ||
5bcc82
+		    !defined($seed2) || !defined($c2) || !defined($h2));
5bcc82
+
5bcc82
+	
5bcc82
+
5bcc82
+	$out .= "Seed = $seed\n";
5bcc82
+	$out .= "c = $c\n";
5bcc82
+	$out .= "H = $h\n";
5bcc82
+
5bcc82
+	$c2 = hex($c2);
5bcc82
+
5bcc82
+	if (hexcomp($P, $p) && hexcomp($Q, $q) && hexcomp($G, $g) && hexcomp($seed, $seed2) && 
5bcc82
+	    $c == $c2 && hex($h) == hex($h2)) {
5bcc82
+		$out .= "Result = P\n\n";
5bcc82
+	}
5bcc82
+	else {
5bcc82
+		$out .= "Result = F\n\n";
5bcc82
+	}
5bcc82
+
5bcc82
+	return $out;
5bcc82
+}
5bcc82
+
5bcc82
+# DSA Keypair test
5bcc82
+# $1 modulus size
5bcc82
+# $2 q size
5bcc82
+# $3 number of rounds to perform the test
5bcc82
+# return: string formatted as expected by CAVS
5bcc82
+sub dsa_keypair_driver($$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $qsize = shift;
5bcc82
+	my $rounds = shift;
5bcc82
+
5bcc82
+	my $out = "";
5bcc82
+	my $tmpkeyfile = "dsa_siggen.tmp.$$";
5bcc82
+	my %pqg = &$gen_dsakey($mod, $qsize, $tmpkeyfile);
5bcc82
+	$out .= "P = " . $pqg{'P'} . "\n";
5bcc82
+	$out .= "Q = " . $pqg{'Q'} . "\n";
5bcc82
+	$out .= "G = " . $pqg{'G'} . "\n\n";
5bcc82
+	unlink($tmpkeyfile);
5bcc82
+
5bcc82
+	for(my $i=0; $i<$rounds; $i++) {
5bcc82
+		my $ret = &$gen_dsakey_domain($pqg{'P'}, $pqg{'Q'}, $pqg{'G'});
5bcc82
+		my ($X, $Y) = split(/\n/, $ret);
5bcc82
+		die "Return value does not contain all expected values of X, Y for gen_dsakey_domain"
5bcc82
+			if (!defined($X) || !defined($Y));
5bcc82
+
5bcc82
+		$out .= "X = $X\n";
5bcc82
+		$out .= "Y = $Y\n\n";
5bcc82
+	}
5bcc82
+
5bcc82
+	return $out;
5bcc82
+}
5bcc82
 
5bcc82
 # DSA SigGen test
5bcc82
 # $1: Message to be signed in hex form
5bcc82
@@ -1598,6 +1874,53 @@ sub dsa_sigver($$$$$$$$) {
5bcc82
 	return $out;
5bcc82
 }
5bcc82
 
5bcc82
+# RSA Keygen RPP test
5bcc82
+# $1 modulus size
5bcc82
+# $2 number of rounds to perform the test
5bcc82
+# return: string formatted as expected by CAVS
5bcc82
+sub rsa_keygen_driver($$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $rounds = shift;
5bcc82
+
5bcc82
+	my $out = "";
5bcc82
+
5bcc82
+	for(my $i=0; $i<$rounds; $i++) {
5bcc82
+		my $ret = &$rsa_keygen($mod);
5bcc82
+		my ($e, $p, $q, $n, $d) = split(/\n/, $ret);
5bcc82
+		die "Return value does not contain all expected values of e, p, q, n, d for rsa_keygen"
5bcc82
+			if (!defined($e) || !defined($p) || !defined($q) || !defined($n) || !defined($d));
5bcc82
+
5bcc82
+		$out .= "e = $e\n";
5bcc82
+		$out .= "p = $p\n";
5bcc82
+		$out .= "q = $q\n";
5bcc82
+		$out .= "n = $n\n";
5bcc82
+		$out .= "d = $d\n\n";
5bcc82
+	}
5bcc82
+
5bcc82
+	return $out;
5bcc82
+}
5bcc82
+
5bcc82
+# RSA RPP Keygen KAT test
5bcc82
+# $1 modulus size
5bcc82
+# $2 p in hex form
5bcc82
+# $3 q in hex form
5bcc82
+# return: string formatted as expected by CAVS
5bcc82
+sub rsa_keygen_kat_driver($$$) {
5bcc82
+	my $mod = shift;
5bcc82
+	my $p = shift;
5bcc82
+	my $q = shift;
5bcc82
+
5bcc82
+	my $out = "";
5bcc82
+	my $ret = &$rsa_keygen_kat($mod, $p, $q);
5bcc82
+	my ($Result) = split(/\n/, $ret);
5bcc82
+	die "Return value does not contain all expected values of Result for rsa_keygen_kat"
5bcc82
+		if (!defined($Result));
5bcc82
+
5bcc82
+	$out .= "Result = $Result\n\n";
5bcc82
+	return $out;
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
 ##############################################################
5bcc82
 # Parser of input file and generator of result file
5bcc82
 #
5bcc82
@@ -1658,12 +1981,18 @@ sub parse($$) {
5bcc82
 	my $klen = "";
5bcc82
 	my $tlen = "";
5bcc82
 	my $modulus = "";
5bcc82
+	my $qsize = "";
5bcc82
 	my $capital_n = 0;
5bcc82
+	my $num = 0;
5bcc82
 	my $capital_p = "";
5bcc82
 	my $capital_q = "";
5bcc82
 	my $capital_g = "";
5bcc82
 	my $capital_y = "";
5bcc82
 	my $capital_r = "";
5bcc82
+	my $capital_h = "";
5bcc82
+	my $c = "";
5bcc82
+	my $prandom = "";
5bcc82
+	my $qrandom = "";
5bcc82
 	my $xp1 = "";
5bcc82
 	my $xp2 = "";
5bcc82
 	my $Xp = "";
5bcc82
@@ -1700,7 +2029,7 @@ sub parse($$) {
5bcc82
 
5bcc82
 		##### Extract cipher
5bcc82
 		# XXX there may be more - to be added
5bcc82
-		if ($tmpline =~ /^#.*(CBC|ECB|OFB|CFB|SHA-|SigGen|SigVer|RC4VS|ANSI X9\.31|Hash sizes tested|PQGGen|KeyGen RSA)/) {
5bcc82
+		if ($tmpline =~ /^#.*(CBC|ECB|OFB|CFB|SHA-|SigGen|SigVer|RC4VS|ANSI X9\.31|Hash sizes tested|PQGGen|KeyGen RSA|KeyGen - Random Probably Prime|KeyPair|PQGVer)/) {
5bcc82
 			if ($tmpline    =~ /CBC/)   { $mode="cbc"; }
5bcc82
 			elsif ($tmpline =~ /ECB/)   { $mode="ecb"; }
5bcc82
 			elsif ($tmpline =~ /OFB/)   { $mode="ofb"; }
5bcc82
@@ -1749,7 +2078,23 @@ sub parse($$) {
5bcc82
 
5bcc82
 			if ($tt == 0) {
5bcc82
 			##### Identify the test type
5bcc82
-				if ($tmpline =~ /KeyGen RSA \(X9\.31\)/) {
5bcc82
+				if ($tmpline =~ /KeyGen - Random Probably Prime Known Answer Test/) {
5bcc82
+					$tt = 19;
5bcc82
+					die "Interface function rsa_keygen_kat for RSA key generation KAT not defined for tested library"
5bcc82
+						if (!defined($rsa_keygen_kat));
5bcc82
+				} elsif ($tmpline =~ /KeyGen - Random Probably Prime Test/) {
5bcc82
+					$tt = 18;
5bcc82
+					die "Interface function rsa_keygen for RSA key generation not defined for tested library"
5bcc82
+						if (!defined($rsa_keygen));
5bcc82
+				} elsif ($tmpline =~ /PQGVer/) {
5bcc82
+					$tt = 16;
5bcc82
+					die "Interface function for DSA PQGVer testing not defined for tested library"
5bcc82
+						if (!defined($dsa_pqggen));
5bcc82
+				} elsif ($tmpline =~ /KeyPair/) {
5bcc82
+					$tt = 14;
5bcc82
+					die "Interface function dsa_keygen for DSA key generation not defined for tested library"
5bcc82
+						if (!defined($gen_dsakey_domain));
5bcc82
+				} elsif ($tmpline =~ /KeyGen RSA \(X9\.31\)/) {
5bcc82
 					$tt = 13;
5bcc82
 					die "Interface function rsa_derive for RSA key generation not defined for tested library"
5bcc82
 						if (!defined($rsa_derive));
5bcc82
@@ -1760,11 +2105,11 @@ sub parse($$) {
5bcc82
 				} elsif ($tmpline =~ /SigGen/ && $opt{'D'}) {
5bcc82
 					$tt = 11;
5bcc82
 					die "Interface function dsa_sign or gen_dsakey for DSA sign not defined for tested library"
5bcc82
-						if (!defined($dsa_sign) || !defined($gen_rsakey));
5bcc82
+						if (!defined($dsa_sign) || !defined($gen_dsakey));
5bcc82
 				} elsif ($tmpline =~ /PQGGen/) {
5bcc82
 					$tt = 10;
5bcc82
 					die "Interface function for DSA PQGGen testing not defined for tested library"
5bcc82
-						if (!defined($dsa_pqggen));
5bcc82
+						if (!defined($dsa_pqggen) || !defined($dsa_ggen));
5bcc82
 				} elsif ($tmpline =~ /Hash sizes tested/) {
5bcc82
 					$tt = 9;
5bcc82
 					die "Interface function hmac for HMAC testing not defined for tested library"
5bcc82
@@ -1792,7 +2137,7 @@ sub parse($$) {
5bcc82
 				} elsif ($tmpline =~ /Monte|MCT|Carlo/) {
5bcc82
 					$tt = 2;
5bcc82
 					die "Interface function state_cipher for Stateful Cipher operation defined for tested library"
5bcc82
-						if (!defined($state_cipher) || !defined($state_cipher_des));
5bcc82
+						if (!defined($state_cipher) && !defined($state_cipher_des));
5bcc82
 				} elsif ($cipher =~ /^sha/) {
5bcc82
 					$tt = 3;
5bcc82
 					die "Interface function hash for Hashing not defined for tested library"
5bcc82
@@ -1875,18 +2220,44 @@ sub parse($$) {
5bcc82
 			die "Msg/Seed seen twice - input file crap" if ($pt ne "");
5bcc82
 			$pt=$2;
5bcc82
 		}
5bcc82
-		elsif ($line =~ /^\[mod\s*=\s*(.*)\]$/) { # found in RSA requests
5bcc82
+		elsif ($line =~ /^\[A.2.1\s.*\]$/) { # found in DSA2 PQGGen request
5bcc82
+			$out .= $line . "\n"; # print it
5bcc82
+			if ($tt == 10) {
5bcc82
+				# now generate G from PQ
5bcc82
+				$tt = 15;
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($line =~ /^\[A.2.2\s.*\]$/) { # found in DSA2 PQGVer request
5bcc82
+			$out .= $line . "\n"; # print it
5bcc82
+			if ($tt == 16) {
5bcc82
+				# now verify PQG
5bcc82
+				$tt = 17;
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($line =~ /^\[mod\s*=\s*L=([0-9]*),\s*N=([0-9]*).*\]$/) { # found in DSA2 requests
5bcc82
 			$modulus = $1;
5bcc82
+			$qsize = $2;
5bcc82
 			$out .= $line . "\n\n"; # print it
5bcc82
+			# clear eventual PQG
5bcc82
+			$capital_p = "";
5bcc82
+			$capital_q = "";
5bcc82
+			$capital_g = "";
5bcc82
 			# generate the private key with given bit length now
5bcc82
 			# as we have the required key length in bit
5bcc82
 			if ($tt == 11) {
5bcc82
 				$dsa_keyfile = "dsa_siggen.tmp.$$";
5bcc82
-				my %pqg = &$gen_dsakey($dsa_keyfile);
5bcc82
+				my %pqg = &$gen_dsakey($modulus, $qsize, $dsa_keyfile);
5bcc82
 				$out .= "P = " . $pqg{'P'} . "\n";
5bcc82
 				$out .= "Q = " . $pqg{'Q'} . "\n";
5bcc82
-				$out .= "G = " . $pqg{'G'} . "\n";
5bcc82
-			} elsif ( $tt == 5 ) {
5bcc82
+				$out .= "G = " . $pqg{'G'} . "\n\n";
5bcc82
+                        }
5bcc82
+                }
5bcc82
+		elsif ($line =~ /^\[mod\s*=\s*(.*)\]$/) { # found in RSA requests
5bcc82
+			$modulus = $1;
5bcc82
+			$out .= $line . "\n\n"; # print it
5bcc82
+			# generate the private key with given bit length now
5bcc82
+			# as we have the required key length in bit
5bcc82
+			if ( $tt == 5 ) {
5bcc82
 				# XXX maybe a secure temp file name is better here
5bcc82
 				# but since it is not run on a security sensitive
5bcc82
 				# system, I hope that this is fine
5bcc82
@@ -1907,6 +2278,9 @@ sub parse($$) {
5bcc82
 		}
5bcc82
 		elsif ($line =~ /^e\s*=\s*(.*)/) { # found in RSA requests
5bcc82
 			$e=$1;
5bcc82
+			if ($tt == 19) {
5bcc82
+				$out .= $line . "\n"; # print it
5bcc82
+			}
5bcc82
 		}
5bcc82
 		elsif ($line =~ /^S\s*=\s*(.*)/) { # found in RSA requests
5bcc82
 			die "S seen twice - input file crap" if ($signature ne "");
5bcc82
@@ -1932,11 +2306,16 @@ sub parse($$) {
5bcc82
 				if ($tlen ne "");
5bcc82
 			$tlen=$1;
5bcc82
 		}
5bcc82
-		elsif ($line =~ /^N\s*=\s*(.*)/) { #DSA PQGGen
5bcc82
+		elsif ($line =~ /^N\s*=\s*(.*)/) { #DSA KeyPair
5bcc82
 			die "N seen twice - check input file"
5bcc82
 				if ($capital_n);
5bcc82
 			$capital_n = $1;
5bcc82
 		}
5bcc82
+		elsif ($line =~ /^Num\s*=\s*(.*)/) { #DSA PQGGen
5bcc82
+			die "Num seen twice - check input file"
5bcc82
+				if ($num);
5bcc82
+			$num = $1;
5bcc82
+		}
5bcc82
 		elsif ($line =~ /^P\s*=\s*(.*)/) { #DSA SigVer
5bcc82
 			die "P seen twice - check input file"
5bcc82
 				if ($capital_p);
5bcc82
@@ -1965,6 +2344,16 @@ sub parse($$) {
5bcc82
 				if ($capital_r);
5bcc82
 			$capital_r = $1;
5bcc82
 		}
5bcc82
+		elsif ($line =~ /^H\s*=\s*(.*)/) { #DSA PQGVer
5bcc82
+			die "H seen twice - check input file"
5bcc82
+				if ($capital_h);
5bcc82
+			$capital_h = $1;
5bcc82
+		}
5bcc82
+		elsif ($line =~ /^c\s*=\s*(.*)/) { #DSA PQGVer
5bcc82
+			die "c seen twice - check input file"
5bcc82
+				if ($c);
5bcc82
+			$c = $1;
5bcc82
+		}
5bcc82
 		elsif ($line =~ /^xp1\s*=\s*(.*)/) { #RSA key gen
5bcc82
 			die "xp1 seen twice - check input file"
5bcc82
 				if ($xp1);
5bcc82
@@ -1995,6 +2384,22 @@ sub parse($$) {
5bcc82
 				if ($Xq);
5bcc82
 			$Xq = $1;
5bcc82
 		}
5bcc82
+		elsif ($line =~ /^prandom\s*=\s*(.*)/) { #RSA key gen KAT
5bcc82
+			die "prandom seen twice - check input file"
5bcc82
+				if ($prandom);
5bcc82
+			$prandom = $1;
5bcc82
+			$out .= $line . "\n"; # print it
5bcc82
+		}
5bcc82
+		elsif ($line =~ /^qrandom\s*=\s*(.*)/) { #RSA key gen KAT
5bcc82
+			die "qrandom seen twice - check input file"
5bcc82
+				if ($qrandom);
5bcc82
+			$qrandom = $1;
5bcc82
+			$out .= $line . "\n"; # print it
5bcc82
+		}
5bcc82
+		elsif ($tt == 19 && $line =~ /^ / && $qrandom eq "") { #RSA key gen KAT
5bcc82
+			$qrandom = "00";
5bcc82
+			$out .= $line . "\n"; # print it
5bcc82
+		}
5bcc82
 		else {
5bcc82
 			$out .= $line . "\n";
5bcc82
 		}
5bcc82
@@ -2074,11 +2479,10 @@ sub parse($$) {
5bcc82
 			}
5bcc82
 		}
5bcc82
 		elsif ($tt == 10) {
5bcc82
-			if ($modulus ne "" && $capital_n > 0) {
5bcc82
-				$out .= dsa_pqggen_driver($modulus, $capital_n);
5bcc82
-				#$mod is not resetted
5bcc82
-				$capital_n = 0;
5bcc82
-			}
5bcc82
+			if ($modulus ne "" && $qsize ne "" && $num > 0) {
5bcc82
+				$out .= dsa_pqgen_driver($modulus, $qsize, $num);
5bcc82
+				$num = 0;
5bcc82
+                        }
5bcc82
 		}
5bcc82
 		elsif ($tt == 11) {
5bcc82
 			if ($pt ne "" && $dsa_keyfile ne "") {
5bcc82
@@ -2124,7 +2528,7 @@ sub parse($$) {
5bcc82
 			   $xq1 ne "" &&
5bcc82
 			   $xq2 ne "" &&
5bcc82
 			   $Xq ne "") {
5bcc82
-				$out .= rsa_keygen($modulus,
5bcc82
+				$out .= rsa_keygen_x931($modulus,
5bcc82
 						   $e,
5bcc82
 						   $xp1,
5bcc82
 						   $xp2,
5bcc82
@@ -2141,6 +2545,96 @@ sub parse($$) {
5bcc82
 				$Xq = "";
5bcc82
 			}
5bcc82
 		}
5bcc82
+		elsif ($tt == 14) {
5bcc82
+			if ($modulus ne "" &&
5bcc82
+			    $qsize ne "" &&
5bcc82
+			    $capital_n > 0) {
5bcc82
+				$out .= dsa_keypair_driver($modulus,
5bcc82
+							   $qsize,
5bcc82
+							   $capital_n);
5bcc82
+				$capital_n = 0;
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($tt == 15) {
5bcc82
+			if ($modulus ne "" &&
5bcc82
+			    $qsize ne "" &&
5bcc82
+			    $capital_p ne "" &&
5bcc82
+			    $capital_q ne "") {
5bcc82
+				$out .= dsa_ggen_driver($modulus,
5bcc82
+							$qsize,
5bcc82
+							$capital_p,
5bcc82
+							$capital_q);
5bcc82
+				$capital_p = "";
5bcc82
+				$capital_q = "";
5bcc82
+				$num--;
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($tt == 16) {
5bcc82
+			if ($modulus ne "" &&
5bcc82
+			    $qsize ne "" &&
5bcc82
+			    $capital_p ne "" &&
5bcc82
+			    $capital_q ne "" &&
5bcc82
+			    $pt ne "" &&
5bcc82
+			    $c ne "") {
5bcc82
+				$out .= dsa_pqver_driver($modulus,
5bcc82
+							$qsize,
5bcc82
+							$capital_p,
5bcc82
+							$capital_q,
5bcc82
+							$pt,
5bcc82
+							$c);
5bcc82
+				$capital_p = "";
5bcc82
+				$capital_q = "";
5bcc82
+				$pt = "";
5bcc82
+				$c = "";
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($tt == 17) {
5bcc82
+			if ($modulus ne "" &&
5bcc82
+			    $qsize ne "" &&
5bcc82
+			    $capital_p ne "" &&
5bcc82
+			    $capital_q ne "" &&
5bcc82
+			    $capital_g ne "" &&
5bcc82
+			    $pt ne "" &&
5bcc82
+			    $c ne "" &&
5bcc82
+			    $capital_h ne "") {
5bcc82
+				$out .= dsa_pqgver_driver($modulus,
5bcc82
+							$qsize,
5bcc82
+							$capital_p,
5bcc82
+							$capital_q,
5bcc82
+							$capital_g,
5bcc82
+							$pt,
5bcc82
+							$c,
5bcc82
+							$capital_h);
5bcc82
+				$capital_p = "";
5bcc82
+				$capital_q = "";
5bcc82
+				$capital_g = "";
5bcc82
+				$pt = "";
5bcc82
+				$c = "";
5bcc82
+				$capital_h = "";
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($tt == 18) {
5bcc82
+			if ($modulus ne "" &&
5bcc82
+			    $capital_n > 0) {
5bcc82
+				$out .= rsa_keygen_driver($modulus,
5bcc82
+						$capital_n);
5bcc82
+				$capital_n = 0;
5bcc82
+			}
5bcc82
+		}
5bcc82
+		elsif ($tt == 19) {
5bcc82
+			if ($modulus ne "" &&
5bcc82
+			    $e ne "" &&
5bcc82
+			    $prandom ne "" &&
5bcc82
+			    $qrandom ne "") {
5bcc82
+				$out .= rsa_keygen_kat_driver($modulus,
5bcc82
+						$e,
5bcc82
+						$prandom,
5bcc82
+						$qrandom);
5bcc82
+				$prandom = "";
5bcc82
+				$qrandom = "";
5bcc82
+				$e = "";
5bcc82
+			}
5bcc82
+		}
5bcc82
 		elsif ($tt > 0) {
5bcc82
 			die "Test case $tt not defined";
5bcc82
 		}
5bcc82
@@ -2199,10 +2693,14 @@ sub main() {
5bcc82
 		$state_rng =	\&libgcrypt_state_rng;
5bcc82
 		$hmac =		\&libgcrypt_hmac;
5bcc82
 		$dsa_pqggen = 	\&libgcrypt_dsa_pqggen;
5bcc82
+		$dsa_ggen =	\&libgcrypt_dsa_ggen;
5bcc82
 		$gen_dsakey =   \&libgcrypt_gen_dsakey;
5bcc82
+		$gen_dsakey_domain =	\&libgcrypt_gen_dsakey_domain;
5bcc82
 		$dsa_sign =     \&libgcrypt_dsa_sign;
5bcc82
 		$dsa_verify =   \&libgcrypt_dsa_verify;
5bcc82
 		$dsa_genpubkey = \&libgcrypt_dsa_genpubkey;
5bcc82
+		$rsa_keygen = \&libgcrypt_rsa_keygen;
5bcc82
+		$rsa_keygen_kat = \&libgcrypt_rsa_keygen_kat;
5bcc82
         } else {
5bcc82
                 die "Invalid interface option given";
5bcc82
         }
5bcc82
diff -up libgcrypt-1.7.3/tests/cavs_tests.sh.cavs libgcrypt-1.7.3/tests/cavs_tests.sh
5bcc82
--- libgcrypt-1.7.3/tests/cavs_tests.sh.cavs	2013-03-15 20:25:38.000000000 +0100
5bcc82
+++ libgcrypt-1.7.3/tests/cavs_tests.sh	2016-11-22 17:29:06.067553077 +0100
5bcc82
@@ -55,7 +55,7 @@ function run_one_test () {
5bcc82
     [ -d "$respdir" ] || mkdir "$respdir"
5bcc82
     [ -f "$rspfile" ] &&  rm "$rspfile"
5bcc82
 
5bcc82
-    if echo "$reqfile" | grep '/DSA/req/' >/dev/null 2>/dev/null; then
5bcc82
+    if echo "$reqfile" | grep '/DSA.\?/req/' >/dev/null 2>/dev/null; then
5bcc82
         dflag="-D"
5bcc82
     fi
5bcc82
 
5bcc82
diff -up libgcrypt-1.7.3/tests/fipsdrv.c.cavs libgcrypt-1.7.3/tests/fipsdrv.c
5bcc82
--- libgcrypt-1.7.3/tests/fipsdrv.c.cavs	2016-07-14 11:19:17.000000000 +0200
5bcc82
+++ libgcrypt-1.7.3/tests/fipsdrv.c	2016-11-22 17:33:15.468330859 +0100
5bcc82
@@ -892,6 +892,9 @@ print_mpi_line (gcry_mpi_t a, int no_lz)
5bcc82
     die ("gcry_mpi_aprint failed: %s\n", gpg_strerror (err));
5bcc82
 
5bcc82
   p = buf;
5bcc82
+  while (*p)
5bcc82
+     *p++ = tolower(*p);
5bcc82
+  p = buf;
5bcc82
   if (no_lz && p[0] == '0' && p[1] == '0' && p[2])
5bcc82
     p += 2;
5bcc82
 
5bcc82
@@ -1765,14 +1768,14 @@ run_rsa_verify (const void *data, size_t
5bcc82
 /* Generate a DSA key of size KEYSIZE and return the complete
5bcc82
    S-expression.  */
5bcc82
 static gcry_sexp_t
5bcc82
-dsa_gen (int keysize)
5bcc82
+dsa_gen (int keysize, int qsize)
5bcc82
 {
5bcc82
   gpg_error_t err;
5bcc82
   gcry_sexp_t keyspec, key;
5bcc82
 
5bcc82
   err = gcry_sexp_build (&keyspec, NULL,
5bcc82
-                         "(genkey (dsa (nbits %d)(use-fips186-2)))",
5bcc82
-                         keysize);
5bcc82
+                         "(genkey (dsa (nbits %d)(qbits %d)(use-fips186)))",
5bcc82
+                         keysize, qsize);
5bcc82
   if (err)
5bcc82
     die ("gcry_sexp_build failed for DSA key generation: %s\n",
5bcc82
          gpg_strerror (err));
5bcc82
@@ -1790,7 +1793,7 @@ dsa_gen (int keysize)
5bcc82
 /* Generate a DSA key of size KEYSIZE and return the complete
5bcc82
    S-expression.  */
5bcc82
 static gcry_sexp_t
5bcc82
-dsa_gen_with_seed (int keysize, const void *seed, size_t seedlen)
5bcc82
+dsa_gen_with_seed (int keysize, int qsize, const void *seed, size_t seedlen)
5bcc82
 {
5bcc82
   gpg_error_t err;
5bcc82
   gcry_sexp_t keyspec, key;
5bcc82
@@ -1799,10 +1802,11 @@ dsa_gen_with_seed (int keysize, const vo
5bcc82
                          "(genkey"
5bcc82
                          "  (dsa"
5bcc82
                          "    (nbits %d)"
5bcc82
-                         "    (use-fips186-2)"
5bcc82
+                         "    (qbits %d)"
5bcc82
+                         "    (use-fips186)"
5bcc82
                          "    (derive-parms"
5bcc82
                          "      (seed %b))))",
5bcc82
-                         keysize, (int)seedlen, seed);
5bcc82
+                         keysize, qsize, (int)seedlen, seed);
5bcc82
   if (err)
5bcc82
     die ("gcry_sexp_build failed for DSA key generation: %s\n",
5bcc82
          gpg_strerror (err));
5bcc82
@@ -1810,6 +1814,37 @@ dsa_gen_with_seed (int keysize, const vo
5bcc82
   err = gcry_pk_genkey (&key, keyspec);
5bcc82
   if (err)
5bcc82
     die ("gcry_pk_genkey failed for DSA: %s\n", gpg_strerror (err));
5bcc82
+  
5bcc82
+  gcry_sexp_release (keyspec);
5bcc82
+
5bcc82
+  return key;
5bcc82
+}
5bcc82
+
5bcc82
+/* Generate a DSA key with specified domain parameters and return the complete
5bcc82
+   S-expression.  */
5bcc82
+static gcry_sexp_t
5bcc82
+dsa_gen_key (const char *domain)
5bcc82
+{
5bcc82
+  gpg_error_t err;
5bcc82
+  gcry_sexp_t keyspec, key, domspec;
5bcc82
+
5bcc82
+  err = gcry_sexp_new (&domspec, domain, strlen(domain), 0);
5bcc82
+  if (err)
5bcc82
+    die ("gcry_sexp_build failed for domain spec: %s\n",
5bcc82
+         gpg_strerror (err));
5bcc82
+
5bcc82
+  err = gcry_sexp_build (&keyspec, NULL,
5bcc82
+                         "(genkey"
5bcc82
+                         "  (dsa"
5bcc82
+                         "    (use-fips186)"
5bcc82
+                         "    %S))",
5bcc82
+                         domspec);
5bcc82
+  if (err)
5bcc82
+    die ("gcry_sexp_build failed for DSA key generation: %s\n",
5bcc82
+         gpg_strerror (err));
5bcc82
+  err = gcry_pk_genkey (&key, keyspec);
5bcc82
+  if (err)
5bcc82
+    die ("gcry_pk_genkey failed for DSA: %s\n", gpg_strerror (err));
5bcc82
 
5bcc82
   gcry_sexp_release (keyspec);
5bcc82
 
5bcc82
@@ -1849,7 +1884,7 @@ ecdsa_gen_key (const char *curve)
5bcc82
    with one parameter per line in hex format using this order: p, q,
5bcc82
    g, seed, counter, h. */
5bcc82
 static void
5bcc82
-print_dsa_domain_parameters (gcry_sexp_t key)
5bcc82
+print_dsa_domain_parameters (gcry_sexp_t key, int print_misc)
5bcc82
 {
5bcc82
   gcry_sexp_t l1, l2;
5bcc82
   gcry_mpi_t mpi;
5bcc82
@@ -1885,6 +1920,9 @@ print_dsa_domain_parameters (gcry_sexp_t
5bcc82
     }
5bcc82
   gcry_sexp_release (l1);
5bcc82
 
5bcc82
+  if (!print_misc)
5bcc82
+	return;
5bcc82
+
5bcc82
   /* Extract the seed values.  */
5bcc82
   l1 = gcry_sexp_find_token (key, "misc-key-info", 0);
5bcc82
   if (!l1)
5bcc82
@@ -1976,38 +2014,106 @@ print_ecdsa_dq (gcry_sexp_t key)
5bcc82
 }
5bcc82
 
5bcc82
 
5bcc82
-/* Generate DSA domain parameters for a modulus size of KEYSIZE.  The
5bcc82
+/* Print just the XY private key parameters.  KEY
5bcc82
+   is the complete key as returned by dsa_gen.  We print to stdout
5bcc82
+   with one parameter per line in hex format using this order: x, y. */
5bcc82
+static void 
5bcc82
+print_dsa_xy (gcry_sexp_t key)
5bcc82
+{
5bcc82
+  gcry_sexp_t l1, l2;
5bcc82
+  gcry_mpi_t mpi;
5bcc82
+  int idx;
5bcc82
+
5bcc82
+  l1 = gcry_sexp_find_token (key, "private-key", 0);
5bcc82
+  if (!l1)
5bcc82
+    die ("private key not found in genkey result\n");
5bcc82
+
5bcc82
+  l2 = gcry_sexp_find_token (l1, "dsa", 0);
5bcc82
+  if (!l2)
5bcc82
+    die ("returned private key not formed as expected\n");
5bcc82
+  gcry_sexp_release (l1);
5bcc82
+  l1 = l2;
5bcc82
+
5bcc82
+  /* Extract the parameters from the S-expression and print them to stdout.  */
5bcc82
+  for (idx=0; "xy"[idx]; idx++) 
5bcc82
+    {
5bcc82
+      l2 = gcry_sexp_find_token (l1, "xy"+idx, 1);
5bcc82
+      if (!l2)
5bcc82
+        die ("no %c parameter in returned public key\n", "xy"[idx]);
5bcc82
+      mpi = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
5bcc82
+      if (!mpi)
5bcc82
+        die ("no value for %c parameter in returned private key\n","xy"[idx]);
5bcc82
+      gcry_sexp_release (l2);
5bcc82
+      if (standalone_mode)
5bcc82
+        printf ("%c = ", "XY"[idx]);
5bcc82
+      print_mpi_line (mpi, 1);
5bcc82
+      gcry_mpi_release (mpi);
5bcc82
+    }
5bcc82
+
5bcc82
+  gcry_sexp_release (l1);
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+/* Generate DSA pq domain parameters for a modulus size of KEYSIZE.  The
5bcc82
    result is printed to stdout with one parameter per line in hex
5bcc82
-   format and in this order: p, q, g, seed, counter, h.  If SEED is
5bcc82
+   format and in this order: p, q, seed, counter.  If SEED is
5bcc82
    not NULL this seed value will be used for the generation.  */
5bcc82
 static void
5bcc82
-run_dsa_pqg_gen (int keysize, const void *seed, size_t seedlen)
5bcc82
+run_dsa_pqg_gen (int keysize, int qsize, const void *seed, size_t seedlen)
5bcc82
 {
5bcc82
   gcry_sexp_t key;
5bcc82
 
5bcc82
   if (seed)
5bcc82
-    key = dsa_gen_with_seed (keysize, seed, seedlen);
5bcc82
+    key = dsa_gen_with_seed (keysize, qsize, seed, seedlen);
5bcc82
   else
5bcc82
-    key = dsa_gen (keysize);
5bcc82
-  print_dsa_domain_parameters (key);
5bcc82
+    key = dsa_gen (keysize, qsize);
5bcc82
+  print_dsa_domain_parameters (key, 1);
5bcc82
+  gcry_sexp_release (key);
5bcc82
+}
5bcc82
+
5bcc82
+
5bcc82
+/* Generate DSA domain parameters for a modulus size of KEYSIZE.  The
5bcc82
+   result is printed to stdout with one parameter per line in hex
5bcc82
+   format and in this order: p, q, g, seed, counter, h.  If SEED is
5bcc82
+   not NULL this seed value will be used for the generation.  */
5bcc82
+static void
5bcc82
+run_dsa_g_gen (int keysize, int qsize, const char *domain)
5bcc82
+{
5bcc82
+  gcry_sexp_t key;
5bcc82
+
5bcc82
+  key = dsa_gen_key (domain);
5bcc82
+  print_dsa_domain_parameters (key, 0);
5bcc82
+  gcry_sexp_release (key);
5bcc82
+}
5bcc82
+
5bcc82
+/* Generate a DSA key with specified domain parameters
5bcc82
+   and print the XY values. */
5bcc82
+static void
5bcc82
+run_dsa_gen_key (const char *domain)
5bcc82
+{
5bcc82
+  gcry_sexp_t key;
5bcc82
+
5bcc82
+  key = dsa_gen_key (domain);
5bcc82
+  print_dsa_xy (key);
5bcc82
+
5bcc82
   gcry_sexp_release (key);
5bcc82
 }
5bcc82
 
5bcc82
 
5bcc82
 /* Generate a DSA key of size of KEYSIZE and write the private key to
5bcc82
    FILENAME.  Also write the parameters to stdout in the same way as
5bcc82
-   run_dsa_pqg_gen.  */
5bcc82
+   run_dsa_g_gen.  */
5bcc82
 static void
5bcc82
-run_dsa_gen (int keysize, const char *filename)
5bcc82
+run_dsa_gen (int keysize, int qsize, const char *filename)
5bcc82
 {
5bcc82
   gcry_sexp_t key, private_key;
5bcc82
   FILE *fp;
5bcc82
 
5bcc82
-  key = dsa_gen (keysize);
5bcc82
+  key = dsa_gen (keysize, qsize);
5bcc82
   private_key = gcry_sexp_find_token (key, "private-key", 0);
5bcc82
   if (!private_key)
5bcc82
     die ("private key not found in genkey result\n");
5bcc82
-  print_dsa_domain_parameters (key);
5bcc82
+  print_dsa_domain_parameters (key, 1);
5bcc82
 
5bcc82
   fp = fopen (filename, "wb");
5bcc82
   if (!fp)
5bcc82
@@ -2020,6 +2126,53 @@ run_dsa_gen (int keysize, const char *fi
5bcc82
 }
5bcc82
 
5bcc82
 
5bcc82
+static int
5bcc82
+dsa_hash_from_key(gcry_sexp_t s_key)
5bcc82
+{
5bcc82
+  gcry_sexp_t l1, l2;
5bcc82
+  gcry_mpi_t q;
5bcc82
+  unsigned int qbits;
5bcc82
+
5bcc82
+  l1 = gcry_sexp_find_token (s_key, "public-key", 0);
5bcc82
+  if (!l1)
5bcc82
+    {
5bcc82
+      l1 = gcry_sexp_find_token (s_key, "private-key", 0);
5bcc82
+      if (!l1)
5bcc82
+        die ("neither private nor public key found in the loaded key\n");
5bcc82
+    }
5bcc82
+
5bcc82
+  l2 = gcry_sexp_find_token (l1, "dsa", 0);
5bcc82
+  if (!l2)
5bcc82
+    die ("public key not formed as expected - no dsa\n");
5bcc82
+  gcry_sexp_release (l1);
5bcc82
+  l1 = l2;
5bcc82
+
5bcc82
+  l2 = gcry_sexp_find_token (l1, "q", 0);
5bcc82
+  if (!l2)
5bcc82
+    die ("public key not formed as expected - no q\n");
5bcc82
+  gcry_sexp_release (l1);
5bcc82
+  l1 = l2;
5bcc82
+
5bcc82
+  q = gcry_sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG);
5bcc82
+  if (!q)
5bcc82
+    die ("public key not formed as expected - no mpi in q\n");
5bcc82
+  qbits = gcry_mpi_get_nbits(q);
5bcc82
+  gcry_sexp_release(l1);
5bcc82
+  gcry_mpi_release(q);
5bcc82
+  switch(qbits)
5bcc82
+    {
5bcc82
+    case 160:
5bcc82
+	return GCRY_MD_SHA1;
5bcc82
+    case 224:
5bcc82
+	return GCRY_MD_SHA224;
5bcc82
+    case 256:
5bcc82
+        return GCRY_MD_SHA256;
5bcc82
+    default:
5bcc82
+        die("bad number bits (%d) of q in key\n", qbits);
5bcc82
+    }
5bcc82
+  return GCRY_MD_NONE;
5bcc82
+}
5bcc82
+
5bcc82
 
5bcc82
 /* Sign DATA of length DATALEN using the key taken from the S-expression
5bcc82
    encoded KEYFILE. */
5bcc82
@@ -2029,11 +2182,16 @@ run_dsa_sign (const void *data, size_t d
5bcc82
 {
5bcc82
   gpg_error_t err;
5bcc82
   gcry_sexp_t s_data, s_key, s_sig, s_tmp, s_tmp2;
5bcc82
-  char hash[20];
5bcc82
+  char hash[128];
5bcc82
   gcry_mpi_t tmpmpi;
5bcc82
+  int algo;
5bcc82
+
5bcc82
+  s_key = read_sexp_from_file (keyfile);
5bcc82
+  algo = dsa_hash_from_key(s_key);
5bcc82
 
5bcc82
-  gcry_md_hash_buffer (GCRY_MD_SHA1, hash, data, datalen);
5bcc82
-  err = gcry_mpi_scan (&tmpmpi, GCRYMPI_FMT_USG, hash, 20, NULL);
5bcc82
+  gcry_md_hash_buffer (algo, hash, data, datalen);
5bcc82
+  err = gcry_mpi_scan (&tmpmpi, GCRYMPI_FMT_USG, hash,
5bcc82
+                       gcry_md_get_algo_dlen(algo), NULL);
5bcc82
   if (!err)
5bcc82
     {
5bcc82
       err = gcry_sexp_build (&s_data, NULL,
5bcc82
@@ -2044,8 +2202,6 @@ run_dsa_sign (const void *data, size_t d
5bcc82
     die ("gcry_sexp_build failed for DSA data input: %s\n",
5bcc82
          gpg_strerror (err));
5bcc82
 
5bcc82
-  s_key = read_sexp_from_file (keyfile);
5bcc82
-
5bcc82
   err = gcry_pk_sign (&s_sig, s_data, s_key);
5bcc82
   if (err)
5bcc82
     {
5bcc82
@@ -2121,13 +2277,18 @@ run_dsa_verify (const void *data, size_t
5bcc82
 {
5bcc82
   gpg_error_t err;
5bcc82
   gcry_sexp_t s_data, s_key, s_sig;
5bcc82
-  char hash[20];
5bcc82
+  char hash[128];
5bcc82
   gcry_mpi_t tmpmpi;
5bcc82
+  int algo;
5bcc82
 
5bcc82
-  gcry_md_hash_buffer (GCRY_MD_SHA1, hash, data, datalen);
5bcc82
+  s_key = read_sexp_from_file (keyfile);
5bcc82
+  algo = dsa_hash_from_key(s_key);
5bcc82
+
5bcc82
+  gcry_md_hash_buffer (algo, hash, data, datalen);
5bcc82
   /* Note that we can't simply use %b with HASH to build the
5bcc82
      S-expression, because that might yield a negative value.  */
5bcc82
-  err = gcry_mpi_scan (&tmpmpi, GCRYMPI_FMT_USG, hash, 20, NULL);
5bcc82
+  err = gcry_mpi_scan (&tmpmpi, GCRYMPI_FMT_USG, hash,
5bcc82
+                       gcry_md_get_algo_dlen(algo), NULL);
5bcc82
   if (!err)
5bcc82
     {
5bcc82
       err = gcry_sexp_build (&s_data, NULL,
5bcc82
@@ -2138,7 +2299,6 @@ run_dsa_verify (const void *data, size_t
5bcc82
     die ("gcry_sexp_build failed for DSA data input: %s\n",
5bcc82
          gpg_strerror (err));
5bcc82
 
5bcc82
-  s_key = read_sexp_from_file (keyfile);
5bcc82
   s_sig = read_sexp_from_file (sigfile);
5bcc82
 
5bcc82
   err = gcry_pk_verify (s_sig, s_data, s_key);
5bcc82
@@ -2304,7 +2464,7 @@ usage (int show_help)
5bcc82
      "MODE:\n"
5bcc82
      "  encrypt, decrypt, digest, random, hmac-sha,\n"
5bcc82
      "  rsa-{derive,gen,sign,verify},\n"
5bcc82
-     "  dsa-{pqg-gen,gen,sign,verify}, ecdsa-{gen-key,sign,verify}\n"
5bcc82
+     "  dsa-{pq-gen,g-gen,gen,sign,verify}, ecdsa-{gen-key,sign,verify}\n"
5bcc82
      "OPTIONS:\n"
5bcc82
      "  --verbose        Print additional information\n"
5bcc82
      "  --binary         Input and output is in binary form\n"
5bcc82
@@ -2315,6 +2475,7 @@ usage (int show_help)
5bcc82
      "  --algo NAME      Use algorithm NAME\n"
5bcc82
      "  --curve NAME     Select ECC curve spec NAME\n"
5bcc82
      "  --keysize N      Use a keysize of N bits\n"
5bcc82
+     "  --qize N         Use a DSA q parameter size of N bits\n"
5bcc82
      "  --signature NAME Take signature from file NAME\n"
5bcc82
      "  --chunk N        Read in chunks of N bytes (implies --binary)\n"
5bcc82
      "  --pkcs1          Use PKCS#1 encoding\n"
5bcc82
@@ -2344,6 +2505,7 @@ main (int argc, char **argv)
5bcc82
   const char *dt_string = NULL;
5bcc82
   const char *algo_string = NULL;
5bcc82
   const char *keysize_string = NULL;
5bcc82
+  const char *qsize_string = NULL;
5bcc82
   const char *signature_string = NULL;
5bcc82
   FILE *input;
5bcc82
   void *data;
5bcc82
@@ -2437,6 +2599,14 @@ main (int argc, char **argv)
5bcc82
           keysize_string = *argv;
5bcc82
           argc--; argv++;
5bcc82
         }
5bcc82
+      else if (!strcmp (*argv, "--qsize"))
5bcc82
+        {
5bcc82
+          argc--; argv++;
5bcc82
+          if (!argc)
5bcc82
+            usage (0);
5bcc82
+          qsize_string = *argv;
5bcc82
+          argc--; argv++;
5bcc82
+        }
5bcc82
       else if (!strcmp (*argv, "--signature"))
5bcc82
         {
5bcc82
           argc--; argv++;
5bcc82
@@ -2792,23 +2962,49 @@ main (int argc, char **argv)
5bcc82
     }
5bcc82
   else if (!strcmp (mode_string, "dsa-pqg-gen"))
5bcc82
     {
5bcc82
-      int keysize;
5bcc82
+      int keysize, qsize;
5bcc82
+
5bcc82
+      keysize = keysize_string? atoi (keysize_string) : 0;
5bcc82
+      if (keysize < 1024 || keysize > 3072)
5bcc82
+        die ("invalid keysize specified; needs to be 1024 .. 3072\n");
5bcc82
+      qsize = qsize_string? atoi (qsize_string) : 0;
5bcc82
+      if (qsize < 160 || qsize > 256)
5bcc82
+        die ("invalid qsize specified; needs to be 160 .. 256\n");
5bcc82
+      run_dsa_pqg_gen (keysize, qsize, datalen? data:NULL, datalen);
5bcc82
+    }
5bcc82
+  else if (!strcmp (mode_string, "dsa-g-gen"))
5bcc82
+    {
5bcc82
+      int keysize, qsize;
5bcc82
 
5bcc82
       keysize = keysize_string? atoi (keysize_string) : 0;
5bcc82
       if (keysize < 1024 || keysize > 3072)
5bcc82
         die ("invalid keysize specified; needs to be 1024 .. 3072\n");
5bcc82
-      run_dsa_pqg_gen (keysize, datalen? data:NULL, datalen);
5bcc82
+      qsize = qsize_string? atoi (qsize_string) : 0;
5bcc82
+      if (qsize < 160 || qsize > 256)
5bcc82
+        die ("invalid qsize specified; needs to be 160 .. 256\n");
5bcc82
+      if (!key_string)
5bcc82
+        die ("option --key containing pq domain parameters is required in this mode\n");
5bcc82
+      run_dsa_g_gen (keysize, qsize, key_string);
5bcc82
+    }
5bcc82
+  else if (!strcmp (mode_string, "dsa-gen-key"))
5bcc82
+    {
5bcc82
+      if (!key_string)
5bcc82
+        die ("option --key containing pqg domain parameters is required in this mode\n");
5bcc82
+      run_dsa_gen_key (key_string);
5bcc82
     }
5bcc82
   else if (!strcmp (mode_string, "dsa-gen"))
5bcc82
     {
5bcc82
-      int keysize;
5bcc82
+      int keysize, qsize;
5bcc82
 
5bcc82
       keysize = keysize_string? atoi (keysize_string) : 0;
5bcc82
       if (keysize < 1024 || keysize > 3072)
5bcc82
         die ("invalid keysize specified; needs to be 1024 .. 3072\n");
5bcc82
+      qsize = qsize_string? atoi (qsize_string) : 0;
5bcc82
+      if (qsize < 160 || qsize > 256)
5bcc82
+        die ("invalid qsize specified; needs to be 160 .. 256\n");
5bcc82
       if (!key_string)
5bcc82
         die ("option --key is required in this mode\n");
5bcc82
-      run_dsa_gen (keysize, key_string);
5bcc82
+      run_dsa_gen (keysize, qsize, key_string);
5bcc82
     }
5bcc82
   else if (!strcmp (mode_string, "dsa-sign"))
5bcc82
     {