Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/digest.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2013-2018 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <stdlib.h>
22
#if defined(HAVE_STDINT_H)
23
# include <stdint.h>
24
#elif defined(HAVE_INTTYPES_H)
25
# include <inttypes.h>
26
#endif
27
#include <unistd.h>
28
#include <errno.h>
29
30
#include <sudo_compat.h>
31
#include <sudo_debug.h>
32
#include <sudo_digest.h>
33
34
#ifdef HAVE_SHA224UPDATE
35
# include <sha2.h>
36
#else
37
# include <compat/sha2.h>
38
#endif
39
40
static struct digest_function {
41
const size_t digest_len;
42
void (*init)(SHA2_CTX *);
43
#ifdef SHA2_VOID_PTR
44
void (*update)(SHA2_CTX *, const void *, size_t);
45
void (*final)(void *, SHA2_CTX *);
46
#else
47
void (*update)(SHA2_CTX *, const unsigned char *, size_t);
48
void (*final)(unsigned char *, SHA2_CTX *);
49
#endif
50
} digest_functions[] = {
51
{
52
SHA224_DIGEST_LENGTH,
53
SHA224Init,
54
SHA224Update,
55
SHA224Final
56
}, {
57
SHA256_DIGEST_LENGTH,
58
SHA256Init,
59
SHA256Update,
60
SHA256Final
61
}, {
62
SHA384_DIGEST_LENGTH,
63
SHA384Init,
64
SHA384Update,
65
SHA384Final
66
}, {
67
SHA512_DIGEST_LENGTH,
68
SHA512Init,
69
SHA512Update,
70
SHA512Final
71
}, {
72
0
73
}
74
};
75
76
struct sudo_digest {
77
struct digest_function *func;
78
SHA2_CTX ctx;
79
};
80
81
struct sudo_digest *
82
sudo_digest_alloc_v1(unsigned int digest_type)
83
{
84
debug_decl(sudo_digest_alloc, SUDO_DEBUG_UTIL);
85
struct digest_function *func = NULL;
86
struct sudo_digest *dig;
87
unsigned int i;
88
89
for (i = 0; digest_functions[i].digest_len != 0; i++) {
90
if (digest_type == i) {
91
func = &digest_functions[i];
92
break;
93
}
94
}
95
if (func == NULL) {
96
errno = EINVAL;
97
debug_return_ptr(NULL);
98
}
99
100
if ((dig = malloc(sizeof(*dig))) == NULL)
101
debug_return_ptr(NULL);
102
func->init(&dig->ctx);
103
dig->func = func;
104
105
debug_return_ptr(dig);
106
}
107
108
void
109
sudo_digest_free_v1(struct sudo_digest *dig)
110
{
111
debug_decl(sudo_digest_free, SUDO_DEBUG_UTIL);
112
113
free(dig);
114
115
debug_return;
116
}
117
118
void
119
sudo_digest_reset_v1(struct sudo_digest *dig)
120
{
121
debug_decl(sudo_digest_reset, SUDO_DEBUG_UTIL);
122
123
dig->func->init(&dig->ctx);
124
125
debug_return;
126
}
127
128
size_t
129
sudo_digest_getlen_v2(unsigned int digest_type)
130
{
131
debug_decl(sudo_digest_getlen, SUDO_DEBUG_UTIL);
132
unsigned int i;
133
134
for (i = 0; digest_functions[i].digest_len != 0; i++) {
135
if (digest_type == i)
136
debug_return_size_t(digest_functions[i].digest_len);
137
}
138
139
debug_return_size_t(0);
140
}
141
142
int
143
sudo_digest_getlen_v1(unsigned int digest_type)
144
{
145
size_t len = sudo_digest_getlen_v2(digest_type);
146
return len ? (int)len : -1;
147
}
148
149
void
150
sudo_digest_update_v1(struct sudo_digest *dig, const void *data, size_t len)
151
{
152
debug_decl(sudo_digest_update, SUDO_DEBUG_UTIL);
153
154
dig->func->update(&dig->ctx, data, len);
155
156
debug_return;
157
}
158
159
void
160
sudo_digest_final_v1(struct sudo_digest *dig, unsigned char *md)
161
{
162
debug_decl(sudo_digest_final, SUDO_DEBUG_UTIL);
163
164
dig->func->final(md, &dig->ctx);
165
166
debug_return;
167
}
168
169