Add a return value to hasher_t.get_hash()
[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 /*
36 * Described in header.
37 */
38 hash_algorithm_t hasher_algorithm_from_oid(int oid)
39 {
40 switch (oid)
41 {
42 case OID_MD2:
43 case OID_MD2_WITH_RSA:
44 return HASH_MD2;
45 case OID_MD5:
46 case OID_MD5_WITH_RSA:
47 return HASH_MD5;
48 case OID_SHA1:
49 case OID_SHA1_WITH_RSA:
50 return HASH_SHA1;
51 case OID_SHA224:
52 case OID_SHA224_WITH_RSA:
53 return HASH_SHA224;
54 case OID_SHA256:
55 case OID_SHA256_WITH_RSA:
56 return HASH_SHA256;
57 case OID_SHA384:
58 case OID_SHA384_WITH_RSA:
59 return HASH_SHA384;
60 case OID_SHA512:
61 case OID_SHA512_WITH_RSA:
62 return HASH_SHA512;
63 default:
64 return HASH_UNKNOWN;
65 }
66 }
67
68 /*
69 * Described in header.
70 */
71 hash_algorithm_t hasher_algorithm_from_prf(pseudo_random_function_t alg)
72 {
73 switch (alg)
74 {
75 case PRF_HMAC_MD5:
76 return HASH_MD5;
77 case PRF_HMAC_SHA1:
78 case PRF_FIPS_SHA1_160:
79 case PRF_KEYED_SHA1:
80 return HASH_SHA1;
81 case PRF_HMAC_SHA2_256:
82 return HASH_SHA256;
83 case PRF_HMAC_SHA2_384:
84 return HASH_SHA384;
85 case PRF_HMAC_SHA2_512:
86 return HASH_SHA512;
87 case PRF_HMAC_TIGER:
88 case PRF_AES128_XCBC:
89 case PRF_AES128_CMAC:
90 case PRF_FIPS_DES:
91 case PRF_CAMELLIA128_XCBC:
92 case PRF_UNDEFINED:
93 break;
94 }
95 return HASH_UNKNOWN;
96 }
97
98 /*
99 * Described in header.
100 */
101 hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
102 size_t *length)
103 {
104 if (length)
105 {
106 switch (alg)
107 {
108 case AUTH_HMAC_MD5_96:
109 case AUTH_HMAC_SHA1_96:
110 case AUTH_HMAC_SHA2_256_96:
111 *length = 12;
112 break;
113 case AUTH_HMAC_MD5_128:
114 case AUTH_HMAC_SHA1_128:
115 case AUTH_HMAC_SHA2_256_128:
116 *length = 16;
117 break;
118 case AUTH_HMAC_SHA1_160:
119 *length = 20;
120 break;
121 case AUTH_HMAC_SHA2_384_192:
122 *length = 24;
123 break;
124 case AUTH_HMAC_SHA2_256_256:
125 case AUTH_HMAC_SHA2_512_256:
126 *length = 32;
127 break;
128 case AUTH_HMAC_SHA2_384_384:
129 *length = 48;
130 break;
131 default:
132 break;
133 }
134 }
135 switch (alg)
136 {
137 case AUTH_HMAC_MD5_96:
138 case AUTH_HMAC_MD5_128:
139 case AUTH_KPDK_MD5:
140 return HASH_MD5;
141 case AUTH_HMAC_SHA1_96:
142 case AUTH_HMAC_SHA1_128:
143 case AUTH_HMAC_SHA1_160:
144 return HASH_SHA1;
145 case AUTH_HMAC_SHA2_256_96:
146 case AUTH_HMAC_SHA2_256_128:
147 case AUTH_HMAC_SHA2_256_256:
148 return HASH_SHA256;
149 case AUTH_HMAC_SHA2_384_192:
150 case AUTH_HMAC_SHA2_384_384:
151 return HASH_SHA384;
152 case AUTH_HMAC_SHA2_512_256:
153 return HASH_SHA512;
154 case AUTH_AES_CMAC_96:
155 case AUTH_AES_128_GMAC:
156 case AUTH_AES_192_GMAC:
157 case AUTH_AES_256_GMAC:
158 case AUTH_AES_XCBC_96:
159 case AUTH_DES_MAC:
160 case AUTH_CAMELLIA_XCBC_96:
161 case AUTH_UNDEFINED:
162 break;
163 }
164 return HASH_UNKNOWN;
165 }
166
167 /*
168 * Described in header.
169 */
170 int hasher_algorithm_to_oid(hash_algorithm_t alg)
171 {
172 int oid;
173
174 switch (alg)
175 {
176 case HASH_MD2:
177 oid = OID_MD2;
178 break;
179 case HASH_MD5:
180 oid = OID_MD5;
181 break;
182 case HASH_SHA1:
183 oid = OID_SHA1;
184 break;
185 case HASH_SHA224:
186 oid = OID_SHA224;
187 break;
188 case HASH_SHA256:
189 oid = OID_SHA256;
190 break;
191 case HASH_SHA384:
192 oid = OID_SHA384;
193 break;
194 case HASH_SHA512:
195 oid = OID_SHA512;
196 break;
197 default:
198 oid = OID_UNKNOWN;
199 }
200 return oid;
201 }
202
203 /*
204 * Described in header.
205 */
206 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg, key_type_t key)
207 {
208 switch (key)
209 {
210 case KEY_RSA:
211 switch (alg)
212 {
213 case HASH_MD2:
214 return OID_MD2_WITH_RSA;
215 case HASH_MD5:
216 return OID_MD5_WITH_RSA;
217 case HASH_SHA1:
218 return OID_SHA1_WITH_RSA;
219 case HASH_SHA224:
220 return OID_SHA224_WITH_RSA;
221 case HASH_SHA256:
222 return OID_SHA256_WITH_RSA;
223 case HASH_SHA384:
224 return OID_SHA384_WITH_RSA;
225 case HASH_SHA512:
226 return OID_SHA512_WITH_RSA;
227 default:
228 return OID_UNKNOWN;
229 }
230 case KEY_ECDSA:
231 switch (alg)
232 {
233 case HASH_SHA1:
234 return OID_ECDSA_WITH_SHA1;
235 case HASH_SHA256:
236 return OID_ECDSA_WITH_SHA256;
237 case HASH_SHA384:
238 return OID_ECDSA_WITH_SHA384;
239 case HASH_SHA512:
240 return OID_ECDSA_WITH_SHA512;
241 default:
242 return OID_UNKNOWN;
243 }
244 default:
245 return OID_UNKNOWN;
246 }
247 }
248