04aef64
diff -up openssh-6.8p1/Makefile.in.ctr-cavs openssh-6.8p1/Makefile.in
04aef64
--- openssh-6.8p1/Makefile.in.ctr-cavs	2015-03-18 11:22:05.493289018 +0100
04aef64
+++ openssh-6.8p1/Makefile.in	2015-03-18 11:22:44.504196316 +0100
0ae19bf
@@ -28,6 +28,7 @@ SSH_KEYSIGN=$(libexecdir)/ssh-keysign
0ae19bf
 SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-helper
0ae19bf
 SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
0ae19bf
 SSH_KEYCAT=$(libexecdir)/ssh-keycat
0ae19bf
+CTR_CAVSTEST=$(libexecdir)/ctr-cavstest
0ae19bf
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
0ae19bf
 PRIVSEP_PATH=@PRIVSEP_PATH@
0ae19bf
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
04aef64
@@ -66,7 +67,7 @@ EXEEXT=@EXEEXT@
0ae19bf
 MANFMT=@MANFMT@
0ae19bf
 INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
0ae19bf
 
0ae19bf
-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)
0ae19bf
+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)
0ae19bf
 
04aef64
 LIBOPENSSH_OBJS=\
04aef64
 	ssh_api.o \
04aef64
@@ -194,6 +195,9 @@ ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) l
0ae19bf
 ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o
0ae19bf
 	$(LD) -o $@ ssh-keycat.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(SSHLIBS)
0ae19bf
 
0ae19bf
+ctr-cavstest$(EXEEXT): $(LIBCOMPAT) libssh.a ctr-cavstest.o
0ae19bf
+	$(LD) -o $@ ctr-cavstest.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
0ae19bf
+
0ae19bf
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
0ae19bf
 	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
0ae19bf
 
04aef64
@@ -326,6 +330,7 @@ install-files:
0ae19bf
 		$(INSTALL) -m 0700 ssh-ldap-wrapper $(DESTDIR)$(SSH_LDAP_WRAPPER) ; \
0ae19bf
 	fi
0ae19bf
 	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
