file_keys.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  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. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include "2sysincludes.h"
  15. #include "2common.h"
  16. #include "2sha.h"
  17. #include "file_keys.h"
  18. #include "host_common.h"
  19. #include "signature_digest.h"
  20. int DigestFile(char *input_file, enum vb2_hash_algorithm alg,
  21. uint8_t *digest, uint32_t digest_size)
  22. {
  23. int input_fd, len;
  24. uint8_t data[VB2_SHA1_BLOCK_SIZE];
  25. struct vb2_digest_context ctx;
  26. if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
  27. fprintf(stderr, "Couldn't open %s\n", input_file);
  28. return VB2_ERROR_UNKNOWN;
  29. }
  30. vb2_digest_init(&ctx, alg);
  31. while ((len = read(input_fd, data, sizeof(data))) == sizeof(data))
  32. vb2_digest_extend(&ctx, data, len);
  33. if (len != -1)
  34. vb2_digest_extend(&ctx, data, len);
  35. close(input_fd);
  36. return vb2_digest_finalize(&ctx, digest, digest_size);
  37. }