crypto.h revision 1.16
1/* $OpenBSD: crypto.h,v 1.16 2005/04/08 16:06:25 deraadt Exp $ */ 2/* $EOM: crypto.h,v 1.12 2000/10/15 21:56:41 niklas Exp $ */ 3 4/* 5 * Copyright (c) 1998 Niels Provos. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28/* 29 * This code was written under funding by Ericsson Radio Systems. 30 */ 31 32#ifndef _CRYPTO_H_ 33#define _CRYPTO_H_ 34 35#if defined (__APPLE__) 36 37#include <openssl/des.h> 38#include <openssl/blowfish.h> 39#include <openssl/cast.h> 40 41#else 42 43#include <des.h> 44#include <blf.h> 45#include <cast.h> 46 47#endif /* __APPLE__ */ 48 49#include <openssl/aes.h> 50 51#define USE_32BIT 52#if defined (USE_64BIT) 53 54#define XOR64(x,y) *(u_int64_t *)(x) ^= *(u_int64_t *)(y); 55#define SET64(x,y) *(u_int64_t *)(x) = *(u_int64_t *)(y); 56 57#elif defined (USE_32BIT) 58 59#define XOR64(x,y) *(u_int32_t *)(x) ^= *(u_int32_t *)(y); \ 60 *(u_int32_t *)((u_int8_t *)(x) + 4) ^= *(u_int32_t *)((u_int8_t *)(y) + 4); 61#define SET64(x,y) *(u_int32_t *)(x) = *(u_int32_t *)(y); \ 62 *(u_int32_t *)((u_int8_t *)(x) + 4) = *(u_int32_t *)((u_int8_t *)(y) + 4); 63 64#else 65 66#define XOR8(x,y,i) (x)[i] ^= (y)[i]; 67#define XOR64(x,y) XOR8(x,y,0); XOR8(x,y,1); XOR8(x,y,2); XOR8(x,y,3); \ 68 XOR8(x,y,4); XOR8(x,y,5); XOR8(x,y,6); XOR8(x,y,7); 69#define SET8(x,y,i) (x)[i] = (y)[i]; 70#define SET64(x,y) SET8(x,y,0); SET8(x,y,1); SET8(x,y,2); SET8(x,y,3); \ 71 SET8(x,y,4); SET8(x,y,5); SET8(x,y,6); SET8(x,y,7); 72 73#endif /* USE_64BIT */ 74 75#define SET_32BIT_BIG(x,y) (x)[3]= (y); (x)[2]= (y) >> 8; \ 76 (x)[1] = (y) >> 16; (x)[0]= (y) >> 24; 77#define GET_32BIT_BIG(x) (u_int32_t)(x)[3] | ((u_int32_t)(x)[2] << 8) | \ 78 ((u_int32_t)(x)[1] << 16)| ((u_int32_t)(x)[0] << 24); 79 80/* 81 * This is standard for all block ciphers we use at the moment. 82 * Keep MAXBLK uptodate. 83 */ 84#define BLOCKSIZE 8 85#define MAXBLK AES_BLOCK_SIZE 86 87struct keystate { 88 struct crypto_xf *xf; /* Back pointer */ 89 u_int8_t iv[MAXBLK]; /* Next IV to use */ 90 u_int8_t iv2[MAXBLK]; 91 u_int8_t *riv, *liv; 92 union { 93 des_key_schedule desks[3]; 94 blf_ctx blfks; 95 cast_key castks; 96 AES_KEY aesks[2]; 97 } keydata; 98}; 99 100#define ks_des keydata.desks 101#define ks_blf keydata.blfks 102#define ks_cast keydata.castks 103#define ks_aes keydata.aesks 104 105/* 106 * Information about the cryptotransform. 107 * 108 * XXX - In regards to the IV (Initialization Vector) the drafts are 109 * completly fucked up and specify a MUST as how it is derived, so 110 * we also have to provide for that. I just don't know where. 111 * Furthermore is this enum needed at all? It seems to be Oakley IDs 112 * only anyhow, and we already have defines for that in ipsec_doi.h. 113 */ 114enum transform { 115 DES_CBC = 1, /* This is a MUST */ 116 IDEA_CBC = 2, /* Licensed, DONT use */ 117 BLOWFISH_CBC = 3, 118 RC5_R16_B64_CBC = 4, /* Licensed, DONT use */ 119 TRIPLEDES_CBC = 5, /* This is a SHOULD */ 120 CAST_CBC = 6, 121 AES_CBC = 7 122}; 123 124enum cryptoerr { 125 EOKAY, /* No error */ 126 ENOCRYPTO, /* A none crypto related error, see errno */ 127 EWEAKKEY, /* A weak key was found in key setup */ 128 EKEYLEN /* The key length was invalid for the cipher */ 129}; 130 131struct crypto_xf { 132 enum transform id; /* Oakley ID */ 133 char *name; /* Transform Name */ 134 u_int16_t keymin, keymax; /* Possible Keying Bytes */ 135 u_int16_t blocksize; /* Need to keep IV in the state */ 136 struct keystate *state; /* Key information, can also be passed sep. */ 137 enum cryptoerr (*init)(struct keystate *, u_int8_t *, u_int16_t); 138 void (*encrypt)(struct keystate *, u_int8_t *, u_int16_t); 139 void (*decrypt)(struct keystate *, u_int8_t *, u_int16_t); 140}; 141 142extern struct keystate *crypto_clone_keystate(struct keystate *); 143extern void crypto_decrypt(struct keystate *, u_int8_t *, u_int16_t); 144extern void crypto_encrypt(struct keystate *, u_int8_t *, u_int16_t); 145extern struct crypto_xf *crypto_get(enum transform); 146extern struct keystate *crypto_init(struct crypto_xf *, u_int8_t *, u_int16_t, 147 enum cryptoerr *); 148extern void crypto_init_iv(struct keystate *, u_int8_t *, size_t); 149extern void crypto_update_iv(struct keystate *); 150 151#endif /* _CRYPTO_H_ */ 152