Add short names for hasher algorithms
[strongswan.git] / src / libstrongswan / crypto / hashers / hasher.h
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 /**
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 <crypto/prfs/prf.h>
31 #include <crypto/signers/signer.h>
32 #include <credentials/keys/public_key.h>
33
34 /**
35 * Algorithms to use for hashing.
36 */
37 enum hash_algorithm_t {
38 /** not specified hash function */
39 HASH_UNKNOWN = 0,
40 /** preferred hash function, general purpose */
41 HASH_PREFERRED = 1,
42 HASH_MD2 = 2,
43 HASH_MD4 = 3,
44 HASH_MD5 = 4,
45 HASH_SHA1 = 5,
46 HASH_SHA224 = 6,
47 HASH_SHA256 = 7,
48 HASH_SHA384 = 8,
49 HASH_SHA512 = 9
50 };
51
52 #define HASH_SIZE_MD2 16
53 #define HASH_SIZE_MD4 16
54 #define HASH_SIZE_MD5 16
55 #define HASH_SIZE_SHA1 20
56 #define HASH_SIZE_SHA224 28
57 #define HASH_SIZE_SHA256 32
58 #define HASH_SIZE_SHA384 48
59 #define HASH_SIZE_SHA512 64
60
61 /**
62 * enum names for hash_algorithm_t.
63 */
64 extern enum_name_t *hash_algorithm_names;
65
66 /**
67 * Short names for hash_algorithm_names
68 */
69 extern enum_name_t *hash_algorithm_short_names;
70
71 /**
72 * Generic interface for all hash functions.
73 */
74 struct hasher_t {
75
76 /**
77 * Hash data and write it in the buffer.
78 *
79 * If the parameter hash is NULL, no result is written back
80 * and more data can be appended to already hashed data.
81 * If not, the result is written back and the hasher is reset.
82 *
83 * The hash output parameter must hold at least
84 * hash_t.get_block_size() bytes.
85 *
86 * @param data data to hash
87 * @param hash pointer where the hash will be written
88 * @return TRUE if hash created successfully
89 */
90 __attribute__((warn_unused_result))
91 bool (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
92
93 /**
94 * Hash data and allocate space for the hash.
95 *
96 * If the parameter hash is NULL, no result is written back
97 * and more data can be appended to already hashed data.
98 * If not, the result is written back and the hasher is reset.
99 *
100 * @param data chunk with data to hash
101 * @param hash chunk which will hold allocated hash
102 * @return TRUE if hash allocated successfully
103 */
104 __attribute__((warn_unused_result))
105 bool (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
106
107 /**
108 * Get the size of the resulting hash.
109 *
110 * @return hash size in bytes
111 */
112 size_t (*get_hash_size) (hasher_t *this);
113
114 /**
115 * Resets the hasher's state.
116 *
117 * @return TRUE if hasher reset successfully
118 */
119 __attribute__((warn_unused_result))
120 bool (*reset) (hasher_t *this);
121
122 /**
123 * Destroys a hasher object.
124 */
125 void (*destroy) (hasher_t *this);
126 };
127
128 /**
129 * Conversion of ASN.1 OID to hash algorithm.
130 *
131 * @param oid ASN.1 OID
132 * @return hash algorithm, HASH_UNKNOWN if OID unsupported
133 */
134 hash_algorithm_t hasher_algorithm_from_oid(int oid);
135
136 /**
137 * Conversion of PRF algorithm to hash algorithm (if based on one).
138 *
139 * @param alg prf algorithm
140 * @return hash algorithm, HASH_UNKNOWN if not based on a hash
141 */
142 hash_algorithm_t hasher_algorithm_from_prf(pseudo_random_function_t alg);
143
144 /**
145 * Conversion of integrity algorithm to hash algorithm (if based on one).
146 *
147 * If length is not NULL the length of the resulting signature is returned,
148 * which might be smaller than the output size of the underlying hash.
149 *
150 * @param alg integrity algorithm
151 * @param length returns signature length, if not NULL
152 * @return hash algorithm, HASH_UNKNOWN if not based on a hash
153 */
154 hash_algorithm_t hasher_algorithm_from_integrity(integrity_algorithm_t alg,
155 size_t *length);
156
157 /**
158 * Conversion of hash algorithm into ASN.1 OID.
159 *
160 * @param alg hash algorithm
161 * @return ASN.1 OID, or OID_UNKNOW
162 */
163 int hasher_algorithm_to_oid(hash_algorithm_t alg);
164
165 /**
166 * Conversion of hash signature algorithm into ASN.1 OID.
167 *
168 * @param alg hash algorithm
169 * @param key public key type
170 * @return ASN.1 OID if, or OID_UNKNOW
171 */
172 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg, key_type_t key);
173
174 #endif /** HASHER_H_ @}*/