Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
hash.h File Reference
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <openssl/evp.h>
Include dependency graph for hash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define YH_INTERNAL   __attribute__((visibility("hidden")))
 

Enumerations

enum  hash_t {
  _NONE , _SHA1 , _SHA256 , _SHA384 ,
  _SHA512
}
 

Functions

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

Macro Definition Documentation

◆ YH_INTERNAL

#define YH_INTERNAL   __attribute__((visibility("hidden")))

Definition at line 42 of file hash.h.

Enumeration Type Documentation

◆ hash_t

enum hash_t
Enumerator
_NONE 
_SHA1 
_SHA256 
_SHA384 
_SHA512 

Definition at line 33 of file hash.h.

33 {
34 _NONE,
35 _SHA1,
36 _SHA256,
37 _SHA384,
38 _SHA512,
39} hash_t;
hash_t
Definition hash.h:33
@ _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

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}
Here is the caller graph for this function:

◆ hash_bytes()

bool YH_INTERNAL 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 YH_INTERNAL hash_create ( hash_ctx * ctx,
hash_t hash )

◆ hash_destroy()

bool YH_INTERNAL hash_destroy ( hash_ctx ctx)

◆ hash_final()

bool YH_INTERNAL hash_final ( hash_ctx ctx,
uint8_t * out,
size_t * pcb_out )

◆ hash_init()

bool YH_INTERNAL hash_init ( hash_ctx ctx)

◆ hash_update()

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