summaryrefslogtreecommitdiff
path: root/utility/pad_digest_utility.c
blob: 440cca3ab526c55199f0e788feb964591ec4c2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "2sysincludes.h"

#include "2common.h"
#include "2sha.h"
#include "file_keys.h"
#include "host_common.h"
#include "padding.h"
#include "signature_digest.h"

static void usage(char* argv[]) {
  fprintf(stderr,
          "Usage: %s <alg_id> <digest_file>\n"
          "\n"
          "Generate a padded hash suitable for generating PKCS#1.5 "
          "signatures.\n",
          basename(argv[0]));
}

int main(int argc, char* argv[]) {
  int algorithm = -1;
  int error_code = 0;
  uint8_t* digest = NULL;
  uint8_t* padded_digest = NULL;
  uint32_t len;
  uint32_t padded_digest_len;

  if (argc != 3) {
    usage(argv);
    return -1;
  }
  algorithm = atoi(argv[1]);
  if (algorithm < 0 || algorithm >= kNumAlgorithms) {
    fprintf(stderr, "Invalid Algorithm!\n");
    return -1;
  }

  if (VB2_SUCCESS != vb2_read_file(argv[2], &digest, &len)) {
    fprintf(stderr, "Could not read file: %s\n", argv[2]);
    return -1;
  }

  padded_digest = PrependDigestInfo(algorithm, digest);
  const int digest_size = vb2_digest_size(vb2_crypto_to_hash(algorithm));
  padded_digest_len = (digest_size + digestinfo_size_map[algorithm]);

  if (!padded_digest)
    error_code = -1;
  if(padded_digest &&
     1 != fwrite(padded_digest, padded_digest_len, 1, stdout))
    error_code = -1;
  free(padded_digest);
  free(digest);
  return error_code;
}