vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 94c6f8
diff --git a/Makefile.in b/Makefile.in
Petr Lautrbach 94c6f8
index 4ab6717..581b121 100644
Petr Lautrbach 94c6f8
--- a/Makefile.in
Petr Lautrbach 94c6f8
+++ b/Makefile.in
Petr Lautrbach 94c6f8
@@ -28,6 +28,7 @@ SSH_KEYSIGN=$(libexecdir)/ssh-keysign
Petr Lautrbach 94c6f8
 SSH_LDAP_HELPER=$(libexecdir)/ssh-ldap-helper
Petr Lautrbach 94c6f8
 SSH_LDAP_WRAPPER=$(libexecdir)/ssh-ldap-wrapper
Petr Lautrbach 94c6f8
 SSH_KEYCAT=$(libexecdir)/ssh-keycat
Petr Lautrbach 94c6f8
+CTR_CAVSTEST=$(libexecdir)/ctr-cavstest
Petr Lautrbach 94c6f8
 SSH_PKCS11_HELPER=$(libexecdir)/ssh-pkcs11-helper
Petr Lautrbach 94c6f8
 PRIVSEP_PATH=@PRIVSEP_PATH@
Petr Lautrbach 94c6f8
 SSH_PRIVSEP_USER=@SSH_PRIVSEP_USER@
Petr Lautrbach 94c6f8
@@ -65,7 +66,7 @@ EXEEXT=@EXEEXT@
Petr Lautrbach 94c6f8
 MANFMT=@MANFMT@
Petr Lautrbach 94c6f8
 INSTALL_SSH_LDAP_HELPER=@INSTALL_SSH_LDAP_HELPER@
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
-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)
Petr Lautrbach 94c6f8
+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)
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
 LIBSSH_OBJS=authfd.o authfile.o bufaux.o bufbn.o buffer.o \
Petr Lautrbach 94c6f8
 	canohost.o channels.o cipher.o cipher-aes.o \
Petr Lautrbach 94c6f8
@@ -180,6 +181,9 @@ ssh-ldap-helper$(EXEEXT): $(LIBCOMPAT) libssh.a ldapconf.o ldapbody.o ldapmisc.o
Petr Lautrbach 94c6f8
 ssh-keycat$(EXEEXT): $(LIBCOMPAT) $(SSHDOBJS) libssh.a ssh-keycat.o
Petr Lautrbach 94c6f8
 	$(LD) -o $@ ssh-keycat.o bufaux.o $(LDFLAGS) -lssh -lopenbsd-compat -lfipscheck $(SSHDLIBS) $(SSHLIBS)
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
+ctr-cavstest$(EXEEXT): $(LIBCOMPAT) libssh.a ctr-cavstest.o
Petr Lautrbach 94c6f8
+	$(LD) -o $@ ctr-cavstest.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh -lfipscheck $(LIBS)
Petr Lautrbach 94c6f8
+
Petr Lautrbach 94c6f8
 ssh-keyscan$(EXEEXT): $(LIBCOMPAT) libssh.a ssh-keyscan.o roaming_dummy.o
Petr Lautrbach 94c6f8
 	$(LD) -o $@ ssh-keyscan.o roaming_dummy.o $(LDFLAGS) -lssh -lopenbsd-compat -lssh $(LIBS)
Petr Lautrbach 94c6f8
 
Petr Lautrbach 94c6f8
@@ -288,6 +292,7 @@ install-files:
Petr Lautrbach 94c6f8
 		$(INSTALL) -m 0700 ssh-ldap-wrapper $(DESTDIR)$(SSH_LDAP_WRAPPER) ; \
Petr Lautrbach 94c6f8
 	fi
Petr Lautrbach 94c6f8
 	$(INSTALL) -m 0755 $(STRIP_OPT) ssh-keycat$(EXEEXT) $(DESTDIR)$(libexecdir)/ssh-keycat$(EXEEXT)
