#ifndef _CRYPTO_H_
#define _CRYPTO_H_
#include <openssl/des.h>
#include <blf.h>
#include <openssl/cast.h>
#include <openssl/aes.h>
#define USE_32BIT
#if defined (USE_64BIT)
#define XOR64(x,y) *(u_int64_t *)(x) ^= *(u_int64_t *)(y);
#define SET64(x,y) *(u_int64_t *)(x) = *(u_int64_t *)(y);
#elif defined (USE_32BIT)
#define XOR64(x,y) *(u_int32_t *)(x) ^= *(u_int32_t *)(y); \
*(u_int32_t *)((u_int8_t *)(x) + 4) ^= *(u_int32_t *)((u_int8_t *)(y) + 4);
#define SET64(x,y) *(u_int32_t *)(x) = *(u_int32_t *)(y); \
*(u_int32_t *)((u_int8_t *)(x) + 4) = *(u_int32_t *)((u_int8_t *)(y) + 4);
#else
#define XOR8(x,y,i) (x)[i] ^= (y)[i];
#define XOR64(x,y) XOR8(x,y,0); XOR8(x,y,1); XOR8(x,y,2); XOR8(x,y,3); \
XOR8(x,y,4); XOR8(x,y,5); XOR8(x,y,6); XOR8(x,y,7);
#define SET8(x,y,i) (x)[i] = (y)[i];
#define SET64(x,y) SET8(x,y,0); SET8(x,y,1); SET8(x,y,2); SET8(x,y,3); \
SET8(x,y,4); SET8(x,y,5); SET8(x,y,6); SET8(x,y,7);
#endif
#define SET_32BIT_BIG(x,y) (x)[3]= (y); (x)[2]= (y) >> 8; \
(x)[1] = (y) >> 16; (x)[0]= (y) >> 24;
#define GET_32BIT_BIG(x) (u_int32_t)(x)[3] | ((u_int32_t)(x)[2] << 8) | \
((u_int32_t)(x)[1] << 16)| ((u_int32_t)(x)[0] << 24);
#define BLOCKSIZE 8
#define MAXBLK AES_BLOCK_SIZE
struct keystate {
struct crypto_xf *xf;
u_int8_t iv[MAXBLK];
u_int8_t iv2[MAXBLK];
u_int8_t *riv, *liv;
union {
DES_key_schedule desks[3];
blf_ctx blfks;
CAST_KEY castks;
AES_KEY aesks[2];
} keydata;
};
#define ks_des keydata.desks
#define ks_blf keydata.blfks
#define ks_cast keydata.castks
#define ks_aes keydata.aesks
enum transform {
DES_CBC = 1,
IDEA_CBC = 2,
BLOWFISH_CBC = 3,
RC5_R16_B64_CBC = 4,
TRIPLEDES_CBC = 5,
CAST_CBC = 6,
AES_CBC = 7
};
enum cryptoerr {
EOKAY,
ENOCRYPTO,
EWEAKKEY,
EKEYLEN
};
struct crypto_xf {
enum transform id;
char *name;
u_int16_t keymin, keymax;
u_int16_t blocksize;
struct keystate *state;
enum cryptoerr (*init)(struct keystate *, u_int8_t *, u_int16_t);
void (*encrypt)(struct keystate *, u_int8_t *, u_int16_t);
void (*decrypt)(struct keystate *, u_int8_t *, u_int16_t);
};
extern struct keystate *crypto_clone_keystate(struct keystate *);
extern void crypto_decrypt(struct keystate *, u_int8_t *, u_int16_t);
extern void crypto_encrypt(struct keystate *, u_int8_t *, u_int16_t);
extern struct crypto_xf *crypto_get(enum transform);
extern struct keystate *crypto_init(struct crypto_xf *, u_int8_t *, u_int16_t,
enum cryptoerr *);
extern void crypto_init_iv(struct keystate *, u_int8_t *, size_t);
extern void crypto_update_iv(struct keystate *);
#endif