OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / klips / net / ipsec / ipsec_alg.h
1 /*
2  * Modular extensions service and registration functions interface
3  *
4  * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
5  *
6  * $Id: ipsec_alg.h,v 1.2 2004-05-11 00:38:42 danield Exp $
7  *
8  */
9 /*
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2 of the License, or (at your
13  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
14  * 
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  * for more details.
19  *
20  */
21 #ifndef IPSEC_ALG_H
22 #define IPSEC_ALG_H
23
24 #define IPSEC_ALG_VERSION       0x00070301
25
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <asm/atomic.h>
29 /*      
30  *      The following structs are used via pointers in ipsec_alg object to
31  *      avoid ipsec_alg.h coupling with freeswan headers, thus simplifying
32  *      module development
33  */
34 struct ipsec_sa;
35 struct esp;
36
37 /**************************************
38  *
39  *      Main registration object 
40  *
41  *************************************/
42 #define IPSEC_ALG_VERSION_QUAD(v)       \
43         (v>>24),((v>>16)&0xff),((v>>8)&0xff),(v&0xff)
44 /*      
45  *      Main ipsec_alg objects: "OOPrograming wannabe"
46  *      Hierachy (carefully handled with _minimal_ cast'ing):
47  *
48  *      ipsec_alg+
49  *               +->ipsec_alg_enc  (ixt_alg_type=SADB_EXT_SUPPORTED_ENCRYPT)
50  *               +->ipsec_alg_auth (ixt_alg_type=SADB_EXT_SUPPORTED_AUTH)
51  */
52
53 /***************************************************************
54  *
55  *      INTERFACE object: struct ipsec_alg
56  *
57  ***************************************************************/
58
59 /* 
60  *      common part for every struct ipsec_alg_*        
61  *      (sortof poor's man OOP)
62  */
63 #define IPSEC_ALG_STRUCT_COMMON \
64         unsigned ixt_version;   /* only allow this version (or 'near')*/ \
65         struct list_head ixt_list;      /* dlinked list */ \
66         struct module *ixt_module;      /* THIS_MODULE */ \
67         unsigned ixt_state;             /* state flags */ \
68         atomic_t ixt_refcnt;    /* ref. count when pointed from ipsec_sa */ \
69         char ixt_name[16];      /* descriptive short name, eg. "3des" */ \
70         uint8_t  ixt_blocksize; /* blocksize in bytes */ \
71         \
72         /* THIS IS A COPY of struct supported (lib/pfkey.h)        \
73          * please keep in sync until we migrate 'supported' stuff  \
74          * to ipsec_alg \
75          */ \
76         uint16_t ixt_alg_type;  /* correspond to IPSEC_ALG_{ENCRYPT,AUTH} */ \
77         uint8_t  ixt_alg_id;    /* enc. alg. number, eg. ESP_3DES */ \
78         uint8_t  ixt_ivlen;     /* ivlen in bits */ \
79         uint16_t ixt_keyminbits;/* min. keybits (of entropy) */ \
80         uint16_t ixt_keymaxbits;/* max. keybits (of entropy) */
81
82 #define ixt_support ixt_alg_type
83         
84 #define IPSEC_ALG_ST_SUPP 0x01
85 #define IPSEC_ALG_ST_REGISTERED 0x02
86 struct ipsec_alg {
87         IPSEC_ALG_STRUCT_COMMON
88 };
89 /* 
90  *      Note the const in cbc_encrypt IV arg:
91  *      some ciphers like to toast passed IV (eg. 3DES): make a local IV copy
92  */
93 struct ipsec_alg_enc {
94         IPSEC_ALG_STRUCT_COMMON
95         unsigned ixt_e_keylen;          /* raw key length in bytes          */
96         unsigned ixt_e_ctx_size;        /* sa_p->key_e_size */
97         int (*ixt_e_set_key)(__u8 *key_e, const __u8 *key, int keysize);
98         int (*ixt_e_cbc_encrypt)(__u8 *key_e, __u8 *in, __u8 *out, int ilen, const __u8 *iv, int encrypt);
99         int (*ixt_e_validate_key)(struct ipsec_sa *sa_p);       /* optional */
100 };
101 struct ipsec_alg_auth {
102         IPSEC_ALG_STRUCT_COMMON
103         unsigned ixt_a_keylen;          /* raw key length in bytes          */
104         unsigned ixt_a_ctx_size;        /* sa_p->key_a_size */
105         unsigned ixt_a_authlen;         /* 'natural' auth. hash len (bytes) */
106         int (*ixt_a_hmac_set_key)(__u8 *key_a, const __u8 *key, int keylen);
107         int (*ixt_a_hmac_hash)(__u8 *key_a, const __u8 *dat, int len, __u8 *hash, int hashlen);
108 };
109 /*      
110  *      These are _copies_ of SADB_EXT_SUPPORTED_{AUTH,ENCRYPT}, 
111  *      to avoid header coupling for true constants
112  *      about headers ... "cp is your friend" --Linus
113  */
114 #define IPSEC_ALG_TYPE_AUTH     14
115 #define IPSEC_ALG_TYPE_ENCRYPT  15
116
117 /***************************************************************
118  *
119  *      INTERFACE for module loading,testing, and unloading
120  *
121  ***************************************************************/
122 /*      -  registration calls   */
123 int register_ipsec_alg(struct ipsec_alg *);
124 int unregister_ipsec_alg(struct ipsec_alg *);
125 /*      -  optional (simple test) for algos     */
126 int ipsec_alg_test(unsigned alg_type, unsigned alg_id, int testparm);
127 /*      inline wrappers (usefull for type validation */
128 static inline int register_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {
129         return register_ipsec_alg((struct ipsec_alg*)ixt);
130 }
131 static inline int unregister_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {
132         return unregister_ipsec_alg((struct ipsec_alg*)ixt);
133 }
134 static inline int register_ipsec_alg_auth(struct ipsec_alg_auth *ixt) {
135         return register_ipsec_alg((struct ipsec_alg*)ixt);
136 }
137 static inline int unregister_ipsec_alg_auth(struct ipsec_alg_auth *ixt) {
138         return unregister_ipsec_alg((struct ipsec_alg*)ixt);
139 }
140
141 /*****************************************************************
142  *
143  *      INTERFACE for ENC services: key creation, encrypt function
144  *
145  *****************************************************************/
146
147 #define IPSEC_ALG_ENCRYPT 1
148 #define IPSEC_ALG_DECRYPT 0
149
150 /*      encryption key context creation function */
151 int ipsec_alg_enc_key_create(struct ipsec_sa *sa_p);
152 #ifdef USE_IXP4XX_CRYPTO
153 /*      cipher block size extraction function */
154 void ipsec_alg_enc_blksize_create(struct ipsec_sa *sa_p);
155 #endif /* USE_IXP4XX_CRYPTO */
156 /* 
157  *      ipsec_alg_esp_encrypt(): encrypt ilen bytes in idat returns
158  *      0 or ERR<0
159  */
160 int ipsec_alg_esp_encrypt(struct ipsec_sa *sa_p, __u8 *idat, int ilen, const __u8 *iv, int action);
161
162 /***************************************************************
163  *
164  *      INTERFACE for AUTH services: key creation, hash functions
165  *
166  ***************************************************************/
167 int ipsec_alg_auth_key_create(struct ipsec_sa *sa_p);
168 int ipsec_alg_sa_esp_hash(const struct ipsec_sa *sa_p, const __u8 *espp, int len, __u8 *hash, int hashlen) ;
169 #define ipsec_alg_sa_esp_update(c,k,l) ipsec_alg_sa_esp_hash(c,k,l,NULL,0)
170
171 /* only called from ipsec_init.c */
172 int ipsec_alg_init(void);
173
174 /* algo module glue for static algos */
175 void ipsec_alg_static_init(void);
176 typedef int (*ipsec_alg_init_func_t) (void);
177
178 /**********************************************
179  *
180  *      INTERFACE for ipsec_sa init and wipe
181  *
182  **********************************************/
183
184 /* returns true if ipsec_sa has ipsec_alg obj attached */
185 #define IPSEC_ALG_SA_ESP_ENC(sa_p) ((sa_p)->ips_alg_enc)
186 #define IPSEC_ALG_SA_ESP_AUTH(sa_p) ((sa_p)->ips_alg_auth)
187 /* 
188  * Initializes ipsec_sa's ipsec_alg object, using already loaded
189  * proto, authalg, encalg.; links ipsec_alg objects (enc, auth)
190  */
191 int ipsec_alg_sa_init(struct ipsec_sa *sa_p);
192 /* 
193  * Destroys ipsec_sa's ipsec_alg object
194  * unlinking ipsec_alg objects
195  */
196 int ipsec_alg_sa_wipe(struct ipsec_sa *sa_p);
197
198 /**********************************************
199  *
200  *      2.2 backport for some 2.4 useful module stuff
201  *
202  **********************************************/
203 #ifdef MODULE
204 #ifndef THIS_MODULE
205 #define THIS_MODULE          (&__this_module)
206 #endif
207 #ifndef module_init
208 typedef int (*__init_module_func_t)(void);
209 typedef void (*__cleanup_module_func_t)(void);
210
211 #define module_init(x) \
212         int init_module(void) __attribute__((alias(#x))); \
213         static inline __init_module_func_t __init_module_inline(void) \
214         { return x; }
215 #define module_exit(x) \
216         void cleanup_module(void) __attribute__((alias(#x))); \
217         static inline __cleanup_module_func_t __cleanup_module_inline(void) \
218         { return x; }
219 #endif
220
221 #define IPSEC_ALG_MODULE_INIT( func_name )      \
222         static int func_name(void);             \
223         module_init(func_name);                 \
224         static int __init func_name(void)
225 #define IPSEC_ALG_MODULE_EXIT( func_name )      \
226         static void func_name(void);            \
227         module_exit(func_name);                 \
228         static void __exit func_name(void)
229 #else   /* not MODULE */
230 #ifndef THIS_MODULE
231 #define THIS_MODULE          NULL
232 #endif
233 /*      
234  *      I only want module_init() magic 
235  *      when algo.c file *is THE MODULE*, in all other
236  *      cases, initialization is called explicitely from ipsec_alg_init()
237  */
238 #define IPSEC_ALG_MODULE_INIT( func_name )      \
239         extern int func_name(void);             \
240         int func_name(void)
241 #define IPSEC_ALG_MODULE_EXIT( func_name )      \
242         extern void func_name(void);            \
243         void func_name(void)
244 #endif
245
246 #endif /* IPSEC_ALG_H */