kentpeacock / rpms / openssh

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