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

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