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