Wed, 28 May 2025 21:09:45 +0200
add sha1 API
|
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 | |
| 587 | 33 | WS_SHA1_CTX* ws_sha256_create(void) { |
| 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 | ||
| 45 | WS_SHA256_CTX* ws_sha1_create(void) { | |
| 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 | |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | |
| 587 | 59 | 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
|
60 | SHA256_Init(ctx); |
| 587 | 61 | return 0; |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | |
| 587 | 64 | 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
|
65 | SHA256_Update(ctx, data, length); |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | |
| 587 | 68 | void ws_sha1_final(char *md, WS_SHA1_CTX *ctx) { |
| 69 | SHA_Final(md, ctx); | |
| 70 | } | |
| 71 | ||
| 72 | void ws_sha256_init(WS_SHA1_CTX *ctx) { | |
| 73 | SHA_Init(ctx); | |
| 74 | return 0; | |
| 75 | } | |
| 76 | ||
| 77 | void ws_sha256_update(WS_SHA1_CTX *ctx, const void *data, size_t length) { | |
| 78 | SHA_Update(ctx, data, length); | |
| 79 | } | |
| 80 | ||
| 81 | void ws_sha256_final(char *md, WS_SHA1_CTX *ctx) { | |
| 82 | SHA_Final(md, ctx); | |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | #else |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | |
| 587 | 87 | 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
|
88 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); |
| 587 | 89 | if(!mdctx) { |
| 90 | return 1; | |
| 91 | } | |
| 92 | EVP_DigestInit_ex(mdctx, EVP_sha1(), NULL); | |
|
586
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | *ctx = mdctx; |
| 587 | 94 | return 0; |
|
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 | |
| 587 | 97 | 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
|
98 | EVP_DigestUpdate(*ctx, data, length); |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | } |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | |
| 587 | 101 | void ws_sha1_final(WS_SHA256_CTX *ctx, unsigned char *md) { |
| 102 | EVP_DigestFinal(*ctx, md, NULL); | |
| 103 | } | |
| 104 | ||
| 105 | int ws_sha256_init(WS_SHA256_CTX *ctx) { | |
| 106 | EVP_MD_CTX *mdctx = EVP_MD_CTX_new(); | |
| 107 | if(!mdctx) { | |
| 108 | return 1; | |
| 109 | } | |
| 110 | EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); | |
| 111 | *ctx = mdctx; | |
| 112 | return 0; | |
| 113 | } | |
| 114 | ||
| 115 | void ws_sha256_update(WS_SHA256_CTX *ctx, const char *data, size_t length) { | |
| 116 | EVP_DigestUpdate(*ctx, data, length); | |
| 117 | } | |
| 118 | ||
| 119 | 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
|
120 | EVP_DigestFinal(*ctx, md, NULL); |
|
9861e8ceaf96
add wrapper for openssl hashing functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | } |
|
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 | #endif |