Skip to content

Commit

Permalink
- [email protected] 2002/02/14 23:41:01
Browse files Browse the repository at this point in the history
     [authfile.c cipher.c cipher.h kex.c kex.h packet.c]
     hide some more implementation details of cipher.[ch] and prepares for move
     to EVP, ok deraadt@
  • Loading branch information
djmdjm committed Feb 19, 2002
1 parent 19a5945 commit 963f6b2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 70 deletions.
6 changes: 5 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
[channels.h session.c ssh.c]
increase the SSH v2 window size to 4 packets. comsumes a little
bit more memory for slow receivers but increases througput.
- [email protected] 2002/02/14 23:41:01
[authfile.c cipher.c cipher.h kex.c kex.h packet.c]
hide some more implementation details of cipher.[ch] and prepares for move
to EVP, ok deraadt@

20020218
- (tim) newer config.guess from ftp://ftp.gnu.org/gnu/config/config.guess
Expand Down Expand Up @@ -7610,4 +7614,4 @@
- Wrote replacements for strlcpy and mkdtemp
- Released 1.0pre1

$Id: ChangeLog,v 1.1857 2002/02/19 04:20:57 djm Exp $
$Id: ChangeLog,v 1.1858 2002/02/19 04:21:23 djm Exp $
26 changes: 14 additions & 12 deletions authfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/

#include "includes.h"
RCSID("$OpenBSD: authfile.c,v 1.45 2001/12/29 21:56:01 stevesk Exp $");
RCSID("$OpenBSD: authfile.c,v 1.46 2002/02/14 23:41:01 markus Exp $");

