/src/strongswan/src/libcharon/sa/keymat.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2011 Tobias Brunner |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU General Public License as published by the |
8 | | * Free Software Foundation; either version 2 of the License, or (at your |
9 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | * for more details. |
15 | | */ |
16 | | |
17 | | #include "keymat.h" |
18 | | |
19 | | #include <sa/ikev1/keymat_v1.h> |
20 | | #include <sa/ikev2/keymat_v2.h> |
21 | | |
22 | | static keymat_constructor_t keymat_v1_ctor = NULL, keymat_v2_ctor = NULL; |
23 | | |
24 | | /** |
25 | | * See header |
26 | | */ |
27 | | keymat_t *keymat_create(ike_version_t version, bool initiator) |
28 | 0 | { |
29 | 0 | keymat_t *keymat = NULL; |
30 | |
|
31 | 0 | switch (version) |
32 | 0 | { |
33 | 0 | case IKEV1: |
34 | | #ifdef USE_IKEV1 |
35 | | keymat = keymat_v1_ctor ? keymat_v1_ctor(initiator) |
36 | | : &keymat_v1_create(initiator)->keymat; |
37 | | #endif |
38 | 0 | break; |
39 | 0 | case IKEV2: |
40 | 0 | #ifdef USE_IKEV2 |
41 | 0 | keymat = keymat_v2_ctor ? keymat_v2_ctor(initiator) |
42 | 0 | : &keymat_v2_create(initiator)->keymat; |
43 | 0 | #endif |
44 | 0 | break; |
45 | 0 | default: |
46 | 0 | break; |
47 | 0 | } |
48 | 0 | return keymat; |
49 | 0 | } |
50 | | |
51 | | /** |
52 | | * Implicit key length for an algorithm |
53 | | */ |
54 | | typedef struct { |
55 | | /** IKEv2 algorithm identifier */ |
56 | | int alg; |
57 | | /** key length in bits */ |
58 | | int len; |
59 | | } keylen_entry_t; |
60 | | |
61 | | /** |
62 | | * See header. |
63 | | */ |
64 | | int keymat_get_keylen_encr(encryption_algorithm_t alg) |
65 | 0 | { |
66 | 0 | keylen_entry_t map[] = { |
67 | 0 | {ENCR_DES, 64}, |
68 | 0 | {ENCR_3DES, 192}, |
69 | 0 | {ENCR_CHACHA20_POLY1305, 256}, |
70 | 0 | }; |
71 | 0 | int i; |
72 | |
|
73 | 0 | for (i = 0; i < countof(map); i++) |
74 | 0 | { |
75 | 0 | if (map[i].alg == alg) |
76 | 0 | { |
77 | 0 | return map[i].len; |
78 | 0 | } |
79 | 0 | } |
80 | 0 | return 0; |
81 | 0 | } |
82 | | |
83 | | /** |
84 | | * See header. |
85 | | */ |
86 | | int keymat_get_keylen_integ(integrity_algorithm_t alg) |
87 | 0 | { |
88 | 0 | keylen_entry_t map[] = { |
89 | 0 | {AUTH_HMAC_MD5_96, 128}, |
90 | 0 | {AUTH_HMAC_MD5_128, 128}, |
91 | 0 | {AUTH_HMAC_SHA1_96, 160}, |
92 | 0 | {AUTH_HMAC_SHA1_160, 160}, |
93 | 0 | {AUTH_HMAC_SHA2_256_96, 256}, |
94 | 0 | {AUTH_HMAC_SHA2_256_128, 256}, |
95 | 0 | {AUTH_HMAC_SHA2_256_256, 256}, |
96 | 0 | {AUTH_HMAC_SHA2_384_192, 384}, |
97 | 0 | {AUTH_HMAC_SHA2_384_384, 384}, |
98 | 0 | {AUTH_HMAC_SHA2_512_256, 512}, |
99 | 0 | {AUTH_HMAC_SHA2_512_512, 512}, |
100 | 0 | {AUTH_AES_XCBC_96, 128}, |
101 | 0 | {AUTH_AES_CMAC_96, 128}, |
102 | 0 | }; |
103 | 0 | int i; |
104 | |
|
105 | 0 | for (i = 0; i < countof(map); i++) |
106 | 0 | { |
107 | 0 | if (map[i].alg == alg) |
108 | 0 | { |
109 | 0 | return map[i].len; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | return 0; |
113 | 0 | } |
114 | | |
115 | | /** |
116 | | * See header. |
117 | | */ |
118 | | void keymat_register_constructor(ike_version_t version, |
119 | | keymat_constructor_t create) |
120 | 0 | { |
121 | 0 | switch (version) |
122 | 0 | { |
123 | 0 | case IKEV1: |
124 | 0 | keymat_v1_ctor = create; |
125 | 0 | break; |
126 | 0 | case IKEV2: |
127 | 0 | keymat_v2_ctor = create; |
128 | 0 | break; |
129 | 0 | default: |
130 | 0 | break; |
131 | 0 | } |
132 | 0 | } |