Blame SOURCES/openssh-6.6p1-ctr-cavstest.patch

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