<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Daniel Stuart on Medium]]></title>
        <description><![CDATA[Stories by Daniel Stuart on Medium]]></description>
        <link>https://medium.com/@daniel.stuart14?source=rss-957f823c750------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Z1cjabe4JbpKuL1y</url>
            <title>Stories by Daniel Stuart on Medium</title>
            <link>https://medium.com/@daniel.stuart14?source=rss-957f823c750------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 10:13:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@daniel.stuart14/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Embive: A Secure Code Sandbox for Tiny Devices]]></title>
            <link>https://itnext.io/embive-a-secure-code-sandbox-for-tiny-devices-aceab0d1ccaf?source=rss-957f823c750------2</link>
            <guid isPermaLink="false">https://medium.com/p/aceab0d1ccaf</guid>
            <category><![CDATA[risc-v]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[virtual-machine]]></category>
            <category><![CDATA[embedded-systems]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[Daniel Stuart]]></dc:creator>
            <pubDate>Mon, 21 Jul 2025 03:05:38 GMT</pubDate>
            <atom:updated>2025-07-22T19:01:24.295Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*FOFk3UWx1k4UuEB6FCEklA.png" /></figure><p><a href="https://github.com/embive/embive">Embive</a> is an interpreter/virtual-machine that leverages <a href="https://riscv.org/">RISC-V</a> bytecode, enabling sandboxed code execution on tiny devices (e.g. microcontrollers).</p><h3>Why?</h3><p>As an embedded developer, I’ve often envied how other programming fields could rely on the OS to safeguard their systems, where features like like memory isolation, out-of-memory (OOM) protection, and dynamic loading are standard.</p><p>In firmware development, these features are rarely available. The code typically runs as a single monolithic binary, often with only a basic RTOS providing minimal preemptive scheduling.</p><p>Over the years, the embedded ecosystem has advanced, introducing tools like stack usage analysis, hardware-based memory protection, and A/B OTA updates to improve safety. Still, none of these measures have matched the level of protection and peace of mind provided by higher-level operating systems.</p><p>Today, cutting-edge technologies like <a href="https://www.rust-lang.org/">Rust</a>’s memory and thread safety, <a href="https://embassy.dev/">Embassy</a>’s single-stack multitasking, and <a href="https://github.com/knurling-rs/flip-link">flip-link</a>’s stack overflow protection are finally bringing real safety to embedded systems. Yet one challenge remains: How can we dynamically update code on an embedded device without disrupting the core firmware functionality?</p><p>While some options exist, all of them suffer from one or more limitations that make them impractical for embedded use:</p><ul><li><a href="https://www.icculus.org/~phaethon/q3mc/q3vm_specs.html"><strong>Q3VM</strong></a>: It uses a custom compiler that hasn’t been updated in a long time, not even supporting C99. Requires heap allocation.</li><li><a href="https://webassembly.org/"><strong>WebAssembly</strong></a>: While there are embeddable engines (like <a href="https://github.com/bytecodealliance/wasm-micro-runtime">WAMR</a>, <a href="https://github.com/wasmi-labs/wasmi">Wasmi</a>, and <a href="https://github.com/explodingcamera/tinywasm">TinyWasm</a>), they all require heap allocation and are relatively complex.</li><li><a href="https://github.com/compuphase/pawn"><strong>Pawn</strong></a>: Supports only programs made with pawn script, a typeless C-like language with limited use.</li></ul><h3>How?</h3><p>Embive supports the RISC-V <strong>RV32IMAC </strong>instruction-set, making it compatible with many readily available languages and toolchains. It currently has templates for guest code written in <a href="https://github.com/embive/embive-c-template">C/C++</a>, <a href="https://github.com/embive/embive-nim-template">Nim</a>, and <a href="https://github.com/embive/embive-rust-template">Rust</a>.</p><p>For better performance on the targeted device, Embive uses a two-stage execution model, transpiling the bytecode ahead-of-time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3G1hP0VmG0W8jfsqrMXutg.png" /></figure><h4>Transpilation</h4><p>This step converts a RISC-V ELF file to an optimized bytecode binary. These are the optimizations done:</p><p><strong>1. Opcode and Function Merging</strong><br>RISC-V instructions often share opcodes, relying on function fields to distinguish between them. Embive combines opcodes and function fields into a single unified value:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dzB0sGwAEN8aQtNGRy9TQQ.png" /></figure><p><strong>2. Immediate Value Reordering<br></strong>RISC-V immediate values are encoded with bit ordering optimized for hardware decoding, minimizing circuit complexity by reusing logic across instructions. Embive reorders these bits into an ordered layout:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*okZyrOK4N9TklWnpJySq2A.png" /></figure><p><strong>3. Sequential Instruction Encoding<br></strong>RISC-V encoding leave gaps between instruction opcodes, especially in smaller ISAs or those without extensions. Embive reassigns merged opcodes sequentially:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FEjkosgjqSPnQ26hQSqyPQ.png" /></figure><p><strong>4. Compressed Register Expansion<br></strong>Compressed RISC-V instructions (C extension) use 3-bit register indexes due to space constraints, requiring expansion during decoding. Embive statically expands these registers wherever possible:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LqKNzFrP1feDPrIsSxYiOA.png" /></figure><h4>Interpretation</h4><p>This step executes the bytecode using a register-based virtual machine. These are the features available:</p><p><strong>1.</strong> <strong>Memory Isolation<br></strong>All the memory (both executable and read/write) is gated by Embive, allowing access only to the region specified during initialization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/1*kVU9YIP-cSch49A55Xx-Kg.png" /></figure><p><strong>2. Guest &lt;-&gt; Host Communication<br></strong>The guest and host code can communicate through syscalls and interrupts. Syscalls allow the guest to call functions on the host, while interrupts allow the host to trigger code execution on the guest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*-xN9rzwmRl8cENsBw5yhSw.png" /></figure><p><strong>3. Resource limiting<br></strong>Embive allows a configurable amount of instructions to be executed before yielding, making it possible to restrict the amount of CPU time the guest code may have.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*Ns-oUgZD9UaU5JU-lBTPyg.png" /></figure><h3>Finally</h3><p>Embive is implemented in Rust but designed for cross-platform compatibility, offering straightforward integration with C/C++ projects through <a href="https://github.com/corrosion-rs/corrosion">Corrosion</a> and providing Python bindings via <a href="https://github.com/embive/pyembive">pyembive</a>.</p><p>And if you are curious about its performance, here is a Fibonacci benchmark made against some WASM engines (Wasmi is the most performant WASM interpreter currently available):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*CqxO3tSRarnBSoLgkyF3BQ.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aceab0d1ccaf" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/embive-a-secure-code-sandbox-for-tiny-devices-aceab0d1ccaf">Embive: A Secure Code Sandbox for Tiny Devices</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rust: The joy of safe zero-copy parsers]]></title>
            <link>https://itnext.io/rust-the-joy-of-safe-zero-copy-parsers-8c8581db8ab2?source=rss-957f823c750------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c8581db8ab2</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[zero-copy]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Daniel Stuart]]></dc:creator>
            <pubDate>Mon, 25 Sep 2023 03:16:07 GMT</pubDate>
            <atom:updated>2023-12-10T01:33:49.764Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xUJoQN3fyP6AVaiJ-1iE4Q.png" /></figure><p>In the context of computer science, parsers are programs or algorithms that can translate a given bulk of data into some kind of structured information. When talking about inter-device communication (e.g.: How this article has been sent to your device), parsers are used to convert the received data into something the device can process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*Y0pYtSmEVoeYlBU9atKTAg.png" /><figcaption>An example of how an HTML website is processed by your computer. Author: <a href="https://www.erwinhofman.com/blog/parsing-and-rendering-process-simplified/">Erwin Hofman</a></figcaption></figure><p>Bearing that in mind, it’s quite easy to understand the importance of such algorithms, as well as how their efficiency may be a critical part of the overall performance of a system. That being said, one of the ways of optimizing such programs is the use of a technique called zero-copy.</p><p>A zero-copy parser is a piece of code that can transform the received data into its structured form, without having to copy its contents into new buffers during this procedure. As such, these kinds of parsers differ from others when parts of the data have to be translated into a collection of values (e.g.: Strings and arrays); as non-zero-copy programs would allocate a new region in the heap and then copy the respective part of the received data into it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*fMkmQDkBwyz8GEgUfEs5Qg.jpeg" /></figure><p>As an example of a very basic zero-copy parser, I created a C program that parses an array of bytes to a pre-defined struct. The program expects the received data to have a header byte followed by a string.</p><pre>struct ParsedData {<br>  uint8_t header;<br>  char *payload;<br>};<br><br>void parse_buffer(uint8_t *buffer, struct ParsedData *parsed_data) {<br>  parsed_data-&gt;header = buffer[0];<br>  parsed_data-&gt;payload = (char *)&amp;buffer[1];<br>};</pre><p>The code is quite simple, with the primitive value (header) and the pointer to the array of chars (payload) being moved into the struct. Note here that while some may call this procedure a copy, it isn’t allocating new memory, and is just executing simple move instructions with the values and pointers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*_ojx7w7uoxewAUEssxRLdg.png" /><figcaption>Decompiled parse_buffer function with Ghidra (Linux x86_64 (GCC), -O3 optimization)</figcaption></figure><p>So now, let’s put our parser to use. The following program allocates a buffer and simulates receiving some data. After that, we parse it using the aforementioned function and then print the payload.</p><pre>#include &lt;stdint.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;string.h&gt;<br><br>struct ParsedData {<br>  uint8_t header;<br>  char *payload;<br>};<br><br>void parse_buffer(uint8_t *buffer, struct ParsedData *parsed_data) {<br>  parsed_data-&gt;header = buffer[0];<br>  parsed_data-&gt;payload = (char *)&amp;buffer[1];<br>};<br><br>void get_data(uint8_t *buffer) {<br>  const uint8_t data[] = {255, &#39;t&#39;, &#39;e&#39;, &#39;s&#39;, &#39;t&#39;};<br>  memcpy(buffer, data, sizeof(data));<br>}<br><br>int main(void) {<br>  // Alloc buffer for received data<br>  uint8_t *buffer = malloc(1024); // We&#39;ll ignore if the pointer is NULL<br><br>  // Simulate getting data from somewhere else (Ex: Socket)<br>  get_data(buffer);<br><br>  // Parse buffer into ParsedData struct<br>  struct ParsedData parsed_data;<br>  parse_buffer(buffer, &amp;parsed_data);<br><br>  // Print payload content<br>  printf(&quot;%s\n&quot;, parsed_data.payload);<br><br>  free(buffer);<br><br>  return 0;<br>}</pre><p>If we execute the code, we’ll see that the parser works and the payload is printed correctly. But… Where is the Rust part?</p><p>And here comes one of the biggest issues with the C/C++ language: memory safety, or the lack thereof. See, while it worked on this specific scenario, our code had a couple of underlying issues:</p><ul><li>We are using a pointer to a dynamically allocated buffer, which could cause undefined behavior if we tried to read the payload after the buffer had been freed.</li><li>Variables are mutable by default in C, which means that the data in the original buffer could be tampered with, resulting in the wrong payload being read from the struct later.</li></ul><p>For the sake of argument, I changed the code to also print the payload after tampering with the buffer, as well as after freeing it:</p><pre> // Print payload content<br>  printf(&quot;Original: %s\n&quot;, parsed_data.payload);<br><br>  // Tamper with the original buffer<br>  buffer[1] = &#39;j&#39;;<br><br>  // Print payload content<br>  printf(&quot;Tampered: %s\n&quot;, parsed_data.payload);<br><br>  free(buffer);<br><br>  // Print payload content<br>  printf(&quot;Freed: %s\n&quot;, parsed_data.payload);</pre><p>Here is the result of this change:</p><pre>Original: test<br>Tampered: jest<br>Freed: </pre><p>While there are ways to prevent this kind of issue, it is 100% up to the developer, with it getting a lot harder when multi-threading is added to the mix. This is one of the reasons why zero-copy parsers may be avoided by some, resorting to always copying the data.</p><p>Rust solves the two previously mentioned issues with its borrow checker. The borrow checker works by verifying a <a href="https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html">set of rules</a> at compile time, restricting unsafe practices. These are the rules and what they solve:</p><ul><li><strong>Ownership</strong>: Only a single variable can own a value at a time, with the value being freed after this variable goes out of scope.<br>- <strong>Solves</strong>: Compiler can deterministically define when to free a value, allowing for safe implicit freeing without a garbage collector.</li><li><strong>Borrowing</strong>: A value may be referenced (borrowed) many times as immutable, but only one time if referenced as mutable.<br>- <strong>Solves</strong>: If a value is referenced as immutable, it is guaranteed that it won&#39;t be modified by another entity while still borrowed.</li><li><strong>Lifetimes</strong>: A value can only be referenced (borrowed) if it lives at least as long as the variable that references it.<br>- <strong>Solves</strong>: References are guaranteed to always be valid (no dangling pointers).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bNR5tqtadbrlrY-MTjjFOg.png" /></figure><p>After all of this explanation, let’s now create a non-zero-copy parser with Rust and then walk through converting it into a zero-copy one. This way, we can not only truly understand the differences between them (benchmarks included!), but also the challenges that it involves.</p><p>After defining the struct, I created a function that parses an array of bytes into it; moving the header value, creating a new vector with the payload bytes, and then converting the vector into a string. The function <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#method.to_vec"><strong>to_vec</strong></a> is responsible for allocating the heap and copying the data from the array slice, while the String method <a href="https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8"><strong>from_utf8</strong></a> moves the ownership of the newly created Vec and uses its data in place:</p><pre>pub struct ParsedData {<br>    pub header: u8,<br>    pub payload: String,<br>}<br><br>impl ParsedData {<br>    pub fn parse(data: &amp;[u8]) -&gt; ParsedData {<br>        let header = data[0];<br>    <br>        let payload_bytes = data[1..data.len()].to_vec();<br>        let payload = String::from_utf8(payload_bytes).unwrap();<br>    <br>        ParsedData {<br>            header,<br>            payload,<br>        }<br>    }<br>}</pre><p>To make it zero-copy we have to remove the <a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#method.to_vec"><strong>to_vec</strong></a><strong> </strong>function, as well as use a reference to a <a href="https://doc.rust-lang.org/std/primitive.str.html"><strong>str</strong></a><strong> </strong>instead of using a <a href="https://doc.rust-lang.org/std/string/index.html"><strong>String</strong></a>. If we just did that, we would have the following code:</p><pre>pub struct ParsedData {<br>    pub header: u8,<br>    pub payload: &amp;str,<br>}<br><br>impl ParsedData {<br>    pub fn parse(data: &amp;[u8]) -&gt; ParsedData {<br>        let header = data[0];<br>    <br>        let payload = std::str::from_utf8(&amp;data[1..data.len()]).unwrap();<br>    <br>        ParsedData {<br>            header,<br>            payload,<br>        }<br>    }<br>}</pre><p>But it isn’t that easy, we have to remember that references can only happen if the compiler can determine that the referenced value will live long enough. Let’s solve this by adding an explicit lifetime to our code, making sure that the original data must outlive its reference inside our struct.</p><pre>pub struct ParsedData&lt;&#39;a&gt; {<br>    pub header: u8,<br>    pub payload: &amp;&#39;a str,<br>}<br><br>impl&lt;&#39;a&gt; ParsedData&lt;&#39;a&gt; {<br>    pub fn parse(data: &amp;&#39;a [u8]) -&gt; ParsedData&lt;&#39;a&gt; {<br>        let header = data[0];<br>    <br>        let payload = std::str::from_utf8(&amp;data[1..data.len()]).unwrap();<br>    <br>        ParsedData {<br>            header,<br>            payload,<br>        }<br>    }<br>}</pre><p>Thanks to <a href="https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision">lifetime elision</a>, the code above can be simplified by removing some explicit annotations that are not needed for the lifetime to be deterministic (this won’t always be the case):</p><pre>pub struct ParsedData&lt;&#39;a&gt; {<br>    pub header: u8,<br>    pub payload: &amp;&#39;a str,<br>}<br><br>impl ParsedData&lt;&#39;_&gt; {<br>    pub fn parse(data: &amp;[u8]) -&gt; ParsedData {<br>        let header = data[0];<br>    <br>        let payload = std::str::from_utf8(&amp;data[1..data.len()]).unwrap();<br>    <br>        ParsedData {<br>            header,<br>            payload,<br>        }<br>    }<br>}</pre><p>Let’s now create a program that uses our parser, in the same way as we did for the C version of it:</p><pre>pub struct ParsedData&lt;&#39;a&gt; {<br>    pub header: u8,<br>    pub payload: &amp;&#39;a str,<br>}<br><br>impl ParsedData&lt;&#39;_&gt; {<br>    pub fn parse(data: &amp;[u8]) -&gt; ParsedData {<br>        let header = data[0];<br>    <br>        let payload = std::str::from_utf8(&amp;data[1..data.len()]).unwrap();<br>    <br>        ParsedData {<br>            header,<br>            payload,<br>        }<br>    }<br>}<br><br>fn get_data() -&gt; Vec&lt;u8&gt; {<br>    const DATA: [u8; 5] = [255, &#39;t&#39; as u8, &#39;e&#39; as u8, &#39;s&#39; as u8, &#39;t&#39; as u8];<br>    DATA.to_vec() // Return dynamically allocated array (Vector)<br>}<br><br>fn main() {<br>    // Simulate getting data from somewhere else (Ex: Socket) (Rust allows us to return a object)<br>    let buffer = get_data();<br><br>    // Parse buffer into ParsedData struct<br>    let parsed_data = ParsedData::parse(&amp;buffer);<br><br>    // Print payload content<br>    println!(&quot;{}&quot;, parsed_data.payload);<br>}</pre><p>Now that we have our complete program, we can try to simulate the same issues that we found with the C code. Let’s start by trying to tamper with the original buffer, this is the new main function:</p><pre>fn main() {<br>    // Simulate getting data from somewhere else (Ex: Socket) (Rust allows us to return a object)<br>    let mut buffer = get_data(); // Make it mutable for testing purposes<br><br>    // Parse buffer into ParsedData struct<br>    let parsed_data = ParsedData::parse(&amp;buffer);<br><br>    // Print payload content<br>    println!(&quot;Original: {}&quot;, parsed_data.payload);<br><br>    // Tamper with the original buffer<br>    buffer[1] = &#39;j&#39; as u8;<br><br>    // Print payload content<br>    println!(&quot;Tempered: {}&quot;, parsed_data.payload);<br>}</pre><p>If we try to compile this code, we’ll get the error: <strong>cannot borrow `buffer` as mutable because it is also borrowed as immutable</strong>. This happens because no other borrow can happen if a value is borrowed as mutable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*5b7pF-wVPQJg5sYyQY4h2A.png" /><figcaption>Rust compiler error: cannot borrow `buffer` as mutable because it is also borrowed as immutable.</figcaption></figure><p>Now, let’s try the second issue we found with the C code: accessing the payload after the original buffer had been freed. This is the new main function (Thanks <a href="https://medium.com/u/acf2db67352d">Riccardo Sagramoni</a> for a better example):</p><pre>fn main() {<br>    let parsed_data = { // Create a scope to simulate the buffer getting freed (RAII)<br>        // Simulate getting data from somewhere else (Ex: Socket) (Rust allows us to return a object)<br>        let buffer = get_data();<br><br>        // Parse buffer into ParsedData struct<br>        let parsed_data = ParsedData::parse(&amp;buffer);<br>        <br>        // Print payload content<br>        println!(&quot;{}&quot;, parsed_data.payload);<br>        <br>        parsed_data<br>    };<br>    <br>    println!(&quot;Freed: {}&quot;, parsed_data.payload);<br>}</pre><p>If we also try to compile this code, we’ll get a new error: <strong>`buffer` does not live long enough</strong>. This happens because while <strong>buffer </strong>is getting out-of-scope, parsed_data is having its ownership moved to a variable outside of it. As so, Rust’s <a href="https://doc.rust-lang.org/rust-by-example/scope/raii.html">RAII</a> will free <strong>buffer</strong>, and then the lifetime we explicitly defined between the original data and our struct will be broken.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BNhvllgOv3wF6ZO236Y9Og.png" /><figcaption>Rust compiler error: `buffer` does not live long enough</figcaption></figure><p>With that in mind, while these concepts of lifetimes and borrowing may be a little hard to understand at first, they allow us to have a zero-copy parser without the memory safety downsides that may come when using other languages.</p><p>I created a simple benchmark using the <a href="https://bheisler.github.io/criterion.rs/book/getting_started.html">Criterion</a> library, comparing both the copy and zero-copy parsers. The tests were done with payloads of 1, 10, 100, 500 and 1000 characters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UxBUvbvqt_3IgczMkYboIw.png" /><figcaption>Graph showing the average time of parsing for each algorithm and payload length.</figcaption></figure><p>As seen in the graph above, the results are staggering. Not only the zero-copy was much faster at all payload sizes, but it was also able to parse 1000 characters in just 70% of the time the copy algorithm took to parse a single one.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c8581db8ab2" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/rust-the-joy-of-safe-zero-copy-parsers-8c8581db8ab2">Rust: The joy of safe zero-copy parsers</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Porting nRF BLE Sniffer to a third-party dongle]]></title>
            <link>https://medium.com/@daniel.stuart14/porting-nrf-ble-sniffer-to-a-third-party-dongle-5bfd98165f1?source=rss-957f823c750------2</link>
            <guid isPermaLink="false">https://medium.com/p/5bfd98165f1</guid>
            <category><![CDATA[sniffing]]></category>
            <category><![CDATA[bluetooth]]></category>
            <category><![CDATA[hardware]]></category>
            <category><![CDATA[firmware]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Daniel Stuart]]></dc:creator>
            <pubDate>Sat, 02 Sep 2023 23:01:20 GMT</pubDate>
            <atom:updated>2024-09-08T01:42:45.930Z</atom:updated>
            <content:encoded><![CDATA[<p>Sniffers are a crucial tool for debugging and reverse engineering communication protocols, allowing an individual to visualize the data being shared between devices, as well as the respective timings.</p><p>When regarding Bluetooth Low Energy (BLE), Nordic’s “nRF Sniffer for Bluetooth LE” is one of the most famous sniffers out there. Its high performance and relatively low price make it a great choice for both beginners and skilled professionals. Nordic’s tool is available for many of their development kits, with the closed-source sniffing firmware being freely <a href="https://www.nordicsemi.com/Products/Development-tools/nrf-sniffer-for-bluetooth-le">distributed by them</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ydCiIpjqrOkSzJTw3p6CHg.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*6CLr1PLmgxQwYF6KwfHAzQ.jpeg" /><figcaption>nRF52 DK (left) and nRF52840 Dongle (right), two Nordic boards that are compatible with the sniffing firmware</figcaption></figure><p>One of the main issues with Nordic’s development kits is their form factor: usually bulky, requiring USB cables, and having the PCB exposed. The nRF52840 Dongle solves the two former aspects by being a USB stick, but its non-compliant USB connector (It lacks the usual USB metal shell) makes the connection quite finicky. All these issues make them not a viable option when working in the field, where a more portable solution is needed.</p><p>With that in mind, I started to search on the internet for a better alternative. While I didn’t find another low-cost and feature-rich sniffer, I had an idea that could work: finding a device with a Nordic chipset and using their firmware on it.</p><p>After some time, I found on AliExpress the perfect device for the test: a dongle based on the nRF52840 chipset with a proper USB connector, while being a lot smaller. The device is made by a chinese company called Holyiot and is named <a href="https://www.aliexpress.com/item/1005004744954818.html?spm=a2g0o.order_detail.order_detail_item.3.eb8bf19cOqh9dg">Holyiot-22046-USB</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*fRvxf77aKq8_kIoy4wTLiA.jpeg" /><figcaption>Holyiot nRF52840 dongle (Holyiot-22046-USB)</figcaption></figure><p>The schematic was even available on the product listing, allowing me to compare it with the original Nordic dongle and verify if it was indeed compatible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*PB3hDGu3w8oUwTspfwZT1A.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-N_CtbXza9tZxWv0h45pg.png" /><figcaption>Schematics for Holyiot (Left) and Nordic (Right) dongles</figcaption></figure><p>After analyzing it, no major differences were spotted, with the exception that the Holyiot dongle was missing the DC/DC circuits, using the less energy-efficient (but cheaper) linear regulators embedded into the Nordic chipset. This, however, wasn’t a lost cause, as I was able to patch Nordic’s firmware to not use the DC/DC regulators.</p><p>As specified by Nordic’s <a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fpower.html&amp;anchor=concept">documentation</a>, the nRF52840 chipset has two regulators embedded into it, with each being configurable to use an internal LDO or a DC/DC with external components. The first regulator (REG0) is responsible for stepping down the USB voltage (5v) to the selected GPIO voltage (from 1.8v to 3.3v), while the second regulator (REG1) takes the GPIO voltage and steps it down to 1.3v (used to power the chipset).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JHu--HYYQVe0SaFIVyAo9g.png" /><figcaption>nRF52840 regulators in both configurations: LDO (left) and DC/DC (right)</figcaption></figure><p>For each regulator, a register inside the nRF52840 power peripheral is responsible for enabling the DC/DC circuit. This is what we need to change on Nordic’s firmware to make it compatible with our dongle. The responsible registers are specified by Nordic as DCDCEN0 and DCDCEN, with them being at addresses 0x40000580 and 0x40000578, respectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*fMbcvMoZ5D8iSSyz13nv3w.png" /><figcaption>DCDCEN and DCDCEN0 register and their offsets from the POWER peripheral address (0x4000000)</figcaption></figure><p>I then proceeded to download the latest nRF Sniffer firmware (4.1.1 at the time) and disassemble the NRF52840 Dongle firmware with <a href="https://ghidra-sre.org/">GHidra</a> (be sure to select the correct architecture). After that, I was able to search for the registers’ addresses and verify that two functions were indeed writing to them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*-4zuMLVURbn8HnMaEw-u3g.png" /><figcaption>Both registers and the respective functions writing to them</figcaption></figure><p>The first function (at address 0xb0fc) writes a value of 0x1 to DCDCEN and thus enables the DC/DC on REG1. To solve that, I patched the instruction at address 0xb0f6 to load a zero instead of a one into the register.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vPKG-XszGP9cpbfH960p6Q.png" /><figcaption>Disassembled code at address 0xb0fc. Original code (left) and patched code (right).</figcaption></figure><p>The second function (at address 0xbc80) is a little more complex, it receives a parameter and uses it to to define a value for both DCDCEN and DCDCEN0. Only the first bit of the parameter was used to set DCDCEN, being extracted by a bitwise AND with the value of one (0x1). I then patched the instruction at address 0xbca8 to instead make the bitwise AND with the value of zero (0x0), thus always loading a zero into the register. After that, I also patched the instruction at address 0xbcb2, making it use the same previous bitwise result to load into the DCDCEN0 register.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HfrIgc12eUavBWM7SQR4bw.png" /><figcaption>Disassembled code at address 0xbc80. Original code (left) and patched code (right).</figcaption></figure><p>After all of that was done, I exported the new firmware and flashed it into the Holyiot dongle with the <a href="https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-desktop">nRF Connect for Desktop</a> application. And, for much of my joy, it worked! The device showed up as “nRF Sniffer for Bluetooth LE” and I was able to use it with <a href="https://www.wireshark.org/">Wireshark</a> as if it was any other supported Nordic development kit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*wJNTqReuinMANCozOfZ2Ow.png" /><figcaption>Wireshark view of sniffed BLE packets. MAC addresses have been redacted.</figcaption></figure><p>With that in mind, porting it to other devices may be harder depending on the differences between it and one of Nordic’s development kits. As an example, missing the 32.768 KHz crystal may need more changes than only setting a couple of registers to the right value.</p><p>If you have this exact dongle and want to use the Nordic BLE sniffer firmware, I created a <a href="https://github.com/danielstuart14/nrfsniffer_patcher">repository with a firmware patcher</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5bfd98165f1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>