Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
hash.c File Reference
#include <openssl/evp.h>
#include "hash.h"
#include "insecure_memzero.h"
Include dependency graph for hash.c:

Go to the source code of this file.

Classes

struct  _hash_ctx
 

Typedefs

typedef struct _hash_ctx _hash_ctx
 
typedef struct _hash_ctxhash_ctx
 

Functions

const YH_INTERNAL EVP_MD * get_hash (hash_t hash)
 
bool hash_bytes (const uint8_t *in, size_t len, hash_t hash, uint8_t *out, size_t *out_len)
 
bool hash_create (_hash_ctx **ctx, hash_t hash)
 
bool hash_init (_hash_ctx *ctx)
 
bool hash_update (_hash_ctx *ctx, const uint8_t *in, size_t cb_in)
 
bool hash_final (_hash_ctx *ctx, uint8_t *out, size_t *pcb_out)
 
bool hash_destroy (_hash_ctx *ctx)
 

Typedef Documentation

◆ _hash_ctx

typedef struct _hash_ctx _hash_ctx

Definition at line 50 of file hash.h.

◆ hash_ctx

typedef struct _hash_ctx * hash_ctx

Definition at line 50 of file hash.h.

Function Documentation

◆ get_hash()

const YH_INTERNAL EVP_MD * get_hash ( hash_t hash)

Definition at line 42 of file hash.c.

42 {
43 switch (hash) {
44 case _NONE:
45 return NULL;
46
47 case _SHA1:
48 return EVP_sha1();
49
50 case _SHA256:
51 return EVP_sha256();
52
53 case _SHA384:
54 return EVP_sha384();
55
56 case _SHA512:
57 return EVP_sha512();
58
59 default:
60 return NULL;
61 }
62}
@ _SHA512
Definition hash.h:38
@ _SHA384
Definition hash.h:37
@ _SHA1
Definition hash.h:35
@ _NONE
Definition hash.h:34
@ _SHA256
Definition hash.h:36
Here is the caller graph for this function:

◆ hash_bytes()

bool hash_bytes ( const uint8_t * in,
size_t len,
hash_t hash,
uint8_t * out,
size_t * out_len )

Definition at line 90 of file hash.c.

91 {
92#ifndef _WIN32_BCRYPT
93
94 const EVP_MD *md;
95
96 uint32_t d_len;
97
98 md = get_hash(hash);
99 if (md == NULL) {
100 return false;
101 }
102
103 EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
104 EVP_DigestInit_ex(mdctx, md, NULL);
105 EVP_DigestUpdate(mdctx, in, len);
106 EVP_DigestFinal_ex(mdctx, out, &d_len);
107
108 *out_len = (uint16_t) d_len;
109
110 EVP_MD_CTX_destroy(mdctx);
111
112 return true;
113
114#else
115
116 bool res = false;
117 NTSTATUS status = 0;
118 LPCWSTR alg = NULL;
119 BCRYPT_ALG_HANDLE hAlg = 0;
120 BCRYPT_HASH_HANDLE hHash = 0;
121 DWORD cbHashObj = 0;
122 DWORD cbHash = 0;
123 DWORD cbData = 0;
124 PBYTE pbHashObj = NULL;
125
126 alg = get_hash(hash);
127 if (alg == NULL) {
128 return false;
129 }
130
131 if (!BCRYPT_SUCCESS(status =
132 BCryptOpenAlgorithmProvider(&hAlg, alg, NULL, 0))) {
133 goto cleanup;
134 }
135
136 if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH,
137 (PBYTE) &cbHashObj,
138 sizeof(DWORD), &cbData, 0))) {
139 goto cleanup;
140 }
141
142 if (!(pbHashObj = (PBYTE) malloc(cbHashObj))) {
143 goto cleanup;
144 }
145
146 if (!BCRYPT_SUCCESS(status = BCryptGetProperty(hAlg, BCRYPT_HASH_LENGTH,
147 (PBYTE) &cbHash, sizeof(DWORD),
148 &cbData, 0))) {
149 goto cleanup;
150 }
151
152 if (*out_len < cbHash) {
153 goto cleanup;
154 }
155
156 if (!BCRYPT_SUCCESS(status = BCryptCreateHash(hAlg, &hHash, pbHashObj,
157 cbHashObj, NULL, 0, 0))) {
158 goto cleanup;
159 }
160
161 if (!BCRYPT_SUCCESS(status = BCryptHashData(hHash, (PBYTE) in, len, 0))) {
162 goto cleanup;
163 }
164
165 if (!BCRYPT_SUCCESS(status = BCryptFinishHash(hHash, out, cbHash, 0))) {
166 goto cleanup;
167 }
168
169 *out_len = cbHash;
170 res = true;
171
172cleanup:
173
174 if (pbHashObj) {
175 free(pbHashObj);
176 }
177 if (hHash) {
178 BCryptDestroyHash(hHash);
179 }
180 if (hAlg) {
181 BCryptCloseAlgorithmProvider(hAlg, 0);
182 }
183
184 return res;
185
186#endif
187}
const YH_INTERNAL EVP_MD * get_hash(hash_t hash)
Definition hash.c:42
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
EVP_MD_CTX * mdctx
size_t out_len
size_t len
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_create()

