blob: dd74f290ea0bc43eb88612546b22486ddc8b0ac5 [file] [log] [blame]
Randall Spanglere166d042014-05-13 09:24:52 -07001/* Copyright (c) 2014 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 message digest functions.
6 */
7
8#include "2sysincludes.h"
9#include "2common.h"
Randall Spanglere166d042014-05-13 09:24:52 -070010#include "2sha.h"
11
Randall Spangler21f100c2014-10-16 10:32:43 -070012#if VB2_SUPPORT_SHA1
13#define CTH_SHA1 VB2_HASH_SHA1
14#else
15#define CTH_SHA1 VB2_HASH_INVALID
16#endif
Randall Spanglere166d042014-05-13 09:24:52 -070017
Randall Spangler21f100c2014-10-16 10:32:43 -070018#if VB2_SUPPORT_SHA256
19#define CTH_SHA256 VB2_HASH_SHA256
20#else
21#define CTH_SHA256 VB2_HASH_INVALID
22#endif
23
24#if VB2_SUPPORT_SHA512
25#define CTH_SHA512 VB2_HASH_SHA512
26#else
27#define CTH_SHA512 VB2_HASH_INVALID
28#endif
29
Randall Spangler4eef8122014-10-23 10:07:54 -070030static const uint8_t crypto_to_hash[] = {
Randall Spangler21f100c2014-10-16 10:32:43 -070031 CTH_SHA1,
32 CTH_SHA256,
33 CTH_SHA512,
34 CTH_SHA1,
35 CTH_SHA256,
36 CTH_SHA512,
37 CTH_SHA1,
38 CTH_SHA256,
39 CTH_SHA512,
40 CTH_SHA1,
41 CTH_SHA256,
42 CTH_SHA512,
Randall Spanglere166d042014-05-13 09:24:52 -070043};
44
Randall Spangler4eef8122014-10-23 10:07:54 -070045enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm)
Randall Spanglere166d042014-05-13 09:24:52 -070046{
Randall Spangler4eef8122014-10-23 10:07:54 -070047 if (algorithm < ARRAY_SIZE(crypto_to_hash))
Randall Spangler21f100c2014-10-16 10:32:43 -070048 return crypto_to_hash[algorithm];
Randall Spanglere166d042014-05-13 09:24:52 -070049 else
Randall Spangler21f100c2014-10-16 10:32:43 -070050 return VB2_HASH_INVALID;
Randall Spanglere166d042014-05-13 09:24:52 -070051}
52
Randall Spangler4eef8122014-10-23 10:07:54 -070053int vb2_digest_size(enum vb2_hash_algorithm hash_alg)
Randall Spanglere166d042014-05-13 09:24:52 -070054{
Randall Spangler4eef8122014-10-23 10:07:54 -070055 switch (hash_alg) {
Randall Spanglere166d042014-05-13 09:24:52 -070056#if VB2_SUPPORT_SHA1
57 case VB2_HASH_SHA1:
58 return VB2_SHA1_DIGEST_SIZE;
59#endif
60#if VB2_SUPPORT_SHA256
61 case VB2_HASH_SHA256:
62 return VB2_SHA256_DIGEST_SIZE;
63#endif
64#if VB2_SUPPORT_SHA512
65 case VB2_HASH_SHA512:
66 return VB2_SHA512_DIGEST_SIZE;
67#endif
68 default:
69 return 0;
70 }
71}
72
Daisuke Nojirif3f9e002016-05-05 17:21:29 -070073int vb2_hash_block_size(enum vb2_hash_algorithm alg)
74{
75 switch (alg) {
76#if VB2_SUPPORT_SHA1
77 case VB2_HASH_SHA1:
78 return VB2_SHA1_BLOCK_SIZE;
79#endif
80#if VB2_SUPPORT_SHA256
81 case VB2_HASH_SHA256:
82 return VB2_SHA256_BLOCK_SIZE;
83#endif
84#if VB2_SUPPORT_SHA512
85 case VB2_HASH_SHA512:
86 return VB2_SHA512_BLOCK_SIZE;
87#endif
88 default:
89 return 0;
90 }
91}
92
93const char *vb2_get_hash_algorithm_name(enum vb2_hash_algorithm alg)
94{
95 switch (alg) {
96#if VB2_SUPPORT_SHA1
97 case VB2_HASH_SHA1:
98 return VB2_SHA1_ALG_NAME;
99#endif
100#if VB2_SUPPORT_SHA256
101 case VB2_HASH_SHA256:
102 return VB2_SHA256_ALG_NAME;
103#endif
104#if VB2_SUPPORT_SHA512
105 case VB2_HASH_SHA512:
106 return VB2_SHA512_ALG_NAME;
107#endif
108 default:
109 return VB2_INVALID_ALG_NAME;
110 }
111}
112
Randall Spangler4eef8122014-10-23 10:07:54 -0700113int vb2_digest_init(struct vb2_digest_context *dc,
114 enum vb2_hash_algorithm hash_alg)
Randall Spanglere166d042014-05-13 09:24:52 -0700115{
Randall Spangler4eef8122014-10-23 10:07:54 -0700116 dc->hash_alg = hash_alg;
Julius Wernerf10e9092014-12-16 19:24:54 -0800117 dc->using_hwcrypto = 0;
Randall Spanglere166d042014-05-13 09:24:52 -0700118
Randall Spangler4eef8122014-10-23 10:07:54 -0700119 switch (dc->hash_alg) {
Randall Spanglere166d042014-05-13 09:24:52 -0700120#if VB2_SUPPORT_SHA1
121 case VB2_HASH_SHA1:
122 vb2_sha1_init(&dc->sha1);
123 return VB2_SUCCESS;
124#endif
125#if VB2_SUPPORT_SHA256
126 case VB2_HASH_SHA256:
127 vb2_sha256_init(&dc->sha256);
128 return VB2_SUCCESS;
129#endif
130#if VB2_SUPPORT_SHA512
131 case VB2_HASH_SHA512:
132 vb2_sha512_init(&dc->sha512);
133 return VB2_SUCCESS;
134#endif
135 default:
Randall Spanglerb9be5362014-06-05 13:32:11 -0700136 return VB2_ERROR_SHA_INIT_ALGORITHM;
Randall Spanglere166d042014-05-13 09:24:52 -0700137 }
138}
139
140int vb2_digest_extend(struct vb2_digest_context *dc,
141 const uint8_t *buf,
142 uint32_t size)
143{
Randall Spangler4eef8122014-10-23 10:07:54 -0700144 switch (dc->hash_alg) {
Randall Spanglere166d042014-05-13 09:24:52 -0700145#if VB2_SUPPORT_SHA1
146 case VB2_HASH_SHA1:
147 vb2_sha1_update(&dc->sha1, buf, size);
148 return VB2_SUCCESS;
149#endif
150#if VB2_SUPPORT_SHA256
151 case VB2_HASH_SHA256:
152 vb2_sha256_update(&dc->sha256, buf, size);
153 return VB2_SUCCESS;
154#endif
155#if VB2_SUPPORT_SHA512
156 case VB2_HASH_SHA512:
157 vb2_sha512_update(&dc->sha512, buf, size);
158 return VB2_SUCCESS;
159#endif
160 default:
Randall Spanglerb9be5362014-06-05 13:32:11 -0700161 return VB2_ERROR_SHA_EXTEND_ALGORITHM;
Randall Spanglere166d042014-05-13 09:24:52 -0700162 }
163}
164
165int vb2_digest_finalize(struct vb2_digest_context *dc,
166 uint8_t *digest,
167 uint32_t digest_size)
168{
Randall Spangler4eef8122014-10-23 10:07:54 -0700169 if (digest_size < vb2_digest_size(dc->hash_alg))
Randall Spanglerb9be5362014-06-05 13:32:11 -0700170 return VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE;
Randall Spanglere166d042014-05-13 09:24:52 -0700171
Randall Spangler4eef8122014-10-23 10:07:54 -0700172 switch (dc->hash_alg) {
Randall Spanglere166d042014-05-13 09:24:52 -0700173#if VB2_SUPPORT_SHA1
174 case VB2_HASH_SHA1:
175 vb2_sha1_finalize(&dc->sha1, digest);
176 return VB2_SUCCESS;
177#endif
178#if VB2_SUPPORT_SHA256
179 case VB2_HASH_SHA256:
180 vb2_sha256_finalize(&dc->sha256, digest);
181 return VB2_SUCCESS;
182#endif
183#if VB2_SUPPORT_SHA512
184 case VB2_HASH_SHA512:
185 vb2_sha512_finalize(&dc->sha512, digest);
186 return VB2_SUCCESS;
187#endif
188 default:
Randall Spanglerb9be5362014-06-05 13:32:11 -0700189 return VB2_ERROR_SHA_FINALIZE_ALGORITHM;
Randall Spanglere166d042014-05-13 09:24:52 -0700190 }
191}
Randall Spanglerf81fce92015-05-05 16:06:50 -0700192
193int vb2_digest_buffer(const uint8_t *buf,
194 uint32_t size,
195 enum vb2_hash_algorithm hash_alg,
196 uint8_t *digest,
197 uint32_t digest_size)
198{
199 struct vb2_digest_context dc;
200 int rv;
201
202 rv = vb2_digest_init(&dc, hash_alg);
203 if (rv)
204 return rv;
205
206 rv = vb2_digest_extend(&dc, buf, size);
207 if (rv)
208 return rv;
209
210 return vb2_digest_finalize(&dc, digest, digest_size);
211}