- moved algorithm definitions from payloads to corresponding transforms
[strongswan.git] / Source / charon / transforms / hashers / hasher.h
1 /**
2 * @file hasher.h
3 *
4 * @brief Interface for 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 typedef enum hash_algorithm_t hash_algorithm_t;
30
31 /**
32 * @brief Algorithms to use for hashing.
33 */
34 enum hash_algorithm_t {
35 HASH_SHA1,
36 HASH_MD5
37 };
38
39 /**
40 * string mappings for hash_algorithm_t
41 */
42 extern mapping_t hash_algorithm_m[];
43
44
45 typedef struct hasher_t hasher_t;
46
47 /**
48 * @brief Generic interface for all hash functions.
49 *
50 * @ingroup hashers
51 */
52 struct hasher_t {
53 /**
54 * @brief hash data and write it in the buffer
55 *
56 * If the parameter hash is NULL, no result is written back
57 * an more data can be appended to already hashed data.
58 * If not, the result is written back and the hasher is reset.
59 *
60 * @warning: the hash output parameter must hold at least
61 * hash_t.get_block_size bytes.
62 *
63 * @param this calling hasher
64 * @param data data to hash
65 * @param [out]buffer pointer where the hash will be written
66 * @return
67 * - SUCCESS in any case
68 */
69 status_t (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
70
71 /**
72 * @brief hash data and allocate space for the hash
73 *
74 * If the parameter hash is NULL, no result is written back
75 * an more data can be appended to already hashed data.
76 * If not, the result is written back and the hasher is reset.
77 *
78 * @param this calling hasher
79 * @param data chunk with data to hash
80 * @param [out]hash chunk which will hold allocated hash
81 * @return
82 * - SUCCESS in any case
83 * - OUT_OF_RES if space could not be allocated
84 */
85 status_t (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
86
87 /**
88 * @brief Get the block size of this hashing function.
89 *
90 * @param this calling hasher
91 * @return block size in bytes
92 */
93 size_t (*get_block_size) (hasher_t *this);
94
95 /**
96 * @brief Resets the hashers state, which allows
97 * computation of a completly new hash.
98 *
99 * @param this calling hasher
100 * @return - SUCCESS in any case
101 */
102 status_t (*reset) (hasher_t *this);
103
104 /**
105 * @brief Destroys a hasher object.
106 *
107 * @param this hasher_t object to destroy
108 * @return
109 * SUCCESS in any case
110 */
111 status_t (*destroy) (hasher_t *this);
112 };
113
114 /**
115 * @brief Generic interface to create a hasher_t.
116 *
117 * @param hash_algorithm Algorithm to use for hashing
118 * @return
119 * - hasher_t if successfully
120 * - NULL if out of ressources
121 *
122 * @ingroup hashers
123 */
124 hasher_t *hasher_create(hash_algorithm_t hash_algorithm);
125
126 #endif /*HASHER_H_*/