bool hash_create ( _hash_ctx ** ctx,
hash_t hash )

Definition at line 189 of file hash.c.

189 {
190 bool res = false;
191 _hash_ctx *ctx_temp = NULL;
192
193#ifdef _WIN32_BCRYPT
194 NTSTATUS status = 0;
195 LPCWSTR alg = NULL;
196 DWORD cbHashObj = 0;
197 DWORD cbHash = 0;
198 DWORD cbData = 0;
199#else
200 const EVP_MD *md = NULL;
201#endif
202
203 if (!ctx) {
204 return false;
205 }
206
207 if (*ctx) {
208 return false;
209 }
210
211 if (!(ctx_temp = (_hash_ctx *) malloc(sizeof(_hash_ctx)))) {
212 return false;
213 }
214
215 insecure_memzero(ctx_temp, sizeof(_hash_ctx));
216
217#ifdef _WIN32_BCRYPT
218 if (!(alg = get_hash(hash))) {
219 goto cleanup;
220 }
221
222 if (!BCRYPT_SUCCESS(status = BCryptOpenAlgorithmProvider(&(ctx_temp->hAlg),
223 alg, NULL, 0))) {
224 goto cleanup;
225 }
226
227 if (!BCRYPT_SUCCESS(status =
228 BCryptGetProperty(ctx_temp->hAlg, BCRYPT_OBJECT_LENGTH,
229 (PBYTE) &cbHashObj, sizeof(DWORD),
230 &cbData, 0))) {
231 goto cleanup;
232 }
233
234 if (!(ctx_temp->pbHashObj = (PBYTE) malloc(cbHashObj))) {
235 goto cleanup;
236 }
237
238 if (!BCRYPT_SUCCESS(status =
239 BCryptGetProperty(ctx_temp->hAlg, BCRYPT_HASH_LENGTH,
240 (PBYTE) &cbHash, sizeof(DWORD),
241 &cbData, 0))) {
242 goto cleanup;
243 }
244
245 ctx_temp->cbHash = (size_t) cbHash;
246
247 if (!BCRYPT_SUCCESS(status =
248 BCryptCreateHash(ctx_temp->hAlg, &(ctx_temp->hHash),
249 ctx_temp->pbHashObj, cbHashObj, NULL,
250 0, BCRYPT_HASH_REUSABLE_FLAG))) {
251 goto cleanup;
252 }
253
254 ctx_temp->fFinal = true;
255
256#else
257 if (!(md = get_hash(hash))) {
258 goto cleanup;
259 }
260
261 if (!(ctx_temp->mdctx = EVP_MD_CTX_create())) {
262 goto cleanup;
263 }
264
265 ctx_temp->md = md;
266
267#endif
268
269 /* set output parameters */
270 *ctx = ctx_temp;
271 ctx_temp = NULL;
272 res = true;
273
274cleanup:
275
276 if (ctx_temp) {
277#ifdef _WIN32_BCRYPT
278 if (ctx_temp->hHash) {
279 BCryptDestroyHash(ctx_temp->hHash);
280 }
281 if (ctx_temp->pbHashObj) {
282 free(ctx_temp->pbHashObj);
283 }
284 if (ctx_temp->hAlg) {
285 BCryptCloseAlgorithmProvider(ctx_temp->hAlg, 0);
286 }
287#endif
288 free(ctx_temp);
289 }
290
291 return res;
292}
#define insecure_memzero(buf, len)
const EVP_MD * md
Definition hash.c:36
EVP_MD_CTX * mdctx
Definition hash.c:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_destroy()

