Documentation for LCLib
There are 3 basic algorithms that the library offers :-
Blowfish (symmetric algorithm - ebc and cbc modes)
SHA (hashing algorithm - produces 160 bit hash)
RSA (asymmetric algorithm)
The Blowfish implementation is one by Paul Kocher (pck@netcom.com)
The SHA implementation is one by Uwe Hollerbacj (uh@alumni.caltech)
The RSA implementation is one by John Horton (jh_squirrel@yahoo.com)
and uses the multi precision library (bnlib-1.1) by Colin Plumb (colin@nyx.net)
All functions in the library have a LCLIB_CTX passed to them. This context is defined as:-
typedef struct{
IV bf_iv;
BLOWFISH_CTX bf_ctx;
SHA_INFO sha_ctx;
RSA_CTX rsa_ctx;
}LCLIB_CTX;
The function declarations can be found below
All functions return 0 if OK and an integer less than 0 upon error
Blowfish
int bf_ebc_init(LCLIB_CTX *ctx,unsigned char *key,unsigned int keyLen)
int bf_cbc_init(LCLIB_CTX *ctx,unsigned char *key,unsigned int keyLen,unsigned
long int l,unsigned long int r)
l and r (used in cbc mode) are the left and right initialisation vectors
bf_ebc_enc(LCLIB_CTX *ctx,unsigned char *data,unsigned int *datLen,unsigned
int blocklen,int pad)
bf_cbc_enc(LCLIB_CTX *ctx,unsigned char *data,unsigned int *datLen,unsigned
int blocklen,int pad)
all pointers (data) passed must be of blocklen size even if unused
bytes are set to zero
datLen is a pointer that must be less than or equal in size to the
blocklen. An error will occur if (datLen%blocklen !=0 and pad=0)
setting pad to 1 will ensure that if a block is short then it will
be padded with zeros (and a unsigned char indicating how many zeros were
used will be the last byte in the block)
bf_ebc_dec(LCLIB_CTX *ctx,unsigned char *data,unsigned int *datLen,int
pad)
bf_cbc_dec(LCLIB_CTX *ctx,unsigned char *data,unsigned int *datLen,int
pad)
datLen is a pointer that may be updated if the data was padded. An
error will occur if datLen%8 !=0
These functions will remove padding if pad = 1
SHA
int sha_hash_init(LCLIB_CTX *ctx)
initialises SHA hashing algorithm
int sha_hash_update(LCLIB_CTX *ctx,unsigned char *data,unsigned int
datLen)
will update SHA with datLen bytes of data
int sha_hash_final(LCLIB_CTX *ctx)
will finalise the hashing algorithm
the hash is now stored in ctx and can be obtained:-
for(int i=0;i<5;i++)
printf("%08lx ",ctx.sha_ctx.digext[i]);
RSA
rsa_init(LCLIB_CTX *ctx,unsigned long bitlen)
initialises key variables so that they can accept bitlen bits for the
keys
rsa_genkeys(LCLIB_CTX *ctx)
generates N,d and e (of size bitlen - defined during rsa_init)
rsa_setkeys(LCLIB_CTX *ctx,unsigned char *N,unsigned char *d,unsigned
char *e)
sets the public and private keys for the context.
N, e and d are null terminated strings.
rsa_extractkeys(LCLIB_CTX *ctx,unsigned char **N,unsigned char **d,unsigned
char **e)
extracts the keys from the context so that may be saved for later
The function will allocate enough memory to hold N, d and e. The user
should provide the address of an address of a pointer for N, d and e.
rsa_encrypt(LCLIB_CTX *ctx,unsigned char *data,unsigned long int *datLen)
data should be a pointer obtained by rsa_alloc (it's size will be equal
to the size of ctx->rsa_ctx->N) datLen is the size of the actual data (and
must be less than or equal to N - (2*SHAHASHSIZE) -1 )
rsa_sign(LCLIB_CTX *ctx,unsigned char *data,unsigned long int *datLen)
rsa_sign is essentially the same as rsa_encrypt so all the same operations
hold true as defined above
rsa_decrypt(LCLIB_CTX *ctx,unsigned char *data,unsigned int *datLen)
rsa_verify(LCLIB_CTX *ctx,unsigned char *data,unsigned int *datLen)
rsa_verify is essentially the same as rsa_decrypt, so , as before the
same rules apply
unsigned char* rsa_alloc(LCLIB_CTX *ctx,unsigned char*data1,unsigned
int size)
This will allocate enough memory to hold data specified by data1. If
data1 is greater in size to the modulus held in ctx then NULL will be returned.
rsa_free(unsigned char*data);
frees previously allocated memory
The test programs included with the distribution will exemplify this documentation
John Horton Jan 2002
( jh_squirrel@yahoo.co.uk )