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