OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / pluto / crypto.h
1 /* crypto interfaces
2  * Copyright (C) 1998, 1999  D. Hugh Redelmeier.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * RCSID $Id: crypto.h,v 1.13 1999/12/13 00:40:50 dhr Exp $
15  */
16
17 #include <gmp.h>    /* GNU MP library */
18
19 extern void init_crypto(void);
20
21 /* Oakley group descriptions */
22
23 extern MP_INT groupgenerator;   /* MODP group generator (2) */
24
25 struct oakley_group_desc {
26     u_int16_t group;
27     MP_INT *modulus;
28     size_t bytes;
29 };
30
31 extern const struct oakley_group_desc unset_group;      /* magic signifier */
32 extern const struct oakley_group_desc *lookup_group(u_int16_t group);
33 #define OAKLEY_GROUP_SIZE 6
34 extern const struct oakley_group_desc oakley_group[OAKLEY_GROUP_SIZE];
35
36 /* unification of cryptographic encoding/decoding algorithms
37  * The IV is taken from and returned to st->st_new_iv.
38  * This allows the old IV to be retained.
39  * Use update_iv to commit to the new IV (for example, once a packet has
40  * been validated).
41  */
42
43 #define MAX_OAKLEY_KEY_LEN0  (3 * DES_CBC_BLOCK_SIZE)
44 #define MAX_OAKLEY_KEY_LEN  (256/BITS_PER_BYTE)
45
46 struct state;   /* forward declaration, dammit */
47
48 struct encrypt_desc;
49 struct hash_desc;
50 struct encrypt_desc *crypto_get_encrypter(int alg);
51 struct hash_desc *crypto_get_hasher(int alg);
52 void crypto_cbc_encrypt(const struct encrypt_desc *e, bool enc, u_int8_t *buf, size_t size, struct state *st);
53 #define update_iv(st)   memcpy((st)->st_iv, (st)->st_new_iv \
54     , (st)->st_iv_len = (st)->st_new_iv_len)
55
56 /* unification of cryptographic hashing mechanisms */
57
58 #ifndef NO_HASH_CTX
59 union hash_ctx {
60         MD5_CTX ctx_md5;
61         SHA1_CTX ctx_sha1;
62         char ctx_sha256[108];   /* This is _ugly_ [TM], but avoids */
63         char ctx_sha512[212];   /* header coupling (is checked at runtime */
64     };
65
66 /* HMAC package
67  * Note that hmac_ctx can be (and is) copied since there are
68  * no persistent pointers into it.
69  */
70
71 struct hmac_ctx {
72     const struct hash_desc *h;  /* underlying hash function */
73     size_t hmac_digest_size;    /* copy of h->hash_digest_size */
74     union hash_ctx hash_ctx;    /* ctx for hash function */
75     u_char buf1[HMAC_BUFSIZE], buf2[HMAC_BUFSIZE];
76     };
77
78 extern void hmac_init(
79     struct hmac_ctx *ctx,
80     const struct hash_desc *h,
81     const u_char *key,
82     size_t key_len);
83
84 #define hmac_init_chunk(ctx, h, ch) hmac_init((ctx), (h), (ch).ptr, (ch).len)
85
86 extern void hmac_reinit(struct hmac_ctx *ctx);  /* saves recreating pads */
87
88 extern void hmac_update(
89     struct hmac_ctx *ctx,
90     const u_char *data,
91     size_t data_len);
92
93 #define hmac_update_chunk(ctx, ch) hmac_update((ctx), (ch).ptr, (ch).len)
94
95 extern void hmac_final(u_char *output, struct hmac_ctx *ctx);
96
97 #define hmac_final_chunk(ch, name, ctx) { \
98         pfreeany((ch).ptr); \
99         (ch).len = (ctx)->hmac_digest_size; \
100         (ch).ptr = alloc_bytes((ch).len, name); \
101         hmac_final((ch).ptr, (ctx)); \
102     }
103 #endif