jonathancammack / rpms / openssh

Forked from rpms/openssh 8 months ago
Clone

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

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