bcf131fd0b60e5a852bb678291c1843ea3e4d65d
[strongswan.git] / src / libcharon / sa / keymat.c
1 /*
2 * Copyright (C) 2011 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "keymat.h"
17
18 #include <sa/ikev1/keymat_v1.h>
19 #include <sa/ikev2/keymat_v2.h>
20
21 /**
22 * See header
23 */
24 keymat_t *keymat_create(ike_version_t version, bool initiator)
25 {
26 switch (version)
27 {
28 case IKEV1:
29 return &keymat_v1_create(initiator)->keymat;
30 case IKEV2:
31 return &keymat_v2_create(initiator)->keymat;
32 default:
33 return NULL;
34 }
35 }
36
37 /**
38 * Implicit key length for an algorithm
39 */
40 typedef struct {
41 /** IKEv2 algorithm identifier */
42 int alg;
43 /** key length in bits */
44 int len;
45 } keylen_entry_t;
46
47 /**
48 * See header.
49 */
50 int keymat_get_keylen_encr(encryption_algorithm_t alg)
51 {
52 keylen_entry_t map[] = {
53 {ENCR_DES, 64},
54 {ENCR_3DES, 192},
55 };
56 int i;
57
58 for (i = 0; i < countof(map); i++)
59 {
60 if (map[i].alg == alg)
61 {
62 return map[i].len;
63 }
64 }
65 return 0;
66 }
67
68 /**
69 * See header.
70 */
71 int keymat_get_keylen_integ(integrity_algorithm_t alg)
72 {
73 keylen_entry_t map[] = {
74 {AUTH_HMAC_MD5_96, 128},
75 {AUTH_HMAC_SHA1_96, 160},
76 {AUTH_HMAC_SHA2_256_96, 256},
77 {AUTH_HMAC_SHA2_256_128, 256},
78 {AUTH_HMAC_SHA2_384_192, 384},
79 {AUTH_HMAC_SHA2_512_256, 512},
80 {AUTH_AES_XCBC_96, 128},
81 };
82 int i;
83
84 for (i = 0; i < countof(map); i++)
85 {
86 if (map[i].alg == alg)
87 {
88 return map[i].len;
89 }
90 }
91 return 0;
92 }