#include <openssl/err.h>
#include <openssl/evp.h>
Expand Down Expand Up @@ -69,7 +69,7 @@ key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
{
Buffer buffer, encrypted;
u_char buf[100], *cp;
int fd, i;
int fd, i, cipher_num;
CipherContext ciphercontext;
Cipher *cipher;
u_int32_t rand;
Expand All @@ -78,11 +78,9 @@ key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
* to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
*/
if (strcmp(passphrase, "") == 0)
cipher = cipher_by_number(SSH_CIPHER_NONE);
else
cipher = cipher_by_number(SSH_AUTHFILE_CIPHER);
if (cipher == NULL)
cipher_num = (strcmp(passphrase, "") == 0) ?
SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
if ((cipher = cipher_by_number(cipher_num)) == NULL)
fatal("save_private_key_rsa: bad cipher");

/* This buffer is used to built the secret part of the private key. */
Expand Down Expand Up @@ -119,7 +117,7 @@ key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
buffer_put_char(&encrypted, 0);

/* Store cipher type. */
buffer_put_char(&encrypted, cipher->number);
buffer_put_char(&encrypted, cipher_num);
buffer_put_int(&encrypted, 0); /* For future extension */

/* Store public key. This will be in plain text. */
Expand All @@ -131,9 +129,11 @@ key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
/* Allocate space for the private part of the key in the buffer. */
cp = buffer_append_space(&encrypted, buffer_len(&buffer));

cipher_set_key_string(&ciphercontext, cipher, passphrase);
cipher_encrypt(&ciphercontext, cp,
cipher_set_key_string(&ciphercontext, cipher, passphrase,
CIPHER_ENCRYPT);
cipher_crypt(&ciphercontext, cp,
buffer_ptr(&buffer), buffer_len(&buffer));
cipher_cleanup(&ciphercontext);
memset(&ciphercontext, 0, sizeof(ciphercontext));

/* Destroy temporary data. */
Expand Down Expand Up @@ -380,9 +380,11 @@ key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
cp = buffer_append_space(&decrypted, buffer_len(&buffer));

/* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
cipher_set_key_string(&ciphercontext, cipher, passphrase);
cipher_decrypt(&ciphercontext, cp,
cipher_set_key_string(&ciphercontext, cipher, passphrase,
CIPHER_DECRYPT);
cipher_crypt(&ciphercontext, cp,
buffer_ptr(&buffer), buffer_len(&buffer));
cipher_cleanup(&ciphercontext);
memset(&ciphercontext, 0, sizeof(ciphercontext));
buffer_free(&buffer);

Expand Down
47 changes: 36 additions & 11 deletions cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@
*/

#include "includes.h"
RCSID("$OpenBSD: cipher.c,v 1.50 2002/01/21 22:30:12 markus Exp $");
RCSID("$OpenBSD: cipher.c,v 1.51 2002/02/14 23:41:01 markus Exp $");

#include "xmalloc.h"
#include "log.h"
#include "cipher.h"

#include <openssl/md5.h>

struct Cipher {
char *name;
int number; /* for ssh1 only */
u_int block_size;
u_int key_len;
void (*setkey)(CipherContext *, const u_char *, u_int);
void (*setiv)(CipherContext *, const u_char *, u_int);
void (*encrypt)(CipherContext *, u_char *, const u_char *, u_int);
void (*decrypt)(CipherContext *, u_char *, const u_char *, u_int);
};

/* no encryption */
static void
none_setkey(CipherContext *cc, const u_char *key, u_int keylen)
Expand Down Expand Up @@ -397,6 +408,18 @@ Cipher ciphers[] = {

/*--*/

u_int
cipher_blocksize(Cipher *c)
{
return (c->block_size);
}

u_int
cipher_keylen(Cipher *c)
{
return (c->key_len);
}

u_int
cipher_mask_ssh1(int client)
{
Expand Down Expand Up @@ -479,8 +502,8 @@ cipher_name(int id)
}

void
cipher_init(CipherContext *cc, Cipher *cipher,
const u_char *key, u_int keylen, const u_char *iv, u_int ivlen)
cipher_init(CipherContext *cc, Cipher *cipher, const u_char *key,
u_int keylen, const u_char *iv, u_int ivlen, int encrypt)
{
if (keylen < cipher->key_len)
fatal("cipher_init: key length %d is insufficient for %s.",
Expand All @@ -489,24 +512,26 @@ cipher_init(CipherContext *cc, Cipher *cipher,
fatal("cipher_init: iv length %d is insufficient for %s.",
ivlen, cipher->name);
cc->cipher = cipher;
cc->encrypt = (encrypt == CIPHER_ENCRYPT);
cipher->setkey(cc, key, keylen);
cipher->setiv(cc, iv, ivlen);
}

void
cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
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);
cc->cipher->encrypt(cc, dest, src, len);
if (cc->encrypt)
cc->cipher->encrypt(cc, dest, src, len);
else
cc->cipher->decrypt(cc, dest, src, len);
}

void
cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
cipher_cleanup(CipherContext *cc)
{
if (len % cc->cipher->block_size)
fatal("cipher_decrypt: bad ciphertext length %d", len);
cc->cipher->decrypt(cc, dest, src, len);
memset(cc, 0, sizeof(*cc));
}

/*
Expand All @@ -516,7 +541,7 @@ cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)

void
cipher_set_key_string(CipherContext *cc, Cipher *cipher,
const char *passphrase)
const char *passphrase, int encrypt)
{
MD5_CTX md;
u_char digest[16];
Expand All @@ -525,7 +550,7 @@ cipher_set_key_string(CipherContext *cc, Cipher *cipher,
MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
MD5_Final(digest, &md);

cipher_init(cc, cipher, digest, 16, NULL, 0);
cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt);

memset(digest, 0, sizeof(digest));
memset(&md, 0, sizeof(md));
Expand Down
29 changes: 13 additions & 16 deletions cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* RCSID("$OpenBSD: cipher.h,v 1.29 2001/08/23 11:31:59 markus Exp $"); */
/* RCSID("$OpenBSD: cipher.h,v 1.30 2002/02/14 23:41:01 markus Exp $"); */

#ifndef CIPHER_H
#define CIPHER_H
Expand All @@ -59,9 +59,13 @@
#define SSH_CIPHER_RESERVED 7
#define SSH_CIPHER_MAX 31

#define CIPHER_ENCRYPT 1
#define CIPHER_DECRYPT 0

typedef struct Cipher Cipher;
typedef struct CipherContext CipherContext;

struct Cipher;
struct CipherContext {
union {
struct {
Expand Down Expand Up @@ -91,18 +95,10 @@ struct CipherContext {
} rijndael;
RC4_KEY rc4;
} u;
int plaintext;
int encrypt;
Cipher *cipher;
};
struct Cipher {
char *name;
int number; /* for ssh1 only */
u_int block_size;
u_int key_len;
void (*setkey)(CipherContext *, const u_char *, u_int);
void (*setiv)(CipherContext *, const u_char *, u_int);
void (*encrypt)(CipherContext *, u_char *, const u_char *, u_int);
void (*decrypt)(CipherContext *, u_char *, const u_char *, u_int);
};

u_int cipher_mask_ssh1(int);
Cipher *cipher_by_name(const char *);
Expand All @@ -111,9 +107,10 @@ int cipher_number(const char *);
char *cipher_name(int);
int ciphers_valid(const char *);
void cipher_init(CipherContext *, Cipher *, const u_char *, u_int,
const u_char *, u_int);
void cipher_encrypt(CipherContext *, u_char *, const u_char *, u_int);
void cipher_decrypt(CipherContext *, u_char *, const u_char *, u_int);
void cipher_set_key_string(CipherContext *, Cipher *, const char *);

const u_char *, u_int, int);
void cipher_crypt(CipherContext *, u_char *, const u_char *, u_int);
void cipher_cleanup(CipherContext *);
void cipher_set_key_string(CipherContext *, Cipher *, const char *, int);
u_int cipher_blocksize(Cipher *);
u_int cipher_keylen(Cipher *);
#endif /* CIPHER_H */
15 changes: 8 additions & 7 deletions kex.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

#include "includes.h"
RCSID("$OpenBSD: kex.c,v 1.44 2002/02/11 16:10:15 markus Exp $");
RCSID("$OpenBSD: kex.c,v 1.45 2002/02/14 23:41:01 markus Exp $");

#include <openssl/crypto.h>

Expand Down Expand Up @@ -232,13 +232,14 @@ choose_enc(Enc *enc, char *client, char *server)
char *name = match_list(client, server, NULL);
if (name == NULL)
fatal("no matching cipher found: client %s server %s", client, server);
enc->cipher = cipher_by_name(name);
if (enc->cipher == NULL)
if ((enc->cipher = cipher_by_name(name)) == NULL)
fatal("matching cipher is not supported: %s", name);
enc->name = name;
enc->enabled = 0;
enc->iv = NULL;
enc->key = NULL;
enc->key_len = cipher_keylen(enc->cipher);
enc->block_size = cipher_blocksize(enc->cipher);
}
static void
choose_mac(Mac *mac, char *client, char *server)
Expand Down Expand Up @@ -341,10 +342,10 @@ kex_choose_conf(Kex *kex)
need = 0;
for (mode = 0; mode < MODE_MAX; mode++) {
newkeys = kex->newkeys[mode];
if (need < newkeys->enc.cipher->key_len)
need = newkeys->enc.cipher->key_len;
if (need < newkeys->enc.cipher->block_size)
need = newkeys->enc.cipher->block_size;
if (need < newkeys->enc.key_len)
need = newkeys->enc.key_len;
if (need < newkeys->enc.block_size)
need = newkeys->enc.block_size;
if (need < newkeys->mac.key_len)
need = newkeys->mac.key_len;
}
Expand Down
4 changes: 3 additions & 1 deletion kex.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: kex.h,v 1.28 2001/12/28 15:06:00 markus Exp $ */
/* $OpenBSD: kex.h,v 1.29 2002/02/14 23:41:01 markus Exp $ */

/*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Expand Down Expand Up @@ -71,6 +71,8 @@ struct Enc {
char *name;
Cipher *cipher;
int enabled;
u_int key_len;
u_int block_size;
u_char *key;
u_char *iv;
};
Expand Down
Loading

0 comments on commit 963f6b2

Please sign in to comment.