Convert the capability device to use SHA1
[akaros.git] / kern / lib / crypto / 2sha_utility.c
1 /* 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 <crypto/2sysincludes.h>
9 #include <crypto/2common.h>
10 #include <crypto/2sha.h>
11
12 #if VB2_SUPPORT_SHA1
13 #define CTH_SHA1 VB2_HASH_SHA1
14 #else
15 #define CTH_SHA1 VB2_HASH_INVALID
16 #endif
17
18 #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
30 static const uint8_t crypto_to_hash[] = {
31         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,
43 };
44
45 enum vb2_hash_algorithm vb2_crypto_to_hash(uint32_t algorithm)
46 {
47         if (algorithm < ARRAY_SIZE(crypto_to_hash))
48                 return crypto_to_hash[algorithm];
49         else
50                 return VB2_HASH_INVALID;
51 }
52
53 int vb2_digest_size(enum vb2_hash_algorithm hash_alg)
54 {
55         switch (hash_alg) {
56 #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
73 int 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
93 const 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
113 int vb2_digest_init(struct vb2_digest_context *dc,
114                     enum vb2_hash_algorithm hash_alg)
115 {
116         dc->hash_alg = hash_alg;
117         dc->using_hwcrypto = 0;
118
119         switch (dc->hash_alg) {
120 #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:
136                 return VB2_ERROR_SHA_INIT_ALGORITHM;
137         }
138 }
139
140 int vb2_digest_extend(struct vb2_digest_context *dc,
141                       const uint8_t *buf,
142                       uint32_t size)
143 {
144         switch (dc->hash_alg) {
145 #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:
161                 return VB2_ERROR_SHA_EXTEND_ALGORITHM;
162         }
163 }
164
165 int vb2_digest_finalize(struct vb2_digest_context *dc,
166                         uint8_t *digest,
167                         uint32_t digest_size)
168 {
169         if (digest_size < vb2_digest_size(dc->hash_alg))
170                 return VB2_ERROR_SHA_FINALIZE_DIGEST_SIZE;
171
172         switch (dc->hash_alg) {
173 #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:
189                 return VB2_ERROR_SHA_FINALIZE_ALGORITHM;
190         }
191 }
192
193 int 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 }