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_hkdf_sha256_extract_and_expand(const uint8_t * key,size_t key_size,const uint8_t * salt,size_t salt_size,const uint8_t * info,size_t info_size,uint8_t * out,size_t out_size)29 bool libspdm_hkdf_sha256_extract_and_expand(const uint8_t *key, size_t key_size,
30                                             const uint8_t *salt, size_t salt_size,
31                                             const uint8_t *info, size_t info_size,
32                                             uint8_t *out, size_t out_size)
33 {
34     return lkca_hkdf_extract_and_expand("hmac(sha256)", key, key_size,
35                                            salt, salt_size, info, info_size,
36                                            out, out_size);
37 }
38 
libspdm_hkdf_sha256_extract(const uint8_t * key,size_t key_size,const uint8_t * salt,size_t salt_size,uint8_t * prk_out,size_t prk_out_size)39 bool libspdm_hkdf_sha256_extract(const uint8_t *key, size_t key_size,
40                                  const uint8_t *salt, size_t salt_size,
41                                  uint8_t *prk_out, size_t prk_out_size)
42 {
43     if (prk_out_size != (256 / 8))
44         return false;
45 
46     return libspdm_hmac_sha256_all(key, key_size, salt, salt_size, prk_out);
47 }
48 
libspdm_hkdf_sha256_expand(const uint8_t * prk,size_t prk_size,const uint8_t * info,size_t info_size,uint8_t * out,size_t out_size)49 bool libspdm_hkdf_sha256_expand(const uint8_t *prk, size_t prk_size,
50                                 const uint8_t *info, size_t info_size,
51                                 uint8_t *out, size_t out_size)
52 {
53     return lkca_hkdf_expand("hmac(sha256)", prk, prk_size, info, info_size,
54                           out, out_size);
55 }
56 
libspdm_hkdf_sha384_extract_and_expand(const uint8_t * key,size_t key_size,const uint8_t * salt,size_t salt_size,const uint8_t * info,size_t info_size,uint8_t * out,size_t out_size)57 bool libspdm_hkdf_sha384_extract_and_expand(const uint8_t *key, size_t key_size,
58                                             const uint8_t *salt, size_t salt_size,
59                                             const uint8_t *info, size_t info_size,
60                                             uint8_t *out, size_t out_size)
61 {
62     return lkca_hkdf_extract_and_expand("hmac(sha384)", key, key_size,
63                                         salt, salt_size, info, info_size,
64                                         out, out_size);
65 }
66 
libspdm_hkdf_sha384_extract(const uint8_t * key,size_t key_size,const uint8_t * salt,size_t salt_size,uint8_t * prk_out,size_t prk_out_size)67 bool libspdm_hkdf_sha384_extract(const uint8_t *key, size_t key_size,
68                                  const uint8_t *salt, size_t salt_size,
69                                  uint8_t *prk_out, size_t prk_out_size)
70 {
71         if (prk_out_size != (384 / 8))
72             return false;
73 
74         return libspdm_hmac_sha384_all(key, key_size, salt, salt_size, prk_out);
75 }
76 
libspdm_hkdf_sha384_expand(const uint8_t * prk,size_t prk_size,const uint8_t * info,size_t info_size,uint8_t * out,size_t out_size)77 bool libspdm_hkdf_sha384_expand(const uint8_t *prk, size_t prk_size,
78                                 const uint8_t *info, size_t info_size,
79                                 uint8_t *out, size_t out_size)
80 {
81     return lkca_hkdf_expand("hmac(sha384)", prk, prk_size, info, info_size,
82                           out, out_size);
83 }
84 
libspdm_hkdf_sha512_extract_and_expand(const uint8_t * key,size_t key_size,const uint8_t * salt,size_t salt_size,const uint8_t * info,size_t info_size,uint8_t * out,size_t out_size)85 bool libspdm_hkdf_sha512_extract_and_expand(const uint8_t *key, size_t key_size,
86                                             const uint8_t *salt, size_t salt_size,
87                                             const uint8_t *info, size_t info_size,
88                                             uint8_t *out, size_t out_size)
89 {
90     return lkca_hkdf_extract_and_expand("hmac(sha512)", key, key_size,
91                                         salt, salt_size, info, info_size, out,
92                                         out_size);
93 }
94 
libspdm_hkdf_sha512_extract(const uint8_t * key,size_t key_size,const uint8_t * salt,size_t salt_size,uint8_t * prk_out,size_t prk_out_size)95 bool libspdm_hkdf_sha512_extract(const uint8_t *key, size_t key_size,
96                                  const uint8_t *salt, size_t salt_size,
97                                  uint8_t *prk_out, size_t prk_out_size)
98 {
99     if (prk_out_size != (512 / 8))
100         return false;
101 
102     return libspdm_hmac_sha512_all(key, key_size, salt, salt_size, prk_out);
103 }
104 
libspdm_hkdf_sha512_expand(const uint8_t * prk,size_t prk_size,const uint8_t * info,size_t info_size,uint8_t * out,size_t out_size)105 bool libspdm_hkdf_sha512_expand(const uint8_t *prk, size_t prk_size,
106                                 const uint8_t *info, size_t info_size,
107                                 uint8_t *out, size_t out_size)
108 {
109     return lkca_hkdf_expand("hmac(sha512)", prk, prk_size, info, info_size,
110                             out, out_size);
111 }
112