The introduced SHA1_NOFINAL hasher was not sufficient for EAP-AKA,
[strongswan.git] / src / libstrongswan / crypto / hashers / hasher.h
1 /*
2 * Copyright (C) 2005 Jan Hutter
3 * Copyright (C) 2005-2006 Martin Willi
4 *
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 /**
21 * @defgroup traffic_selector traffic_selector
22 * @{ @ingroup config
23 */
24
25 #ifndef HASHER_H_
26 #define HASHER_H_
27
28 typedef enum hash_algorithm_t hash_algorithm_t;
29 typedef struct hasher_t hasher_t;
30
31 #include <library.h>
32
33 /**
34 * Algorithms to use for hashing.
35 */
36 enum hash_algorithm_t {
37 /** not specified hash function */
38 HASH_UNKNOWN = 0,
39 /** preferred hash function, general purpose */
40 HASH_PREFERRED = 1,
41 HASH_MD2 = 2,
42 HASH_MD5 = 3,
43 HASH_SHA1 = 4,
44 HASH_SHA256 = 5,
45 HASH_SHA384 = 6,
46 HASH_SHA512 = 7,
47 };
48
49 #define HASH_SIZE_MD2 16
50 #define HASH_SIZE_MD5 16
51 #define HASH_SIZE_SHA1 20
52 #define HASH_SIZE_SHA256 32
53 #define HASH_SIZE_SHA384 48
54 #define HASH_SIZE_SHA512 64
55
56 /**
57 * enum names for hash_algorithm_t.
58 */
59 extern enum_name_t *hash_algorithm_names;
60
61 /**
62 * Generic interface for all hash functions.
63 */
64 struct hasher_t {
65 /**
66 * Hash data and write it in the buffer.
67 *
68 * If the parameter hash is NULL, no result is written back
69 * and more data can be appended to already hashed data.
70 * If not, the result is written back and the hasher is reset.
71 *
72 * The hash output parameter must hold at least
73 * hash_t.get_block_size() bytes.
74 *
75 * @param data data to hash
76 * @param hash pointer where the hash will be written
77 */
78 void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
79
80 /**
81 * Hash data and allocate space for the hash.
82 *
83 * If the parameter hash is NULL, no result is written back
84 * and more data can be appended to already hashed data.
85 * If not, the result is written back and the hasher is reset.
86 *
87 * @param data chunk with data to hash
88 * @param hash chunk which will hold allocated hash
89 */
90 void (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
91
92 /**
93 * Get the size of the resulting hash.
94 *
95 * @return hash size in bytes
96 */
97 size_t (*get_hash_size) (hasher_t *this);
98
99 /**
100 * Resets the hashers state.
101 */
102 void (*reset) (hasher_t *this);
103
104 /**
105 * Destroys a hasher object.
106 */
107 void (*destroy) (hasher_t *this);
108 };
109
110 /**
111 * Conversion of ASN.1 OID to hash algorithm.
112 *
113 * @param oid ASN.1 OID
114 * @return hash algorithm, HASH_UNKNOWN if OID unsuported
115 */
116 hash_algorithm_t hasher_algorithm_from_oid(int oid);
117
118 /**
119 * Conversion of hash algorithm into ASN.1 OID.
120 *
121 * @param alg hash algorithm
122 * @return ASN.1 OID, or OID_UNKNOW
123 */
124 int hasher_algorithm_to_oid(hash_algorithm_t alg);
125
126 /**
127 * Conversion of hash signature algorithm into ASN.1 OID.
128 *
129 * @param alg hash algorithm
130 * @return ASN.1 OID if, or OID_UNKNOW
131 */
132 int hasher_signature_algorithm_to_oid(hash_algorithm_t alg);
133
134 #endif /* HASHER_H_ @} */