kentpeacock / rpms / openssh

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