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