Thu, 29 May 2025 15:17:01 +0200
add sha512 hashing functions
|
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 | 34 | WS_SHA1_CTX *ctx = malloc(sizeof(WS_SHA1_CTX)); |
| 35 | if(!ctx) { | |
| 36 | return NULL; | |
| 37 | } | |
| 38 | if(ws_sha1_init(ctx)) { | |
| 39 | free(ctx); | |
| 40 | return NULL; | |
| 41 | } | |
| 42 | return ctx; | |
| 43 | } | |
| 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 | 46 | WS_SHA256_CTX *ctx = malloc(sizeof(WS_SHA256_CTX)); |
| 47 | if(!ctx) { | |
| 48 | return NULL; | |
| 49 | } | |
| 50 | if(ws_sha256_init(ctx)) { | |
| 51 | free(ctx); | |
| 52 | return NULL; | |
| 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 | |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | |
| 587 | 71 | void ws_sha1_init(WS_SHA1_CTX *ctx) { |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | SHA256_Init(ctx); |
| 587 | 73 | return 0; |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | |
| 587 | 76 | void ws_sha1_update(WS_SHA1_CTX *ctx, const void *data, size_t length) { |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | SHA256_Update(ctx, data, length); |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | |
| 587 | 80 | void ws_sha1_final(char *md, WS_SHA1_CTX *ctx) { |
| 81 | SHA_Final(md, ctx); | |
| 82 | } | |
| 83 | ||
|
589
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
84 | void ws_sha256_init(WS_SHA256_CTX *ctx) { |
| 587 | 85 | SHA_Init(ctx); |
| 86 | return 0; | |
| 87 | } | |
| 88 | ||
|
589
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
89 | void ws_sha256_update(WS_SHA256_CTX *ctx, const void *data, size_t length) { |
| 587 | 90 | SHA_Update(ctx, data, length); |
| 91 | } | |
| 92 | ||
|
589
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
93 | void ws_sha256_final(char *md, WS_SHA256_CTX *ctx) { |
| 587 | 94 | SHA_Final(md, ctx); |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | |
|
589
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
97 | void ws_sha512_init(WS_SHA512_CTX *ctx) { |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
98 | SHA512_Init(ctx); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
99 | return 0; |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
100 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
101 | |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
102 | 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
|
103 | SHA512_Update(ctx, data, length); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
104 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
105 | |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
106 | 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
|
107 | SHA512_Final(md, ctx); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
108 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
109 | |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | #else |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | |
| 587 | 112 | 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
|
113 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); |
| 587 | 114 | if(!mdctx) { |
| 115 | return 1; | |
| 116 | } | |
| 117 | EVP_DigestInit_ex(mdctx, EVP_sha1(), NULL); | |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | *ctx = mdctx; |
| 587 | 119 | return 0; |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | |
| 587 | 122 | 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
|
123 | EVP_DigestUpdate(*ctx, data, length); |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | |
| 587 | 126 | void ws_sha1_final(WS_SHA256_CTX *ctx, unsigned char *md) { |
| 127 | EVP_DigestFinal(*ctx, md, NULL); | |
| 128 | } | |
| 129 | ||
| 130 | int ws_sha256_init(WS_SHA256_CTX *ctx) { | |
| 131 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); | |
| 132 | if(!mdctx) { | |
| 133 | return 1; | |
| 134 | } | |
| 135 | EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); | |
| 136 | *ctx = mdctx; | |
| 137 | return 0; | |
| 138 | } | |
| 139 | ||
| 140 | void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { | |
| 141 | EVP_DigestUpdate(*ctx, data, length); | |
| 142 | } | |
| 143 | ||
| 144 | 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
|
145 | EVP_DigestFinal(*ctx, md, NULL); |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | |
|
589
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
148 | int ws_sha512_init(WS_SHA512_CTX *ctx) { |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
149 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
150 | if(!mdctx) { |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
151 | return 1; |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
152 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
153 | EVP_DigestInit_ex(mdctx, EVP_sha512(), NULL); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
154 | *ctx = mdctx; |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
155 | return 0; |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
156 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
157 | |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
158 | 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
|
159 | EVP_DigestUpdate(*ctx, data, length); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
160 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
161 | |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
162 | 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
|
163 | EVP_DigestFinal(*ctx, md, NULL); |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
164 | } |
|
70ad04769cbf
add sha512 hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
587
diff
changeset
|
165 | |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | #endif |