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

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