blob: 0aba6b868404cd51c78d1ec2c62b051dfe7254a7 [file] [log] [blame]
Randall Spangler32a65262011-06-27 10:49:11 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shah431b9882010-02-12 15:54:37 -08002 * 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
Gaurav Shah431b9882010-02-12 15:54:37 -08008#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
Randall Spangler7c3ae422016-05-11 13:50:18 -070016#include "2sysincludes.h"
17
18#include "2common.h"
19#include "2sha.h"
Randall Spangler32a65262011-06-27 10:49:11 -070020#include "file_keys.h"
21#include "host_common.h"
Gaurav Shahf5564fa2010-03-02 15:40:01 -080022#include "signature_digest.h"
Gaurav Shah431b9882010-02-12 15:54:37 -080023
Randall Spangler7c3ae422016-05-11 13:50:18 -070024int DigestFile(char *input_file, enum vb2_hash_algorithm alg,
Randall Spanglerfb267152016-10-11 15:28:16 -070025 uint8_t *digest, uint32_t digest_size)
26{
Randall Spangler7c3ae422016-05-11 13:50:18 -070027 int input_fd, len;
28 uint8_t data[VB2_SHA1_BLOCK_SIZE];
29 struct vb2_digest_context ctx;
Gaurav Shah5411c7a2010-03-31 10:56:49 -070030
Randall Spangler7c3ae422016-05-11 13:50:18 -070031 if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
32 VBDEBUG(("Couldn't open %s\n", input_file));
33 return VB2_ERROR_UNKNOWN;
34 }
35 vb2_digest_init(&ctx, alg);
36 while ((len = read(input_fd, data, sizeof(data))) == sizeof(data))
37 vb2_digest_extend(&ctx, data, len);
38 if (len != -1)
39 vb2_digest_extend(&ctx, data, len);
40 close(input_fd);
41
42 return vb2_digest_finalize(&ctx, digest, digest_size);
Gaurav Shah5411c7a2010-03-31 10:56:49 -070043}