1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Prototypes and checks taken from DMTF: Copyright 2021-2022 DMTF. All rights reserved.
24 * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
25 */
26 
27 #include "internal_crypt_lib.h"
28 
libspdm_hmac_sha256_new(void)29 void *libspdm_hmac_sha256_new(void)
30 {
31     return lkca_hash_new("hmac(sha256)");
32 }
33 
libspdm_hmac_sha256_free(void * hmac_sha256_ctx)34 void libspdm_hmac_sha256_free(void *hmac_sha256_ctx)
35 {
36     lkca_hash_free(hmac_sha256_ctx);
37 }
38 
libspdm_hmac_sha256_set_key(void * hmac_sha256_ctx,const uint8_t * key,size_t key_size)39 bool libspdm_hmac_sha256_set_key(void *hmac_sha256_ctx, const uint8_t *key,
40                                  size_t key_size)
41 {
42     if (hmac_sha256_ctx == NULL)
43         return false;
44 
45     return lkca_hmac_set_key(hmac_sha256_ctx, key, key_size);
46 }
47 
libspdm_hmac_sha256_duplicate(const void * hmac_sha256_ctx,void * new_hmac_sha256_ctx)48 bool libspdm_hmac_sha256_duplicate(const void *hmac_sha256_ctx,
49                                    void *new_hmac_sha256_ctx)
50 {
51     if (hmac_sha256_ctx == NULL || new_hmac_sha256_ctx == NULL) {
52         return false;
53     }
54 
55     return lkca_hmac_duplicate(new_hmac_sha256_ctx, hmac_sha256_ctx);
56 }
57 
libspdm_hmac_sha256_update(void * hmac_sha256_ctx,const void * data,size_t data_size)58 bool libspdm_hmac_sha256_update(void *hmac_sha256_ctx, const void *data,
59                                 size_t data_size)
60 {
61     int32_t ret;
62 
63     if (hmac_sha256_ctx == NULL) {
64         return false;
65     }
66 
67     if (data == NULL && data_size != 0) {
68         return false;
69     }
70     if (data_size > INT_MAX) {
71         return false;
72     }
73 
74     ret = crypto_shash_update(hmac_sha256_ctx, data, data_size);
75     if (ret != 0) {
76         return false;
77     }
78     return true;
79 }
80 
libspdm_hmac_sha256_final(void * hmac_sha256_ctx,uint8_t * hmac_value)81 bool libspdm_hmac_sha256_final(void *hmac_sha256_ctx, uint8_t *hmac_value)
82 {
83     int32_t ret;
84 
85     if (hmac_sha256_ctx == NULL || hmac_value == NULL) {
86         return false;
87     }
88 
89     ret = crypto_shash_final(hmac_sha256_ctx, hmac_value);
90 
91     if (ret != 0) {
92         return false;
93     }
94     return true;
95 }
96 
libspdm_hmac_sha256_all(const void * data,size_t data_size,const uint8_t * key,size_t key_size,uint8_t * hmac_value)97 bool libspdm_hmac_sha256_all(const void *data, size_t data_size,
98                              const uint8_t *key, size_t key_size,
99                              uint8_t *hmac_value)
100 {
101     if (hmac_value == NULL) {
102         return false;
103     }
104     if (data == NULL && data_size != 0) {
105         return false;
106     }
107     if (data_size > INT_MAX) {
108         return false;
109     }
110 
111     return lkca_hmac_all("hmac(sha256)", key, key_size, data, data_size, hmac_value);
112 }
113 
libspdm_hmac_sha384_new(void)114 void *libspdm_hmac_sha384_new(void)
115 {
116     return lkca_hash_new("hmac(sha384)");
117 }
118 
libspdm_hmac_sha384_free(void * hmac_sha384_ctx)119 void libspdm_hmac_sha384_free(void *hmac_sha384_ctx)
120 {
121     lkca_hash_free(hmac_sha384_ctx);
122 }
123 
libspdm_hmac_sha384_set_key(void * hmac_sha384_ctx,const uint8_t * key,size_t key_size)124 bool libspdm_hmac_sha384_set_key(void *hmac_sha384_ctx, const uint8_t *key,
125                                  size_t key_size)
126 {
127     if (hmac_sha384_ctx == NULL)
128         return false;
129 
130     return lkca_hmac_set_key(hmac_sha384_ctx, key, key_size);
131 }
132 
libspdm_hmac_sha384_duplicate(const void * hmac_sha384_ctx,void * new_hmac_sha384_ctx)133 bool libspdm_hmac_sha384_duplicate(const void *hmac_sha384_ctx,
134                                    void *new_hmac_sha384_ctx)
135 {
136     if (hmac_sha384_ctx == NULL || new_hmac_sha384_ctx == NULL) {
137         return false;
138     }
139 
140     return lkca_hmac_duplicate(new_hmac_sha384_ctx, hmac_sha384_ctx);
141 }
142 
libspdm_hmac_sha384_update(void * hmac_sha384_ctx,const void * data,size_t data_size)143 bool libspdm_hmac_sha384_update(void *hmac_sha384_ctx, const void *data,
144                                 size_t data_size)
145 {
146     int32_t ret;
147 
148     if (hmac_sha384_ctx == NULL) {
149         return false;
150     }
151 
152     if (data == NULL && data_size != 0) {
153         return false;
154     }
155     if (data_size > INT_MAX) {
156         return false;
157     }
158 
159     ret = crypto_shash_update(hmac_sha384_ctx, data, data_size);
160     if (ret != 0) {
161         return false;
162     }
163     return true;
164 }
165 
libspdm_hmac_sha384_final(void * hmac_sha384_ctx,uint8_t * hmac_value)166 bool libspdm_hmac_sha384_final(void *hmac_sha384_ctx, uint8_t *hmac_value)
167 {
168     int32_t ret;
169 
170     if (hmac_sha384_ctx == NULL || hmac_value == NULL) {
171         return false;
172     }
173 
174     ret = crypto_shash_final(hmac_sha384_ctx, hmac_value);
175 
176     if (ret != 0) {
177         return false;
178     }
179     return true;
180 }
181 
libspdm_hmac_sha384_all(const void * data,size_t data_size,const uint8_t * key,size_t key_size,uint8_t * hmac_value)182 bool libspdm_hmac_sha384_all(const void *data, size_t data_size,
183                              const uint8_t *key, size_t key_size,
184                              uint8_t *hmac_value)
185 {
186     if (hmac_value == NULL) {
187         return false;
188     }
189     if (data == NULL && data_size != 0) {
190         return false;
191     }
192     if (data_size > INT_MAX) {
193         return false;
194     }
195 
196     return lkca_hmac_all("hmac(sha384)", key, key_size, data, data_size, hmac_value);
197 }
198 
libspdm_hmac_sha512_new(void)199 void *libspdm_hmac_sha512_new(void)
200 {
201     return lkca_hash_new("hmac(sha512)");
202 }
203 
libspdm_hmac_sha512_free(void * hmac_sha512_ctx)204 void libspdm_hmac_sha512_free(void *hmac_sha512_ctx)
205 {
206     lkca_hash_free(hmac_sha512_ctx);
207 }
208 
libspdm_hmac_sha512_set_key(void * hmac_sha512_ctx,const uint8_t * key,size_t key_size)209 bool libspdm_hmac_sha512_set_key(void *hmac_sha512_ctx, const uint8_t *key,
210                                  size_t key_size)
211 {
212     if (hmac_sha512_ctx == NULL)
213         return false;
214 
215     return lkca_hmac_set_key(hmac_sha512_ctx, key, key_size);
216 }
217 
libspdm_hmac_sha512_duplicate(const void * hmac_sha512_ctx,void * new_hmac_sha512_ctx)218 bool libspdm_hmac_sha512_duplicate(const void *hmac_sha512_ctx,
219                                    void *new_hmac_sha512_ctx)
220 {
221     if (new_hmac_sha512_ctx == NULL || new_hmac_sha512_ctx == NULL) {
222         return false;
223     }
224 
225     return lkca_hmac_duplicate(new_hmac_sha512_ctx, hmac_sha512_ctx);
226 }
227 
libspdm_hmac_sha512_update(void * hmac_sha512_ctx,const void * data,size_t data_size)228 bool libspdm_hmac_sha512_update(void *hmac_sha512_ctx, const void *data,
229                                 size_t data_size)
230 {
231     int32_t ret;
232 
233     if (hmac_sha512_ctx == NULL) {
234         return false;
235     }
236 
237     if (data == NULL && data_size != 0) {
238         return false;
239     }
240     if (data_size > INT_MAX) {
241         return false;
242     }
243 
244     ret = crypto_shash_update(hmac_sha512_ctx, data, data_size);
245     if (ret != 0) {
246         return false;
247     }
248     return true;
249 }
250 
libspdm_hmac_sha512_final(void * hmac_sha512_ctx,uint8_t * hmac_value)251 bool libspdm_hmac_sha512_final(void *hmac_sha512_ctx, uint8_t *hmac_value)
252 {
253     int32_t ret;
254 
255     if (hmac_sha512_ctx == NULL || hmac_value == NULL) {
256         return false;
257     }
258 
259     ret = crypto_shash_final(hmac_sha512_ctx, hmac_value);
260 
261     if (ret != 0) {
262         return false;
263     }
264     return true;
265 }
266 
libspdm_hmac_sha512_all(const void * data,size_t data_size,const uint8_t * key,size_t key_size,uint8_t * hmac_value)267 bool libspdm_hmac_sha512_all(const void *data, size_t data_size,
268                              const uint8_t *key, size_t key_size,
269                              uint8_t *hmac_value)
270 {
271     if (hmac_value == NULL) {
272         return false;
273     }
274     if (data == NULL && data_size != 0) {
275         return false;
276     }
277     if (data_size > INT_MAX) {
278         return false;
279     }
280 
281     return lkca_hmac_all("hmac(sha512)", key, key_size, data, data_size, hmac_value);
282 }
283