<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[wolfSSL - Embedded SSL Library — Exchanging enc Data with BoringSSL [UPDATED]]]></title>
		<link>https://www.wolfssl.com/forums/topic1980-exchanging-enc-data-with-boringssl-updated.html</link>
		<atom:link href="https://www.wolfssl.com/forums/feed-rss-topic1980.xml" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Exchanging enc Data with BoringSSL [UPDATED].]]></description>
		<lastBuildDate>Fri, 17 Mar 2023 19:24:13 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Exchanging enc Data with BoringSSL [UPDATED]]]></title>
			<link>https://www.wolfssl.com/forums/post7028.html#p7028</link>
			<description><![CDATA[<p>After further testing and investigation, I&#039;ve discovered that the encryption and decryption works just fine. I&#039;m actually trying to port parts of this code from adb to wolfssl:<br /><a href="https://cs.android.com/android/platform/superproject/+/master:packages/modules/adb/pairing_auth/aes_128_gcm.cpp">https://cs.android.com/android/platform … 28_gcm.cpp</a> </p><p>I&#039;ve zeroed down the problem to my copy pasta of SPAKE2 from boringssl, needed for pairing authentication in adb. The encryption/decryption must be failing because the key received from <strong>SPAKE2_process_msg</strong> is wrong.</p>]]></description>
			<author><![CDATA[null@example.com (johnot)]]></author>
			<pubDate>Fri, 17 Mar 2023 19:24:13 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post7028.html#p7028</guid>
		</item>
		<item>
			<title><![CDATA[Re: Exchanging enc Data with BoringSSL [UPDATED]]]></title>
			<link>https://www.wolfssl.com/forums/post7025.html#p7025</link>
			<description><![CDATA[<p>Hi johnot,</p><p>your ported code looks almost correct. The only thing you are missing is the AAD. Even though your AAD is 0-length, it still needs to be applied. To do this, you need to add this step after the Init but before the first Update.<br /></p><div class="codebox"><pre><code>EVP_EncryptUpdate(encryptContext.get(), nullptr, &amp;written_sz, nullptr, 0);</code></pre></div><p>This will apply the 0-length AAD and you should achieve the correct output.</p><p>Sincerely<br />Juliusz</p>]]></description>
			<author><![CDATA[null@example.com (juliusz)]]></author>
			<pubDate>Fri, 17 Mar 2023 13:57:09 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post7025.html#p7025</guid>
		</item>
		<item>
			<title><![CDATA[Exchanging enc Data with BoringSSL [UPDATED]]]></title>
			<link>https://www.wolfssl.com/forums/post7020.html#p7020</link>
			<description><![CDATA[<p>Hi there,</p><p>I&#039;m trying to replicate the functions </p><div class="codebox"><pre><code> EVP_AEAD_CTX_seal </code></pre></div><p> and </p><div class="codebox"><pre><code> EVP_AEAD_CTX_open </code></pre></div><p> in order to send (and receive) data to a server using BoringSSL. However, the server fails to decrypt the data. It seems like wolfSSL doesn&#039;t have any directly compatible functions. Here&#039;s my approach:</p><p><strong>[UPDATE]: I posted decryption code earlier instead of encryption code, my bad.</strong></p><div class="codebox"><pre><code>// Original code using BoringSSL
size_t Encrypt(bssl::ScopedEVP_AEAD_CTX context, const uint8_t* in, size_t in_len, uint8_t* out, size_t out_len)
{
    size_t written_sz;
    std::vector&lt;uint8_t&gt; nonce(EVP_AEAD_nonce_length(EVP_AEAD_CTX_aead(context.get())), 0);

    if (!EVP_AEAD_CTX_seal(context.get(), out, &amp;written_sz, out_len, nonce.data(),
                                        nonce.size(), in, in_len, nullptr, 0)) {
        std::cerr  &lt;&lt; &quot;Failed to encrypt&quot; &lt;&lt; std::endl;
        return 0;
    }
    
    return written_sz;
}</code></pre></div><p>and</p><div class="codebox"><pre><code>// Code ported to use wolfSSL
// key_g = { preset key }
size_t Decrypt(wolfssl::UniquePtr&lt;WOLFSSL_EVP_CIPHER_CTX&gt; encryptContext,
                                   const uint8_t* in, size_t in_len, uint8_t* out, size_t out_len)
{
    int written_sz, ret, taglen = 16;
    std::vector&lt;uint8_t&gt; nonce(EVP_CIPHER_iv_length(EVP_aes_128_gcm()), 0);

    ret = EVP_EncryptInit(encryptContext.get(), EVP_aes_128_gcm(), key_g, nonce.data());

    assert(ret == SSL_SUCCESS))

    ret = EVP_EncryptUpdate(encryptContext.get(), out, &amp;written_sz, in, (int)in_len);

    if (ret != WOLFSSL_SUCCESS){
        std::cerr  &lt;&lt; &quot;Failed to encrypt&quot; &lt;&lt; std::endl;
        return 0;
    }

    ret = EVP_EncryptFinal(encryptContext.get(), out, &amp;written_sz);

    if (ret != WOLFSSL_SUCCESS || written_sz != in_len){
        std::cerr  &lt;&lt; &quot;Failed to encrypt&quot; &lt;&lt; std::endl;
        return 0;
    }

    unsigned char tag[taglen]
    // the last 16 bytes are the tag. Copy the tag into those last 16 bytes as boringssl does
    assert((in_len + taglen) == out_len);
    assert(EVP_CIPHER_CTX_ctrl(encryptContext.get(), EVP_CTRL_GCM_GET_TAG, taglen, (void*)tag) == SSL_SUCCESS);
    memcpy((void*)(out + in_len), tag, taglen);

    return written_sz + taglen; // bssl returns sizeof(encryptedData) + taglen
}</code></pre></div><p>I tried analyzing the encrypted data for bssl and figured out that authTag, which is 16 bytes (depending on cipher I suppose), is appended to the end of the encryption data. I tried following a similar scheme since wolfSSL didn&#039;t have the functions out of the box.</p><p>However, the server fails to decrypt the data. Can anybody help out?</p><p>Thanks in advance</p>]]></description>
			<author><![CDATA[null@example.com (johnot)]]></author>
			<pubDate>Thu, 16 Mar 2023 23:07:46 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post7020.html#p7020</guid>
		</item>
	</channel>
</rss>
