wolfssl: Add wolfSSL plugin for cryptographic implementations
[strongswan.git] / src / libstrongswan / plugins / wolfssl / wolfssl_ed_private_key.c
1 /*
2 * Copyright (C) 2019 Sean Parkinson, wolfSSL Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #include "wolfssl_common.h"
24
25 #ifdef HAVE_ED25519
26
27 #include "wolfssl_ed_private_key.h"
28
29 #include <utils/debug.h>
30
31 #include <wolfssl/wolfcrypt/ed25519.h>
32
33 typedef struct private_private_key_t private_private_key_t;
34
35 /**
36 * Private data
37 */
38 struct private_private_key_t {
39
40 /**
41 * Public interface
42 */
43 private_key_t public;
44
45 /**
46 * Key object
47 */
48 ed25519_key key;
49
50 /**
51 * Key type
52 */
53 key_type_t type;
54
55 /**
56 * reference count
57 */
58 refcount_t ref;
59 };
60
61
62 /* from ed public key */
63 bool wolfssl_ed_fingerprint(ed25519_key *key, cred_encoding_type_t type,
64 chunk_t *fp);
65
66 METHOD(private_key_t, sign, bool,
67 private_private_key_t *this, signature_scheme_t scheme,
68 void *params, chunk_t data, chunk_t *signature)
69 {
70 word32 len;
71 int ret;
72 byte dummy[1];
73
74 if (this->type == KEY_ED25519 && scheme != SIGN_ED25519)
75 {
76 DBG1(DBG_LIB, "signature scheme %N not supported by %N key",
77 signature_scheme_names, scheme, key_type_names, this->type);
78 return FALSE;
79 }
80
81 if (data.ptr == NULL && data.len == 0)
82 {
83 data.ptr = dummy;
84 }
85
86 len = ED25519_SIG_SIZE;
87 *signature = chunk_alloc(len);
88 ret = wc_ed25519_sign_msg(data.ptr, data.len, signature->ptr, &len,
89 &this->key);
90 return ret == 0;
91 }
92
93 METHOD(private_key_t, decrypt, bool,
94 private_private_key_t *this, encryption_scheme_t scheme,
95 chunk_t crypto, chunk_t *plain)
96 {
97 DBG1(DBG_LIB, "EdDSA private key decryption not implemented");
98 return FALSE;
99 }
100
101 METHOD(private_key_t, get_keysize, int,
102 private_private_key_t *this)
103 {
104 return ED25519_KEY_SIZE * 8;
105 }
106
107 METHOD(private_key_t, get_type, key_type_t,
108 private_private_key_t *this)
109 {
110 return this->type;
111 }
112
113 METHOD(private_key_t, get_public_key, public_key_t*,
114 private_private_key_t *this)
115 {
116 public_key_t *public;
117 chunk_t key;
118 word32 len = ED25519_PUB_KEY_SIZE;
119
120 /* Allocate on stack */
121 key = chunk_alloca(len);
122 if (wc_ed25519_export_public(&this->key, key.ptr, &len) != 0)
123 {
124 chunk_free(&key);
125 return NULL;
126 }
127 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, this->type,
128 BUILD_EDDSA_PUB, key, BUILD_END);
129 return public;
130 }
131
132 METHOD(private_key_t, get_fingerprint, bool,
133 private_private_key_t *this, cred_encoding_type_t type,
134 chunk_t *fingerprint)
135 {
136 return wolfssl_ed_fingerprint(&this->key, type, fingerprint);
137 }
138
139 METHOD(private_key_t, get_encoding, bool,
140 private_private_key_t *this, cred_encoding_type_t type, chunk_t *encoding)
141 {
142 int ret;
143
144 switch (type)
145 {
146 case PRIVKEY_ASN1_DER:
147 case PRIVKEY_PEM:
148 {
149 bool success = TRUE;
150
151 *encoding = chunk_alloc(ED25519_PRV_KEY_SIZE + 20);
152 ret = wc_Ed25519PrivateKeyToDer(&this->key, encoding->ptr,
153 encoding->len);
154 if (ret < 0)
155 {
156 chunk_free(encoding);
157 return FALSE;
158 }
159 encoding->len = ret;
160
161 if (type == PRIVKEY_PEM)
162 {
163 chunk_t asn1_encoding = *encoding;
164
165 success = lib->encoding->encode(lib->encoding, PRIVKEY_PEM,
166 NULL, encoding, CRED_PART_EDDSA_PRIV_ASN1_DER,
167 asn1_encoding, CRED_PART_END);
168 chunk_clear(&asn1_encoding);
169 }
170 return success;
171 }
172 default:
173 return FALSE;
174 }
175 }
176
177 METHOD(private_key_t, get_ref, private_key_t*,
178 private_private_key_t *this)
179 {
180 ref_get(&this->ref);
181 return &this->public;
182 }
183
184 METHOD(private_key_t, destroy, void,
185 private_private_key_t *this)
186 {
187 if (ref_put(&this->ref))
188 {
189 lib->encoding->clear_cache(lib->encoding, &this->key);
190 wc_ed25519_free(&this->key);
191 free(this);
192 }
193 }
194
195 /**
196 * Internal generic constructor
197 */
198 static private_private_key_t *create_internal(key_type_t type)
199 {
200 private_private_key_t *this;
201
202 INIT(this,
203 .public = {
204 .get_type = _get_type,
205 .sign = _sign,
206 .decrypt = _decrypt,
207 .get_keysize = _get_keysize,
208 .get_public_key = _get_public_key,
209 .equals = private_key_equals,
210 .belongs_to = private_key_belongs_to,
211 .get_fingerprint = _get_fingerprint,
212 .has_fingerprint = private_key_has_fingerprint,
213 .get_encoding = _get_encoding,
214 .get_ref = _get_ref,
215 .destroy = _destroy,
216 },
217 .type = type,
218 .ref = 1,
219 );
220
221 if (wc_ed25519_init(&this->key) != 0)
222 {
223 free(this);
224 this = NULL;
225 }
226 return this;
227 }
228
229 /*
230 * Described in header
231 */
232 private_key_t *wolfssl_ed_private_key_gen(key_type_t type, va_list args)
233 {
234 private_private_key_t *this;
235 WC_RNG rng;
236 int ret;
237
238 if (wc_InitRng(&rng) != 0)
239 {
240 DBG1(DBG_LIB, "initializing random failed");
241 return NULL;
242 }
243
244 while (TRUE)
245 {
246 switch (va_arg(args, builder_part_t))
247 {
248 case BUILD_KEY_SIZE:
249 /* just ignore the key size */
250 va_arg(args, u_int);
251 continue;
252 case BUILD_END:
253 break;
254 default:
255 return NULL;
256 }
257 break;
258 }
259
260 switch (type)
261 {
262 case KEY_ED25519:
263 break;
264 default:
265 return NULL;
266 }
267 this = create_internal(type);
268 if (this == NULL)
269 {
270 return NULL;
271 }
272
273 ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &this->key);
274 wc_FreeRng(&rng);
275 if (ret < 0)
276 {
277 DBG1(DBG_LIB, "generating %N key failed", key_type_names, type);
278 destroy(this);
279 return NULL;
280 }
281
282 return &this->public;
283 }
284
285 /*
286 * Described in header
287 */
288 private_key_t *wolfssl_ed_private_key_load(key_type_t type, va_list args)
289 {
290 private_private_key_t *this;
291 chunk_t blob = chunk_empty, priv = chunk_empty;
292 int idx;
293 int ret = -1;
294
295 while (TRUE)
296 {
297 switch (va_arg(args, builder_part_t))
298 {
299 case BUILD_BLOB_ASN1_DER:
300 blob = va_arg(args, chunk_t);
301 continue;
302 case BUILD_EDDSA_PRIV_ASN1_DER:
303 priv = va_arg(args, chunk_t);
304 continue;
305 case BUILD_END:
306 break;
307 default:
308 return NULL;
309 }
310 break;
311 }
312 this = create_internal(type);
313 if (this == NULL)
314 {
315 return NULL;
316 }
317
318 if (priv.len)
319 {
320 /* Check for ASN.1 wrapped key (Octet String == 0x04) */
321 if (priv.len == ED25519_KEY_SIZE + 2 && priv.ptr[0] == 0x04 &&
322 priv.ptr[1] == ED25519_KEY_SIZE)
323 {
324 priv = chunk_skip(priv, 2);
325 }
326 ret = wc_ed25519_import_private_only(priv.ptr, priv.len, &this->key);
327 if (ret == 0)
328 {
329 ret = wc_ed25519_make_public(&this->key, this->key.p,
330 ED25519_PUB_KEY_SIZE);
331 }
332 if (ret == 0)
333 {
334 /* Fix internal state now public key set */
335 memmove(this->key.k + ED25519_KEY_SIZE, this->key.p,
336 ED25519_PUB_KEY_SIZE);
337 this->key.pubKeySet = 1;
338 }
339 }
340 else if (blob.len)
341 {
342 idx = 0;
343 ret = wc_Ed25519PrivateKeyDecode(blob.ptr, &idx, &this->key, blob.len);
344 }
345
346 if (ret != 0)
347 {
348 destroy(this);
349 return NULL;
350 }
351 return &this->public;
352 }
353
354 #endif /* HAVE_ED25519 */