7111f5a9eafc7f4eb62f08e21d81f35f1bbccdb1
[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
28 #include <types.h>
29
30
31 typedef enum hash_algorithm_t hash_algorithm_t;
32
33 /**
34 * @brief Algorithms to use for hashing.
35 *
36 * Currently only the following algorithms are implemented and therefore supported:
37 * - HASH_MD5
38 * - HASH_SHA1
39 *
40 * @ingroup hashers
41 *
42 */
43 enum hash_algorithm_t {
44 HASH_MD2,
45 /**
46 * Implemented in class md5_hasher_t.
47 */
48 HASH_MD5,
49 /**
50 * Implemented in class sha1_hasher_t.
51 */
52 HASH_SHA1,
53 HASH_SHA256,
54 HASH_SHA384,
55 HASH_SHA512,
56 };
57
58 /**
59 * String mappings for hash_algorithm_t.
60 */
61 extern mapping_t hash_algorithm_m[];
62
63
64 typedef struct hasher_t hasher_t;
65
66 /**
67 * @brief Generic interface for all hash functions.
68 *
69 * @b Constructors:
70 * - hasher_create()
71 * - md5_hasher_create()
72 * - sha1_hasher_create()
73 *
74 * @see
75 * - md5_hasher_t
76 * - sha1_hasher_t
77 *
78 * @todo Implement more hash algorithms
79 *
80 * @ingroup hashers
81 */
82 struct hasher_t {
83 /**
84 * @brief Hash data and write it in the buffer.
85 *
86 * If the parameter hash is NULL, no result is written back
87 * an more data can be appended to already hashed data.
88 * If not, the result is written back and the hasher is reseted.
89 *
90 * @warning: the hash output parameter must hold at least
91 * hash_t.get_block_size bytes.
92 *
93 * @param this calling object
94 * @param data data to hash
95 * @param[out] hash pointer where the hash will be written
96 */
97 void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
98
99 /**
100 * @brief Hash data and allocate space for the hash.
101 *
102 * If the parameter hash is NULL, no result is written back
103 * an more data can be appended to already hashed data.
104 * If not, the result is written back and the hasher is reseted.
105 *
106 * @param this calling object
107 * @param data chunk with data to hash
108 * @param[out] hash chunk which will hold allocated hash
109 */
110 void (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
111
112 /**
113 * @brief Get the size of the resulting hash.
114 *
115 * @param this calling object
116 * @return hash size in bytes
117 */
118 size_t (*get_hash_size) (hasher_t *this);
119
120 /**
121 * @brief Resets the hashers state, which allows
122 * computation of a completely new hash.
123 *
124 * @param this calling object
125 */
126 void (*reset) (hasher_t *this);
127
128 /**
129 * @brief Destroys a hasher object.
130 *
131 * @param this calling object
132 */
133 void (*destroy) (hasher_t *this);
134 };
135
136 /**
137 * @brief Generic interface to create a hasher_t.
138 *
139 * @param hash_algorithm Algorithm to use for hashing
140 * @return
141 * - hasher_t object
142 * - NULL if algorithm not supported
143 *
144 * @ingroup hashers
145 */
146 hasher_t *hasher_create(hash_algorithm_t hash_algorithm);
147
148 #endif /*HASHER_H_*/