1 | /* |
2 | Copyright (C) 2018 MariaDB Corporation AB |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public |
15 | License along with this library; if not see <http://www.gnu.org/licenses> |
16 | or write to the Free Software Foundation, Inc., |
17 | 51 Franklin St., Fifth Floor, Boston, MA 02110, USA |
18 | */ |
19 | |
20 | #ifndef _ma_hash_h_ |
21 | #define _ma_hash_h_ |
22 | |
23 | #include <stddef.h> |
24 | #include <stdarg.h> |
25 | |
26 | /*! Hash algorithms */ |
27 | #define MA_HASH_MD5 1 |
28 | #define MA_HASH_SHA1 2 |
29 | #define MA_HASH_SHA224 3 |
30 | #define MA_HASH_SHA256 4 |
31 | #define MA_HASH_SHA384 5 |
32 | #define MA_HASH_SHA512 6 |
33 | #define MA_HASH_RIPEMD160 7 |
34 | |
35 | /*! Hash digest sizes */ |
36 | #define MA_MD5_HASH_SIZE 16 |
37 | #define MA_SHA1_HASH_SIZE 20 |
38 | #define MA_SHA224_HASH_SIZE 28 |
39 | #define MA_SHA256_HASH_SIZE 32 |
40 | #define MA_SHA384_HASH_SIZE 48 |
41 | #define MA_SHA512_HASH_SIZE 64 |
42 | #define MA_RIPEMD160_HASH_SIZE 20 |
43 | |
44 | #define MA_MAX_HASH_SIZE 64 |
45 | /** \typedef MRL hash context */ |
46 | |
47 | #if defined(WIN32) |
48 | #include <windows.h> |
49 | #include <bcrypt.h> |
50 | typedef struct { |
51 | char free_me; |
52 | BCRYPT_ALG_HANDLE hAlg; |
53 | BCRYPT_HASH_HANDLE hHash; |
54 | PBYTE hashObject; |
55 | DWORD digest_len; |
56 | } MA_HASH_CTX; |
57 | #elif defined(HAVE_OPENSSL) |
58 | typedef void MA_HASH_CTX; |
59 | #elif defined(HAVE_GNUTLS) |
60 | typedef struct { |
61 | void *ctx; |
62 | const struct nettle_hash *hash; |
63 | } MA_HASH_CTX; |
64 | #endif |
65 | |
66 | /** |
67 | @brief acquire and initialize new hash context |
68 | |
69 | @param[in] algorithm hash algorithm |
70 | @param[in] ctx pointer to a crypto context |
71 | |
72 | @return hash context on success, NULL on error |
73 | */ |
74 | MA_HASH_CTX *ma_hash_new(unsigned int algorithm, MA_HASH_CTX *ctx); |
75 | |
76 | /** |
77 | @brief release and deinitializes a hash context |
78 | |
79 | @param[in] hash context |
80 | |
81 | @return void |
82 | */ |
83 | void ma_hash_free(MA_HASH_CTX *ctx); |
84 | |
85 | /** |
86 | @brief hashes len bytes of data into the hash context. |
87 | This function can be called several times on same context to |
88 | hash additional data. |
89 | |
90 | @param[in] ctx hash context |
91 | @param[in] buffer data buffer |
92 | @param[in] len size of buffer |
93 | |
94 | @return void |
95 | */ |
96 | void ma_hash_input(MA_HASH_CTX *ctx, |
97 | const unsigned char *buffer, |
98 | size_t len); |
99 | |
100 | /** |
101 | @brief retrieves the hash value from hash context |
102 | |
103 | @param[in] ctx hash context |
104 | @param[out] digest digest containing hash value |
105 | |
106 | @return void |
107 | */ |
108 | void ma_hash_result(MA_HASH_CTX *ctx, unsigned char *digest); |
109 | |
110 | |
111 | /** |
112 | @brief returns digest size for a given hash algorithm |
113 | |
114 | @param[in] hash algorithm |
115 | |
116 | @retuns digest size or 0 on error |
117 | */ |
118 | static inline size_t ma_hash_digest_size(unsigned int hash_alg) |
119 | { |
120 | switch(hash_alg) { |
121 | case MA_HASH_MD5: |
122 | return MA_MD5_HASH_SIZE; |
123 | case MA_HASH_SHA1: |
124 | return MA_SHA1_HASH_SIZE; |
125 | case MA_HASH_SHA224: |
126 | return MA_SHA224_HASH_SIZE; |
127 | case MA_HASH_SHA256: |
128 | return MA_SHA256_HASH_SIZE; |
129 | case MA_HASH_SHA384: |
130 | return MA_SHA384_HASH_SIZE; |
131 | case MA_HASH_SHA512: |
132 | return MA_SHA512_HASH_SIZE; |
133 | case MA_HASH_RIPEMD160: |
134 | return MA_RIPEMD160_HASH_SIZE; |
135 | default: |
136 | return 0; |
137 | } |
138 | } |
139 | |
140 | /** |
141 | @brief function to compute hash from buffer. |
142 | |
143 | @param[in] hash_alg hash algorithm |
144 | @param[in] buffer buffer |
145 | @param[in] buffer_leng length of buffer |
146 | @param[out] digest computed hash digest |
147 | |
148 | @return void |
149 | */ |
150 | static inline void ma_hash(unsigned int algorithm, |
151 | const unsigned char *buffer, |
152 | size_t buffer_length, |
153 | unsigned char *digest) |
154 | { |
155 | MA_HASH_CTX *ctx= NULL; |
156 | #ifdef HAVE_SCHANNEL |
157 | MA_HASH_CTX dctx; |
158 | ctx= &dctx; |
159 | #endif |
160 | ctx= ma_hash_new(algorithm, ctx); |
161 | ma_hash_input(ctx, buffer, buffer_length); |
162 | ma_hash_result(ctx, digest); |
163 | ma_hash_free(ctx); |
164 | } |
165 | |
166 | #endif /* _ma_hash_h_ */ |
167 | |