rcolebaugh / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone

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

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