bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone

Blame SOURCES/openssh-6.3p1-ctr-evp-fast.patch

8f2528
diff -up openssh-5.9p1/cipher-ctr.c.ctr-evp openssh-5.9p1/cipher-ctr.c
8f2528
--- openssh-5.9p1/cipher-ctr.c.ctr-evp	2012-01-11 09:24:06.000000000 +0100
8f2528
+++ openssh-5.9p1/cipher-ctr.c	2012-01-11 15:54:04.675956600 +0100
8f2528
@@ -38,7 +38,7 @@ void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, in
8f2528
 
8f2528
 struct ssh_aes_ctr_ctx
8f2528
 {
8f2528
-	AES_KEY		aes_ctx;
8f2528
+	EVP_CIPHER_CTX	ecbctx;
8f2528
 	u_char		aes_counter[AES_BLOCK_SIZE];
8f2528
 };
8f2528
 
8f2528
@@ -63,21 +63,42 @@ ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char
8f2528
 {
8f2528
 	struct ssh_aes_ctr_ctx *c;
8f2528
 	size_t n = 0;
8f2528
-	u_char buf[AES_BLOCK_SIZE];
8f2528
+	u_char ctrbuf[AES_BLOCK_SIZE*256];
8f2528
+	u_char buf[AES_BLOCK_SIZE*256];
8f2528
 
8f2528
 	if (len == 0)
8f2528
 		return (1);
8f2528
 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
8f2528
 		return (0);
8f2528
 
8f2528
-	while ((len--) > 0) {
8f2528
+	for (; len > 0; len -= sizeof(u_int)) {
8f2528
+		u_int r,a,b;
8f2528
+
8f2528
 		if (n == 0) {
8f2528
-			AES_encrypt(c->aes_counter, buf, &c->aes_ctx);
8f2528
-			ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
8f2528
+			int outl, i, buflen;
8f2528
+
8f2528
+			buflen = MIN(len, sizeof(ctrbuf));
8f2528
+
8f2528
+			for(i = 0; i < buflen; i += AES_BLOCK_SIZE) {
8f2528
+				memcpy(&ctrbuf[i], c->aes_counter, AES_BLOCK_SIZE);
8f2528
+				ssh_ctr_inc(c->aes_counter, AES_BLOCK_SIZE);
8f2528
+			}
8f2528
+
8f2528
+			EVP_EncryptUpdate(&c->ecbctx, buf, &outl,
8f2528
+				ctrbuf, buflen);
8f2528
 		}
8f2528
-		*(dest++) = *(src++) ^ buf[n];
8f2528
-		n = (n + 1) % AES_BLOCK_SIZE;
8f2528
+
8f2528
+		memcpy(&a, src, sizeof(a));
8f2528
+		memcpy(&b, &buf[n], sizeof(b));
8f2528
+		r = a ^ b;
8f2528
+		memcpy(dest, &r, sizeof(r));
8f2528
+		src += sizeof(a);
8f2528
+		dest += sizeof(r);
8f2528
+
8f2528
+		n = (n + sizeof(b)) % sizeof(buf);
8f2528
 	}
8f2528
+	memset(ctrbuf, '\0', sizeof(ctrbuf));
8f2528
+	memset(buf, '\0', sizeof(buf));
8f2528
 	return (1);
8f2528
 }
8f2528
 
8f2528
@@ -91,9 +112,28 @@ ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, co
8f2528
 		c = xmalloc(sizeof(*c));
8f2528
 		EVP_CIPHER_CTX_set_app_data(ctx, c);
8f2528
 	}
8f2528
-	if (key != NULL)
8f2528
-		AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
8f2528
-		    &c->aes_ctx);
8f2528
+
8f2528
+	EVP_CIPHER_CTX_init(&c->ecbctx);
8f2528
+
8f2528
+	if (key != NULL) {
8f2528
+		const EVP_CIPHER *cipher;
8f2528
+		switch(EVP_CIPHER_CTX_key_length(ctx)*8) {
8f2528
+			case 128:
8f2528
+				cipher = EVP_aes_128_ecb();
8f2528
+				break;
8f2528
+			case 192:
8f2528
+				cipher = EVP_aes_192_ecb();
8f2528
+				break;
8f2528
+			case 256:
8f2528
+				cipher = EVP_aes_256_ecb();
8f2528
+				break;
8f2528
+			default:
8f2528
+				fatal("ssh_aes_ctr_init: wrong aes key length");
8f2528
+		}
8f2528
+		if(!EVP_EncryptInit_ex(&c->ecbctx, cipher, NULL, key, NULL))
8f2528
+			fatal("ssh_aes_ctr_init: cannot initialize aes encryption");
8f2528
+		EVP_CIPHER_CTX_set_padding(&c->ecbctx, 0);
8f2528
+	}
8f2528
 	if (iv != NULL)
8f2528
 		memcpy(c->aes_counter, iv, AES_BLOCK_SIZE);
8f2528
 	return (1);
8f2528
@@ -105,6 +145,7 @@ ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
8f2528
 	struct ssh_aes_ctr_ctx *c;
8f2528
 
8f2528
 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
8f2528
+		EVP_CIPHER_CTX_cleanup(&c->ecbctx);
8f2528
 		memset(c, 0, sizeof(*c));
8f2528
 		free(c);
8f2528
 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);