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