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