vishalmishra434 / rpms / openssh

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