Skip to content

Commit 6dd9615

Browse files
committed
Add missing hpke.c to project. Add overload to support internal ephemeral key generation
1 parent 78f7ad4 commit 6dd9615

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

wrapper/CSharp/wolfCrypt-Test/wolfCrypt-Test.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,28 @@ private static void hpke_test()
976976
throw new Exception("Decrypted text does not match original plaintext");
977977
}
978978
Console.WriteLine("HPKE Base mode test PASSED.");
979+
980+
/* Test convenience overload (no ephemeral key) */
981+
Console.WriteLine("Testing HpkeSealBase convenience overload...");
982+
byte[] encCiphertext2 = wolfcrypt.HpkeSealBase(hpke, receiverKey,
983+
info, aad, plaintext, kem);
984+
if (encCiphertext2 == null)
985+
{
986+
throw new Exception("HpkeSealBase (convenience) failed");
987+
}
988+
Console.WriteLine($"HpkeSealBase convenience passed. Output length: {encCiphertext2.Length}");
989+
990+
byte[] decrypted2 = wolfcrypt.HpkeOpenBase(hpke, receiverKey,
991+
encCiphertext2, info, aad, plaintext.Length);
992+
if (decrypted2 == null)
993+
{
994+
throw new Exception("HpkeOpenBase (convenience) failed");
995+
}
996+
if (!wolfcrypt.ByteArrayVerify(plaintext, decrypted2))
997+
{
998+
throw new Exception("Convenience seal/open: decrypted text does not match");
999+
}
1000+
Console.WriteLine("HPKE convenience overload test PASSED.");
9791001
}
9801002
finally
9811003
{

wrapper/CSharp/wolfSSL_CSharp/wolfCrypt.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3302,10 +3302,7 @@ public static IntPtr HpkeInit(HpkeKem kem, HpkeKdf kdf, HpkeAead aead)
33023302
}
33033303

33043304
/* Zero the memory */
3305-
for (int i = 0; i < HPKE_STRUCT_SZ; i++)
3306-
{
3307-
Marshal.WriteByte(hpke, i, 0);
3308-
}
3305+
Marshal.Copy(new byte[HPKE_STRUCT_SZ], 0, hpke, HPKE_STRUCT_SZ);
33093306

33103307
int ret = wc_HpkeInit(hpke, (int)kem, (int)kdf, (int)aead, IntPtr.Zero);
33113308
if (ret != 0)
@@ -3537,6 +3534,48 @@ public static byte[] HpkeSealBase(IntPtr hpke, IntPtr ephemeralKey, IntPtr recei
35373534
}
35383535
}
35393536

3537+
/// <summary>
3538+
/// Convenience SingleShot seal (encrypt) using HPKE Base mode.
3539+
/// Generates an ephemeral keypair internally so the caller does not
3540+
/// need to manage one.
3541+
/// Returns enc||ciphertext as a single byte array.
3542+
/// </summary>
3543+
/// <param name="hpke">HPKE context from HpkeInit()</param>
3544+
/// <param name="receiverKey">Receiver public key</param>
3545+
/// <param name="info">Info context bytes (can be null)</param>
3546+
/// <param name="aad">Additional authenticated data (can be null)</param>
3547+
/// <param name="plaintext">Plaintext to encrypt</param>
3548+
/// <param name="kem">KEM used (needed to free the ephemeral key)</param>
3549+
/// <returns>enc||ciphertext byte array or null on failure</returns>
3550+
public static byte[] HpkeSealBase(IntPtr hpke, IntPtr receiverKey,
3551+
byte[] info, byte[] aad, byte[] plaintext, HpkeKem kem)
3552+
{
3553+
IntPtr ephemeralKey = IntPtr.Zero;
3554+
3555+
try
3556+
{
3557+
ephemeralKey = HpkeGenerateKeyPair(hpke);
3558+
if (ephemeralKey == IntPtr.Zero)
3559+
{
3560+
log(ERROR_LOG, "HPKE seal base: ephemeral keygen failed");
3561+
return null;
3562+
}
3563+
3564+
return HpkeSealBase(hpke, ephemeralKey, receiverKey,
3565+
info, aad, plaintext);
3566+
}
3567+
catch (Exception e)
3568+
{
3569+
log(ERROR_LOG, "HPKE seal base exception " + e.ToString());
3570+
return null;
3571+
}
3572+
finally
3573+
{
3574+
if (ephemeralKey != IntPtr.Zero)
3575+
HpkeFreeKey(hpke, ephemeralKey, kem);
3576+
}
3577+
}
3578+
35403579
/// <summary>
35413580
/// SingleShot open (decrypt) using HPKE Base mode.
35423581
/// Takes the full enc||ciphertext blob returned by HpkeSealBase.

wrapper/CSharp/wolfssl.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@
317317
<ClCompile Include="..\..\wolfcrypt\src\ge_low_mem.c" />
318318
<ClCompile Include="..\..\wolfcrypt\src\ge_operations.c" />
319319
<ClCompile Include="..\..\wolfcrypt\src\hash.c" />
320+
<ClCompile Include="..\..\wolfcrypt\src\hpke.c" />
320321
<ClCompile Include="..\..\wolfcrypt\src\hmac.c" />
321322
<ClCompile Include="..\..\wolfcrypt\src\integer.c" />
322323
<ClCompile Include="..\..\wolfcrypt\src\kdf.c" />
@@ -332,6 +333,7 @@
332333
<ClCompile Include="..\..\wolfcrypt\src\poly1305.c" />
333334
<ClCompile Include="..\..\wolfcrypt\src\pwdbased.c" />
334335
<ClCompile Include="..\..\wolfcrypt\src\random.c" />
336+
<ClCompile Include="..\..\wolfcrypt\src\rng_bank.c" />
335337
<ClCompile Include="..\..\wolfcrypt\src\rc2.c" />
336338
<ClCompile Include="..\..\wolfcrypt\src\ripemd.c" />
337339
<ClCompile Include="..\..\wolfcrypt\src\rsa.c" />

0 commit comments

Comments
 (0)