-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathcurve25519.rs
More file actions
675 lines (646 loc) · 24.2 KB
/
curve25519.rs
File metadata and controls
675 lines (646 loc) · 24.2 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/*!
This module provides a Rust wrapper for the wolfCrypt library's Curve25519
functionality.
*/
#![cfg(curve25519)]
#[cfg(random)]
use crate::random::RNG;
use crate::sys;
use core::mem::MaybeUninit;
pub struct Curve25519Key {
wc_key: sys::curve25519_key,
}
impl Curve25519Key {
/// Curve 25519 key size (32 bytes).
pub const KEYSIZE: usize = sys::CURVE25519_KEYSIZE as usize;
/// Check that a public key buffer holds a valid Curve25519 key value
/// given the endian ordering.
///
/// # Parameters
///
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn check_public(public: &[u8], big_endian: bool) -> Result<(), i32> {
let public_size = crate::buffer_len_to_u32(public.len())?;
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_check_public(public.as_ptr(), public_size,
endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Generate a new private key.
///
/// # Parameters
///
/// * `rng`: Random number generator struct to use for blinding operation.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
#[cfg(random)]
pub fn generate(rng: &mut RNG) -> Result<Self, i32> {
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let rc = unsafe {
sys::wc_curve25519_make_key(&mut rng.wc_rng, Self::KEYSIZE as i32,
&mut curve25519key.wc_key)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Generate a new private key as a bare vector.
///
/// # Parameters
///
/// * `rng`: Random number generator struct to use for blinding operation.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
#[cfg(random)]
pub fn generate_priv(rng: &mut RNG, out: &mut [u8]) -> Result<(), i32> {
if out.len() != Self::KEYSIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
let rc = unsafe {
sys::wc_curve25519_make_priv(&mut rng.wc_rng, Self::KEYSIZE as i32, out.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Import a Curve25519 private key only (big-endian only).
///
/// # Parameters
///
/// * `private`: Buffer containing the Curve25519 private key.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn import_private(private: &[u8]) -> Result<Self, i32> {
let private_size = crate::buffer_len_to_u32(private.len())?;
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let rc = unsafe {
sys::wc_curve25519_import_private(private.as_ptr(), private_size,
&mut curve25519key.wc_key)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Import a Curve25519 private key only (big or little endian).
///
/// # Parameters
///
/// * `private`: Buffer containing the Curve25519 private key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn import_private_ex(private: &[u8], big_endian: bool) -> Result<Self, i32> {
let private_size = crate::buffer_len_to_u32(private.len())?;
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_import_private_ex(private.as_ptr(),
private_size, &mut curve25519key.wc_key, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Import a Curve25519 public/private key pair (big-endian only).
///
/// # Parameters
///
/// * `private`: Buffer containing the Curve25519 private key.
/// * `public`: Buffer containing the Curve25519 public key.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn import_private_raw(private: &[u8], public: &[u8]) -> Result<Self, i32> {
let private_size = crate::buffer_len_to_u32(private.len())?;
let public_size = crate::buffer_len_to_u32(public.len())?;
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let rc = unsafe {
sys::wc_curve25519_import_private_raw(private.as_ptr(),
private_size, public.as_ptr(), public_size,
&mut curve25519key.wc_key)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Import a Curve25519 public/private key pair (big or little endian).
///
/// # Parameters
///
/// * `private`: Buffer containing the Curve25519 private key.
/// * `public`: Buffer containing the Curve25519 public key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn import_private_raw_ex(private: &[u8], public: &[u8], big_endian: bool) -> Result<Self, i32> {
let private_size = crate::buffer_len_to_u32(private.len())?;
let public_size = crate::buffer_len_to_u32(public.len())?;
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_import_private_raw_ex(private.as_ptr(),
private_size, public.as_ptr(), public_size,
&mut curve25519key.wc_key, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Import a Curve25519 public key (big-endian only).
///
/// # Parameters
///
/// * `public`: Buffer containing the Curve25519 public key.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn import_public(public: &[u8]) -> Result<Self, i32> {
let public_size = crate::buffer_len_to_u32(public.len())?;
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let rc = unsafe {
sys::wc_curve25519_import_public(public.as_ptr(), public_size,
&mut curve25519key.wc_key)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Import a Curve25519 public key (big or little endian).
///
/// # Parameters
///
/// * `public`: Buffer containing the Curve25519 public key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(curve25519key) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn import_public_ex(public: &[u8], big_endian: bool) -> Result<Self, i32> {
let public_size = crate::buffer_len_to_u32(public.len())?;
let mut wc_key: MaybeUninit<sys::curve25519_key> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_curve25519_init(wc_key.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
let wc_key = unsafe { wc_key.assume_init() };
let mut curve25519key = Curve25519Key { wc_key };
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_import_public_ex(public.as_ptr(), public_size,
&mut curve25519key.wc_key, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(curve25519key)
}
/// Compute the public key from an existing private key using bare vectors.
///
/// # Parameters
///
/// * `private`: Private key (input).
/// * `public`: Buffer in which to store the computed public key.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn make_pub(private: &[u8], public: &mut [u8]) -> Result<(), i32> {
let private_size = crate::buffer_len_to_i32(private.len())?;
let public_size = crate::buffer_len_to_i32(public.len())?;
let rc = unsafe {
sys::wc_curve25519_make_pub(private_size, private.as_ptr(),
public_size, public.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Compute the public key from an existing private key using bare vectors
/// with blinding.
///
/// # Parameters
///
/// * `private`: Private key (input).
/// * `public`: Buffer in which to store the computed public key.
/// * `rng`: Random number generator struct to use for blinding operation.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
#[cfg(all(curve25519_blinding, random))]
pub fn make_pub_blind(private: &[u8], public: &mut [u8], rng: &mut RNG) -> Result<(), i32> {
let private_size = crate::buffer_len_to_i32(private.len())?;
let public_size = crate::buffer_len_to_i32(public.len())?;
let rc = unsafe {
sys::wc_curve25519_make_pub_blind(private_size, private.as_ptr(),
public_size, public.as_mut_ptr(), &mut rng.wc_rng)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Compute the public key from an existing private key with supplied
/// basepoint, using bare vectors.
///
/// # Parameters
///
/// * `private`: Private key (input).
/// * `public`: Buffer in which to store the computed public key.
/// * `basepoint`: Basepoint value to use.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn make_pub_generic(private: &[u8], public: &mut [u8], basepoint: &[u8]) -> Result<(), i32> {
let private_size = crate::buffer_len_to_i32(private.len())?;
let public_size = crate::buffer_len_to_i32(public.len())?;
let basepoint_size = crate::buffer_len_to_i32(basepoint.len())?;
let rc = unsafe {
sys::wc_curve25519_generic(private_size, private.as_ptr(),
public_size, public.as_mut_ptr(), basepoint_size, basepoint.as_ptr())
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Compute the public key from an existing private key with supplied
/// basepoint, using bare vectors.
///
/// # Parameters
///
/// * `private`: Private key (input).
/// * `public`: Buffer in which to store the computed public key.
/// * `basepoint`: Basepoint value to use.
/// * `rng`: Random number generator struct to use for blinding operation.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
#[cfg(all(curve25519_blinding, random))]
pub fn make_pub_generic_blind(private: &[u8], public: &mut [u8], basepoint: &[u8], rng: &mut RNG) -> Result<(), i32> {
let private_size = crate::buffer_len_to_i32(private.len())?;
let public_size = crate::buffer_len_to_i32(public.len())?;
let basepoint_size = crate::buffer_len_to_i32(basepoint.len())?;
let rc = unsafe {
sys::wc_curve25519_generic_blind(private_size, private.as_ptr(),
public_size, public.as_mut_ptr(), basepoint_size, basepoint.as_ptr(),
&mut rng.wc_rng)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Compute a shared secret key given a secret private key and a received
/// public key. It stores the generated secret key in the buffer out and
/// returns the generated key size. Only supports big endian.
///
/// # Parameters
///
/// * `private_key`: Curve25519Key struct holding the user's private key.
/// * `public_key`: Curve25519Key struct holding the received public key.
/// * `out`: Output buffer in which to store the generated secret key.
///
/// # Returns
///
/// Returns either Ok(size) containing the number of bytes written to `out`
/// on success or Err(e) containing the wolfSSL library error code value.
pub fn shared_secret(private_key: &mut Curve25519Key, public_key: &mut Curve25519Key, out: &mut [u8]) -> Result<usize, i32> {
let mut outlen = crate::buffer_len_to_u32(out.len())?;
let rc = unsafe {
sys::wc_curve25519_shared_secret(&mut private_key.wc_key,
&mut public_key.wc_key, out.as_mut_ptr(), &mut outlen)
};
if rc != 0 {
return Err(rc);
}
Ok(outlen as usize)
}
/// Associates a `RNG` instance with this `Curve25519Key` instance.
///
/// This is necessary when generating a shared secret if wolfSSL is built
/// with the `WOLFSSL_CURVE25519_BLINDING` build option enabled.
///
/// # Parameters
///
/// * `rng`: The `RNG` struct instance to associate with this
/// `Curve25519Key` instance. The `RNG` struct should not be moved in
/// memory after calling this method.
///
/// # Returns
///
/// Returns Ok(()) on success or Err(e) containing the wolfSSL library
/// error code value.
#[cfg(all(curve25519_blinding, random))]
pub fn set_rng(&mut self, rng: &mut RNG) -> Result<(), i32> {
let rc = unsafe {
sys::wc_curve25519_set_rng(&mut self.wc_key, &mut rng.wc_rng)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Compute a shared secret key given a secret private key and a received
/// public key. It stores the generated secret key in the buffer out and
/// returns the generated key size. Supports big or little endian.
///
/// # Parameters
///
/// * `private_key`: Curve25519Key struct holding the user's private key.
/// * `public_key`: Curve25519Key struct holding the received public key.
/// * `out`: Output buffer in which to store the generated secret key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(size) containing the number of bytes written to `out`
/// on success or Err(e) containing the wolfSSL library error code value.
pub fn shared_secret_ex(private_key: &mut Curve25519Key, public_key: &mut Curve25519Key, out: &mut [u8], big_endian: bool) -> Result<usize, i32> {
let mut outlen = crate::buffer_len_to_u32(out.len())?;
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_shared_secret_ex(&mut private_key.wc_key,
&mut public_key.wc_key, out.as_mut_ptr(), &mut outlen, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(outlen as usize)
}
/// Export public and private keys from Curve25519Key struct to raw buffers
/// (big-endian only).
///
/// # Parameters
///
/// * `private`: Buffer in which to store the raw private key.
/// * `public`: Buffer in which to store the raw public key.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn export_key_raw(&mut self, private: &mut [u8], public: &mut [u8]) -> Result<(), i32> {
let mut private_size = crate::buffer_len_to_u32(private.len())?;
let mut public_size = crate::buffer_len_to_u32(public.len())?;
let rc = unsafe {
sys::wc_curve25519_export_key_raw(&mut self.wc_key,
private.as_mut_ptr(), &mut private_size,
public.as_mut_ptr(), &mut public_size)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Export public and private keys from Curve25519Key struct to raw buffers
/// (big or little endian).
///
/// # Parameters
///
/// * `private`: Buffer in which to store the raw private key.
/// * `public`: Buffer in which to store the raw public key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn export_key_raw_ex(&mut self, private: &mut [u8], public: &mut [u8], big_endian: bool) -> Result<(), i32> {
let mut private_size = crate::buffer_len_to_u32(private.len())?;
let mut public_size = crate::buffer_len_to_u32(public.len())?;
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_export_key_raw_ex(&mut self.wc_key,
private.as_mut_ptr(), &mut private_size,
public.as_mut_ptr(), &mut public_size, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Export private key from Curve25519Key struct to a raw buffer
/// (big-endian only).
///
/// # Parameters
///
/// * `out`: Buffer in which to store the raw private key.
///
/// # Returns
///
/// Returns either Ok(size) containing the number of bytes written to `out`
/// on success or Err(e) containing the wolfSSL library error code value.
pub fn export_private_raw(&mut self, out: &mut [u8]) -> Result<usize, i32> {
let mut outlen = crate::buffer_len_to_u32(out.len())?;
let rc = unsafe {
sys::wc_curve25519_export_private_raw(&mut self.wc_key,
out.as_mut_ptr(), &mut outlen)
};
if rc != 0 {
return Err(rc);
}
Ok(outlen as usize)
}
/// Export private key from Curve25519Key struct to a raw buffer
/// (big or little endian).
///
/// # Parameters
///
/// * `out`: Buffer in which to store the raw private key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(size) containing the number of bytes written to `out`
/// on success or Err(e) containing the wolfSSL library error code value.
pub fn export_private_raw_ex(&mut self, out: &mut [u8], big_endian: bool) -> Result<usize, i32> {
let mut outlen = crate::buffer_len_to_u32(out.len())?;
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_export_private_raw_ex(&mut self.wc_key,
out.as_mut_ptr(), &mut outlen, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(outlen as usize)
}
/// Export public key from Curve25519Key struct to a raw buffer
/// (big-endian only).
///
/// # Parameters
///
/// * `out`: Buffer in which to store the raw public key.
///
/// # Returns
///
/// Returns either Ok(size) containing the number of bytes written to `out`
/// on success or Err(e) containing the wolfSSL library error code value.
pub fn export_public(&mut self, out: &mut [u8]) -> Result<usize, i32> {
let mut outlen = crate::buffer_len_to_u32(out.len())?;
let rc = unsafe {
sys::wc_curve25519_export_public(&mut self.wc_key,
out.as_mut_ptr(), &mut outlen)
};
if rc != 0 {
return Err(rc);
}
Ok(outlen as usize)
}
/// Export public key from Curve25519Key struct to a raw buffer
/// (big or little endian).
///
/// # Parameters
///
/// * `out`: Buffer in which to store the raw public key.
/// * `big_endian`: True for big-endian, false for little-endian.
///
/// # Returns
///
/// Returns either Ok(size) containing the number of bytes written to `out`
/// on success or Err(e) containing the wolfSSL library error code value.
pub fn export_public_ex(&mut self, out: &mut [u8], big_endian: bool) -> Result<usize, i32> {
let mut outlen = crate::buffer_len_to_u32(out.len())?;
let endian = if big_endian {sys::EC25519_BIG_ENDIAN} else {sys::EC25519_LITTLE_ENDIAN};
let rc = unsafe {
sys::wc_curve25519_export_public_ex(&mut self.wc_key,
out.as_mut_ptr(), &mut outlen, endian as i32)
};
if rc != 0 {
return Err(rc);
}
Ok(outlen as usize)
}
}
impl Curve25519Key {
fn zeroize(&mut self) {
unsafe { crate::zeroize_raw(&mut self.wc_key); }
}
}
impl Drop for Curve25519Key {
/// Safely free the underlying wolfSSL Curve25519Key context.
///
/// This calls the `wc_curve25519_free` wolfssl library function.
///
/// The Rust Drop trait guarantees that this method is called when the
/// struct goes out of scope, automatically cleaning up resources and
/// preventing memory leaks.
fn drop(&mut self) {
unsafe { sys::wc_curve25519_free(&mut self.wc_key); }
self.zeroize();
}
}