bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone

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

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