src/server/util/hashing.c

Sun, 01 Jun 2025 21:50:24 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Jun 2025 21:50:24 +0200
changeset 604
886658ba8f25
parent 595
2c316612f648
child 605
07074daff678
permissions
-rw-r--r--

hopefully this actually fixes building with openssl < 3.0

586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2025 Olaf Wintermann. All rights reserved.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "hashing.h"
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdlib.h>
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
33 WS_SHA1_CTX* ws_sha1_create(void) {
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
34 WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX));
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
35 if(!ctx) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
36 return NULL;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
37 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
38 if(ws_sha1_init(ctx)) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
39 free(ctx);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
40 return NULL;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
41 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
42 return ctx;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
43 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
44
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
45 WS_SHA256_CTX* ws_sha256_create(void) {
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
46 WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX));
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
47 if(!ctx) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
48 return NULL;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
49 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
50 if(ws_sha256_init(ctx)) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
51 free(ctx);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
52 return NULL;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
53 }
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 return ctx;
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
57 WS_SHA1_CTX* ws_sha512_create(void) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
58 WS_SHA512_CTX *ctx = malloc(sizeof(WS_SHA512_CTX));
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
59 if(!ctx) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
60 return NULL;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
61 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
62 if(ws_sha512_init(ctx)) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
63 free(ctx);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
64 return NULL;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
65 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
66 return ctx;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
67 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
68
594
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
69 #ifdef WS_USE_OPENSSL
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
70
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 #if OPENSSL_VERSION_NUMBER < 0x30000000L
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
73 void ws_sha1_init(WS_SHA1_CTX *ctx) {
604
886658ba8f25 hopefully this actually fixes building with openssl < 3.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 595
diff changeset
74 SHA_Init(ctx);
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
75 return 0;
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
78 void ws_sha1_update(WS_SHA1_CTX *ctx, const void *data, size_t length) {
604
886658ba8f25 hopefully this actually fixes building with openssl < 3.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 595
diff changeset
79 SHA_Update(ctx, data, length);
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
82 void ws_sha1_final(char *md, WS_SHA1_CTX *ctx) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
83 SHA_Final(md, ctx);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
84 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
85
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
86 void ws_sha256_init(WS_SHA256_CTX *ctx) {
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
87 SHA_Init(ctx);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
88 return 0;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
89 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
90
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
91 void ws_sha256_update(WS_SHA256_CTX *ctx, const void *data, size_t length) {
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
92 SHA_Update(ctx, data, length);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
93 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
94
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
95 void ws_sha256_final(char *md, WS_SHA256_CTX *ctx) {
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
96 SHA_Final(md, ctx);
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
99 void ws_sha512_init(WS_SHA512_CTX *ctx) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
100 SHA512_Init(ctx);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
101 return 0;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
102 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
103
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
104 void ws_sha256_update(WS_SHA512_CTX *ctx, const void *data, size_t length) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
105 SHA512_Update(ctx, data, length);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
106 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
107
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
108 void ws_sha256_final(char *md, WS_SHA512_CTX *ctx) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
109 SHA512_Final(md, ctx);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
110 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
111
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 #else
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
114 int ws_sha1_init(WS_SHA256_CTX *ctx) {
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
116 if(!mdctx) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
117 return 1;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
118 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
119 EVP_DigestInit_ex(mdctx, EVP_sha1(), NULL);
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 *ctx = mdctx;
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
121 return 0;
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
124 void ws_sha1_update(WS_SHA256_CTX *ctx, const char *data, size_t length) {
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 EVP_DigestUpdate(*ctx, data, length);
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
587
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
128 void ws_sha1_final(WS_SHA256_CTX *ctx, unsigned char *md) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
129 EVP_DigestFinal(*ctx, md, NULL);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
130 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
131
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
132 int ws_sha256_init(WS_SHA256_CTX *ctx) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
133 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
134 if(!mdctx) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
135 return 1;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
136 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
137 EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
138 *ctx = mdctx;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
139 return 0;
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
140 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
141
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
142 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t length) {
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
143 EVP_DigestUpdate(*ctx, data, length);
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
144 }
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
145
c94800af0490 add sha1 API
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 586
diff changeset
146 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *md) {
586
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 EVP_DigestFinal(*ctx, md, NULL);
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }
9861e8ceaf96 add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
589
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
150 int ws_sha512_init(WS_SHA512_CTX *ctx) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
151 EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
152 if(!mdctx) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
153 return 1;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
154 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
155 EVP_DigestInit_ex(mdctx, EVP_sha512(), NULL);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
156 *ctx = mdctx;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
157 return 0;
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
158 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
159
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
160 void ws_sha512_update(WS_SHA256_CTX *ctx, const char *data, size_t length) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
161 EVP_DigestUpdate(*ctx, data, length);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
162 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
163
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
164 void ws_sha512_final(WS_SHA256_CTX *ctx, unsigned char *md) {
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
165 EVP_DigestFinal(*ctx, md, NULL);
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
166 }
70ad04769cbf add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 587
diff changeset
167
594
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
168 #endif // OPENSSL_VERSION_NUMBER < 0x30000000L
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
169
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
170 #endif // WS_USE_OPENSSL
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
171
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
172
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
173 #ifdef WS_USE_CRYPTO_COMMON
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
174
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
175 int ws_sha1_init(WS_SHA1_CTX *ctx) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
176 CC_SHA1_Init(ctx);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
177 return 0;
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
178 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
179
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
180 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
181 CC_SHA1_Update(ctx, data, len);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
182 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
183
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
184 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
185 CC_SHA1_Final(buf, ctx);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
186 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
187
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
188 int ws_sha256_init(WS_SHA256_CTX *ctx) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
189 CC_SHA256_Init(ctx);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
190 return 0;
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
191 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
192
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
193 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
194 CC_SHA256_Update(ctx, data, len);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
195 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
196
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
197 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
198 CC_SHA256_Final(buf, ctx);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
199 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
200
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
201
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
202 int ws_sha512_init(WS_SHA512_CTX *ctx) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
203 CC_SHA512_Init(ctx);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
204 return 0;
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
205 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
206
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
207 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
208 CC_SHA512_Update(ctx, data, len);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
209 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
210
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
211 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf) {
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
212 CC_SHA512_Final(buf, ctx);
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
213 }
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
214
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
215 #endif // WS_USE_CRYPTO_COMMON
36a46311e0f6 add common crypto hashing implementation for macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 589
diff changeset
216
595
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
217 #ifdef WS_USE_CRYPTO_CNG
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
218
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
219 static int cng_hash_init(WinBCryptSHACTX *ctx, LPCWSTR algId) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
220 if(BCryptOpenAlgorithmProvider(&ctx->hAlg, algId, NULL, 0)) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
221 return 1;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
222 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
223
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
224 ULONG hashObjectLen;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
225 ULONG result;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
226 if(BCryptGetProperty(ctx->hAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&hashObjectLen, sizeof(DWORD), &result, 0)) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
227 cng_cleanup(ctx->hAlg, NULL, NULL, NULL);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
228 return 1;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
229 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
230
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
231 ctx->pbHashObject = calloc(1, hashObjectLen);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
232 if(!ctx->pbHashObject) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
233 cng_cleanup(ctx->hAlg, NULL, NULL, NULL);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
234 return 1;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
235 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
236
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
237 if(BCryptCreateHash(ctx->hAlg, &ctx->hHash, ctx->pbHashObject, hashObjectLen, NULL, 0, 0)) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
238 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
239 return 1;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
240 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
241
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
242 return 0;
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
243 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
244
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
245 static void cng_cleanup(BCRYPT_ALG_HANDLE hAesAlg, BCRYPT_KEY_HANDLE hKey, BCRYPT_HASH_HANDLE hHash, void *pbObject) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
246 if(hAesAlg) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
247 BCryptCloseAlgorithmProvider(hAesAlg,0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
248 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
249 if(hKey) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
250 BCryptDestroyKey(hKey);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
251 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
252 if(hHash) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
253 BCryptDestroyHash(hHash);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
254 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
255 if(pbObject) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
256 free(pbObject);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
257 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
258 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
259
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
260 int ws_sha1_init(WS_SHA1_CTX *ctx) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
261 return cng_hash_init(ctx, BCRYPT_SHA1_ALGORITHM);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
262 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
263
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
264 void ws_sha1_update(WS_SHA1_CTX *ctx, const char *data, size_t len) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
265 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
266 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
267
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
268 void ws_sha1_final(WS_SHA1_CTX *ctx, unsigned char *buf) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
269 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
270
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
271 // cleanup
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
272 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
273 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
274
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
275 int ws_sha256_init(WS_SHA256_CTX *ctx) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
276 return cng_hash_init(ctx, BCRYPT_SHA256_ALGORITHM);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
277 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
278
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
279 void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t len) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
280 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
281 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
282
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
283 void ws_sha256_final(WS_SHA256_CTX *ctx, unsigned char *buf) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
284 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
285
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
286 // cleanup
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
287 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
288 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
289
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
290 int ws_sha512_init(WS_SHA512_CTX *ctx) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
291 return cng_hash_init(ctx, BCRYPT_SHA512_ALGORITHM);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
292 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
293
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
294 void ws_sha512_update(WS_SHA512_CTX *ctx, const char *data, size_t len) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
295 BCryptHashData(ctx->hHash, (PUCHAR)data, len, 0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
296 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
297
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
298 void ws_sha512_final(WS_SHA512_CTX *ctx, unsigned char *buf) {
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
299 BCryptFinishHash(ctx->hHash, (PUCHAR)buf, DAV_SHA256_DIGEST_LENGTH, 0);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
300
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
301 // cleanup
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
302 cng_cleanup(ctx->hAlg, NULL, ctx->hHash, ctx->pbHashObject);
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
303 }
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
304
2c316612f648 add crypto cng hashing implementation for windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 594
diff changeset
305 #endif // WS_USE_CRYPTO_CNG

mercurial