1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
static int
with_n(int (*initfunc)(struct libhashsum_hasher *, size_t), struct libhashsum_hasher *this, const char *algorithm)
{
const char *p;
size_t n = 0, digit;
p = strchr(algorithm, '[');
if (!p || *++p == ']')
return initfunc(this, 0);
if (*p++ != 'n' && *p++ != 'N')
goto einval;
if (*p++ != '=' || !('1' <= *p && *p <= '9'))
goto einval;
while (isdigit(*p)) {
digit = (size_t)(*p++ & 15);
if (n > (SIZE_MAX - digit) / 10U)
goto einval;
n = n * 10U + digit;
}
if (*p++ != ']' || *p)
goto einval;
return initfunc(this, n);
einval:
errno = EINVAL;
return -1;
}
static int
parse_value(size_t *valp, const char *p, const char **end_out)
{
size_t digit;
if (*valp)
return -1;
if (*p++ != '=' || !('1' <= *p && *p <= '9'))
return -1;
while (isdigit(*p)) {
digit = (size_t)(*p++ & 15);
if (*valp > (SIZE_MAX - digit) / 10U)
return -1;
*valp = *valp * 10U + digit;
}
*end_out = p;
return 0;
}
static int
with_rcnz(int (*initfunc)(struct libhashsum_hasher *, size_t, size_t, size_t, size_t),
struct libhashsum_hasher *this, const char *algorithm)
{
const char *p;
size_t r = 0, c = 0, n = 0, z = 0;
p = strchr(algorithm, '[');
if (!p || *++p == ']')
return initfunc(this, 0, 0, 0, 0);
for (;;) {
if (*p == 'r' || *p == 'R') {
if (parse_value(&r, &p[1], &p))
goto einval;
} else if (*p == 'c' || *p == 'C') {
if (parse_value(&c, &p[1], &p))
goto einval;
} else if (*p == 'n' || *p == 'N') {
if (parse_value(&n, &p[1], &p))
goto einval;
} else if (*p == 'z' || *p == 'Z') {
if (parse_value(&z, &p[1], &p))
goto einval;
} else if (*p == ']') {
break;
} else {
goto einval;
}
if (*p == ']')
break;
if (*p++ != ',')
goto einval;
}
if (p[1])
goto einval;
return initfunc(this, r, c, n, z);
einval:
errno = EINVAL;
return -1;
}
static int
with_salt(int (*initfunc)(struct libhashsum_hasher *, const void *),
struct libhashsum_hasher *this, const char *algorithm, size_t saltbytes)
{
const char *p;
uint8_t a, b, salt[32];
size_t salti = 0;
p = strchr(algorithm, '[');
if (!p || *++p == ']')
return initfunc(this, NULL);
if (*p++ != 's' && *p++ != 'S')
goto einval;
if (*p++ != 'a' && *p++ != 'A')
goto einval;
if (*p++ != 'l' && *p++ != 'L')
goto einval;
if (*p++ != 't' && *p++ != 'T')
goto einval;
if (*p++ != '=')
goto einval;
if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
p = &p[2];
for (;;) {
if (isxdigit(p[0]) && isxdigit(p[1])) {
if (!saltbytes)
goto einval;
saltbytes--;
} else if (isxdigit(p[0])) {
goto einval;
} else if (saltbytes) {
goto einval;
} else {
break;
}
a = (uint8_t)(((*p & 15) + (*p > '9' ? 9 : 0)) << 4);
b = (uint8_t)(((*p & 15) + (*p > '9' ? 9 : 0)) << 0);
salt[salti++] = (uint8_t)(a | b);
}
if (*p++ != ']' || *p)
goto einval;
return initfunc(this, salt);
einval:
errno = EINVAL;
return -1;
}
int
libhashsum_init_hasher_from_string(struct libhashsum_hasher *this, const char *algorithm)
{
enum libhashsum_algorithm algo;
if (!libhashsum_get_algorithm_string(&algo, algorithm)) {
errno = EINVAL;
return -1;
}
switch (algo) {
case LIBHASHSUM_MD2: return libhashsum_init_md2_hasher(this);
case LIBHASHSUM_MD4: return libhashsum_init_md4_hasher(this);
case LIBHASHSUM_MD5: return libhashsum_init_md5_hasher(this);
case LIBHASHSUM_RIPEMD_128: return libhashsum_init_ripemd_128_hasher(this);
case LIBHASHSUM_RIPEMD_160: return libhashsum_init_ripemd_160_hasher(this);
case LIBHASHSUM_RIPEMD_256: return libhashsum_init_ripemd_256_hasher(this);
case LIBHASHSUM_RIPEMD_320: return libhashsum_init_ripemd_320_hasher(this);
case LIBHASHSUM_SHA0: return libhashsum_init_sha0_hasher(this);
case LIBHASHSUM_SHA1: return libhashsum_init_sha1_hasher(this);
case LIBHASHSUM_SHA_224: return libhashsum_init_sha_224_hasher(this);
case LIBHASHSUM_SHA_256: return libhashsum_init_sha_256_hasher(this);
case LIBHASHSUM_SHA_384: return libhashsum_init_sha_384_hasher(this);
case LIBHASHSUM_SHA_512: return libhashsum_init_sha_512_hasher(this);
case LIBHASHSUM_SHA_512_224: return libhashsum_init_sha_512_224_hasher(this);
case LIBHASHSUM_SHA_512_256: return libhashsum_init_sha_512_256_hasher(this);
case LIBHASHSUM_KECCAK_224: return libhashsum_init_keccak_224_hasher(this);
case LIBHASHSUM_KECCAK_256: return libhashsum_init_keccak_256_hasher(this);
case LIBHASHSUM_KECCAK_384: return libhashsum_init_keccak_384_hasher(this);
case LIBHASHSUM_KECCAK_512: return libhashsum_init_keccak_512_hasher(this);
case LIBHASHSUM_SHA3_224: return libhashsum_init_sha3_224_hasher(this);
case LIBHASHSUM_SHA3_256: return libhashsum_init_sha3_256_hasher(this);
case LIBHASHSUM_SHA3_384: return libhashsum_init_sha3_384_hasher(this);
case LIBHASHSUM_SHA3_512: return libhashsum_init_sha3_512_hasher(this);
case LIBHASHSUM_SHAKE128: return with_n(&libhashsum_init_shake128_hasher, this, algorithm);
case LIBHASHSUM_SHAKE256: return with_n(&libhashsum_init_shake256_hasher, this, algorithm);
case LIBHASHSUM_SHAKE512: return with_n(&libhashsum_init_shake512_hasher, this, algorithm);
case LIBHASHSUM_RAWSHAKE128: return with_n(&libhashsum_init_rawshake128_hasher, this, algorithm);
case LIBHASHSUM_RAWSHAKE256: return with_n(&libhashsum_init_rawshake256_hasher, this, algorithm);
case LIBHASHSUM_RAWSHAKE512: return with_n(&libhashsum_init_rawshake512_hasher, this, algorithm);
case LIBHASHSUM_KECCAK: return with_rcnz(&libhashsum_init_keccak_hasher, this, algorithm);
case LIBHASHSUM_BLAKE224: return with_salt(&libhashsum_init_blake224_hasher, this, algorithm, 16U);
case LIBHASHSUM_BLAKE256: return with_salt(&libhashsum_init_blake256_hasher, this, algorithm, 16U);
case LIBHASHSUM_BLAKE384: return with_salt(&libhashsum_init_blake384_hasher, this, algorithm, 32U);
case LIBHASHSUM_BLAKE512: return with_salt(&libhashsum_init_blake512_hasher, this, algorithm, 32U);
default:
abort();
}
}
|