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