Fixes for building with .NET 3.5 (new WindowsCE macro). Fix for build error with NO_WOLFSSL_MSG_EX. Fix for ECC TFM option (only set with TFM).

This commit is contained in:
David Garske
2025-04-14 16:07:03 -07:00
parent 43e68add96
commit 42644a55fb
9 changed files with 832 additions and 69 deletions

View File

@ -518,6 +518,7 @@ USE_STSAFE_VERBOSE
USE_TLSV13
USE_WOLF_STRNSTR
USS_API
WindowsCE
WC_AESXTS_STREAM_NO_REQUEST_ACCOUNTING
WC_AES_BS_WORD_SIZE
WC_AES_GCM_DEC_AUTH_EARLY

View File

@ -4321,7 +4321,8 @@ fi
if test "$ENABLED_ECC" != "no"
then
AM_CFLAGS="$AM_CFLAGS -DHAVE_ECC -DTFM_ECC256"
AM_CFLAGS="$AM_CFLAGS -DHAVE_ECC"
if test "$ENABLED_ECC_SHAMIR" = "yes" && test "$ENABLED_LOWRESOURCE" = "no"
then
AM_CFLAGS="$AM_CFLAGS -DECC_SHAMIR"
@ -4332,9 +4333,14 @@ then
AM_CFLAGS="$AM_CFLAGS -DWC_ECC_NONBLOCK"
fi
if test "$ENABLED_LOWRESOURCE" = "yes" && test "$ENABLED_FASTMATH" = "yes"
if test "$ENABLED_FASTMATH" = "yes"
then
AM_CFLAGS="$AM_CFLAGS -DALT_ECC_SIZE"
if test "$ENABLED_LOWRESOURCE" = "yes"
then
AM_CFLAGS="$AM_CFLAGS -DALT_ECC_SIZE"
else
AM_CFLAGS="$AM_CFLAGS -DTFM_ECC256"
fi
fi
ENABLED_CERTS=yes

View File

@ -165,7 +165,6 @@ curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code);
WOLFSSL_API
int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p);
#endif
WOLFSSL_API
/* raw key helpers */
WOLFSSL_API

View File

@ -187,7 +187,6 @@ ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code);
WOLFSSL_API
int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p);
#endif
WOLFSSL_API
#ifdef HAVE_ED25519_KEY_IMPORT
WOLFSSL_API

View File

@ -174,7 +174,7 @@ WOLFSSL_API void wolfSSL_SetLoggingPrefix(const char* prefix);
#define WOLFSSL_STUB(m) \
WOLFSSL_MSG(WOLFSSL_LOG_CAT(wolfSSL Stub, m, not implemented))
WOLFSSL_API int WOLFSSL_IS_DEBUG_ON(void);
#if defined(XVSNPRINTF)
#if defined(XVSNPRINTF) && !defined(NO_WOLFSSL_MSG_EX)
WOLFSSL_API void WOLFSSL_MSG_EX(const char* fmt, ...);
#define HAVE_WOLFSSL_MSG_EX
#else

View File

@ -548,7 +548,7 @@ public class wolfCrypt_Test_CSharp
IntPtr keyB = IntPtr.Zero;
IntPtr publicKeyA = IntPtr.Zero;
IntPtr publicKeyB = IntPtr.Zero;
byte[] derKey;
byte[] rawPub, rawPrivate, derKey;
Console.WriteLine("\nStarting Curve25519 shared secret test...");
@ -569,6 +569,14 @@ public class wolfCrypt_Test_CSharp
}
Console.WriteLine("Curve25519 Key generation test passed.");
/* Export Public Key A private and public to raw format */
wolfcrypt.Curve25519ExportKeyRaw(keyA, out rawPrivate, out rawPub);
/* Export Public Key B public to raw format */
rawPub = wolfcrypt.Curve25519ExportPublicKey(keyB);
/* rawPub / rawPrivate - not used */
/* Export Public Key B to DER format */
Console.WriteLine("Exporting Public Key B to DER format...");
ret = wolfcrypt.Curve25519ExportPublicKeyToDer(keyB, out derKey, true);

