2fa4423e15ac5b71b1fde167446a1702181a59e3
[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 #include "keymat_v1.h"
18 #include "keymat_v2.h"
19
20 /**
21 * See header
22 */
23 keymat_t *keymat_create(ike_version_t version, bool initiator)
24 {
25 switch (version)
26 {
27 case IKEV1:
28 return &keymat_v1_create(initiator)->keymat;
29 case IKEV2:
30 return &keymat_v2_create(initiator)->keymat;
31 }
32 return NULL;
33 }
34
35 /**
36 * Implicit key length for an algorithm
37 */
38 typedef struct {
39 /** IKEv2 algorithm identifier */
40 int alg;
41 /** key length in bits */
42 int len;
43 } keylen_entry_t;
44
45 /**
46 * See header.
47 */
48 int keymat_get_keylen_encr(encryption_algorithm_t alg)
49 {
50 keylen_entry_t map[] = {
51 {ENCR_DES, 64},
52 {ENCR_3DES, 192},
53 };
54 int i;
55
56 for (i = 0; i < countof(map); i++)
57 {
58 if (map[i].alg == alg)
59 {
60 return map[i].len;
61 }
62 }
63 return 0;
64 }
65
66 /**
67 * See header.
68 */
69 int keymat_get_keylen_integ(integrity_algorithm_t alg)
70 {
71 keylen_entry_t map[] = {
72 {AUTH_HMAC_MD5_96, 128},
73 {AUTH_HMAC_SHA1_96, 160},
74 {AUTH_HMAC_SHA2_256_96, 256},
75 {AUTH_HMAC_SHA2_256_128, 256},
76 {AUTH_HMAC_SHA2_384_192, 384},
77 {AUTH_HMAC_SHA2_512_256, 512},
78 {AUTH_AES_XCBC_96, 128},
79 };
80 int i;
81
82 for (i = 0; i < countof(map); i++)
83 {
84 if (map[i].alg == alg)
85 {
86 return map[i].len;
87 }
88 }
89 return 0;
90 }