Skip to content

Commit d47108c

Browse files
authored
Merge pull request #9368 from holtrop/rust-wc-ed25519
Rust wrapper: add wolfssl::wolfcrypt::ed25519 module
2 parents e0eac95 + 7cbcd0b commit d47108c

File tree

9 files changed

+1493
-37
lines changed

9 files changed

+1493
-37
lines changed

doc/dox_comments/header_files/ed25519.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ int wc_ed25519ctx_verify_msg(const byte* sig, word32 siglen, const byte* msg,
440440
byte hash[] = { initialize with SHA-512 hash of message };
441441
byte context[] = { initialize with context of signature };
442442
// initialize key with received public key
443-
ret = wc_ed25519ph_verify_hash(sig, sizeof(sig), msg, sizeof(msg),
444-
&verified, &key, );
443+
ret = wc_ed25519ph_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
444+
&verified, &key, context, sizeof(context));
445445
if (ret < 0) {
446446
// error performing verification
447447
} else if (verified == 0)
@@ -496,8 +496,8 @@ int wc_ed25519ph_verify_hash(const byte* sig, word32 siglen, const byte* hash,
496496
byte msg[] = { initialize with message };
497497
byte context[] = { initialize with context of signature };
498498
// initialize key with received public key
499-
ret = wc_ed25519ctx_verify_msg(sig, sizeof(sig), msg, sizeof(msg),
500-
&verified, &key, );
499+
ret = wc_ed25519ph_verify_msg(sig, sizeof(sig), msg, sizeof(msg),
500+
&verified, &key, context, sizeof(context));
501501
if (ret < 0) {
502502
// error performing verification
503503
} else if (verified == 0)
@@ -506,8 +506,8 @@ int wc_ed25519ph_verify_hash(const byte* sig, word32 siglen, const byte* hash,
506506
\endcode
507507
508508
\sa wc_ed25519_verify_msg
509+
\sa wc_ed25519ctx_verify_msg
509510
\sa wc_ed25519ph_verify_hash
510-
\sa wc_ed25519ph_verify_msg
511511
\sa wc_ed25519_sign_msg
512512
*/
513513

@@ -747,7 +747,7 @@ int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
747747
748748
ed25519_key key;
749749
wc_ed25519_init_key(&key);
750-
ret = wc_ed25519_import_private_key(priv, sizeof(priv), pub, sizeof(pub),
750+
ret = wc_ed25519_import_private_key_ex(priv, sizeof(priv), pub, sizeof(pub),
751751
&key, 1);
752752
if (ret != 0) {
753753
// error importing key
@@ -774,7 +774,7 @@ int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
774774
\return 0 Returned upon successfully exporting the public key.
775775
\return BAD_FUNC_ARG Returned if any of the input values evaluate to NULL.
776776
\return BUFFER_E Returned if the buffer provided is not large enough to
777-
store the private key. Upon returning this error, the function sets the
777+
store the public key. Upon returning this error, the function sets the
778778
size required in outLen.
779779
780780
\param [in] key Pointer to an ed25519_key structure from which to export the
@@ -801,10 +801,11 @@ int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
801801
802802
\sa wc_ed25519_import_public
803803
\sa wc_ed25519_import_public_ex
804+
\sa wc_ed25519_export_private
804805
\sa wc_ed25519_export_private_only
805806
*/
806807

807-
int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen);
808+
int wc_ed25519_export_public(const ed25519_key* key, byte* out, word32* outLen);
808809

809810
/*!
810811
\ingroup ED25519
@@ -840,11 +841,12 @@ int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen);
840841
\endcode
841842
842843
\sa wc_ed25519_export_public
844+
\sa wc_ed25519_export_private
843845
\sa wc_ed25519_import_private_key
844846
\sa wc_ed25519_import_private_key_ex
845847
*/
846848

847-
int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen);
849+
int wc_ed25519_export_private_only(const ed25519_key* key, byte* out, word32* outLen);
848850

849851
/*!
850852
\ingroup ED25519
@@ -888,7 +890,7 @@ int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen);
888890
\sa wc_ed25519_export_private_only
889891
*/
890892

891-
int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen);
893+
int wc_ed25519_export_private(const ed25519_key* key, byte* out, word32* outLen);
892894

893895
/*!
894896
\ingroup ED25519
@@ -935,7 +937,7 @@ int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen);
935937
\sa wc_ed25519_export_public
936938
*/
937939

938-
int wc_ed25519_export_key(ed25519_key* key,
940+
int wc_ed25519_export_key(const ed25519_key* key,
939941
byte* priv, word32 *privSz,
940942
byte* pub, word32 *pubSz);
941943

@@ -999,7 +1001,7 @@ int wc_ed25519_check_key(ed25519_key* key);
9991001
\sa wc_ed25519_make_key
10001002
*/
10011003

1002-
int wc_ed25519_size(ed25519_key* key);
1004+
int wc_ed25519_size(const ed25519_key* key);
10031005

10041006
/*!
10051007
\ingroup ED25519
@@ -1028,7 +1030,7 @@ int wc_ed25519_size(ed25519_key* key);
10281030
\sa wc_ed25519_pub_size
10291031
*/
10301032

1031-
int wc_ed25519_priv_size(ed25519_key* key);
1033+
int wc_ed25519_priv_size(const ed25519_key* key);
10321034

10331035
/*!
10341036
\ingroup ED25519
@@ -1056,7 +1058,7 @@ int wc_ed25519_priv_size(ed25519_key* key);
10561058
\sa wc_ed25519_priv_size
10571059
*/
10581060

1059-
int wc_ed25519_pub_size(ed25519_key* key);
1061+
int wc_ed25519_pub_size(const ed25519_key* key);
10601062

10611063
/*!
10621064
\ingroup ED25519
@@ -1084,4 +1086,4 @@ int wc_ed25519_pub_size(ed25519_key* key);
10841086
\sa wc_ed25519_sign_msg
10851087
*/
10861088

1087-
int wc_ed25519_sig_size(ed25519_key* key);
1089+
int wc_ed25519_sig_size(const ed25519_key* key);

wolfcrypt/src/asn.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13499,7 +13499,7 @@ int SetAsymKeyDerPublic(const byte* pubKey, word32 pubKeyLen,
1349913499
* @return BAD_FUNC_ARG when key is NULL.
1350013500
* @return MEMORY_E when dynamic memory allocation failed.
1350113501
*/
13502-
int wc_Ed25519PublicKeyToDer(ed25519_key* key, byte* output, word32 inLen,
13502+
int wc_Ed25519PublicKeyToDer(const ed25519_key* key, byte* output, word32 inLen,
1350313503
int withAlg)
1350413504
{
1350513505
int ret;
@@ -37770,7 +37770,7 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
3777037770
#if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
3777137771
/* Write a Private ED25519 key, including public to DER format,
3777237772
* length on success else < 0 */
37773-
int wc_Ed25519KeyToDer(ed25519_key* key, byte* output, word32 inLen)
37773+
int wc_Ed25519KeyToDer(const ed25519_key* key, byte* output, word32 inLen)
3777437774
{
3777537775
if (key == NULL) {
3777637776
return BAD_FUNC_ARG;
@@ -37781,7 +37781,7 @@ int wc_Ed25519KeyToDer(ed25519_key* key, byte* output, word32 inLen)
3778137781

3778237782
/* Write only private ED25519 key to DER format,
3778337783
* length on success else < 0 */
37784-
int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output, word32 inLen)
37784+
int wc_Ed25519PrivateKeyToDer(const ed25519_key* key, byte* output, word32 inLen)
3778537785
{
3778637786
if (key == NULL) {
3778737787
return BAD_FUNC_ARG;

wolfcrypt/src/ed25519.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ void wc_ed25519_free(ed25519_key* key)
11171117
to the final output length.
11181118
returns 0 on success
11191119
*/
1120-
int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen)
1120+
int wc_ed25519_export_public(const ed25519_key* key, byte* out, word32* outLen)
11211121
{
11221122
/* sanity check on arguments */
11231123
if (key == NULL || out == NULL || outLen == NULL)
@@ -1343,7 +1343,7 @@ int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
13431343
to the final output length.
13441344
returns 0 on success
13451345
*/
1346-
int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen)
1346+
int wc_ed25519_export_private_only(const ed25519_key* key, byte* out, word32* outLen)
13471347
{
13481348
/* sanity checks on arguments */
13491349
if (key == NULL || out == NULL || outLen == NULL)
@@ -1366,7 +1366,7 @@ int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen)
13661366
to the final output length.
13671367
returns 0 on success
13681368
*/
1369-
int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen)
1369+
int wc_ed25519_export_private(const ed25519_key* key, byte* out, word32* outLen)
13701370
{
13711371
/* sanity checks on arguments */
13721372
if (key == NULL || out == NULL || outLen == NULL)
@@ -1386,7 +1386,7 @@ int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen)
13861386
/* export full private key and public key
13871387
return 0 on success
13881388
*/
1389-
int wc_ed25519_export_key(ed25519_key* key,
1389+
int wc_ed25519_export_key(const ed25519_key* key,
13901390
byte* priv, word32 *privSz,
13911391
byte* pub, word32 *pubSz)
13921392
{
@@ -1487,7 +1487,7 @@ int wc_ed25519_check_key(ed25519_key* key)
14871487
}
14881488

14891489
/* returns the private key size (secret only) in bytes */
1490-
int wc_ed25519_size(ed25519_key* key)
1490+
int wc_ed25519_size(const ed25519_key* key)
14911491
{
14921492
if (key == NULL)
14931493
return BAD_FUNC_ARG;
@@ -1496,7 +1496,7 @@ int wc_ed25519_size(ed25519_key* key)
14961496
}
14971497

14981498
/* returns the private key size (secret + public) in bytes */
1499-
int wc_ed25519_priv_size(ed25519_key* key)
1499+
int wc_ed25519_priv_size(const ed25519_key* key)
15001500
{
15011501
if (key == NULL)
15021502
return BAD_FUNC_ARG;
@@ -1505,7 +1505,7 @@ int wc_ed25519_priv_size(ed25519_key* key)
15051505
}
15061506

15071507
/* returns the compressed key size in bytes (public key) */
1508-
int wc_ed25519_pub_size(ed25519_key* key)
1508+
int wc_ed25519_pub_size(const ed25519_key* key)
15091509
{
15101510
if (key == NULL)
15111511
return BAD_FUNC_ARG;
@@ -1514,7 +1514,7 @@ int wc_ed25519_pub_size(ed25519_key* key)
15141514
}
15151515

15161516
/* returns the size of signature in bytes */
1517-
int wc_ed25519_sig_size(ed25519_key* key)
1517+
int wc_ed25519_sig_size(const ed25519_key* key)
15181518
{
15191519
if (key == NULL)
15201520
return BAD_FUNC_ARG;

wolfssl/wolfcrypt/asn_public.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,11 @@ WOLFSSL_API int wc_Ed25519PublicKeyDecode(const byte* input, word32* inOutIdx,
728728
ed25519_key* key, word32 inSz);
729729
#endif
730730
#ifdef HAVE_ED25519_KEY_EXPORT
731-
WOLFSSL_API int wc_Ed25519KeyToDer(ed25519_key* key, byte* output,
731+
WOLFSSL_API int wc_Ed25519KeyToDer(const ed25519_key* key, byte* output,
732732
word32 inLen);
733-
WOLFSSL_API int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output,
733+
WOLFSSL_API int wc_Ed25519PrivateKeyToDer(const ed25519_key* key, byte* output,
734734
word32 inLen);
735-
WOLFSSL_API int wc_Ed25519PublicKeyToDer(ed25519_key* key, byte* output,
735+
WOLFSSL_API int wc_Ed25519PublicKeyToDer(const ed25519_key* key, byte* output,
736736
word32 inLen, int withAlg);
737737
#endif
738738
#endif /* HAVE_ED25519 */

wolfssl/wolfcrypt/ed25519.h

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

208208
#ifdef HAVE_ED25519_KEY_EXPORT
209209
WOLFSSL_API
210-
int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen);
210+
int wc_ed25519_export_public(const ed25519_key* key, byte* out, word32* outLen);
211211
WOLFSSL_API
212-
int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen);
212+
int wc_ed25519_export_private_only(const ed25519_key* key, byte* out, word32* outLen);
213213
WOLFSSL_API
214-
int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen);
214+
int wc_ed25519_export_private(const ed25519_key* key, byte* out, word32* outLen);
215215
WOLFSSL_API
216-
int wc_ed25519_export_key(ed25519_key* key,
216+
int wc_ed25519_export_key(const ed25519_key* key,
217217
byte* priv, word32 *privSz,
218218
byte* pub, word32 *pubSz);
219219
#endif /* HAVE_ED25519_KEY_EXPORT */
@@ -223,13 +223,13 @@ int wc_ed25519_check_key(ed25519_key* key);
223223

224224
/* size helper */
225225
WOLFSSL_API
226-
int wc_ed25519_size(ed25519_key* key);
226+
int wc_ed25519_size(const ed25519_key* key);
227227
WOLFSSL_API
228-
int wc_ed25519_priv_size(ed25519_key* key);
228+
int wc_ed25519_priv_size(const ed25519_key* key);
229229
WOLFSSL_API
230-
int wc_ed25519_pub_size(ed25519_key* key);
230+
int wc_ed25519_pub_size(const ed25519_key* key);
231231
WOLFSSL_API
232-
int wc_ed25519_sig_size(ed25519_key* key);
232+
int wc_ed25519_sig_size(const ed25519_key* key);
233233

234234
#ifdef __cplusplus
235235
} /* extern "C" */

wrapper/rust/include.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/aes.rs
2020
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
23+
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/ed25519.rs
2324
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/hkdf.rs
2425
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/hmac.rs
2526
EXTRA_DIST += wrapper/rust/wolfssl/src/wolfcrypt/kdf.rs
@@ -31,6 +32,7 @@ EXTRA_DIST += wrapper/rust/wolfssl/tests/test_aes.rs
3132
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_cmac.rs
3233
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_dh.rs
3334
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_ecc.rs
35+
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_ed25519.rs
3436
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_hkdf.rs
3537
EXTRA_DIST += wrapper/rust/wolfssl/tests/test_hmac.rs
3638
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
@@ -22,6 +22,7 @@ pub mod aes;
2222
pub mod cmac;
2323
pub mod dh;
2424
pub mod ecc;
25+
pub mod ed25519;
2526
pub mod hkdf;
2627
pub mod hmac;
2728
pub mod kdf;

0 commit comments

Comments
 (0)