Skip to content

Commit b0a7f59

Browse files
authored
Merge pull request #9379 from holtrop/rust-wc-ed448
Rust wrapper: add wolfssl::wolfcrypt::ed448 module
2 parents aa0b37a + 7f0e575 commit b0a7f59

File tree

8 files changed

+1460
-44
lines changed

8 files changed

+1460
-44
lines changed

wolfcrypt/src/ed448.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ void wc_ed448_free(ed448_key* key)
969969
* ECC_BAD_ARG_E when outLen is less than ED448_PUB_KEY_SIZE,
970970
* 0 otherwise.
971971
*/
972-
int wc_ed448_export_public(ed448_key* key, byte* out, word32* outLen)
972+
int wc_ed448_export_public(const ed448_key* key, byte* out, word32* outLen)
973973
{
974974
int ret = 0;
975975

@@ -1212,7 +1212,7 @@ int wc_ed448_import_private_key(const byte* priv, word32 privSz,
12121212
* ECC_BAD_ARG_E when outLen is less than ED448_KEY_SIZE,
12131213
* 0 otherwise.
12141214
*/
1215-
int wc_ed448_export_private_only(ed448_key* key, byte* out, word32* outLen)
1215+
int wc_ed448_export_private_only(const ed448_key* key, byte* out, word32* outLen)
12161216
{
12171217
int ret = 0;
12181218

@@ -1244,7 +1244,7 @@ int wc_ed448_export_private_only(ed448_key* key, byte* out, word32* outLen)
12441244
* BUFFER_E when outLen is less than ED448_PRV_KEY_SIZE,
12451245
* 0 otherwise.
12461246
*/
1247-
int wc_ed448_export_private(ed448_key* key, byte* out, word32* outLen)
1247+
int wc_ed448_export_private(const ed448_key* key, byte* out, word32* outLen)
12481248
{
12491249
int ret = 0;
12501250

@@ -1279,7 +1279,7 @@ int wc_ed448_export_private(ed448_key* key, byte* out, word32* outLen)
12791279
* than ED448_PUB_KEY_SIZE,
12801280
* 0 otherwise.
12811281
*/
1282-
int wc_ed448_export_key(ed448_key* key, byte* priv, word32 *privSz,
1282+
int wc_ed448_export_key(const ed448_key* key, byte* priv, word32 *privSz,
12831283
byte* pub, word32 *pubSz)
12841284
{
12851285
int ret = 0;
@@ -1392,7 +1392,7 @@ int wc_ed448_check_key(ed448_key* key)
13921392
* returns BAD_FUNC_ARG when key is NULL,
13931393
* ED448_KEY_SIZE otherwise.
13941394
*/
1395-
int wc_ed448_size(ed448_key* key)
1395+
int wc_ed448_size(const ed448_key* key)
13961396
{
13971397
int ret = ED448_KEY_SIZE;
13981398

@@ -1409,7 +1409,7 @@ int wc_ed448_size(ed448_key* key)
14091409
* returns BAD_FUNC_ARG when key is NULL,
14101410
* ED448_PRV_KEY_SIZE otherwise.
14111411
*/
1412-
int wc_ed448_priv_size(ed448_key* key)
1412+
int wc_ed448_priv_size(const ed448_key* key)
14131413
{
14141414
int ret = ED448_PRV_KEY_SIZE;
14151415

@@ -1426,7 +1426,7 @@ int wc_ed448_priv_size(ed448_key* key)
14261426
* returns BAD_FUNC_ARG when key is NULL,
14271427
* ED448_PUB_KEY_SIZE otherwise.
14281428
*/
1429-
int wc_ed448_pub_size(ed448_key* key)
1429+
int wc_ed448_pub_size(const ed448_key* key)
14301430
{
14311431
int ret = ED448_PUB_KEY_SIZE;
14321432

@@ -1443,7 +1443,7 @@ int wc_ed448_pub_size(ed448_key* key)
14431443
* returns BAD_FUNC_ARG when key is NULL,
14441444
* ED448_SIG_SIZE otherwise.
14451445
*/
1446-
int wc_ed448_sig_size(ed448_key* key)
1446+
int wc_ed448_sig_size(const ed448_key* key)
14471447
{
14481448
int ret = ED448_SIG_SIZE;
14491449

wolfssl/wolfcrypt/ed448.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ int wc_ed448_import_private_key_ex(const byte* priv, word32 privSz,
183183

184184
#ifdef HAVE_ED448_KEY_EXPORT
185185
WOLFSSL_API
186-
int wc_ed448_export_public(ed448_key* key, byte* out, word32* outLen);
186+
int wc_ed448_export_public(const ed448_key* key, byte* out, word32* outLen);
187187
WOLFSSL_API
188-
int wc_ed448_export_private_only(ed448_key* key, byte* out, word32* outLen);
188+
int wc_ed448_export_private_only(const ed448_key* key, byte* out, word32* outLen);
189189
WOLFSSL_API
190-
int wc_ed448_export_private(ed448_key* key, byte* out, word32* outLen);
190+
int wc_ed448_export_private(const ed448_key* key, byte* out, word32* outLen);
191191
WOLFSSL_API
192-
int wc_ed448_export_key(ed448_key* key, byte* priv, word32 *privSz,
192+
int wc_ed448_export_key(const ed448_key* key, byte* priv, word32 *privSz,
193193
byte* pub, word32 *pubSz);
194194
#endif /* HAVE_ED448_KEY_EXPORT */
195195

@@ -198,13 +198,13 @@ int wc_ed448_check_key(ed448_key* key);
198198

199199
/* size helper */
200200
WOLFSSL_API
201-
int wc_ed448_size(ed448_key* key);
201+
int wc_ed448_size(const ed448_key* key);
202202
WOLFSSL_API
203-
int wc_ed448_priv_size(ed448_key* key);
203+
int wc_ed448_priv_size(const ed448_key* key);
204204
WOLFSSL_API
205-
int wc_ed448_pub_size(ed448_key* key);
205+
int wc_ed448_pub_size(const ed448_key* key);
206206
WOLFSSL_API
207-
int wc_ed448_sig_size(ed448_key* key);
207+
int wc_ed448_sig_size(const ed448_key* key);
208208

209209
#ifdef __cplusplus
210210
} /* extern "C" */

wrapper/rust/include.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/cmac.rs
2121
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/dh.rs
2222
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/ecc.rs
2323
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs
24+
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/ed448.rs
2425
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs
2526
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs
2627
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs
@@ -33,6 +34,7 @@ EXTRA_DIST += wrapper/rust/wolfssl/tests/test_cmac.rs
3334
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_dh.rs
3435
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_ecc.rs
3536
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_ed25519.rs
37+
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_ed448.rs
3638
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_hkdf.rs
3739
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_hmac.rs
3840
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_kdf.rs

wrapper/rust/wolfssl/src/wolfcrypt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod cmac;
2323
pub mod dh;
2424
pub mod ecc;
2525
pub mod ed25519;
26+
pub mod ed448;
2627
pub mod hkdf;
2728
pub mod hmac;
2829
pub mod kdf;

wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl Ed25519 {
627627
/// # Parameters
628628
///
629629
/// * `hash`: Message digest to sign.
630-
/// * `context`: Buffer containing context for which hash is being signed.
630+
/// * `context`: Optional buffer containing context for which hash is being signed.
631631
/// * `signature`: Output buffer to hold signature.
632632
///
633633
/// # Returns
@@ -655,16 +655,21 @@ impl Ed25519 {
655655
/// ];
656656
/// let context = b"context";
657657
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
658-
/// ed.sign_hash_ph(&hash, context, &mut signature).expect("Error with sign_hash_ph()");
658+
/// ed.sign_hash_ph(&hash, Some(context), &mut signature).expect("Error with sign_hash_ph()");
659659
/// ```
660-
pub fn sign_hash_ph(&mut self, hash: &[u8], context: &[u8], signature: &mut [u8]) -> Result<usize, i32> {
660+
pub fn sign_hash_ph(&mut self, hash: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result<usize, i32> {
661661
let hash_size = hash.len() as u32;
662-
let context_size = context.len() as u8;
662+
let mut context_ptr: *const u8 = core::ptr::null();
663+
let mut context_size = 0u8;
664+
if let Some(context) = context {
665+
context_ptr = context.as_ptr();
666+
context_size = context.len() as u8;
667+
}
663668
let mut signature_size = signature.len() as u32;
664669
let rc = unsafe {
665670
ws::wc_ed25519ph_sign_hash(hash.as_ptr(), hash_size,
666671
signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key,
667-
context.as_ptr(), context_size)
672+
context_ptr, context_size)
668673
};
669674
if rc != 0 {
670675
return Err(rc);
@@ -680,7 +685,7 @@ impl Ed25519 {
680685
/// # Parameters
681686
///
682687
/// * `message`: Message digest to sign.
683-
/// * `context`: Buffer containing context for which message is being signed.
688+
/// * `context`: Optional buffer containing context for which message is being signed.
684689
/// * `signature`: Output buffer to hold signature.
685690
///
686691
/// # Returns
@@ -699,16 +704,21 @@ impl Ed25519 {
699704
/// let message = [0x42u8, 33, 55, 66];
700705
/// let context = b"context";
701706
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
702-
/// ed.sign_msg_ph(&message, context, &mut signature).expect("Error with sign_msg_ph()");
707+
/// ed.sign_msg_ph(&message, Some(context), &mut signature).expect("Error with sign_msg_ph()");
703708
/// ```
704-
pub fn sign_msg_ph(&mut self, message: &[u8], context: &[u8], signature: &mut [u8]) -> Result<usize, i32> {
709+
pub fn sign_msg_ph(&mut self, message: &[u8], context: Option<&[u8]>, signature: &mut [u8]) -> Result<usize, i32> {
705710
let message_size = message.len() as u32;
706-
let context_size = context.len() as u8;
711+
let mut context_ptr: *const u8 = core::ptr::null();
712+
let mut context_size = 0u8;
713+
if let Some(context) = context {
714+
context_ptr = context.as_ptr();
715+
context_size = context.len() as u8;
716+
}
707717
let mut signature_size = signature.len() as u32;
708718
let rc = unsafe {
709719
ws::wc_ed25519ph_sign_msg(message.as_ptr(), message_size,
710720
signature.as_mut_ptr(), &mut signature_size, &mut self.ws_key,
711-
context.as_ptr(), context_size)
721+
context_ptr, context_size)
712722
};
713723
if rc != 0 {
714724
return Err(rc);
@@ -859,7 +869,7 @@ impl Ed25519 {
859869
///
860870
/// * `signature`: Signature to verify.
861871
/// * `hash`: Message to verify the signature of.
862-
/// * `context`: Buffer containing context for which the hash was signed.
872+
/// * `context`: Optional buffer containing context for which the hash was signed.
863873
///
864874
/// # Returns
865875
///
@@ -885,19 +895,24 @@ impl Ed25519 {
885895
/// ];
886896
/// let context = b"context";
887897
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
888-
/// ed.sign_hash_ph(&hash, context, &mut signature).expect("Error with sign_hash_ph()");
889-
/// let signature_valid = ed.verify_hash_ph(&signature, &hash, context).expect("Error with verify_hash_ph()");
898+
/// ed.sign_hash_ph(&hash, Some(context), &mut signature).expect("Error with sign_hash_ph()");
899+
/// let signature_valid = ed.verify_hash_ph(&signature, &hash, Some(context)).expect("Error with verify_hash_ph()");
890900
/// assert!(signature_valid);
891901
/// ```
892-
pub fn verify_hash_ph(&mut self, signature: &[u8], hash: &[u8], context: &[u8]) -> Result<bool, i32> {
902+
pub fn verify_hash_ph(&mut self, signature: &[u8], hash: &[u8], context: Option<&[u8]>) -> Result<bool, i32> {
893903
let signature_size = signature.len() as u32;
894904
let hash_size = hash.len() as u32;
895-
let context_size = context.len() as u8;
905+
let mut context_ptr: *const u8 = core::ptr::null();
906+
let mut context_size = 0u8;
907+
if let Some(context) = context {
908+
context_ptr = context.as_ptr();
909+
context_size = context.len() as u8;
910+
}
896911
let mut res = 0i32;
897912
let rc = unsafe {
898913
ws::wc_ed25519ph_verify_hash(signature.as_ptr(), signature_size,
899914
hash.as_ptr(), hash_size, &mut res, &mut self.ws_key,
900-
context.as_ptr(), context_size)
915+
context_ptr, context_size)
901916
};
902917
if rc != 0 {
903918
return Err(rc);
@@ -914,7 +929,7 @@ impl Ed25519 {
914929
///
915930
/// * `signature`: Signature to verify.
916931
/// * `message`: Message to verify the signature of.
917-
/// * `context`: Buffer containing context for which the message was signed.
932+
/// * `context`: Option buffer containing context for which the message was signed.
918933
///
919934
/// # Returns
920935
///
@@ -931,19 +946,24 @@ impl Ed25519 {
931946
/// let message = [0x42u8, 33, 55, 66];
932947
/// let context = b"context";
933948
/// let mut signature = [0u8; Ed25519::SIG_SIZE];
934-
/// ed.sign_msg_ph(&message, context, &mut signature).expect("Error with sign_msg_ph()");
935-
/// let signature_valid = ed.verify_msg_ph(&signature, &message, context).expect("Error with verify_msg_ph()");
949+
/// ed.sign_msg_ph(&message, Some(context), &mut signature).expect("Error with sign_msg_ph()");
950+
/// let signature_valid = ed.verify_msg_ph(&signature, &message, Some(context)).expect("Error with verify_msg_ph()");
936951
/// assert!(signature_valid);
937952
/// ```
938-
pub fn verify_msg_ph(&mut self, signature: &[u8], message: &[u8], context: &[u8]) -> Result<bool, i32> {
953+
pub fn verify_msg_ph(&mut self, signature: &[u8], message: &[u8], context: Option<&[u8]>) -> Result<bool, i32> {
939954
let signature_size = signature.len() as u32;
940955
let message_size = message.len() as u32;
941-
let context_size = context.len() as u8;
956+
let mut context_ptr: *const u8 = core::ptr::null();
957+
let mut context_size = 0u8;
958+
if let Some(context) = context {
959+
context_ptr = context.as_ptr();
960+
context_size = context.len() as u8;
961+
}
942962
let mut res = 0i32;
943963
let rc = unsafe {
944964
ws::wc_ed25519ph_verify_msg(signature.as_ptr(), signature_size,
945965
message.as_ptr(), message_size, &mut res, &mut self.ws_key,
946-
context.as_ptr(), context_size)
966+
context_ptr, context_size)
947967
};
948968
if rc != 0 {
949969
return Err(rc);

0 commit comments

Comments
 (0)