bmh10 / rpms / openssh

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