extended hasher_signature_algorithm_to_oid() function
[strongswan.git] / src / libstrongswan / crypto / hashers / hasher.h
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 /**
19 * @defgroup hasher hasher
20 * @{ @ingroup crypto
21 */
22
23 #ifndef HASHER_H_
24 #define HASHER_H_
25
26 typedef enum hash_algorithm_t hash_algorithm_t;
27 typedef struct hasher_t hasher_t;
28
29 #include <library.h>
30 #include <credentials/keys/public_key.h>
31
32 /**
33 * Algorithms to use for hashing.
34 */
35 enum hash_algorithm_t {
36 /** not specified hash function */
37 HASH_UNKNOWN = 0,
38 /** preferred hash function, general purpose */
39 HASH_PREFERRED = 1,
40 HASH_MD2 = 2,
41 HASH_MD4 = 3,
42 HASH_MD5 = 4,
43 HASH_SHA1 = 5,
44 HASH_SHA224 = 6,
45 HASH_SHA256 = 7,
46 HASH_SHA384 = 8,
47 HASH_SHA512 = 9
48 };
49
50 #define HASH_SIZE_MD2 16
51 #define HASH_SIZE_MD4 16
52 #define HASH_SIZE_MD5 16
53 #define HASH_SIZE_SHA1 20
54 #define HASH_SIZE_SHA224 28
55 #define HASH_SIZE_SHA256 32
56 #define HASH_SIZE_SHA384 48
57 #define HASH_SIZE_SHA512 64
58
59 /**
60 * enum names for hash_algorithm_t.
61 */
62 extern enum_name_t *hash_algorithm_names;
63
64 /**
65 * Generic interface for all hash functions.
66 */
67 struct hasher_t {
68 /**
69 * Hash data and write it in the buffer.
70 *
71 * If the parameter hash is NULL, no result is written back
72 * and more data can be appended to already hashed data.
73 * If not, the result is written back and the hasher is reset.
74 *
75 * The hash output parameter must hold at least
76 * hash_t.get_block_size() bytes.
77 *
78 * @param data data to hash
79 * @param hash pointer where the hash will be written
80 */
81 void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
82
83 /**
84 * Hash data and allocate space for the hash.
85 *
86 * If the parameter hash is NULL, no result is written back
87 * and more data can be appended to already hashed data.
88 * If not, the result is written back and the hasher is reset.
89 *
90 * @param data chunk with data to hash
91 * @param hash chunk which will hold allocated hash
92 */
93 void (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
94
95 /**
96 * Get the size of the resulting hash.
97 *
98 * @return hash size in bytes
99 */
100 size_t (*get_hash_size) (hasher_t *this);
101
102 /**
103 * Resets the hashers state.
104 */
105 void (*reset) (hasher_t *this);
106
107 /**
108 * Destroys a hasher object.
109 */
110 void (*destroy) (hasher_t *this);
111 };
112
113 /**
114 * Conversion of ASN.1 OID to hash algorithm.
115 *
116 * @param oid ASN.1 OID
117 * @return hash algorithm, HASH_UNKNOWN if OID unsuported
118 */
119 hash_algorithm_t hasher_algorithm_from_oid(int oid);
120
121 /**
122 * Conversion of hash algorithm into ASN.1 OID.
123 *
124 * @param alg hash algorithm
125 * @return ASN.1 OID, or OID_UNKNOW
126 */
127 int hasher_algorithm_to_oid(hash_algorithm_t alg);
128
129 /**
130 * Conversion of hash signature algorithm into ASN.1 OID.
131 *
132 * @param alg hash algorithm
133 * @param alg public key type
134 * @return ASN.1 OID if, or OID_UNKNOW
135 */
136 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg, key_type_t key);
137
138 #endif /** HASHER_H_ @}*/