src/server/util/hashing.c

changeset 595
2c316612f648
parent 594
36a46311e0f6
child 604
886658ba8f25
equal deleted inserted replaced
594:36a46311e0f6 595:2c316612f648
212 CC_SHA512_Final(buf, ctx); 212 CC_SHA512_Final(buf, ctx);
213 } 213 }
214 214
215 #endif // WS_USE_CRYPTO_COMMON 215 #endif // WS_USE_CRYPTO_COMMON
216 216
217 #ifdef WS_USE_CRYPTO_CNG
218
219 static int cng_hash_init(WinBCryptSHACTX *ctx, LPCWSTR algId) {
220 if(BCryptOpenAlgorithmProvider(&ctx->hAlg, algId, NULL, 0)) {
221 return 1;
222 }
223
224 ULONG hashObjectLen;
225 ULONG result;
226 if(BCryptGetProperty(ctx->hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&hashObjectLen, sizeof(DWORD), &result, 0)) {
227 cng_cleanup(ctx->hAlg, NULL, NULL, NULL);
228 return 1;
229 }
230
231 ctx->pbHashObject = calloc(1, hashObjectLen);
232 if(!ctx->pbHashObject) {
233 cng_cleanup(ctx->hAlg, NULL, NULL, NULL);
234 return 1;
235 }
236
237 if(BCryptCreateHash(ctx->hAlg, &ctx->hHash, ctx->pbHashObject, hashObjectLen, NULL, 0, 0)) {
238 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
239 return 1;
240 }
241
242 return 0;
243 }
244
245 static void cng_cleanup(BCRYPT_ALG_HANDLE hAesAlg, BCRYPT_KEY_HANDLE hKey, BCRYPT_HASH_HANDLE hHash, void *pbObject) {
246 if(hAesAlg) {
247 BCryptCloseAlgorithmProvider(hAesAlg,0);
248 }
249 if(hKey) {
250 BCryptDestroyKey(hKey);
251 }
252 if(hHash) {
253 BCryptDestroyHash(hHash);
254 }
255 if(pbObject) {
256 free(pbObject);
257 }
258 }
259
260 int ws_sha1_init(WS_SHA1_CTX *ctx) {
261 return cng_hash_init(ctx, BCRYPT_SHA1_ALGORITHM);
262 }
263
264 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len) {
265 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
266 }
267
268 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf) {
269 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
270
271 // cleanup
272 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
273 }
274
275 int ws_sha256_init(WS_SHA256_CTX *ctx) {
276 return cng_hash_init(ctx, BCRYPT_SHA256_ALGORITHM);
277 }
278
279 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len) {
280 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
281 }
282
283 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf) {
284 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
285
286 // cleanup
287 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
288 }
289
290 int ws_sha512_init(WS_SHA512_CTX *ctx) {
291 return cng_hash_init(ctx, BCRYPT_SHA512_ALGORITHM);
292 }
293
294 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len) {
295 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
296 }
297
298 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf) {
299 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
300
301 // cleanup
302 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
303 }
304
305 #endif // WS_USE_CRYPTO_CNG

mercurial