Blame src/mw_cipher.h

Packit 16808d
Packit 16808d
/*
Packit 16808d
  Meanwhile - Unofficial Lotus Sametime Community Client Library
Packit 16808d
  Copyright (C) 2004  Christopher (siege) O'Brien
Packit 16808d
  
Packit 16808d
  This library is free software; you can redistribute it and/or
Packit 16808d
  modify it under the terms of the GNU Library General Public
Packit 16808d
  License as published by the Free Software Foundation; either
Packit 16808d
  version 2 of the License, or (at your option) any later version.
Packit 16808d
  
Packit 16808d
  This library is distributed in the hope that it will be useful,
Packit 16808d
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 16808d
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 16808d
  Library General Public License for more details.
Packit 16808d
  
Packit 16808d
  You should have received a copy of the GNU Library General Public
Packit 16808d
  License along with this library; if not, write to the Free
Packit 16808d
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 16808d
*/
Packit 16808d
Packit 16808d
#ifndef _MW_CIPHER_H
Packit 16808d
#define _MW_CIPHER_H
Packit 16808d
Packit 16808d
Packit 16808d
#include <glib.h>
Packit 16808d
#include "mw_common.h"
Packit 16808d
Packit 16808d
Packit 16808d
#ifdef __cplusplus
Packit 16808d
extern "C" {
Packit 16808d
#endif
Packit 16808d
Packit 16808d
Packit 16808d
/* place-holders */
Packit 16808d
struct mwChannel;
Packit 16808d
struct mwSession;
Packit 16808d
Packit 16808d
Packit 16808d
/** @enum mwCipherType
Packit 16808d
    Common cipher types */
Packit 16808d
enum mwCipherType {
Packit 16808d
  mwCipher_RC2_40   = 0x0000,
Packit 16808d
  mwCipher_RC2_128  = 0x0001,
Packit 16808d
};
Packit 16808d
Packit 16808d
Packit 16808d
struct mwCipher;
Packit 16808d
struct mwCipherInstance;
Packit 16808d
Packit 16808d
Packit 16808d
/** Obtain an instance of a given cipher, which can be used for the
Packit 16808d
    processing of a single channel. */
Packit 16808d
typedef struct mwCipherInstance *(*mwCipherInstantiator)
Packit 16808d
     (struct mwCipher *cipher, struct mwChannel *chan);
Packit 16808d
Packit 16808d
Packit 16808d
/** Process (encrypt or decrypt, depending) the given data. The passed
Packit 16808d
    buffer may be freed in processing and be replaced with a freshly
Packit 16808d
    allocated buffer. The post-processed buffer must in turn be freed
Packit 16808d
    after use */
Packit 16808d
typedef int (*mwCipherProcessor)
Packit 16808d
     (struct mwCipherInstance *ci, struct mwOpaque *data);
Packit 16808d
Packit 16808d
Packit 16808d
/** A cipher. Ciphers are primarily used to provide cipher instances
Packit 16808d
    for bi-directional encryption on channels, but some may be used
Packit 16808d
    for other activities. Expand upon this structure to create a
Packit 16808d
    custom encryption provider.
Packit 16808d
    @see mwCipherInstance */
Packit 16808d
struct mwCipher {
Packit 16808d
Packit 16808d
  /** service this cipher is providing for
Packit 16808d
      @see mwCipher_getSession */
Packit 16808d
  struct mwSession *session;
Packit 16808d
Packit 16808d
  guint16 type;               /**< @see mwCipher_getType */
Packit 16808d
  const char *(*get_name)(void);  /**< @see mwCipher_getName */
Packit 16808d
  const char *(*get_desc)(void);  /**< @see mwCipher_getDesc */
Packit 16808d
Packit 16808d
  /** Generate a new Cipher Instance for use on a channel
Packit 16808d
      @see mwCipher_newInstance */
Packit 16808d
  mwCipherInstantiator new_instance;
Packit 16808d
Packit 16808d
  void (*offered)(struct mwCipherInstance *ci, struct mwEncryptItem *item);
Packit 16808d
  struct mwEncryptItem *(*offer)(struct mwCipherInstance *ci);
Packit 16808d
  void (*accepted)(struct mwCipherInstance *ci, struct mwEncryptItem *item);
Packit 16808d
  struct mwEncryptItem *(*accept)(struct mwCipherInstance *ci);
Packit 16808d
Packit 16808d
  mwCipherProcessor encrypt; /**< @see mwCipherInstance_encrypt */
Packit 16808d
  mwCipherProcessor decrypt; /**< @see mwCipherInstance_decrypt */
Packit 16808d
Packit 16808d
  /** prepare this cipher for being free'd
Packit 16808d
      @see mwCipher_free */
Packit 16808d
  void (*clear)(struct mwCipher *c);
Packit 16808d
Packit 16808d
  /** clean up a cipher instance before being free'd
Packit 16808d
      @see mwCipherInstance_free */
Packit 16808d
  void (*clear_instance)(struct mwCipherInstance *ci);
Packit 16808d
};
Packit 16808d
Packit 16808d
Packit 16808d
/** An instance of a cipher. Expand upon this structure to contain
Packit 16808d
    necessary state data
Packit 16808d
    @see mwCipher */
Packit 16808d
struct mwCipherInstance {
Packit 16808d
Packit 16808d
  /** the parent cipher.
Packit 16808d
      @see mwCipherInstance_getCipher */
Packit 16808d
  struct mwCipher *cipher;
Packit 16808d
Packit 16808d
  /** the channel this instances processes
Packit 16808d
      @see mwCipherInstance_getChannel */
Packit 16808d
  struct mwChannel *channel;
Packit 16808d
};
Packit 16808d
Packit 16808d
Packit 16808d
struct mwCipher *mwCipher_new_RC2_40(struct mwSession *s);
Packit 16808d
Packit 16808d
Packit 16808d
struct mwCipher *mwCipher_new_RC2_128(struct mwSession *s);
Packit 16808d
Packit 16808d
Packit 16808d
struct mwSession *mwCipher_getSession(struct mwCipher *cipher);
Packit 16808d
Packit 16808d
Packit 16808d
guint16 mwCipher_getType(struct mwCipher *cipher);
Packit 16808d
Packit 16808d
Packit 16808d
const char *mwCipher_getName(struct mwCipher *cipher);
Packit 16808d
Packit 16808d
Packit 16808d
const char *mwCipher_getDesc(struct mwCipher *cipher);
Packit 16808d
Packit 16808d
Packit 16808d
struct mwCipherInstance *mwCipher_newInstance(struct mwCipher *cipher,
Packit 16808d
					      struct mwChannel *channel);
Packit 16808d
Packit 16808d
Packit 16808d
/** destroy a cipher */
Packit 16808d
void mwCipher_free(struct mwCipher* cipher);
Packit 16808d
Packit 16808d
Packit 16808d
/** reference the parent cipher of an instance */
Packit 16808d
struct mwCipher *mwCipherInstance_getCipher(struct mwCipherInstance *ci);
Packit 16808d
Packit 16808d
Packit 16808d
/** reference the channel a cipher instance is attached to */
Packit 16808d
struct mwChannel *mwCipherInstance_getChannel(struct mwCipherInstance *ci);
Packit 16808d
Packit 16808d
Packit 16808d
/** Indicates a cipher has been offered to our channel */
Packit 16808d
void mwCipherInstance_offered(struct mwCipherInstance *ci,
Packit 16808d
			      struct mwEncryptItem *item);
Packit 16808d
Packit 16808d
Packit 16808d
/** Offer a cipher */
Packit 16808d
struct mwEncryptItem *
Packit 16808d
mwCipherInstance_offer(struct mwCipherInstance *ci);
Packit 16808d
Packit 16808d
Packit 16808d
/** Indicates an offered cipher has been accepted */
Packit 16808d
void mwCipherInstance_accepted(struct mwCipherInstance *ci,
Packit 16808d
			       struct mwEncryptItem *item);
Packit 16808d
Packit 16808d
Packit 16808d
/** Accept a cipher offered to our channel */
Packit 16808d
struct mwEncryptItem *
Packit 16808d
mwCipherInstance_accept(struct mwCipherInstance *ci);
Packit 16808d
Packit 16808d
Packit 16808d
/** encrypt data */
Packit 16808d
int mwCipherInstance_encrypt(struct mwCipherInstance *ci,
Packit 16808d
			     struct mwOpaque *data);
Packit 16808d
Packit 16808d
Packit 16808d
/** decrypt data */
Packit 16808d
int mwCipherInstance_decrypt(struct mwCipherInstance *ci,
Packit 16808d
			     struct mwOpaque *data);
Packit 16808d
Packit 16808d
Packit 16808d
/** destroy a cipher instance */
Packit 16808d
void mwCipherInstance_free(struct mwCipherInstance *ci);
Packit 16808d
Packit 16808d
Packit 16808d
/**
Packit 16808d
  @section General Cipher Functions
Packit 16808d
Packit 16808d
  These functions are reused where encryption is necessary outside of
Packit 16808d
  a channel (eg. session authentication)
Packit 16808d
*/
Packit 16808d
/* @{ */
Packit 16808d
Packit 16808d
Packit 16808d
/** generate some pseudo-random bytes
Packit 16808d
    @param keylen  count of bytes to write into key
Packit 16808d
    @param key     buffer to write keys into
Packit 16808d
*/
Packit 16808d
void mwKeyRandom(guchar *key, gsize keylen);
Packit 16808d
Packit 16808d
Packit 16808d
/** Setup an Initialization Vector. IV must be at least 8 bytes */
Packit 16808d
void mwIV_init(guchar *iv);
Packit 16808d
Packit 16808d
Packit 16808d
/** Expand a variable-length key into a 128-byte key (represented as
Packit 16808d
    an an array of 64 ints) */
Packit 16808d
void mwKeyExpand(int *ekey, const guchar *key, gsize keylen);
Packit 16808d
Packit 16808d
Packit 16808d
/** Encrypt data using an already-expanded key */
Packit 16808d
void mwEncryptExpanded(const int *ekey, guchar *iv,
Packit 16808d
		       struct mwOpaque *in,
Packit 16808d
		       struct mwOpaque *out);
Packit 16808d
Packit 16808d
Packit 16808d
/** Encrypt data using an expanded form of the given key */
Packit 16808d
void mwEncrypt(const guchar *key, gsize keylen, guchar *iv,
Packit 16808d
	       struct mwOpaque *in, struct mwOpaque *out);
Packit 16808d
Packit 16808d
Packit 16808d
/** Decrypt data using an already expanded key */
Packit 16808d
void mwDecryptExpanded(const int *ekey, guchar *iv,
Packit 16808d
		       struct mwOpaque *in,
Packit 16808d
		       struct mwOpaque *out);
Packit 16808d
Packit 16808d
Packit 16808d
/** Decrypt data using an expanded form of the given key */
Packit 16808d
void mwDecrypt(const guchar *key, gsize keylen, guchar *iv,
Packit 16808d
	       struct mwOpaque *in, struct mwOpaque *out);
Packit 16808d
Packit 16808d
Packit 16808d
/* @} */
Packit 16808d
Packit 16808d
Packit 16808d
/**
Packit 16808d
  @section Diffie-Hellman Functions
Packit 16808d
Packit 16808d
  These functions are reused where DH Key negotiation is necessary
Packit 16808d
  outside of a channel (eg. session authentication). These are
Packit 16808d
  wrapping a full multiple-precision integer math library, but most of
Packit 16808d
  the functionality there-of is not exposed. Currently, the math is
Packit 16808d
  provided by a copy of the public domain libmpi.
Packit 16808d
Packit 16808d
  for more information on the used MPI Library, visit
Packit 16808d
  http://www.cs.dartmouth.edu/~sting/mpi/
Packit 16808d
*/
Packit 16808d
/* @{ */
Packit 16808d
Packit 16808d
Packit 16808d
/** @struct mwMpi */
Packit 16808d
struct mwMpi;
Packit 16808d
Packit 16808d
Packit 16808d
/** prepare a new mpi value */
Packit 16808d
struct mwMpi *mwMpi_new(void);
Packit 16808d
Packit 16808d
Packit 16808d
/** destroy an mpi value */
Packit 16808d
void mwMpi_free(struct mwMpi *i);
Packit 16808d
Packit 16808d
Packit 16808d
/** Import a value from an opaque */
Packit 16808d
void mwMpi_import(struct mwMpi *i, struct mwOpaque *o);
Packit 16808d
Packit 16808d
Packit 16808d
/** Export a value into an opaque */
Packit 16808d
void mwMpi_export(struct mwMpi *i, struct mwOpaque *o);
Packit 16808d
Packit 16808d
Packit 16808d
/** set a big integer to the Sametime Prime value */
Packit 16808d
void mwMpi_setDHPrime(struct mwMpi *i);
Packit 16808d
Packit 16808d
Packit 16808d
/** set a big integer to the Sametime Base value */
Packit 16808d
void mwMpi_setDHBase(struct mwMpi *i);
Packit 16808d
Packit 16808d
Packit 16808d
/** sets private to a randomly generated value, and calculates public
Packit 16808d
    using the Sametime Prime and Base */
Packit 16808d
void mwMpi_randDHKeypair(struct mwMpi *private_key, struct mwMpi *public_key);
Packit 16808d
Packit 16808d
Packit 16808d
/** sets the shared key value based on the remote and private keys,
Packit 16808d
    using the Sametime Prime and Base */
Packit 16808d
void mwMpi_calculateDHShared(struct mwMpi *shared_key, struct mwMpi *remote_key,
Packit 16808d
			     struct mwMpi *private_key);
Packit 16808d
Packit 16808d
Packit 16808d
/* @} */
Packit 16808d
Packit 16808d
Packit 16808d
#ifdef __cplusplus
Packit 16808d
}
Packit 16808d
#endif
Packit 16808d
Packit 16808d
Packit 16808d
#endif /* _MW_CIPHER_H */