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