kentpeacock / rpms / openssh

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