inserted HASH_MD4 in increasing order
[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_SHA256",
30 "HASH_SHA384",
31 "HASH_SHA512"
32 );
33
34 /*
35 * Described in header.
36 */
37 hash_algorithm_t hasher_algorithm_from_oid(int oid)
38 {
39 switch (oid)
40 {
41 case OID_MD2:
42 case OID_MD2_WITH_RSA:
43 return HASH_MD2;
44 case OID_MD5:
45 case OID_MD5_WITH_RSA:
46 return HASH_MD5;
47 case OID_SHA1:
48 case OID_SHA1_WITH_RSA:
49 return HASH_SHA1;
50 case OID_SHA256:
51 case OID_SHA256_WITH_RSA:
52 return HASH_SHA256;
53 case OID_SHA384:
54 case OID_SHA384_WITH_RSA:
55 return HASH_SHA384;
56 case OID_SHA512:
57 case OID_SHA512_WITH_RSA:
58 return HASH_SHA512;
59 default:
60 return HASH_UNKNOWN;
61 }
62 }
63
64 /*
65 * Described in header.
66 */
67 int hasher_algorithm_to_oid(hash_algorithm_t alg)
68 {
69 int oid;
70
71 switch (alg)
72 {
73 case HASH_MD2:
74 oid = OID_MD2;
75 break;
76 case HASH_MD5:
77 oid = OID_MD5;
78 break;
79 case HASH_SHA1:
80 oid = OID_SHA1;
81 break;
82 case HASH_SHA256:
83 oid = OID_SHA256;
84 break;
85 case HASH_SHA384:
86 oid = OID_SHA384;
87 break;
88 case HASH_SHA512:
89 oid = OID_SHA512;
90 break;
91 default:
92 oid = OID_UNKNOWN;
93 }
94 return oid;
95 }
96
97 /*
98 * Described in header.
99 */
100 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg)
101 {
102 int oid;
103
104 switch (alg)
105 {
106 case HASH_MD2:
107 oid = OID_MD2_WITH_RSA;
108 break;
109 case HASH_MD5:
110 oid = OID_MD5_WITH_RSA;
111 break;
112 case HASH_SHA1:
113 oid = OID_SHA1_WITH_RSA;
114 break;
115 case HASH_SHA256:
116 oid = OID_SHA256_WITH_RSA;
117 break;
118 case HASH_SHA384:
119 oid = OID_SHA384_WITH_RSA;
120 break;
121 case HASH_SHA512:
122 oid = OID_SHA512_WITH_RSA;
123 break;
124 default:
125 oid = OID_UNKNOWN;
126 }
127 return oid;
128 }
129