bool hash_destroy ( _hash_ctx * ctx)

Definition at line 387 of file hash.c.

387 {
388 if (!ctx) {
389 return false;
390 }
391
392#ifdef _WIN32_BCRYPT
393 if (ctx->hHash) {
394 BCryptDestroyHash(ctx->hHash);
395 }
396 if (ctx->pbHashObj) {
397 free(ctx->pbHashObj);
398 }
399 if (ctx->hAlg) {
400 BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
401 }
402#else
403 if (ctx->mdctx) {
404 EVP_MD_CTX_destroy(ctx->mdctx);
405 }
406#endif
407
408 free(ctx);
409
410 return true;
411}
Here is the caller graph for this function:

◆ hash_final()

bool hash_final ( _hash_ctx * ctx,
uint8_t * out,
size_t * pcb_out )

Definition at line 351 of file hash.c.

351 {
352#ifdef _WIN32_BCRYPT
353 NTSTATUS status = 0;
354#else
355 uint32_t d_len = 0;
356#endif
357
358 if (!ctx) {
359 return false;
360 }
361
362#ifdef _WIN32_BCRYPT
363 if (!(ctx->hHash)) {
364 return false;
365 }
366
367 if (*pcb_out < ctx->cbHash) {
368 return false;
369 }
370
371 if (!BCRYPT_SUCCESS(status =
372 BCryptFinishHash(ctx->hHash, out, ctx->cbHash, 0))) {
373 return false;
374 }
375
376 *pcb_out = ctx->cbHash;
377
378#else
379 EVP_DigestFinal_ex(ctx->mdctx, out, &d_len);
380 *pcb_out = d_len;
381
382#endif
383
384 return true;
385}
Here is the caller graph for this function:

◆ hash_init()

bool hash_init ( _hash_ctx * ctx)

Definition at line 294 of file hash.c.

294 {
295 if (!ctx) {
296 return false;
297 }
298
299#ifdef _WIN32_BCRYPT
300 /* finalize the hash, it should be marked as reusable */
301 if (!ctx->fFinal) {
302 size_t cbHash = ctx->cbHash;
303 uint8_t *temp = (uint8_t *) malloc(cbHash);
304
305 if (temp) {
306 bool res = hash_final(ctx, temp, &cbHash);
307 free(temp);
308 return res;
309 }
310 }
311
312#else
313 EVP_DigestInit_ex(ctx->mdctx, ctx->md, NULL);
314#endif
315
316 return true;
317}
bool hash_final(_hash_ctx *ctx, uint8_t *out, size_t *pcb_out)
Definition hash.c:351
unsigned char uint8_t
Definition stdint.h:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash_update()

bool hash_update ( _hash_ctx * ctx,
const uint8_t * in,
size_t cb_in )

Definition at line 319 of file hash.c.

319 {
320#ifdef _WIN32_BCRYPT
321 NTSTATUS status = 0;
322#endif
323
324 if (!ctx) {
325 return false;
326 }
327
328#ifdef _WIN32_BCRYPT
329 if (!ctx->hHash) {
330 return false;
331 }
332
333 ctx->fFinal = true;
334
335 if (!BCRYPT_SUCCESS(status =
336 BCryptHashData(ctx->hHash, (PBYTE) in, cb_in, 0))) {
337 return false;
338 }
339
340#else
341 if (!(ctx->mdctx)) {
342 return false;
343 }
344
345 EVP_DigestUpdate(ctx->mdctx, in, cb_in);
346#endif
347
348 return true;
349}
Here is the caller graph for this function: