1 /* Copyright 2011 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * Utility functions for file and key handling.
6 */
7
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include "2common.h"
17 #include "2sha.h"
18 #include "2sysincludes.h"
19 #include "file_keys.h"
20 #include "host_common.h"
21 #include "signature_digest.h"
22
DigestFile(char * input_file,enum vb2_hash_algorithm alg,uint8_t * digest,uint32_t digest_size)23 vb2_error_t DigestFile(char *input_file, enum vb2_hash_algorithm alg,
24 uint8_t *digest, uint32_t digest_size)
25 {
26 int input_fd, len;
27 uint8_t data[VB2_SHA1_BLOCK_SIZE];
28 struct vb2_digest_context ctx;
29
30 if ((input_fd = open(input_file, O_RDONLY)) == -1) {
31 fprintf(stderr, "Couldn't open %s\n", input_file);
32 return VB2_ERROR_UNKNOWN;
33 }
34 vb2_digest_init(&ctx, false, alg, 0);
35 while ((len = read(input_fd, data, sizeof(data))) == sizeof(data))
36 vb2_digest_extend(&ctx, data, len);
37 if (len != -1)
38 vb2_digest_extend(&ctx, data, len);
39 close(input_fd);
40
41 return vb2_digest_finalize(&ctx, digest, digest_size);
42 }
43