]> andersk Git - gssapi-openssh.git/blobdiff - openssh/cipher.c
o Merge to OPENSSH_3_6_1P1_GSSAPI_20030408.
[gssapi-openssh.git] / openssh / cipher.c
index 821543a66ecacef148d59536913e21fb2cacb896..b5d38747ed8039124eb2a7ec61ee1f8db95da621 100644 (file)
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: cipher.c,v 1.54 2002/03/19 10:49:35 markus Exp $");
+RCSID("$OpenBSD: cipher.c,v 1.62 2002/11/21 22:45:31 markus Exp $");
 
 #include "xmalloc.h"
 #include "log.h"
 #include "cipher.h"
 
 #include <openssl/md5.h>
-#include "rijndael.h"
 
-static EVP_CIPHER *evp_ssh1_3des(void);
-static EVP_CIPHER *evp_ssh1_bf(void);
-static EVP_CIPHER *evp_rijndael(void);
+#if OPENSSL_VERSION_NUMBER < 0x00906000L
+#define SSH_OLD_EVP
+#define EVP_CIPHER_CTX_get_app_data(e)          ((e)->app_data)
+#endif
+
+#if OPENSSL_VERSION_NUMBER < 0x00907000L
+#include "rijndael.h"
+static const EVP_CIPHER *evp_rijndael(void);
+#endif
+static const EVP_CIPHER *evp_ssh1_3des(void);
+static const EVP_CIPHER *evp_ssh1_bf(void);
 
 struct Cipher {
        char    *name;
        int     number;         /* for ssh1 only */
        u_int   block_size;
        u_int   key_len;
-       EVP_CIPHER      *(*evptype)(void);
+       const EVP_CIPHER        *(*evptype)(void);
 } ciphers[] = {
        { "none",               SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
        { "des",                SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
@@ -64,9 +71,19 @@ struct Cipher {
        { "blowfish-cbc",       SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
        { "cast128-cbc",        SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
        { "arcfour",            SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
+#if OPENSSL_VERSION_NUMBER < 0x00907000L
        { "aes128-cbc",         SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
        { "aes192-cbc",         SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
        { "aes256-cbc",         SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
+       { "rijndael-cbc@lysator.liu.se",
+                               SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
+#else
+       { "aes128-cbc",         SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc },
+       { "aes192-cbc",         SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc },
+       { "aes256-cbc",         SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
+       { "rijndael-cbc@lysator.liu.se",
+                               SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
+#endif
 
        { NULL,                 SSH_CIPHER_ILLEGAL, 0, 0, NULL }
 };
@@ -78,11 +95,13 @@ cipher_blocksize(Cipher *c)
 {
        return (c->block_size);
 }
+
 u_int
 cipher_keylen(Cipher *c)
 {
        return (c->key_len);
 }
+
 u_int
 cipher_get_number(Cipher *c)
 {
@@ -176,7 +195,11 @@ cipher_init(CipherContext *cc, Cipher *cipher,
     int encrypt)
 {
        static int dowarn = 1;
+#ifdef SSH_OLD_EVP
+       EVP_CIPHER *type;
+#else
        const EVP_CIPHER *type;
+#endif
        int klen;
 
        if (cipher->number == SSH_CIPHER_DES) {
@@ -201,13 +224,22 @@ cipher_init(CipherContext *cc, Cipher *cipher,
        type = (*cipher->evptype)();
 
        EVP_CIPHER_CTX_init(&cc->evp);
+#ifdef SSH_OLD_EVP
+       if (type->key_len > 0 && type->key_len != keylen) {
+               debug("cipher_init: set keylen (%d -> %d)",
+                   type->key_len, keylen);
+               type->key_len = keylen;
+       }
+       EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv,
+           (encrypt == CIPHER_ENCRYPT));
+#else
        if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
            (encrypt == CIPHER_ENCRYPT)) == 0)
                fatal("cipher_init: EVP_CipherInit failed for %s",
                    cipher->name);
        klen = EVP_CIPHER_CTX_key_length(&cc->evp);
        if (klen > 0 && keylen != klen) {
-               debug("cipher_init: set keylen (%d -> %d)", klen, keylen);
+               debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
                if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
                        fatal("cipher_init: set keylen failed (%d -> %d)",
                            klen, keylen);
@@ -215,6 +247,7 @@ cipher_init(CipherContext *cc, Cipher *cipher,
        if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
                fatal("cipher_init: EVP_CipherInit: set key failed for %s",
                    cipher->name);
+#endif
 }
 
 void
@@ -222,15 +255,23 @@ cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
 {
        if (len % cc->cipher->block_size)
                fatal("cipher_encrypt: bad plaintext length %d", len);
+#ifdef SSH_OLD_EVP
+       EVP_Cipher(&cc->evp, dest, (u_char *)src, len);
+#else
        if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
                fatal("evp_crypt: EVP_Cipher failed");
+#endif
 }
 
 void
 cipher_cleanup(CipherContext *cc)
 {
+#ifdef SSH_OLD_EVP
+       EVP_CIPHER_CTX_cleanup(&cc->evp);
+#else
        if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
                error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
+#endif
 }
 
 /*
@@ -275,6 +316,7 @@ struct ssh1_3des_ctx
 {
        EVP_CIPHER_CTX  k1, k2, k3;
 };
+
 static int
 ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
     int enc)
@@ -301,6 +343,11 @@ ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
        EVP_CIPHER_CTX_init(&c->k1);
        EVP_CIPHER_CTX_init(&c->k2);
        EVP_CIPHER_CTX_init(&c->k3);
+#ifdef SSH_OLD_EVP
+       EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
+       EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
+       EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
+#else
        if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
            EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
            EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
@@ -309,8 +356,10 @@ ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
                EVP_CIPHER_CTX_set_app_data(ctx, NULL);
                return (0);
        }
+#endif
        return (1);
 }
+
 static int
 ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
 {
@@ -320,12 +369,19 @@ ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
                error("ssh1_3des_cbc: no context");
                return (0);
        }
+#ifdef SSH_OLD_EVP
+       EVP_Cipher(&c->k1, dest, (u_char *)src, len);
+       EVP_Cipher(&c->k2, dest, dest, len);
+       EVP_Cipher(&c->k3, dest, dest, len);
+#else
        if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
            EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
            EVP_Cipher(&c->k3, dest, dest, len) == 0)
                return (0);
+#endif
        return (1);
 }
+
 static int
 ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
 {
@@ -338,7 +394,8 @@ ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
        }
        return (1);
 }
-static EVP_CIPHER *
+
+static const EVP_CIPHER *
 evp_ssh1_3des(void)
 {
        static EVP_CIPHER ssh1_3des;
@@ -351,7 +408,9 @@ evp_ssh1_3des(void)
        ssh1_3des.init = ssh1_3des_init;
        ssh1_3des.cleanup = ssh1_3des_cleanup;
        ssh1_3des.do_cipher = ssh1_3des_cbc;
+#ifndef SSH_OLD_EVP
        ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
+#endif
        return (&ssh1_3des);
 }
 
@@ -377,7 +436,21 @@ swap_bytes(const u_char *src, u_char *dst, int n)
                *dst++ = c[3];
        }
 }
+
+#ifdef SSH_OLD_EVP
+static void bf_ssh1_init (EVP_CIPHER_CTX * ctx, const unsigned char *key,
+                         const unsigned char *iv, int enc)
+{
+       if (iv != NULL)
+               memcpy (&(ctx->oiv[0]), iv, 8);
+       memcpy (&(ctx->iv[0]), &(ctx->oiv[0]), 8);
+       if (key != NULL)
+               BF_set_key (&(ctx->c.bf_ks), EVP_CIPHER_CTX_key_length (ctx),
+                           key);
+}
+#endif
 static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL;
+
 static int
 bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
 {
@@ -388,7 +461,8 @@ bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len)
        swap_bytes(out, out, len);
        return (ret);
 }
-static EVP_CIPHER *
+
+static const EVP_CIPHER *
 evp_ssh1_bf(void)
 {
        static EVP_CIPHER ssh1_bf;
@@ -396,11 +470,15 @@ evp_ssh1_bf(void)
        memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER));
        orig_bf = ssh1_bf.do_cipher;
        ssh1_bf.nid = NID_undef;
+#ifdef SSH_OLD_EVP
+       ssh1_bf.init = bf_ssh1_init;
+#endif
        ssh1_bf.do_cipher = bf_ssh1_cipher;
        ssh1_bf.key_len = 32;
        return (&ssh1_bf);
 }
 
+#if OPENSSL_VERSION_NUMBER < 0x00907000L
 /* RIJNDAEL */
 #define RIJNDAEL_BLOCKSIZE 16
 struct ssh_rijndael_ctx
@@ -429,6 +507,7 @@ ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
                memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
        return (1);
 }
+
 static int
 ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
     u_int len)
@@ -474,6 +553,7 @@ ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
        }
        return (1);
 }
+
 static int
 ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
 {
@@ -486,7 +566,8 @@ ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
        }
        return (1);
 }
-static EVP_CIPHER *
+
+static const EVP_CIPHER *
 evp_rijndael(void)
 {
        static EVP_CIPHER rijndal_cbc;
@@ -499,10 +580,13 @@ evp_rijndael(void)
        rijndal_cbc.init = ssh_rijndael_init;
        rijndal_cbc.cleanup = ssh_rijndael_cleanup;
        rijndal_cbc.do_cipher = ssh_rijndael_cbc;
+#ifndef SSH_OLD_EVP
        rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
-           EVP_CIPH_ALWAYS_CALL_INIT;
+           EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
+#endif
        return (&rijndal_cbc);
 }
+#endif
 
 /*
  * Exports an IV from the CipherContext required to export the key
@@ -538,35 +622,38 @@ cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
                if (evplen == 0)
                        return;
                if (evplen != len)
-                       fatal("cipher_get_keyiv: wrong iv length %d != %d",
+                       fatal("%s: wrong iv length %d != %d", __func__,
                            evplen, len);
 
-               if (strncmp(c->name, "aes", 3) == 0) {
+#if OPENSSL_VERSION_NUMBER < 0x00907000L
+               if (c->evptype == evp_rijndael) {
                        struct ssh_rijndael_ctx *aesc;
 
                        aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
                        if (aesc == NULL)
-                               fatal("cipher_get_keyiv: no rijndael context");
+                               fatal("%s: no rijndael context", __func__);
                        civ = aesc->r_iv;
-               } else {
+               } else
+#endif
+               {
                        civ = cc->evp.iv;
                }
                break;
        case SSH_CIPHER_3DES: {
                struct ssh1_3des_ctx *desc;
                if (len != 24)
-                       fatal("cipher_get_keyiv: bad 3des iv length: %d", len);
+                       fatal("%s: bad 3des iv length: %d", __func__, len);
                desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
                if (desc == NULL)
-                       fatal("cipher_get_keyiv: no 3des context");
-               debug3("cipher_get_keyiv: Copying 3DES IV");
+                       fatal("%s: no 3des context", __func__);
+               debug3("%s: Copying 3DES IV", __func__);
                memcpy(iv, desc->k1.iv, 8);
                memcpy(iv + 8, desc->k2.iv, 8);
                memcpy(iv + 16, desc->k3.iv, 8);
                return;
        }
        default:
-               fatal("cipher_get_keyiv: bad cipher %d", c->number);
+               fatal("%s: bad cipher %d", __func__, c->number);
        }
        memcpy(iv, civ, len);
 }
@@ -586,14 +673,17 @@ cipher_set_keyiv(CipherContext *cc, u_char *iv)
                if (evplen == 0)
                        return;
 
-               if (strncmp(c->name, "aes", 3) == 0) {
+#if OPENSSL_VERSION_NUMBER < 0x00907000L
+               if (c->evptype == evp_rijndael) {
                        struct ssh_rijndael_ctx *aesc;
 
                        aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
                        if (aesc == NULL)
-                               fatal("cipher_set_keyiv: no rijndael context");
+                               fatal("%s: no rijndael context", __func__);
                        div = aesc->r_iv;
-               }else {
+               } else
+#endif
+               {
                        div = cc->evp.iv;
                }
                break;
@@ -601,15 +691,15 @@ cipher_set_keyiv(CipherContext *cc, u_char *iv)
                struct ssh1_3des_ctx *desc;
                desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
                if (desc == NULL)
-                       fatal("cipher_set_keyiv: no 3des context");
-               debug3("cipher_set_keyiv: Installed 3DES IV");
+                       fatal("%s: no 3des context", __func__);
+               debug3("%s: Installed 3DES IV", __func__);
                memcpy(desc->k1.iv, iv, 8);
                memcpy(desc->k2.iv, iv + 8, 8);
                memcpy(desc->k3.iv, iv + 16, 8);
                return;
        }
        default:
-               fatal("cipher_set_keyiv: bad cipher %d", c->number);
+               fatal("%s: bad cipher %d", __func__, c->number);
        }
        memcpy(div, iv, evplen);
 }
@@ -626,28 +716,14 @@ int
 cipher_get_keycontext(CipherContext *cc, u_char *dat)
 {
        Cipher *c = cc->cipher;
-       int plen;
+       int plen = 0;
 
-       if (c->number == SSH_CIPHER_3DES) {
-               struct ssh1_3des_ctx *desc;
-               desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
-               if (desc == NULL)
-                       fatal("cipher_get_keycontext: no 3des context");
-               plen = EVP_X_STATE_LEN(desc->k1);
+       if (c->evptype == EVP_rc4) {
+               plen = EVP_X_STATE_LEN(cc->evp);
                if (dat == NULL)
-                       return (3*plen);
-               memcpy(dat, EVP_X_STATE(desc->k1), plen);
-               memcpy(dat + plen, EVP_X_STATE(desc->k2), plen);
-               memcpy(dat + 2*plen, EVP_X_STATE(desc->k3), plen);
-               return (3*plen);
+                       return (plen);
+               memcpy(dat, EVP_X_STATE(cc->evp), plen);
        }
-
-       /* Generic EVP */
-       plen = EVP_X_STATE_LEN(cc->evp);
-       if (dat == NULL)
-               return (plen);
-
-       memcpy(dat, EVP_X_STATE(cc->evp), plen);
        return (plen);
 }
 
@@ -657,16 +733,7 @@ cipher_set_keycontext(CipherContext *cc, u_char *dat)
        Cipher *c = cc->cipher;
        int plen;
 
-       if (c->number == SSH_CIPHER_3DES) {
-               struct ssh1_3des_ctx *desc;
-               desc = EVP_CIPHER_CTX_get_app_data(&cc->evp);
-               if (desc == NULL)
-                       fatal("cipher_set_keycontext: no 3des context");
-               plen = EVP_X_STATE_LEN(desc->k1);
-               memcpy(EVP_X_STATE(desc->k1), dat, plen);
-               memcpy(EVP_X_STATE(desc->k2), dat + plen, plen);
-               memcpy(EVP_X_STATE(desc->k3), dat + 2*plen, plen);
-       } else {
+       if (c->evptype == EVP_rc4) {
                plen = EVP_X_STATE_LEN(cc->evp);
                memcpy(EVP_X_STATE(cc->evp), dat, plen);
        }
This page took 0.051091 seconds and 4 git commands to generate.