fixed big endian bug in md5 hasher
[strongswan.git] / src / libstrongswan / crypto / hashers / hasher.h
1 /**
2 * @file hasher.h
3 *
4 * @brief Interface hasher_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #ifndef HASHER_H_
25 #define HASHER_H_
26
27 typedef enum hash_algorithm_t hash_algorithm_t;
28 typedef struct hasher_t hasher_t;
29
30 #include <library.h>
31
32 /**
33 * @brief Algorithms to use for hashing.
34 *
35 * Currently only the following algorithms are implemented:
36 * - HASH_MD5
37 * - HASH_SHA1
38 * - HASH_SHA256
39 * - HASH_SHA384
40 * - HASH_SHA512
41 *
42 * @ingroup hashers
43 */
44 enum hash_algorithm_t {
45 HASH_MD2 = 0,
46 /** Implemented in class md5_hasher_t */
47 HASH_MD5 = 1,
48 /** Implemented in class sha1_hasher_t */
49 HASH_SHA1 = 2,
50 /** Implemented in class sha2_hasher_t */
51 HASH_SHA256 = 3,
52 /** Implemented in class sha2_hasher_t */
53 HASH_SHA384 = 4,
54 /** Implemented in class sha2_hasher_t */
55 HASH_SHA512 = 5,
56 };
57
58 #define HASH_SIZE_MD2 16
59 #define HASH_SIZE_MD5 16
60 #define HASH_SIZE_SHA1 20
61 #define HASH_SIZE_SHA256 32
62 #define HASH_SIZE_SHA384 48
63 #define HASH_SIZE_SHA512 64
64 #define HASH_SIZE_MAX 64
65
66 /**
67 * enum names for hash_algorithm_t.
68 */
69 extern enum_name_t *hash_algorithm_names;
70
71
72 /**
73 * @brief Generic interface for all hash functions.
74 *
75 * @b Constructors:
76 * - hasher_create()
77 *
78 * @ingroup hashers
79 */
80 struct hasher_t {
81 /**
82 * @brief Hash data and write it in the buffer.
83 *
84 * If the parameter hash is NULL, no result is written back
85 * an more data can be appended to already hashed data.
86 * If not, the result is written back and the hasher is reseted.
87 *
88 * The hash output parameter must hold at least
89 * hash_t.get_block_size() bytes.
90 *
91 * @param this calling object
92 * @param data data to hash
93 * @param[out] hash pointer where the hash will be written
94 */
95 void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
96
97 /**
98 * @brief Hash data and allocate space for the hash.
99 *
100 * If the parameter hash is NULL, no result is written back
101 * an more data can be appended to already hashed data.
102 * If not, the result is written back and the hasher is reseted.
103 *
104 * @param this calling object
105 * @param data chunk with data to hash
106 * @param[out] hash chunk which will hold allocated hash
107 */
108 void (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
109
110 /**
111 * @brief Get the size of the resulting hash.
112 *
113 * @param this calling object
114 * @return hash size in bytes
115 */
116 size_t (*get_hash_size) (hasher_t *this);
117
118 /**
119 * @brief Resets the hashers state.
120 *
121 * @param this calling object
122 */
123 void (*reset) (hasher_t *this);
124
125 /**
126 * @brief Get the state of the hasher.
127 *
128 * A hasher stores internal state information. This state may be
129 * manipulated to include a "seed" into the hashing operation. It used by
130 * some exotic protocols (such as AKA).
131 * The data pointed by chunk may be manipulated, but not replaced nor freed.
132 * This is more a hack than a feature. The hasher's state may be byte
133 * order dependant; use with care.
134 *
135 * @param this calling object
136 */
137 chunk_t (*get_state) (hasher_t *this);
138
139 /**
140 * @brief Destroys a hasher object.
141 *
142 * @param this calling object
143 */
144 void (*destroy) (hasher_t *this);
145 };
146
147 /**
148 * @brief Generic interface to create a hasher_t.
149 *
150 * @param hash_algorithm Algorithm to use for hashing
151 * @return
152 * - hasher_t object
153 * - NULL if algorithm not supported
154 *
155 * @ingroup hashers
156 */
157 hasher_t *hasher_create(hash_algorithm_t hash_algorithm);
158
159 #endif /* HASHER_H_ */