Debian Codegraph @

src/OVAL/probes/crapi/digest.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
24 #include <config.h>cxx . . $ `<file>/build/config.h`/
25 #endif
26
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36
37 #include "crapi.h"cxx . . $ `<file>/src/OVAL/probes/crapi/crapi.h`/
38 #include "digest.h"cxx . . $ `<file>/src/OVAL/probes/crapi/digest.h`/
39
40 #if defined(HAVE_NSS3)
41 #include <sechash.h>
42 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
43 #include <gcrypt.h>
44 #else
45 #error "No crypto library available!"
46 #endif
47
48 #if defined(HAVE_NSS3)
49 static int crapi_alg_t_to_lib_arg(crapi_alg_t alg)
50 {
51 switch (alg) {
52 #ifdef OPENSCAP_ENABLE_MD5
53 case CRAPI_DIGEST_MD5:
54 return HASH_AlgMD5;
55 #endif
56 #ifdef OPENSCAP_ENABLE_SHA1
57 case CRAPI_DIGEST_SHA1:
58 return HASH_AlgSHA1;
59 #endif
60 case CRAPI_DIGEST_SHA224:
61 return HASH_AlgSHA224;
62 case CRAPI_DIGEST_SHA256:
63 return HASH_AlgSHA256;
64 case CRAPI_DIGEST_SHA384:
65 return HASH_AlgSHA384;
66 case CRAPI_DIGEST_SHA512:
67 return HASH_AlgSHA512;
68 default:
69 return -1;
70 }
71 }
72 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
73 static int crapi_alg_t_to_lib_argcxx . . $ crapi_alg_t_to_lib_arg(9d38edfe78becda2).(crapi_alg_tcxx . . $ crapi_alg_t# alg)
74 {
75 switch (alg) {
76 #ifdef OPENSCAP_ENABLE_MD5
77 case CRAPI_DIGEST_MD5:
78 return GCRY_MD_MD5;
79 #endif
80 #ifdef OPENSCAP_ENABLE_SHA1
81 case CRAPI_DIGEST_SHA1:
82 return GCRY_MD_SHA1;
83 #endif
84 case CRAPI_DIGEST_SHA224cxx . . $ CRAPI_DIGEST_SHA224.:
85 return GCRY_MD_SHA224cxx . . $ GCRY_MD_SHA224.;
86 case CRAPI_DIGEST_SHA256cxx . . $ CRAPI_DIGEST_SHA256.:
87 return GCRY_MD_SHA256cxx . . $ GCRY_MD_SHA256.;
88 case CRAPI_DIGEST_SHA384cxx . . $ CRAPI_DIGEST_SHA384.:
89 return GCRY_MD_SHA384cxx . . $ GCRY_MD_SHA384.;
90 case CRAPI_DIGEST_SHA512cxx . . $ CRAPI_DIGEST_SHA512.:
91 return GCRY_MD_SHA512cxx . . $ GCRY_MD_SHA512.;
92 case CRAPI_DIGEST_RMD160cxx . . $ CRAPI_DIGEST_RMD160.:
93 return GCRY_MD_RMD160cxx . . $ GCRY_MD_RMD160.;
94 default:
95 return -1;
96 }
97 }
98 #endif
99
100 int crapi_digest_fdcxx . . $ crapi_digest_fd(3fe1f3ccb841b56).(int fd, crapi_alg_tcxx . . $ crapi_alg_t# alg, void *dst, size_t *size)
101 {
102 struct statcxx . . $ stat# st;
103 void *buffer;
104 size_t buflen;
105
106 if (size == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || dst == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
107 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
108 return -1;
109 }
110 int lib_alg = crapi_alg_t_to_lib_argcxx . . $ crapi_alg_t_to_lib_arg(9d38edfe78becda2).(alg);
111 #if defined(HAVE_NSS3)
112 if (*size < HASH_ResultLen(lib_alg)) {
113 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
114 if (*size < gcry_md_get_algo_dlencxx . . $ gcry_md_get_algo_dlen(46cc46841198e427).(lib_alg)) {
115 #endif
116 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOBUFScxx . . $ `<external>/17922322188510340118/errno.h:89:9`!;
117 return -1;
118 }
119
120 if (fstatcxx . . $ fstat(a43660ce2f600cb5). (fd, &st) != 0)
121 return (-1);
122 else {
123 #if _FILE_OFFSET_BITScxx . . $ `src/OVAL/probes/crapi/crapi.h:29:10`! == 32
124 buflen = st.st_sizecxx . . $ stat#st_size.;
125 # if defined(OS_FREEBSD)
126 buffer = mmap(NULL, buflen, PROT_READ, MAP_SHARED | MAP_NOCORE, fd, 0);
127 # else
128 buffer = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, buflen, PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!, MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!, fd, 0);
129 # endif
130 if (buffer == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
131 #endif /* _FILE_OFFSET_BITS == 32 */
132 uint8_tcxx . . $ uint8_t# _buffer[CRAPI_IO_BUFSZcxx . . $ `src/OVAL/probes/crapi/crapi.h:26:9`!];
133 ssize_tcxx . . $ ssize_t# ret;
134
135 buffer = _buffer;
136 #if defined(HAVE_NSS3)
137 HASHContext *ctx = HASH_Create (lib_alg);
138
139 if (ctx == NULL)
140 return (-1);
141 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
142 gcry_md_hd_tcxx . . $ gcry_md_hd_t# hd;
143 gcry_md_opencxx . . $ gcry_md_open(8076ef54c38e4b45). (&hd, lib_alg, 0);
144 #endif
145
146 while ((ret = readcxx . . $ read(7e34d0a36f355389).(fd, buffer, sizeof _buffer)) == sizeof _buffer)
147 #if defined(HAVE_NSS3)
148 HASH_Update(ctx, (const unsigned char *)buffer, (unsigned int) sizeof _buffer);
149 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
150 gcry_md_writecxx . . $ gcry_md_write(446ad22b1c1f596a).(hd, (const void *)buffer, sizeof _buffer);
151 #endif
152
153 switch (ret) {
154 case 0:
155 break;
156 case -1:
157 return (-1);
158 default:
159 if (ret <= 0) {
160 #if defined(HAVE_NSS3)
161 HASH_Destroy(ctx);
162 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
163 gcry_md_closecxx . . $ gcry_md_close(ed1decbd08e8af8).(hd);
164 #endif
165 return -1;
166 }
167 #if defined(HAVE_NSS3)
168 HASH_Update(ctx, (const unsigned char *)buffer, (unsigned int) ret);
169 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
170 gcry_md_writecxx . . $ gcry_md_write(446ad22b1c1f596a).(hd, (const void *)buffer, (size_t)ret);
171 #endif
172 }
173
174 #if defined(HAVE_NSS3)
175 HASH_End(ctx, dst, (unsigned int *)size, *size);
176 HASH_Destroy(ctx);
177 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
178 gcry_md_finalcxx . . $ `<external>/2486504861408398646/gcrypt.h:1465:9`!(hd);
179
180 buffer = (void *)gcry_md_readcxx . . $ gcry_md_read(f33f3819c393cdc5).(hd, lib_alg);
181 memcpycxx . . $ memcpy(cc817cb175f282d). (dst, buffer, gcry_md_get_algo_dlencxx . . $ gcry_md_get_algo_dlen(46cc46841198e427).(lib_alg));
182 gcry_md_closecxx . . $ gcry_md_close(ed1decbd08e8af8). (hd);
183 #endif
184
185 #if _FILE_OFFSET_BITScxx . . $ `src/OVAL/probes/crapi/crapi.h:29:10`! == 32
186 } else {
187 #if defined(HAVE_NSS3)
188 HASH_HashBuf(lib_alg, (unsigned char *)dst, (unsigned char *)buffer, (unsigned int)buflen);
189 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
190 gcry_md_hash_buffercxx . . $ gcry_md_hash_buffer(2c0816dc3af1d592).(lib_alg, dst, (const void *)buffer, buflen);
191 #endif
192 munmapcxx . . $ munmap(c0a5f96701d3f89).(buffer, buflen);
193 }
194 #endif /* _FILE_OFFSET_BITS == 32 */
195 }
196 return (0);
197 }
198
199 struct crapi_digest_ctxcxx . . $ crapi_digest_ctx# {
200 #if defined(HAVE_NSS3)
201 HASHContext *ctx;
202 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
203 gcry_md_hd_tcxx . . $ gcry_md_hd_t# ctxcxx . . $ crapi_digest_ctx#ctx.;
204 #endif
205 void *dstcxx . . $ crapi_digest_ctx#dst.;
206 size_t *sizecxx . . $ crapi_digest_ctx#size.;
207 };
208
209 struct digest_ctbl_tcxx . . $ digest_ctbl_t# {
210 struct crapi_digest_ctxcxx . . $ crapi_digest_ctx# *ctxcxx . . $ digest_ctbl_t#ctx.;
211 crapi_alg_tcxx . . $ crapi_alg_t# algcxx . . $ digest_ctbl_t#alg.;
212 };
213
214 static void *crapi_digest_initcxx . . $ crapi_digest_init(6e6d490cd5297fe9). (void *dst, void *size, crapi_alg_tcxx . . $ crapi_alg_t# alg)
215 {
216 struct crapi_digest_ctxcxx . . $ crapi_digest_ctx# *ctx = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(struct crapi_digest_ctxcxx . . $ crapi_digest_ctx#));
217
218 int lib_alg = crapi_alg_t_to_lib_argcxx . . $ crapi_alg_t_to_lib_arg(9d38edfe78becda2).(alg);
219 #if defined(HAVE_NSS3)
220 ctx->ctx = HASH_Create(lib_alg);
221 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
222 if (gcry_md_opencxx . . $ gcry_md_open(8076ef54c38e4b45).(&ctx->ctxcxx . . $ crapi_digest_ctx#ctx., lib_alg, 0) != 0) {
223 freecxx . . $ free(5d22bdacc48458e8).(ctx);
224 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
225 }
226 #endif
227 ctx->dstcxx . . $ crapi_digest_ctx#dst. = dst;
228 ctx->sizecxx . . $ crapi_digest_ctx#size. = size;
229
230 #if defined(HAVE_NSS3)
231 if (ctx->ctx != NULL) {
232 HASH_Begin(ctx->ctx);
233 } else {
234 free(ctx);
235 ctx = NULL;
236 }
237 #endif
238
239 return (ctx);
240 }
241
242 static int crapi_digest_updatecxx . . $ crapi_digest_update(1b7f15a9ca1f17b3).(struct crapi_digest_ctxcxx . . $ crapi_digest_ctx# *ctx, void *bptr, size_t blen)
243 {
244 #if defined(HAVE_NSS3)
245 HASH_Update(ctx->ctx, (const unsigned char *)bptr, (unsigned int)blen);
246 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
247 gcry_md_writecxx . . $ gcry_md_write(446ad22b1c1f596a).(ctx->ctxcxx . . $ crapi_digest_ctx#ctx., (const void *)bptr, blen);
248 #endif
249 return (0);
250 }
251
252 static int crapi_digest_finicxx . . $ crapi_digest_fini(449afe4c94cbd2e0).(struct crapi_digest_ctxcxx . . $ crapi_digest_ctx# *ctx, crapi_alg_tcxx . . $ crapi_alg_t# alg)
253 {
254 #if defined(HAVE_NSS3)
255 unsigned int result_len;
256 HASH_End(ctx->ctx, ctx->dst, &result_len, *ctx->size);
257 *ctx->size = result_len;
258 HASH_Destroy (ctx->ctx);
259 #elif defined(HAVE_GCRYPTcxx . . $ `build/config.h:18:9`!)
260 void *buffer;
261
262 gcry_md_finalcxx . . $ `<external>/2486504861408398646/gcrypt.h:1465:9`!(ctx->ctx);
263 int lib_alg = crapi_alg_t_to_lib_argcxx . . $ crapi_alg_t_to_lib_arg(9d38edfe78becda2).(alg);
264 buffer = (void *)gcry_md_readcxx . . $ gcry_md_read(f33f3819c393cdc5).(ctx->ctxcxx . . $ crapi_digest_ctx#ctx., lib_alg);
265 memcpycxx . . $ memcpy(cc817cb175f282d).(ctx->dstcxx . . $ crapi_digest_ctx#dst., buffer, gcry_md_get_algo_dlencxx . . $ gcry_md_get_algo_dlen(46cc46841198e427).(lib_alg));
266 gcry_md_closecxx . . $ gcry_md_close(ed1decbd08e8af8).(ctx->ctxcxx . . $ crapi_digest_ctx#ctx.);
267 #endif
268 freecxx . . $ free(5d22bdacc48458e8). (ctx);
269
270 return (0);
271 }
272
273 static void crapi_digest_freecxx . . $ crapi_digest_free(8cfeb4a99fc53c10).(struct crapi_digest_ctxcxx . . $ crapi_digest_ctx# *ctx)
274 {
275 #if defined(HAVE_NSS3)
276 HASH_Destroy(ctx->ctx);
277 free(ctx);
278 #endif
279 return;
280 }
281
282 int crapi_mdigest_fdcxx . . $ crapi_mdigest_fd(cac4efb70f20a0fb). (int fd, int num, ... /* crapi_alg_t alg, void *dst, size_t *size, ...*/)
283 {
284 register int i;
285 va_listcxx . . $ va_list# ap;
286 struct digest_ctbl_tcxx . . $ digest_ctbl_t# *ctbl = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(num * sizeof(struct digest_ctbl_tcxx . . $ digest_ctbl_t#));
287
288 crapi_alg_tcxx . . $ crapi_alg_t# alg;
289 void *dst;
290 size_t *size;
291
292 uint8_tcxx . . $ uint8_t# fd_buf[CRAPI_IO_BUFSZcxx . . $ `src/OVAL/probes/crapi/crapi.h:26:9`!];
293 ssize_tcxx . . $ ssize_t# ret;
294
295 if (num <= 0 || fd <= 0) {
296 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
297 freecxx . . $ free(5d22bdacc48458e8).(ctbl);
298 return -1;
299 }
300 for (i = 0; i < num; ++i)
301 ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
302
303 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(ap, num);
304
305 for (i = 0; i < num; ++i) {
306 alg = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(ap, crapi_alg_t);
307 dst = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(ap, void *);
308 size = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(ap, size_t *);
309
310 ctbl[i].algcxx . . $ digest_ctbl_t#alg. = alg;
311 if ((ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx. = crapi_digest_initcxx . . $ crapi_digest_init(6e6d490cd5297fe9).(dst, size, alg)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
312 *size = 0;
313 }
314
315 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`! (ap);
316
317 while ((ret = readcxx . . $ read(7e34d0a36f355389).(fd, fd_buf, sizeof fd_buf)) == sizeof fd_buf) {
318 for (i = 0; i < num; ++i) {
319 if (ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
320 continue;
321 if (crapi_digest_updatecxx . . $ crapi_digest_update(1b7f15a9ca1f17b3).(ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx., fd_buf, sizeof fd_buf) != 0) {
322 goto fail;
323 }
324 }
325 }
326
327 switch (ret) {
328 case 0:
329 break;
330 case -1:
331 goto fail;
332 default:
333 if (ret <= 0) {
334 freecxx . . $ free(5d22bdacc48458e8).(ctbl);
335 return -1;
336 }
337
338 for (i = 0; i < num; ++i) {
339 if (ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
340 continue;
341 if (crapi_digest_updatecxx . . $ crapi_digest_update(1b7f15a9ca1f17b3).(ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx., fd_buf, (size_t)ret) != 0) {
342 goto fail;
343 }
344 }
345 }
346
347 for (i = 0; i < num; ++i) {
348 if (ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
349 continue;
350 crapi_digest_finicxx . . $ crapi_digest_fini(449afe4c94cbd2e0).(ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx., ctbl[i].algcxx . . $ digest_ctbl_t#alg.);
351 }
352 freecxx . . $ free(5d22bdacc48458e8).(ctbl);
353 return (0);
354 fail:
355 for (i = 0; i < num; ++i) {
356 if (ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
357 crapi_digest_freecxx . . $ crapi_digest_free(8cfeb4a99fc53c10).(ctbl[i].ctxcxx . . $ digest_ctbl_t#ctx.);
358 }
359
360 freecxx . . $ free(5d22bdacc48458e8).(ctbl);
361 return (-1);
362 }