Debian Codegraph @

apps/kcapi-dgst.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <limits.h>
33
34 #include "app-internal.h"
35
36 struct opt_data {
37 const char *infile;
38 const char *outfile;
39 const char *ciphername;
40 const char *passwd;
41 const char *salt;
42 const char *pbkdf_hash;
43 int password_fd;
44 int key_fd;
45 uint32_t pbkdf_iterations;
46 bool keyed_md;
47 bool hexout;
48 };
49
50 static int cipher_op(struct kcapi_handle *handle, struct opt_data *opts)
51 {
52 int infd = -1, outfd = -1;
53 ssize_t ret = 0;
54 struct stat insb, outsb;
55 uint8_t *inmem = NULL;
56 uint8_t *outmem = NULL;
57 uint8_t tmpbuf[TMPBUFLEN] __aligned(KCAPI_APP_ALIGN);
58 uint32_t outlen = 0;
59
60 /*
61 * To avoid spurious padding, the buffer must be multiples of the
62 * block size.
63 */
64 BUILD_BUG_ON(TMPBUFLEN % 32);
65
66 /* Access input data */
67 if (opts->infile) {
68 infd = open(opts->infile, O_RDONLY | O_CLOEXEC);
69 if (infd < 0) {
70 dolog(KCAPI_LOG_ERR, "Cannot open file %s: %s",
71 opts->infile, strerror(errno));
72 return -EIO;
73 }
74 ret = check_filetype(infd, &insb, opts->infile);
75 if (ret)
76 goto out;
77 } else
78 infd = STDIN_FD;
79
80 /* Access output data */
81 if (opts->outfile) {
82 outfd = open(opts->outfile, O_RDWR | O_CLOEXEC | O_CREAT,
83 S_IRWXU | S_IRWXG | S_IRWXO);
84 if (outfd < 0) {
85 dolog(KCAPI_LOG_ERR, "Cannot open file %s: %s",
86 opts->outfile, strerror(errno));
87 ret = -EIO;
88 goto out;
89 }
90
91 ret = check_filetype(outfd, &outsb, opts->outfile);
92 if (ret)
93 goto out;
94 } else
95 outfd = STDOUT_FD;
96
97 if (infd == STDIN_FD) {
98 size_t tmpbuflen;
99
100 while ((tmpbuflen =
101 fread(tmpbuf, sizeof(uint8_t), TMPBUFLEN, stdin))) {
102
103 ret = kcapi_md_update(handle, tmpbuf, tmpbuflen);
104 if (ret < 0)
105 goto out;
106 }
107 } else if (insb.st_size) {
108 uint8_t *inmem_p;
109
110 inmem = mmap(NULL, (size_t)insb.st_size, PROT_READ, MAP_SHARED,
111 infd, 0);
112 if (inmem == MAP_FAILED) {
113 dolog(KCAPI_LOG_ERR, "Use of mmap for infd failed");
114 ret = -ENOMEM;
115 goto out;
116 }
117
118 inmem_p = inmem;
119 while (insb.st_size) {
120 size_t todo = (insb.st_size > INT_MAX) ? INT_MAX :
121 (size_t)insb.st_size;
122 ret = kcapi_md_update(handle, inmem_p, todo);
123 if (ret < 0)
124 goto out;
125 inmem_p += todo;
126 insb.st_size -= (off_t)todo;
127 }
128 }
129
130 outlen = kcapi_md_digestsize(handle);
131
132 if (opts->hexout)
133 outlen *= 2;
134
135 if (outfd == STDOUT_FD) {
136 ret = kcapi_md_final(handle, tmpbuf, TMPBUFLEN);
137 if (ret < 0)
138 goto out;
139
140 if ((uint32_t)ret != kcapi_md_digestsize(handle)) {
141 dolog(KCAPI_LOG_ERR,
142 "Unexpected digest output size: %ld (expected %u)\n",
143 ret, kcapi_md_digestsize(handle));
144 ret = -EFAULT;
145 goto out;
146 }
147
148 if (opts->hexout) {
149 bin2print(tmpbuf, kcapi_md_digestsize(handle), NULL,
150 stdout, 0);
151 } else {
152 if (fwrite(tmpbuf, sizeof(char), outlen,
153 stdout) != outlen) {
154 dolog(KCAPI_LOG_ERR, "Write failed %d", -errno);
155 ret = -errno;
156 goto out;
157 }
158 }
159
160 dolog(KCAPI_LOG_VERBOSE, "Digest of %ld bytes generated\n", ret);
161 } else {
162 if ((uint64_t)outsb.st_size != outlen) {
163 ret = ftruncate(outfd, outlen);
164 if (ret)
165 goto out;
166 }
167
168 outmem = mmap(NULL, outlen, PROT_WRITE, MAP_SHARED, outfd, 0);
169 if (outmem == MAP_FAILED) {
170 dolog(KCAPI_LOG_ERR, "Use of mmap for outfd failed");
171 ret = -ENOMEM;
172 goto out;
173 }
174
175 if (opts->hexout)
176 ret = kcapi_md_final(handle, tmpbuf,
177 kcapi_md_digestsize(handle));
178 else
179 ret = kcapi_md_final(handle, outmem,
180 kcapi_md_digestsize(handle));
181 if (ret < 0)
182 goto out;
183
184 if ((uint32_t)ret != kcapi_md_digestsize(handle)) {
185 dolog(KCAPI_LOG_ERR,
186 "Unexpected digest output size: %ld (expected %u)\n",
187 ret, kcapi_md_digestsize(handle));
188 ret = -EFAULT;
189 goto out;
190 }
191
192 if (opts->hexout)
193 bin2hex(tmpbuf, kcapi_md_digestsize(handle),
194 (char *)outmem, outlen, 0);
195
196 dolog(KCAPI_LOG_VERBOSE, "Digest of %ld bytes generated\n", ret);
197 }
198
199 out:
200 if (outmem && outmem != MAP_FAILED)
201 munmap(outmem, outlen);
202
203 if (inmem && inmem != MAP_FAILED)
204 munmap(inmem, (size_t)insb.st_size);
205
206 if (infd >= 0 && infd != STDIN_FD)
207 close(infd);
208 if (outfd >= 0 && outfd != STDOUT_FD)
209 close(outfd);
210
211 return (ret < 0) ? (int)ret : (int)kcapi_md_digestsize(handle);
212 }
213
214 static int set_key(struct kcapi_handle *handle, struct opt_data *opts)
215 {
216 uint8_t passwdbuf[128] __aligned(KCAPI_APP_ALIGN);
217 uint32_t passwdlen = 0;
218 uint8_t keybuf[32];
219 uint32_t keybuflen = 0;
220 int have_key = 0;
221 const uint8_t *passwdptr = NULL;
222 ssize_t ret;
223
224 /* Only set keys when needed */
225 if (!opts->keyed_md)
226 return 0;
227
228 /* Get password from command line */
229 if (opts->passwd) {
230 passwdptr = (uint8_t *)opts->passwd;
231 passwdlen = (uint32_t)strlen(opts->passwd);
232 }
233
234 /* Get password from password FD */
235 if (opts->password_fd != -1) {
236 ret = read_complete(opts->password_fd, passwdbuf,
237 sizeof(passwdbuf));
238 if (ret < 0)
239 goto out;
240
241 passwdbuf[sizeof(passwdbuf) - 1] = '\0';
242 passwdptr = passwdbuf;
243 passwdlen = (uint32_t)ret;
244 }
245
246 if (passwdptr && passwdlen) {
247 uint8_t *saltbuf = NULL;
248 uint32_t saltbuflen = 0;
249
250 dolog(KCAPI_LOG_DEBUG, "password %s", passwdptr);
251
252 if (!opts->pbkdf_iterations) {
253 opts->pbkdf_iterations =
254 kcapi_pbkdf_iteration_count(opts->pbkdf_hash, 0);
255
256 dolog(KCAPI_LOG_WARN, "PBKDF2 iterations used: %u",
257 opts->pbkdf_iterations);
258 }
259
260 if (opts->salt) {
261 ret = hex2bin_alloc(opts->salt,
262 (uint32_t)strlen(opts->salt),
263 &saltbuf, &saltbuflen);
264 if (ret)
265 goto out;
266 } else {
267 struct kcapi_handle *rng;
268 uint32_t j = 0;
269
270 ret = kcapi_rng_init(&rng, "stdrng", 0);
271 if (ret)
272 goto out;
273 ret = kcapi_rng_seed(rng, NULL, 0);
274 if (ret) {
275 kcapi_rng_destroy(rng);
276 goto out;
277 }
278
279 saltbuflen = 32;
280 saltbuf = malloc(saltbuflen);
281 if (!saltbuf) {
282 ret = -ENOMEM;
283 kcapi_rng_destroy(rng);
284 goto out;
285 }
286
287 while (j < saltbuflen) {
288 ret = kcapi_rng_generate(rng, saltbuf,
289 (size_t)saltbuflen);
290 if (ret < 0) {
291 kcapi_rng_destroy(rng);
292 free(saltbuf);
293 goto out;
294 }
295 j += (uint32_t)ret;
296 }
297 kcapi_rng_destroy(rng);
298
299 dolog_bin(KCAPI_LOG_WARN, saltbuf, saltbuflen,
300 "PBKDF2 salt used");
301 }
302
303 /* reading of sizeof(keybuf) implies 256 bit key */
304 ret = kcapi_pbkdf(opts->pbkdf_hash, passwdptr, passwdlen,
305 saltbuf, saltbuflen, opts->pbkdf_iterations,
306 keybuf, sizeof(keybuf));
307 free(saltbuf);
308 if (ret)
309 goto out;
310
311 have_key = 1;
312 keybuflen = sizeof(keybuf);
313
314 dolog(KCAPI_LOG_VERBOSE,
315 "Data Encryption Key derived from Password using PBKDF2 using %s with %u iterations",
316 opts->pbkdf_hash, opts->pbkdf_iterations);
317 }
318
319 /* Get key from key FD */
320 if (opts->key_fd != -1) {
321 ret = read_complete(opts->key_fd, keybuf, sizeof(keybuf));
322 if (ret < 0)
323 return (int)ret;
324
325 have_key = 1;
326 keybuflen = (uint32_t)ret;
327 }
328
329 if (!have_key) {
330 dolog(KCAPI_LOG_ERR, "No key found in input data");
331 ret = -EINVAL;
332 goto out;
333 }
334
335 dolog_bin(KCAPI_LOG_DEBUG, keybuf, keybuflen,
336 "keyed message digest key");
337
338 ret = kcapi_md_setkey(handle, keybuf, keybuflen);
339
340 out:
341 kcapi_memset_secure(passwdbuf, 0, sizeof(passwdbuf));
342 kcapi_memset_secure(keybuf, 0, sizeof(keybuf));
343
344 return (int)ret;
345 }
346
347 static void usage(void)
348 {
349 char version[30];
350 uint32_t ver = kcapi_version();
351
352 memset(version, 0, sizeof(version));
353 kcapi_versionstring(version, sizeof(version));
354
355 fprintf(stderr, "\nKernel Crypto API Message Digest Crypto Helper\n");
356 fprintf(stderr, "\nKernel Crypto API interface library version: %s\n", version);
357 fprintf(stderr, "Reported numeric version number %u\n\n", ver);
358 fprintf(stderr, "Usage:\n");
359 fprintf(stderr, "\t-c --cipher <NAME>\tCipher name to use for crypto operation\n");
360 fprintf(stderr, "\t-i --infile <FILE>\tFile with input data\n");
361 fprintf(stderr, "\t-o --outfile <FILE>\tFile with output data\n");
362 fprintf(stderr, "\t-s --salt <SALT>\tSalt for PBKDF2\n");
363 fprintf(stderr, "\t-p --passwd <PWD>\tPassword the session key is derived from using\n");
364 fprintf(stderr, "\t\t\t\tPBKDF2\n");
365 fprintf(stderr, "\t --passwdfd <FD>\tPassword file descriptor providing password\n");
366 fprintf(stderr, "\t --pbkdfiter <NUM>\tNumber of PBKDF2 iterations\n");
367 fprintf(stderr, "\t --pbkdfmac <MAC>\tMac for PBKDF2 (default: hmac(sha256))\n");
368 fprintf(stderr, "\t --keyfd <FD>\t\tKey file descriptor providing password\n");
369 fprintf(stderr, "\t --hex\t\tDigest is returned in hexadecimal notation\n");
370 fprintf(stderr, "\t-h --help\t\tThis help information\n");
371 fprintf(stderr, "\t --version\t\tPrint version\n");
372 fprintf(stderr, "\t-v --verbose\t\tVerbose logging, multiple options increase\n");
373 fprintf(stderr, "\t\t\t\tverbosity\n");
374 fprintf(stderr, "\t-q --quiet\t\tNo informational output - quiet operation\n");
375 fprintf(stderr, "\nData provided at stdin is hashed\n");
376 fprintf(stderr, "Data output at stdout\n");
377
378 exit(1);
379 }
380
381 static void parse_opts(int argc, char *argv[], struct opt_data *opts)
382 {
383 int c = 0;
384 char version[30];
385 unsigned long val = 0;
386 uint32_t verbosity = KCAPI_LOG_WARN;
387
388 memset(opts, 0, sizeof(*opts));
389 opts->password_fd = -1;
390 opts->key_fd = -1;
391
392 while (1) {
393 int opt_index = 0;
394 static struct option options[] = {
395 {"cipher", required_argument, 0, 'c'},
396 {"infile", required_argument, 0, 'i'},
397 {"outfile", required_argument, 0, 'o'},
398 {"salt", required_argument, 0, 's'},
399 {"passwd", required_argument, 0, 'p'},
400 {"passwdfd", required_argument, 0, 0},
401 {"pbkdfiter", required_argument, 0, 0},
402 {"pbkdfmac", required_argument, 0, 0},
403 {"keyfd", required_argument, 0, 0},
404 {"hex", no_argument, 0, 0},
405
406 {"verbose", no_argument, 0, 'v'},
407 {"quiet", no_argument, 0, 'q'},
408 {"help", no_argument, 0, 'h'},
409 {"version", no_argument, 0, 0},
410 {0, 0, 0, 0}
411 };
412 c = getopt_long(argc, argv, "c:i:o:s:p:vqh",
413 options, &opt_index);
414 if (-1 == c)
415 break;
416 switch (c) {
417 case 0:
418 switch (opt_index) {
419 case 0:
420 opts->ciphername = optarg;
421 break;
422 case 1:
423 opts->infile = optarg;
424 break;
425 case 2:
426 opts->outfile = optarg;
427 break;
428 case 3:
429 opts->salt = optarg;
430 break;
431 case 4:
432 opts->passwd = optarg;
433 break;
434 case 5:
435 val = strtoul(optarg, NULL, 10);
436 if (val == UINT_MAX) {
437 dolog(KCAPI_LOG_ERR,
438 "Passwd FD value too big");
439 usage();
440 }
441 opts->password_fd = (int)val;
442 break;
443 case 6:
444 val = strtoul(optarg, NULL, 10);
445 if (val == UINT_MAX) {
446 dolog(KCAPI_LOG_ERR,
447 "PBKDF2 iteration value too big");
448 usage();
449 }
450 opts->pbkdf_iterations = (uint32_t)val;
451 break;
452 case 7:
453 opts->pbkdf_hash = optarg;
454 break;
455 case 8:
456 val = strtoul(optarg, NULL, 10);
457 if (val == UINT_MAX) {
458 dolog(KCAPI_LOG_ERR,
459 "Passwd FD value too big");
460 usage();
461 }
462 opts->key_fd = (int)val;
463 break;
464 case 9:
465 opts->hexout = true;
466 break;
467
468 case 10:
469 verbosity++;
470 break;
471 case 11:
472 verbosity = KCAPI_LOG_NONE;
473 break;
474 case 12:
475 usage();
476 break;
477 case 13:
478 memset(version, 0, sizeof(version));
479 kcapi_versionstring(version, sizeof(version));
480 fprintf(stderr, "Version %s\n", version);
481 exit(0);
482 break;
483 default:
484 usage();
485 }
486 break;
487
488 case 'c':
489 opts->ciphername = optarg;
490 break;
491 case 'i':
492 opts->infile = optarg;
493 break;
494 case 'o':
495 opts->outfile = optarg;
496 break;
497 case 's':
498 opts->salt = optarg;
499 break;
500 case 'p':
501 opts->passwd = optarg;
502 break;
503
504
505 case 'v':
506 verbosity++;
507 break;
508 case 'q':
509 verbosity = KCAPI_LOG_NONE;
510 break;
511 case 'h':
512 usage();
513 break;
514 default:
515 usage();
516 }
517 }
518
519 set_verbosity("kcapi-dgst", verbosity);
520
521 if (!opts->ciphername) {
522 dolog(KCAPI_LOG_ERR, "Provide cipher name");
523 usage();
524 }
525
526 if (opts->passwd || opts->password_fd != -1 || opts->key_fd != -1)
527 opts->keyed_md = true;
528
529 if (opts->passwd)
530 dolog(KCAPI_LOG_WARN,
531 "Password on command line is visible in process listing and /proc! Use --passwd_fd command line option!");
532
533 if (!opts->pbkdf_hash)
534 opts->pbkdf_hash = "hmac(sha256)";
535 dolog(KCAPI_LOG_DEBUG, "Using PBKDF2 mac of %s", opts->pbkdf_hash);
536 }
537
538 int main(int argc, char *argv[])
539 {
540 struct kcapi_handle *handle = NULL;
541 struct opt_data opts;
542 int ret;
543
544 parse_opts(argc, argv, &opts);
545
546 ret = kcapi_md_init(&handle, opts.ciphername, 0);
547 if (ret)
548 return ret;
549
550 ret = set_key(handle, &opts);
551 if (ret)
552 goto out;
553
554 ret = cipher_op(handle, &opts);
555
556 if (ret > 0) {
557 dolog(KCAPI_LOG_VERBOSE, "%d bytes of %smessage digest created",
558 ret, opts.keyed_md ? "keyed " : "");
559 ret = 0;
560 } else {
561 dolog(KCAPI_LOG_ERR,
562 "message digest creation failed with error %d",
563 ret);
564 }
565
566 out:
567 kcapi_md_destroy(handle);
568
569 return ret;
570 }