View File

@ -1,4 +1,25 @@
using System;
/* X509.cs
*
* Copyright (C) 2006-2025 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 2 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
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
@ -9,6 +30,23 @@ namespace wolfSSL.CSharp
{
private const string wolfssl_dll = "wolfssl.dll";
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_X509_get_der(IntPtr x509, IntPtr bufSz);
[DllImport(wolfssl_dll)]
private extern static void wolfSSL_X509_free(IntPtr x509);
[DllImport(wolfssl_dll)]
private extern static int wc_DerToPem(IntPtr der, int derSz, IntPtr pem, int pemSz, int type);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_X509_get_name_oneline(IntPtr x509Name, IntPtr buf, int bufSz);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509);
[DllImport(wolfssl_dll)]
private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfSSL_X509_get_pubkey_buffer(IntPtr x509, IntPtr buf, IntPtr bufSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -25,6 +63,7 @@ namespace wolfSSL.CSharp
private extern static IntPtr wolfSSL_X509_get_subject_name(IntPtr x509);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wolfSSL_X509_get_issuer_name(IntPtr x509);
#endif
private IntPtr x509;
private int type;
@ -51,11 +90,12 @@ namespace wolfSSL.CSharp
this.x509 = x509;
ret = wolfSSL_X509_get_name_oneline(
wolfSSL_X509_get_issuer_name(this.x509), IntPtr.Zero, 0);
this.Issuer = Marshal.PtrToStringAnsi(ret);
this.Issuer = wolfssl.PtrToStringAnsi(ret);
ret = wolfSSL_X509_get_name_oneline(
wolfSSL_X509_get_subject_name(this.x509), IntPtr.Zero, 0);
this.Subject = Marshal.PtrToStringAnsi(ret);
this.Subject = wolfssl.PtrToStringAnsi(ret);
this.isDynamic = isDynamic;
}

View File

@ -33,26 +33,66 @@ namespace wolfSSL.CSharp
/********************************
* Init wolfSSL library
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wolfCrypt_Init();
[DllImport(wolfssl_dll)]
private extern static int wolfCrypt_Cleanup();
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfCrypt_Init();
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wolfCrypt_Cleanup();
#endif
/********************************
* Random
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_rng_new(IntPtr nonce, UInt32 nonceSz, IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_rng_free(IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_RNG_GenerateBlock(IntPtr rng, IntPtr output, UInt32 sz);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_rng_new(IntPtr nonce, UInt32 nonceSz, IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static void wc_rng_free(IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RNG_GenerateBlock(IntPtr rng, IntPtr output, UInt32 sz);
#endif
/********************************
* ECC
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_key_new(IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_ecc_key_free(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_set_rng(IntPtr key, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_make_key_ex(IntPtr rng, int keysize, IntPtr key, int curve_id);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_sign_hash(IntPtr hashPtr, uint hashlen, IntPtr sigPtr, IntPtr siglen, IntPtr rng, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_verify_hash(IntPtr sigPtr, uint siglen, IntPtr hashPtr, uint hashlen, IntPtr res, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private extern static int wc_EccPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll)]
private static extern int wc_EccPublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_EccPrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_EccPublicKeyToDer(IntPtr key, byte[] output, uint inLen, int with_AlgCurve);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_ecc_key_new(IntPtr heap);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -75,11 +115,40 @@ namespace wolfSSL.CSharp
private static extern int wc_EccPrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_EccPublicKeyToDer(IntPtr key, byte[] output, uint inLen, int with_AlgCurve);
#endif
/********************************
* ECIES
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_ctx_new(int flags, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_ctx_new_ex(int flags, IntPtr rng, IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_ecc_ctx_free(IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_reset(IntPtr ctx, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_algo(IntPtr ctx, byte encAlgo, byte kdfAlgo, byte macAlgo);
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_ecc_ctx_get_own_salt(IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_peer_salt(IntPtr ctx, IntPtr salt);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_own_salt(IntPtr ctx, IntPtr salt, uint sz);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_kdf_salt(IntPtr ctx, IntPtr salt, uint sz);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_ctx_set_info(IntPtr ctx, IntPtr info, int sz);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_encrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_encrypt_ex(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx, int compressed);
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_decrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_ecc_ctx_new(int flags, IntPtr rng);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -106,18 +175,58 @@ namespace wolfSSL.CSharp
private extern static int wc_ecc_encrypt_ex(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx, int compressed);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_decrypt(IntPtr privKey, IntPtr pubKey, IntPtr msg, uint msgSz, IntPtr outBuffer, IntPtr outSz, IntPtr ctx);
#endif
/********************************
* ECDHE
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static int wc_ecc_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_ecc_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
#endif
/********************************
* RSA
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private static extern int wc_DeleteRsaKey(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll)]
private extern static int wc_InitRsaKey(IntPtr key, IntPtr heap);
[DllImport(wolfssl_dll)]
private extern static void wc_FreeRsaKey(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_MakeRsaKey(IntPtr key, int keysize, Int32 exponent, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaSSL_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, IntPtr key, IntPtr rng);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaSSL_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPublicEncrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPrivateDecrypt(IntPtr inPtr, int inLen, IntPtr outPtr, int outLen, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPrivateKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPublicKeyDecode(IntPtr keyBuf, IntPtr idx, IntPtr key, uint keyBufSz);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPSS_Sign(IntPtr hashPtr, int hashLen, IntPtr sigPtr, int sigLen, int hashType, IntPtr rng, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPSS_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, int hashType, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_RsaPSS_CheckPadding(IntPtr sigPtr, int sigLen, int hashType, IntPtr key);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr wc_NewRsaKey(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -149,11 +258,52 @@ namespace wolfSSL.CSharp
private extern static int wc_RsaPSS_Verify(IntPtr sigPtr, int sigLen, IntPtr hashPtr, int hashLen, int hashType, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_RsaPSS_CheckPadding(IntPtr sigPtr, int sigLen, int hashType, IntPtr key);
#endif
/********************************
* ED25519
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_delete(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_init(IntPtr key);
[DllImport(wolfssl_dll)]
private static extern void wc_ed25519_free(IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_make_key(IntPtr rng, int keysize, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_sign_msg(IntPtr inMsg, uint inlen, IntPtr outMsg, ref uint outlen, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_verify_msg(IntPtr sig, uint siglen, IntPtr msg, uint msgLen, ref int ret, IntPtr key);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519KeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_Ed25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg);
/* RAW format */
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_make_public(IntPtr key, IntPtr pubKey, uint pubKeySz);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_import_public(IntPtr inMsg, uint inLen, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_export_public(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_export_private(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll)]
private static extern int wc_ed25519_size(IntPtr key);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr wc_ed25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -192,11 +342,52 @@ namespace wolfSSL.CSharp
private static extern int wc_ed25519_export_private(IntPtr key, IntPtr outMsg, ref uint outLen);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern int wc_ed25519_size(IntPtr key);
#endif
/********************************
* Curve25519
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private static extern int wc_curve25519_delete(IntPtr key, IntPtr key_p);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_init(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static void wc_curve25519_free(IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_make_key(IntPtr rng, int keysize, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_shared_secret(IntPtr privateKey, IntPtr publicKey, byte[] outSharedSecret, ref int outlen);
/* ASN.1 DER format */
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PrivateKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PublicKeyDecode(byte[] input, ref uint inOutIdx, IntPtr key, uint inSz);
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PrivateKeyToDer(IntPtr key, byte[] output, uint inLen);
[DllImport(wolfssl_dll)]
private static extern int wc_Curve25519PublicKeyToDer(IntPtr key, byte[] output, uint inLen, int withAlg);
/* RAW format */
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_import_private(IntPtr privKey, int privKeySz, IntPtr key);
[DllImport(wolfssl_dll)]
private static extern int wc_curve25519_export_public(IntPtr key, byte[] outBuffer, ref uint outLen);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_import_public(IntPtr pubKey, int pubKeySz, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_export_public(IntPtr key, IntPtr outPubKey, ref int outlen);
[DllImport(wolfssl_dll)]
private static extern int wc_curve25519_export_key_raw(IntPtr key, byte[] priv, ref uint privSz, byte[] pub, ref uint pubSz);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_import_private_raw(IntPtr privKey, IntPtr pubKey, IntPtr key);
[DllImport(wolfssl_dll)]
private extern static int wc_curve25519_export_private_raw(IntPtr key, IntPtr outPrivKey, IntPtr outPubKey);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr wc_curve25519_new(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -235,11 +426,30 @@ namespace wolfSSL.CSharp
private extern static int wc_curve25519_import_private_raw(IntPtr privKey, IntPtr pubKey, IntPtr key);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_curve25519_export_private_raw(IntPtr key, IntPtr outPrivKey, IntPtr outPubKey);
#endif
/********************************
* AES-GCM
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private extern static int wc_AesDelete(IntPtr aes, IntPtr aes_p);
[DllImport(wolfssl_dll)]
private extern static int wc_AesFree(IntPtr aes);
[DllImport(wolfssl_dll)]
private extern static int wc_AesInit(IntPtr aes, IntPtr heap, int devId);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmInit(IntPtr aes, IntPtr key, uint len, IntPtr iv, uint ivSz);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmSetKey(IntPtr aes, IntPtr key, uint len);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmEncrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
[DllImport(wolfssl_dll)]
private extern static int wc_AesGcmDecrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_AesNew(IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -256,11 +466,28 @@ namespace wolfSSL.CSharp
private extern static int wc_AesGcmEncrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_AesGcmDecrypt(IntPtr aes, IntPtr output, IntPtr input, uint sz, IntPtr iv, uint ivSz, IntPtr authTag, uint authTagSz, IntPtr authIn, uint authInSz);
#endif
/********************************
* HASH
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll)]
private extern static int wc_HashDelete(IntPtr hash, IntPtr hash_p);
[DllImport(wolfssl_dll)]
private extern static int wc_HashInit(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll)]
private extern static int wc_HashUpdate(IntPtr hash, uint hashType, IntPtr data, uint dataSz);
[DllImport(wolfssl_dll)]
private extern static int wc_HashFinal(IntPtr hash, uint hashType, IntPtr output);
[DllImport(wolfssl_dll)]
private extern static int wc_HashFree(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll)]
private extern static int wc_HashGetDigestSize(uint hashType);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_HashNew(uint hashType, IntPtr heap, int devId, IntPtr result_code);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
@ -275,15 +502,21 @@ namespace wolfSSL.CSharp
private extern static int wc_HashFree(IntPtr hash, uint hashType);
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static int wc_HashGetDigestSize(uint hashType);
#endif
/********************************
* Logging
*/
#if WindowsCE
[DllImport(wolfssl_dll)]
private extern static IntPtr wc_GetErrorString(int error);
public delegate void loggingCb(int lvl, string msg);
#else
[DllImport(wolfssl_dll, CallingConvention = CallingConvention.Cdecl)]
private extern static IntPtr wc_GetErrorString(int error);
public delegate void loggingCb(int lvl, StringBuilder msg);
#endif
private static loggingCb internal_log;
/// <summary>
@ -291,6 +524,15 @@ namespace wolfSSL.CSharp
/// </summary>
/// <param name="lvl">Level of log message</param>
/// <param name="msg">Message to log</param>
#if WindowsCE
private static void log(int lvl, string msg)
{
/* if log is not set then print nothing */
if (internal_log == null)
return;
internal_log(lvl, msg);
}
#else
private static void log(int lvl, string msg)
{
/* if log is not set then print nothing */
@ -299,6 +541,7 @@ namespace wolfSSL.CSharp
StringBuilder ptr = new StringBuilder(msg);
internal_log(lvl, ptr);
}
#endif
/********************************
@ -312,7 +555,7 @@ namespace wolfSSL.CSharp
public static readonly int OTHER_LOG = 4;
public static readonly int INVALID_DEVID = -2;
public static readonly int ECC_MAX_SIG_SIZE = 141; /* ECC max sig size */
public static readonly int ECC_KEY_SIZE = 32; /* ECC key size */
public static readonly int ECC_KEY_SIZE = 32; /* ECC key size */
public static readonly int MAX_ECIES_TEST_SZ = 200; /* ECIES max sig size */
public static readonly int ED25519_SIG_SIZE = 64; /* ED25519 pub + priv */
public static readonly int ED25519_KEY_SIZE = 32; /* Private key only */
@ -1645,7 +1888,7 @@ namespace wolfSSL.CSharp
if (inMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(inMsgPtr);
if (outMsgPtr != IntPtr.Zero) Marshal.FreeHGlobal(outMsgPtr);
}
return ret;
}
@ -1900,11 +2143,11 @@ namespace wolfSSL.CSharp
**********************************************************************/
/// <summary>
/// Initialize an ED25519 key.
/// </summary>
/// <param name="key">Buffer to receive the initialized key</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519InitKey(out IntPtr key)
/// Initialize an ED25519 key.
/// </summary>
/// <param name="key">Buffer to receive the initialized key</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int Ed25519InitKey(out IntPtr key)
{
key = IntPtr.Zero;
try
@ -1966,7 +2209,7 @@ namespace wolfSSL.CSharp
}
catch (Exception ex)
{
Console.WriteLine($"Exception in EdImportPublic: {ex.Message}");
Console.WriteLine("Exception in EdImportPublic: " + ex.Message);
return EXCEPTION_E;
}
@ -2425,15 +2668,17 @@ namespace wolfSSL.CSharp
return publicKey;
}
/// <summary>
/// Export both private and public keys from a Curve25519 key structure
/// </summary>
/// <param name="key">Curve25519 key structure</param>
/// <returns>A tuple containing the private key and public key as byte arrays</returns>
public static (byte[] privateKey, byte[] publicKey) Curve25519ExportKeyRaw(IntPtr key)
/// <param name="privateKey">returned raw private key as byte array</param>
/// <param name="publicKey">returned raw public key as byte array</param>
public static void Curve25519ExportKeyRaw(IntPtr key, out byte[] privateKey, out byte[] publicKey)
{
byte[] privateKey = new byte[ED25519_KEY_SIZE];
byte[] publicKey = new byte[ED25519_PUB_KEY_SIZE];
privateKey = new byte[ED25519_KEY_SIZE];
publicKey = new byte[ED25519_PUB_KEY_SIZE];
uint privSize = (uint)privateKey.Length;
uint pubSize = (uint)publicKey.Length;
int ret = wc_curve25519_export_key_raw(key, privateKey, ref privSize, publicKey, ref pubSize);
@ -2441,7 +2686,7 @@ namespace wolfSSL.CSharp
{
throw new Exception("Failed to export Curve25519 keys. Error code: " + ret);
}
return (privateKey, publicKey);
return;
}
/* END RAW Curve25519 */
@ -2470,9 +2715,9 @@ namespace wolfSSL.CSharp
}
}
catch (Exception e)
catch (Exception ex)
{
Console.WriteLine($"AES context creation failed: {e.Message}");
Console.WriteLine("AES context creation failed: " + ex.Message);
}
return aesPtr;
@ -2498,7 +2743,7 @@ namespace wolfSSL.CSharp
ret = wc_AesGcmSetKey(aes, keyPtr, (uint)key.Length);
if (ret != 0)
{
throw new Exception($"AES-GCM initialization failed with error code {ret}");
throw new Exception("AES-GCM initialization failed with error code ret = " + ret.ToString());
}
}
finally
@ -2534,7 +2779,7 @@ namespace wolfSSL.CSharp
ret = wc_AesGcmInit(aes, keyPtr, (uint)key.Length, ivPtr, (uint)iv.Length);
if (ret != 0)
{
throw new Exception($"AES-GCM initialization failed with error code {ret}");
throw new Exception("AES-GCM initialization failed with error code ret = " + ret.ToString());
}
}
finally
@ -2557,7 +2802,7 @@ namespace wolfSSL.CSharp
/// <param name="authTag">Buffer to receive the authentication tag</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext,
byte[] ciphertext, byte[] authTag, byte[] addAuth = null)
byte[] ciphertext, byte[] authTag, byte[] addAuth)
{
int ret;
IntPtr ivPtr = IntPtr.Zero;
@ -2613,6 +2858,11 @@ namespace wolfSSL.CSharp
return ret;
}
public static int AesGcmEncrypt(IntPtr aes, byte[] iv, byte[] plaintext,
byte[] ciphertext, byte[] authTag)
{
return AesGcmEncrypt(aes, iv, plaintext, ciphertext, null);
}
/// <summary>
/// Decrypt data using AES-GCM
@ -2624,7 +2874,7 @@ namespace wolfSSL.CSharp
/// <param name="authTag">Authentication tag for verification</param>
/// <returns>0 on success, otherwise an error code</returns>
public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext,
byte[] plaintext, byte[] authTag, byte[] addAuth = null)
byte[] plaintext, byte[] authTag, byte[] addAuth)
{
int ret;
IntPtr ivPtr = IntPtr.Zero;
@ -2680,6 +2930,11 @@ namespace wolfSSL.CSharp
return ret;
}
public static int AesGcmDecrypt(IntPtr aes, byte[] iv, byte[] ciphertext,
byte[] plaintext, byte[] authTag)
{
return AesGcmDecrypt(aes, iv, ciphertext, plaintext, authTag, null);
}
/// <summary>
/// Free AES-GCM context
@ -2747,7 +3002,7 @@ namespace wolfSSL.CSharp
ret = wc_HashInit(hash, hashType);
if (ret != 0)
{
throw new Exception($"Failed to initialize hash context. Error code: {ret}");
throw new Exception("Failed to initialize hash context. Error code: ret = " + ret.ToString());
}
}
catch (Exception e)
@ -2791,7 +3046,7 @@ namespace wolfSSL.CSharp
ret = wc_HashUpdate(hash, hashType, dataPtr, (uint)data.Length);
if (ret != 0)
{
throw new Exception($"Failed to update hash. Error code: {ret}");
throw new Exception("Failed to update hash. Error code: ret = " + ret.ToString());
}
}
catch (Exception e)
@ -2837,7 +3092,7 @@ namespace wolfSSL.CSharp
ret = wc_HashFinal(hash, hashType, outputPtr);
if (ret != 0)
{
throw new Exception($"Failed to finalize hash. Error code: {ret}");
throw new Exception("Failed to finalize hash. Error code: ret = " + ret.ToString());
}
Marshal.Copy(outputPtr, output, 0, hashSize);
@ -2877,7 +3132,7 @@ namespace wolfSSL.CSharp
hash = IntPtr.Zero;
if (ret != 0)
{
throw new Exception($"Failed to free hash context. Error code: {ret}");
throw new Exception("Failed to free hash context. Error code: ret = " + ret.ToString());
}
}
catch (Exception e)
@ -2938,7 +3193,7 @@ namespace wolfSSL.CSharp
try
{
IntPtr errStr = wc_GetErrorString(error);
return Marshal.PtrToStringAnsi(errStr);
return wolfssl.PtrToStringAnsi(errStr);
}
catch (Exception e)
{

File diff suppressed because it is too large Load Diff