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