/* $Header: /smsa/bsd:include/des_crypt.h:bsd  1.5  $
 * $Source: /smsa/bsd:include/des_crypt.h: $
 *
 * Copyright (c) 1989 Acorn Computers Ltd.
 */
/*
 * @(#)des_crypt.h	1.1 88/04/01 4.0NFSSRC SMI;	from 1.4 88/02/08 (C) 1986 SMI
 *
 * des_crypt.h, des library routine interface
 * Copyright (C) 1986, Sun Microsystems, Inc.
 */
#ifndef __des_crypt_h
#define __des_crypt_h

/*
 * DES_MAXDATA is an arbitrary limit - in fact the interfaces will encrypt
 * or decrypt any amount of data in one call.
 */
#define DES_MAXDATA 8192	/* max bytes encrypted in one call */
#define DES_DIRMASK (1 << 0)
#define DES_ENCRYPT (0*DES_DIRMASK)	/* Encrypt */
#define DES_DECRYPT (1*DES_DIRMASK)	/* Decrypt */

/*
 * The ARM implementation is always a software one.
 */
#define DES_DEVMASK (1 << 1)
#define	DES_HW (0*DES_DEVMASK)	/* Use hardware device */ 
#define DES_SW (1*DES_DEVMASK)	/* Use software device */


#define DESERR_NONE 0	/* succeeded */
#define DESERR_NOHWDEVICE 1	/* succeeded, but hw device not available */
#define DESERR_HWERROR 2	/* failed, hardware/driver error */
#define DESERR_BADPARAM 3	/* failed, bad parameter to call */

#define DES_FAILED(err) ((err) > DESERR_NOHWDEVICE)

/*
 * cbc_crypt()
 * ecb_crypt()
 *
 * Encrypt (or decrypt) len bytes of a buffer buf.
 * The length must be a multiple of eight, the data must be aligned on
 * a word boundary (4 byte boundary).
 * Key parity is ignored - the parity bit is the low order bit of each
 * character.
 * ivec is the input vector, and is updated to the new one (cbc only).
 * The mode is created by oring together the appropriate parameters.
 * DESERR_NOHWDEVICE is returned if DES_HW was specified - no hardware
 * implementation is provided.  (The data is still encrypted or decrypted,
 * using the software implementation).
 */


/*
 * Cipher Block Chaining mode
 */
extern int cbc_crypt(
	char		/*key*/[8],	/* 8 byte key (56 bits, plus 8 bits parity */
	void		*/*buf*/,	/* Word aligned data */
	unsigned	/*length*/,	/* Multiple of 8 - length in bytes */
	int		/*mode*/,	/* Mode flag bits */
	char		/*ivec*/[8]	/* Effectively part of the key - chaining block */
);


/*
 * Electronic Code Book mode - use only if the data must be accessed randomly.
 */
extern int ecb_crypt(
	char		/*key*/[8],	/* 8 byte key */
	void		*/*buf*/,	/* Word aligned data */
	unsigned	/*length*/,	/* Multiple of 8 bytes */
	int		/*mode*/	/* Flags as above */
);


/* 
 * Set des parity for a key.
 * DES parity is odd and in the low bit of each byte.  The high
 * order bit of each byte is set to 0.
 */
extern void des_setparity(char /*key*/[8]);



/*
 * LOW LEVEL INTERFACES
 *
 * The following routines are specific to the ARM implementation.
 * They provide a slightly faster interface which does considerably
 * less checking - all arguments must be aligned as suggested by
 * the types (the input and output data must still be word aligned
 * as well).  The routines also transfer data between buffers rather
 * than modifying it in place.  It is acceptable to pass the same
 * buffer to both the input and output paramters.
 */


/*
 * _des_makekey()
 *
 *	Construct a key schedule from an 8 byte key.  Notice that
 *	the internal format of the key schedule is an array of
 *	16 48 bit values, where each 48 bit value is stored as
 *	low LITTLE ENDIAN 24 bit values in the low bits of the
 *	corresponding word (the FIRST 24 bits in word 0, the 
 *	SECOND in word 1).
 */
extern void _des_makekey(
	unsigned long	/*schedule*/[16][2],
	unsigned long	/*key*/[2]
);


/*
 * _des_cbc_crypt()
 *
 *	Encrypt or decrypt in Cipher Block Chaining mode.  The ivec
 *	value is used to start the chain - when decrypting the value
 *	passed to the original encrypt must be given (thus the key
 *	is effectively the 56 bit key used to make the key schedule
 *	plus the 64 bits given as the original ivec value).  The
 *	final ivec value is the input value required to encrypt or
 *	decrypt blocks following the ``nBlocks'' blocks - note that
 *	the number of blocks is given, not the length of the data
 *	in bytes.
 */
extern void _des_cbc_crypt(
	void		*/*input*/,
	void		*/*output*/,
	unsigned long	/*keySchedule*/[16][2],
	unsigned	/*nBlocks*/,
	int		/*mode*/,
	unsigned long	/*ivec*/[2]
);


/*
 * _des_ecb_crypt()
 *
 *	Encrypt or decrypt data in Electronic Code Book mode.  This
 *	mode should only be used if the data blocks are accessed
 *	randomly.  The cipher text depends only on the key and the
 *	input plain text (contrast with CBC mode where the cipher
 *	text depends on all the preceding plain text).  The length
 *	is given as the number of blocks (ie the actual length of
 *	the data in bytes is 8 times the value given).
 */
extern void _des_ecb_crypt(
	void		*/*input*/,
	void		*/*output*/,
	unsigned long	/*keySchedule*/[16][2],
	unsigned	/*nBlocks*/,
	int		/*mode*/
);


/*
 * _des_setparity()
 *
 *	This interface sets the relevant bits of the input buffer
 *	to give odd parity in the low bit of each byte.  The format
 *	of the data is equivalent to (char [8]) - that is, the
 *	first byte of the data is at the lowest address of the buf
 *	parameter.  The parameter must be word aligned.  Unlike
 *	des_setparity the most significant bit of each byte is
 *	NOT cleared.
 */
extern void _des_setparity(unsigned long /*buf*/[2]);


/*
 * _des_ecb_encrypt()
 *
 *	Routine to encrypt a single (8 byte) block.
 */
void _des_ecb_encrypt(
	unsigned long	/*input*/[2],
	unsigned long	/*output*/[2],
	unsigned long	/*keySchedule*/[16][2],
	int		/*mode*/
);

#endif/*__des_crypt_h*/

/* EOF des_crypt.h */
