cipher.c revision cd7d5faf5bbb52336a6f85578a90b31a648ac3fa
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
* All rights reserved
*
* As far as I am concerned, the code I have written for this software
* can be used freely for any purpose. Any derived versions of this
* software must be clearly marked as such, and if the derived work is
* incompatible with the protocol description in the RFC file, it must be
* called by a name other than "ssh" or "Secure Shell".
*
*
* Copyright (c) 1999 Niels Provos. All rights reserved.
* Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include "includes.h"
#include "xmalloc.h"
#include "log.h"
#include "cipher.h"
#if OPENSSL_VERSION_NUMBER < 0x00906000L
#define SSH_OLD_EVP
#define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data)
#endif
/*
* Symmetric ciphers can be offloaded to any engine through the EVP API only.
* However, OpenSSL doesn't offer AES in counter mode through EVP. So, we must
* define our own EVP functions.
*/
extern const EVP_CIPHER *evp_aes_128_ctr(void);
extern const EVP_CIPHER *evp_aes_192_ctr(void);
extern const EVP_CIPHER *evp_aes_256_ctr(void);
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 */
const EVP_CIPHER *(*evptype)(void);
} ciphers[] = {
#ifdef SOLARIS_SSH_ENABLE_CAST5_128
#endif /* SOLARIS_SSH_ENABLE_CAST5_128 */
};
/*--*/
{
return (c->block_size);
}
cipher_keylen(Cipher *c)
{
return (c->key_len);
}
{
return (c->number);
}
cipher_mask_ssh1(int client)
{
if (client) {
}
return mask;
}
Cipher *
cipher_by_name(const char *name)
{
Cipher *c;
return c;
return NULL;
}
Cipher *
cipher_by_number(int id)
{
Cipher *c;
return c;
return NULL;
}
#define CIPHER_SEP ","
int
ciphers_valid(const char *names)
{
Cipher *c;
char *p;
return 0;
c = cipher_by_name(p);
return 0;
} else {
}
}
return 1;
}
/*
* Parses the name of the cipher. Returns the number of the corresponding
* cipher, or -1 on error.
*/
int
cipher_number(const char *name)
{
Cipher *c;
return -1;
c = cipher_by_name(name);
}
char *
cipher_name(int id)
{
}
void
int encrypt)
{
static int dowarn = 1;
#ifdef SSH_OLD_EVP
#else
const EVP_CIPHER *type;
#endif
int klen;
if (dowarn) {
error("Warning: use of DES is strongly discouraged "
"due to cryptographic weaknesses");
dowarn = 0;
}
if (keylen > 8)
keylen = 8;
}
fatal("cipher_init: key length %d is insufficient for %s.",
fatal("cipher_init: iv length %d is insufficient for %s.",
#ifdef SSH_OLD_EVP
debug("cipher_init: set keylen (%d -> %d)",
}
(encrypt == CIPHER_ENCRYPT));
#else
(encrypt == CIPHER_ENCRYPT)) == 0)
fatal("cipher_init: EVP_CipherInit failed for %s",
fatal("cipher_init: set keylen failed (%d -> %d)",
}
fatal("cipher_init: EVP_CipherInit: set key failed for %s",
#endif
}
void
{
#ifdef SSH_OLD_EVP
#else
fatal("evp_crypt: EVP_Cipher failed");
#endif
}
void
{
#ifdef SSH_OLD_EVP
#else
error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
#endif
}
/*
* Selects the cipher, and keys if by computing the MD5 checksum of the
* passphrase and using the resulting 16 bytes as the key.
*/
void
const char *passphrase, int encrypt)
{
}
/* Implementations for other non-EVP ciphers */
/*
* This is used by SSH1:
*
* What kind of triple DES are these 2 routines?
*
* Why is there a redundant initialization vector?
*
* If only iv3 was used, then, this would till effect have been
* outer-cbc. However, there is also a private iv1 == iv2 which
* perhaps makes differential analysis easier. On the other hand, the
* private iv1 probably makes the CRC-32 attack ineffective. This is a
* result of that there is no longer any known iv1 to use when
* choosing the X block.
*/
struct ssh1_3des_ctx
{
};
static int
int enc)
{
struct ssh1_3des_ctx *c;
c = xmalloc(sizeof(*c));
}
return (1);
if (enc == -1)
k2 += 8;
if (enc)
k3 += 16;
else
k1 += 16;
}
EVP_CIPHER_CTX_init(&c->k1);
EVP_CIPHER_CTX_init(&c->k2);
EVP_CIPHER_CTX_init(&c->k3);
#ifdef SSH_OLD_EVP
#else
memset(c, 0, sizeof(*c));
xfree(c);
return (0);
}
#endif
return (1);
}
static int
{
struct ssh1_3des_ctx *c;
error("ssh1_3des_cbc: no context");
return (0);
}
#ifdef SSH_OLD_EVP
#else
return (0);
#endif
return (1);
}
static int
{
struct ssh1_3des_ctx *c;
memset(c, 0, sizeof(*c));
xfree(c);
}
return (1);
}
static const EVP_CIPHER *
evp_ssh1_3des(void)
{
static EVP_CIPHER ssh1_3des;
#ifndef SSH_OLD_EVP
#endif
return (&ssh1_3des);
}
/*
* SSH1 uses a variation on Blowfish, all bytes must be swapped before
* and after encryption/decryption. Thus the swap_bytes stuff (yuk).
*/
static void
{
u_char c[4];
/* Process 4 bytes every lap. */
for (n = n / 4; n > 0; n--) {
c[3] = *src++;
c[2] = *src++;
c[1] = *src++;
c[0] = *src++;
*dst++ = c[0];
*dst++ = c[1];
*dst++ = c[2];
*dst++ = c[3];
}
}
#ifdef SSH_OLD_EVP
{
key);
}
#endif
static int
{
int ret;
return (ret);
}
static const EVP_CIPHER *
evp_ssh1_bf(void)
{
static EVP_CIPHER ssh1_bf;
#ifdef SSH_OLD_EVP
#endif
return (&ssh1_bf);
}
#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
/* RIJNDAEL */
#define RIJNDAEL_BLOCKSIZE 16
struct ssh_rijndael_ctx
{
};
static int
int enc)
{
struct ssh_rijndael_ctx *c;
c = xmalloc(sizeof(*c));
}
if (enc == -1)
}
return (1);
}
static int
{
struct ssh_rijndael_ctx *c;
if (len == 0)
return (1);
if (len % RIJNDAEL_BLOCKSIZE)
error("ssh_rijndael_cbc: no context");
return (0);
}
for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
}
} else {
for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
}
}
return (1);
}
static int
{
struct ssh_rijndael_ctx *c;
memset(c, 0, sizeof(*c));
xfree(c);
}
return (1);
}
static const EVP_CIPHER *
evp_rijndael(void)
{
static EVP_CIPHER rijndal_cbc;
#ifndef SSH_OLD_EVP
#endif
return (&rijndal_cbc);
}
#endif
/*
* Exports an IV from the CipherContext required to export the key
* state back from the unprivileged child to the privileged parent
* process.
*/
int
{
int ivlen;
if (c->number == SSH_CIPHER_3DES)
ivlen = 24;
else
return (ivlen);
}
void
{
int evplen;
switch (c->number) {
case SSH_CIPHER_SSH2:
case SSH_CIPHER_DES:
case SSH_CIPHER_BLOWFISH:
if (evplen == 0)
return;
#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
if (c->evptype == evp_rijndael) {
struct ssh_rijndael_ctx *aesc;
} else
#endif
if (c->evptype == evp_aes_128_ctr) {
return;
} else {
}
break;
case SSH_CIPHER_3DES: {
struct ssh1_3des_ctx *desc;
if (len != 24)
return;
}
default:
}
}
void
{
int evplen = 0;
switch (c->number) {
case SSH_CIPHER_SSH2:
case SSH_CIPHER_DES:
case SSH_CIPHER_BLOWFISH:
if (evplen == 0)
return;
#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
if (c->evptype == evp_rijndael) {
struct ssh_rijndael_ctx *aesc;
} else
#endif
if (c->evptype == evp_aes_128_ctr) {
return;
} else {
}
break;
case SSH_CIPHER_3DES: {
struct ssh1_3des_ctx *desc;
return;
}
default:
}
}
#if OPENSSL_VERSION_NUMBER < 0x00907000L
#else
#endif
int
{
int plen = 0;
return (plen);
}
return (plen);
}
void
{
int plen;
}
}