Petr Lautrbach 94c6f8
+	$(INSTALL) -m 0755 $(STRIP_OPT) ctr-cavstest$(EXEEXT) $(DESTDIR)$(libexecdir)/ctr-cavstest$(EXEEXT)
Petr Lautrbach 94c6f8
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp$(EXEEXT) $(DESTDIR)$(bindir)/sftp$(EXEEXT)
Petr Lautrbach 94c6f8
 	$(INSTALL) -m 0755 $(STRIP_OPT) sftp-server$(EXEEXT) $(DESTDIR)$(SFTP_SERVER)$(EXEEXT)
Petr Lautrbach 94c6f8
 	$(INSTALL) -m 644 ssh.1.out $(DESTDIR)$(mandir)/$(mansubdir)1/ssh.1
Petr Lautrbach 94c6f8
diff --git a/ctr-cavstest.c b/ctr-cavstest.c
Petr Lautrbach 94c6f8
new file mode 100644
Petr Lautrbach 94c6f8
index 0000000..bbcbe8a
Petr Lautrbach 94c6f8
--- /dev/null
Petr Lautrbach 94c6f8
+++ b/ctr-cavstest.c
Tomas Mraz 017c65
@@ -0,0 +1,208 @@
Tomas Mraz 017c65
+/*
Tomas Mraz 017c65
+ *
Tomas Mraz 017c65
+ * invocation (all of the following are equal):
Tomas Mraz 017c65
+ * ./ctr-cavstest --algo aes128-ctr --key 987212980144b6a632e864031f52dacc --mode encrypt --data a6deca405eef2e8e4609abf3c3ccf4a6
Tomas Mraz 017c65
+ * ./ctr-cavstest --algo aes128-ctr --key 987212980144b6a632e864031f52dacc --mode encrypt --data a6deca405eef2e8e4609abf3c3ccf4a6 --iv 00000000000000000000000000000000
Tomas Mraz 017c65
+ * echo -n a6deca405eef2e8e4609abf3c3ccf4a6 | ./ctr-cavstest --algo aes128-ctr --key 987212980144b6a632e864031f52dacc --mode encrypt
Tomas Mraz 017c65
+ */
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+#include "includes.h"
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+#include <sys/types.h>
Tomas Mraz 017c65
+#include <sys/param.h>
Tomas Mraz 017c65
+#include <stdarg.h>
Tomas Mraz 017c65
+#include <stdio.h>
Tomas Mraz 017c65
+#include <stdlib.h>
Tomas Mraz 017c65
+#include <string.h>
Tomas Mraz 017c65
+#include <ctype.h>
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+#include "xmalloc.h"
Tomas Mraz 017c65
+#include "log.h"
Tomas Mraz 017c65
+#include "cipher.h"
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+/* compatibility with old or broken OpenSSL versions */
Tomas Mraz 017c65
+#include "openbsd-compat/openssl-compat.h"
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+void usage(void) {
Tomas Mraz 017c65
+        fprintf(stderr, "Usage: ctr-cavstest --algo <ssh-crypto-algorithm>\n"
Tomas Mraz 017c65
+                        "                    --key <hexadecimal-key> --mode <encrypt|decrypt>\n"
Tomas Mraz 017c65
+                        "                    [--iv <hexadecimal-iv>] --data <hexadecimal-data>\n\n"
Tomas Mraz 017c65
+                        "Hexadecimal output is printed to stdout.\n"
Tomas Mraz 017c65
+                        "Hexadecimal input data can be alternatively read from stdin.\n");
Tomas Mraz 017c65
+        exit(1);
Tomas Mraz 017c65
+}
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+void *fromhex(char *hex, size_t *len)
Tomas Mraz 017c65
+{
Tomas Mraz 017c65
+        unsigned char *bin;
Tomas Mraz 017c65
+        char *p;
Tomas Mraz 017c65
+        size_t n = 0;
Tomas Mraz 017c65
+        int shift = 4;
Tomas Mraz 017c65
+        unsigned char out = 0;
Tomas Mraz 017c65
+        unsigned char *optr;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        bin = xmalloc(strlen(hex)/2);
Tomas Mraz 017c65
+        optr = bin;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        for (p = hex; *p != '\0'; ++p) {
Tomas Mraz 017c65
+                unsigned char c;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+                c = *p;
Tomas Mraz 017c65
+                if (isspace(c))
Tomas Mraz 017c65
+                        continue;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+                if (c >= '0' && c <= '9') {
Tomas Mraz 017c65
+                        c = c - '0';
Tomas Mraz 017c65
+                } else if (c >= 'A' && c <= 'F') {
Tomas Mraz 017c65
+                        c = c - 'A' + 10;
Tomas Mraz 017c65
+                } else if (c >= 'a' && c <= 'f') {
Tomas Mraz 017c65
+                        c = c - 'a' + 10;
Tomas Mraz 017c65
+                } else {
Tomas Mraz 017c65
+                        /* truncate on nonhex cipher */
Tomas Mraz 017c65
+                        break;
Tomas Mraz 017c65
+                }
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+                out |= c << shift;
Tomas Mraz 017c65
+                shift = (shift + 4) % 8;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+                if (shift) {
Tomas Mraz 017c65
+                        *(optr++) = out;
Tomas Mraz 017c65
+                        out = 0;
Tomas Mraz 017c65
+                        ++n;
Tomas Mraz 017c65
+                }
Tomas Mraz 017c65
+        }
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        *len = n;
Tomas Mraz 017c65
+        return bin;
Tomas Mraz 017c65
+}
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+#define READ_CHUNK 4096
Tomas Mraz 017c65
+#define MAX_READ_SIZE 1024*1024*100
Tomas Mraz 017c65
+char *read_stdin(void)
Tomas Mraz 017c65
+{
Tomas Mraz 017c65
+        char *buf;
Tomas Mraz 017c65
+        size_t n, total = 0;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        buf = xmalloc(READ_CHUNK);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        do {
Tomas Mraz 017c65
+                n = fread(buf + total, 1, READ_CHUNK, stdin);
Tomas Mraz 017c65
+                if (n < READ_CHUNK) /* terminate on short read */
Tomas Mraz 017c65
+                        break;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+                total += n;
Tomas Mraz 017c65
+                buf = xrealloc(buf, total + READ_CHUNK, 1);
Tomas Mraz 017c65
+        } while(total < MAX_READ_SIZE);
Tomas Mraz 017c65
+        return buf;
Tomas Mraz 017c65
+}
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+int main (int argc, char *argv[])
Tomas Mraz 017c65
+{
Tomas Mraz 017c65
+
Petr Lautrbach 94c6f8
+        const Cipher *c;
Tomas Mraz 017c65
+        CipherContext cc;
Tomas Mraz 017c65
+        char *algo = "aes128-ctr";
Tomas Mraz 017c65
+        char *hexkey = NULL;
Tomas Mraz 017c65
+        char *hexiv = "00000000000000000000000000000000";
Tomas Mraz 017c65
+        char *hexdata = NULL;
Tomas Mraz 017c65
+        char *p;
Tomas Mraz 017c65
+        int i;
Tomas Mraz 017c65
+        int encrypt = 1;
Tomas Mraz 017c65
+        void *key;
Tomas Mraz 017c65
+        size_t keylen;
Tomas Mraz 017c65
+        void *iv;
Tomas Mraz 017c65
+        size_t ivlen;
Tomas Mraz 017c65
+        void *data;
Tomas Mraz 017c65
+        size_t datalen;
Tomas Mraz 017c65
+        void *outdata;
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        for (i = 1; i < argc; ++i) {
Tomas Mraz 017c65
+                if (strcmp(argv[i], "--algo") == 0) {
Tomas Mraz 017c65
+                        algo = argv[++i];
Tomas Mraz 017c65
+                } else if (strcmp(argv[i], "--key") == 0) {
Tomas Mraz 017c65
+                        hexkey = argv[++i];
Tomas Mraz 017c65
+                } else if (strcmp(argv[i], "--mode") == 0) {
Tomas Mraz 017c65
+                        ++i;
Tomas Mraz 017c65
+                        if (argv[i] == NULL) {
Tomas Mraz 017c65
+                                usage();
Tomas Mraz 017c65
+                        }
Tomas Mraz 017c65
+                        if (strncmp(argv[i], "enc", 3) == 0) {
Tomas Mraz 017c65
+                                encrypt = 1;
Tomas Mraz 017c65
+                        } else if (strncmp(argv[i], "dec", 3) == 0) {
Tomas Mraz 017c65
+                                encrypt = 0;
Tomas Mraz 017c65
+                        } else {
Tomas Mraz 017c65
+                                usage();
Tomas Mraz 017c65
+                        }
Tomas Mraz 017c65
+                } else if (strcmp(argv[i], "--iv") == 0) {
Tomas Mraz 017c65
+                        hexiv = argv[++i];
Tomas Mraz 017c65
+                } else if (strcmp(argv[i], "--data") == 0) {
Tomas Mraz 017c65
+                        hexdata = argv[++i];
Tomas Mraz 017c65
+                }
Tomas Mraz 017c65
+        }
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        if (hexkey == NULL || algo == NULL) {
Tomas Mraz 017c65
+                usage();
Tomas Mraz 017c65
+        }
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	SSLeay_add_all_algorithms();
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	c = cipher_by_name(algo);
Tomas Mraz 017c65
+	if (c == NULL) {
Tomas Mraz 017c65
+		fprintf(stderr, "Error: unknown algorithm\n");
Tomas Mraz 017c65
+		return 2;
Tomas Mraz 017c65
+	}
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        if (hexdata == NULL) {
Tomas Mraz 017c65
+                hexdata = read_stdin();
Tomas Mraz 017c65
+        } else {
Tomas Mraz 017c65
+                hexdata = xstrdup(hexdata);
Tomas Mraz 017c65
+        }
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        key = fromhex(hexkey, &keylen);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	if (keylen != 16 && keylen != 24 && keylen == 32) {
Tomas Mraz 017c65
+		fprintf(stderr, "Error: unsupported key length\n");
Tomas Mraz 017c65
+		return 2;
Tomas Mraz 017c65
+	}
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        iv = fromhex(hexiv, &ivlen);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        if (ivlen != 16) {
Tomas Mraz 017c65
+		fprintf(stderr, "Error: unsupported iv length\n");
Tomas Mraz 017c65
+		return 2;
Tomas Mraz 017c65
+        }
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        data = fromhex(hexdata, &datalen);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	if (data == NULL || datalen == 0) {
Tomas Mraz 017c65
+		fprintf(stderr, "Error: no data to encrypt/decrypt\n");
Tomas Mraz 017c65
+		return 2;
Tomas Mraz 017c65
+	}
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	cipher_init(&cc, c, key, keylen, iv, ivlen, encrypt);
Tomas Mraz 017c65
+
Petr Lautrbach 84822b
+        free(key);
Petr Lautrbach 84822b
+        free(iv);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	outdata = malloc(datalen);
Tomas Mraz 017c65
+	if(outdata == NULL) {
Tomas Mraz 017c65
+		fprintf(stderr, "Error: memory allocation failure\n");
Tomas Mraz 017c65
+		return 2;
Tomas Mraz 017c65
+	}
Tomas Mraz 017c65
+
Petr Lautrbach 94c6f8
+	cipher_crypt(&cc, 0, outdata, data, datalen, 0, 0);
Tomas Mraz 017c65
+
Petr Lautrbach 84822b
+        free(data);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+	cipher_cleanup(&cc);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        for (p = outdata; datalen > 0; ++p, --datalen) {
Tomas Mraz 017c65
+		printf("%02X", (unsigned char)*p);
Tomas Mraz 017c65
+	}
Tomas Mraz 017c65
+
Petr Lautrbach 84822b
+        free(outdata);
Tomas Mraz 017c65
+
Tomas Mraz 017c65
+        printf("\n");
Tomas Mraz 017c65
+        return 0;
Tomas Mraz 017c65
+}
Tomas Mraz 017c65
+