-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathOSSLMLDSAPrivateKey.cpp
More file actions
229 lines (199 loc) · 5.48 KB
/
Copy pathOSSLMLDSAPrivateKey.cpp
File metadata and controls
229 lines (199 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*****************************************************************************
OSSLMLDSAPrivateKey.cpp
OpenSSL ML-DSA private key class
*****************************************************************************/
#include "config.h"
#ifdef WITH_ML_DSA
#include "log.h"
#include "OSSLMLDSAPrivateKey.h"
#include "MLDSAParameters.h"
#include "OSSLUtil.h"
#include <cstring>
#include <utility>
#include <openssl/bn.h>
#include <openssl/core_names.h>
#include <openssl/x509.h>
// Constructors
OSSLMLDSAPrivateKey::OSSLMLDSAPrivateKey()
{
pkey = NULL;
}
OSSLMLDSAPrivateKey::OSSLMLDSAPrivateKey(const EVP_PKEY* inMLDSAKEY)
{
pkey = NULL;
setFromOSSL(inMLDSAKEY);
}
// Destructor
OSSLMLDSAPrivateKey::~OSSLMLDSAPrivateKey()
{
if (pkey != NULL)
{
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
OSSLMLDSAPrivateKey::OSSLMLDSAPrivateKey(OSSLMLDSAPrivateKey&& other) noexcept
: MLDSAPrivateKey(std::move(other)), pkey(other.pkey)
{
other.pkey = NULL;
}
OSSLMLDSAPrivateKey& OSSLMLDSAPrivateKey::operator=(OSSLMLDSAPrivateKey&& other) noexcept
{
if (this != &other)
{
MLDSAPrivateKey::operator=(std::move(other));
if (pkey) EVP_PKEY_free(pkey);
pkey = other.pkey;
other.pkey = NULL;
}
return *this;
}
// The type
const char* OSSLMLDSAPrivateKey::type = "OpenSSL ML-DSA Private Key";
// Set from OpenSSL representation
bool OSSLMLDSAPrivateKey::setFromOSSL(const EVP_PKEY* inMLDSAKEY)
{
ByteString localSeed;
uint8_t osslSeed[32];
size_t osslSeed_len;
int rv = EVP_PKEY_get_octet_string_param(inMLDSAKEY, OSSL_PKEY_PARAM_ML_DSA_SEED,
osslSeed, sizeof(osslSeed), &osslSeed_len);
if(rv && osslSeed_len == 32) {
localSeed = ByteString(osslSeed, osslSeed_len);
}
// let's use max priv length
uint8_t priv[MLDSAParameters::ML_DSA_87_PRIV_LENGTH];
size_t priv_len;
rv = EVP_PKEY_get_octet_string_param(inMLDSAKEY, OSSL_PKEY_PARAM_PRIV_KEY,
priv, sizeof(priv), &priv_len);
if(!rv) {
ERROR_MSG("Could not get private key, rv: %d", rv);
memset(osslSeed, 0, sizeof(osslSeed));
memset(priv, 0, sizeof(priv));
return false;
}
if (priv_len != MLDSAParameters::ML_DSA_44_PRIV_LENGTH &&
priv_len != MLDSAParameters::ML_DSA_65_PRIV_LENGTH &&
priv_len != MLDSAParameters::ML_DSA_87_PRIV_LENGTH)
{
ERROR_MSG("Unsupported ML-DSA private key length: %zu", priv_len);
memset(osslSeed, 0, sizeof(osslSeed));
memset(priv, 0, sizeof(priv));
return false;
}
// Commit state atomically after successful extraction
setSeed(localSeed);
setValue(ByteString(priv, priv_len));
memset(osslSeed, 0, sizeof(osslSeed));
memset(priv, 0, sizeof(priv));
return true;
}
// Check if the key is of the given type
bool OSSLMLDSAPrivateKey::isOfType(const char* inType)
{
return !strcmp(type, inType);
}
void OSSLMLDSAPrivateKey::setValue(const ByteString& inValue)
{
MLDSAPrivateKey::setValue(inValue);
if (pkey != NULL)
{
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
void OSSLMLDSAPrivateKey::setSeed(const ByteString& inSeed)
{
MLDSAPrivateKey::setSeed(inSeed);
if (pkey != NULL)
{
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
// Encode into PKCS#8 DER
ByteString OSSLMLDSAPrivateKey::PKCS8Encode()
{
ByteString der;
EVP_PKEY* key = getOSSLKey();
if (key == NULL) return der;
PKCS8_PRIV_KEY_INFO* p8inf = EVP_PKEY2PKCS8(key);
if (p8inf == NULL) return der;
int len = i2d_PKCS8_PRIV_KEY_INFO(p8inf, NULL);
if (len < 0)
{
PKCS8_PRIV_KEY_INFO_free(p8inf);
return der;
}
der.resize(len);
unsigned char* priv = &der[0];
int len2 = i2d_PKCS8_PRIV_KEY_INFO(p8inf, &priv);
PKCS8_PRIV_KEY_INFO_free(p8inf);
if (len2 != len) der.wipe();
return der;
}
// Decode from PKCS#8 BER
bool OSSLMLDSAPrivateKey::PKCS8Decode(const ByteString& ber)
{
int len = ber.size();
if (len <= 0) return false;
const unsigned char* priv = ber.const_byte_str();
PKCS8_PRIV_KEY_INFO* p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &priv, len);
if (p8 == NULL) return false;
EVP_PKEY* localPKey = EVP_PKCS82PKEY(p8);
PKCS8_PRIV_KEY_INFO_free(p8);
if (localPKey == NULL) return false;
const bool ok = setFromOSSL(localPKey);
EVP_PKEY_free(localPKey);
return ok;
}
// Retrieve the OpenSSL representation of the key
EVP_PKEY* OSSLMLDSAPrivateKey::getOSSLKey()
{
if (pkey == NULL) createOSSLKey();
return pkey;
}
// Create the OpenSSL representation of the key
void OSSLMLDSAPrivateKey::createOSSLKey()
{
if (pkey != NULL) return;
ByteString localValue = getValue();
const char* name = OSSL::mldsaParameterSet2Name(getParameterSet());
if (name == NULL)
{
ERROR_MSG("Unknown ML-DSA parameter set (value length: %zu)", localValue.size());
return;
}
if (localValue.size() == 0)
{
ERROR_MSG("Empty ML-DSA private key value; cannot create EVP_PKEY");
return;
}
int selection = 0;
EVP_PKEY_CTX *ctx = NULL;
OSSL_PARAM params[3], *p = params;
*p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
localValue.byte_str(), localValue.size());
selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
*p = OSSL_PARAM_construct_end();
// Use the default provider for internal ML-DSA key reconstruction.
ctx = EVP_PKEY_CTX_new_from_name(NULL, name, "provider=default");
if (ctx == NULL) {
ERROR_MSG("Could not create context");
return;
}
int rv = EVP_PKEY_fromdata_init(ctx);
if (rv <= 0) {
ERROR_MSG("Could not EVP_PKEY_fromdata_init:%d", rv);
EVP_PKEY_CTX_free(ctx);
return;
}
rv = EVP_PKEY_fromdata(ctx, &pkey, selection, params);
if (rv <= 0) {
ERROR_MSG("Could not EVP_PKEY_fromdata:%d", rv);
EVP_PKEY_CTX_free(ctx);
return;
}
EVP_PKEY_CTX_free(ctx);
}
#endif