]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libstrongswan/crypto/hashers/hasher.c
support of SHA224-based certificate signatures
[people/ms/strongswan.git] / src / libstrongswan / crypto / hashers / hasher.c
1 /*
2 * Copyright (C) 2005 Jan Hutter
3 * Copyright (C) 2005-2006 Martin Willi
4 *
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 int hasher_algorithm_to_oid(hash_algorithm_t alg)
72 {
73 int oid;
74
75 switch (alg)
76 {
77 case HASH_MD2:
78 oid = OID_MD2;
79 break;
80 case HASH_MD5:
81 oid = OID_MD5;
82 break;
83 case HASH_SHA1:
84 oid = OID_SHA1;
85 break;
86 case HASH_SHA224:
87 oid = OID_SHA224;
88 break;
89 case HASH_SHA256:
90 oid = OID_SHA256;
91 break;
92 case HASH_SHA384:
93 oid = OID_SHA384;
94 break;
95 case HASH_SHA512:
96 oid = OID_SHA512;
97 break;
98 default:
99 oid = OID_UNKNOWN;
100 }
101 return oid;
102 }
103
104 /*
105 * Described in header.
106 */
107 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg)
108 {
109 int oid;
110
111 switch (alg)
112 {
113 case HASH_MD2:
114 oid = OID_MD2_WITH_RSA;
115 break;
116 case HASH_MD5:
117 oid = OID_MD5_WITH_RSA;
118 break;
119 case HASH_SHA1:
120 oid = OID_SHA1_WITH_RSA;
121 break;
122 case HASH_SHA224:
123 oid = OID_SHA224_WITH_RSA;
124 break;
125 case HASH_SHA256:
126 oid = OID_SHA256_WITH_RSA;
127 break;
128 case HASH_SHA384:
129 oid = OID_SHA384_WITH_RSA;
130 break;
131 case HASH_SHA512:
132 oid = OID_SHA512_WITH_RSA;
133 break;
134 default:
135 oid = OID_UNKNOWN;
136 }
137 return oid;
138 }
139