xref: /openbsd/sbin/isakmpd/crypto.h (revision 0ceec575)
1 /* $OpenBSD: crypto.h,v 1.20 2010/10/19 07:47:34 mikeb 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 #include <openssl/des.h>
36 #include <blf.h>
37 #include <openssl/cast.h>
38 
39 #include <openssl/aes.h>
40 
41 #define USE_32BIT
42 #if defined (USE_64BIT)
43 
44 #define XOR64(x,y) *(u_int64_t *)(x) ^= *(u_int64_t *)(y);
45 #define SET64(x,y) *(u_int64_t *)(x) = *(u_int64_t *)(y);
46 
47 #elif defined (USE_32BIT)
48 
49 #define XOR64(x,y) *(u_int32_t *)(x) ^= *(u_int32_t *)(y); \
50    *(u_int32_t *)((u_int8_t *)(x) + 4) ^= *(u_int32_t *)((u_int8_t *)(y) + 4);
51 #define SET64(x,y) *(u_int32_t *)(x) = *(u_int32_t *)(y); \
52    *(u_int32_t *)((u_int8_t *)(x) + 4) = *(u_int32_t *)((u_int8_t *)(y) + 4);
53 
54 #else
55 
56 #define XOR8(x,y,i) (x)[i] ^= (y)[i];
57 #define XOR64(x,y) XOR8(x,y,0); XOR8(x,y,1); XOR8(x,y,2); XOR8(x,y,3); \
58    XOR8(x,y,4); XOR8(x,y,5); XOR8(x,y,6); XOR8(x,y,7);
59 #define SET8(x,y,i) (x)[i] = (y)[i];
60 #define SET64(x,y) SET8(x,y,0); SET8(x,y,1); SET8(x,y,2); SET8(x,y,3); \
61    SET8(x,y,4); SET8(x,y,5); SET8(x,y,6); SET8(x,y,7);
62 
63 #endif				/* USE_64BIT */
64 
65 #define SET_32BIT_BIG(x,y) (x)[3]= (y); (x)[2]= (y) >> 8; \
66     (x)[1] = (y) >> 16; (x)[0]= (y) >> 24;
67 #define GET_32BIT_BIG(x) (u_int32_t)(x)[3] | ((u_int32_t)(x)[2] << 8) | \
68     ((u_int32_t)(x)[1] << 16)| ((u_int32_t)(x)[0] << 24);
69 
70 /*
71  * This is standard for all block ciphers we use at the moment.
72  * Keep MAXBLK uptodate.
73  */
74 #define BLOCKSIZE	8
75 #define MAXBLK		AES_BLOCK_SIZE
76 
77 struct keystate {
78 	struct crypto_xf *xf;	/* Back pointer */
79 	u_int8_t        iv[MAXBLK];	/* Next IV to use */
80 	u_int8_t        iv2[MAXBLK];
81 	u_int8_t       *riv, *liv;
82 	union {
83 		DES_key_schedule desks[3];
84 		blf_ctx         blfks;
85 		CAST_KEY        castks;
86 		AES_KEY         aesks[2];
87 	}               keydata;
88 };
89 
90 #define ks_des	keydata.desks
91 #define ks_blf	keydata.blfks
92 #define ks_cast	keydata.castks
93 #define ks_aes	keydata.aesks
94 
95 /*
96  * Information about the cryptotransform.
97  *
98  * XXX - In regards to the IV (Initialization Vector) the drafts are
99  * completely fucked up and specify a MUST as how it is derived, so
100  * we also have to provide for that. I just don't know where.
101  * Furthermore is this enum needed at all?  It seems to be Oakley IDs
102  * only anyhow, and we already have defines for that in ipsec_doi.h.
103  */
104 enum transform {
105 	DES_CBC = 1,		/* This is a MUST */
106 	IDEA_CBC = 2,		/* Licensed, DONT use */
107 	BLOWFISH_CBC = 3,
108 	RC5_R16_B64_CBC = 4,	/* Licensed, DONT use */
109 	TRIPLEDES_CBC = 5,	/* This is a SHOULD */
110 	CAST_CBC = 6,
111 	AES_CBC = 7
112 };
113 
114 enum cryptoerr {
115 	EOKAY,			/* No error */
116 	ENOCRYPTO,		/* A none crypto related error, see errno */
117 	EWEAKKEY,		/* A weak key was found in key setup */
118 	EKEYLEN			/* The key length was invalid for the cipher */
119 };
120 
121 struct crypto_xf {
122 	enum transform  id;	/* Oakley ID */
123 	char           *name;	/* Transform Name */
124 	u_int16_t       keymin, keymax;	/* Possible Keying Bytes */
125 	u_int16_t       blocksize;	/* Need to keep IV in the state */
126 	struct keystate *state;	/* Key information, can also be passed sep. */
127 	enum cryptoerr  (*init)(struct keystate *, u_int8_t *, u_int16_t);
128 	void            (*encrypt)(struct keystate *, u_int8_t *, u_int16_t);
129 	void            (*decrypt)(struct keystate *, u_int8_t *, u_int16_t);
130 };
131 
132 extern struct keystate *crypto_clone_keystate(struct keystate *);
133 extern void     crypto_decrypt(struct keystate *, u_int8_t *, u_int16_t);
134 extern void     crypto_encrypt(struct keystate *, u_int8_t *, u_int16_t);
135 extern struct crypto_xf *crypto_get(enum transform);
136 extern struct keystate *crypto_init(struct crypto_xf *, u_int8_t *, u_int16_t,
137 		    enum cryptoerr *);
138 extern void     crypto_init_iv(struct keystate *, u_int8_t *, size_t);
139 extern void     crypto_update_iv(struct keystate *);
140 
141 #endif				/* _CRYPTO_H_ */
142