bmh10 / rpms / openssh

Forked from rpms/openssh 11 days ago
Clone
aedd00
diff -up openssh-6.8p1/Makefile.in.kdf-cavs openssh-6.8p1/Makefile.in
aedd00
--- openssh-6.8p1/Makefile.in.kdf-cavs	2015-03-18 11:23:46.346049359 +0100
aedd00
+++ openssh-6.8p1/Makefile.in	2015-03-18 11:24:20.395968445 +0100
aedd00
@@ -29,6 +29,7 @@ SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-h
aedd00
 SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
aedd00
 SSH_KEYCAT=$(libexecdir)/ssh-keycat
aedd00
 CTR_CAVSTEST=$(libexecdir)/ctr-cavstest
aedd00
+SSH_CAVS=$(libexecdir)/ssh-cavs
aedd00
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
aedd00
 PRIVSEP_PATH=@PRIVSEP_PATH@
aedd00
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
aedd00
@@ -67,7 +68,7 @@ EXEEXT=@EXEEXT@
aedd00
 MKDIR_P=@MKDIR_P@
aedd00
 INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
aedd00
 
aedd00
-TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-pkcs11-helper$(EXEEXT) ssh-agent$(EXEEXT) scp$(EXEEXT) sftp-server$(EXEEXT) sftp$(EXEEXT) ssh-ldap-helper$(EXEEXT) ssh-keycat$(EXEEXT) ctr-cavstest$(EXEEXT)
aedd00
+TARGETS=ssh$(EXEEXT) sshd$(EXEEXT) ssh-add$(EXEEXT) ssh-keygen$(EXEEXT) ssh-keyscan${EXEEXT} ssh-keysign${EXEEXT} ssh-pkcs11-helper$(EXEEXT) ssh-agent$(EXEEXT) scp$(EXEEXT) sftp-server$(EXEEXT) sftp$(EXEEXT) ssh-ldap-helper$(EXEEXT) ssh-keycat$(EXEEXT) ctr-cavstest$(EXEEXT) ssh-cavs$(EXEEXT)
aedd00
 
aedd00
 XMSS_OBJS=\
aedd00
 	ssh-xmss.o \
aedd00
@@ -198,6 +199,9 @@ ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHD
aedd00
 ctr-cavstest$(EXEEXT): $(LIBCOMPAT) libssh.a ctr-cavstest.o
aedd00
 	$(LD) -o $@ ctr-cavstest.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
aedd00
 
aedd00
+ssh-cavs$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-cavs.o
aedd00
+	$(LD) -o $@ ssh-cavs.o $(LDFLAGS) -lssh -lopenbsd-compat $(LIBS)
aedd00
+
aedd00
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o
aedd00
 	$(LD) -o $@ ssh-keyscan.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
aedd00
 
aedd00
@@ -331,6 +335,8 @@ install-files:
aedd00
 	fi
aedd00
 	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
aedd00
 	$(INSTALL) -m 0755 $(STRIP_OPT) ctr-cavstest$(EXEEXT) $(DESTDIR)$(libexecdir)/ctr-cavstest$(EXEEXT)
