summaryrefslogtreecommitdiff
path: root/firmware/lib/cryptolib/sha_utility.c
blob: c676040bbbe78ff6765df469451829c07983a356 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* 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.
 *
 * Utility functions for message digest functions.
 */

#include "cryptolib.h"
#include "utility.h"
#include "vboot_api.h"

void DigestInit(DigestContext* ctx, int sig_algorithm) {
  ctx->algorithm = hash_type_map[sig_algorithm];
  switch(ctx->algorithm) {
#ifndef CHROMEOS_EC
    case SHA1_DIGEST_ALGORITHM:
      ctx->sha1_ctx = (SHA1_CTX*) VbExMalloc(sizeof(SHA1_CTX));
      SHA1_init(ctx->sha1_ctx);
      break;
#endif
    case SHA256_DIGEST_ALGORITHM:
      ctx->sha256_ctx = (SHA256_CTX*) VbExMalloc(sizeof(SHA256_CTX));
      SHA256_init(ctx->sha256_ctx);
      break;
#ifndef CHROMEOS_EC
    case SHA512_DIGEST_ALGORITHM:
      ctx->sha512_ctx = (SHA512_CTX*) VbExMalloc(sizeof(SHA512_CTX));
      SHA512_init(ctx->sha512_ctx);
      break;
#endif
  };
}

void DigestUpdate(DigestContext* ctx, const uint8_t* data, uint32_t len) {
  switch(ctx->algorithm) {
#ifndef CHROMEOS_EC
    case SHA1_DIGEST_ALGORITHM:
      SHA1_update(ctx->sha1_ctx, data, len);
      break;
#endif
    case SHA256_DIGEST_ALGORITHM:
      SHA256_update(ctx->sha256_ctx, data, len);
      break;
#ifndef CHROMEOS_EC
    case SHA512_DIGEST_ALGORITHM:
      SHA512_update(ctx->sha512_ctx, data, len);
      break;
#endif
  };
}

uint8_t* DigestFinal(DigestContext* ctx) {
  uint8_t* digest = NULL;
  switch(ctx->algorithm) {
#ifndef CHROMEOS_EC
    case SHA1_DIGEST_ALGORITHM:
      digest = (uint8_t*) VbExMalloc(SHA1_DIGEST_SIZE);
      Memcpy(digest, SHA1_final(ctx->sha1_ctx), SHA1_DIGEST_SIZE);
      VbExFree(ctx->sha1_ctx);
      break;
#endif
    case SHA256_DIGEST_ALGORITHM:
      digest = (uint8_t*) VbExMalloc(SHA256_DIGEST_SIZE);
      Memcpy(digest, SHA256_final(ctx->sha256_ctx), SHA256_DIGEST_SIZE);
      VbExFree(ctx->sha256_ctx);
      break;
#ifndef CHROMEOS_EC
    case SHA512_DIGEST_ALGORITHM:
      digest = (uint8_t*) VbExMalloc(SHA512_DIGEST_SIZE);
      Memcpy(digest, SHA512_final(ctx->sha512_ctx), SHA512_DIGEST_SIZE);
      VbExFree(ctx->sha512_ctx);
      break;
#endif
  };
  return digest;
}

uint8_t* DigestBuf(const uint8_t* buf, uint64_t len, int sig_algorithm) {
  /* Allocate enough space for the largest digest */
  uint8_t* digest = (uint8_t*) VbExMalloc(SHA512_DIGEST_SIZE);
  /* Define an array mapping [sig_algorithm] to function pointers to the
   * SHA{1|256|512} functions.
   */
  typedef uint8_t* (*Hash_ptr) (const uint8_t*, uint64_t, uint8_t*);
  Hash_ptr hash[] = {
#ifdef CHROMEOS_EC
    0,  /* RSA 1024 */
    0,
    0,
    0,  /* RSA 2048 */
    0,
    0,
    0,  /* RSA 4096 */
    SHA256,
    0,
    0,  /* RSA 8192 */
    0,
    0,
#else
    SHA1,  /* RSA 1024 */
    SHA256,
    SHA512,
    SHA1,  /* RSA 2048 */
    SHA256,
    SHA512,
    SHA1,  /* RSA 4096 */
    SHA256,
    SHA512,
    SHA1,  /* RSA 8192 */
    SHA256,
    SHA512,
#endif
  };
  /* Call the appropriate hash function. */
  return hash[sig_algorithm](buf, len, digest);
}