Removed len argument from proposal_get_token()
[strongswan.git] / src / libstrongswan / crypto / hashers / hasher.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "hasher.h"
19
20 #include <asn1/oid.h>
21
22 ENUM(hash_algorithm_names, HASH_UNKNOWN, HASH_SHA512,
23 "HASH_UNKNOWN",
24 "HASH_PREFERRED",
25 "HASH_MD2",
26 "HASH_MD4",
27 "HASH_MD5",
28 "HASH_SHA1",
29 "HASH_SHA224",
30 "HASH_SHA256",
31 "HASH_SHA384",
32 "HASH_SHA512"
33 );
34
35 ENUM(hash_algorithm_short_names, HASH_UNKNOWN, HASH_SHA512,
36 "unknown",
37 "preferred",
38 "md2",
39 "md4",
40 "md5",
41 "sha1",
42 "sha224",
43 "sha256",
44 "sha384",
45 "sha512"
46 );
47
48 /*
49 * Described in header.
50 */
51 hash_algorithm_t hasher_algorithm_from_oid(int oid)
52 {
53 switch (oid)
54 {
55 case OID_MD2:
56 case OID_MD2_WITH_RSA:
57 return HASH_MD2;
58 case OID_MD5:
59 case OID_MD5_WITH_RSA:
60 return HASH_MD5;
61 case OID_SHA1:
62 case OID_SHA1_WITH_RSA:
63 return HASH_SHA1;
64 case OID_SHA224:
65 case OID_SHA224_WITH_RSA:
66 return HASH_SHA224;
67 case OID_SHA256:
68 case OID_SHA256_WITH_RSA:
69 return HASH_SHA256;
70 case OID_SHA384:
71 case OID_SHA384_WITH_RSA:
72 return HASH_SHA384;
73 case OID_SHA512:
74 case OID_SHA512_WITH_RSA:
75 return HASH_SHA512;
76 default:
77 return HASH_UNKNOWN;
78 }
79 }
80
81 /*
82 * Described in header.
83 */
84 hash_algorithm_t hasher_algorithm_from_prf(pseudo_random_function_t alg)
85 {
86 switch (alg)
87 {
88 case PRF_HMAC_MD5:
89 return HASH_MD5;
90 case PRF_HMAC_SHA1:
91 case PRF_FIPS_SHA1_160:
92 case PRF_KEYED_SHA1:
93 return HASH_SHA1;
94 case PRF_HMAC_SHA2_256:
95 return HASH_SHA256;
96 case PRF_HMAC_SHA2_384:
97 return HASH_SHA384;
98 case PRF_HMAC_SHA2_512:
99 return HASH_SHA512;
100 case PRF_HMAC_TIGER:
101 case PRF_AES128_XCBC:
102 case PRF_AES128_CMAC:
103 case PRF_FIPS_DES:
104 case PRF_CAMELLIA128_XCBC:
105 case PRF_UNDEFINED:
106 break;
107 }
108 return HASH_UNKNOWN;
109 }
110
111 /*
112 * Described in header.
113 */
114 hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
115 size_t *length)
116 {
117 if (length)
118 {
119 switch (alg)
120 {
121 case AUTH_HMAC_MD5_96:
122 case AUTH_HMAC_SHA1_96:
123 case AUTH_HMAC_SHA2_256_96:
124 *length = 12;
125 break;
126 case AUTH_HMAC_MD5_128:
127 case AUTH_HMAC_SHA1_128:
128 case AUTH_HMAC_SHA2_256_128:
129 *length = 16;
130 break;
131 case AUTH_HMAC_SHA1_160:
132 *length = 20;
133 break;
134 case AUTH_HMAC_SHA2_384_192:
135 *length = 24;
136 break;
137 case AUTH_HMAC_SHA2_256_256:
138 case AUTH_HMAC_SHA2_512_256:
139 *length = 32;
140 break;
141 case AUTH_HMAC_SHA2_384_384:
142 *length = 48;
143 break;
144 default:
145 break;
146 }
147 }
148 switch (alg)
149 {
150 case AUTH_HMAC_MD5_96:
151 case AUTH_HMAC_MD5_128:
152 case AUTH_KPDK_MD5:
153 return HASH_MD5;
154 case AUTH_HMAC_SHA1_96:
155 case AUTH_HMAC_SHA1_128:
156 case AUTH_HMAC_SHA1_160:
157 return HASH_SHA1;
158 case AUTH_HMAC_SHA2_256_96:
159 case AUTH_HMAC_SHA2_256_128:
160 case AUTH_HMAC_SHA2_256_256:
161 return HASH_SHA256;
162 case AUTH_HMAC_SHA2_384_192:
163 case AUTH_HMAC_SHA2_384_384:
164 return HASH_SHA384;
165 case AUTH_HMAC_SHA2_512_256:
166 return HASH_SHA512;
167 case AUTH_AES_CMAC_96:
168 case AUTH_AES_128_GMAC:
169 case AUTH_AES_192_GMAC:
170 case AUTH_AES_256_GMAC:
171 case AUTH_AES_XCBC_96:
172 case AUTH_DES_MAC:
173 case AUTH_CAMELLIA_XCBC_96:
174 case AUTH_UNDEFINED:
175 break;
176 }
177 return HASH_UNKNOWN;
178 }
179
180 /*
181 * Described in header.
182 */
183 int hasher_algorithm_to_oid(hash_algorithm_t alg)
184 {
185 int oid;
186
187 switch (alg)
188 {
189 case HASH_MD2:
190 oid = OID_MD2;
191 break;
192 case HASH_MD5:
193 oid = OID_MD5;
194 break;
195 case HASH_SHA1:
196 oid = OID_SHA1;
197 break;
198 case HASH_SHA224:
199 oid = OID_SHA224;
200 break;
201 case HASH_SHA256:
202 oid = OID_SHA256;
203 break;
204 case HASH_SHA384:
205 oid = OID_SHA384;
206 break;
207 case HASH_SHA512:
208 oid = OID_SHA512;
209 break;
210 default:
211 oid = OID_UNKNOWN;
212 }
213 return oid;
214 }
215
216 /*
217 * Described in header.
218 */
219 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg, key_type_t key)
220 {
221 switch (key)
222 {
223 case KEY_RSA:
224 switch (alg)
225 {
226 case HASH_MD2:
227 return OID_MD2_WITH_RSA;
228 case HASH_MD5:
229 return OID_MD5_WITH_RSA;
230 case HASH_SHA1:
231 return OID_SHA1_WITH_RSA;
232 case HASH_SHA224:
233 return OID_SHA224_WITH_RSA;
234 case HASH_SHA256:
235 return OID_SHA256_WITH_RSA;
236 case HASH_SHA384:
237 return OID_SHA384_WITH_RSA;
238 case HASH_SHA512:
239 return OID_SHA512_WITH_RSA;
240 default:
241 return OID_UNKNOWN;
242 }
243 case KEY_ECDSA:
244 switch (alg)
245 {
246 case HASH_SHA1:
247 return OID_ECDSA_WITH_SHA1;
248 case HASH_SHA256:
249 return OID_ECDSA_WITH_SHA256;
250 case HASH_SHA384:
251 return OID_ECDSA_WITH_SHA384;
252 case HASH_SHA512:
253 return OID_ECDSA_WITH_SHA512;
254 default:
255 return OID_UNKNOWN;
256 }
257 default:
258 return OID_UNKNOWN;
259 }
260 }
261