aedd00
+	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-cavs$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-cavs$(EXEEXT)
aedd00
+	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-cavs_driver.pl $(DESTDIR)$(libexecdir)/ssh-cavs_driver.pl
aedd00
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
aedd00
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
aedd00
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
aedd00
diff -up openssh-6.8p1/ssh-cavs.c.kdf-cavs openssh-6.8p1/ssh-cavs.c
aedd00
--- openssh-6.8p1/ssh-cavs.c.kdf-cavs	2015-03-18 11:23:46.348049354 +0100
aedd00
+++ openssh-6.8p1/ssh-cavs.c	2015-03-18 11:23:46.348049354 +0100
aedd00
@@ -0,0 +1,387 @@
aedd00
+/*
aedd00
+ * Copyright (C) 2015, Stephan Mueller <smueller@chronox.de>
aedd00
+ *
aedd00
+ * Redistribution and use in source and binary forms, with or without
aedd00
+ * modification, are permitted provided that the following conditions
aedd00
+ * are met:
aedd00
+ * 1. Redistributions of source code must retain the above copyright
aedd00
+ *    notice, and the entire permission notice in its entirety,
aedd00
+ *    including the disclaimer of warranties.
aedd00
+ * 2. Redistributions in binary form must reproduce the above copyright
aedd00
+ *    notice, this list of conditions and the following disclaimer in the
aedd00
+ *    documentation and/or other materials provided with the distribution.
aedd00
+ * 3. The name of the author may not be used to endorse or promote
aedd00
+ *    products derived from this software without specific prior
aedd00
+ *    written permission.
aedd00
+ *
aedd00
+ * ALTERNATIVELY, this product may be distributed under the terms of
aedd00
+ * the GNU General Public License, in which case the provisions of the GPL2
aedd00
+ * are required INSTEAD OF the above restrictions.  (This clause is
aedd00
+ * necessary due to a potential bad interaction between the GPL and
aedd00
+ * the restrictions contained in a BSD-style copyright.)
aedd00
+ *
aedd00
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
aedd00
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
aedd00
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
aedd00
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
aedd00
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
aedd00
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
aedd00
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
aedd00
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
aedd00
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
aedd00
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
aedd00
+ * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
aedd00
+ * DAMAGE.
aedd00
+ */
aedd00
+
aedd00
+#include "includes.h"
aedd00
+
aedd00
+#include <stdio.h>
aedd00
+#include <stdlib.h>
aedd00
+#include <errno.h>
aedd00
+#include <sys/types.h>
aedd00
+#include <string.h>
aedd00
+
aedd00
+#include <openssl/bn.h>
aedd00
+
aedd00
+#include "xmalloc.h"
aedd00
+#include "sshbuf.h"
aedd00
+#include "sshkey.h"
aedd00
+#include "cipher.h"
aedd00
+#include "kex.h"
aedd00
+#include "packet.h"
aedd00
+#include "digest.h"
aedd00
+
aedd00
+static int bin_char(unsigned char hex)
aedd00
+{
aedd00
+	if (48 <= hex && 57 >= hex)
aedd00
+		return (hex - 48);
aedd00
+	if (65 <= hex && 70 >= hex)
aedd00
+		return (hex - 55);
aedd00
+	if (97 <= hex && 102 >= hex)
aedd00
+		return (hex - 87);
aedd00
+	return 0;
aedd00
+}
aedd00
+
aedd00
+/*
aedd00
+ * Convert hex representation into binary string
aedd00
+ * @hex input buffer with hex representation
aedd00
+ * @hexlen length of hex
aedd00
+ * @bin output buffer with binary data
aedd00
+ * @binlen length of already allocated bin buffer (should be at least
aedd00
+ *	   half of hexlen -- if not, only a fraction of hexlen is converted)
aedd00
+ */
aedd00
+static void hex2bin(const char *hex, size_t hexlen,
aedd00
+		    unsigned char *bin, size_t binlen)
aedd00
+{
aedd00
+	size_t i = 0;
aedd00
+	size_t chars = (binlen > (hexlen / 2)) ? (hexlen / 2) : binlen;
aedd00
+
aedd00
+	for (i = 0; i < chars; i++) {
aedd00
+		bin[i] = bin_char(hex[(i*2)]) << 4;
aedd00
+		bin[i] |= bin_char(hex[((i*2)+1)]);
aedd00
+	}
aedd00
+}
aedd00
+
aedd00
+/*
aedd00
+ * Allocate sufficient space for binary representation of hex
aedd00
+ * and convert hex into bin
aedd00
+ *
aedd00
+ * Caller must free bin
aedd00
+ * @hex input buffer with hex representation
aedd00
+ * @hexlen length of hex
aedd00
+ * @bin return value holding the pointer to the newly allocated buffer
aedd00
+ * @binlen return value holding the allocated size of bin
aedd00
+ *
aedd00
+ * return: 0 on success, !0 otherwise
aedd00
+ */
aedd00
+static int hex2bin_alloc(const char *hex, size_t hexlen,
aedd00
+			 unsigned char **bin, size_t *binlen)
aedd00
+{
aedd00
+	unsigned char *out = NULL;
aedd00
+	size_t outlen = 0;
aedd00
+
aedd00
+	if (!hexlen)
aedd00
+		return -EINVAL;
aedd00
+
aedd00
+	outlen = (hexlen + 1) / 2;
aedd00
+
aedd00
+	out = calloc(1, outlen);
aedd00
+	if (!out)
aedd00
+		return -errno;
aedd00
+
aedd00
+	hex2bin(hex, hexlen, out, outlen);
aedd00
+	*bin = out;
aedd00
+	*binlen = outlen;
aedd00
+	return 0;
aedd00
+}
aedd00
+
aedd00
+static char hex_char_map_l[] = { '0', '1', '2', '3', '4', '5', '6', '7',
aedd00
+				 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
aedd00
+static char hex_char_map_u[] = { '0', '1', '2', '3', '4', '5', '6', '7',
aedd00
+				 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
aedd00
+static char hex_char(unsigned int bin, int u)
aedd00
+{
aedd00
+	if (bin < sizeof(hex_char_map_l))
aedd00
+		return (u) ? hex_char_map_u[bin] : hex_char_map_l[bin];
aedd00
+	return 'X';
aedd00
+}
aedd00
+
aedd00
+/*
aedd00
+ * Convert binary string into hex representation
aedd00
+ * @bin input buffer with binary data
aedd00
+ * @binlen length of bin
aedd00
+ * @hex output buffer to store hex data
aedd00
+ * @hexlen length of already allocated hex buffer (should be at least
aedd00
+ *	   twice binlen -- if not, only a fraction of binlen is converted)
aedd00
+ * @u case of hex characters (0=>lower case, 1=>upper case)
aedd00
+ */
aedd00
+static void bin2hex(const unsigned char *bin, size_t binlen,
aedd00
+		    char *hex, size_t hexlen, int u)
aedd00
+{
aedd00
+	size_t i = 0;
aedd00
+	size_t chars = (binlen > (hexlen / 2)) ? (hexlen / 2) : binlen;
aedd00
+
aedd00
+	for (i = 0; i < chars; i++) {
aedd00
+		hex[(i*2)] = hex_char((bin[i] >> 4), u);
aedd00
+		hex[((i*2)+1)] = hex_char((bin[i] & 0x0f), u);
aedd00
+	}
aedd00
+}
aedd00
+
aedd00
+struct kdf_cavs {
aedd00
+	unsigned char *K;
aedd00
+	size_t Klen;
aedd00
+	unsigned char *H;
aedd00
+	size_t Hlen;
aedd00
+	unsigned char *session_id;
aedd00
+	size_t session_id_len;
aedd00
+
aedd00
+	unsigned int iv_len;
aedd00
+	unsigned int ek_len;
aedd00
+	unsigned int ik_len;
aedd00
+};
aedd00
+
aedd00
+static int sshkdf_cavs(struct kdf_cavs *test)
aedd00
+{
aedd00
+	int ret = 0;
aedd00
+	struct kex kex;
aedd00
+	struct sshbuf *Kb = NULL;
aedd00
+	BIGNUM *Kbn = NULL;
aedd00
+	int mode = 0;
aedd00
+	struct newkeys *ctoskeys;
aedd00
+	struct newkeys *stockeys;
aedd00
+	struct ssh *ssh = NULL;
aedd00
+
aedd00
+#define HEXOUTLEN 500
aedd00
+	char hex[HEXOUTLEN];
aedd00
+
aedd00
+	memset(&kex, 0, sizeof(struct kex));
aedd00
+
aedd00
+	Kbn = BN_new();
aedd00
+	BN_bin2bn(test->K, test->Klen, Kbn);
aedd00
+	if (!Kbn) {
aedd00
+		printf("cannot convert K into bignum\n");
aedd00
+		ret = 1;
aedd00
+		goto out;
aedd00
+	}
aedd00
+	Kb = sshbuf_new();
aedd00
+	if (!Kb) {
aedd00
+		printf("cannot convert K into sshbuf\n");
aedd00
+		ret = 1;
aedd00
+		goto out;
aedd00
+	}
aedd00
+	sshbuf_put_bignum2(Kb, Kbn);
aedd00
+
aedd00
+	kex.session_id = test->session_id;
aedd00
+	kex.session_id_len = test->session_id_len;
aedd00
+
aedd00
+	/* setup kex */
aedd00
+
aedd00
+	/* select the right hash based on struct ssh_digest digests */
aedd00
+	switch (test->ik_len) {
aedd00
+		case 20:
aedd00
+			kex.hash_alg = SSH_DIGEST_SHA1;
aedd00
+			break;
aedd00
+		case 32:
aedd00
+			kex.hash_alg = SSH_DIGEST_SHA256;
aedd00
+			break;
aedd00
+		case 48:
aedd00
+			kex.hash_alg = SSH_DIGEST_SHA384;
aedd00
+			break;
aedd00
+		case 64:
aedd00
+			kex.hash_alg = SSH_DIGEST_SHA512;
aedd00
+			break;
aedd00
+		default:
aedd00
+			printf("Wrong hash type %u\n", test->ik_len);
aedd00
+			ret = 1;
aedd00
+			goto out;
aedd00
+	}
aedd00
+
aedd00
+	/* implement choose_enc */
aedd00
+	for (mode = 0; mode < 2; mode++) {
aedd00
+		kex.newkeys[mode] = calloc(1, sizeof(struct newkeys));
aedd00
+		if (!kex.newkeys[mode]) {
aedd00
+			printf("allocation of newkeys failed\n");
aedd00
+			ret = 1;
aedd00
+			goto out;
aedd00
+		}
aedd00
+		kex.newkeys[mode]->enc.iv_len = test->iv_len;
aedd00
+		kex.newkeys[mode]->enc.key_len = test->ek_len;
aedd00
+		kex.newkeys[mode]->enc.block_size = (test->iv_len == 64) ? 8 : 16;
aedd00
+		kex.newkeys[mode]->mac.key_len = test->ik_len;
aedd00
+	}
aedd00
+
aedd00
+	/* implement kex_choose_conf */
aedd00
+	kex.we_need = kex.newkeys[0]->enc.key_len;
aedd00
+	if (kex.we_need < kex.newkeys[0]->enc.block_size)
aedd00
+		kex.we_need = kex.newkeys[0]->enc.block_size;
aedd00
+	if (kex.we_need < kex.newkeys[0]->enc.iv_len)
aedd00
+		kex.we_need = kex.newkeys[0]->enc.iv_len;
aedd00
+	if (kex.we_need < kex.newkeys[0]->mac.key_len)
aedd00
+		kex.we_need = kex.newkeys[0]->mac.key_len;
aedd00
+
aedd00
+	/* MODE_OUT (1) -> server to client
aedd00
+	 * MODE_IN (0) -> client to server */
aedd00
+	kex.server = 1;
aedd00
+
aedd00
+	/* do it */
aedd00
+	if ((ssh = ssh_packet_set_connection(NULL, -1, -1)) == NULL){
aedd00
+		printf("Allocation error\n");
aedd00
+		goto out;
aedd00
+	}
aedd00
+	ssh->kex = &ke;;
aedd00
+	kex_derive_keys(ssh, test->H, test->Hlen, Kb);
aedd00
+
aedd00
+	ctoskeys = kex.newkeys[0];
aedd00
+	stockeys = kex.newkeys[1];
aedd00
+
aedd00
+	/* get data */
aedd00
+	memset(hex, 0, HEXOUTLEN);
aedd00
+	bin2hex(ctoskeys->enc.iv, (size_t)ctoskeys->enc.iv_len,
aedd00
+		hex, HEXOUTLEN, 0);
aedd00
+	printf("Initial IV (client to server) = %s\n", hex);
aedd00
+	memset(hex, 0, HEXOUTLEN);
aedd00
+	bin2hex(stockeys->enc.iv, (size_t)stockeys->enc.iv_len,
aedd00
+		hex, HEXOUTLEN, 0);
aedd00
+	printf("Initial IV (server to client) = %s\n", hex);
aedd00
+
aedd00
+	memset(hex, 0, HEXOUTLEN);
aedd00
+	bin2hex(ctoskeys->enc.key, (size_t)ctoskeys->enc.key_len,
aedd00
+		hex, HEXOUTLEN, 0);
aedd00
+	printf("Encryption key (client to server) = %s\n", hex);
aedd00
+	memset(hex, 0, HEXOUTLEN);
aedd00
+	bin2hex(stockeys->enc.key, (size_t)stockeys->enc.key_len,
aedd00
+		hex, HEXOUTLEN, 0);
aedd00
+	printf("Encryption key (server to client) = %s\n", hex);
aedd00
+
aedd00
+	memset(hex, 0, HEXOUTLEN);
aedd00
+	bin2hex(ctoskeys->mac.key, (size_t)ctoskeys->mac.key_len,
aedd00
+		hex, HEXOUTLEN, 0);
aedd00
+	printf("Integrity key (client to server) = %s\n", hex);
aedd00
+	memset(hex, 0, HEXOUTLEN);
aedd00
+	bin2hex(stockeys->mac.key, (size_t)stockeys->mac.key_len,
aedd00
+		hex, HEXOUTLEN, 0);
aedd00
+	printf("Integrity key (server to client) = %s\n", hex);
aedd00
+
aedd00
+out:
aedd00
+	if (Kbn)
aedd00
+		BN_free(Kbn);
aedd00
+	if (Kb)
aedd00
+		sshbuf_free(Kb);
aedd00
+	if (ssh)
aedd00
+		ssh_packet_close(ssh);
aedd00
+	return ret;
aedd00
+}
aedd00
+
aedd00
+static void usage(void)
aedd00
+{
aedd00
+	fprintf(stderr, "\nOpenSSH KDF CAVS Test\n\n");
aedd00
+	fprintf(stderr, "Usage:\n");
aedd00
+	fprintf(stderr, "\t-K\tShared secret string\n");
aedd00
+	fprintf(stderr, "\t-H\tHash string\n");
aedd00
+	fprintf(stderr, "\t-s\tSession ID string\n");
aedd00
+	fprintf(stderr, "\t-i\tIV length to be generated\n");
aedd00
+	fprintf(stderr, "\t-e\tEncryption key length to be generated\n");
aedd00
+	fprintf(stderr, "\t-m\tMAC key length to be generated\n");
aedd00
+}
aedd00
+
aedd00
+/*
aedd00
+ * Test command example:
aedd00
+ * ./ssh-cavs -K 0055d50f2d163cc07cd8a93cc7c3430c30ce786b572c01ad29fec7597000cf8618d664e2ec3dcbc8bb7a1a7eb7ef67f61cdaf291625da879186ac0a5cb27af571b59612d6a6e0627344d846271959fda61c78354aa498773d59762f8ca2d0215ec590d8633de921f920d41e47b3de6ab9a3d0869e1c826d0e4adebf8e3fb646a15dea20a410b44e969f4b791ed6a67f13f1b74234004d5fa5e87eff7abc32d49bbdf44d7b0107e8f10609233b7e2b7eff74a4daf25641de7553975dac6ac1e5117df6f6dbaa1c263d23a6c3e5a3d7d49ae8a828c1e333ac3f85fbbf57b5c1a45be45e43a7be1a4707eac779b8285522d1f531fe23f890fd38a004339932b93eda4 -H d3ab91a850febb417a25d892ec48ed5952c7a5de -s d3ab91a850febb417a25d892ec48ed5952c7a5de -i 8 -e 24 -m 20
aedd00
+ *
aedd00
+ * Initial IV (client to server) = 4bb320d1679dfd3a
aedd00
+ * Initial IV (server to client) = 43dea6fdf263a308
aedd00
+ * Encryption key (client to server) = 13048cc600b9d3cf9095aa6cf8e2ff9cf1c54ca0520c89ed
aedd00
+ * Encryption key (server to client) = 1e483c5134e901aa11fc4e0a524e7ec7b75556148a222bb0
aedd00
+ * Integrity key (client to server) = ecef63a092b0dcc585bdc757e01b2740af57d640
aedd00
+ * Integrity key (server to client) = 7424b05f3c44a72b4ebd281fb71f9cbe7b64d479
aedd00
+ */
aedd00
+int main(int argc, char *argv[])
aedd00
+{
aedd00
+	struct kdf_cavs test;
aedd00
+	int ret = 1;
aedd00
+	int opt = 0;
aedd00
+
aedd00
+	memset(&test, 0, sizeof(struct kdf_cavs));
aedd00
+	while((opt = getopt(argc, argv, "K:H:s:i:e:m:")) != -1)
aedd00
+	{
aedd00
+		size_t len = 0;
aedd00
+		switch(opt)
aedd00
+		{
aedd00
+			/*
aedd00
+			 * CAVS K is MPINT
aedd00
+			 * we want a hex (i.e. the caller must ensure the
aedd00
+			 * following transformations already happened):
aedd00
+			 * 	1. cut off first four bytes
aedd00
+			 * 	2. if most significant bit of value is
aedd00
+			 *	   1, prepend 0 byte
aedd00
+			 */
aedd00
+			case 'K':
aedd00
+				len = strlen(optarg);
aedd00
+				ret = hex2bin_alloc(optarg, len,
aedd00
+						    &test.K, &test.Klen);
aedd00
+				if (ret)
aedd00
+					goto out;
aedd00
+				break;
aedd00
+			case 'H':
aedd00
+				len = strlen(optarg);
aedd00
+				ret = hex2bin_alloc(optarg, len,
aedd00
+						    &test.H, &test.Hlen);
aedd00
+				if (ret)
aedd00
+					goto out;
aedd00
+				break;
aedd00
+			case 's':
aedd00
+				len = strlen(optarg);
aedd00
+				ret = hex2bin_alloc(optarg, len,
aedd00
+						    &test.session_id,
aedd00
+						    &test.session_id_len);
aedd00
+				if (ret)
aedd00
+					goto out;
aedd00
+				break;
aedd00
+			case 'i':
aedd00
+				test.iv_len = strtoul(optarg, NULL, 10);
aedd00
+				break;
aedd00
+			case 'e':
aedd00
+				test.ek_len = strtoul(optarg, NULL, 10);
aedd00
+				break;
aedd00
+			case 'm':
aedd00
+				test.ik_len = strtoul(optarg, NULL, 10);
aedd00
+				break;
aedd00
+			default:
aedd00
+				usage();
aedd00
+				goto out;
aedd00
+		}
aedd00
+	}
aedd00
+
aedd00
+	ret = sshkdf_cavs(&test);
aedd00
+
aedd00
+out:
aedd00
+	if (test.session_id)
aedd00
+		free(test.session_id);
aedd00
+	if (test.K)
aedd00
+		free(test.K);
aedd00
+	if (test.H)
aedd00
+		free(test.H);
aedd00
+	return ret;
aedd00
+
aedd00
+}
aedd00
diff -up openssh-6.8p1/ssh-cavs_driver.pl.kdf-cavs openssh-6.8p1/ssh-cavs_driver.pl
aedd00
--- openssh-6.8p1/ssh-cavs_driver.pl.kdf-cavs	2015-03-18 11:23:46.348049354 +0100
aedd00
+++ openssh-6.8p1/ssh-cavs_driver.pl	2015-03-18 11:23:46.348049354 +0100
aedd00
@@ -0,0 +1,184 @@
aedd00
+#!/usr/bin/env perl
aedd00
+#
aedd00
+# CAVS test driver for OpenSSH
aedd00
+#
aedd00
+# Copyright (C) 2015, Stephan Mueller <smueller@chronox.de>
aedd00
+#
aedd00
+# Permission is hereby granted, free of charge, to any person obtaining a copy
aedd00
+# of this software and associated documentation files (the "Software"), to deal
aedd00
+# in the Software without restriction, including without limitation the rights
aedd00
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aedd00
+# copies of the Software, and to permit persons to whom the Software is
aedd00
+# furnished to do so, subject to the following conditions:
aedd00
+#
aedd00
+# The above copyright notice and this permission notice shall be included in
aedd00
+# all copies or substantial portions of the Software.
aedd00
+#
aedd00
+#                            NO WARRANTY
aedd00
+#
aedd00
+#    BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
aedd00
+#    FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
aedd00
+#    OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
aedd00
+#    PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
aedd00
+#    OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
aedd00
+#    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
aedd00
+#    TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
aedd00
+#    PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
aedd00
+#    REPAIR OR CORRECTION.
aedd00
+#
aedd00
+#    IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
aedd00
+#    WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
aedd00
+#    REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
aedd00
+#    INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
aedd00
+#    OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
aedd00
+#    TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
aedd00
+#    YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
aedd00
+#    PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
aedd00
+#    POSSIBILITY OF SUCH DAMAGES.
aedd00
+#
aedd00
+use strict;
aedd00
+use warnings;
aedd00
+use IPC::Open2;
aedd00
+
aedd00
+# Executing a program by feeding STDIN and retrieving
aedd00
+# STDOUT
aedd00
+# $1: data string to be piped to the app on STDIN
aedd00
+# rest: program and args
aedd00
+# returns: STDOUT of program as string
aedd00
+sub pipe_through_program($@) {
aedd00
+	my $in = shift;
aedd00
+	my @args = @_;
aedd00
+
aedd00
+	my ($CO, $CI);
aedd00
+	my $pid = open2($CO, $CI, @args);
aedd00
+
aedd00
+	my $out = "";
aedd00
+	my $len = length($in);
aedd00
+	my $first = 1;
aedd00
+	while (1) {
aedd00
+		my $rin = "";
aedd00
+		my $win = "";
aedd00
+		# Output of prog is FD that we read
aedd00
+		vec($rin,fileno($CO),1) = 1;
aedd00
+		# Input of prog is FD that we write
aedd00
+		# check for $first is needed because we can have NULL input
aedd00
+		# that is to be written to the app
aedd00
+		if ( $len > 0 || $first) {
aedd00
+			(vec($win,fileno($CI),1) = 1);
aedd00
+			$first=0;
aedd00
+		}
aedd00
+		# Let us wait for 100ms
aedd00
+		my $nfound = select(my $rout=$rin, my $wout=$win, undef, 0.1);
aedd00
+		if ( $wout ) {
aedd00
+			my $written = syswrite($CI, $in, $len);
aedd00
+			die "broken pipe" if !defined $written;
aedd00
+			$len -= $written;
aedd00
+			substr($in, 0, $written) = "";
aedd00
+			if ($len <= 0) {
aedd00
+				close $CI or die "broken pipe: $!";
aedd00
+			}
aedd00
+		}
aedd00
+		if ( $rout ) {
aedd00
+			my $tmp_out = "";
aedd00
+			my $bytes_read = sysread($CO, $tmp_out, 4096);
aedd00
+			$out .= $tmp_out;
aedd00
+			last if ($bytes_read == 0);
aedd00
+		}
aedd00
+	}
aedd00
+	close $CO or die "broken pipe: $!";
aedd00
+	waitpid $pid, 0;
aedd00
+
aedd00
+	return $out;
aedd00
+}
aedd00
+
aedd00
+# Parser of CAVS test vector file
aedd00
+# $1: Test vector file
aedd00
+# $2: Output file for test results
aedd00
+# return: nothing
aedd00
+sub parse($$) {
aedd00
+	my $infile = shift;
aedd00
+	my $outfile = shift;
aedd00
+
aedd00
+	my $out = "";
aedd00
+
aedd00
+	my $K = "";
aedd00
+	my $H = "";
aedd00
+	my $session_id = "";
aedd00
+	my $ivlen = 0;
aedd00
+	my $eklen = "";
aedd00
+	my $iklen = "";
aedd00
+
aedd00
+	open(IN, "<$infile");
aedd00
+	while(<IN>) {
aedd00
+
aedd00
+		my $line = $_;
aedd00
+		chomp($line);
aedd00
+		$line =~ s/\r//;
aedd00
+
aedd00
+		if ($line =~ /\[SHA-1\]/) {
aedd00
+			$iklen = 20;
aedd00
+		} elsif ($line =~ /\[SHA-256\]/) {
aedd00
+			$iklen = 32;
aedd00
+		} elsif ($line =~ /\[SHA-384\]/) {
aedd00
+			$iklen = 48;
aedd00
+		} elsif ($line =~ /\[SHA-512\]/) {
aedd00
+			$iklen = 64;
aedd00
+		} elsif ($line =~ /^\[IV length\s*=\s*(.*)\]/) {
aedd00
+			$ivlen = $1;
aedd00
+			$ivlen = $ivlen / 8;
aedd00
+		} elsif ($line =~ /^\[encryption key length\s*=\s*(.*)\]/) {
aedd00
+			$eklen = $1;
aedd00
+			$eklen = $eklen / 8;
aedd00
+		} elsif ($line =~ /^K\s*=\s*(.*)/) {
aedd00
+			$K = $1;
aedd00
+			$K = substr($K, 8);
aedd00
+			$K = "00" . $K;
aedd00
+		} elsif ($line =~ /^H\s*=\s*(.*)/) {
aedd00
+			$H = $1;
aedd00
+		} elsif ($line =~ /^session_id\s*=\s*(.*)/) {
aedd00
+			$session_id = $1;
aedd00
+		}
aedd00
+		$out .= $line . "\n";
aedd00
+
aedd00
+		if ($K ne "" && $H ne "" && $session_id ne "" &&
aedd00
+		    $ivlen ne "" && $eklen ne "" && $iklen > 0) {
aedd00
+			$out .= pipe_through_program("", "./ssh-cavs -H $H -K $K -s $session_id -i $ivlen -e $eklen -m $iklen");
aedd00
+
aedd00
+			$K = "";
aedd00
+			$H = "";
aedd00
+			$session_id = "";
aedd00
+		}
aedd00
+	}
aedd00
+	close IN;
aedd00
+	$out =~ s/\n/\r\n/g; # make it a dos file
aedd00
+	open(OUT, ">$outfile") or die "Cannot create output file $outfile: $?";
aedd00
+	print OUT $out;
aedd00
+	close OUT;
aedd00
+}
aedd00
+
aedd00
+############################################################
aedd00
+#
aedd00
+# let us pretend to be C :-)
aedd00
+sub main() {
aedd00
+
aedd00
+	my $infile=$ARGV[0];
aedd00
+	die "Error: Test vector file $infile not found" if (! -f $infile);
aedd00
+
aedd00
+	my $outfile = $infile;
aedd00
+	# let us add .rsp regardless whether we could strip .req
aedd00
+	$outfile =~ s/\.req$//;
aedd00
+	$outfile .= ".rsp";
aedd00
+	if (-f $outfile) {
aedd00
+		die "Output file $outfile could not be removed: $?"
aedd00
+			unless unlink($outfile);
aedd00
+	}
aedd00
+	print STDERR "Performing tests from source file $infile with results stored in destination file $outfile\n";
aedd00
+
aedd00
+	# Do the job
aedd00
+	parse($infile, $outfile);
aedd00
+}
aedd00
+
aedd00
+###########################################
aedd00
+# Call it
aedd00
+main();
aedd00
+1;