0ae19bf
+	$(INSTALL) -m 0755 $(STRIP_OPT) ctr-cavstest$(EXEEXT) $(DESTDIR)$(libexecdir)/ctr-cavstest$(EXEEXT)
0ae19bf
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
0ae19bf
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
0ae19bf
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
04aef64
diff -up openssh-6.8p1/ctr-cavstest.c.ctr-cavs openssh-6.8p1/ctr-cavstest.c
04aef64
--- openssh-6.8p1/ctr-cavstest.c.ctr-cavs	2015-03-18 11:22:05.521288952 +0100
04aef64
+++ openssh-6.8p1/ctr-cavstest.c	2015-03-18 11:22:05.521288952 +0100
634cb6d
@@ -0,0 +1,208 @@
634cb6d
+/*
634cb6d
+ *
634cb6d
+ * invocation (all of the following are equal):
634cb6d
+ * ./ctr-cavstest --algo aes128-ctr --key 987212980144b6a632e864031f52dacc --mode encrypt --data a6deca405eef2e8e4609abf3c3ccf4a6
634cb6d
+ * ./ctr-cavstest --algo aes128-ctr --key 987212980144b6a632e864031f52dacc --mode encrypt --data a6deca405eef2e8e4609abf3c3ccf4a6 --iv 00000000000000000000000000000000
634cb6d
+ * echo -n a6deca405eef2e8e4609abf3c3ccf4a6 | ./ctr-cavstest --algo aes128-ctr --key 987212980144b6a632e864031f52dacc --mode encrypt
634cb6d
+ */
634cb6d
+
634cb6d
+#include "includes.h"
634cb6d
+
634cb6d
+#include <sys/types.h>
634cb6d
+#include <sys/param.h>
634cb6d
+#include <stdarg.h>
634cb6d
+#include <stdio.h>
634cb6d
+#include <stdlib.h>
634cb6d
+#include <string.h>
634cb6d
+#include <ctype.h>
634cb6d
+
634cb6d
+#include "xmalloc.h"
634cb6d
+#include "log.h"
634cb6d
+#include "cipher.h"
634cb6d
+
634cb6d
+/* compatibility with old or broken OpenSSL versions */
634cb6d
+#include "openbsd-compat/openssl-compat.h"
634cb6d
+
634cb6d
+void usage(void) {
634cb6d
+        fprintf(stderr, "Usage: ctr-cavstest --algo <ssh-crypto-algorithm>\n"
634cb6d
+                        "                    --key <hexadecimal-key> --mode <encrypt|decrypt>\n"
634cb6d
+                        "                    [--iv <hexadecimal-iv>] --data <hexadecimal-data>\n\n"
634cb6d
+                        "Hexadecimal output is printed to stdout.\n"
634cb6d
+                        "Hexadecimal input data can be alternatively read from stdin.\n");
634cb6d
+        exit(1);
634cb6d
+}
634cb6d
+
634cb6d
+void *fromhex(char *hex, size_t *len)
634cb6d
+{
634cb6d
+        unsigned char *bin;
634cb6d
+        char *p;
634cb6d
+        size_t n = 0;
634cb6d
+        int shift = 4;
634cb6d
+        unsigned char out = 0;
634cb6d
+        unsigned char *optr;
634cb6d
+
634cb6d
+        bin = xmalloc(strlen(hex)/2);
634cb6d
+        optr = bin;
634cb6d
+
634cb6d
+        for (p = hex; *p != '\0'; ++p) {
634cb6d
+                unsigned char c;
634cb6d
+
634cb6d
+                c = *p;
634cb6d
+                if (isspace(c))
634cb6d
+                        continue;
634cb6d
+
634cb6d
+                if (c >= '0' && c <= '9') {
634cb6d
+                        c = c - '0';
634cb6d
+                } else if (c >= 'A' && c <= 'F') {
634cb6d
+                        c = c - 'A' + 10;
634cb6d
+                } else if (c >= 'a' && c <= 'f') {
634cb6d
+                        c = c - 'a' + 10;
634cb6d
+                } else {
634cb6d
+                        /* truncate on nonhex cipher */
634cb6d
+                        break;
634cb6d
+                }
634cb6d
+
634cb6d
+                out |= c << shift;
634cb6d
+                shift = (shift + 4) % 8;
634cb6d
+
634cb6d
+                if (shift) {
634cb6d
+                        *(optr++) = out;
634cb6d
+                        out = 0;
634cb6d
+                        ++n;
634cb6d
+                }
634cb6d
+        }
634cb6d
+
634cb6d
+        *len = n;
634cb6d
+        return bin;
634cb6d
+}
634cb6d
+
634cb6d
+#define READ_CHUNK 4096
634cb6d
+#define MAX_READ_SIZE 1024*1024*100
634cb6d
+char *read_stdin(void)
634cb6d
+{
634cb6d
+        char *buf;
634cb6d
+        size_t n, total = 0;
634cb6d
+
634cb6d
+        buf = xmalloc(READ_CHUNK);
634cb6d
+
634cb6d
+        do {
634cb6d
+                n = fread(buf + total, 1, READ_CHUNK, stdin);
634cb6d
+                if (n < READ_CHUNK) /* terminate on short read */
634cb6d
+                        break;
634cb6d
+
634cb6d
+                total += n;
634cb6d
+                buf = xrealloc(buf, total + READ_CHUNK, 1);
634cb6d
+        } while(total < MAX_READ_SIZE);
634cb6d
+        return buf;
634cb6d
+}
634cb6d
+
634cb6d
+int main (int argc, char *argv[])
634cb6d
+{
634cb6d
+
0ae19bf
+        const Cipher *c;
634cb6d
+        CipherContext cc;
634cb6d
+        char *algo = "aes128-ctr";
634cb6d
+        char *hexkey = NULL;
634cb6d
+        char *hexiv = "00000000000000000000000000000000";
634cb6d
+        char *hexdata = NULL;
634cb6d
+        char *p;
634cb6d
+        int i;
634cb6d
+        int encrypt = 1;
634cb6d
+        void *key;
634cb6d
+        size_t keylen;
634cb6d
+        void *iv;
634cb6d
+        size_t ivlen;
634cb6d
+        void *data;
634cb6d
+        size_t datalen;
634cb6d
+        void *outdata;
634cb6d
+
634cb6d
+        for (i = 1; i < argc; ++i) {
634cb6d
+                if (strcmp(argv[i], "--algo") == 0) {
634cb6d
+                        algo = argv[++i];
634cb6d
+                } else if (strcmp(argv[i], "--key") == 0) {
634cb6d
+                        hexkey = argv[++i];
634cb6d
+                } else if (strcmp(argv[i], "--mode") == 0) {
634cb6d
+                        ++i;
634cb6d
+                        if (argv[i] == NULL) {
634cb6d
+                                usage();
634cb6d
+                        }
634cb6d
+                        if (strncmp(argv[i], "enc", 3) == 0) {
634cb6d
+                                encrypt = 1;
634cb6d
+                        } else if (strncmp(argv[i], "dec", 3) == 0) {
634cb6d
+                                encrypt = 0;
634cb6d
+                        } else {
634cb6d
+                                usage();
634cb6d
+                        }
634cb6d
+                } else if (strcmp(argv[i], "--iv") == 0) {
634cb6d
+                        hexiv = argv[++i];
634cb6d
+                } else if (strcmp(argv[i], "--data") == 0) {
634cb6d
+                        hexdata = argv[++i];
634cb6d
+                }
634cb6d
+        }
634cb6d
+
634cb6d
+        if (hexkey == NULL || algo == NULL) {
634cb6d
+                usage();
634cb6d
+        }
634cb6d
+
634cb6d
+	SSLeay_add_all_algorithms();
634cb6d
+
634cb6d
+	c = cipher_by_name(algo);
634cb6d
+	if (c == NULL) {
634cb6d
+		fprintf(stderr, "Error: unknown algorithm\n");
634cb6d
+		return 2;
634cb6d
+	}
634cb6d
+
634cb6d
+        if (hexdata == NULL) {
634cb6d
+                hexdata = read_stdin();
634cb6d
+        } else {
634cb6d
+                hexdata = xstrdup(hexdata);
634cb6d
+        }
634cb6d
+
634cb6d
+        key = fromhex(hexkey, &keylen);
634cb6d
+
634cb6d
+	if (keylen != 16 && keylen != 24 && keylen == 32) {
634cb6d
+		fprintf(stderr, "Error: unsupported key length\n");
634cb6d
+		return 2;
634cb6d
+	}
634cb6d
+
634cb6d
+        iv = fromhex(hexiv, &ivlen);
634cb6d
+
634cb6d
+        if (ivlen != 16) {
634cb6d
+		fprintf(stderr, "Error: unsupported iv length\n");
634cb6d
+		return 2;
634cb6d
+        }
634cb6d
+
634cb6d
+        data = fromhex(hexdata, &datalen);
634cb6d
+
634cb6d
+	if (data == NULL || datalen == 0) {
634cb6d
+		fprintf(stderr, "Error: no data to encrypt/decrypt\n");
634cb6d
+		return 2;
634cb6d
+	}
634cb6d
+
634cb6d
+	cipher_init(&cc, c, key, keylen, iv, ivlen, encrypt);
634cb6d
+
aaf34e5
+        free(key);
aaf34e5
+        free(iv);
634cb6d
+
634cb6d
+	outdata = malloc(datalen);
634cb6d
+	if(outdata == NULL) {
634cb6d
+		fprintf(stderr, "Error: memory allocation failure\n");
634cb6d
+		return 2;
634cb6d
+	}
634cb6d
+
0ae19bf
+	cipher_crypt(&cc, 0, outdata, data, datalen, 0, 0);
634cb6d
+
aaf34e5
+        free(data);
634cb6d
+
634cb6d
+	cipher_cleanup(&cc);
634cb6d
+
634cb6d
+        for (p = outdata; datalen > 0; ++p, --datalen) {
634cb6d
+		printf("%02X", (unsigned char)*p);
634cb6d
+	}
634cb6d
+
aaf34e5
+        free(outdata);
634cb6d
+
634cb6d
+        printf("\n");
634cb6d
+        return 0;
634cb6d
+}
634cb6d
+