]> git.ipfire.org Git - thirdparty/strongswan.git/blob - programs/pluto/alg/ike_alg_serpent.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / pluto / alg / ike_alg_serpent.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stddef.h>
4 #include <sys/types.h>
5 #include <freeswan.h>
6
7 #include "constants.h"
8 #include "defs.h"
9 #include "log.h"
10 #include "libserpent/serpent_cbc.h"
11 #include "alg_info.h"
12 #include "ike_alg.h"
13
14 #define SERPENT_CBC_BLOCK_SIZE (128/BITS_PER_BYTE)
15 #define SERPENT_KEY_MIN_LEN 128
16 #define SERPENT_KEY_DEF_LEN 128
17 #define SERPENT_KEY_MAX_LEN 256
18
19 static void
20 do_serpent(u_int8_t *buf, size_t buf_size, u_int8_t *key, size_t key_size, u_int8_t *iv, bool enc)
21 {
22 serpent_context serpent_ctx;
23 char iv_bak[SERPENT_CBC_BLOCK_SIZE];
24 char *new_iv = NULL; /* logic will avoid copy to NULL */
25
26
27 serpent_set_key(&serpent_ctx, key, key_size);
28 /*
29 * my SERPENT cbc does not touch passed IV (optimization for
30 * ESP handling), so I must "emulate" des-like IV
31 * crunching
32 */
33 if (!enc)
34 memcpy(new_iv=iv_bak,
35 (char*) buf + buf_size-SERPENT_CBC_BLOCK_SIZE,
36 SERPENT_CBC_BLOCK_SIZE);
37
38 serpent_cbc_encrypt(&serpent_ctx, buf, buf, buf_size, iv, enc);
39
40 if (enc)
41 new_iv = (char*) buf + buf_size-SERPENT_CBC_BLOCK_SIZE;
42
43 memcpy(iv, new_iv, SERPENT_CBC_BLOCK_SIZE);
44 }
45
46 struct encrypt_desc encrypt_desc_serpent =
47 {
48 algo_type: IKE_ALG_ENCRYPT,
49 algo_id: OAKLEY_SERPENT_CBC,
50 algo_next: NULL,
51 enc_ctxsize: sizeof(struct serpent_context),
52 enc_blocksize: SERPENT_CBC_BLOCK_SIZE,
53 keyminlen: SERPENT_KEY_MIN_LEN,
54 keydeflen: SERPENT_KEY_DEF_LEN,
55 keymaxlen: SERPENT_KEY_MAX_LEN,
56 do_crypt: do_serpent,
57 };
58
59 int ike_alg_serpent_init(void);
60
61 int
62 ike_alg_serpent_init(void)
63 {
64 int ret = ike_alg_register_enc(&encrypt_desc_serpent);
65
66 return ret;
67 }
68 /*
69 IKE_ALG_INIT_NAME: ike_alg_serpent_init
70 */