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

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