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