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