vishalmishra434 / rpms / openssh

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