bmh10 / rpms / openssh

Forked from rpms/openssh 10 days ago
Clone

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

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