Line data Source code
1 : /**
2 : * Copyright Notice:
3 : * Copyright 2025 DMTF. All rights reserved.
4 : * License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5 : **/
6 :
7 : #include "internal/libspdm_lib_config.h"
8 : #include "spdm_crypt_ext_lib/spdm_crypt_ext_lib.h"
9 : #include "hal/library/cryptlib.h"
10 : #include "spdm_crypt_ext_lib/cryptlib_ext.h"
11 : #include "industry_standard/spdm.h"
12 : #include "hal/library/debuglib.h"
13 :
14 : /**
15 : * Retrieve the Private key from the password-protected PEM key data.
16 : *
17 : * @param pqc_asym_algo SPDM pqc_asym_algo
18 : * @param pem_data Pointer to the PEM-encoded key data to be retrieved.
19 : * @param pem_size Size of the PEM key data in bytes.
20 : * @param password NULL-terminated passphrase used for encrypted PEM key data.
21 : * @param context Pointer to newly generated asymmetric context which contain the retrieved
22 : * private key component.
23 : * Use libspdm_asym_free() function to free the resource.
24 : *
25 : * @retval true Private key was retrieved successfully.
26 : * @retval false Invalid PEM key data or incorrect password.
27 : **/
28 0 : bool libspdm_pqc_asym_get_private_key_from_pem(uint32_t pqc_asym_algo,
29 : const uint8_t *pem_data,
30 : size_t pem_size,
31 : const char *password,
32 : void **context)
33 : {
34 0 : switch (pqc_asym_algo) {
35 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_44:
36 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_65:
37 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_87:
38 : #if LIBSPDM_ML_DSA_SUPPORT
39 : return libspdm_mldsa_get_private_key_from_pem(pem_data, pem_size, password, context);
40 : #else
41 0 : LIBSPDM_ASSERT(false);
42 0 : return false;
43 : #endif
44 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128S:
45 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128S:
46 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128F:
47 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128F:
48 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192S:
49 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192S:
50 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192F:
51 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192F:
52 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256S:
53 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256S:
54 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256F:
55 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256F:
56 : #if LIBSPDM_SLH_DSA_SUPPORT
57 : return libspdm_slhdsa_get_private_key_from_pem(pem_data, pem_size, password, context);
58 : #else
59 0 : LIBSPDM_ASSERT(false);
60 0 : return false;
61 : #endif
62 0 : default:
63 0 : LIBSPDM_ASSERT(false);
64 0 : return false;
65 : }
66 : }
67 : /**
68 : * Retrieve the Private key from the password-protected PEM key data.
69 : *
70 : * @param req_pqc_asym_alg SPDM req_pqc_asym_alg
71 : * @param pem_data Pointer to the PEM-encoded key data to be retrieved.
72 : * @param pem_size Size of the PEM key data in bytes.
73 : * @param password NULL-terminated passphrase used for encrypted PEM key data.
74 : * @param context Pointer to newly generated asymmetric context which contain the
75 : * retrieved private key component. Use libspdm_asym_free() function to
76 : * free the resource.
77 : *
78 : * @retval true Private key was retrieved successfully.
79 : * @retval false Invalid PEM key data or incorrect password.
80 : **/
81 0 : bool libspdm_req_pqc_asym_get_private_key_from_pem(uint32_t req_pqc_asym_alg,
82 : const uint8_t *pem_data,
83 : size_t pem_size,
84 : const char *password,
85 : void **context)
86 : {
87 0 : return libspdm_pqc_asym_get_private_key_from_pem (
88 : req_pqc_asym_alg,
89 : pem_data, pem_size,
90 : password, context);
91 : }
92 :
93 6 : size_t libspdm_get_pqc_aysm_nid(uint32_t pqc_asym_algo)
94 : {
95 6 : switch (pqc_asym_algo)
96 : {
97 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_44:
98 0 : return LIBSPDM_CRYPTO_NID_ML_DSA_44;
99 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_65:
100 0 : return LIBSPDM_CRYPTO_NID_ML_DSA_65;
101 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_ML_DSA_87:
102 0 : return LIBSPDM_CRYPTO_NID_ML_DSA_87;
103 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128S:
104 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHA2_128S;
105 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128S:
106 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHAKE_128S;
107 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_128F:
108 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHA2_128F;
109 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_128F:
110 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHAKE_128F;
111 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192S:
112 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHA2_192S;
113 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192S:
114 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHAKE_192S;
115 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_192F:
116 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHA2_192F;
117 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_192F:
118 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHAKE_192F;
119 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256S:
120 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHA2_256S;
121 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256S:
122 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHAKE_256S;
123 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHA2_256F:
124 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHA2_256F;
125 0 : case SPDM_ALGORITHMS_PQC_ASYM_ALGO_SLH_DSA_SHAKE_256F:
126 0 : return LIBSPDM_CRYPTO_NID_SLH_DSA_SHAKE_256F;
127 6 : default:
128 6 : return LIBSPDM_CRYPTO_NID_NULL;
129 : }
130 : }
|