]> git.ipfire.org Git - people/ms/strongswan.git/blob - Source/lib/crypto/hashers/hasher.h
- renamed get_block_size of hasher
[people/ms/strongswan.git] / Source / lib / crypto / hashers / hasher.h
1 /**
2 * @file hasher.h
3 *
4 * @brief Interface hasher_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef HASHER_H_
24 #define HASHER_H_
25
26
27 #include <types.h>
28
29
30 typedef enum hash_algorithm_t hash_algorithm_t;
31
32 /**
33 * @brief Algorithms to use for hashing.
34 *
35 * Currently only the following algorithms are implemented and therefore supported:
36 * - HASH_MD5
37 * - HASH_SHA1
38 *
39 * @ingroup hashers
40 *
41 */
42 enum hash_algorithm_t {
43 HASH_MD2,
44 /**
45 * Implemented in class md5_hasher_t.
46 */
47 HASH_MD5,
48 /**
49 * Implemented in class sha1_hasher_t.
50 */
51 HASH_SHA1,
52 HASH_SHA256,
53 HASH_SHA384,
54 HASH_SHA512,
55 };
56
57 /**
58 * String mappings for hash_algorithm_t.
59 */
60 extern mapping_t hash_algorithm_m[];
61
62
63 typedef struct hasher_t hasher_t;
64
65 /**
66 * @brief Generic interface for all hash functions.
67 *
68 * @b Constructors:
69 * - hasher_create()
70 * - md5_hasher_create()
71 * - sha1_hasher_create()
72 *
73 * @see
74 * - md5_hasher_t
75 * - sha1_hasher_t
76 *
77 * @todo Implement more hash algorithms
78 *
79 * @ingroup hashers
80 */
81 struct hasher_t {
82 /**
83 * @brief Hash data and write it in the buffer.
84 *
85 * If the parameter hash is NULL, no result is written back
86 * an more data can be appended to already hashed data.
87 * If not, the result is written back and the hasher is reseted.
88 *
89 * @warning: the hash output parameter must hold at least
90 * hash_t.get_block_size bytes.
91 *
92 * @param this calling object
93 * @param data data to hash
94 * @param[out] hash pointer where the hash will be written
95 */
96 void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
97
98 /**
99 * @brief Hash data and allocate space for the hash.
100 *
101 * If the parameter hash is NULL, no result is written back
102 * an more data can be appended to already hashed data.
103 * If not, the result is written back and the hasher is reseted.
104 *
105 * @param this calling object
106 * @param data chunk with data to hash
107 * @param[out] hash chunk which will hold allocated hash
108 */
109 void (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
110
111 /**
112 * @brief Get the size of the resulting hash.
113 *
114 * @param this calling object
115 * @return hash size in bytes
116 */
117 size_t (*get_hash_size) (hasher_t *this);
118
119 /**
120 * @brief Resets the hashers state, which allows
121 * computation of a completely new hash.
122 *
123 * @param this calling object
124 */
125 void (*reset) (hasher_t *this);
126
127 /**
128 * @brief Destroys a hasher object.
129 *
130 * @param this calling object
131 */
132 void (*destroy) (hasher_t *this);
133 };
134
135 /**
136 * @brief Generic interface to create a hasher_t.
137 *
138 * @param hash_algorithm Algorithm to use for hashing
139 * @return
140 * - hasher_t object
141 * - NULL if algorithm not supported
142 *
143 * @ingroup hashers
144 */
145 hasher_t *hasher_create(hash_algorithm_t hash_algorithm);
146
147 #endif /*HASHER_H_*/